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.clone(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=current_nick})
120 :tag('subject'):text(subject):up();
121 broadcast_message_stanza(room, msg, false);
125 function broadcast_message_stanza(room, stanza, historic)
126 local r = rooms:get(room);
128 for occupant, o_data in pairs(r) do
129 for jid in pairs(o_data.sessions) do
130 stanza.attr.to = jid;
131 core_route_stanza(component, stanza);
134 if historic then -- add to history
135 local history = rooms_info:get(room, 'history');
136 if not history then history = {}; rooms_info:set(room, 'history', history); end
137 -- stanza = st.clone(stanza);
138 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
139 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
140 t_insert(history, st.clone(st.preserialize(stanza)));
141 while #history > history_length do t_remove(history, 1) end
145 function broadcast_presence_stanza(room, stanza, code, nick)
146 stanza = get_filtered_presence(stanza);
147 local data = rooms:get(room, stanza.attr.from);
148 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
149 :tag("item", {affiliation=data.affiliation, role=data.role, nick=nick}):up();
151 stanza:tag("status", {code=code}):up();
154 local r = rooms:get(room);
156 for occupant, o_data in pairs(r) do
157 if occupant ~= stanza.attr.from then
158 for jid in pairs(o_data.sessions) do
159 stanza.attr.to = jid;
160 core_route_stanza(component, stanza);
168 stanza:tag("status", {code='110'});
169 for jid in pairs(me.sessions) do
170 stanza.attr.to = jid;
171 core_route_stanza(component, stanza);
176 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
177 local from, to = stanza.attr.from, stanza.attr.to;
178 local room = jid_bare(to);
179 local current_nick = jid_nick:get(from, room);
180 local type = stanza.attr.type;
181 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
182 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
183 if stanza.name == "presence" then
184 local pr = get_filtered_presence(stanza);
185 pr.attr.from = current_nick;
186 if type == "error" then -- error, kick em out!
188 log("debug", "kicking %s from %s", current_nick, room);
189 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
191 elseif type == "unavailable" then -- unavailable
193 log("debug", "%s leaving %s", current_nick, room);
194 local data = rooms:get(room, current_nick);
196 broadcast_presence_stanza(room, pr);
197 rooms:remove(room, current_nick);
198 jid_nick:remove(from, room);
200 elseif not type then -- available
202 if #pr == #stanza or current_nick ~= to then
203 if current_nick == to then -- simple presence
204 log("debug", "%s broadcasted presence", current_nick);
205 rooms:get(room, current_nick).sessions[from] = pr;
206 broadcast_presence_stanza(room, pr);
208 if rooms:get(room, to) then
209 log("debug", "%s couldn't change nick", current_nick);
210 origin.send(st.error_reply(stanza, "cancel", "conflict"));
212 local data = rooms:get(room, current_nick);
213 local to_nick = select(3, jid_split(to));
215 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
216 local p = st.presence({type='unavailable', from=current_nick});
217 broadcast_presence_stanza(room, p, '303', to_nick);
218 rooms:remove(room, current_nick);
219 rooms:set(room, to, data);
220 jid_nick:set(from, room, to);
222 rooms:get(room, to).sessions[from] = pr;
223 broadcast_presence_stanza(room, pr);
229 else -- possible rejoin
230 log("debug", "%s had connection replaced", current_nick);
231 handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}):tag('status'):text('Replaced by new connection')); -- send unavailable
232 handle_to_occupant(origin, stanza); -- resend available
236 if rooms:get(room, to) then
240 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
241 origin.send(st.error_reply(stanza, "cancel", "conflict"));
243 log("debug", "%s joining as %s", from, to);
245 if not rooms:get(room) and not rooms_info:get(room) then -- new room
246 data = {affiliation='owner', role='moderator', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
248 if not data then -- new occupant
249 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
251 rooms:set(room, to, data);
252 jid_nick:set(from, room, to);
253 local r = rooms:get(room);
255 for occupant, o_data in pairs(r) do
256 if occupant ~= to then
257 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
258 pres.attr.to, pres.attr.from = from, occupant;
259 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
260 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
261 core_route_stanza(component, pres);
266 broadcast_presence_stanza(room, pr);
267 local history = rooms_info:get(room, 'history'); -- send discussion history
269 for _, msg in ipairs(history) do
270 msg = st.deserialize(msg);
272 core_route_stanza(component, msg);
275 if rooms_info:get(room, 'subject') then
276 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
280 elseif type ~= 'result' then -- bad type
281 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
283 elseif not current_nick then -- not in room
284 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
285 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
286 origin.send(st.error_reply(stanza, "modify", "bad-request"));
287 elseif stanza.name == "message" and type == "error" then
288 log("debug", "%s kicked from %s for sending an error message", current_nick, room);
289 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
290 else -- private stanza
291 local o_data = rooms:get(room, to);
293 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
294 local jid = o_data.jid;
295 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
296 stanza.attr.to, stanza.attr.from = jid, current_nick;
297 core_route_stanza(component, stanza);
298 else -- recipient not in room
299 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
304 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
305 local type = stanza.attr.type;
306 if stanza.name == "iq" and type == "get" then -- disco requests
307 local xmlns = stanza.tags[1].attr.xmlns;
308 if xmlns == "http://jabber.org/protocol/disco#info" then
309 origin.send(get_room_disco_info(stanza));
310 elseif xmlns == "http://jabber.org/protocol/disco#items" then
311 origin.send(get_room_disco_items(stanza));
313 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
315 elseif stanza.name == "message" and type == "groupchat" then
316 local from, to = stanza.attr.from, stanza.attr.to;
317 local room = jid_bare(to);
318 local current_nick = jid_nick:get(from, room);
319 if not current_nick then -- not in room
320 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
322 local from = stanza.attr.from;
323 stanza.attr.from = current_nick;
324 local subject = getText(stanza, {"subject"});
326 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
328 broadcast_message_stanza(room, stanza, true);
331 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
332 local to = stanza.attr.to;
333 local current_nick = jid_nick:get(stanza.attr.from, to);
335 stanza.attr.to = current_nick;
336 handle_to_occupant(origin, stanza);
339 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
342 if type == "error" or type == "result" then return; end
343 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
347 function handle_to_domain(origin, stanza)
348 local type = stanza.attr.type;
349 if type == "error" or type == "result" then return; end
350 if stanza.name == "iq" and type == "get" then
351 local xmlns = stanza.tags[1].attr.xmlns;
352 if xmlns == "http://jabber.org/protocol/disco#info" then
353 origin.send(get_disco_info(stanza));
354 elseif xmlns == "http://jabber.org/protocol/disco#items" then
355 origin.send(get_disco_items(stanza));
357 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
360 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
364 register_component(muc_domain, function(origin, stanza)
365 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
366 if to_resource and not to_node then
367 if type == "error" or type == "result" then return; end
368 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
369 elseif to_resource then
370 handle_to_occupant(origin, stanza);
372 handle_to_room(origin, stanza)
373 else -- to the main muc domain
374 if type == "error" or type == "result" then return; end
375 handle_to_domain(origin, stanza);
379 module.unload = function()
380 deregister_component(muc_domain);
382 module.save = function()
383 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
385 module.restore = function(data)
386 rooms.data, jid_nick.data, rooms_info.data, persist_list =
387 data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};