a2bc683b842bd2a4a5d7c1f9136ea0bfa50dd6bb
[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.clone(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=current_nick})
120                 :tag('subject'):text(subject):up();
121         broadcast_message_stanza(room, msg, false);
122         return true;
123 end
124
125 function broadcast_message_stanza(room, stanza, historic)
126         local r = rooms:get(room);
127         if r then
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);
132                         end
133                 end
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
142                 end
143         end
144 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();
150         if code then
151                 stanza:tag("status", {code=code}):up();
152         end
153         local me;
154         local r = rooms:get(room);
155         if r then
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);
161                                 end
162                         else
163                                 me = o_data;
164                         end
165                 end
166         end
167         if me then
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);
172                 end
173         end
174 end
175
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!
187                         if current_nick then
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
190                         end
191                 elseif type == "unavailable" then -- unavailable
192                         if current_nick then
193                                 log("debug", "%s leaving %s", current_nick, room);
194                                 local data = rooms:get(room, current_nick);
195                                 data.role = 'none';
196                                 broadcast_presence_stanza(room, pr);
197                                 rooms:remove(room, current_nick);
198                                 jid_nick:remove(from, room);
199                         end
200                 elseif not type then -- available
201                         if current_nick then
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);
207                                         else -- change nick
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"));
211                                                 else
212                                                         local data = rooms:get(room, current_nick);
213                                                         local to_nick = select(3, jid_split(to));
214                                                         if to_nick then
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);
221                                                                 pr.attr.from = to;
222                                                                 rooms:get(room, to).sessions[from] = pr;
223                                                                 broadcast_presence_stanza(room, pr);
224                                                         else
225                                                                 --TODO malformed-jid
226                                                         end
227                                                 end
228                                         end
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
233                                 end
234                         else -- enter room
235                                 local new_nick = to;
236                                 if rooms:get(room, to) then
237                                         new_nick = nil;
238                                 end
239                                 if not new_nick then
240                                         log("debug", "%s couldn't join due to nick conflict: %s", from, to);
241                                         origin.send(st.error_reply(stanza, "cancel", "conflict"));
242                                 else
243                                         log("debug", "%s joining as %s", from, to);
244                                         local data;
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)}};
247                                         end
248                                         if not data then -- new occupant
249                                                 data = {affiliation='none', role='participant', jid=from, sessions={[from]=get_filtered_presence(stanza)}};
250                                         end
251                                         rooms:set(room, to, data);
252                                         jid_nick:set(from, room, to);
253                                         local r = rooms:get(room);
254                                         if r then
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);
262                                                         end
263                                                 end
264                                         end
265                                         pr.attr.from = to;
266                                         broadcast_presence_stanza(room, pr);
267                                         local history = rooms_info:get(room, 'history'); -- send discussion history
268                                         if history then
269                                                 for _, msg in ipairs(history) do
270                                                         msg = st.deserialize(msg);
271                                                         msg.attr.to=from;
272                                                         core_route_stanza(component, msg);
273                                                 end
274                                         end
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')));
277                                         end
278                                 end
279                         end
280                 elseif type ~= 'result' then -- bad type
281                         origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
282                 end
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);
292                 if o_data then
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"));
300                 end
301         end
302 end
303
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));
312                 else
313                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
314                 end
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"));
321                 else
322                         local from = stanza.attr.from;
323                         stanza.attr.from = current_nick;
324                         local subject = getText(stanza, {"subject"});
325                         if subject then
326                                 set_subject(current_nick, room, subject); -- TODO use broadcast_message_stanza
327                         else
328                                 broadcast_message_stanza(room, stanza, true);
329                         end
330                 end
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);
334                 if current_nick then
335                         stanza.attr.to = current_nick;
336                         handle_to_occupant(origin, stanza);
337                         stanza.attr.to = to;
338                 else
339                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
340                 end
341         else
342                 if type == "error" or type == "result" then return; end
343                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
344         end
345 end
346
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));
356                 else
357                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
358                 end
359         else
360                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
361         end
362 end
363
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);
371         elseif to_node then
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);
376         end
377 end);
378
379 module.unload = function()
380         deregister_component(muc_domain);
381 end
382 module.save = function()
383         return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
384 end
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 {};
388 end