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