3 local register_component = require "core.componentmanager".register_component;
4 local deregister_component = require "core.componentmanager".deregister_component;
5 local jid_split = require "util.jid".split;
6 local jid_bare = require "util.jid".bare;
7 local st = require "util.stanza";
8 local log = require "util.logger".init("mod_muc");
9 local multitable_new = require "util.multitable".new;
10 local t_insert, t_remove = table.insert, table.remove;
12 if module:get_host_type() ~= "component" then
13 error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
16 local muc_domain = module:get_host();
17 local muc_name = "MUCMUCMUC!!!";
18 local history_length = 20;
21 -- occupant_room_nick -> data
24 -- jid = occupant's real jid
25 local rooms = multitable_new();
27 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
30 -- name - the room's friendly name
31 -- subject - the room's subject
32 -- non-anonymous = true|nil
33 -- persistent = true|nil
34 -- history = {preserialized stanzas}
35 local rooms_info = multitable_new();
37 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {};
38 for room in pairs(persist_list) do
39 rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil);
44 function filter_xmlns_from_array(array, filters)
47 local attr = array[i].attr;
48 if filters[attr and attr.xmlns] then
55 function filter_xmlns_from_stanza(stanza, filters)
57 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
58 return stanza, filter_xmlns_from_array(stanza, filters);
63 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
64 function get_filtered_presence(stanza)
65 return filter_xmlns_from_stanza(st.deserialize(st.preserialize(stanza)), presence_filters);
67 function getUsingPath(stanza, path, getText)
69 for _, name in ipairs(path) do
70 if type(tag) ~= 'table' then return; end
71 tag = tag:child_with_name(name);
73 if tag and getText then tag = table.concat(tag); end
76 function getTag(stanza, path) return getUsingPath(stanza, path); end
77 function getText(stanza, path) return getUsingPath(stanza, path, true); end
79 function get_disco_info(stanza)
80 return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
81 :tag("identity", {category='conference', type='text', name=muc_name}):up()
82 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
84 function get_disco_items(stanza)
85 local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
86 for room in pairs(rooms_info:get()) do
87 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
89 return reply; -- TODO cache disco reply
91 function get_room_disco_info(stanza)
92 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
93 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up()
94 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
96 function get_room_disco_items(stanza)
97 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
98 end -- TODO allow non-private rooms
100 function save_room(room)
101 local persistent = rooms_info:get(room, 'persistent');
103 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
105 if persistent ~= persist_list[room] then
106 if not persistent then
107 datamanager.store(room, muc_domain, 'rooms', nil);
109 persist_list[room] = persistent;
110 datamanager.store(nil, muc_domain, 'room_list', persist_list);
114 function set_subject(current_nick, room, subject)
115 -- TODO check nick's authority
116 if subject == "" then subject = nil; end
117 rooms_info:set(room, 'subject', subject);
119 local msg = st.message({type='groupchat', from=from})
120 :tag('subject'):text(subject):up();
121 broadcast_message_stanza(room, msg, false);
122 --broadcast_message(current_nick, room, subject or "", nil);
126 function broadcast_presence(type, from, room, code, newnick)
127 local data = rooms:get(room, from);
128 local stanza = st.presence({type=type, from=from})
129 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
130 :tag("item", {affiliation=data.affiliation, role=data.role, nick = newnick}):up();
132 stanza:tag("status", {code=code}):up();
135 local r = rooms:get(room);
137 for occupant, o_data in pairs(r) do
138 if occupant ~= from then
139 stanza.attr.to = o_data.jid;
140 core_route_stanza(component, stanza);
147 stanza:tag("status", {code='110'});
149 core_route_stanza(component, stanza);
152 function broadcast_message(from, room, subject, body)
153 local stanza = st.message({type='groupchat', from=from});
154 if subject then stanza:tag('subject'):text(subject):up(); end
155 if body then stanza:tag('body'):text(body):up(); end
156 local r = rooms:get(room);
158 for occupant, o_data in pairs(r) do
159 stanza.attr.to = o_data.jid;
160 core_route_stanza(component, stanza);
162 if not subject and body then -- add to history
163 local history = rooms_info:get(room, 'history');
164 if not history then history = {}; rooms_info:set(room, 'history', history); end
165 -- stanza = st.deserialize(st.preserialize(stanza));
166 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
167 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
168 t_insert(history, st.preserialize(stanza));
169 while #history > history_length do t_remove(history, 1) end
173 function broadcast_message_stanza(room, stanza, historic)
174 local r = rooms:get(room);
176 for occupant, o_data in pairs(r) do
177 for jid in pairs(o_data.sessions) do
178 stanza.attr.to = jid;
179 core_route_stanza(component, stanza);
182 if historic then -- add to history
183 local history = rooms_info:get(room, 'history');
184 if not history then history = {}; rooms_info:set(room, 'history', history); end
185 -- stanza = st.deserialize(st.preserialize(stanza));
186 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
187 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
188 t_insert(history, st.preserialize(stanza));
189 while #history > history_length do t_remove(history, 1) end
193 function broadcast_presence_stanza(room, stanza, code, nick)
194 stanza = get_filtered_presence(stanza);
195 local data = rooms:get(room, stanza.attr.from);
196 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
197 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
199 stanza:tag("status", {code=code}):up();
202 local r = rooms:get(room);
204 for occupant, o_data in pairs(r) do
205 if occupant ~= stanza.attr.from then
206 for jid in pairs(o_data.sessions) do
207 stanza.attr.to = jid;
208 core_route_stanza(component, stanza);
216 stanza:tag("status", {code='110'});
217 for jid in pairs(me.sessions) do
218 stanza.attr.to = jid;
219 core_route_stanza(component, stanza);
224 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
225 local from, to = stanza.attr.from, stanza.attr.to;
226 local room = jid_bare(to);
227 local current_nick = jid_nick:get(from, room);
228 local type = stanza.attr.type;
229 if stanza.name == "presence" then
230 local pr = get_filtered_presence(stanza);
232 if type == "error" then -- error, kick em out!
234 local data = rooms:get(room, to);
236 local pr = st.presence({type='unavailable', from=current_nick}):tag('status'):text('This participant is kicked from the room because he sent an error presence'):up()
237 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
238 :tag("item", {affiliation=data.affiliation, role=data.role}):up();
239 broadcast_presence_stanza(room, pr);
240 --broadcast_presence('unavailable', to, room); -- TODO also add <status>This participant is kicked from the room because he sent an error presence: badformed error stanza</status>
241 rooms:remove(room, to);
242 jid_nick:remove(from, room);
244 elseif type == "unavailable" then -- unavailable
246 local data = rooms:get(room, to);
248 broadcast_presence_stanza(room, pr);
249 --broadcast_presence('unavailable', to, room);
250 rooms:remove(room, to);
251 jid_nick:remove(from, room);
253 elseif not type then -- available
255 if current_nick == to then -- simple presence
256 broadcast_presence_stanza(room, pr);
257 -- FIXME check if something was filtered. if it was, then user may be rejoining
259 if rooms:get(room, to) then
260 origin.send(st.error_reply(stanza, "cancel", "conflict"));
262 local data = rooms:get(room, current_nick);
263 local to_nick = select(3, jid_split(to));
265 local p = st.presence({type='unavailable', from=current_nick});
266 --[[:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
267 :tag('item', {affiliation=data.affiliation, role=data.role, nick=to_nick}):up()
268 :tag('status', {code='303'});]]
269 broadcast_presence_stanza(room, p, '303', to_nick);
270 --broadcast_presence('unavailable', current_nick, room, '303', to_nick);
271 rooms:remove(room, current_nick);
272 rooms:set(room, to, data);
273 jid_nick:set(from, room, to);
274 broadcast_presence_stanza(room, pr);
275 --broadcast_presence(nil, to, room, nil);
283 if rooms:get(room, to) then
287 origin.send(st.error_reply(stanza, "cancel", "conflict"));
290 if not rooms:get(room) and not rooms_info:get(room) then -- new room
291 data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
293 if not data then -- new occupant
294 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
296 rooms:set(room, to, data);
297 jid_nick:set(from, room, to);
298 local r = rooms:get(room);
300 for occupant, o_data in pairs(r) do
301 if occupant ~= from then
302 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
303 pres.attr.to, pres.attr.from = from, occupant;
305 --local pres = st.presence({to=from, from=occupant})
306 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
307 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
308 core_route_stanza(component, pres);
312 broadcast_presence_stanza(room, pr);
313 --broadcast_presence(nil, to, room);
314 local history = rooms_info:get(room, 'history'); -- send discussion history
316 for _, msg in ipairs(history) do
317 msg = st.deserialize(msg);
319 core_route_stanza(component, msg);
322 if rooms_info:get(room, 'subject') then
323 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
327 elseif type ~= 'result' then -- bad type
328 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
330 elseif not current_nick then -- not in room
331 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
332 elseif stanza.name == "message" and type == "groupchat" then
333 -- groupchat messages not allowed in PM
334 origin.send(st.error_reply(stanza, "modify", "bad-request"));
335 else -- private stanza
336 local o_data = rooms:get(room, to);
338 stanza.attr.to, stanza.attr.from = o_data.jid, current_nick;
339 core_route_stanza(component, stanza);
340 else -- recipient not in room
341 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
346 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
347 local type = stanza.attr.type;
348 if stanza.name == "iq" and type == "get" then -- disco requests
349 local xmlns = stanza.tags[1].attr.xmlns;
350 if xmlns == "http://jabber.org/protocol/disco#info" then
351 origin.send(get_room_disco_info(stanza));
352 elseif xmlns == "http://jabber.org/protocol/disco#items" then
353 origin.send(get_room_disco_items(stanza));
355 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
357 elseif stanza.name == "message" and type == "groupchat" then
358 local from, to = stanza.attr.from, stanza.attr.to;
359 local room = jid_bare(to);
360 local current_nick = jid_nick:get(from, room);
361 if not current_nick then -- not in room
362 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
364 local from = stanza.attr.from;
365 stanza.attr.from = current_nick;
366 local subject = getText(stanza, {"subject"});
368 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
370 --broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
371 broadcast_message_stanza(room, stanza, true);
374 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
375 local to = stanza.attr.to;
376 local current_nick = jid_nick:get(stanza.attr.from, to);
378 stanza.attr.to = current_nick;
379 handle_to_occupant(origin, stanza);
382 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
385 if type == "error" or type == "result" then return; end
386 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
390 function handle_to_domain(origin, stanza)
391 local type = stanza.attr.type;
392 if type == "error" or type == "result" then return; end
393 if stanza.name == "iq" and type == "get" then
394 local xmlns = stanza.tags[1].attr.xmlns;
395 if xmlns == "http://jabber.org/protocol/disco#info" then
396 origin.send(get_disco_info(stanza));
397 elseif xmlns == "http://jabber.org/protocol/disco#items" then
398 origin.send(get_disco_items(stanza));
400 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
403 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
407 register_component(muc_domain, function(origin, stanza)
408 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
409 if to_resource and not to_node then
410 if type == "error" or type == "result" then return; end
411 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
412 elseif to_resource then
413 handle_to_occupant(origin, stanza);
415 handle_to_room(origin, stanza)
416 else -- to the main muc domain
417 if type == "error" or type == "result" then return; end
418 handle_to_domain(origin, stanza);
422 module.unload = function()
423 deregister_component(muc_domain);
425 module.save = function()
426 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
428 module.restore = function(data)
429 rooms.data, jid_nick.data, rooms_info.data, persist_list =
430 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};