b2f89972ddf387983ab03e8de9af212c4788958e
[prosody.git] / plugins / muc / muc.lib.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10 local select = select;
11 local pairs, ipairs = pairs, ipairs;
12 local next = next;
13 local setmetatable = setmetatable;
14
15 local dataform = require "util.dataforms";
16 local iterators = require "util.iterators";
17 local jid_split = require "util.jid".split;
18 local jid_bare = require "util.jid".bare;
19 local jid_prep = require "util.jid".prep;
20 local jid_join = require "util.jid".join;
21 local st = require "util.stanza";
22 local log = require "util.logger".init("mod_muc");
23 local base64 = require "util.encodings".base64;
24 local md5 = require "util.hashes".md5;
25
26 local occupant_lib = module:require "muc/occupant"
27 local muc_util = module:require "muc/util";
28 local is_kickable_error = muc_util.is_kickable_error;
29 local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations;
30
31 local room_mt = {};
32 room_mt.__index = room_mt;
33
34 function room_mt:__tostring()
35         return "MUC room ("..self.jid..")";
36 end
37
38 function room_mt:get_occupant_jid(real_jid)
39         return self._jid_nick[real_jid]
40 end
41
42 function room_mt:get_default_role(affiliation)
43         local role = module:fire_event("muc-get-default-role", {
44                 room = self;
45                 affiliation = affiliation;
46                 affiliation_rank = valid_affiliations[affiliation or "none"];
47         });
48         return role, valid_roles[role or "none"];
49 end
50 module:hook("muc-get-default-role", function(event)
51         if event.affiliation_rank >= valid_affiliations.admin then
52                 return "moderator";
53         elseif event.affiliation_rank >= valid_affiliations.none then
54                 return "participant";
55         end
56 end);
57
58 --- Occupant functions
59 function room_mt:new_occupant(bare_real_jid, nick)
60         local occupant = occupant_lib.new(bare_real_jid, nick);
61         local affiliation = self:get_affiliation(bare_real_jid);
62         occupant.role = self:get_default_role(affiliation);
63         return occupant;
64 end
65
66 function room_mt:get_occupant_by_nick(nick)
67         local occupant = self._occupants[nick];
68         if occupant == nil then return nil end
69         return occupant_lib.copy(occupant);
70 end
71
72 do
73         local function next_copied_occupant(occupants, occupant_jid)
74                 local next_occupant_jid, raw_occupant = next(occupants, occupant_jid);
75                 if next_occupant_jid == nil then return nil end
76                 return next_occupant_jid, occupant_lib.copy(raw_occupant);
77         end
78         function room_mt:each_occupant(read_only)
79                 return next_copied_occupant, self._occupants, nil;
80         end
81 end
82
83 function room_mt:has_occupant()
84         return next(self._occupants, nil) ~= nil
85 end
86
87 function room_mt:get_occupant_by_real_jid(real_jid)
88         local occupant_jid = self:get_occupant_jid(real_jid);
89         if occupant_jid == nil then return nil end
90         return self:get_occupant_by_nick(occupant_jid);
91 end
92
93 function room_mt:save_occupant(occupant)
94         occupant = occupant_lib.copy(occupant); -- So that occupant can be modified more
95         local id = occupant.nick
96
97         -- Need to maintain _jid_nick secondary index
98         local old_occupant = self._occupants[id];
99         if old_occupant then
100                 for real_jid in old_occupant:each_session() do
101                         self._jid_nick[real_jid] = nil;
102                 end
103         end
104
105         local has_live_session = false
106         if occupant.role ~= nil then
107                 for real_jid, presence in occupant:each_session() do
108                         if presence.attr.type == nil then
109                                 has_live_session = true
110                                 self._jid_nick[real_jid] = occupant.nick;
111                         end
112                 end
113                 if not has_live_session then
114                         -- Has no live sessions left; they have left the room.
115                         occupant.role = nil
116                 end
117         end
118         if not has_live_session then
119                 occupant = nil
120         end
121         self._occupants[id] = occupant
122 end
123
124 function room_mt:route_to_occupant(occupant, stanza)
125         local to = stanza.attr.to;
126         for jid, pr in occupant:each_session() do
127                 stanza.attr.to = jid;
128                 self:route_stanza(stanza);
129         end
130         stanza.attr.to = to;
131 end
132
133 -- actor is the attribute table
134 local function add_item(x, affiliation, role, jid, nick, actor_nick, actor_jid, reason)
135         x:tag("item", {affiliation = affiliation; role = role; jid = jid; nick = nick;})
136         if actor_nick or actor_jid then
137                 x:tag("actor", {nick = actor_nick; jid = actor_jid;}):up()
138         end
139         if reason then
140                 x:tag("reason"):text(reason):up()
141         end
142         x:up();
143         return x
144 end
145
146 -- actor is (real) jid
147 function room_mt:build_item_list(occupant, x, is_anonymous, nick, actor_nick, actor_jid, reason)
148         local affiliation = self:get_affiliation(occupant.bare_jid) or "none";
149         local role = occupant.role or "none";
150         if is_anonymous then
151                 add_item(x, affiliation, role, nil, nick, actor_nick, actor_jid, reason);
152         else
153                 for real_jid, session in occupant:each_session() do
154                         add_item(x, affiliation, role, real_jid, nick, actor_nick, actor_jid, reason);
155                 end
156         end
157         return x
158 end
159
160 function room_mt:broadcast_message(stanza)
161         if module:fire_event("muc-broadcast-message", {room = self, stanza = stanza}) then
162                 return true;
163         end
164         self:broadcast(stanza);
165         return true;
166 end
167
168 -- Broadcast a stanza to all occupants in the room.
169 -- optionally checks conditional called with (nick, occupant)
170 function room_mt:broadcast(stanza, cond_func)
171         for nick, occupant in self:each_occupant() do
172                 if cond_func == nil or cond_func(nick, occupant) then
173                         self:route_to_occupant(occupant, stanza)
174                 end
175         end
176 end
177
178 local function can_see_real_jids(whois, occupant)
179         if whois == "anyone" then
180                 return true;
181         elseif whois == "moderators" then
182                 return valid_roles[occupant.role or "none"] >= valid_roles.moderator;
183         end
184 end
185
186 -- Broadcasts an occupant's presence to the whole room
187 -- Takes the x element that goes into the stanzas
188 function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason)
189         -- Build real jid and (optionally) occupant jid template presences
190         local base_presence do
191                 -- Try to use main jid's presence
192                 local pr = occupant:get_presence();
193                 if pr and (pr.attr.type ~= "unavailable" or occupant.role == nil) then
194                         base_presence = st.clone(pr);
195                 else -- user is leaving but didn't send a leave presence. make one for them
196                         base_presence = st.presence {from = occupant.nick; type = "unavailable";};
197                 end
198         end
199
200         -- Fire event (before full_p and anon_p are created)
201         local event = {
202                 room = self; stanza = base_presence; x = base_x;
203                 occupant = occupant; nick = nick; actor = actor;
204                 reason = reason;
205         }
206         module:fire_event("muc-broadcast-presence", event);
207
208         -- Allow muc-broadcast-presence listeners to change things
209         nick = event.nick;
210         actor = event.actor;
211         reason = event.reason;
212
213         local whois = self:get_whois();
214
215         local actor_nick;
216         if actor then
217                 actor_nick = select(3, jid_split(self:get_occupant_jid(actor)));
218         end
219
220         local full_p, full_x;
221         local function get_full_p()
222                 if full_p == nil then
223                         full_x = st.clone(base_x);
224                         self:build_item_list(occupant, full_x, false, nick, actor_nick, actor, reason);
225                         full_p = st.clone(base_presence):add_child(full_x);
226                 end
227                 return full_p, full_x;
228         end
229
230         local anon_p, anon_x;
231         local function get_anon_p()
232                 if anon_p == nil then
233                         anon_x = st.clone(base_x);
234                         self:build_item_list(occupant, anon_x, true, nick, actor_nick, nil, reason);
235                         anon_p = st.clone(base_presence):add_child(anon_x);
236                 end
237                 return anon_p, anon_x;
238         end
239
240         local self_p, self_x;
241         if can_see_real_jids(whois, occupant) then
242                 self_p, self_x = get_full_p();
243         else
244                 -- Can always see your own full jids
245                 -- But not allowed to see actor's
246                 self_x = st.clone(base_x);
247                 self:build_item_list(occupant, self_x, false, nick, actor_nick, nil, reason);
248                 self_p = st.clone(base_presence):add_child(self_x);
249         end
250
251         -- General populance
252         for nick, n_occupant in self:each_occupant() do
253                 if nick ~= occupant.nick then
254                         local pr;
255                         if can_see_real_jids(whois, n_occupant) then
256                                 pr = get_full_p();
257                         elseif occupant.bare_jid == n_occupant.bare_jid then
258                                 pr = self_p;
259                         else
260                                 pr = get_anon_p();
261                         end
262                         self:route_to_occupant(n_occupant, pr);
263                 end
264         end
265
266         -- Presences for occupant itself
267         self_x:tag("status", {code = "110";}):up();
268         if occupant.role == nil then
269                 -- They get an unavailable
270                 self:route_to_occupant(occupant, self_p);
271         else
272                 -- use their own presences as templates
273                 for full_jid, pr in occupant:each_session() do
274                         pr = st.clone(pr);
275                         pr.attr.to = full_jid;
276                         pr:add_child(self_x);
277                         self:route_stanza(pr);
278                 end
279         end
280 end
281
282 function room_mt:send_occupant_list(to, filter)
283         local to_bare = jid_bare(to);
284         local is_anonymous = false;
285         local whois = self:get_whois();
286         if whois ~= "anyone" then
287                 local affiliation = self:get_affiliation(to);
288                 if affiliation ~= "admin" and affiliation ~= "owner" then
289                         local occupant = self:get_occupant_by_real_jid(to);
290                         if not (occupant and can_see_real_jids(whois, occupant)) then
291                                 is_anonymous = true;
292                         end
293                 end
294         end
295         for occupant_jid, occupant in self:each_occupant() do
296                 if filter == nil or filter(occupant_jid, occupant) then
297                         local x = st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
298                         self:build_item_list(occupant, x, is_anonymous and to_bare ~= occupant.bare_jid); -- can always see your own jids
299                         local pres = st.clone(occupant:get_presence());
300                         pres.attr.to = to;
301                         pres:add_child(x);
302                         self:route_stanza(pres);
303                 end
304         end
305 end
306
307 function room_mt:get_disco_info(stanza)
308         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info");
309         local form = dataform.new {
310                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" };
311         };
312         module:fire_event("muc-disco#info", {room = self; reply = reply; form = form;});
313         reply:add_child(form:form(nil, "result"));
314         return reply;
315 end
316 module:hook("muc-disco#info", function(event)
317         event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
318 end);
319 module:hook("muc-disco#info", function(event)
320         local count = iterators.count(event.room:each_occupant());
321         table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) });
322 end);
323
324 function room_mt:get_disco_items(stanza)
325         local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
326         for room_jid in self:each_occupant() do
327                 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
328         end
329         return reply;
330 end
331
332 function room_mt:handle_kickable(origin, stanza)
333         local real_jid = stanza.attr.from;
334         local occupant = self:get_occupant_by_real_jid(real_jid);
335         if occupant == nil then return nil; end
336         local type, condition, text = stanza:get_error();
337         local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
338         if text then
339                 error_message = error_message..": "..text;
340         end
341         occupant:set_session(real_jid, st.presence({type="unavailable"})
342                 :tag('status'):text(error_message));
343         self:save_occupant(occupant);
344         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
345                 :tag("status", {code = "307"})
346         self:publicise_occupant_status(occupant, x);
347         if occupant.jid == real_jid then -- Was last session
348                 module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
349         end
350         return true;
351 end
352
353 -- Give the room creator owner affiliation
354 module:hook("muc-room-pre-create", function(event)
355         event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
356 end, -1);
357
358 -- check if user is banned
359 module:hook("muc-occupant-pre-join", function(event)
360         local room, stanza = event.room, event.stanza;
361         local affiliation = room:get_affiliation(stanza.attr.from);
362         if affiliation == "outcast" then
363                 local reply = st.error_reply(stanza, "auth", "forbidden"):up();
364                 reply.tags[1].attr.code = "403";
365                 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
366                 return true;
367         end
368 end, -10);
369
370 function room_mt:handle_presence_to_occupant(origin, stanza)
371         local type = stanza.attr.type;
372         if type == "error" then -- error, kick em out!
373                 return self:handle_kickable(origin, stanza)
374         elseif type == nil or type == "unavailable" then
375                 local real_jid = stanza.attr.from;
376                 local bare_jid = jid_bare(real_jid);
377                 local orig_occupant, dest_occupant;
378                 local is_new_room = next(self._affiliations) == nil;
379                 if is_new_room then
380                         if type == "unavailable" then return true; end -- Unavailable from someone not in the room
381                         if module:fire_event("muc-room-pre-create", {
382                                         room = self;
383                                         origin = origin;
384                                         stanza = stanza;
385                                 }) then return true; end
386                 else
387                         orig_occupant = self:get_occupant_by_real_jid(real_jid);
388                         if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room
389                 end
390                 local is_first_dest_session;
391                 if type == "unavailable" then
392                         -- dest_occupant = nil
393                 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update
394                         log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid);
395                         dest_occupant = orig_occupant;
396                 else
397                         local dest_jid = stanza.attr.to;
398                         dest_occupant = self:get_occupant_by_nick(dest_jid);
399                         if dest_occupant == nil then
400                                 log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid);
401                                 is_first_dest_session = true;
402                                 dest_occupant = self:new_occupant(bare_jid, dest_jid);
403                         else
404                                 is_first_dest_session = false;
405                         end
406                 end
407                 local is_last_orig_session;
408                 if orig_occupant ~= nil then
409                         -- Is there are least 2 sessions?
410                         local iter, ob, last = orig_occupant:each_session();
411                         is_last_orig_session = iter(ob, iter(ob, last)) == nil;
412                 end
413
414                 local event, event_name = {
415                         room = self;
416                         origin = origin;
417                         stanza = stanza;
418                         is_first_session = is_first_dest_session;
419                         is_last_session = is_last_orig_session;
420                 };
421                 if orig_occupant == nil then
422                         event_name = "muc-occupant-pre-join";
423                         event.is_new_room = is_new_room;
424                         event.occupant = dest_occupant;
425                 elseif dest_occupant == nil then
426                         event_name = "muc-occupant-pre-leave";
427                         event.occupant = orig_occupant;
428                 else
429                         event_name = "muc-occupant-pre-change";
430                         event.orig_occupant = orig_occupant;
431                         event.dest_occupant = dest_occupant;
432                 end
433                 if module:fire_event(event_name, event) then return true; end
434
435                 -- Check for nick conflicts
436                 if dest_occupant ~= nil and not is_first_dest_session and bare_jid ~= jid_bare(dest_occupant.bare_jid) then -- new nick or has different bare real jid
437                         log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick);
438                         local reply = st.error_reply(stanza, "cancel", "conflict"):up();
439                         reply.tags[1].attr.code = "409";
440                         origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
441                         return true;
442                 end
443
444                 -- Send presence stanza about original occupant
445                 if orig_occupant ~= nil and orig_occupant ~= dest_occupant then
446                         local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
447                         local dest_nick;
448                         if dest_occupant == nil then -- Session is leaving
449                                 log("debug", "session %s is leaving occupant %s", real_jid, orig_occupant.nick);
450                                 if is_last_orig_session then
451                                         orig_occupant.role = nil;
452                                 end
453                                 orig_occupant:set_session(real_jid, stanza);
454                         else
455                                 log("debug", "session %s is changing from occupant %s to %s", real_jid, orig_occupant.nick, dest_occupant.nick);
456                                 local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"};
457                                 orig_occupant:set_session(real_jid, generated_unavail);
458                                 dest_nick = select(3, jid_split(dest_occupant.nick));
459                                 if not is_first_dest_session then -- User is swapping into another pre-existing session
460                                         log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick);
461                                         -- Show the other session leaving
462                                         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";})
463                                                 :tag("status"):text("you are joining pre-existing session " .. dest_nick):up();
464                                         add_item(x, self:get_affiliation(bare_jid), "none");
465                                         local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"}
466                                                 :add_child(x);
467                                         self:route_stanza(pr);
468                                 end
469                                 if is_first_dest_session and is_last_orig_session then -- Normal nick change
470                                         log("debug", "no sessions in %s left; publically marking as nick change", orig_occupant.nick);
471                                         orig_x:tag("status", {code = "303";}):up();
472                                 else -- The session itself always needs to see a nick change
473                                         -- don't want to get our old nick's available presence,
474                                         -- so remove our session from there, and manually generate an unavailable
475                                         orig_occupant:remove_session(real_jid);
476                                         log("debug", "generating nick change for %s", real_jid);
477                                         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
478                                         -- self:build_item_list(orig_occupant, x, false, dest_nick); -- COMPAT: clients get confused if they see other items besides their own
479                                         add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick);
480                                         x:tag("status", {code = "303";}):up();
481                                         x:tag("status", {code = "110";}):up();
482                                         self:route_stanza(generated_unavail:add_child(x));
483                                         dest_nick = nil; -- set dest_nick to nil; so general populance doesn't see it for whole orig_occupant
484                                 end
485                         end
486                         self:save_occupant(orig_occupant);
487                         self:publicise_occupant_status(orig_occupant, orig_x, dest_nick);
488
489                         if is_last_orig_session then
490                                 module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick; occupant = orig_occupant;});
491                         end
492                 end
493
494                 if dest_occupant ~= nil then
495                         dest_occupant:set_session(real_jid, stanza);
496                         local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
497                         if is_new_room then
498                                 dest_x:tag("status", {code = "201"}):up();
499                         end
500                         if orig_occupant == nil and self:get_whois() == "anyone" then
501                                 dest_x:tag("status", {code = "100"}):up();
502                         end
503                         self:save_occupant(dest_occupant);
504
505                         if orig_occupant == nil then
506                                 -- Send occupant list to newly joined user
507                                 self:send_occupant_list(real_jid, function(nick, occupant)
508                                         -- Don't include self
509                                         return occupant:get_presence(real_jid) == nil;
510                                 end)
511                         end
512                         self:publicise_occupant_status(dest_occupant, dest_x);
513
514                         if orig_occupant ~= nil and orig_occupant ~= dest_occupant and not is_last_orig_session then -- If user is swapping and wasn't last original session
515                                 log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick);
516                                 -- Show the original nick joining again
517                                 local pr = st.clone(orig_occupant:get_presence());
518                                 pr.attr.to = real_jid;
519                                 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";});
520                                 self:build_item_list(orig_occupant, x, false);
521                                 -- TODO: new status code to inform client this was the multi-session it left?
522                                 pr:add_child(x);
523                                 self:route_stanza(pr);
524                         end
525
526                         if orig_occupant == nil then
527                                 if is_first_dest_session then
528                                         module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; occupant = dest_occupant;});
529                                 end
530                                 module:fire_event("muc-occupant-session-new", {room = self; nick = dest_occupant.nick; occupant = dest_occupant; stanza = stanza; jid = real_jid;});
531                         end
532                 end
533         elseif type ~= 'result' then -- bad type
534                 if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
535                         origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
536                 end
537         end
538         return true;
539 end
540
541 function room_mt:handle_iq_to_occupant(origin, stanza)
542         local from, to = stanza.attr.from, stanza.attr.to;
543         local type = stanza.attr.type;
544         local id = stanza.attr.id;
545         local occupant = self:get_occupant_by_nick(to);
546         if (type == "error" or type == "result") then
547                 do -- deconstruct_stanza_id
548                         if not occupant then return nil; end
549                         local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(%Z+)%z(%Z*)%z(.+)$");
550                         if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
551                         local from_occupant_jid = self:get_occupant_jid(from_jid);
552                         if from_occupant_jid == nil then return nil; end
553                         local session_jid
554                         for to_jid in occupant:each_session() do
555                                 if md5(to_jid) == to_jid_hash then
556                                         session_jid = to_jid;
557                                         break;
558                                 end
559                         end
560                         if session_jid == nil then return nil; end
561                         stanza.attr.from, stanza.attr.to, stanza.attr.id = from_occupant_jid, session_jid, id;
562                 end
563                 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
564                 self:route_stanza(stanza);
565                 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
566                 return true;
567         else -- Type is "get" or "set"
568                 local current_nick = self:get_occupant_jid(from);
569                 if not current_nick then
570                         origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
571                         return true;
572                 end
573                 if not occupant then -- recipient not in room
574                         origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
575                         return true;
576                 end
577                 do -- construct_stanza_id
578                         stanza.attr.id = base64.encode(occupant.jid.."\0"..stanza.attr.id.."\0"..md5(from));
579                 end
580                 stanza.attr.from, stanza.attr.to = current_nick, occupant.jid;
581                 log("debug", "%s sent private iq stanza to %s (%s)", from, to, occupant.jid);
582                 if stanza.tags[1].attr.xmlns == 'vcard-temp' then
583                         stanza.attr.to = jid_bare(stanza.attr.to);
584                 end
585                 self:route_stanza(stanza);
586                 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
587                 return true;
588         end
589 end
590
591 function room_mt:handle_message_to_occupant(origin, stanza)
592         local from, to = stanza.attr.from, stanza.attr.to;
593         local current_nick = self:get_occupant_jid(from);
594         local type = stanza.attr.type;
595         if not current_nick then -- not in room
596                 if type ~= "error" then
597                         origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
598                 end
599                 return true;
600         end
601         if type == "groupchat" then -- groupchat messages not allowed in PM
602                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
603                 return true;
604         elseif type == "error" and is_kickable_error(stanza) then
605                 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
606                 return self:handle_kickable(origin, stanza); -- send unavailable
607         end
608
609         local o_data = self:get_occupant_by_nick(to);
610         if not o_data then
611                 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
612                 return true;
613         end
614         log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid);
615         stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up();
616         stanza.attr.from = current_nick;
617         self:route_to_occupant(o_data, stanza)
618         -- TODO: Remove x tag?
619         stanza.attr.from = from;
620         return true;
621 end
622
623 function room_mt:send_form(origin, stanza)
624         origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
625                 :add_child(self:get_form_layout(stanza.attr.from):form())
626         );
627 end
628
629 function room_mt:get_form_layout(actor)
630         local form = dataform.new({
631                 title = "Configuration for "..self.jid,
632                 instructions = "Complete and submit this form to configure the room.",
633                 {
634                         name = 'FORM_TYPE',
635                         type = 'hidden',
636                         value = 'http://jabber.org/protocol/muc#roomconfig'
637                 }
638         });
639         return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
640 end
641
642 function room_mt:process_form(origin, stanza)
643         local form = stanza.tags[1]:get_child("x", "jabber:x:data");
644         if form.attr.type == "cancel" then
645                 origin.send(st.reply(stanza));
646         elseif form.attr.type == "submit" then
647                 local fields;
648                 if form.tags[1] == nil then -- Instant room
649                         fields = {};
650                 else
651                         fields = self:get_form_layout(stanza.attr.from):data(form);
652                         if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then
653                                 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration"));
654                                 return true;
655                         end
656                 end
657
658                 local event = {room = self; origin = origin; stanza = stanza; fields = fields; status_codes = {};};
659                 function event.update_option(name, field, allowed)
660                         local new = fields[field];
661                         if new == nil then return; end
662                         if allowed and not allowed[new] then return; end
663                         if new == self["get_"..name](self) then return; end
664                         event.status_codes["104"] = true;
665                         self["set_"..name](self, new);
666                         return true;
667                 end
668                 module:fire_event("muc-config-submitted", event);
669
670                 if self.save then self:save(true); end
671                 origin.send(st.reply(stanza));
672
673                 if next(event.status_codes) then
674                         local msg = st.message({type='groupchat', from=self.jid})
675                                 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
676                         for code in pairs(event.status_codes) do
677                                 msg:tag("status", {code = code;}):up();
678                         end
679                         msg:up();
680                         self:broadcast_message(msg);
681                 end
682         else
683                 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form"));
684         end
685         return true;
686 end
687
688 -- Removes everyone from the room
689 function room_mt:clear(x)
690         x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
691         local occupants_updated = {};
692         for nick, occupant in self:each_occupant() do
693                 occupant.role = nil;
694                 self:save_occupant(occupant);
695                 occupants_updated[occupant] = true;
696         end
697         for occupant in pairs(occupants_updated) do
698                 self:publicise_occupant_status(occupant, x);
699                 module:fire_event("muc-occupant-left", { room = self; nick = occupant.nick; occupant = occupant;});
700         end
701 end
702
703 function room_mt:destroy(newjid, reason, password)
704         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
705                 :tag("item", { affiliation='none', role='none' }):up()
706                 :tag("destroy", {jid=newjid});
707         if reason then x:tag("reason"):text(reason):up(); end
708         if password then x:tag("password"):text(password):up(); end
709         x:up();
710         self:clear(x);
711         module:fire_event("muc-room-destroyed", { room = self });
712 end
713
714 function room_mt:handle_disco_info_get_query(origin, stanza)
715         origin.send(self:get_disco_info(stanza));
716         return true;
717 end
718
719 function room_mt:handle_disco_items_get_query(origin, stanza)
720         origin.send(self:get_disco_items(stanza));
721         return true;
722 end
723
724 function room_mt:handle_admin_query_set_command(origin, stanza)
725         local item = stanza.tags[1].tags[1];
726         if not item then
727                 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
728         end
729         if item.attr.jid then -- Validate provided JID
730                 item.attr.jid = jid_prep(item.attr.jid);
731                 if not item.attr.jid then
732                         origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
733                         return true;
734                 end
735         end
736         if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
737                 local occupant = self:get_occupant_by_nick(self.jid.."/"..item.attr.nick);
738                 if occupant then item.attr.jid = occupant.jid; end
739         elseif not item.attr.nick and item.attr.jid then
740                 local nick = self:get_occupant_jid(item.attr.jid);
741                 if nick then item.attr.nick = select(3, jid_split(nick)); end
742         end
743         local actor = stanza.attr.from;
744         local reason = item:get_child_text("reason");
745         local success, errtype, err
746         if item.attr.affiliation and item.attr.jid and not item.attr.role then
747                 success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, reason);
748         elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
749                 success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, reason);
750         else
751                 success, errtype, err = nil, "cancel", "bad-request";
752         end
753         if not success then
754                 origin.send(st.error_reply(stanza, errtype, err));
755         else
756                 origin.send(st.reply(stanza));
757         end
758         return true;
759 end
760
761 function room_mt:handle_admin_query_get_command(origin, stanza)
762         local actor = stanza.attr.from;
763         local affiliation = self:get_affiliation(actor);
764         local item = stanza.tags[1].tags[1];
765         local _aff = item.attr.affiliation;
766         local _aff_rank = valid_affiliations[_aff or "none"];
767         local _rol = item.attr.role;
768         if _aff and _aff_rank and not _rol then
769                 -- You need to be at least an admin, and be requesting info about your affifiliation or lower
770                 -- e.g. an admin can't ask for a list of owners
771                 local affiliation_rank = valid_affiliations[affiliation or "none"];
772                 if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then
773                         local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
774                         for jid in self:each_affiliation(_aff or "none") do
775                                 reply:tag("item", {affiliation = _aff, jid = jid}):up();
776                         end
777                         origin.send(reply:up());
778                         return true;
779                 else
780                         origin.send(st.error_reply(stanza, "auth", "forbidden"));
781                         return true;
782                 end
783         elseif _rol and valid_roles[_rol or "none"] and not _aff then
784                 local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
785                 if valid_roles[role or "none"] >= valid_roles.moderator then
786                         if _rol == "none" then _rol = nil; end
787                         local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
788                         -- TODO: whois check here? (though fully anonymous rooms are not supported)
789                         for occupant_jid, occupant in self:each_occupant() do
790                                 if occupant.role == _rol then
791                                         local nick = select(3,jid_split(occupant_jid));
792                                         self:build_item_list(occupant, reply, false, nick);
793                                 end
794                         end
795                         origin.send(reply:up());
796                         return true;
797                 else
798                         origin.send(st.error_reply(stanza, "auth", "forbidden"));
799                         return true;
800                 end
801         else
802                 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
803                 return true;
804         end
805 end
806
807 function room_mt:handle_owner_query_get_to_room(origin, stanza)
808         if self:get_affiliation(stanza.attr.from) ~= "owner" then
809                 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
810                 return true;
811         end
812
813         self:send_form(origin, stanza);
814         return true;
815 end
816 function room_mt:handle_owner_query_set_to_room(origin, stanza)
817         if self:get_affiliation(stanza.attr.from) ~= "owner" then
818                 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
819                 return true;
820         end
821
822         local child = stanza.tags[1].tags[1];
823         if not child then
824                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
825                 return true;
826         elseif child.name == "destroy" then
827                 local newjid = child.attr.jid;
828                 local reason = child:get_child_text("reason");
829                 local password = child:get_child_text("password");
830                 self:destroy(newjid, reason, password);
831                 origin.send(st.reply(stanza));
832                 return true;
833         elseif child.name == "x" and child.attr.xmlns == "jabber:x:data" then
834                 return self:process_form(origin, stanza);
835         else
836                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
837                 return true;
838         end
839 end
840
841 function room_mt:handle_groupchat_to_room(origin, stanza)
842         local from = stanza.attr.from;
843         local occupant = self:get_occupant_by_real_jid(from);
844         if module:fire_event("muc-occupant-groupchat", {
845                 room = self; origin = origin; stanza = stanza; from = from; occupant = occupant;
846         }) then return true; end
847         stanza.attr.from = occupant.nick;
848         self:broadcast_message(stanza);
849         stanza.attr.from = from;
850         return true;
851 end
852
853 -- Role check
854 module:hook("muc-occupant-groupchat", function(event)
855         local role_rank = valid_roles[event.occupant and event.occupant.role or "none"];
856         if role_rank <= valid_roles.none then
857                 event.origin.send(st.error_reply(event.stanza, "cancel", "not-acceptable"));
858                 return true;
859         elseif role_rank <= valid_roles.visitor then
860                 event.origin.send(st.error_reply(event.stanza, "auth", "forbidden"));
861                 return true;
862         end
863 end, 50);
864
865 -- hack - some buggy clients send presence updates to the room rather than their nick
866 function room_mt:handle_presence_to_room(origin, stanza)
867         local current_nick = self:get_occupant_jid(stanza.attr.from);
868         local handled
869         if current_nick then
870                 local to = stanza.attr.to;
871                 stanza.attr.to = current_nick;
872                 handled = self:handle_presence_to_occupant(origin, stanza);
873                 stanza.attr.to = to;
874         end
875         return handled;
876 end
877
878 -- Need visitor role or higher to invite
879 module:hook("muc-pre-invite", function(event)
880         local room, stanza = event.room, event.stanza;
881         local _from, _to = stanza.attr.from, stanza.attr.to;
882         local inviter = room:get_occupant_by_real_jid(_from);
883         local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from));
884         if valid_roles[role or "none"] <= valid_roles.visitor then
885                 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
886                 return true;
887         end
888 end);
889
890 function room_mt:handle_mediated_invite(origin, stanza)
891         local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
892         local invitee = jid_prep(payload.attr.to);
893         if not invitee then
894                 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
895                 return true;
896         elseif module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then
897                 return true;
898         end
899         local invite = muc_util.filter_muc_x(st.clone(stanza));
900         invite.attr.from = self.jid;
901         invite.attr.to = invitee;
902         invite:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
903                         :tag('invite', {from = stanza.attr.from;})
904                                 :tag('reason'):text(payload:get_child_text("reason")):up()
905                         :up()
906                 :up();
907         if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then
908                 self:route_stanza(invite);
909         end
910         return true;
911 end
912
913 -- COMPAT: Some older clients expect this
914 module:hook("muc-invite", function(event)
915         local room, stanza = event.room, event.stanza;
916         local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
917         local reason = invite:get_child_text("reason");
918         stanza:tag('x', {xmlns = "jabber:x:conference"; jid = room.jid;})
919                 :text(reason or "")
920         :up();
921 end);
922
923 -- Add a plain message for clients which don't support invites
924 module:hook("muc-invite", function(event)
925         local room, stanza = event.room, event.stanza;
926         if not stanza:get_child("body") then
927                 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
928                 local reason = invite:get_child_text("reason") or "";
929                 stanza:tag("body")
930                         :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
931                 :up();
932         end
933 end);
934
935 function room_mt:handle_mediated_decline(origin, stanza)
936         local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
937         local declinee = jid_prep(payload.attr.to);
938         if not declinee then
939                 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
940                 return true;
941         elseif module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then
942                 return true;
943         end
944         local decline = muc_util.filter_muc_x(st.clone(stanza));
945         decline.attr.from = self.jid;
946         decline.attr.to = declinee;
947         decline:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
948                         :tag("decline", {from = stanza.attr.from})
949                                 :tag("reason"):text(payload:get_child_text("reason")):up()
950                         :up()
951                 :up();
952         if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then
953                 local declinee = decline.attr.to; -- re-fetch, in case event modified it
954                 local occupant
955                 if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid
956                         occupant = self:get_occupant_by_nick(declinee);
957                 end
958                 if occupant then
959                         self:route_to_occupant(occupant, decline);
960                 else
961                         self:route_stanza(decline);
962                 end
963         end
964         return true;
965 end
966
967 -- Add a plain message for clients which don't support declines
968 module:hook("muc-decline", function(event)
969         local room, stanza = event.room, event.stanza;
970         if not stanza:get_child("body") then
971                 local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
972                 local reason = decline:get_child_text("reason") or "";
973                 stanza:tag("body")
974                         :text(decline.attr.from.." declined your invite to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
975                 :up();
976         end
977 end);
978
979 function room_mt:handle_message_to_room(origin, stanza)
980         local type = stanza.attr.type;
981         if type == "groupchat" then
982                 return self:handle_groupchat_to_room(origin, stanza)
983         elseif type == "error" and is_kickable_error(stanza) then
984                 return self:handle_kickable(origin, stanza)
985         elseif type == nil then
986                 local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
987                 if x then
988                         local payload = x.tags[1];
989                         if payload == nil then
990                                 -- fallthrough
991                         elseif payload.name == "invite" and payload.attr.to then
992                                 return self:handle_mediated_invite(origin, stanza)
993                         elseif payload.name == "decline" and payload.attr.to then
994                                 return self:handle_mediated_decline(origin, stanza)
995                         end
996                         origin.send(st.error_reply(stanza, "cancel", "bad-request"));
997                         return true;
998                 end
999         end
1000 end
1001
1002 function room_mt:route_stanza(stanza)
1003         module:send(stanza);
1004 end
1005
1006 function room_mt:get_affiliation(jid)
1007         local node, host, resource = jid_split(jid);
1008         local bare = node and node.."@"..host or host;
1009         local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
1010         if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
1011         return result;
1012 end
1013
1014 -- Iterates over jid, affiliation pairs
1015 function room_mt:each_affiliation(with_affiliation)
1016         if not with_affiliation then
1017                 return pairs(self._affiliations);
1018         else
1019                 return function(_affiliations, jid)
1020                         local affiliation;
1021                         repeat -- Iterate until we get a match
1022                                 jid, affiliation = next(_affiliations, jid);
1023                         until jid == nil or affiliation == with_affiliation
1024                         return jid, affiliation;
1025                 end, self._affiliations, nil
1026         end
1027 end
1028
1029 function room_mt:set_affiliation(actor, jid, affiliation, reason)
1030         if not actor then return nil, "modify", "not-acceptable"; end;
1031
1032         local node, host, resource = jid_split(jid);
1033         if not host then return nil, "modify", "not-acceptable"; end
1034         jid = jid_join(node, host); -- Bare
1035         local is_host_only = node == nil;
1036
1037         if valid_affiliations[affiliation or "none"] == nil then
1038                 return nil, "modify", "not-acceptable";
1039         end
1040         affiliation = affiliation ~= "none" and affiliation or nil; -- coerces `affiliation == false` to `nil`
1041
1042         local target_affiliation = self._affiliations[jid]; -- Raw; don't want to check against host
1043         local is_downgrade = valid_affiliations[target_affiliation or "none"] > valid_affiliations[affiliation or "none"];
1044
1045         if actor == true then
1046                 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
1047         else
1048                 local actor_affiliation = self:get_affiliation(actor);
1049                 if actor_affiliation == "owner" then
1050                         if jid_bare(actor) == jid then -- self change
1051                                 -- need at least one owner
1052                                 local is_last = true;
1053                                 for j in self:each_affiliation("owner") do
1054                                         if j ~= jid then is_last = false; break; end
1055                                 end
1056                                 if is_last then
1057                                         return nil, "cancel", "conflict";
1058                                 end
1059                         end
1060                         -- owners can do anything else
1061                 elseif affiliation == "owner" or affiliation == "admin"
1062                         or actor_affiliation ~= "admin"
1063                         or target_affiliation == "owner" or target_affiliation == "admin" then
1064                         -- Can't demote owners or other admins
1065                         return nil, "cancel", "not-allowed";
1066                 end
1067         end
1068
1069         -- Set in 'database'
1070         self._affiliations[jid] = affiliation;
1071
1072         -- Update roles
1073         local role = self:get_default_role(affiliation);
1074         local role_rank = valid_roles[role or "none"];
1075         local occupants_updated = {}; -- Filled with old roles
1076         for nick, occupant in self:each_occupant() do
1077                 if occupant.bare_jid == jid or (
1078                         -- Outcast can be by host.
1079                         is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host
1080                 ) then
1081                         -- need to publcize in all cases; as affiliation in <item/> has changed.
1082                         occupants_updated[occupant] = occupant.role;
1083                         if occupant.role ~= role and (
1084                                 is_downgrade or
1085                                 valid_roles[occupant.role or "none"] < role_rank -- upgrade
1086                         ) then
1087                                 occupant.role = role;
1088                                 self:save_occupant(occupant);
1089                         end
1090                 end
1091         end
1092
1093         -- Tell the room of the new occupant affiliations+roles
1094         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
1095         if not role then -- getting kicked
1096                 if affiliation == "outcast" then
1097                         x:tag("status", {code="301"}):up(); -- banned
1098                 else
1099                         x:tag("status", {code="321"}):up(); -- affiliation change
1100                 end
1101         end
1102         local is_semi_anonymous = self:get_whois() == "moderators";
1103         for occupant, old_role in pairs(occupants_updated) do
1104                 self:publicise_occupant_status(occupant, x, nil, actor, reason);
1105                 if occupant.role == nil then
1106                         module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
1107                 elseif is_semi_anonymous and
1108                         (old_role == "moderator" and occupant.role ~= "moderator") or
1109                         (old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status
1110                         -- Send everyone else's presences (as jid visibility has changed)
1111                         for real_jid in occupant:each_session() do
1112                                 self:send_occupant_list(real_jid, function(occupant_jid, occupant)
1113                                         return occupant.bare_jid ~= jid;
1114                                 end);
1115                         end
1116                 end
1117         end
1118
1119         if self.save then self:save(); end
1120
1121         module:fire_event("muc-set-affiliation", {
1122                 room = self;
1123                 actor = actor;
1124                 jid = jid;
1125                 affiliation = affiliation or "none";
1126                 reason = reason;
1127                 previous_affiliation = target_affiliation;
1128                 in_room = next(occupants_updated) ~= nil;
1129         });
1130
1131         return true;
1132 end
1133
1134 function room_mt:get_role(nick)
1135         local occupant = self:get_occupant_by_nick(nick);
1136         return occupant and occupant.role or nil;
1137 end
1138
1139 function room_mt:set_role(actor, occupant_jid, role, reason)
1140         if not actor then return nil, "modify", "not-acceptable"; end
1141
1142         local occupant = self:get_occupant_by_nick(occupant_jid);
1143         if not occupant then return nil, "modify", "not-acceptable"; end
1144
1145         if valid_roles[role or "none"] == nil then
1146                 return nil, "modify", "not-acceptable";
1147         end
1148         role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
1149
1150         if actor == true then
1151                 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
1152         else
1153                 -- Can't do anything to other owners or admins
1154                 local occupant_affiliation = self:get_affiliation(occupant.bare_jid);
1155                 if occupant_affiliation == "owner" or occupant_affiliation == "admin" then
1156                         return nil, "cancel", "not-allowed";
1157                 end
1158
1159                 -- If you are trying to give or take moderator role you need to be an owner or admin
1160                 if occupant.role == "moderator" or role == "moderator" then
1161                         local actor_affiliation = self:get_affiliation(actor);
1162                         if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then
1163                                 return nil, "cancel", "not-allowed";
1164                         end
1165                 end
1166
1167                 -- Need to be in the room and a moderator
1168                 local actor_occupant = self:get_occupant_by_real_jid(actor);
1169                 if not actor_occupant or actor_occupant.role ~= "moderator" then
1170                         return nil, "cancel", "not-allowed";
1171                 end
1172         end
1173
1174         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
1175         if not role then
1176                 x:tag("status", {code = "307"}):up();
1177         end
1178         occupant.role = role;
1179         self:save_occupant(occupant);
1180         self:publicise_occupant_status(occupant, x, nil, actor, reason);
1181         if role == nil then
1182                 module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
1183         end
1184         return true;
1185 end
1186
1187 local whois = module:require "muc/whois";
1188 room_mt.get_whois = whois.get;
1189 room_mt.set_whois = whois.set;
1190
1191 local _M = {}; -- module "muc"
1192
1193 function _M.new_room(jid, config)
1194         return setmetatable({
1195                 jid = jid;
1196                 _jid_nick = {};
1197                 _occupants = {};
1198                 _data = {
1199                 };
1200                 _affiliations = {};
1201         }, room_mt);
1202 end
1203
1204 _M.room_mt = room_mt;
1205
1206 return _M;