comparison plugins/muc/members_only.lib.lua @ 6609:d2faaaca695d

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Mar 2015 22:24:57 +0000
parents 29f979f554d3
children 84e01dbb739e
comparison
equal deleted inserted replaced
6608:b6e558febb7a 6609:d2faaaca695d
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 members_only then
24 --[[
25 If as a result of a change in the room configuration the room type is
26 changed to members-only but there are non-members in the room,
27 the service MUST remove any non-members from the room and include a
28 status code of 322 in the presence unavailable stanzas sent to those users
29 as well as any remaining occupants.
30 ]]
31 local occupants_changed = {};
32 for nick, occupant in room:each_occupant() do
33 local affiliation = room:get_affiliation(occupant.bare_jid);
34 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
35 occupant.role = nil;
36 room:save_occupant(occupant);
37 occupants_changed[occupant] = true;
38 end
39 end
40 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
41 :tag("status", {code="322"}):up();
42 for occupant in pairs(occupants_changed) do
43 room:publicise_occupant_status(occupant, x);
44 module:fire_event("muc-occupant-left", {room = room; nick = occupant.nick; occupant = occupant;});
45 end
46 end
47 if room.save then room:save(true); end
48 return true;
49 end
50
51 module:hook("muc-disco#info", function(event)
52 event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
53 end);
54
55 module:hook("muc-config-form", function(event)
56 table.insert(event.form, {
57 name = "muc#roomconfig_membersonly";
58 type = "boolean";
59 label = "Make Room Members-Only?";
60 value = get_members_only(event.room);
61 });
62 end);
63
64 module:hook("muc-config-submitted", function(event)
65 local new = event.fields["muc#roomconfig_membersonly"];
66 if new ~= nil and set_members_only(event.room, new) then
67 event.status_codes["104"] = true;
68 end
69 end);
70
71 -- No affiliation => role of "none"
72 module:hook("muc-get-default-role", function(event)
73 if not event.affiliation and get_members_only(event.room) then
74 return false;
75 end
76 end);
77
78 -- registration required for entering members-only room
79 module:hook("muc-occupant-pre-join", function(event)
80 local room = event.room;
81 if get_members_only(room) then
82 local stanza = event.stanza;
83 local affiliation = room:get_affiliation(stanza.attr.from);
84 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
85 local reply = st.error_reply(stanza, "auth", "registration-required"):up();
86 reply.tags[1].attr.code = "407";
87 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
88 return true;
89 end
90 end
91 end, -5);
92
93 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
94 -- if a member without privileges to edit the member list attempts to invite another user
95 -- the service SHOULD return a <forbidden/> error to the occupant
96 module:hook("muc-pre-invite", function(event)
97 local room = event.room;
98 if get_members_only(room) then
99 local stanza = event.stanza;
100 local affiliation = room:get_affiliation(stanza.attr.from);
101 if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
102 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
103 return true;
104 end
105 end
106 end);
107
108 -- When an invite is sent; add an affiliation for the invitee
109 module:hook("muc-invite", function(event)
110 local room = event.room;
111 if get_members_only(room) then
112 local stanza = event.stanza;
113 local invitee = stanza.attr.to;
114 local affiliation = room:get_affiliation(invitee);
115 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
116 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
117 :get_child("invite").attr.from;
118 module:log("debug", "%s invited %s into members only room %s, granting membership",
119 from, invitee, room.jid);
120 -- This might fail; ignore for now
121 room:set_affiliation(from, invitee, "member", "Invited by " .. from);
122 end
123 end
124 end);
125
126 return {
127 get = get_members_only;
128 set = set_members_only;
129 };