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 = event.room;
55 if get_members_only(room) then
56 local stanza = event.stanza;
57 local affiliation = room:get_affiliation(stanza.attr.from);
58 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
59 local reply = st.error_reply(stanza, "auth", "registration-required"):up();
60 reply.tags[1].attr.code = "407";
61 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
67 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
68 -- if a member without privileges to edit the member list attempts to invite another user
69 -- the service SHOULD return a <forbidden/> error to the occupant
70 module:hook("muc-pre-invite", function(event)
71 local room = event.room;
72 if get_members_only(room) then
73 local stanza = event.stanza;
74 local affiliation = room:get_affiliation(stanza.attr.from);
75 if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
76 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
82 -- When an invite is sent; add an affiliation for the invitee
83 module:hook("muc-invite", function(event)
84 local room = event.room;
85 if get_members_only(room) then
86 local stanza = event.stanza;
87 local invitee = stanza.attr.to;
88 local affiliation = room:get_affiliation(invitee);
89 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
90 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
91 :get_child("invite").attr.from;
92 module:log("debug", "%s invited %s into members only room %s, granting membership",
93 from, invitee, room.jid);
94 -- This might fail; ignore for now
95 room:set_affiliation(from, invitee, "member", "Invited by " .. from);
101 get = get_members_only;
102 set = set_members_only;