9a559f2dbfaf55e362c3ce2993c71c0618edf674
[prosody.git] / plugins / mod_muc.lua
1
2
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;
11
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);
14 end
15
16 local muc_domain = module:get_host();
17 local muc_name = "MUCMUCMUC!!!";
18 local history_length = 20;
19
20 -- room_name -> room
21         -- occupant_room_nick -> data
22                 -- affiliation = ...
23                 -- role
24                 -- jid = occupant's real jid
25 local rooms = multitable_new();
26
27 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
28
29 -- room_name -> info
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();
36
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);
40 end
41
42 local component;
43
44 function filter_xmlns_from_array(array, filters)
45         local count = 0;
46         for i=#array,1,-1 do
47                 local attr = array[i].attr;
48                 if filters[attr and attr.xmlns] then
49                         t_remove(array, i);
50                         count = count + 1;
51                 end
52         end
53         return count;
54 end
55 function filter_xmlns_from_stanza(stanza, filters)
56         if filters then
57                 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
58                         return stanza, filter_xmlns_from_array(stanza, filters);
59                 end
60         end
61         return stanza, 0;
62 end
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);
66 end
67 function getUsingPath(stanza, path, getText)
68         local tag = stanza;
69         for _, name in ipairs(path) do
70                 if type(tag) ~= 'table' then return; end
71                 tag = tag:child_with_name(name);
72         end
73         if tag and getText then tag = table.concat(tag); end
74         return tag;
75 end
76 function getTag(stanza, path) return getUsingPath(stanza, path); end
77 function getText(stanza, path) return getUsingPath(stanza, path, true); end
78
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
83 end
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();
88         end
89         return reply; -- TODO cache disco reply
90 end
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
95 end
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
99
100 function save_room(room)
101         local persistent = rooms_info:get(room, 'persistent');
102         if persistent then
103                 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
104         end
105         if persistent ~= persist_list[room] then
106                 if not persistent then
107                         datamanager.store(room, muc_domain, 'rooms', nil);
108                 end
109                 persist_list[room] = persistent;
110                 datamanager.store(nil, muc_domain, 'room_list', persist_list);
111         end
112 end
113
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);
118         save_room();
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);
123         return true;
124 end
125
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();
131         if code then
132                 stanza:tag("status", {code=code}):up();
133         end
134         local me;
135         local r = rooms:get(room);
136         if r then
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);
141                         else
142                                 me = o_data.jid;
143                         end
144                 end
145         end
146         if me then
147                 stanza:tag("status", {code='110'});
148                 stanza.attr.to = me;
149                 core_route_stanza(component, stanza);
150         end
151 end
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);
157         if r then
158                 for occupant, o_data in pairs(r) do
159                         stanza.attr.to = o_data.jid;
160                         core_route_stanza(component, stanza);
161                 end
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
170                 end
171         end
172 end
173 function broadcast_message_stanza(room, stanza, historic)
174         local r = rooms:get(room);
175         if r then
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);
180                         end
181                 end
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
190                 end
191         end
192 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();
198         if code then
199                 stanza:tag("status", {code=code}):up();
200         end
201         local me;
202         local r = rooms:get(room);
203         if r then
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);
209                                 end
210                         else
211                                 me = o_data;
212                         end
213                 end
214         end
215         if me then
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);
220                 end
221         end
222 end
223
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);
231                 pr.attr.from = to;
232                 if type == "error" then -- error, kick em out!
233                         if current_nick then
234                                 local data = rooms:get(room, to);
235                                 data.role = 'none';
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);
243                         end
244                 elseif type == "unavailable" then -- unavailable
245                         if current_nick then
246                                 local data = rooms:get(room, to);
247                                 data.role = 'none';
248                                 broadcast_presence_stanza(room, pr);
249                                 --broadcast_presence('unavailable', to, room);
250                                 rooms:remove(room, to);
251                                 jid_nick:remove(from, room);
252                         end
253                 elseif not type then -- available
254                         if current_nick then
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
258                                 else -- change nick
259                                         if rooms:get(room, to) then
260                                                 origin.send(st.error_reply(stanza, "cancel", "conflict"));
261                                         else
262                                                 local data = rooms:get(room, current_nick);
263                                                 local to_nick = select(3, jid_split(to));
264                                                 if to_nick then
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);
276                                                 else
277                                                         --TODO malformed-jid
278                                                 end
279                                         end
280                                 end
281                         else -- enter room
282                                 local new_nick = to;
283                                 if rooms:get(room, to) then
284                                         new_nick = nil;
285                                 end
286                                 if not new_nick then
287                                         origin.send(st.error_reply(stanza, "cancel", "conflict"));
288                                 else
289                                         local data;
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)}};
292                                         end
293                                         if not data then -- new occupant
294                                                 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
295                                         end
296                                         rooms:set(room, to, data);
297                                         jid_nick:set(from, room, to);
298                                         local r = rooms:get(room);
299                                         if r then
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;
304                                                                 pres
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);
309                                                         end
310                                                 end
311                                         end
312                                         broadcast_presence_stanza(room, pr);
313                                         --broadcast_presence(nil, to, room);
314                                         local history = rooms_info:get(room, 'history'); -- send discussion history
315                                         if history then
316                                                 for _, msg in ipairs(history) do
317                                                         msg = st.deserialize(msg);
318                                                         msg.attr.to=from;
319                                                         core_route_stanza(component, msg);
320                                                 end
321                                         end
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')));
324                                         end
325                                 end
326                         end
327                 elseif type ~= 'result' then -- bad type
328                         origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
329                 end
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 -- groupchat messages not allowed in PM
333                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
334         elseif stanza.name == "message" and type == "error" then
335                 if current_nick then
336                         local data = rooms:get(room, to);
337                         data.role = 'none';
338                         local pr = st.presence({type='unavailable', from=current_nick}):tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant'):up()
339                                 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
340                                 :tag("item", {affiliation=data.affiliation, role=data.role}):up();
341                         broadcast_presence_stanza(room, pr);
342                         rooms:remove(room, to);
343                         jid_nick:remove(from, room);
344                 end
345         else -- private stanza
346                 local o_data = rooms:get(room, to);
347                 if o_data then
348                         local jid = o_data.jid;
349                         if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' then jid = jid_bare(jid); end
350                         stanza.attr.to, stanza.attr.from = jid, current_nick;
351                         core_route_stanza(component, stanza);
352                 else -- recipient not in room
353                         origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
354                 end
355         end
356 end
357
358 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
359         local type = stanza.attr.type;
360         if stanza.name == "iq" and type == "get" then -- disco requests
361                 local xmlns = stanza.tags[1].attr.xmlns;
362                 if xmlns == "http://jabber.org/protocol/disco#info" then
363                         origin.send(get_room_disco_info(stanza));
364                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
365                         origin.send(get_room_disco_items(stanza));
366                 else
367                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
368                 end
369         elseif stanza.name == "message" and type == "groupchat" then
370                 local from, to = stanza.attr.from, stanza.attr.to;
371                 local room = jid_bare(to);
372                 local current_nick = jid_nick:get(from, room);
373                 if not current_nick then -- not in room
374                         origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
375                 else
376                         local from = stanza.attr.from;
377                         stanza.attr.from = current_nick;
378                         local subject = getText(stanza, {"subject"});
379                         if subject then
380                                 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
381                         else
382                                 --broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
383                                 broadcast_message_stanza(room, stanza, true);
384                         end
385                 end
386         elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
387                 local to = stanza.attr.to;
388                 local current_nick = jid_nick:get(stanza.attr.from, to);
389                 if current_nick then
390                         stanza.attr.to = current_nick;
391                         handle_to_occupant(origin, stanza);
392                         stanza.attr.to = to;
393                 else
394                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
395                 end
396         else
397                 if type == "error" or type == "result" then return; end
398                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
399         end
400 end
401
402 function handle_to_domain(origin, stanza)
403         local type = stanza.attr.type;
404         if type == "error" or type == "result" then return; end
405         if stanza.name == "iq" and type == "get" then
406                 local xmlns = stanza.tags[1].attr.xmlns;
407                 if xmlns == "http://jabber.org/protocol/disco#info" then
408                         origin.send(get_disco_info(stanza));
409                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
410                         origin.send(get_disco_items(stanza));
411                 else
412                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
413                 end
414         else
415                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
416         end
417 end
418
419 register_component(muc_domain, function(origin, stanza)
420         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
421         if to_resource and not to_node then
422                 if type == "error" or type == "result" then return; end
423                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
424         elseif to_resource then
425                 handle_to_occupant(origin, stanza);
426         elseif to_node then
427                 handle_to_room(origin, stanza)
428         else -- to the main muc domain
429                 if type == "error" or type == "result" then return; end
430                 handle_to_domain(origin, stanza);
431         end
432 end);
433
434 module.unload = function()
435         deregister_component(muc_domain);
436 end
437 module.save = function()
438         return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
439 end
440 module.restore = function(data)
441         rooms.data, jid_nick.data, rooms_info.data, persist_list =
442         data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
443 end