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 muc_util = module:require "muc/util";
11 local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations;
13 local function get_members_only(room)
14 return room._data.members_only;
17 local function set_members_only(room, members_only)
18 members_only = members_only and true or nil;
19 if room._data.members_only == members_only then return false; end
20 room._data.members_only = members_only;
21 if room.save then room:save(true); end
25 module:hook("muc-disco#info", function(event)
26 event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
29 module:hook("muc-config-form", function(event)
30 table.insert(event.form, {
31 name = "muc#roomconfig_membersonly";
33 label = "Make Room Members-Only?";
34 value = get_members_only(event.room);
38 module:hook("muc-config-submitted", function(event)
39 local new = event.fields["muc#roomconfig_membersonly"];
40 if new ~= nil and set_members_only(event.room, new) then
41 event.status_codes["104"] = true;
45 -- No affiliation => role of "none"
46 module:hook("muc-get-default-role", function(event)
47 if not event.affiliation and get_members_only(event.room) then
52 -- registration required for entering members-only room
53 module:hook("muc-occupant-pre-join", function(event)
54 local room, stanza = event.room, event.stanza;
55 local affiliation = room:get_affiliation(stanza.attr.from);
56 if affiliation == nil and get_members_only(event.room) then
57 local reply = st.error_reply(stanza, "auth", "registration-required"):up();
58 reply.tags[1].attr.code = "407";
59 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
64 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
65 -- if a member without privileges to edit the member list attempts to invite another user
66 -- the service SHOULD return a <forbidden/> error to the occupant
67 module:hook("muc-pre-invite", function(event)
68 local room, stanza = event.room, event.stanza;
69 if get_members_only(room) and room:get_affiliation(stanza.attr.from) or "none" < valid_affiliations.admin then
70 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
75 -- When an invite is sent; add an affiliation for the invitee
76 module:hook("muc-invite", function(event)
77 local room, stanza = event.room, event.stanza;
78 local invitee = stanza.attr.to;
79 if get_members_only(room) and not room:get_affiliation(invitee) then
80 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from;
81 module:log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
82 room:set_affiliation(from, invitee, "member", "Invited by " .. from); -- This might fail; ignore for now
87 get = get_members_only;
88 set = set_members_only;