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
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
15 local muc_domain = module:get_host();
\r
17 local muc_name = "MUCMUCMUC!!!";
\r
19 -- room_name -> room
\r
20 -- occupant_room_nick -> data
\r
21 -- affiliation = ...
\r
23 -- jid = occupant's real jid
\r
24 local rooms = multitable_new();
\r
26 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
\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
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
42 function getUsingPath(stanza, path, getText)
\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
48 if tag and getText then tag = table.concat(tag); 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
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
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
64 return reply; -- TODO cache disco reply
\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
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
75 function save_room(room)
\r
76 local persistent = rooms_info:get(room, 'persistent');
\r
78 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
\r
80 if persistent ~= persist_list[room] then
\r
81 if not persistent then
\r
82 datamanager.store(room, muc_domain, 'rooms', nil);
\r
84 persist_list[room] = persistent;
\r
85 datamanager.store(nil, muc_domain, 'room_list', persist_list);
\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
94 broadcast_message(current_nick, room, subject or "", nil);
\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
104 stanza:tag("status", {code=code}):up();
\r
107 local r = rooms:get(room);
\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
119 stanza:tag("status", {code='110'});
\r
120 stanza.attr.to = me;
\r
121 core_route_stanza(component, stanza);
\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
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
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
161 else -- change nick
\r
162 if rooms:get(room, to) then
\r
163 origin.send(st.error_reply(stanza, "cancel", "conflict"));
\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
174 if rooms:get(room, to) then
\r
175 origin.send(st.error_reply(stanza, "cancel", "conflict"));
\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
181 if not data then -- new occupant
\r
182 data = {affiliation='none', role='participant', jid=from};
\r
184 rooms:set(room, to, data);
\r
185 jid_nick:set(from, room, to);
\r
186 local r = rooms:get(room);
\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
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
204 elseif type ~= 'result' then -- bad type
\r
205 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
\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
211 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
\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
224 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
\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
233 local subject = getText(stanza, {"subject"});
\r
235 set_subject(current_nick, room, subject);
\r
237 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
\r
238 -- TODO add to discussion history
\r
242 if type == "error" or type == "result" then return; end
\r
243 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
\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
257 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
\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
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
282 module.load_component = function()
\r
283 return handle_stanza; -- Return the function that we want to handle incoming stanzas
\r
286 module.unload = function()
\r
287 deregister_component(muc_domain);
\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
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