Annotate

plugins/muc/moderated.lib.lua @ 8860:0bb46a1bb398

MUC: Handle and return error in role change when granting voice
author Kim Alvefur <zash@zash.se>
date Fri, 01 Jun 2018 10:26:18 +0200
parent 8859:11176f47a03a
child 8865:2a8bbfcb6868
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
1 -- Prosody IM
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
4 -- Copyright (C) 2014 Daurnimator
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
5 --
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
7 -- COPYING file in the source package for more information.
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
8 --
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
9
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
10 local st = require "util.stanza";
8854
5cd7813d4e94 MUC: Split out the nickname from the full room JID in voice request from
Kim Alvefur <zash@zash.se>
parents: 8853
diff changeset
11 local jid_resource = require "util.jid".resource;
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
12
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
13 local function get_moderated(room)
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
14 return room._data.moderated;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
15 end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
16
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
17 local function set_moderated(room, moderated)
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
18 moderated = moderated and true or nil;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
19 if get_moderated(room) == moderated then return false; end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
20 room._data.moderated = moderated;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
21 return true;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
22 end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
23
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
24 module:hook("muc-disco#info", function(event)
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
25 event.reply:tag("feature", {var = get_moderated(event.room) and "muc_moderated" or "muc_unmoderated"}):up();
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
26 end);
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
27
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
28 module:hook("muc-config-form", function(event)
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
29 table.insert(event.form, {
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
30 name = "muc#roomconfig_moderatedroom";
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
31 type = "boolean";
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
32 label = "Make Room Moderated?";
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
33 value = get_moderated(event.room);
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
34 });
7401
e16b3fd0bd80 MUC: Assign priorities to config form hooks so they have a consistent order on each start
Kim Alvefur <zash@zash.se>
parents: 7353
diff changeset
35 end, 100-4);
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
36
6991
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6226
diff changeset
37 module:hook("muc-config-submitted/muc#roomconfig_moderatedroom", function(event)
84e01dbb739e MUC: Update all config form handlers to take advantage of the new per-option events
Matthew Wild <mwild1@gmail.com>
parents: 6226
diff changeset
38 if set_moderated(event.room, event.value) then
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
39 event.status_codes["104"] = true;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
40 end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
41 end);
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
42
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
43 module:hook("muc-get-default-role", function(event)
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
44 if event.affiliation == nil then
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
45 if get_moderated(event.room) then
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
46 return "visitor"
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
47 end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
48 end
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49 end, 1);
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
50
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
51 module:hook("muc-voice-request", function(event)
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
52 if event.occupant.role == "visitor" then
8853
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
53 local form = event.room:get_voice_form_layout()
8854
5cd7813d4e94 MUC: Split out the nickname from the full room JID in voice request from
Kim Alvefur <zash@zash.se>
parents: 8853
diff changeset
54 local nick = jid_resource(event.occupant.nick);
8853
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
55 local formdata = {
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
56 ["muc#jid"] = event.stanza.attr.from;
8854
5cd7813d4e94 MUC: Split out the nickname from the full room JID in voice request from
Kim Alvefur <zash@zash.se>
parents: 8853
diff changeset
57 ["muc#roomnick"] = nick;
8853
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
58 };
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
59
8853
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
60 local message = st.message({ type = "normal"; from = event.room.jid }):add_child(form:form(formdata)):up();
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
61
8852
5e98d62f3f9b MUC: Ignore unused argumens [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8851
diff changeset
62 event.room:broadcast(message, function (_, occupant)
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
63 return occupant.role == "moderator";
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
64 end);
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
65 end
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
66 end);
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
67
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
68 module:hook("muc-voice-response", function(event)
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
69 local actor = event.stanza.attr.from;
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
70 local affected_occupant = event.room:get_occupant_by_real_jid(event.fields["muc#jid"]);
8857
f4cc818db995 MUC: Get acting occupant into a local variable for easier access
Kim Alvefur <zash@zash.se>
parents: 8854
diff changeset
71 local occupant = event.occupant;
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
72
8857
f4cc818db995 MUC: Get acting occupant into a local variable for easier access
Kim Alvefur <zash@zash.se>
parents: 8854
diff changeset
73 if occupant.role ~= "moderator" then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
74 module:log("debug", "%s tried to grant voice but wasn't a moderator", jid_resource(occupant.nick));
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
75 return;
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
76 end
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
77
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
78 if not event.fields["muc#request_allow"] then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
79 module:log("debug", "%s did not grant voice", jid_resource(occupant.nick));
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
80 return;
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
81 end
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
82
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
83 if not affected_occupant then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
84 module:log("debug", "%s tried to grant voice to unknown occupant %s", jid_resource(occupant.nick), event.fields["muc#jid"]);
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
85 return;
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
86 end
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
87
8858
2096742859c1 MUC: Invert final conditional to be consistent with the other if statements
Kim Alvefur <zash@zash.se>
parents: 8857
diff changeset
88 if affected_occupant.role ~= "visitor" then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
89 module:log("debug", "%s tried to grant voice to %s but they already have it", jid_resource(occupant.nick), jid_resource(occupant.jid));
8858
2096742859c1 MUC: Invert final conditional to be consistent with the other if statements
Kim Alvefur <zash@zash.se>
parents: 8857
diff changeset
90 return;
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
91 end
8858
2096742859c1 MUC: Invert final conditional to be consistent with the other if statements
Kim Alvefur <zash@zash.se>
parents: 8857
diff changeset
92
8860
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
93 module:log("debug", "%s granted voice to %s", jid_resource(event.occupant.nick), jid_resource(occupant.jid));
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
94 local ok, errtype, err = event.room:set_role(actor, affected_occupant.nick, "participant", "Voice granted");
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
95
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
96 if not ok then
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
97 module:log("debug", "Error granting voice: %s", err or errtype);
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
98 event.origin.send(st.error_reply(event.stanza, errtype, err));
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
99 end
8851
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
100 end);
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
101
ab5f678f1376 MUC: Support MUC voice requests and approvals in moderated rooms (closes #655) (thanks to Lance Stout)
Kim Alvefur <zash@zash.se>
parents: 7401
diff changeset
102
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
103 return {
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
104 get = get_moderated;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
105 set = set_moderated;
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
106 };