Software /
code /
prosody
Comparison
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 |
comparison
equal
deleted
inserted
replaced
8864:cf2f66b233d1 | 8865:2a8bbfcb6868 |
---|---|
1 -- Prosody IM | |
2 -- Copyright (C) 2008-2010 Matthew Wild | |
3 -- Copyright (C) 2008-2010 Waqas Hussain | |
4 -- Copyright (C) 2014 Daurnimator | |
5 -- | |
6 -- This project is MIT/X11 licensed. Please see the | |
7 -- COPYING file in the source package for more information. | |
8 -- | |
9 | |
10 local st = require "util.stanza"; | |
11 local jid_resource = require "util.jid".resource; | |
12 | |
13 local voice_request_form = require "util.dataforms".new({ | |
14 title = "Voice Request"; | |
15 { | |
16 name = "FORM_TYPE"; | |
17 type = "hidden"; | |
18 value = "http://jabber.org/protocol/muc#request"; | |
19 }, | |
20 { | |
21 name = "muc#jid"; | |
22 type = "jid-single"; | |
23 label = "User ID"; | |
24 }, | |
25 { | |
26 name = "muc#roomnick"; | |
27 type = "text-single"; | |
28 label = "Room Nickname"; | |
29 }, | |
30 { | |
31 name = "muc#role"; | |
32 type = "list-single"; | |
33 label = "Requested Role"; | |
34 value = "participant"; | |
35 options = { | |
36 "none", | |
37 "visitor", | |
38 "participant", | |
39 "moderator", | |
40 }; | |
41 }, | |
42 { | |
43 name = "muc#request_allow"; | |
44 type = "boolean"; | |
45 label = "Grant voice to this person?"; | |
46 value = false; | |
47 } | |
48 }); | |
49 | |
50 local function handle_request(room, origin, stanza, form) | |
51 local occupant = room:get_occupant_by_real_jid(stanza.attr.from); | |
52 local fields = voice_request_form:data(form); | |
53 local event = { | |
54 room = room; | |
55 origin = origin; | |
56 stanza = stanza; | |
57 fields = fields; | |
58 occupant = occupant; | |
59 }; | |
60 if occupant.role == "moderator" then | |
61 module:log("debug", "%s responded to a voice request in %s", jid_resource(occupant.nick), room.jid); | |
62 module:fire_event("muc-voice-response", event); | |
63 else | |
64 module:log("debug", "%s requested voice in %s", jid_resource(occupant.nick), room.jid); | |
65 module:fire_event("muc-voice-request", event); | |
66 end | |
67 end | |
68 | |
69 module:hook("muc-voice-request", function(event) | |
70 if event.occupant.role == "visitor" then | |
71 local nick = jid_resource(event.occupant.nick); | |
72 local formdata = { | |
73 ["muc#jid"] = event.stanza.attr.from; | |
74 ["muc#roomnick"] = nick; | |
75 }; | |
76 | |
77 local message = st.message({ type = "normal"; from = event.room.jid }):add_child(voice_request_form:form(formdata)):up(); | |
78 | |
79 event.room:broadcast(message, function (_, occupant) | |
80 return occupant.role == "moderator"; | |
81 end); | |
82 end | |
83 end); | |
84 | |
85 module:hook("muc-voice-response", function(event) | |
86 local actor = event.stanza.attr.from; | |
87 local affected_occupant = event.room:get_occupant_by_real_jid(event.fields["muc#jid"]); | |
88 local occupant = event.occupant; | |
89 | |
90 if occupant.role ~= "moderator" then | |
91 module:log("debug", "%s tried to grant voice but wasn't a moderator", jid_resource(occupant.nick)); | |
92 return; | |
93 end | |
94 | |
95 if not event.fields["muc#request_allow"] then | |
96 module:log("debug", "%s did not grant voice", jid_resource(occupant.nick)); | |
97 return; | |
98 end | |
99 | |
100 if not affected_occupant then | |
101 module:log("debug", "%s tried to grant voice to unknown occupant %s", jid_resource(occupant.nick), event.fields["muc#jid"]); | |
102 return; | |
103 end | |
104 | |
105 if affected_occupant.role ~= "visitor" then | |
106 module:log("debug", "%s tried to grant voice to %s but they already have it", jid_resource(occupant.nick), jid_resource(occupant.jid)); | |
107 return; | |
108 end | |
109 | |
110 module:log("debug", "%s granted voice to %s", jid_resource(event.occupant.nick), jid_resource(occupant.jid)); | |
111 local ok, errtype, err = event.room:set_role(actor, affected_occupant.nick, "participant", "Voice granted"); | |
112 | |
113 if not ok then | |
114 module:log("debug", "Error granting voice: %s", err or errtype); | |
115 event.origin.send(st.error_reply(event.stanza, errtype, err)); | |
116 end | |
117 end); | |
118 | |
119 | |
120 return { | |
121 handle_request = handle_request; | |
122 }; |