Annotate

plugins/muc/request.lib.lua @ 8865:2a8bbfcb6868

MUC: Move voice request into its own lib
author Kim Alvefur <zash@zash.se>
date Sat, 02 Jun 2018 20:15:32 +0200
parent 8860:plugins/muc/moderated.lib.lua@0bb46a1bb398
child 8871:b67a861e883e
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
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
13 local voice_request_form = require "util.dataforms".new({
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
14 title = "Voice Request";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
15 {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
16 name = "FORM_TYPE";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
17 type = "hidden";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
18 value = "http://jabber.org/protocol/muc#request";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
19 },
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
20 {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
21 name = "muc#jid";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
22 type = "jid-single";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
23 label = "User ID";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
24 },
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
25 {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
26 name = "muc#roomnick";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
27 type = "text-single";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
28 label = "Room Nickname";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
29 },
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
30 {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
31 name = "muc#role";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
32 type = "list-single";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
33 label = "Requested Role";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
34 value = "participant";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
35 options = {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
36 "none",
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
37 "visitor",
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
38 "participant",
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
39 "moderator",
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
40 };
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
41 },
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
42 {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
43 name = "muc#request_allow";
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
44 type = "boolean";
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
45 label = "Grant voice to this person?";
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
46 value = false;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
47 }
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
48 });
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
49
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
50 local function handle_request(room, origin, stanza, form)
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
51 local occupant = room:get_occupant_by_real_jid(stanza.attr.from);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
52 local fields = voice_request_form:data(form);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
53 local event = {
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
54 room = room;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
55 origin = origin;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
56 stanza = stanza;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
57 fields = fields;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
58 occupant = occupant;
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
59 };
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
60 if occupant.role == "moderator" then
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
61 module:log("debug", "%s responded to a voice request in %s", jid_resource(occupant.nick), room.jid);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
62 module:fire_event("muc-voice-response", event);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
63 else
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
64 module:log("debug", "%s requested voice in %s", jid_resource(occupant.nick), room.jid);
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
65 module:fire_event("muc-voice-request", event);
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
66 end
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
67 end
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
68
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
69 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
70 if event.occupant.role == "visitor" then
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
71 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
72 local formdata = {
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
73 ["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
74 ["muc#roomnick"] = nick;
8853
f84f566dea58 MUC: Reuse the same dataform for voice requests
Kim Alvefur <zash@zash.se>
parents: 8852
diff changeset
75 };
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
76
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
77 local message = st.message({ type = "normal"; from = event.room.jid }):add_child(voice_request_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
78
8852
5e98d62f3f9b MUC: Ignore unused argumens [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8851
diff changeset
79 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
80 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
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 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
83 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
84
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 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
86 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
87 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
88 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
89
8857
f4cc818db995 MUC: Get acting occupant into a local variable for easier access
Kim Alvefur <zash@zash.se>
parents: 8854
diff changeset
90 if occupant.role ~= "moderator" then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
91 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
92 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
93 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
94
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
95 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
96 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
97 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
98 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
99
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 if not affected_occupant then
8859
11176f47a03a MUC: Add some debug logging for voice requests
Kim Alvefur <zash@zash.se>
parents: 8858
diff changeset
101 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
102 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
103 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
104
8858
2096742859c1 MUC: Invert final conditional to be consistent with the other if statements
Kim Alvefur <zash@zash.se>
parents: 8857
diff changeset
105 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
106 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
107 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
108 end
8858
2096742859c1 MUC: Invert final conditional to be consistent with the other if statements
Kim Alvefur <zash@zash.se>
parents: 8857
diff changeset
109
8860
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
110 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
111 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
112
0bb46a1bb398 MUC: Handle and return error in role change when granting voice
Kim Alvefur <zash@zash.se>
parents: 8859
diff changeset
113 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
114 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
115 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
116 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
117 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
118
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
119
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
120 return {
8865
2a8bbfcb6868 MUC: Move voice request into its own lib
Kim Alvefur <zash@zash.se>
parents: 8860
diff changeset
121 handle_request = handle_request;
6226
7582deb85812 plugins/muc: Move 'moderated' code to seperate file; changes default "muc-get-default-role" behaviour
daurnimator <quae@daurnimator.com>
parents:
diff changeset
122 };