2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local select = select;
11 local pairs, ipairs = pairs, ipairs;
13 local gettime = os.time;
14 local datetime = require "util.datetime";
16 local dataform = require "util.dataforms";
18 local jid_split = require "util.jid".split;
19 local jid_bare = require "util.jid".bare;
20 local jid_prep = require "util.jid".prep;
21 local st = require "util.stanza";
22 local log = require "util.logger".init("mod_muc");
23 local t_insert, t_remove = table.insert, table.remove;
24 local setmetatable = setmetatable;
25 local base64 = require "util.encodings".base64;
26 local md5 = require "util.hashes".md5;
28 local default_history_length, max_history_length = 20, math.huge;
30 local get_filtered_presence do
31 local presence_filters = {
32 ["http://jabber.org/protocol/muc"] = true;
33 ["http://jabber.org/protocol/muc#user"] = true;
35 local function presence_filter(tag)
36 if presence_filters[tag.attr.xmlns] then
41 function get_filtered_presence(stanza)
42 return st.clone(stanza):maptags(presence_filter);
46 local is_kickable_error do
47 local kickable_error_conditions = {
49 ["internal-server-error"] = true;
50 ["item-not-found"] = true;
51 ["jid-malformed"] = true;
52 ["recipient-unavailable"] = true;
54 ["remote-server-not-found"] = true;
55 ["remote-server-timeout"] = true;
56 ["service-unavailable"] = true;
57 ["malformed error"] = true;
59 function is_kickable_error(stanza)
60 local cond = select(2, stanza:get_error()) or "malformed error";
61 return kickable_error_conditions[cond];
66 room_mt.__index = room_mt;
68 function room_mt:__tostring()
69 return "MUC room ("..self.jid..")";
72 function room_mt:get_occupant_jid(real_jid)
73 return self._jid_nick[real_jid]
76 function room_mt:get_default_role(affiliation)
77 if affiliation == "owner" or affiliation == "admin" then
79 elseif affiliation == "member" then
81 elseif not affiliation then
82 if not self:get_members_only() then
83 return self:get_moderated() and "visitor" or "participant";
88 function room_mt:lock()
91 function room_mt:unlock()
92 module:fire_event("muc-room-unlocked", { room = self });
95 function room_mt:is_locked()
96 return not not self.locked
99 function room_mt:route_to_occupant(o_data, stanza)
100 local to = stanza.attr.to;
101 for jid in pairs(o_data.sessions) do
102 stanza.attr.to = jid;
103 self:_route_stanza(stanza);
108 function room_mt:broadcast_presence(stanza, sid, code, nick)
109 stanza = get_filtered_presence(stanza);
110 local occupant = self._occupants[stanza.attr.from];
111 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
112 :tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none", nick=nick}):up();
114 stanza:tag("status", {code=code}):up();
116 self:broadcast_except_nick(stanza, stanza.attr.from);
117 stanza:tag("status", {code='110'}):up();
118 stanza.attr.to = sid;
119 self:_route_stanza(stanza);
121 function room_mt:broadcast_message(stanza, historic)
122 module:fire_event("muc-broadcast-message", {room = self, stanza = stanza, historic = historic});
123 self:broadcast(stanza);
127 module:hook("muc-broadcast-message", function(event)
128 if event.historic then
129 local room = event.room
130 local history = room._data['history'];
131 if not history then history = {}; room._data['history'] = history; end
132 local stanza = st.clone(event.stanza);
134 local ts = gettime();
135 local stamp = datetime.datetime(ts);
136 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp}):up(); -- XEP-0203
137 stanza:tag("x", {xmlns = "jabber:x:delay", from = module.host, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated)
138 local entry = { stanza = stanza, timestamp = ts };
139 t_insert(history, entry);
140 while #history > room:get_historylength() do t_remove(history, 1) end
144 function room_mt:broadcast_except_nick(stanza, nick)
145 return self:broadcast(stanza, function(rnick, occupant) return rnick ~= nick end)
148 -- Broadcast a stanza to all occupants in the room.
149 -- optionally checks conditional called with nicl
150 function room_mt:broadcast(stanza, cond_func)
151 for nick, occupant in pairs(self._occupants) do
152 if cond_func == nil or cond_func(nick, occupant) then
153 self:route_to_occupant(occupant, stanza)
158 function room_mt:send_occupant_list(to)
159 local current_nick = self:get_occupant_jid(to);
160 for occupant, o_data in pairs(self._occupants) do
161 if occupant ~= current_nick then
162 local pres = get_filtered_presence(o_data.sessions[o_data.jid]);
163 pres.attr.to, pres.attr.from = to, occupant;
164 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
165 :tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up();
166 self:_route_stanza(pres);
171 local function parse_history(stanza)
172 local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc");
173 local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc");
174 if not history_tag then
178 local maxchars = tonumber(history_tag.attr.maxchars);
180 local maxstanzas = tonumber(history_tag.attr.maxstanzas);
182 -- messages received since the UTC datetime specified
183 local since = history_tag.attr.since;
185 since = datetime.parse(since);
188 -- messages received in the last "X" seconds.
189 local seconds = tonumber(history_tag.attr.seconds);
191 seconds = gettime() - seconds
193 since = math.max(since, seconds);
199 return maxchars, maxstanzas, since
202 module:hook("muc-get-history", function(event)
203 local room = event.room
204 local history = room._data['history']; -- send discussion history
205 if not history then return nil end
206 local history_len = #history
209 local maxchars = event.maxchars
210 local maxstanzas = event.maxstanzas or history_len
211 local since = event.since
214 for i=history_len,1,-1 do
215 local entry = history[i];
217 if not entry.chars then
218 entry.stanza.attr.to = "";
219 entry.chars = #tostring(entry.stanza);
221 charcount = charcount + entry.chars + #to;
222 if charcount > maxchars then break; end
224 if since and since > entry.timestamp then break; end
225 if n + 1 > maxstanzas then break; end
229 local i = history_len-n+1
230 function event:next_stanza()
231 if i > history_len then return nil end
232 local entry = history[i]
233 local msg = entry.stanza
241 function room_mt:send_history(stanza)
242 local maxchars, maxstanzas, since = parse_history(stanza)
245 to = stanza.attr.from; -- `to` is required to calculate the character count for `maxchars`
246 maxchars = maxchars, maxstanzas = maxstanzas, since = since;
247 next_stanza = function() end; -- events should define this iterator
249 module:fire_event("muc-get-history", event)
250 for msg in event.next_stanza , event do
251 self:_route_stanza(msg);
255 function room_mt:get_disco_info(stanza)
256 local count = 0; for _ in pairs(self._occupants) do count = count + 1; end
257 return st.reply(stanza):query("http://jabber.org/protocol/disco#info")
258 :tag("identity", {category="conference", type="text", name=self:get_name()}):up()
259 :tag("feature", {var="http://jabber.org/protocol/muc"}):up()
260 :tag("feature", {var=self:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up()
261 :tag("feature", {var=self:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up()
262 :tag("feature", {var=self:get_members_only() and "muc_membersonly" or "muc_open"}):up()
263 :tag("feature", {var=self:get_persistent() and "muc_persistent" or "muc_temporary"}):up()
264 :tag("feature", {var=self:get_hidden() and "muc_hidden" or "muc_public"}):up()
265 :tag("feature", {var=self:get_whois() ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up()
266 :add_child(dataform.new({
267 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" },
268 { name = "muc#roominfo_description", label = "Description", value = "" },
269 { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) }
270 }):form({["muc#roominfo_description"] = self:get_description()}, 'result'))
273 function room_mt:get_disco_items(stanza)
274 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items");
275 for room_jid in pairs(self._occupants) do
276 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up();
281 function room_mt:get_subject()
282 return self._data['subject'], self._data['subject_from']
284 local function create_subject_message(subject)
285 return st.message({type='groupchat'})
286 :tag('subject'):text(subject):up();
288 function room_mt:send_subject(to)
289 local from, subject = self:get_subject()
291 local msg = create_subject_message(subject)
294 self:_route_stanza(msg);
297 function room_mt:set_subject(current_nick, subject)
298 if subject == "" then subject = nil; end
299 self._data['subject'] = subject;
300 self._data['subject_from'] = current_nick;
301 if self.save then self:save(); end
302 local msg = create_subject_message(subject)
303 msg.attr.from = current_nick
304 self:broadcast_message(msg, false);
308 function room_mt:handle_kickable(origin, stanza)
309 local type, condition, text = stanza:get_error();
310 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error");
312 error_message = error_message..": "..text;
314 local kick_stanza = st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to})
315 :tag('status'):text(error_message);
316 self:handle_unavailable_to_occupant(origin, kick_stanza); -- send unavailable
320 function room_mt:set_name(name)
321 if name == "" or type(name) ~= "string" or name == (jid_split(self.jid)) then name = nil; end
322 if self._data.name ~= name then
323 self._data.name = name;
324 if self.save then self:save(true); end
327 function room_mt:get_name()
328 return self._data.name or jid_split(self.jid);
330 function room_mt:set_description(description)
331 if description == "" or type(description) ~= "string" then description = nil; end
332 if self._data.description ~= description then
333 self._data.description = description;
334 if self.save then self:save(true); end
337 function room_mt:get_description()
338 return self._data.description;
340 function room_mt:set_password(password)
341 if password == "" or type(password) ~= "string" then password = nil; end
342 if self._data.password ~= password then
343 self._data.password = password;
344 if self.save then self:save(true); end
347 function room_mt:get_password()
348 return self._data.password;
350 function room_mt:set_moderated(moderated)
351 moderated = moderated and true or nil;
352 if self._data.moderated ~= moderated then
353 self._data.moderated = moderated;
354 if self.save then self:save(true); end
357 function room_mt:get_moderated()
358 return self._data.moderated;
360 function room_mt:set_members_only(members_only)
361 members_only = members_only and true or nil;
362 if self._data.members_only ~= members_only then
363 self._data.members_only = members_only;
364 if self.save then self:save(true); end
367 function room_mt:get_members_only()
368 return self._data.members_only;
370 function room_mt:set_persistent(persistent)
371 persistent = persistent and true or nil;
372 if self._data.persistent ~= persistent then
373 self._data.persistent = persistent;
374 if self.save then self:save(true); end
377 function room_mt:get_persistent()
378 return self._data.persistent;
380 function room_mt:set_hidden(hidden)
381 hidden = hidden and true or nil;
382 if self._data.hidden ~= hidden then
383 self._data.hidden = hidden;
384 if self.save then self:save(true); end
387 function room_mt:get_hidden()
388 return self._data.hidden;
390 function room_mt:get_public()
391 return not self:get_hidden();
393 function room_mt:set_public(public)
394 return self:set_hidden(not public);
396 function room_mt:set_changesubject(changesubject)
397 changesubject = changesubject and true or nil;
398 if self._data.changesubject ~= changesubject then
399 self._data.changesubject = changesubject;
400 if self.save then self:save(true); end
403 function room_mt:get_changesubject()
404 return self._data.changesubject;
406 function room_mt:get_historylength()
407 return self._data.history_length or default_history_length;
409 function room_mt:set_historylength(length)
410 length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge);
411 if length == default_history_length then
414 self._data.history_length = length;
418 local valid_whois = { moderators = true, anyone = true };
420 function room_mt:set_whois(whois)
421 if valid_whois[whois] and self._data.whois ~= whois then
422 self._data.whois = whois;
423 if self.save then self:save(true); end
427 function room_mt:get_whois()
428 return self._data.whois;
431 function room_mt:handle_unavailable_to_occupant(origin, stanza)
432 local from = stanza.attr.from;
433 local current_nick = self:get_occupant_jid(from);
434 if not current_nick then
435 return true; -- discard
437 local pr = get_filtered_presence(stanza);
438 pr.attr.from = current_nick;
439 log("debug", "%s leaving %s", current_nick, self.jid);
440 self._jid_nick[from] = nil;
441 local occupant = self._occupants[current_nick];
442 local new_jid = next(occupant.sessions);
443 if new_jid == from then new_jid = next(occupant.sessions, new_jid); end
445 local jid = occupant.jid;
446 occupant.jid = new_jid;
447 occupant.sessions[from] = nil;
449 pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
450 :tag("item", {affiliation=occupant.affiliation or "none", role='none'}):up()
451 :tag("status", {code='110'}):up();
452 self:_route_stanza(pr);
453 if jid ~= new_jid then
454 pr = st.clone(occupant.sessions[new_jid])
455 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
456 :tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none"});
457 pr.attr.from = current_nick;
458 self:broadcast_except_nick(pr, current_nick);
461 occupant.role = 'none';
462 self:broadcast_presence(pr, from);
463 self._occupants[current_nick] = nil;
464 module:fire_event("muc-occupant-left", { room = self; nick = current_nick; });
469 function room_mt:handle_occupant_presence(origin, stanza)
470 local from = stanza.attr.from;
471 local pr = get_filtered_presence(stanza);
472 local current_nick = stanza.attr.to
473 pr.attr.from = current_nick;
474 log("debug", "%s broadcasted presence", current_nick);
475 self._occupants[current_nick].sessions[from] = pr;
476 self:broadcast_presence(pr, from);
480 function room_mt:handle_change_nick(origin, stanza, current_nick, to)
481 local from = stanza.attr.from;
482 local occupant = self._occupants[current_nick];
483 local is_multisession = next(occupant.sessions, next(occupant.sessions));
484 if self._occupants[to] or is_multisession then
485 log("debug", "%s couldn't change nick", current_nick);
486 local reply = st.error_reply(stanza, "cancel", "conflict"):up();
487 reply.tags[1].attr.code = "409";
488 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
491 local to_nick = select(3, jid_split(to));
492 log("debug", "%s (%s) changing nick to %s", current_nick, occupant.jid, to);
493 local p = st.presence({type='unavailable', from=current_nick});
494 self:broadcast_presence(p, from, '303', to_nick);
495 self._occupants[current_nick] = nil;
496 self._occupants[to] = occupant;
497 self._jid_nick[from] = to;
498 local pr = get_filtered_presence(stanza);
500 self._occupants[to].sessions[from] = pr;
501 self:broadcast_presence(pr, from);
506 module:hook("muc-occupant-pre-join", function(event)
507 return module:fire_event("muc-occupant-pre-join/affiliation", event)
508 or module:fire_event("muc-occupant-pre-join/password", event)
509 or module:fire_event("muc-occupant-pre-join/locked", event)
510 or module:fire_event("muc-occupant-pre-join/nick-conflict", event)
513 module:hook("muc-occupant-pre-join/password", function(event)
514 local room, stanza = event.room, event.stanza;
515 local from, to = stanza.attr.from, stanza.attr.to;
516 local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
517 password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
518 if not password or password == "" then password = nil; end
519 if room:get_password() ~= password then
520 local from, to = stanza.attr.from, stanza.attr.to;
521 log("debug", "%s couldn't join due to invalid password: %s", from, to);
522 local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
523 reply.tags[1].attr.code = "401";
524 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
529 module:hook("muc-occupant-pre-join/nick-conflict", function(event)
530 local room, stanza = event.room, event.stanza;
531 local from, to = stanza.attr.from, stanza.attr.to;
532 local occupant = room._occupants[to]
533 if occupant -- occupant already exists
534 and jid_bare(from) ~= jid_bare(occupant.jid) then -- and has different bare real jid
535 log("debug", "%s couldn't join due to nick conflict: %s", from, to);
536 local reply = st.error_reply(stanza, "cancel", "conflict"):up();
537 reply.tags[1].attr.code = "409";
538 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
543 module:hook("muc-occupant-pre-join/locked", function(event)
544 if event.room:is_locked() then -- Deny entry
545 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found"));
550 function room_mt:handle_join(origin, stanza)
551 local from, to = stanza.attr.from, stanza.attr.to;
552 local affiliation = self:get_affiliation(from);
553 if affiliation == nil and next(self._affiliations) == nil then -- new room, no owners
554 affiliation = "owner";
555 self._affiliations[jid_bare(from)] = affiliation;
556 if self:is_locked() and not stanza:get_child("x", "http://jabber.org/protocol/muc") then
557 self:unlock(); -- Older groupchat protocol doesn't lock
560 if module:fire_event("muc-occupant-pre-join", {
564 affiliation = affiliation;
565 }) then return true; end
566 log("debug", "%s joining as %s", from, to);
568 local role = self:get_default_role(affiliation)
569 if role then -- new occupant
570 local is_merge = not not self._occupants[to]
572 self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}};
574 self._occupants[to].sessions[from] = get_filtered_presence(stanza);
576 self._jid_nick[from] = to;
577 self:send_occupant_list(from);
578 local pr = get_filtered_presence(stanza);
580 pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'})
581 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up();
583 self:broadcast_except_nick(pr, to);
585 pr:tag("status", {code='110'}):up();
586 if self:get_whois() == 'anyone' then
587 pr:tag("status", {code='100'}):up();
589 if self:is_locked() then
590 pr:tag("status", {code='201'}):up();
593 self:_route_stanza(pr);
594 self:send_history(from, stanza);
595 self:send_subject(from);
600 -- registration required for entering members-only room
601 module:hook("muc-occupant-pre-join/affiliation", function(event)
602 if event.affiliation == nil and event.room:get_members_only() then
603 local reply = st.error_reply(event.stanza, "auth", "registration-required"):up();
604 reply.tags[1].attr.code = "407";
605 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
611 module:hook("muc-occupant-pre-join/affiliation", function(event)
612 if event.affiliation == "outcast" then
613 local reply = st.error_reply(event.stanza, "auth", "forbidden"):up();
614 reply.tags[1].attr.code = "403";
615 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
620 function room_mt:handle_available_to_occupant(origin, stanza)
621 local from, to = stanza.attr.from, stanza.attr.to;
622 local current_nick = self:get_occupant_jid(from);
624 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence
625 if current_nick == to then -- simple presence
626 return self:handle_occupant_presence(origin, stanza)
628 return self:handle_change_nick(origin, stanza, current_nick, to)
630 --else -- possible rejoin
631 -- log("debug", "%s had connection replaced", current_nick);
632 -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to})
633 -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable
634 -- self:handle_to_occupant(origin, stanza); -- resend available
637 return self:handle_join(origin, stanza)
641 function room_mt:handle_presence_to_occupant(origin, stanza)
642 local type = stanza.attr.type;
643 if type == "error" then -- error, kick em out!
644 return self:handle_kickable(origin, stanza)
645 elseif type == "unavailable" then -- unavailable
646 return self:handle_unavailable_to_occupant(origin, stanza)
647 elseif not type then -- available
648 return self:handle_available_to_occupant(origin, stanza)
649 elseif type ~= 'result' then -- bad type
650 if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences
651 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error?
657 function room_mt:handle_iq_to_occupant(origin, stanza)
658 local from, to = stanza.attr.from, stanza.attr.to;
659 local type = stanza.attr.type;
660 local id = stanza.attr.id;
661 local current_nick = self:get_occupant_jid(from);
662 local o_data = self._occupants[to];
663 if (type == "error" or type == "result") then
664 do -- deconstruct_stanza_id
665 if not current_nick or not o_data then return nil; end
666 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$");
667 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end
669 for to_jid in pairs(o_data.sessions) do
670 if md5(to_jid) == to_jid_hash then
671 session_jid = to_jid;
675 if session_jid == nil then return nil; end
676 stanza.attr.from, stanza.attr.to, stanza.attr.id = current_nick, session_jid, id
678 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to);
679 self:_route_stanza(stanza);
680 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
682 else -- Type is "get" or "set"
683 if not current_nick then
684 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
687 if not o_data then -- recipient not in room
688 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
691 do -- construct_stanza_id
692 stanza.attr.id = base64.encode(o_data.jid.."\0"..stanza.attr.id.."\0"..md5(from));
694 stanza.attr.from, stanza.attr.to = current_nick, o_data.jid;
695 log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid);
696 if stanza.tags[1].attr.xmlns == 'vcard-temp' then
697 stanza.attr.to = jid_bare(stanza.attr.to);
699 self:_route_stanza(stanza);
700 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id;
705 function room_mt:handle_message_to_occupant(origin, stanza)
706 local from, to = stanza.attr.from, stanza.attr.to;
707 local current_nick = self:get_occupant_jid(from);
708 local type = stanza.attr.type;
709 if not current_nick then -- not in room
710 if type ~= "error" then
711 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
715 if type == "groupchat" then -- groupchat messages not allowed in PM
716 origin.send(st.error_reply(stanza, "modify", "bad-request"));
718 elseif type == "error" and is_kickable_error(stanza) then
719 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid);
720 return self:handle_kickable(origin, stanza); -- send unavailable
723 local o_data = self._occupants[to];
725 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
728 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid);
729 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up();
730 stanza.attr.from = current_nick;
731 self:route_to_occupant(o_data, stanza)
732 stanza.attr.from = from;
736 function room_mt:send_form(origin, stanza)
737 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
738 :add_child(self:get_form_layout(stanza.attr.from):form())
742 function room_mt:get_form_layout(actor)
743 local whois = self:get_whois()
744 local form = dataform.new({
745 title = "Configuration for "..self.jid,
746 instructions = "Complete and submit this form to configure the room.",
750 value = 'http://jabber.org/protocol/muc#roomconfig'
753 name = 'muc#roomconfig_roomname',
754 type = 'text-single',
756 value = self:get_name() or "",
759 name = 'muc#roomconfig_roomdesc',
760 type = 'text-single',
761 label = 'Description',
762 value = self:get_description() or "",
765 name = 'muc#roomconfig_persistentroom',
767 label = 'Make Room Persistent?',
768 value = self:get_persistent()
771 name = 'muc#roomconfig_publicroom',
773 label = 'Make Room Publicly Searchable?',
774 value = not self:get_hidden()
777 name = 'muc#roomconfig_changesubject',
779 label = 'Allow Occupants to Change Subject?',
780 value = self:get_changesubject()
783 name = 'muc#roomconfig_whois',
784 type = 'list-single',
785 label = 'Who May Discover Real JIDs?',
787 { value = 'moderators', label = 'Moderators Only', default = whois == 'moderators' },
788 { value = 'anyone', label = 'Anyone', default = whois == 'anyone' }
792 name = 'muc#roomconfig_roomsecret',
793 type = 'text-private',
795 value = self:get_password() or "",
798 name = 'muc#roomconfig_moderatedroom',
800 label = 'Make Room Moderated?',
801 value = self:get_moderated()
804 name = 'muc#roomconfig_membersonly',
806 label = 'Make Room Members-Only?',
807 value = self:get_members_only()
810 name = 'muc#roomconfig_historylength',
811 type = 'text-single',
812 label = 'Maximum Number of History Messages Returned by Room',
813 value = tostring(self:get_historylength())
816 return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
819 function room_mt:process_form(origin, stanza)
820 local query = stanza.tags[1];
821 local form = query:get_child("x", "jabber:x:data")
822 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
823 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
824 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end
826 local fields = self:get_form_layout(stanza.attr.from):data(form);
827 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); return; end
832 local function handle_option(name, field, allowed)
833 local new = fields[field];
834 if new == nil then return; end
835 if allowed and not allowed[new] then return; end
836 if new == self["get_"..name](self) then return; end
837 changed[name] = true;
838 self["set_"..name](self, new);
841 local event = { room = self, fields = fields, changed = changed, stanza = stanza, origin = origin, update_option = handle_option };
842 module:fire_event("muc-config-submitted", event);
844 handle_option("name", "muc#roomconfig_roomname");
845 handle_option("description", "muc#roomconfig_roomdesc");
846 handle_option("persistent", "muc#roomconfig_persistentroom");
847 handle_option("moderated", "muc#roomconfig_moderatedroom");
848 handle_option("members_only", "muc#roomconfig_membersonly");
849 handle_option("public", "muc#roomconfig_publicroom");
850 handle_option("changesubject", "muc#roomconfig_changesubject");
851 handle_option("historylength", "muc#roomconfig_historylength");
852 handle_option("whois", "muc#roomconfig_whois", valid_whois);
853 handle_option("password", "muc#roomconfig_roomsecret");
855 if self.save then self:save(true); end
856 if self:is_locked() then
859 origin.send(st.reply(stanza));
861 if next(changed) then
862 local msg = st.message({type='groupchat', from=self.jid})
863 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up()
864 :tag('status', {code = '104'}):up();
865 if changed.whois then
866 local code = (self:get_whois() == 'moderators') and "173" or "172";
867 msg.tags[1]:tag('status', {code = code}):up();
869 self:broadcast_message(msg, false)
873 function room_mt:destroy(newjid, reason, password)
874 local pr = st.presence({type = "unavailable"})
875 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
876 :tag("item", { affiliation='none', role='none' }):up()
877 :tag("destroy", {jid=newjid})
878 if reason then pr:tag("reason"):text(reason):up(); end
879 if password then pr:tag("password"):text(password):up(); end
880 for nick, occupant in pairs(self._occupants) do
882 for jid in pairs(occupant.sessions) do
884 self:_route_stanza(pr);
885 self._jid_nick[jid] = nil;
887 self._occupants[nick] = nil;
888 module:fire_event("muc-occupant-left", { room = self; nick = nick; });
890 self:set_persistent(false);
891 module:fire_event("muc-room-destroyed", { room = self });
894 function room_mt:handle_disco_info_get_query(origin, stanza)
895 origin.send(self:get_disco_info(stanza));
899 function room_mt:handle_disco_items_get_query(origin, stanza)
900 origin.send(self:get_disco_items(stanza));
904 function room_mt:handle_admin_query_set_command(origin, stanza)
905 local item = stanza.tags[1].tags[1];
906 if item.attr.jid then -- Validate provided JID
907 item.attr.jid = jid_prep(item.attr.jid);
908 if not item.attr.jid then
909 origin.send(st.error_reply(stanza, "modify", "jid-malformed"));
913 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation
914 local occupant = self._occupants[self.jid.."/"..item.attr.nick];
915 if occupant then item.attr.jid = occupant.jid; end
916 elseif not item.attr.nick and item.attr.jid then
917 local nick = self:get_occupant_jid(item.attr.jid);
918 if nick then item.attr.nick = select(3, jid_split(nick)); end
920 local actor = stanza.attr.from;
921 local callback = function() origin.send(st.reply(stanza)); end
922 local reason = item:get_child_text("reason");
923 if item.attr.affiliation and item.attr.jid and not item.attr.role then
924 local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback, reason);
925 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
927 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then
928 local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback, reason);
929 if not success then origin.send(st.error_reply(stanza, errtype, err)); end
932 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
937 function room_mt:handle_admin_query_get_command(origin, stanza)
938 local actor = stanza.attr.from;
939 local affiliation = self:get_affiliation(actor);
940 local item = stanza.tags[1].tags[1];
941 local _aff = item.attr.affiliation;
942 local _rol = item.attr.role;
943 if _aff and not _rol then
944 if affiliation == "owner" or (affiliation == "admin" and _aff ~= "owner" and _aff ~= "admin") then
945 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
946 for jid, affiliation in pairs(self._affiliations) do
947 if affiliation == _aff then
948 reply:tag("item", {affiliation = _aff, jid = jid}):up();
954 origin.send(st.error_reply(stanza, "auth", "forbidden"));
957 elseif _rol and not _aff then
958 local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation);
959 if role == "moderator" then
960 if _rol == "none" then _rol = nil; end
961 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin");
962 for occupant_jid, occupant in pairs(self._occupants) do
963 if occupant.role == _rol then
965 nick = select(3, jid_split(occupant_jid)),
966 role = _rol or "none",
967 affiliation = occupant.affiliation or "none",
975 origin.send(st.error_reply(stanza, "auth", "forbidden"));
979 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
984 function room_mt:handle_owner_query_get_to_room(origin, stanza)
985 if self:get_affiliation(stanza.attr.from) ~= "owner" then
986 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
990 self:send_form(origin, stanza);
993 function room_mt:handle_owner_query_set_to_room(origin, stanza)
994 if self:get_affiliation(stanza.attr.from) ~= "owner" then
995 origin.send(st.error_reply(stanza, "auth", "forbidden", "Only owners can configure rooms"));
999 local child = stanza.tags[1].tags[1];
1001 origin.send(st.error_reply(stanza, "modify", "bad-request"));
1003 elseif child.name == "destroy" then
1004 local newjid = child.attr.jid;
1005 local reason = child:get_child_text("reason");
1006 local password = child:get_child_text("password");
1007 self:destroy(newjid, reason, password);
1008 origin.send(st.reply(stanza));
1011 self:process_form(origin, stanza);
1016 function room_mt:handle_groupchat_to_room(origin, stanza)
1017 local from = stanza.attr.from;
1018 local current_nick = self:get_occupant_jid(from);
1019 local occupant = self._occupants[current_nick];
1020 if not occupant then -- not in room
1021 origin.send(st.error_reply(stanza, "cancel", "not-acceptable"));
1023 elseif occupant.role == "visitor" then
1024 origin.send(st.error_reply(stanza, "auth", "forbidden"));
1027 local from = stanza.attr.from;
1028 stanza.attr.from = current_nick;
1029 local subject = stanza:get_child_text("subject");
1031 if occupant.role == "moderator" or
1032 ( self:get_changesubject() and occupant.role == "participant" ) then -- and participant
1033 self:set_subject(current_nick, subject);
1035 stanza.attr.from = from;
1036 origin.send(st.error_reply(stanza, "auth", "forbidden"));
1039 self:broadcast_message(stanza, self:get_historylength() > 0 and stanza:get_child("body"));
1041 stanza.attr.from = from;
1046 -- hack - some buggy clients send presence updates to the room rather than their nick
1047 function room_mt:handle_presence_to_room(origin, stanza)
1048 local current_nick = self:get_occupant_jid(stanza.attr.from);
1050 if current_nick then
1051 local to = stanza.attr.to;
1052 stanza.attr.to = current_nick;
1053 handled = self:handle_presence_to_occupant(origin, stanza);
1054 stanza.attr.to = to;
1059 function room_mt:handle_mediated_invite(origin, stanza)
1060 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite")
1061 local _from, _to = stanza.attr.from, stanza.attr.to;
1062 local current_nick = self:get_occupant_jid(_from)
1063 -- Need visitor role or higher to invite
1064 if not self:get_role(current_nick) or not self:get_default_role(self:get_affiliation(_from)) then
1065 origin.send(st.error_reply(stanza, "auth", "forbidden"));
1068 local _invitee = jid_prep(payload.attr.to);
1070 if self:get_whois() == "moderators" then
1071 _from = current_nick;
1073 local _reason = payload:get_child_text("reason")
1074 local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id})
1075 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
1076 :tag('invite', {from=_from})
1077 :tag('reason'):text(_reason or ""):up()
1079 local password = self:get_password()
1081 invite:tag("password"):text(password):up();
1084 :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this
1085 :text(_reason or "")
1087 :tag('body') -- Add a plain message for clients which don't support invites
1088 :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or ""))
1090 module:fire_event("muc-invite", { room = self, stanza = invite, origin = origin, incoming = stanza });
1093 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
1098 module:hook("muc-invite", function(event)
1099 event.room:_route_stanza(event.stanza);
1103 -- When an invite is sent; add an affiliation for the invitee
1104 module:hook("muc-invite", function(event)
1105 local room, stanza = event.room, event.stanza
1106 local invitee = stanza.attr.to
1107 if room:get_members_only() and not room:get_affiliation(invitee) then
1108 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
1109 local current_nick = room:get_occupant_jid(from)
1110 log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
1111 room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick)
1115 function room_mt:handle_mediated_decline(origin, stanza)
1116 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline")
1117 local declinee = jid_prep(payload.attr.to);
1119 local from, to = stanza.attr.from, stanza.attr.to;
1120 -- TODO: Validate declinee
1121 local reason = payload:get_child_text("reason")
1122 local decline = st.message({from = to, to = declinee, id = stanza.attr.id})
1123 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'})
1124 :tag('decline', {from=from})
1125 :tag('reason'):text(reason or ""):up()
1128 :tag('body') -- Add a plain message for clients which don't support declines
1129 :text(from..' declined your invite to the room '..to..(reason and (' ('..reason..')') or ""))
1131 module:fire_event("muc-decline", { room = self, stanza = decline, origin = origin, incoming = stanza });
1134 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
1139 module:hook("muc-decline", function(event)
1140 local room, stanza = event.room, event.stanza
1141 local occupant = room:get_occupant_by_real_jid(stanza.attr.to);
1143 room:route_to_occupant(occupant, stanza)
1145 room:route_stanza(stanza);
1150 function room_mt:handle_message_to_room(origin, stanza)
1151 local type = stanza.attr.type;
1152 if type == "groupchat" then
1153 return self:handle_groupchat_to_room(origin, stanza)
1154 elseif type == "error" and is_kickable_error(stanza) then
1155 return self:handle_kickable(origin, stanza)
1156 elseif type == nil then
1157 local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
1159 local payload = x.tags[1];
1160 if payload == nil then
1162 elseif payload.name == "invite" and payload.attr.to then
1163 return self:handle_mediated_invite(origin, stanza)
1164 elseif payload.name == "decline" and payload.attr.to then
1165 return self:handle_mediated_decline(origin, stanza)
1167 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
1175 function room_mt:route_stanza(stanza)
1179 function room_mt:get_affiliation(jid)
1180 local node, host, resource = jid_split(jid);
1181 local bare = node and node.."@"..host or host;
1182 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID.
1183 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned
1186 function room_mt:set_affiliation(actor, jid, affiliation, callback, reason)
1187 jid = jid_bare(jid);
1188 if affiliation == "none" then affiliation = nil; end
1189 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then
1190 return nil, "modify", "not-acceptable";
1192 if actor ~= true then
1193 local actor_affiliation = self:get_affiliation(actor);
1194 local target_affiliation = self:get_affiliation(jid);
1195 if target_affiliation == affiliation then -- no change, shortcut
1196 if callback then callback(); end
1199 if actor_affiliation ~= "owner" then
1200 if affiliation == "owner" or affiliation == "admin" or actor_affiliation ~= "admin" or target_affiliation == "owner" or target_affiliation == "admin" then
1201 return nil, "cancel", "not-allowed";
1203 elseif target_affiliation == "owner" and jid_bare(actor) == jid then -- self change
1204 local is_last = true;
1205 for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end
1207 return nil, "cancel", "conflict";
1211 self._affiliations[jid] = affiliation;
1212 local role = self:get_default_role(affiliation);
1213 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
1214 :tag("item", {affiliation=affiliation or "none", role=role or "none"})
1215 :tag("reason"):text(reason or ""):up()
1217 local presence_type = nil;
1218 if not role then -- getting kicked
1219 presence_type = "unavailable";
1220 if affiliation == "outcast" then
1221 x:tag("status", {code="301"}):up(); -- banned
1223 x:tag("status", {code="321"}):up(); -- affiliation change
1226 local modified_nicks = {};
1227 for nick, occupant in pairs(self._occupants) do
1228 if jid_bare(occupant.jid) == jid then
1229 if not role then -- getting kicked
1230 self._occupants[nick] = nil;
1232 occupant.affiliation, occupant.role = affiliation, role;
1234 for jid,pres in pairs(occupant.sessions) do -- remove for all sessions of the nick
1235 if not role then self._jid_nick[jid] = nil; end
1236 local p = st.clone(pres);
1238 p.attr.type = presence_type;
1241 self:_route_stanza(p);
1242 if occupant.jid == jid then
1243 modified_nicks[nick] = p;
1248 if self.save then self:save(); end
1249 if callback then callback(); end
1250 for nick,p in pairs(modified_nicks) do
1252 self:broadcast_except_nick(p, nick);
1257 function room_mt:get_role(nick)
1258 local session = self._occupants[nick];
1259 return session and session.role or nil;
1261 function room_mt:can_set_role(actor_jid, occupant_jid, role)
1262 local occupant = self._occupants[occupant_jid];
1263 if not occupant or not actor_jid then return nil, "modify", "not-acceptable"; end
1265 if actor_jid == true then return true; end
1267 local actor = self._occupants[self:get_occupant_jid(actor_jid)];
1268 if actor and actor.role == "moderator" then
1269 if occupant.affiliation ~= "owner" and occupant.affiliation ~= "admin" then
1270 if actor.affiliation == "owner" or actor.affiliation == "admin" then
1272 elseif occupant.role ~= "moderator" and role ~= "moderator" then
1277 return nil, "cancel", "not-allowed";
1279 function room_mt:set_role(actor, occupant_jid, role, callback, reason)
1280 if role == "none" then role = nil; end
1281 if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end
1282 local allowed, err_type, err_condition = self:can_set_role(actor, occupant_jid, role);
1283 if not allowed then return allowed, err_type, err_condition; end
1284 local occupant = self._occupants[occupant_jid];
1285 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
1286 :tag("item", {affiliation=occupant.affiliation or "none", nick=select(3, jid_split(occupant_jid)), role=role or "none"})
1287 :tag("reason"):text(reason or ""):up()
1289 local presence_type = nil;
1290 if not role then -- kick
1291 presence_type = "unavailable";
1292 self._occupants[occupant_jid] = nil;
1293 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick
1294 self._jid_nick[jid] = nil;
1296 x:tag("status", {code = "307"}):up();
1298 occupant.role = role;
1301 for jid,pres in pairs(occupant.sessions) do -- send to all sessions of the nick
1302 local p = st.clone(pres);
1303 p.attr.from = occupant_jid;
1304 p.attr.type = presence_type;
1307 self:_route_stanza(p);
1308 if occupant.jid == jid then
1312 if callback then callback(); end
1314 self:broadcast_except_nick(bp, occupant_jid);
1319 function room_mt:_route_stanza(stanza)
1321 if stanza.name == "presence" then
1322 local to_occupant = self._occupants[self:get_occupant_jid(stanza.attr.to)];
1323 local from_occupant = self._occupants[stanza.attr.from];
1324 if to_occupant and from_occupant then
1325 if self:get_whois() == 'anyone' then
1326 muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
1328 if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then
1329 muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
1334 for item in muc_child:childtags("item") do
1335 if from_occupant == to_occupant then
1336 item.attr.jid = stanza.attr.to;
1338 item.attr.jid = from_occupant.jid;
1343 self:route_stanza(stanza);
1345 for item in muc_child:childtags("item") do
1346 item.attr.jid = nil;
1351 local _M = {}; -- module "muc"
1353 function _M.new_room(jid, config)
1354 return setmetatable({
1360 whois = 'moderators';
1361 history_length = math.min((config and config.history_length)
1362 or default_history_length, max_history_length);
1368 function _M.set_max_history_length(_max_history_length)
1369 max_history_length = _max_history_length or math.huge;
1372 _M.room_mt = room_mt;