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;
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);
15 local muc_domain = module:get_host();
17 local muc_name = "MUCMUCMUC!!!";
20 -- occupant_room_nick -> data
23 -- jid = occupant's real jid
24 local rooms = multitable_new();
26 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
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();
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);
42 function getUsingPath(stanza, path, getText)
44 for _, name in ipairs(path) do
45 if type(tag) ~= 'table' then return; end
46 tag = tag:child_with_name(name);
48 if tag and getText then tag = table.concat(tag); end
51 function getTag(stanza, path) return getUsingPath(stanza, path); end
52 function getText(stanza, path) return getUsingPath(stanza, path, true); end
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
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();
64 return reply; -- TODO cache disco reply
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
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
75 function save_room(room)
76 local persistent = rooms_info:get(room, 'persistent');
78 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
80 if persistent ~= persist_list[room] then
81 if not persistent then
82 datamanager.store(room, muc_domain, 'rooms', nil);
84 persist_list[room] = persistent;
85 datamanager.store(nil, muc_domain, 'room_list', persist_list);
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);
94 broadcast_message(current_nick, room, subject or "", nil);
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();
104 stanza:tag("status", {code=code}):up();
107 local r = rooms:get(room);
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);
119 stanza:tag("status", {code='110'});
121 core_route_stanza(component, stanza);
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);
130 for occupant, o_data in pairs(r) do
131 stanza.attr.to = o_data.jid;
132 core_route_stanza(component, stanza);
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);
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);
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
159 if current_nick == to then -- simple presence
162 if rooms:get(room, to) then
163 origin.send(st.error_reply(stanza, "cancel", "conflict"));
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);
174 if rooms:get(room, to) then
175 origin.send(st.error_reply(stanza, "cancel", "conflict"));
178 if not rooms:get(room) and not rooms_info:get(room) then -- new room
179 data = {affiliation='owner', role='moderator', jid=from};
181 if not data then -- new occupant
182 data = {affiliation='none', role='participant', jid=from};
184 rooms:set(room, to, data);
185 jid_nick:set(from, room, to);
186 local r = rooms:get(room);
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);
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')));
204 elseif type ~= 'result' then -- bad type
205 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
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"));
211 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
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));
224 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
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"));
233 local subject = getText(stanza, {"subject"});
235 set_subject(current_nick, room, subject);
237 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
238 -- TODO add to discussion history
242 if type == "error" or type == "result" then return; end
243 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
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));
257 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
260 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
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);
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);
282 module.load_component = function()
283 return handle_stanza; -- Return the function that we want to handle incoming stanzas
286 module.unload = function()
287 deregister_component(muc_domain);
289 module.save = function()
290 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
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 {};