mod_muc: Import util.stanza into the config handler modules that need it. Fixes ...
[prosody.git] / plugins / muc / members_only.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 st = require "util.stanza";
11
12 local muc_util = module:require "muc/util";
13 local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations;
14
15 local function get_members_only(room)
16         return room._data.members_only;
17 end
18
19 local function set_members_only(room, members_only)
20         members_only = members_only and true or nil;
21         if room._data.members_only == members_only then return false; end
22         room._data.members_only = members_only;
23         if room.save then room:save(true); end
24         return true;
25 end
26
27 module:hook("muc-disco#info", function(event)
28         event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
29 end);
30
31 module:hook("muc-config-form", function(event)
32         table.insert(event.form, {
33                 name = "muc#roomconfig_membersonly";
34                 type = "boolean";
35                 label = "Make Room Members-Only?";
36                 value = get_members_only(event.room);
37         });
38 end);
39
40 module:hook("muc-config-submitted", function(event)
41         local new = event.fields["muc#roomconfig_membersonly"];
42         if new ~= nil and set_members_only(event.room, new) then
43                 event.status_codes["104"] = true;
44         end
45 end);
46
47 -- No affiliation => role of "none"
48 module:hook("muc-get-default-role", function(event)
49         if not event.affiliation and get_members_only(event.room) then
50                 return false;
51         end
52 end);
53
54 -- registration required for entering members-only room
55 module:hook("muc-occupant-pre-join", function(event)
56         local room = event.room;
57         if get_members_only(room) then
58                 local stanza = event.stanza;
59                 local affiliation = room:get_affiliation(stanza.attr.from);
60                 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
61                         local reply = st.error_reply(stanza, "auth", "registration-required"):up();
62                         reply.tags[1].attr.code = "407";
63                         event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
64                         return true;
65                 end
66         end
67 end, -5);
68
69 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
70 -- if a member without privileges to edit the member list attempts to invite another user
71 -- the service SHOULD return a <forbidden/> error to the occupant
72 module:hook("muc-pre-invite", function(event)
73         local room = event.room;
74         if get_members_only(room) then
75                 local stanza = event.stanza;
76                 local affiliation = room:get_affiliation(stanza.attr.from);
77                 if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
78                         event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
79                         return true;
80                 end
81         end
82 end);
83
84 -- When an invite is sent; add an affiliation for the invitee
85 module:hook("muc-invite", function(event)
86         local room = event.room;
87         if get_members_only(room) then
88                 local stanza = event.stanza;
89                 local invitee = stanza.attr.to;
90                 local affiliation = room:get_affiliation(invitee);
91                 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
92                         local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
93                                 :get_child("invite").attr.from;
94                         module:log("debug", "%s invited %s into members only room %s, granting membership",
95                                 from, invitee, room.jid);
96                         -- This might fail; ignore for now
97                         room:set_affiliation(from, invitee, "member", "Invited by " .. from);
98                 end
99         end
100 end);
101
102 return {
103         get = get_members_only;
104         set = set_members_only;
105 };