mod_muc: Convert to unix line endings
[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)
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}):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                                                 broadcast_presence('unavailable', current_nick, room, '303');
167                                                 rooms:remove(room, current_nick);
168                                                 rooms:set(room, to, data);
169                                                 jid_nick:set(from, room, to);
170                                                 broadcast_presence(nil, to, room);
171                                         end
172                                 end
173                         else -- enter room
174                                 if rooms:get(room, to) then
175                                         origin.send(st.error_reply(stanza, "cancel", "conflict"));
176                                 else
177                                         local data;
178                                         if not rooms:get(room) and not rooms_info:get(room) then -- new room
179                                                 data = {affiliation='owner', role='moderator', jid=from};
180                                         end
181                                         if not data then -- new occupant
182                                                 data = {affiliation='none', role='participant', jid=from};
183                                         end
184                                         rooms:set(room, to, data);
185                                         jid_nick:set(from, room, to);
186                                         local r = rooms:get(room);
187                                         if r then
188                                                 for occupant, o_data in pairs(r) do
189                                                         if occupant ~= from then
190                                                                 local pres = st.presence({to=from, from=occupant})
191                                                                         :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
192                                                                         :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
193                                                                 core_route_stanza(component, pres);
194                                                         end
195                                                 end
196                                         end
197                                         broadcast_presence(nil, to, room);
198                                         -- TODO send discussion history
199                                         if rooms_info:get(room, 'subject') then
200                                                 core_route_stanza(component, st.message({type='groupchat', from=room, to=from}):tag("subject"):text(rooms_info:get(room, 'subject')));
201                                         end
202                                 end
203                         end
204                 elseif type ~= 'result' then -- bad type
205                         origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
206                 end
207         elseif stanza.name == "message" and type == "groupchat" then
208                 -- groupchat messages not allowed in PM
209                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
210         else
211                 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
212         end
213 end
214
215 function handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
216         local type = stanza.attr.type;
217         if stanza.name == "iq" and type == "get" then -- disco requests
218                 local xmlns = stanza.tags[1].attr.xmlns;
219                 if xmlns == "http://jabber.org/protocol/disco#info" then
220                         origin.send(get_room_disco_info(stanza));
221                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
222                         origin.send(get_room_disco_items(stanza));
223                 else
224                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
225                 end
226         elseif stanza.name == "message" and type == "groupchat" then
227                 local from, to = stanza.attr.from, stanza.attr.to;
228                 local room = jid_bare(to);
229                 local current_nick = jid_nick:get(from, room);
230                 if not current_nick then -- not in room
231                         origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
232                 else
233                         local subject = getText(stanza, {"subject"});
234                         if subject then
235                                 set_subject(current_nick, room, subject);
236                         else
237                                 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
238                                 -- TODO add to discussion history
239                         end
240                 end
241         else
242                 if type == "error" or type == "result" then return; end
243                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
244         end
245 end
246
247 function handle_to_domain(origin, stanza)
248         local type = stanza.attr.type;
249         if type == "error" or type == "result" then return; end
250         if stanza.name == "iq" and type == "get" then
251                 local xmlns = stanza.tags[1].attr.xmlns;
252                 if xmlns == "http://jabber.org/protocol/disco#info" then
253                         origin.send(get_disco_info(stanza));
254                 elseif xmlns == "http://jabber.org/protocol/disco#items" then
255                         origin.send(get_disco_items(stanza));
256                 else
257                         origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
258                 end
259         else
260                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
261         end
262 end
263
264 function handle_stanza(origin, stanza)
265         local to_node, to_host, to_resource = jid_split(stanza.attr.to);
266         if stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then
267                 if type == "error" or type == "result" then return; end
268                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- FIXME what's appropriate?
269         elseif to_resource and not to_node then
270                 if type == "error" or type == "result" then return; end
271                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- host/resource
272         elseif to_resource then
273                 handle_to_occupant(origin, stanza);
274         elseif to_node then
275                 handle_to_room(origin, stanza)
276         else -- to the main muc domain
277                 if type == "error" or type == "result" then return; end
278                 handle_to_domain(origin, stanza);
279         end
280 end
281
282 module.load_component = function()
283         return handle_stanza; -- Return the function that we want to handle incoming stanzas
284 end
285
286 module.unload = function()
287         deregister_component(muc_domain);
288 end
289 module.save = function()
290         return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
291 end
292 module.restore = function(data)
293         rooms.data, jid_nick.data, rooms_info.data, persist_list =
294         data.rooms or {}, data.jid_nick or {}, data.rooms_info or {}, data.persist_list or {};
295 end