Software /
code /
prosody
Annotate
plugins/muc/members_only.lib.lua @ 6317:ec57067c1e0d
mod_muc/muc.lib: Pass all info to muc-broadcast-presence handlers that would be required to use room:build_item_list() - useful for plugins
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 25 Jul 2014 18:32:15 +0100 |
parent | 6230:97d53caef325 |
child | 6329:6b3eb1611587 |
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 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
10 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
|
11 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
|
12 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
13 local function get_members_only(room) |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
14 return room._data.members_only; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
15 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
16 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
17 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
|
18 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
|
19 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
|
20 room._data.members_only = members_only; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
21 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
|
22 return true; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
23 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
24 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
25 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
|
26 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
|
27 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
28 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
29 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
|
30 table.insert(event.form, { |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
31 name = "muc#roomconfig_membersonly"; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
32 type = "boolean"; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
33 label = "Make Room Members-Only?"; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
34 value = get_members_only(event.room); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
35 }); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
36 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
37 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
38 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
|
39 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
|
40 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
|
41 event.status_codes["104"] = true; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
42 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
43 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
44 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
45 -- No affiliation => role of "none" |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
46 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
|
47 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
|
48 return false; |
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 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
51 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
52 -- 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 return true; |
97d53caef325
plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents:
6221
diff
changeset
|
63 end |
6221
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
64 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
65 end, -5); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
66 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
67 -- 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
|
68 -- 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
|
69 -- 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 return true; |
97d53caef325
plugins/muc/members_only.lib: Compare affiliations via rank; wrap some long lines
daurnimator <quae@daurnimator.com>
parents:
6221
diff
changeset
|
78 end |
6221
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
79 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
80 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
81 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
82 -- 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 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
|
91 :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
|
92 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
|
93 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
|
94 -- 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
|
95 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
|
96 end |
6221
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
97 end |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
98 end); |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
99 |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
100 return { |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
101 get = get_members_only; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
102 set = set_members_only; |
f321536afeec
plugins/muc/muc.lib: Move members_only into seperate file
daurnimator <quae@daurnimator.com>
parents:
diff
changeset
|
103 }; |