Comparison

plugins/muc/moderated.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:0bb46a1bb398
child 9034:1c709e3d2e5e
comparison
equal deleted inserted replaced
8864:cf2f66b233d1 8865:2a8bbfcb6868
4 -- Copyright (C) 2014 Daurnimator 4 -- Copyright (C) 2014 Daurnimator
5 -- 5 --
6 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
8 -- 8 --
9
10 local st = require "util.stanza";
11 local jid_resource = require "util.jid".resource;
12 9
13 local function get_moderated(room) 10 local function get_moderated(room)
14 return room._data.moderated; 11 return room._data.moderated;
15 end 12 end
16 13
46 return "visitor" 43 return "visitor"
47 end 44 end
48 end 45 end
49 end, 1); 46 end, 1);
50 47
51 module:hook("muc-voice-request", function(event)
52 if event.occupant.role == "visitor" then
53 local form = event.room:get_voice_form_layout()
54 local nick = jid_resource(event.occupant.nick);
55 local formdata = {
56 ["muc#jid"] = event.stanza.attr.from;
57 ["muc#roomnick"] = nick;
58 };
59
60 local message = st.message({ type = "normal"; from = event.room.jid }):add_child(form:form(formdata)):up();
61
62 event.room:broadcast(message, function (_, occupant)
63 return occupant.role == "moderator";
64 end);
65 end
66 end);
67
68 module:hook("muc-voice-response", function(event)
69 local actor = event.stanza.attr.from;
70 local affected_occupant = event.room:get_occupant_by_real_jid(event.fields["muc#jid"]);
71 local occupant = event.occupant;
72
73 if occupant.role ~= "moderator" then
74 module:log("debug", "%s tried to grant voice but wasn't a moderator", jid_resource(occupant.nick));
75 return;
76 end
77
78 if not event.fields["muc#request_allow"] then
79 module:log("debug", "%s did not grant voice", jid_resource(occupant.nick));
80 return;
81 end
82
83 if not affected_occupant then
84 module:log("debug", "%s tried to grant voice to unknown occupant %s", jid_resource(occupant.nick), event.fields["muc#jid"]);
85 return;
86 end
87
88 if affected_occupant.role ~= "visitor" then
89 module:log("debug", "%s tried to grant voice to %s but they already have it", jid_resource(occupant.nick), jid_resource(occupant.jid));
90 return;
91 end
92
93 module:log("debug", "%s granted voice to %s", jid_resource(event.occupant.nick), jid_resource(occupant.jid));
94 local ok, errtype, err = event.room:set_role(actor, affected_occupant.nick, "participant", "Voice granted");
95
96 if not ok then
97 module:log("debug", "Error granting voice: %s", err or errtype);
98 event.origin.send(st.error_reply(event.stanza, errtype, err));
99 end
100 end);
101
102
103 return { 48 return {
104 get = get_moderated; 49 get = get_moderated;
105 set = set_moderated; 50 set = set_moderated;
106 }; 51 };