Annotate

util/muc.lua @ 1349:1d6c4a6c75e5

util.muc: Fixed error occuring on room leave
author Waqas Hussain <waqas20@gmail.com>
date Sun, 14 Jun 2009 16:28:01 +0500
parent 1170:4845372a2c03
child 1350:ea43c371228a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1145
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 -- Prosody IM v0.4
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local datamanager = require "util.datamanager";
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local datetime = require "util.datetime";
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local jid_split = require "util.jid".split;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local jid_bare = require "util.jid".bare;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 local st = require "util.stanza";
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local log = require "util.logger".init("mod_muc");
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 local multitable_new = require "util.multitable".new;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local t_insert, t_remove = table.insert, table.remove;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 local muc_domain = nil; --module:get_host();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local history_length = 20;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 ------------
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local function filter_xmlns_from_array(array, filters)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 local count = 0;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 for i=#array,1,-1 do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local attr = array[i].attr;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if filters[attr and attr.xmlns] then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 t_remove(array, i);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 count = count + 1;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return count;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local function filter_xmlns_from_stanza(stanza, filters)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if filters then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 return stanza, filter_xmlns_from_array(stanza, filters);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 return stanza, 0;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 local function get_filtered_presence(stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 local kickable_error_conditions = {
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 ["gone"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 ["internal-server-error"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 ["item-not-found"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 ["jid-malformed"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 ["recipient-unavailable"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 ["redirect"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 ["remote-server-not-found"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 ["remote-server-timeout"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 ["service-unavailable"] = true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 };
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 local function get_kickable_error(stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 for _, tag in ipairs(stanza.tags) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 for _, cond in ipairs(tag.tags) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 return kickable_error_conditions[cond.name] and cond.name;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 return true; -- malformed error message
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 return true; -- malformed error message
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 local function getUsingPath(stanza, path, getText)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local tag = stanza;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 for _, name in ipairs(path) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 if type(tag) ~= 'table' then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 tag = tag:child_with_name(name);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 if tag and getText then tag = table.concat(tag); end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 return tag;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 local function getTag(stanza, path) return getUsingPath(stanza, path); end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 local function getText(stanza, path) return getUsingPath(stanza, path, true); end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 -----------
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 --[[function get_room_disco_info(room, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 :tag("identity", {category='conference', type='text', name=room._data["name"]):up()
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 function get_room_disco_items(room, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 end -- TODO allow non-private rooms]]
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 --
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 local function room_broadcast_presence(room, stanza, code, nick)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 stanza = get_filtered_presence(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 local data = room._participants[stanza.attr.from];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 if code then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 stanza:tag("status", {code=code}):up();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 local me;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
103 for occupant, o_data in pairs(room._participants) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 if occupant ~= stanza.attr.from then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 for jid in pairs(o_data.sessions) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 stanza.attr.to = jid;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 room:route_stanza(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 me = o_data;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 if me then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 stanza:tag("status", {code='110'});
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 for jid in pairs(me.sessions) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 stanza.attr.to = jid;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 room:route_stanza(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 end
1170
4845372a2c03 util.muc: Made internal methods local
Waqas Hussain <waqas20@gmail.com>
parents: 1145
diff changeset
121 local function room_broadcast_message(room, stanza, historic)
1145
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 for occupant, o_data in pairs(room._participants) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 for jid in pairs(o_data.sessions) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 stanza.attr.to = jid;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 room:route_stanza(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 if historic then -- add to history
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 local history = room._data['history'];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 if not history then history = {}; room._data['history'] = history; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 -- stanza = st.clone(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 t_insert(history, st.clone(st.preserialize(stanza)));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 while #history > history_length do t_remove(history, 1) end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 local function room_send_occupant_list(room, to)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 local current_nick = room._jid_nick[to];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 for occupant, o_data in pairs(room._participants) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 if occupant ~= current_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 pres.attr.to, pres.attr.from = to, occupant;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148 room:route_stanza(pres);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
150 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
151 end
1170
4845372a2c03 util.muc: Made internal methods local
Waqas Hussain <waqas20@gmail.com>
parents: 1145
diff changeset
152 local function room_send_history(room, to)
1145
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
153 local history = room._data['history']; -- send discussion history
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 if history then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 for _, msg in ipairs(history) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 msg = st.deserialize(msg);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157 msg.attr.to=to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
158 room:route_stanza(msg);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
159 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
160 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
161 if room._data['subject'] then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
162 room:route_stanza(st.message({type='groupchat', from=room, to=to}):tag("subject"):text(room._data['subject']));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
163 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
164 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 local function room_get_disco_info(self, stanza) end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 local function room_get_disco_items(self, stanza) end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
168 local function room_set_subject(room, current_nick, room, subject)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 -- TODO check nick's authority
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 if subject == "" then subject = nil; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 room._data['subject'] = subject;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172 local msg = st.message({type='groupchat', from=current_nick})
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 :tag('subject'):text(subject):up();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 room_broadcast_message(room, msg, false);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 return true;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 local function room_handle_to_occupant(self, origin, stanza) -- PM, vCards, etc
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 local from, to = stanza.attr.from, stanza.attr.to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
180 local room = jid_bare(to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 local current_nick = self._jid_nick[from];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182 local type = stanza.attr.type;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
184 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
185 if stanza.name == "presence" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
186 local pr = get_filtered_presence(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
187 pr.attr.from = current_nick;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
188 if type == "error" then -- error, kick em out!
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
189 if current_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
190 log("debug", "kicking %s from %s", current_nick, room);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
191 room_handle_to_occupant(self, origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
192 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
193 elseif type == "unavailable" then -- unavailable
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
194 if current_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
195 log("debug", "%s leaving %s", current_nick, room);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196 local data = self._participants[current_nick];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 data.role = 'none';
1349
1d6c4a6c75e5 util.muc: Fixed error occuring on room leave
Waqas Hussain <waqas20@gmail.com>
parents: 1170
diff changeset
198 room_broadcast_presence(self, pr);
1145
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 self._participants[current_nick] = nil;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
200 self._jid_nick[from] = nil;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
201 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 elseif not type then -- available
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 if current_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
204 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
205 if current_nick == to then -- simple presence
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 log("debug", "%s broadcasted presence", current_nick);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 self._participants[current_nick].sessions[from] = pr;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
208 room_broadcast_presence(self, pr);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209 else -- change nick
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
210 if self._participants[to] then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
211 log("debug", "%s couldn't change nick", current_nick);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
212 origin.send(st.error_reply(stanza, "cancel", "conflict"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
213 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
214 local data = self._participants[current_nick];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
215 local to_nick = select(3, jid_split(to));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
216 if to_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
217 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
218 local p = st.presence({type='unavailable', from=current_nick});
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
219 room_broadcast_presence(self, p, '303', to_nick);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
220 self._participants[current_nick] = nil;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
221 self._participants[to] = data;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
222 self._jid_nick[from] = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
223 pr.attr.from = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
224 self._participants[to].sessions[from] = pr;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
225 room_broadcast_presence(self, pr);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
226 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
227 --TODO malformed-jid
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
228 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
229 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
230 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
231 --else -- possible rejoin
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
232 -- log("debug", "%s had connection replaced", current_nick);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
233 -- handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection'):up()); -- send unavailable
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
234 -- handle_to_occupant(origin, stanza); -- resend available
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
235 --end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
236 else -- enter room
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
237 local new_nick = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
238 if self._participants[to] then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
239 new_nick = nil;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
240 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
241 if not new_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
242 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
243 origin.send(st.error_reply(stanza, "cancel", "conflict"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
244 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
245 log("debug", "%s joining as %s", from, to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
246 local data;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
247 -- if not rooms:get(room) and not rooms_info:get(room) then -- new room
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
248 -- rooms_info:set(room, 'name', (jid_split(room)));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
249 -- data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
250 -- end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
251 if not data then -- new occupant
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
252 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
253 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
254 self._participants[to] = data;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
255 self._jid_nick[from] = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
256 room_send_occupant_list(self, from);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
257 pr.attr.from = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
258 room_broadcast_presence(self, pr);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
259 room_send_history(self, from);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
260 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
261 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262 elseif type ~= 'result' then -- bad type
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
263 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
264 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
265 elseif not current_nick and type ~= "error" then -- not in room
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
266 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
267 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 origin.send(st.error_reply(stanza, "modify", "bad-request"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
269 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
270 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 room_handle_to_occupant(self, origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 else -- private stanza
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 local o_data = self._participants[to];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
274 if o_data then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
275 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
276 local jid = o_data.jid;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
277 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
278 stanza.attr.to, stanza.attr.from = jid, current_nick;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
279 self:route_stanza(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
280 elseif type ~= "error" and type ~= "result" then -- recipient not in room
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
281 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
282 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
283 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
285
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
286 local function room_handle_to_room(self, origin, stanza) -- presence changes and groupchat messages, along with disco/etc
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
287 local type = stanza.attr.type;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
288 if stanza.name == "iq" and type == "get" then -- disco requests
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
289 local xmlns = stanza.tags[1].attr.xmlns;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
290 if xmlns == "http://jabber.org/protocol/disco#info" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
291 origin.send(room_get_disco_info(self, stanza));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
292 elseif xmlns == "http://jabber.org/protocol/disco#items" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
293 origin.send(room_get_disco_items(self, stanza));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
294 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
295 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
296 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
297 elseif stanza.name == "message" and type == "groupchat" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
298 local from, to = stanza.attr.from, stanza.attr.to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
299 local room = jid_bare(to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
300 local current_nick = self._jid_nick[from];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
301 if not current_nick then -- not in room
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
302 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
303 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
304 local from = stanza.attr.from;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
305 stanza.attr.from = current_nick;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
306 local subject = getText(stanza, {"subject"});
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
307 if subject then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
308 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
309 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
310 room_broadcast_message(self, stanza, true);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
311 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
312 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
313 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
314 local to = stanza.attr.to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
315 local current_nick = self._jid_nick[stanza.attr.from];
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
316 if current_nick then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
317 stanza.attr.to = current_nick;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
318 room_handle_to_occupant(origin, stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
319 stanza.attr.to = to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
320 elseif type ~= "error" and type ~= "result" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
321 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
322 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
323 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from]
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
324 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
325 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 local _from, _to = stanza.attr.from, stanza.attr.to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
327 local _invitee = stanza.tags[1].tags[1].attr.to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 stanza.attr.from, stanza.attr.to = _to, _invitee;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
329 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 self:route_stanza(stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
332 stanza.attr.from, stanza.attr.to = _from, _to;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
333 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
334 if type == "error" or type == "result" then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
339 local function room_handle_stanza(self, origin, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
341 if to_resource and not to_node then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
342 if type == "error" or type == "result" then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
343 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
344 elseif to_resource then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
345 room_handle_to_occupant(self, origin, stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
346 elseif to_node then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
347 room_handle_to_room(self, origin, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
348 else -- to the main muc domain
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
349 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
350 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
351
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
352 module "muc"
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
354 function new_room(jid)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 return {
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
356 jid = jid;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
357 handle_stanza = room_handle_stanza;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
358 set_subject = room_set_subject;
1170
4845372a2c03 util.muc: Made internal methods local
Waqas Hussain <waqas20@gmail.com>
parents: 1145
diff changeset
359 route_stanza = function(room, stanza) end; -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end
1145
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
360 _jid_nick = {};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
361 _participants = {};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
362 _data = {};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
363 }
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
364 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
365
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
366 return _M;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
367
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
368 --[[function get_disco_info(stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
369 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
370 :tag("identity", {category='conference', type='text', name=muc_name}):up()
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
371 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
372 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
373 function get_disco_items(stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
374 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
375 for room in pairs(rooms_info:get()) do
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
376 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
377 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
378 return reply; -- TODO cache disco reply
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
379 end]]
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
380
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
381 --[[function handle_to_domain(origin, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
382 local type = stanza.attr.type;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
383 if type == "error" or type == "result" then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
384 if stanza.name == "iq" and type == "get" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
385 local xmlns = stanza.tags[1].attr.xmlns;
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
386 if xmlns == "http://jabber.org/protocol/disco#info" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
387 origin.send(get_disco_info(stanza));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
388 elseif xmlns == "http://jabber.org/protocol/disco#items" then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
389 origin.send(get_disco_items(stanza));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
390 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
391 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
392 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
393 else
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
394 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
395 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
396 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
397
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
398 register_component(muc_domain, function(origin, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
399 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
400 if to_resource and not to_node then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
401 if type == "error" or type == "result" then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
402 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
403 elseif to_resource then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
404 handle_to_occupant(origin, stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
405 elseif to_node then
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
406 handle_to_room(origin, stanza)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
407 else -- to the main muc domain
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
408 if type == "error" or type == "result" then return; end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
409 handle_to_domain(origin, stanza);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
410 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
411 end);]]
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
412
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
413 --[[module.unload = function()
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
414 deregister_component(muc_domain);
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
415 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
416 module.save = function()
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
417 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
418 end
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
419 module.restore = function(data)
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
420 rooms.data, jid_nick.data, rooms_info.data, persist_list =
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
421 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
06051191913d util.muc: A MUC library - initial commit
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
422 end]]