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 function getUsingPath(stanza, path, getText)
76 for _, name in ipairs(path) do
77 if type(tag) ~= 'table' then return; end
78 tag = tag:child_with_name(name);
80 if tag and getText then tag = table.concat(tag); end
83 function getTag(stanza, path) return getUsingPath(stanza, path); end
84 function getText(stanza, path) return getUsingPath(stanza, path, true); end
86 function get_disco_info(stanza)
87 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
88 :tag("identity", {category='conference', type='text', name=muc_name}):up()
89 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
91 function get_disco_items(stanza)
92 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
93 for room in pairs(rooms_info:get()) do
94 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
96 return reply; -- TODO cache disco reply
98 function get_room_disco_info(stanza)
99 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
100 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up()
101 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
103 function get_room_disco_items(stanza)
104 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
105 end -- TODO allow non-private rooms
107 function save_room(room)
108 local persistent = rooms_info:get(room, 'persistent');
110 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
112 if persistent ~= persist_list[room] then
113 if not persistent then
114 datamanager.store(room, muc_domain, 'rooms', nil);
116 persist_list[room] = persistent;
117 datamanager.store(nil, muc_domain, 'room_list', persist_list);
121 function set_subject(current_nick, room, subject)
122 -- TODO check nick's authority
123 if subject == "" then subject = nil; end
124 rooms_info:set(room, 'subject', subject);
126 local msg = st.message({type='groupchat', from=current_nick})
127 :tag('subject'):text(subject):up();
128 broadcast_message_stanza(room, msg, false);
132 function broadcast_message_stanza(room, stanza, historic)
133 local r = rooms:get(room);
135 for occupant, o_data in pairs(r) do
136 for jid in pairs(o_data.sessions) do
137 stanza.attr.to = jid;
138 core_route_stanza(component, stanza);
141 if historic then -- add to history
142 local history = rooms_info:get(room, 'history');
143 if not history then history = {}; rooms_info:set(room, 'history', history); end
144 -- stanza = st.clone(stanza);
145 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
146 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
147 t_insert(history, st.clone(st.preserialize(stanza)));
148 while #history > history_length do t_remove(history, 1) end
152 function broadcast_presence_stanza(room, stanza, code, nick)
153 stanza = get_filtered_presence(stanza);
154 local data = rooms:get(room, stanza.attr.from);
155 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
156 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
158 stanza:tag("status", {code=code}):up();
161 local r = rooms:get(room);
163 for occupant, o_data in pairs(r) do
164 if occupant ~= stanza.attr.from then
165 for jid in pairs(o_data.sessions) do
166 stanza.attr.to = jid;
167 core_route_stanza(component, stanza);
175 stanza:tag("status", {code='110'});
176 for jid in pairs(me.sessions) do
177 stanza.attr.to = jid;
178 core_route_stanza(component, stanza);
183 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
184 local from, to = stanza.attr.from, stanza.attr.to;
185 local room = jid_bare(to);
186 local current_nick = jid_nick:get(from, room);
187 local type = stanza.attr.type;
188 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
189 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
190 if stanza.name == "presence" then
191 local pr = get_filtered_presence(stanza);
192 pr.attr.from = current_nick;
193 if type == "error" then -- error, kick em out!
195 log("debug", "kicking %s from %s", current_nick, room);
196 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
198 elseif type == "unavailable" then -- unavailable
200 log("debug", "%s leaving %s", current_nick, room);
201 local data = rooms:get(room, current_nick);
203 broadcast_presence_stanza(room, pr);
204 rooms:remove(room, current_nick);
205 jid_nick:remove(from, room);
207 elseif not type then -- available
209 if #pr == #stanza or current_nick ~= to then
210 if current_nick == to then -- simple presence
211 log("debug", "%s broadcasted presence", current_nick);
212 rooms:get(room, current_nick).sessions[from] = pr;
213 broadcast_presence_stanza(room, pr);
215 if rooms:get(room, to) then
216 log("debug", "%s couldn't change nick", current_nick);
217 origin.send(st.error_reply(stanza, "cancel", "conflict"));
219 local data = rooms:get(room, current_nick);
220 local to_nick = select(3, jid_split(to));
222 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
223 local p = st.presence({type='unavailable', from=current_nick});
224 broadcast_presence_stanza(room, p, '303', to_nick);
225 rooms:remove(room, current_nick);
226 rooms:set(room, to, data);
227 jid_nick:set(from, room, to);
229 rooms:get(room, to).sessions[from] = pr;
230 broadcast_presence_stanza(room, pr);
236 else -- possible rejoin
237 log("debug", "%s had connection replaced", current_nick);
238 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection')); -- send unavailable
239 handle_to_occupant(origin, stanza); -- resend available
243 if rooms:get(room, to) then
247 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
248 origin.send(st.error_reply(stanza, "cancel", "conflict"));
250 log("debug", "%s joining as %s", from, to);
252 if not rooms:get(room) and not rooms_info:get(room) then -- new room
253 data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
255 if not data then -- new occupant
256 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
258 rooms:set(room, to, data);
259 jid_nick:set(from, room, to);
260 local r = rooms:get(room);
262 for occupant, o_data in pairs(r) do
263 if occupant ~= to then
264 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
265 pres.attr.to, pres.attr.from = from, occupant;
266 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
267 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
268 core_route_stanza(component, pres);
273 broadcast_presence_stanza(room, pr);
274 local history = rooms_info:get(room, 'history'); -- send discussion history
276 for _, msg in ipairs(history) do
277 msg = st.deserialize(msg);
279 core_route_stanza(component, msg);
282 if rooms_info:get(room, 'subject') then
283 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
287 elseif type ~= 'result' then -- bad type
288 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
290 elseif not current_nick then -- not in room
291 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
292 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
293 origin.send(st.error_reply(stanza, "modify", "bad-request"));
294 elseif stanza.name == "message" and type == "error" then
295 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
296 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
297 else -- private stanza
298 local o_data = rooms:get(room, to);
300 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
301 local jid = o_data.jid;
302 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
303 stanza.attr.to, stanza.attr.from = jid, current_nick;
304 core_route_stanza(component, stanza);
305 else -- recipient not in room
306 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
311 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
312 local type = stanza.attr.type;
313 if stanza.name == "iq" and type == "get" then -- disco requests
314 local xmlns = stanza.tags[1].attr.xmlns;
315 if xmlns == "http://jabber.org/protocol/disco#info" then
316 origin.send(get_room_disco_info(stanza));
317 elseif xmlns == "http://jabber.org/protocol/disco#items" then
318 origin.send(get_room_disco_items(stanza));
320 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
322 elseif stanza.name == "message" and type == "groupchat" then
323 local from, to = stanza.attr.from, stanza.attr.to;
324 local room = jid_bare(to);
325 local current_nick = jid_nick:get(from, room);
326 if not current_nick then -- not in room
327 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
329 local from = stanza.attr.from;
330 stanza.attr.from = current_nick;
331 local subject = getText(stanza, {"subject"});
333 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
335 broadcast_message_stanza(room, stanza, true);
338 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
339 local to = stanza.attr.to;
340 local current_nick = jid_nick:get(stanza.attr.from, to);
342 stanza.attr.to = current_nick;
343 handle_to_occupant(origin, stanza);
346 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
349 if type == "error" or type == "result" then return; end
350 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
354 function handle_to_domain(origin, stanza)
355 local type = stanza.attr.type;
356 if type == "error" or type == "result" then return; end
357 if stanza.name == "iq" and type == "get" then
358 local xmlns = stanza.tags[1].attr.xmlns;
359 if xmlns == "http://jabber.org/protocol/disco#info" then
360 origin.send(get_disco_info(stanza));
361 elseif xmlns == "http://jabber.org/protocol/disco#items" then
362 origin.send(get_disco_items(stanza));
364 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
367 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
371 register_component(muc_domain, function(origin, stanza)
372 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
373 if to_resource and not to_node then
374 if type == "error" or type == "result" then return; end
375 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
376 elseif to_resource then
377 handle_to_occupant(origin, stanza);
379 handle_to_room(origin, stanza)
380 else -- to the main muc domain
381 if type == "error" or type == "result" then return; end
382 handle_to_domain(origin, stanza);
386 module.unload = function()
387 deregister_component(muc_domain);
389 module.save = function()
390 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
392 module.restore = function(data)
393 rooms.data, jid_nick.data, rooms_info.data, persist_list =
394 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};