Merge 0.10->trunk
[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;
191         if occupant.role ~= nil then
192                 -- Try to use main jid's presence
193                 local pr = occupant:get_presence();
194                 if pr ~= nil then
195                         base_presence = st.clone(pr);
196                 end
197         end
198         base_presence = base_presence or st.presence {from = occupant.nick; type = "unavailable";};
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(.+)$");
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 item.attr.jid then -- Validate provided JID
727                 item.attr.jid = jid_prep(item.attr.jid);
728                 if not item.attr.jid then
729                         origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
730                         return true;
731                 end
732         end
733         if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
734                 local occupant = self:get_occupant_by_nick(self.jid.."/"..item.attr.nick);
735                 if occupant then item.attr.jid = occupant.jid; end
736         elseif not item.attr.nick and item.attr.jid then
737                 local nick = self:get_occupant_jid(item.attr.jid);
738                 if nick then item.attr.nick = select(3, jid_split(nick)); end
739         end
740         local actor = stanza.attr.from;
741         local reason = item:get_child_text("reason");
742         local success, errtype, err
743         if item.attr.affiliation and item.attr.jid and not item.attr.role then
744                 success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, reason);
745         elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
746                 success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, reason);
747         else
748                 success, errtype, err = nil, "cancel", "bad-request";
749         end
750         if not success then origin.send(st.error_reply(stanza, errtype, err)); end
751         origin.send(st.reply(stanza));
752         return true;
753 end
754
755 function room_mt:handle_admin_query_get_command(origin, stanza)
756         local actor = stanza.attr.from;
757         local affiliation = self:get_affiliation(actor);
758         local item = stanza.tags[1].tags[1];
759         local _aff = item.attr.affiliation;
760         local _aff_rank = valid_affiliations[_aff or "none"];
761         local _rol = item.attr.role;
762         if _aff and _aff_rank and not _rol then
763                 -- You need to be at least an admin, and be requesting info about your affifiliation or lower
764                 -- e.g. an admin can't ask for a list of owners
765                 local affiliation_rank = valid_affiliations[affiliation];
766                 if affiliation_rank >= valid_affiliations.admin and affiliation_rank >= _aff_rank then
767                         local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
768                         for jid in self:each_affiliation(_aff or "none") do
769                                 reply:tag("item", {affiliation = _aff, jid = jid}):up();
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         elseif _rol and valid_roles[_rol or "none"] and not _aff then
778                 local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
779                 if valid_roles[role or "none"] >= valid_roles.moderator then
780                         if _rol == "none" then _rol = nil; end
781                         local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
782                         -- TODO: whois check here? (though fully anonymous rooms are not supported)
783                         for occupant_jid, occupant in self:each_occupant() do
784                                 if occupant.role == _rol then
785                                         local nick = select(3,jid_split(occupant_jid));
786                                         self:build_item_list(occupant, reply, false, nick);
787                                 end
788                         end
789                         origin.send(reply:up());
790                         return true;
791                 else
792                         origin.send(st.error_reply(stanza, "auth", "forbidden"));
793                         return true;
794                 end
795         else
796                 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
797                 return true;
798         end
799 end
800
801 function room_mt:handle_owner_query_get_to_room(origin, stanza)
802         if self:get_affiliation(stanza.attr.from) ~= "owner" then
803                 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
804                 return true;
805         end
806
807         self:send_form(origin, stanza);
808         return true;
809 end
810 function room_mt:handle_owner_query_set_to_room(origin, stanza)
811         if self:get_affiliation(stanza.attr.from) ~= "owner" then
812                 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
813                 return true;
814         end
815
816         local child = stanza.tags[1].tags[1];
817         if not child then
818                 origin.send(st.error_reply(stanza, "modify", "bad-request"));
819                 return true;
820         elseif child.name == "destroy" then
821                 local newjid = child.attr.jid;
822                 local reason = child:get_child_text("reason");
823                 local password = child:get_child_text("password");
824                 self:destroy(newjid, reason, password);
825                 origin.send(st.reply(stanza));
826                 return true;
827         elseif child.name == "x" and child.attr.xmlns == "jabber:x:data" then
828                 return self:process_form(origin, stanza);
829         else
830                 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
831                 return true;
832         end
833 end
834
835 function room_mt:handle_groupchat_to_room(origin, stanza)
836         local from = stanza.attr.from;
837         local occupant = self:get_occupant_by_real_jid(from);
838         if module:fire_event("muc-occupant-groupchat", {
839                 room = self; origin = origin; stanza = stanza; from = from; occupant = occupant;
840         }) then return true; end
841         stanza.attr.from = occupant.nick;
842         self:broadcast_message(stanza);
843         stanza.attr.from = from;
844         return true;
845 end
846
847 -- Role check
848 module:hook("muc-occupant-groupchat", function(event)
849         local role_rank = valid_roles[event.occupant and event.occupant.role or "none"];
850         if role_rank <= valid_roles.none then
851                 event.origin.send(st.error_reply(event.stanza, "cancel", "not-acceptable"));
852                 return true;
853         elseif role_rank <= valid_roles.visitor then
854                 event.origin.send(st.error_reply(event.stanza, "auth", "forbidden"));
855                 return true;
856         end
857 end, 50);
858
859 -- hack - some buggy clients send presence updates to the room rather than their nick
860 function room_mt:handle_presence_to_room(origin, stanza)
861         local current_nick = self:get_occupant_jid(stanza.attr.from);
862         local handled
863         if current_nick then
864                 local to = stanza.attr.to;
865                 stanza.attr.to = current_nick;
866                 handled = self:handle_presence_to_occupant(origin, stanza);
867                 stanza.attr.to = to;
868         end
869         return handled;
870 end
871
872 -- Need visitor role or higher to invite
873 module:hook("muc-pre-invite", function(event)
874         local room, stanza = event.room, event.stanza;
875         local _from, _to = stanza.attr.from, stanza.attr.to;
876         local inviter = room:get_occupant_by_real_jid(_from);
877         local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from));
878         if valid_roles[role or "none"] <= valid_roles.visitor then
879                 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
880                 return true;
881         end
882 end);
883
884 function room_mt:handle_mediated_invite(origin, stanza)
885         local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
886         local invitee = jid_prep(payload.attr.to);
887         if not invitee then
888                 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
889                 return true;
890         elseif module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then
891                 return true;
892         end
893         local invite = muc_util.filter_muc_x(st.clone(stanza));
894         invite.attr.from = self.jid;
895         invite.attr.to = invitee;
896         invite:tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
897                         :tag('invite', {from = stanza.attr.from;})
898                                 :tag('reason'):text(payload:get_child_text("reason")):up()
899                         :up()
900                 :up();
901         if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then
902                 self:route_stanza(invite);
903         end
904         return true;
905 end
906
907 -- COMPAT: Some older clients expect this
908 module:hook("muc-invite", function(event)
909         local room, stanza = event.room, event.stanza;
910         local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
911         local reason = invite:get_child_text("reason");
912         stanza:tag('x', {xmlns = "jabber:x:conference"; jid = room.jid;})
913                 :text(reason or "")
914         :up();
915 end);
916
917 -- Add a plain message for clients which don't support invites
918 module:hook("muc-invite", function(event)
919         local room, stanza = event.room, event.stanza;
920         if not stanza:get_child("body") then
921                 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
922                 local reason = invite:get_child_text("reason") or "";
923                 stanza:tag("body")
924                         :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
925                 :up();
926         end
927 end);
928
929 function room_mt:handle_mediated_decline(origin, stanza)
930         local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
931         local declinee = jid_prep(payload.attr.to);
932         if not declinee then
933                 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
934                 return true;
935         elseif module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then
936                 return true;
937         end
938         local decline = muc_util.filter_muc_x(st.clone(stanza));
939         decline.attr.from = self.jid;
940         decline.attr.to = declinee;
941         decline:tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
942                         :tag("decline", {from = stanza.attr.from})
943                                 :tag("reason"):text(payload:get_child_text("reason")):up()
944                         :up()
945                 :up();
946         if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then
947                 local declinee = decline.attr.to; -- re-fetch, in case event modified it
948                 local occupant
949                 if jid_bare(declinee) == self.jid then -- declinee jid is already an in-room jid
950                         occupant = self:get_occupant_by_nick(declinee);
951                 end
952                 if occupant then
953                         self:route_to_occupant(occupant, decline);
954                 else
955                         self:route_stanza(decline);
956                 end
957         end
958         return true;
959 end
960
961 -- Add a plain message for clients which don't support declines
962 module:hook("muc-decline", function(event)
963         local room, stanza = event.room, event.stanza;
964         if not stanza:get_child("body") then
965                 local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
966                 local reason = decline:get_child_text("reason") or "";
967                 stanza:tag("body")
968                         :text(decline.attr.from.." declined your invite to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
969                 :up();
970         end
971 end);
972
973 function room_mt:handle_message_to_room(origin, stanza)
974         local type = stanza.attr.type;
975         if type == "groupchat" then
976                 return self:handle_groupchat_to_room(origin, stanza)
977         elseif type == "error" and is_kickable_error(stanza) then
978                 return self:handle_kickable(origin, stanza)
979         elseif type == nil then
980                 local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
981                 if x then
982                         local payload = x.tags[1];
983                         if payload == nil then
984                                 -- fallthrough
985                         elseif payload.name == "invite" and payload.attr.to then
986                                 return self:handle_mediated_invite(origin, stanza)
987                         elseif payload.name == "decline" and payload.attr.to then
988                                 return self:handle_mediated_decline(origin, stanza)
989                         end
990                         origin.send(st.error_reply(stanza, "cancel", "bad-request"));
991                         return true;
992                 end
993         end
994 end
995
996 function room_mt:route_stanza(stanza)
997         module:send(stanza);
998 end
999
1000 function room_mt:get_affiliation(jid)
1001         local node, host, resource = jid_split(jid);
1002         local bare = node and node.."@"..host or host;
1003         local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
1004         if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
1005         return result;
1006 end
1007
1008 -- Iterates over jid, affiliation pairs
1009 function room_mt:each_affiliation(with_affiliation)
1010         if not with_affiliation then
1011                 return pairs(self._affiliations);
1012         else
1013                 return function(_affiliations, jid)
1014                         local affiliation;
1015                         repeat -- Iterate until we get a match
1016                                 jid, affiliation = next(_affiliations, jid);
1017                         until jid == nil or affiliation == with_affiliation
1018                         return jid, affiliation;
1019                 end, self._affiliations, nil
1020         end
1021 end
1022
1023 function room_mt:set_affiliation(actor, jid, affiliation, reason)
1024         if not actor then return nil, "modify", "not-acceptable"; end;
1025
1026         local node, host, resource = jid_split(jid);
1027         if not host then return nil, "modify", "not-acceptable"; end
1028         jid = jid_join(node, host); -- Bare
1029         local is_host_only = node == nil;
1030
1031         if valid_affiliations[affiliation or "none"] == nil then
1032                 return nil, "modify", "not-acceptable";
1033         end
1034         affiliation = affiliation ~= "none" and affiliation or nil; -- coerces `affiliation == false` to `nil`
1035
1036         local target_affiliation = self._affiliations[jid]; -- Raw; don't want to check against host
1037         local is_downgrade = valid_affiliations[target_affiliation or "none"] > valid_affiliations[affiliation or "none"];
1038
1039         if actor == true then
1040                 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
1041         else
1042                 local actor_affiliation = self:get_affiliation(actor);
1043                 if actor_affiliation == "owner" then
1044                         if jid_bare(actor) == jid then -- self change
1045                                 -- need at least one owner
1046                                 local is_last = true;
1047                                 for j in self:each_affiliation("owner") do
1048                                         if j ~= jid then is_last = false; break; end
1049                                 end
1050                                 if is_last then
1051                                         return nil, "cancel", "conflict";
1052                                 end
1053                         end
1054                         -- owners can do anything else
1055                 elseif affiliation == "owner" or affiliation == "admin"
1056                         or actor_affiliation ~= "admin"
1057                         or target_affiliation == "owner" or target_affiliation == "admin" then
1058                         -- Can't demote owners or other admins
1059                         return nil, "cancel", "not-allowed";
1060                 end
1061         end
1062
1063         -- Set in 'database'
1064         self._affiliations[jid] = affiliation;
1065
1066         -- Update roles
1067         local role = self:get_default_role(affiliation);
1068         local role_rank = valid_roles[role or "none"];
1069         local occupants_updated = {}; -- Filled with old roles
1070         for nick, occupant in self:each_occupant() do
1071                 if occupant.bare_jid == jid or (
1072                         -- Outcast can be by host.
1073                         is_host_only and affiliation == "outcast" and select(2, jid_split(occupant.bare_jid)) == host
1074                 ) then
1075                         -- need to publcize in all cases; as affiliation in <item/> has changed.
1076                         occupants_updated[occupant] = occupant.role;
1077                         if occupant.role ~= role and (
1078                                 is_downgrade or
1079                                 valid_roles[occupant.role or "none"] < role_rank -- upgrade
1080                         ) then
1081                                 occupant.role = role;
1082                                 self:save_occupant(occupant);
1083                         end
1084                 end
1085         end
1086
1087         -- Tell the room of the new occupant affiliations+roles
1088         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
1089         if not role then -- getting kicked
1090                 if affiliation == "outcast" then
1091                         x:tag("status", {code="301"}):up(); -- banned
1092                 else
1093                         x:tag("status", {code="321"}):up(); -- affiliation change
1094                 end
1095         end
1096         local is_semi_anonymous = self:get_whois() == "moderators";
1097         for occupant, old_role in pairs(occupants_updated) do
1098                 self:publicise_occupant_status(occupant, x, nil, actor, reason);
1099                 if occupant.role == nil then
1100                         module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
1101                 elseif is_semi_anonymous and
1102                         (old_role == "moderator" and occupant.role ~= "moderator") or
1103                         (old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status
1104                         -- Send everyone else's presences (as jid visibility has changed)
1105                         for real_jid in occupant:each_session() do
1106                                 self:send_occupant_list(real_jid, function(occupant_jid, occupant)
1107                                         return occupant.bare_jid ~= jid;
1108                                 end);
1109                         end
1110                 end
1111         end
1112
1113         if self.save then self:save(); end
1114
1115         module:fire_event("muc-set-affiliation", {
1116                 room = self;
1117                 actor = actor;
1118                 jid = jid;
1119                 affiliation = affiliation or "none";
1120                 reason = reason;
1121                 previous_affiliation = target_affiliation;
1122                 in_room = next(occupants_updated) ~= nil;
1123         });
1124
1125         return true;
1126 end
1127
1128 function room_mt:get_role(nick)
1129         local occupant = self:get_occupant_by_nick(nick);
1130         return occupant and occupant.role or nil;
1131 end
1132
1133 function room_mt:set_role(actor, occupant_jid, role, reason)
1134         if not actor then return nil, "modify", "not-acceptable"; end
1135
1136         local occupant = self:get_occupant_by_nick(occupant_jid);
1137         if not occupant then return nil, "modify", "not-acceptable"; end
1138
1139         if valid_roles[role or "none"] == nil then
1140                 return nil, "modify", "not-acceptable";
1141         end
1142         role = role ~= "none" and role or nil; -- coerces `role == false` to `nil`
1143
1144         if actor == true then
1145                 actor = nil -- So we can pass it safely to 'publicise_occupant_status' below
1146         else
1147                 -- Can't do anything to other owners or admins
1148                 local occupant_affiliation = self:get_affiliation(occupant.bare_jid);
1149                 if occupant_affiliation == "owner" and occupant_affiliation == "admin" then
1150                         return nil, "cancel", "not-allowed";
1151                 end
1152
1153                 -- If you are trying to give or take moderator role you need to be an owner or admin
1154                 if occupant.role == "moderator" or role == "moderator" then
1155                         local actor_affiliation = self:get_affiliation(actor);
1156                         if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then
1157                                 return nil, "cancel", "not-allowed";
1158                         end
1159                 end
1160
1161                 -- Need to be in the room and a moderator
1162                 local actor_occupant = self:get_occupant_by_real_jid(actor);
1163                 if not actor_occupant or actor_occupant.role ~= "moderator" then
1164                         return nil, "cancel", "not-allowed";
1165                 end
1166         end
1167
1168         local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"});
1169         if not role then
1170                 x:tag("status", {code = "307"}):up();
1171         end
1172         occupant.role = role;
1173         self:save_occupant(occupant);
1174         self:publicise_occupant_status(occupant, x, nil, actor, reason);
1175         if role == nil then
1176                 module:fire_event("muc-occupant-left", {room = self; nick = occupant.nick; occupant = occupant;});
1177         end
1178         return true;
1179 end
1180
1181 local affiliation_notify = module:require "muc/affiliation_notify";
1182
1183 local name = module:require "muc/name";
1184 room_mt.get_name = name.get;
1185 room_mt.set_name = name.set;
1186
1187 local description = module:require "muc/description";
1188 room_mt.get_description = description.get;
1189 room_mt.set_description = description.set;
1190
1191 local hidden = module:require "muc/hidden";
1192 room_mt.get_hidden = hidden.get;
1193 room_mt.set_hidden = hidden.set;
1194 function room_mt:get_public()
1195         return not self:get_hidden();
1196 end
1197 function room_mt:set_public(public)
1198         return self:set_hidden(not public);
1199 end
1200
1201 local password = module:require "muc/password";
1202 room_mt.get_password = password.get;
1203 room_mt.set_password = password.set;
1204
1205 local whois = module:require "muc/whois";
1206 room_mt.get_whois = whois.get;
1207 room_mt.set_whois = whois.set;
1208
1209 local members_only = module:require "muc/members_only";
1210 room_mt.get_members_only = members_only.get;
1211 room_mt.set_members_only = members_only.set;
1212
1213 local moderated = module:require "muc/moderated";
1214 room_mt.get_moderated = moderated.get;
1215 room_mt.set_moderated = moderated.set;
1216
1217 local persistent = module:require "muc/persistent";
1218 room_mt.get_persistent = persistent.get;
1219 room_mt.set_persistent = persistent.set;
1220
1221 local subject = module:require "muc/subject";
1222 room_mt.get_changesubject = subject.get_changesubject;
1223 room_mt.set_changesubject = subject.set_changesubject;
1224 room_mt.get_subject = subject.get;
1225 room_mt.set_subject = subject.set;
1226 room_mt.send_subject = subject.send;
1227
1228 local history = module:require "muc/history";
1229 room_mt.send_history = history.send;
1230 room_mt.get_historylength = history.get_length;
1231 room_mt.set_historylength = history.set_length;
1232
1233 local _M = {}; -- module "muc"
1234
1235 function _M.new_room(jid, config)
1236         return setmetatable({
1237                 jid = jid;
1238                 _jid_nick = {};
1239                 _occupants = {};
1240                 _data = {
1241                 };
1242                 _affiliations = {};
1243         }, room_mt);
1244 end
1245
1246 _M.room_mt = room_mt;
1247
1248 return _M;