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, 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();
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 local to_nick = select(3, jid_split(to));
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);
174 --TODO: malformed-jid
179 if rooms:get(room, to) then
180 origin.send(st.error_reply(stanza, "cancel", "conflict"));
183 if not rooms:get(room) and not rooms_info:get(room) then -- new room
184 data = {affiliation='owner', role='moderator', jid=from};
186 if not data then -- new occupant
187 data = {affiliation='none', role='participant', jid=from};
189 rooms:set(room, to, data);
190 jid_nick:set(from, room, to);
191 local r = rooms:get(room);
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);
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')));
209 elseif type ~= 'result' then -- bad type
210 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
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"));
216 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
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));
229 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
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"));
238 local subject = getText(stanza, {"subject"});
240 set_subject(current_nick, room, subject);
242 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
243 -- TODO add to discussion history
247 if type == "error" or type == "result" then return; end
248 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
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));
262 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
265 origin.send(st.error_reply(stanza, "cancel", "service-unavailable", "The muc server doesn't deal with messages and presence directed at it"));
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);
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);
287 module.load_component = function()
288 return handle_stanza; -- Return the function that we want to handle incoming stanzas
291 module.unload = function()
292 deregister_component(muc_domain);
294 module.save = function()
295 return {rooms = rooms.data; jid_nick = jid_nick.data; rooms_info = rooms_info.data; persist_list = persist_list};
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 {};