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