Annotate

plugins/muc/members_only.lib.lua @ 6679:96e034508978

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 13 May 2015 17:00:27 +0100
parent 6477:29f979f554d3
child 6991:84e01dbb739e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
1 -- Prosody IM
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
4 -- Copyright (C) 2014 Daurnimator
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
5 --
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
8 --
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
9
6329
6b3eb1611587 mod_muc: Import util.stanza into the config handler modules that need it. Fixes #432.
Matthew Wild <mwild1@gmail.com>
parents: 6230
diff changeset
10 local st = require "util.stanza";
6b3eb1611587 mod_muc: Import util.stanza into the config handler modules that need it. Fixes #432.
Matthew Wild <mwild1@gmail.com>
parents: 6230
diff changeset
11
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
12 local muc_util = module:require "muc/util";
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
13 local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
14
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
15 local function get_members_only(room)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
16 return room._data.members_only;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
17 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
18
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
19 local function set_members_only(room, members_only)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
20 members_only = members_only and true or nil;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
21 if room._data.members_only == members_only then return false; end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
22 room._data.members_only = members_only;
6477
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
23 if members_only then
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
24 --[[
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
25 If as a result of a change in the room configuration the room type is
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
26 changed to members-only but there are non-members in the room,
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
27 the service MUST remove any non-members from the room and include a
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
28 status code of 322 in the presence unavailable stanzas sent to those users
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
29 as well as any remaining occupants.
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
30 ]]
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
31 local occupants_changed = {};
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
32 for nick, occupant in room:each_occupant() do
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
33 local affiliation = room:get_affiliation(occupant.bare_jid);
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
34 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
35 occupant.role = nil;
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
36 room:save_occupant(occupant);
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
37 occupants_changed[occupant] = true;
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
38 end
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
39 end
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
40 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"})
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
41 :tag("status", {code="322"}):up();
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
42 for occupant in pairs(occupants_changed) do
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
43 room:publicise_occupant_status(occupant, x);
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
44 module:fire_event("muc-occupant-left", {room = room; nick = occupant.nick; occupant = occupant;});
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
45 end
29f979f554d3 plugins/muc/members_only: Kick non-members when members-only is turned on
daurnimator <quae@daurnimator.com>
parents: 6329
diff changeset
46 end
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
47 if room.save then room:save(true); end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
48 return true;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
50
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
51 module:hook("muc-disco#info", function(event)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
52 event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
53 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
54
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
55 module:hook("muc-config-form", function(event)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
56 table.insert(event.form, {
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
57 name = "muc#roomconfig_membersonly";
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
58 type = "boolean";
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
59 label = "Make Room Members-Only?";
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
60 value = get_members_only(event.room);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
61 });
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
62 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
63
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
64 module:hook("muc-config-submitted", function(event)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
65 local new = event.fields["muc#roomconfig_membersonly"];
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
66 if new ~= nil and set_members_only(event.room, new) then
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
67 event.status_codes["104"] = true;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
68 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
69 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
70
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
71 -- No affiliation => role of "none"
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
72 module:hook("muc-get-default-role", function(event)
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
73 if not event.affiliation and get_members_only(event.room) then
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
74 return false;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
75 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
76 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
77
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
78 -- registration required for entering members-only room
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
79 module:hook("muc-occupant-pre-join", function(event)
6230
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
80 local room = event.room;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
81 if get_members_only(room) then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
82 local stanza = event.stanza;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
83 local affiliation = room:get_affiliation(stanza.attr.from);
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
84 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
85 local reply = st.error_reply(stanza, "auth", "registration-required"):up();
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
86 reply.tags[1].attr.code = "407";
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
87 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
88 return true;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
89 end
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
90 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
91 end, -5);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
92
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
93 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
94 -- if a member without privileges to edit the member list attempts to invite another user
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
95 -- the service SHOULD return a <forbidden/> error to the occupant
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
96 module:hook("muc-pre-invite", function(event)
6230
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
97 local room = event.room;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
98 if get_members_only(room) then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
99 local stanza = event.stanza;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
100 local affiliation = room:get_affiliation(stanza.attr.from);
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
101 if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
102 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
103 return true;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
104 end
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
105 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
106 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
107
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
108 -- When an invite is sent; add an affiliation for the invitee
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
109 module:hook("muc-invite", function(event)
6230
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
110 local room = event.room;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
111 if get_members_only(room) then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
112 local stanza = event.stanza;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
113 local invitee = stanza.attr.to;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
114 local affiliation = room:get_affiliation(invitee);
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
115 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
116 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
117 :get_child("invite").attr.from;
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
118 module:log("debug", "%s invited %s into members only room %s, granting membership",
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
119 from, invitee, room.jid);
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
120 -- This might fail; ignore for now
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
121 room:set_affiliation(from, invitee, "member", "Invited by " .. from);
97d53caef325 plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents: 6221
diff changeset
122 end
6221
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
123 end
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
124 end);
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
125
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
126 return {
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
127 get = get_members_only;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
128 set = set_members_only;
f321536afeec plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff changeset
129 };