2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
9 local datamanager = require "util.datamanager";
10 local datetime = require "util.datetime";
12 local jid_split = require "util.jid".split;
13 local jid_bare = require "util.jid".bare;
14 local st = require "util.stanza";
15 local log = require "util.logger".init("mod_muc");
16 local multitable_new = require "util.multitable".new;
17 local t_insert, t_remove = table.insert, table.remove;
18 local setmetatable = setmetatable;
19 local base64 = require "util.encodings".base64;
20 local md5 = require "util.hashes".md5;
22 local muc_domain = nil; --module:get_host();
23 local history_length = 20;
26 local function filter_xmlns_from_array(array, filters)
29 local attr = array[i].attr;
30 if filters[attr and attr.xmlns] then
37 local function filter_xmlns_from_stanza(stanza, filters)
39 if filter_xmlns_from_array(stanza.tags, filters) ~= 0 then
40 return stanza, filter_xmlns_from_array(stanza, filters);
45 local presence_filters = {["http://jabber.org/protocol/muc"]=true;["http://jabber.org/protocol/muc#user"]=true};
46 local function get_filtered_presence(stanza)
47 return filter_xmlns_from_stanza(st.clone(stanza), presence_filters);
49 local kickable_error_conditions = {
51 ["internal-server-error"] = true;
52 ["item-not-found"] = true;
53 ["jid-malformed"] = true;
54 ["recipient-unavailable"] = true;
56 ["remote-server-not-found"] = true;
57 ["remote-server-timeout"] = true;
58 ["service-unavailable"] = true;
60 local function get_kickable_error(stanza)
61 for _, tag in ipairs(stanza.tags) do
62 if tag.name == "error" and tag.attr.xmlns == "jabber:client" then
63 for _, cond in ipairs(tag.tags) do
64 if cond.attr.xmlns == "urn:ietf:params:xml:ns:xmpp-stanzas" then
65 return kickable_error_conditions[cond.name] and cond.name;
68 return true; -- malformed error message
71 return true; -- malformed error message
73 local function getUsingPath(stanza, path, getText)
75 for _, name in ipairs(path) do
76 if type(tag) ~= 'table' then return; end
77 tag = tag:child_with_name(name);
79 if tag and getText then tag = table.concat(tag); end
82 local function getTag(stanza, path) return getUsingPath(stanza, path); end
83 local function getText(stanza, path) return getUsingPath(stanza, path, true); end
86 --[[function get_room_disco_info(room, stanza)
87 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#info")
88 :tag("identity", {category='conference', type='text', name=room._data["name"]):up()
89 :tag("feature", {var="http://jabber.org/protocol/muc"}); -- TODO cache disco reply
91 function get_room_disco_items(room, stanza)
92 return st.iq({type='result', id=stanza.attr.id, from=stanza.attr.to, to=stanza.attr.from}):query("http://jabber.org/protocol/disco#items");
93 end -- TODO allow non-private rooms]]
98 room_mt.__index = room_mt;
100 function room_mt:get_default_role(affiliation)
101 if affiliation == "owner" or affiliation == "admin" then
103 elseif affiliation == "member" or not affiliation then
104 return "participant";
108 function room_mt:broadcast_presence(stanza, code, nick)
109 stanza = get_filtered_presence(stanza);
110 local occupant = self._occupants[stanza.attr.from];
111 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
112 :tag("item", {affiliation=occupant.affiliation, role=occupant.role, nick=nick}):up();
114 stanza:tag("status", {code=code}):up();
116 self:broadcast_except_nick(stanza, stanza.attr.from);
117 local me = self._occupants[stanza.attr.from];
119 stanza:tag("status", {code='110'});
120 for jid in pairs(me.sessions) do
121 stanza.attr.to = jid;
122 self:route_stanza(stanza);
126 function room_mt:broadcast_message(stanza, historic)
127 for occupant, o_data in pairs(self._occupants) do
128 for jid in pairs(o_data.sessions) do
129 stanza.attr.to = jid;
130 self:route_stanza(stanza);
133 if historic then -- add to history
134 local history = self._data['history'];
135 if not history then history = {}; self._data['history'] = history; end
136 -- stanza = st.clone(stanza);
137 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = muc_domain, stamp = datetime.datetime()}):up(); -- XEP-0203
138 stanza:tag("x", {xmlns = "jabber:x:delay", from = muc_domain, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
139 t_insert(history, st.clone(st.preserialize(stanza)));
140 while #history > history_length do t_remove(history, 1) end
143 function room_mt:broadcast_except_nick(stanza, nick)
144 for rnick, occupant in pairs(self._occupants) do
145 if rnick ~= nick then
146 for jid in pairs(occupant.sessions) do
147 stanza.attr.to = jid;
148 self:route_stanza(stanza);
154 function room_mt:send_occupant_list(to)
155 local current_nick = self._jid_nick[to];
156 for occupant, o_data in pairs(self._occupants) do
157 if occupant ~= current_nick then
158 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
159 pres.attr.to, pres.attr.from = to, occupant;
160 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
161 :tag("item", {affiliation=o_data.affiliation, role=o_data.role}):up();
162 self:route_stanza(pres);
166 function room_mt:send_history(to)
167 local history = self._data['history']; -- send discussion history
169 for _, msg in ipairs(history) do
170 msg = st.deserialize(msg);
172 self:route_stanza(msg);
175 if self._data['subject'] then
176 self:route_stanza(st.message({type='groupchat', from=self.jid, to=to}):tag("subject"):text(self._data['subject']));
180 local function room_get_disco_info(self, stanza)
181 return st.reply(stanza):query("http://jabber.org/protocol/disco#info")
182 :tag("identity", {category="conference", type="text"}):up()
183 :tag("feature", {var="http://jabber.org/protocol/muc"});
185 local function room_get_disco_items(self, stanza)
186 return st.reply(stanza):query("http://jabber.org/protocol/disco#items");
188 function room_mt:set_subject(current_nick, subject)
189 -- TODO check nick's authority
190 if subject == "" then subject = nil; end
191 self._data['subject'] = subject;
192 if self.save then self:save(); end
193 local msg = st.message({type='groupchat', from=current_nick})
194 :tag('subject'):text(subject):up();
195 self:broadcast_message(msg, false);
199 function room_mt:handle_to_occupant(origin, stanza) -- PM, vCards, etc
200 local from, to = stanza.attr.from, stanza.attr.to;
201 local room = jid_bare(to);
202 local current_nick = self._jid_nick[from];
203 local type = stanza.attr.type;
204 log("debug", "room: %s, current_nick: %s, stanza: %s", room or "nil", current_nick or "nil", stanza:top_tag());
205 if (select(2, jid_split(from)) == muc_domain) then error("Presence from the MUC itself!!!"); end
206 if stanza.name == "presence" then
207 local pr = get_filtered_presence(stanza);
208 pr.attr.from = current_nick;
209 if type == "error" then -- error, kick em out!
211 log("debug", "kicking %s from %s", current_nick, room);
212 self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
213 :tag('status'):text('This participant is kicked from the room because he sent an error presence')); -- send unavailable
215 elseif type == "unavailable" then -- unavailable
217 log("debug", "%s leaving %s", current_nick, room);
218 local occupant = self._occupants[current_nick];
219 local old_session = occupant.sessions[from];
220 local new_jid = next(occupant.sessions);
221 if new_jid == from then new_jid = next(occupant.sessions, new_jid); end
223 occupant.jid = new_jid;
224 occupant.sessions[from] = nil;
225 local pr = st.clone(occupant[new_jid])
226 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
227 :tag("item", {affiliation=occupant.affiliation, role=occupant.role});
228 self:broadcast_except_nick(pr, current_nick);
230 occupant.role = 'none';
231 self:broadcast_presence(pr);
232 self._occupants[current_nick] = nil;
234 self._jid_nick[from] = nil;
236 elseif not type then -- available
238 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
239 if current_nick == to then -- simple presence
240 log("debug", "%s broadcasted presence", current_nick);
241 self._occupants[current_nick].sessions[from] = pr;
242 self:broadcast_presence(pr);
244 if self._occupants[to] then
245 log("debug", "%s couldn't change nick", current_nick);
246 local reply = st.error_reply(stanza, "cancel", "conflict"):up();
247 reply.tags[1].attr.code = "409";
248 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
250 local data = self._occupants[current_nick];
251 local to_nick = select(3, jid_split(to));
253 log("debug", "%s (%s) changing nick to %s", current_nick, data.jid, to);
254 local p = st.presence({type='unavailable', from=current_nick});
255 self:broadcast_presence(p, '303', to_nick);
256 self._occupants[current_nick] = nil;
257 self._occupants[to] = data;
258 self._jid_nick[from] = to;
260 self._occupants[to].sessions[from] = pr;
261 self:broadcast_presence(pr);
267 --else -- possible rejoin
268 -- log("debug", "%s had connection replaced", current_nick);
269 -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
270 -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable
271 -- self:handle_to_occupant(origin, stanza); -- resend available
275 if self._occupants[to] then
279 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
280 local reply = st.error_reply(stanza, "cancel", "conflict"):up();
281 reply.tags[1].attr.code = "409";
282 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
284 log("debug", "%s joining as %s", from, to);
285 if not next(self._affiliations) then -- new room, no owners
286 self._affiliations[jid_bare(from)] = "owner";
288 local affiliation = self:get_affiliation(from);
289 local role = self:get_default_role(affiliation)
290 if role then -- new occupant
291 self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}};
292 self._jid_nick[from] = to;
293 self:send_occupant_list(from);
295 self:broadcast_presence(pr);
296 self:send_history(from);
298 local reply = st.error_reply(stanza, "auth", "forbidden"):up();
299 reply.tags[1].attr.code = "403";
300 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
304 elseif type ~= 'result' then -- bad type
305 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
307 elseif not current_nick then -- not in room
308 if type == "error" or type == "result" then
309 local id = stanza.name == "iq" and stanza.attr.id and base64.decode(stanza.attr.id);
310 local _nick, _id, _hash = (id or ""):match("^(.+)%z(.*)%z(.+)$");
311 local occupant = self._occupants[stanza.attr.to];
312 if occupant and _nick and self._jid_nick[_nick] and _id and _hash then
313 local id, _to = stanza.attr.id;
314 for jid in pairs(occupant.sessions) do
315 if md5(jid) == _hash then
321 stanza.attr.to, stanza.attr.from, stanza.attr.id = _to, self._jid_nick[_nick], _id;
322 self:route_stanza(stanza);
323 stanza.attr.to, stanza.attr.from, stanza.attr.id = to, from, id;
327 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
329 elseif stanza.name == "message" and type == "groupchat" then -- groupchat messages not allowed in PM
330 origin.send(st.error_reply(stanza, "modify", "bad-request"));
331 else -- private stanza
332 local o_data = self._occupants[to];
334 log("debug", "%s sent private stanza to %s (%s)", from, to, o_data.jid);
335 local jid = o_data.jid;
336 local bare = jid_bare(jid);
337 stanza.attr.to, stanza.attr.from = jid, current_nick;
338 local id = stanza.attr.id;
339 if stanza.name=='iq' and type=='get' and stanza.tags[1].attr.xmlns == 'vcard-temp' and bare ~= jid then
340 stanza.attr.to = bare;
341 stanza.attr.id = base64.encode(jid.."\0"..id.."\0"..md5(from));
343 self:route_stanza(stanza);
344 stanza.attr.to, stanza.attr.from, stanza.attr.id = to, from, id;
345 elseif type ~= "error" and type ~= "result" then -- recipient not in room
346 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
351 function room_mt:handle_form(origin, stanza)
352 if self:get_affiliation(stanza.attr.from) ~= "owner" then origin.send(st.error_reply(stanza, "auth", "forbidden")); return; end
353 if stanza.attr.type == "get" then
354 local title = "Configuration for "..self.jid;
355 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
356 :tag("x", {xmlns='jabber:x:data', type='form'})
357 :tag("title"):text(title):up()
358 :tag("instructions"):text(title):up()
359 :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up()
360 :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'})
361 :tag("value"):text(self._data.persistent and "1" or "0"):up()
363 :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'})
364 :tag("value"):text(self._data.hidden and "0" or "1"):up()
367 elseif stanza.attr.type == "set" then
368 local query = stanza.tags[1];
370 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end
371 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
372 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
373 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
375 for _, field in pairs(form.tags) do
376 if field.name == "field" and field.attr.var and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
377 fields[field.attr.var] = field.tags[1][1] or "";
380 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
382 local persistent = fields['muc#roomconfig_persistentroom'];
383 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true;
384 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
385 self._data.persistent = persistent;
386 module:log("debug", "persistent=%s", tostring(persistent));
388 local public = fields['muc#roomconfig_publicroom'];
389 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true;
390 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
391 self._data.hidden = not public and true or nil;
393 if self.save then self:save(true); end
394 origin.send(st.reply(stanza));
398 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
399 local type = stanza.attr.type;
400 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
401 if stanza.name == "iq" then
402 if xmlns == "http://jabber.org/protocol/disco#info" and type == "get" then
403 origin.send(room_get_disco_info(self, stanza));
404 elseif xmlns == "http://jabber.org/protocol/disco#items" and type == "get" then
405 origin.send(room_get_disco_items(self, stanza));
406 elseif xmlns == "http://jabber.org/protocol/muc#admin" then
407 local actor = stanza.attr.from;
408 local affiliation = self:get_affiliation(actor);
409 local current_nick = self._jid_nick[actor];
410 local role = current_nick and self._occupants[current_nick].role or self:get_default_role(affiliation);
411 local item = stanza.tags[1].tags[1];
412 if item and item.name == "item" then
413 if type == "set" then
414 local callback = function() origin.send(st.reply(stanza)); end
415 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
416 local occupant = self._occupants[self.jid.."/"..item.attr.nick];
417 if occupant then item.attr.jid = occupant.jid; end
419 if item.attr.affiliation and item.attr.jid and not item.attr.role then
420 local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback);
421 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
422 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
423 local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback);
424 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
426 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
428 elseif type == "get" then
429 local _aff = item.attr.affiliation;
430 local _rol = item.attr.role;
431 if _aff and not _rol then
432 if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then
433 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
434 for jid, affiliation in pairs(self._affiliations) do
435 if affiliation == _aff then
436 reply:tag("item", {affiliation = _aff, jid = jid}):up();
441 origin.send(st.error_reply(stanza, "auth", "forbidden"));
443 elseif _rol and not _aff then
444 if role == "moderator" then
445 -- TODO allow admins and owners not in room? Provide read-only access to everyone who can see the participants anyway?
446 if _rol == "none" then _rol = nil; end
447 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
448 for nick, occupant in pairs(self._occupants) do
449 if occupant.role == _rol then
450 reply:tag("item", {nick = nick, role = _rol or "none", affiliation = occupant.affiliation or "none", jid = occupant.jid}):up();
455 origin.send(st.error_reply(stanza, "auth", "forbidden"));
458 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
461 elseif type == "set" or type == "get" then
462 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
464 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then
465 self:handle_form(origin, stanza);
466 elseif type == "set" or type == "get" then
467 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
469 elseif stanza.name == "message" and type == "groupchat" then
470 local from, to = stanza.attr.from, stanza.attr.to;
471 local room = jid_bare(to);
472 local current_nick = self._jid_nick[from];
473 if not current_nick then -- not in room
474 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
476 local from = stanza.attr.from;
477 stanza.attr.from = current_nick;
478 local subject = getText(stanza, {"subject"});
480 self:set_subject(current_nick, subject); -- TODO use broadcast_message_stanza
482 self:broadcast_message(stanza, true);
485 elseif stanza.name == "message" and type == "error" and get_kickable_error(stanza) then
486 local current_nick = self._jid_nick[stanza.attr.from];
487 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
488 self:handle_to_occupant(origin, st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
489 :tag('status'):text('This participant is kicked from the room because he sent an error message to another occupant')); -- send unavailable
490 elseif stanza.name == "presence" then -- hack - some buggy clients send presence updates to the room rather than their nick
491 local to = stanza.attr.to;
492 local current_nick = self._jid_nick[stanza.attr.from];
494 stanza.attr.to = current_nick;
495 self:handle_to_occupant(origin, stanza);
497 elseif type ~= "error" and type ~= "result" then
498 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
500 elseif stanza.name == "message" and not stanza.attr.type and #stanza.tags == 1 and self._jid_nick[stanza.attr.from]
501 and stanza.tags[1].name == "x" and stanza.tags[1].attr.xmlns == "http://jabber.org/protocol/muc#user" and #stanza.tags[1].tags == 1
502 and stanza.tags[1].tags[1].name == "invite" and stanza.tags[1].tags[1].attr.to then
503 local _from, _to = stanza.attr.from, stanza.attr.to;
504 local _invitee = stanza.tags[1].tags[1].attr.to;
505 stanza.attr.from, stanza.attr.to = _to, _invitee;
506 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = _from, nil;
507 self:route_stanza(stanza);
508 stanza.tags[1].tags[1].attr.from, stanza.tags[1].tags[1].attr.to = nil, _invitee;
509 stanza.attr.from, stanza.attr.to = _from, _to;
511 if type == "error" or type == "result" then return; end
512 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
516 function room_mt:handle_stanza(origin, stanza)
517 local to_node, to_host, to_resource = jid_split(stanza.attr.to);
519 self:handle_to_occupant(origin, stanza);
521 self:handle_to_room(origin, stanza);
525 function room_mt:route_stanza(stanza) end -- Replace with a routing function, e.g., function(room, stanza) core_route_stanza(origin, stanza); end
527 function room_mt:get_affiliation(jid)
528 local node, host, resource = jid_split(jid);
529 local bare = node and node.."@"..host or host;
530 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
531 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
534 function room_mt:set_affiliation(actor, jid, affiliation, callback)
536 if affiliation == "none" then affiliation = nil; end
537 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then
538 return nil, "modify", "not-acceptable";
540 if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end
541 if jid_bare(actor) == jid then return nil, "cancel", "not-allowed"; end
542 self._affiliations[jid] = affiliation;
543 local role = self:get_default_role(affiliation);
544 local p = st.presence()
545 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
546 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
548 local item = x.tags[1];
549 if not role then -- getting kicked
550 p.attr.type = "unavailable";
551 if affiliation == "outcast" then
552 x:tag("status", {code="301"}):up(); -- banned
554 x:tag("status", {code="321"}):up(); -- affiliation change
557 local modified_nicks = {};
558 for nick, occupant in pairs(self._occupants) do
559 if jid_bare(occupant.jid) == jid then
560 if not role then -- getting kicked
561 self._occupants[nick] = nil;
563 t_insert(modified_nicks, nick);
564 occupant.affiliation, occupant.role = affiliation, role;
567 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
568 if not role then self._jid_nick[jid] = nil; end
570 self:route_stanza(p);
574 if self.save then self:save(); end
575 if callback then callback(); end
576 for _, nick in ipairs(modified_nicks) do
578 self:broadcast_except_nick(p, nick);
583 function room_mt:get_role(nick)
584 local session = self._occupants[nick];
585 return session and session.role or nil;
587 function room_mt:set_role(actor, nick, role, callback)
588 if role == "none" then role = nil; end
589 if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end
590 if self:get_affiliation(actor) ~= "owner" then return nil, "cancel", "not-allowed"; end
591 local occupant = self._occupants[nick];
592 if not occupant then return nil, "modify", "not-acceptable"; end
593 if occupant.affiliation == "owner" or occupant.affiliation == "admin" then return nil, "cancel", "not-allowed"; end
594 local p = st.presence({from = nick})
595 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
596 :tag("item", {affiliation=occupant.affiliation or "none", nick=nick, role=role or "none"}):up();
597 if not role then -- kick
598 p.attr.type = "unavailable";
599 self._occupants[nick] = nil;
600 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
601 self._jid_nick[jid] = nil;
603 p:tag("status", {code = "307"}):up();
605 occupant.role = role;
607 for jid in pairs(occupant.sessions) do -- send to all sessions of the nick
609 self:route_stanza(p);
611 if callback then callback(); end
612 self:broadcast_except_nick(p, nick);
616 local _M = {}; -- module "muc"
618 function _M.new_room(jid)
619 return setmetatable({