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