util.encodings: Spell out all IDNA 2008 options ICU has
[prosody.git] / plugins / muc / request.lib.lua
blob4e95fdc3f333b3219214616a26d7c4ac8b23fb87
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 --
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();
15 end);
17 local voice_request_form = require "util.dataforms".new({
18 title = "Voice Request";
20 name = "FORM_TYPE";
21 type = "hidden";
22 value = "http://jabber.org/protocol/muc#request";
25 name = "muc#jid";
26 type = "jid-single";
27 label = "User ID";
28 desc = "The user's JID (address)";
31 name = "muc#roomnick";
32 type = "text-single";
33 label = "Room nickname";
34 desc = "The user's nickname within the room";
37 name = "muc#role";
38 type = "list-single";
39 label = "Requested role";
40 value = "participant";
41 options = {
42 "none",
43 "visitor",
44 "participant",
45 "moderator",
49 name = "muc#request_allow";
50 type = "boolean";
51 label = "Grant voice to this person?";
52 desc = "Specify whether this person is able to speak in a moderated room";
53 value = false;
55 });
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);
60 local event = {
61 room = room;
62 origin = origin;
63 stanza = stanza;
64 fields = fields;
65 occupant = occupant;
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);
70 else
71 module:log("debug", "%s requested voice in %s", jid_resource(occupant.nick), room.jid);
72 module:fire_event("muc-voice-request", event);
73 end
74 end
76 module:hook("muc-voice-request", function(event)
77 if event.occupant.role == "visitor" then
78 local nick = jid_resource(event.occupant.nick);
79 local formdata = {
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";
89 end);
90 end
91 end);
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));
100 return;
103 if not event.fields["muc#request_allow"] then
104 module:log("debug", "%s did not grant voice", jid_resource(occupant.nick));
105 return;
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"]);
111 return;
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));
117 return;
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");
123 if not ok then
124 module:log("debug", "Error granting voice: %s", err or errtype);
125 event.origin.send(st.error_reply(event.stanza, errtype, err));
127 end);
130 return {
131 handle_request = handle_request;