2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local register_component = require "core.componentmanager".register_component;
11 local deregister_component = require "core.componentmanager".deregister_component;
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;
19 if module:get_host_type() ~= "component" then
20 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
23 local muc_domain = module:get_host();
24 local muc_name = "MUCMUCMUC!!!";
25 local history_length = 20;
28 -- occupant_room_nick -> data
31 -- jid = occupant's real jid
32 local rooms = multitable_new();
34 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
37 -- name - the room's friendly name
38 -- subject - the room's subject
39 -- non-anonymous = true|nil
40 -- persistent = true|nil
41 -- history = {preserialized stanzas}
42 local rooms_info = multitable_new();
44 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {};
45 for room in pairs(persist_list) do
46 rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil);
51 function filter_xmlns_from_array(array, filters)
54 local attr = array[i].attr;
55 if filters[attr and attr.xmlns] then
62 function filter_xmlns_from_stanza(stanza, filters)
64 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
65 return stanza, filter_xmlns_from_array(stanza, filters);
70 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
71 function get_filtered_presence(stanza)
72 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
74 local kickable_error_conditions = {
76 ["internal-server-error"] = true;
77 ["item-not-found"] = true;
78 ["jid-malformed"] = true;
79 ["recipient-unavailable"] = true;
81 ["remote-server-not-found"] = true;
82 ["remote-server-timeout"] = true;
83 ["service-unavailable"] = true;
85 function get_kickable_error(stanza)
86 for _, tag in ipairs(stanza.tags) do
87 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then
88 for _, cond in ipairs(tag.tags) do
89 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
90 return kickable_error_conditions[cond.name] and cond.name;
93 return true; -- malformed error message
96 return true; -- malformed error message
98 function getUsingPath(stanza, path, getText)
100 for _, name in ipairs(path) do
101 if type(tag) ~= 'table' then return; end
102 tag = tag:child_with_name(name);
104 if tag and getText then tag = table.concat(tag); end
107 function getTag(stanza, path) return getUsingPath(stanza, path); end
108 function getText(stanza, path) return getUsingPath(stanza, path, true); end
110 function get_disco_info(stanza)
111 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
112 :tag("identity", {category='conference', type='text', name=muc_name}):up()
113 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
115 function get_disco_items(stanza)
116 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
117 for room in pairs(rooms_info:get()) do
118 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
120 return reply; -- TODO cache disco reply
122 function get_room_disco_info(stanza)
123 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
124 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up()
125 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
127 function get_room_disco_items(stanza)
128 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
129 end -- TODO allow non-private rooms
131 function save_room(room)
132 local persistent = rooms_info:get(room, 'persistent');
134 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
136 if persistent ~= persist_list[room] then
137 if not persistent then
138 datamanager.store(room, muc_domain, 'rooms', nil);
140 persist_list[room] = persistent;
141 datamanager.store(nil, muc_domain, 'room_list', persist_list);
145 function set_subject(current_nick, room, subject)
146 -- TODO check nick's authority
147 if subject == "" then subject = nil; end
148 rooms_info:set(room, 'subject', subject);
150 local msg = st.message({type='groupchat', from=current_nick})
151 :tag('subject'):text(subject):up();
152 broadcast_message_stanza(room, msg, false);
156 function broadcast_message_stanza(room, stanza, historic)
157 local r = rooms:get(room);
159 for occupant, o_data in pairs(r) do
160 for jid in pairs(o_data.sessions) do
161 stanza.attr.to = jid;
162 core_route_stanza(component, stanza);
165 if historic then -- add to history
166 local history = rooms_info:get(room, 'history');
167 if not history then history = {}; rooms_info:set(room, 'history', history); end
168 -- stanza = st.clone(stanza);
169 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
170 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
171 t_insert(history, st.clone(st.preserialize(stanza)));
172 while #history > history_length do t_remove(history, 1) end
176 function broadcast_presence_stanza(room, stanza, code, nick)
177 stanza = get_filtered_presence(stanza);
178 local data = rooms:get(room, stanza.attr.from);
179 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
180 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
182 stanza:tag("status", {code=code}):up();
185 local r = rooms:get(room);
187 for occupant, o_data in pairs(r) do
188 if occupant ~= stanza.attr.from then
189 for jid in pairs(o_data.sessions) do
190 stanza.attr.to = jid;
191 core_route_stanza(component, stanza);
199 stanza:tag("status", {code='110'});
200 for jid in pairs(me.sessions) do
201 stanza.attr.to = jid;
202 core_route_stanza(component, stanza);
207 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
208 local from, to = stanza.attr.from, stanza.attr.to;
209 local room = jid_bare(to);
210 local current_nick = jid_nick:get(from, room);
211 local type = stanza.attr.type;
212 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
213 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
214 if stanza.name == "presence" then
215 local pr = get_filtered_presence(stanza);
216 pr.attr.from = current_nick;
217 if type == "error" then -- error, kick em out!
219 log("debug", "kicking %s from %s", current_nick, room);
220 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 presence')); -- send unavailable
222 elseif type == "unavailable" then -- unavailable
224 log("debug", "%s leaving %s", current_nick, room);
225 local data = rooms:get(room, current_nick);
227 broadcast_presence_stanza(room, pr);
228 rooms:remove(room, current_nick);
229 jid_nick:remove(from, room);
231 elseif not type then -- available
233 if #pr == #stanza or current_nick ~= to then
234 if current_nick == to then -- simple presence
235 log("debug", "%s broadcasted presence", current_nick);
236 rooms:get(room, current_nick).sessions[from] = pr;
237 broadcast_presence_stanza(room, pr);
239 if rooms:get(room, to) then
240 log("debug", "%s couldn't change nick", current_nick);
241 origin.send(st.error_reply(stanza, "cancel", "conflict"));
243 local data = rooms:get(room, current_nick);
244 local to_nick = select(3, jid_split(to));
246 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
247 local p = st.presence({type='unavailable', from=current_nick});
248 broadcast_presence_stanza(room, p, '303', to_nick);
249 rooms:remove(room, current_nick);
250 rooms:set(room, to, data);
251 jid_nick:set(from, room, to);
253 rooms:get(room, to).sessions[from] = pr;
254 broadcast_presence_stanza(room, pr);
260 else -- possible rejoin
261 log("debug", "%s had connection replaced", current_nick);
262 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection')); -- send unavailable
263 handle_to_occupant(origin, stanza); -- resend available
267 if rooms:get(room, to) then
271 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
272 origin.send(st.error_reply(stanza, "cancel", "conflict"));
274 log("debug", "%s joining as %s", from, to);
276 if not rooms:get(room) and not rooms_info:get(room) then -- new room
277 data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
279 if not data then -- new occupant
280 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
282 rooms:set(room, to, data);
283 jid_nick:set(from, room, to);
284 local r = rooms:get(room);
286 for occupant, o_data in pairs(r) do
287 if occupant ~= to then
288 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
289 pres.attr.to, pres.attr.from = from, occupant;
290 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
291 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
292 core_route_stanza(component, pres);
297 broadcast_presence_stanza(room, pr);
298 local history = rooms_info:get(room, 'history'); -- send discussion history
300 for _, msg in ipairs(history) do
301 msg = st.deserialize(msg);
303 core_route_stanza(component, msg);
306 if rooms_info:get(room, 'subject') then
307 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
311 elseif type ~= 'result' then -- bad type
312 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
314 elseif not current_nick and type ~= "error" then -- not in room
315 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
316 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
317 origin.send(st.error_reply(stanza, "modify", "bad-request"));
318 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
319 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
320 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
321 else -- private stanza
322 local o_data = rooms:get(room, to);
324 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
325 local jid = o_data.jid;
326 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
327 stanza.attr.to, stanza.attr.from = jid, current_nick;
328 core_route_stanza(component, stanza);
329 elseif type ~= "error" and type ~= "result" then -- recipient not in room
330 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
335 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
336 local type = stanza.attr.type;
337 if stanza.name == "iq" and type == "get" then -- disco requests
338 local xmlns = stanza.tags[1].attr.xmlns;
339 if xmlns == "http://jabber.org/protocol/disco#info" then
340 origin.send(get_room_disco_info(stanza));
341 elseif xmlns == "http://jabber.org/protocol/disco#items" then
342 origin.send(get_room_disco_items(stanza));
344 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
346 elseif stanza.name == "message" and type == "groupchat" then
347 local from, to = stanza.attr.from, stanza.attr.to;
348 local room = jid_bare(to);
349 local current_nick = jid_nick:get(from, room);
350 if not current_nick then -- not in room
351 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
353 local from = stanza.attr.from;
354 stanza.attr.from = current_nick;
355 local subject = getText(stanza, {"subject"});
357 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
359 broadcast_message_stanza(room, stanza, true);
362 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
363 local to = stanza.attr.to;
364 local current_nick = jid_nick:get(stanza.attr.from, to);
366 stanza.attr.to = current_nick;
367 handle_to_occupant(origin, stanza);
369 elseif type ~= "error" and type ~= "result" then
370 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
373 if type == "error" or type == "result" then return; end
374 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
378 function handle_to_domain(origin, stanza)
379 local type = stanza.attr.type;
380 if type == "error" or type == "result" then return; end
381 if stanza.name == "iq" and type == "get" then
382 local xmlns = stanza.tags[1].attr.xmlns;
383 if xmlns == "http://jabber.org/protocol/disco#info" then
384 origin.send(get_disco_info(stanza));
385 elseif xmlns == "http://jabber.org/protocol/disco#items" then
386 origin.send(get_disco_items(stanza));
388 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
391 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
395 register_component(muc_domain, function(origin, stanza)
396 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
397 if to_resource and not to_node then
398 if type == "error" or type == "result" then return; end
399 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
400 elseif to_resource then
401 handle_to_occupant(origin, stanza);
403 handle_to_room(origin, stanza)
404 else -- to the main muc domain
405 if type == "error" or type == "result" then return; end
406 handle_to_domain(origin, stanza);
410 module.unload = function()
411 deregister_component(muc_domain);
413 module.save = function()
414 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
416 module.restore = function(data)
417 rooms.data, jid_nick.data, rooms_info.data, persist_list =
418 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};