GPL->MIT!
[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
11 if module:get_host_type() ~= "component" then
12         error("MUC should be loaded as a component, please see http://prosody.im/doc/components", 0);
13 end
14
15 local muc_domain = module:get_host();
16
17 local muc_name = "MUCMUCMUC!!!";
18
19 -- room_name -> room
20         -- occupant_room_nick -> data
21                 -- affiliation = ...
22                 -- role
23                 -- jid = occupant's real jid
24 local rooms = multitable_new();
25
26 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
27
28 -- room_name -> info
29         -- name - the room's friendly name
30         -- subject - the room's subject
31         -- non-anonymous = true|nil
32         -- persistent = true|nil
33 local rooms_info = multitable_new();
34
35 local persist_list = datamanager.load(nil, muc_domain, 'room_list') or {};
36 for room in pairs(persist_list) do
37         rooms_info:set(room, datamanager.store(room, muc_domain, 'rooms') or nil);
38 end
39
40 local component;
41
42 function getUsingPath(stanza, path, getText)
43         local tag = stanza;
44         for _, name in ipairs(path) do
45                 if type(tag) ~= 'table' then return; end
46                 tag = tag:child_with_name(name);
47         end
48         if tag and getText then tag = table.concat(tag); end
49         return tag;
50 end
51 function getTag(stanza, path) return getUsingPath(stanza, path); end
52 function getText(stanza, path) return getUsingPath(stanza, path, true); end
53
54 function get_disco_info(stanza)
55         return st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
56                 :tag("identity", {category='conference', type='text', name=muc_name}):up()
57                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
58 end
59 function get_disco_items(stanza)
60         local reply = st.iq({type='result', id=stanza.attr.id, from=muc_domain, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
61         for room in pairs(rooms_info:get()) do
62                 reply:tag("item", {jid=room, name=rooms_info:get(room, "name")}):up();
63         end
64         return reply; -- TODO cache disco reply
65 end
66 function get_room_disco_info(stanza)
67         return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
68                 :tag("identity", {category='conference', type='text', name=rooms_info:get(stanza.attr.to, "name")}):up()
69                 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
70 end
71 function get_room_disco_items(stanza)
72         return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
73 end -- TODO allow non-private rooms
74
75 function save_room(room)
76         local persistent = rooms_info:get(room, 'persistent');
77         if persistent then
78                 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
79         end
80         if persistent ~= persist_list[room] then
81                 if not persistent then
82                         datamanager.store(room, muc_domain, 'rooms', nil);
83                 end
84                 persist_list[room] = persistent;
85                 datamanager.store(nil, muc_domain, 'room_list', persist_list);
86         end
87 end
88
89 function set_subject(current_nick, room, subject)
90         -- TODO check nick's authority
91         if subject == "" then subject = nil; end
92         rooms_info:set(room, 'subject', subject);
93         save_room();
94         broadcast_message(current_nick, room, subject or "", nil);
95         return true;
96 end
97
98 function broadcast_presence(type, from, room, code, newnick)
99         local data = rooms:get(room, from);
100         local stanza = st.presence({type=type, from=from})
101                 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
102                 :tag("item", {affiliation=data.affiliation, role=data.role, nick = newnick}):up();
103         if code then
104                 stanza:tag("status", {code=code}):up();
105         end
106         local me;
107         local r = rooms:get(room);
108         if r then
109                 for occupant, o_data in pairs(r) do
110                         if occupant ~= from then
111                                 stanza.attr.to = o_data.jid;
112                                 core_route_stanza(component, stanza);
113                         else
114                                 me = o_data.jid;
115                         end
116                 end
117         end
118         if me then
119                 stanza:tag("status", {code='110'});
120                 stanza.attr.to = me;
121                 core_route_stanza(component, stanza);
122         end
123 end
124 function broadcast_message(from, room, subject, body)
125         local stanza = st.message({type='groupchat', from=from});
126         if subject then stanza:tag('subject'):text(subject):up(); end
127         if body then stanza:tag('body'):text(body):up(); end
128         local r = rooms:get(room);
129         if r then
130                 for occupant, o_data in pairs(r) do
131                         stanza.attr.to = o_data.jid;
132                         core_route_stanza(component, stanza);
133                 end
134         end
135 end
136
137 function handle_to_occupant(origin, stanza) -- PM, vCards, etc
138         local from, to = stanza.attr.from, stanza.attr.to;
139         local room = jid_bare(to);
140         local current_nick = jid_nick:get(from, room);
141         local type = stanza.attr.type;
142         if stanza.name == "presence" then
143                 if type == "error" then -- error, kick em out!
144                         local data = rooms:get(room, to);
145                         data.role = 'none';
146                         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>
147                         rooms:remove(room, to);
148                         jid_nick:remove(from, room);
149                 elseif type == "unavailable" then -- unavailable
150                         if current_nick == to then
151                                 local data = rooms:get(room, to);
152                                 data.role = 'none';
153                                 broadcast_presence('unavailable', to, room);
154                                 rooms:remove(room, to);
155                                 jid_nick:remove(from, room);
156                         end -- TODO else do nothing?
157                 elseif not type then -- available
158                         if current_nick then
159                                 if current_nick == to then -- simple presence
160                                         -- TODO broadcast
161                                 else -- change nick
162                                         if rooms:get(room, to) then
163                                                 origin.send(st.error_reply(stanza, "cancel", "conflict"));
164                                         else
165                                                 local data = rooms:get(room, current_nick);
166                                                 local to_nick = select(3, jid_split(to));
167                                                 if to_nick then
168                                                         broadcast_presence('unavailable', current_nick, room, '303', to_nick);
169                                                         rooms:remove(room, current_nick);
170                                                         rooms:set(room, to, data);
171                                                         jid_nick:set(from, room, to);
172                                                         broadcast_presence(nil, to, room, nil);
173                                                 else
174                                                         --TODO: malformed-jid
175                                                 end
176                                         end
177                                 end
178                         else -- enter room
179                                 if rooms:get(room, to) then
180                                         origin.send(st.error_reply(stanza, "cancel", "conflict"));
181                                 else
182                                         local data;
183                                         if not rooms:get(room) and not rooms_info:get(room) then -- new room
184                                                 data = {affiliation='owner', role='moderator', jid=from};
185                                         end
186                                         if not data then -- new occupant
187                                                 data = {affiliation='none', role='participant', jid=from};
188                                         end
189                                         rooms:set(room, to, data);
190                                         jid_nick:set(from, room, to);
191                                         local r = rooms:get(room);
192                                         if r then
193                                                 for occupant, o_data in pairs(r) do
194                                                         if occupant ~= from then
195                                                                 local pres = st.presence({to=from, from=occupant})
196                                                                         :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
197                                                                         :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
198                                                                 core_route_stanza(component, pres);
199                                                         end
200                                                 end
201                                         end
202                                         broadcast_presence(nil, to, room);
203                                         -- TODO send discussion history
204                                         if rooms_info:get(room, 'subject') then
205                                                 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
206                                         end
207                                 end
208                         end
209                 elseif type ~= 'result' then -- bad type
210                         origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
211                 end
212         elseif stanza.name == "message" and type == "groupchat" then
213                 -- groupchat messages not allowed in PM
214                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
215         else
216                 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
217         end
218 end
219
220 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
221         local type = stanza.attr.type;
222         if stanza.name == "iq" and type == "get" then -- disco requests
223                 local xmlns = stanza.tags[1].attr.xmlns;
224                 if xmlns == "http://jabber.org/protocol/disco#info" then
225                         origin.send(get_room_disco_info(stanza));
226                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
227                         origin.send(get_room_disco_items(stanza));
228                 else
229                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
230                 end
231         elseif stanza.name == "message" and type == "groupchat" then
232                 local from, to = stanza.attr.from, stanza.attr.to;
233                 local room = jid_bare(to);
234                 local current_nick = jid_nick:get(from, room);
235                 if not current_nick then -- not in room
236                         origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
237                 else
238                         local subject = getText(stanza, {"subject"});
239                         if subject then
240                                 set_subject(current_nick, room, subject);
241                         else
242                                 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
243                                 -- TODO add to discussion history
244                         end
245                 end
246         else
247                 if type == "error" or type == "result" then return; end
248                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
249         end
250 end
251
252 function handle_to_domain(origin, stanza)
253         local type = stanza.attr.type;
254         if type == "error" or type == "result" then return; end
255         if stanza.name == "iq" and type == "get" then
256                 local xmlns = stanza.tags[1].attr.xmlns;
257                 if xmlns == "http://jabber.org/protocol/disco#info" then
258                         origin.send(get_disco_info(stanza));
259                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
260                         origin.send(get_disco_items(stanza));
261                 else
262                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
263                 end
264         else
265                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
266         end
267 end
268
269 function handle_stanza(origin, stanza)
270         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
271         if stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
272                 if type == "error" or type == "result" then return; end
273                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME what's appropriate?
274         elseif to_resource and not to_node then
275                 if type == "error" or type == "result" then return; end
276                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
277         elseif to_resource then
278                 handle_to_occupant(origin, stanza);
279         elseif to_node then
280                 handle_to_room(origin, stanza)
281         else -- to the main muc domain
282                 if type == "error" or type == "result" then return; end
283                 handle_to_domain(origin, stanza);
284         end
285 end
286
287 module.load_component = function()
288         return handle_stanza; -- Return the function that we want to handle incoming stanzas
289 end
290
291 module.unload = function()
292         deregister_component(muc_domain);
293 end
294 module.save = function()
295         return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
296 end
297 module.restore = function(data)
298         rooms.data, jid_nick.data, rooms_info.data, persist_list =
299         data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
300 end