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 local muc_domain = "conference."..module:get_host();
\r
12 local muc_name = "MUCMUCMUC!!!";
\r
14 -- room_name -> room
\r
15 -- occupant_room_nick -> data
\r
16 -- affiliation = ...
\r
18 -- jid = occupant's real jid
\r
19 local rooms = multitable_new();
\r
21 local jid_nick = multitable_new(); -- real jid -> room's jid -> room nick
\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
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
37 function getUsingPath(stanza, path, getText)
\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
43 if tag and getText then tag = table.concat(tag); 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
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
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
59 return reply; -- TODO cache disco reply
\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
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
70 function save_room(room)
\r
71 local persistent = rooms_info:get(room, 'persistent');
\r
73 datamanager.store(room, muc_domain, 'rooms', rooms_info:get(room));
\r
75 if persistent ~= persist_list[room] then
\r
76 if not persistent then
\r
77 datamanager.store(room, muc_domain, 'rooms', nil);
\r
79 persist_list[room] = persistent;
\r
80 datamanager.store(nil, muc_domain, 'room_list', persist_list);
\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
89 broadcast_message(current_nick, room, subject or "", nil);
\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
99 stanza:tag("status", {code=code}):up();
\r
102 local r = rooms:get(room);
\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
114 stanza:tag("status", {code='110'});
\r
115 stanza.attr.to = me;
\r
116 core_route_stanza(component, stanza);
\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
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
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
156 else -- change nick
\r
157 if rooms:get(room, to) then
\r
158 origin.send(st.error_reply(stanza, "cancel", "conflict"));
\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
169 if rooms:get(room, to) then
\r
170 origin.send(st.error_reply(stanza, "cancel", "conflict"));
\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
176 if not data then -- new occupant
\r
177 data = {affiliation='none', role='participant', jid=from};
\r
179 rooms:set(room, to, data);
\r
180 jid_nick:set(from, room, to);
\r
181 local r = rooms:get(room);
\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
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
199 elseif type ~= 'result' then -- bad type
\r
200 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
\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
206 origin.send(st.error_reply(stanza, "cancel", "not-implemented", "Private stanzas not implemented")); -- TODO route private stanza
\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
219 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
\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
228 local subject = getText(stanza, {"subject"});
\r
230 set_subject(current_nick, room, subject);
\r
232 broadcast_message(current_nick, room, nil, getText(stanza, {"body"}));
\r
233 -- TODO add to discussion history
\r
237 if type == "error" or type == "result" then return; end
\r
238 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
\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
252 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); -- TODO disco/etc
\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
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
277 module.unload = function()
\r
278 deregister_component(muc_domain);
\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
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