2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2014 Daurnimator
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local st
= require
"util.stanza";
11 local jid_resource
= require
"util.jid".resource
;
13 module
:hook("muc-disco#info", function(event
)
14 event
.reply
:tag("feature", {var
= "http://jabber.org/protocol/muc#request"}):up();
17 local voice_request_form
= require
"util.dataforms".new({
18 title
= "Voice Request";
22 value
= "http://jabber.org/protocol/muc#request";
28 desc
= "The user's JID (address)";
31 name
= "muc#roomnick";
33 label
= "Room nickname";
34 desc
= "The user's nickname within the room";
39 label
= "Requested role";
40 value
= "participant";
49 name
= "muc#request_allow";
51 label
= "Grant voice to this person?";
52 desc
= "Specify whether this person is able to speak in a moderated room";
57 local function handle_request(room
, origin
, stanza
, form
)
58 local occupant
= room
:get_occupant_by_real_jid(stanza
.attr
.from
);
59 local fields
= voice_request_form
:data(form
);
67 if occupant
.role
== "moderator" then
68 module
:log("debug", "%s responded to a voice request in %s", jid_resource(occupant
.nick
), room
.jid
);
69 module
:fire_event("muc-voice-response", event
);
71 module
:log("debug", "%s requested voice in %s", jid_resource(occupant
.nick
), room
.jid
);
72 module
:fire_event("muc-voice-request", event
);
76 module
:hook("muc-voice-request", function(event
)
77 if event
.occupant
.role
== "visitor" then
78 local nick
= jid_resource(event
.occupant
.nick
);
80 ["muc#jid"] = event
.stanza
.attr
.from
;
81 ["muc#roomnick"] = nick
;
84 local message
= st
.message({ type = "normal"; from
= event
.room
.jid
})
85 :add_child(voice_request_form
:form(formdata
));
87 event
.room
:broadcast(message
, function (_
, occupant
)
88 return occupant
.role
== "moderator";
93 module
:hook("muc-voice-response", function(event
)
94 local actor
= event
.stanza
.attr
.from
;
95 local affected_occupant
= event
.room
:get_occupant_by_real_jid(event
.fields
["muc#jid"]);
96 local occupant
= event
.occupant
;
98 if occupant
.role
~= "moderator" then
99 module
:log("debug", "%s tried to grant voice but wasn't a moderator", jid_resource(occupant
.nick
));
103 if not event
.fields
["muc#request_allow"] then
104 module
:log("debug", "%s did not grant voice", jid_resource(occupant
.nick
));
108 if not affected_occupant
then
109 module
:log("debug", "%s tried to grant voice to unknown occupant %s",
110 jid_resource(occupant
.nick
), event
.fields
["muc#jid"]);
114 if affected_occupant
.role
~= "visitor" then
115 module
:log("debug", "%s tried to grant voice to %s but they already have it",
116 jid_resource(occupant
.nick
), jid_resource(occupant
.jid
));
120 module
:log("debug", "%s granted voice to %s", jid_resource(event
.occupant
.nick
), jid_resource(occupant
.jid
));
121 local ok
, errtype
, err
= event
.room
:set_role(actor
, affected_occupant
.nick
, "participant", "Voice granted");
124 module
:log("debug", "Error granting voice: %s", err
or errtype
);
125 event
.origin
.send(st
.error_reply(event
.stanza
, errtype
, err
));
131 handle_request
= handle_request
;