Comparison

plugins/muc/members_only.lib.lua @ 8976:92f0876b9230

MUC: Add config option to allow members to invite other members to the room (previously only owners/admins could do this)
author Matthew Wild <mwild1@gmail.com>
date Fri, 06 Jul 2018 15:33:46 +0100
parent 7401:e16b3fd0bd80
child 8985:4101bcf9639a
comparison
equal deleted inserted replaced
8975:47dd2e850781 8976:92f0876b9230
45 end 45 end
46 end 46 end
47 return true; 47 return true;
48 end 48 end
49 49
50 local function get_allow_member_invites(room)
51 return room._data.allow_member_invites;
52 end
53
54 -- Allows members to invite new members into a members-only room,
55 -- effectively creating an invite-only room
56 local function set_allow_member_invites(room, allow_member_invites)
57 allow_member_invites = allow_member_invites and true or nil;
58 if room._data.allow_member_invites == allow_member_invites then return false; end
59 room._data.allow_member_invites = allow_member_invites;
60 return true;
61 end
62
50 module:hook("muc-disco#info", function(event) 63 module:hook("muc-disco#info", function(event)
51 event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up(); 64 event.reply:tag("feature", {var = get_members_only(event.room) and "muc_membersonly" or "muc_open"}):up();
52 end); 65 end);
53 66
54 module:hook("muc-config-form", function(event) 67 module:hook("muc-config-form", function(event)
56 name = "muc#roomconfig_membersonly"; 69 name = "muc#roomconfig_membersonly";
57 type = "boolean"; 70 type = "boolean";
58 label = "Make Room Members-Only?"; 71 label = "Make Room Members-Only?";
59 value = get_members_only(event.room); 72 value = get_members_only(event.room);
60 }); 73 });
74 table.insert(event.form, {
75 name = "{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites";
76 type = "boolean";
77 label = "Allow members to invite new members?";
78 value = get_allow_member_invites(event.room);
79 });
61 end, 100-6); 80 end, 100-6);
62 81
63 module:hook("muc-config-submitted/muc#roomconfig_membersonly", function(event) 82 module:hook("muc-config-submitted/muc#roomconfig_membersonly", function(event)
64 if set_members_only(event.room, event.value) then 83 if set_members_only(event.room, event.value) then
84 event.status_codes["104"] = true;
85 end
86 end);
87
88 module:hook("muc-config-submitted/{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites", function(event)
89 if set_allow_member_invites(event.room, event.value) then
65 event.status_codes["104"] = true; 90 event.status_codes["104"] = true;
66 end 91 end
67 end); 92 end);
68 93
69 -- No affiliation => role of "none" 94 -- No affiliation => role of "none"
94 module:hook("muc-pre-invite", function(event) 119 module:hook("muc-pre-invite", function(event)
95 local room = event.room; 120 local room = event.room;
96 if get_members_only(room) then 121 if get_members_only(room) then
97 local stanza = event.stanza; 122 local stanza = event.stanza;
98 local affiliation = room:get_affiliation(stanza.attr.from); 123 local affiliation = room:get_affiliation(stanza.attr.from);
99 if valid_affiliations[affiliation or "none"] < valid_affiliations.admin then 124 if not room._data.allow_member_invites and valid_affiliations[affiliation or "none"] < valid_affiliations.admin then
100 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); 125 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
101 return true; 126 return true;
102 end 127 end
103 end 128 end
104 end); 129 end);
108 local room = event.room; 133 local room = event.room;
109 if get_members_only(room) then 134 if get_members_only(room) then
110 local stanza = event.stanza; 135 local stanza = event.stanza;
111 local invitee = stanza.attr.to; 136 local invitee = stanza.attr.to;
112 local affiliation = room:get_affiliation(invitee); 137 local affiliation = room:get_affiliation(invitee);
113 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none then 138 if valid_affiliations[affiliation or "none"] <= valid_affiliations.none and room._data.allow_member_invites then
114 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user") 139 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
115 :get_child("invite").attr.from; 140 :get_child("invite").attr.from;
116 module:log("debug", "%s invited %s into members only room %s, granting membership", 141 module:log("debug", "%s invited %s into members only room %s, granting membership",
117 from, invitee, room.jid); 142 from, invitee, room.jid);
118 -- This might fail; ignore for now 143 -- This might fail; ignore for now
119 room:set_affiliation(from, invitee, "member", "Invited by " .. from); 144 room:set_affiliation(true, invitee, "member", "Invited by " .. from);
120 room:save(); 145 room:save();
121 end 146 end
122 end 147 end
123 end); 148 end);
124 149
125 return { 150 return {
126 get = get_members_only; 151 get = get_members_only;
127 set = set_members_only; 152 set = set_members_only;
153 get_allow_member_invites = get_allow_member_invites;
154 set_allow_member_invites = set_allow_member_invites;
128 }; 155 };