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