Skip to content

Instantly share code, notes, and snippets.

@legastero
Created March 28, 2016 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save legastero/ad5f40c3ce4fd5651857 to your computer and use it in GitHub Desktop.
Save legastero/ad5f40c3ce4fd5651857 to your computer and use it in GitHub Desktop.
diff -r 62d533535334 plugins/muc/moderated.lib.lua
--- a/plugins/muc/moderated.lib.lua Thu Mar 24 13:57:58 2016 +0100
+++ b/plugins/muc/moderated.lib.lua Mon Mar 28 10:53:44 2016 -0700
@@ -7,6 +7,10 @@
-- COPYING file in the source package for more information.
--
+local st = require "util.stanza";
+local dataform = require "util.dataforms";
+
+
local function get_moderated(room)
return room._data.moderated;
end
@@ -46,6 +50,71 @@
end
end, 1);
+module:hook("muc-voice-request", function(event)
+ if event.occupant.role == "visitor" then
+ local form = dataform.new({
+ title = "Voice Request";
+ {
+ name = "FORM_TYPE";
+ type = "hidden";
+ value = "http://jabber.org/protocol/muc#request";
+ },
+ {
+ name = "muc#role";
+ type = "text-single";
+ label = "Requested Role";
+ value = "participant";
+ },
+ {
+ name = "muc#jid";
+ type = "jid-single";
+ label = "User ID";
+ value = event.stanza.attr.from;
+ },
+ {
+ name = "muc#roomnick";
+ type = "text-single";
+ label = "Room Nickname";
+ value = event.occupant.nick;
+ },
+ {
+ name = "muc#request_allow";
+ type = "boolean";
+ label = "Grant voice to this person?";
+ value = false;
+ }
+ });
+
+ local message = st.message({ type = "normal"; from = event.room.jid }):add_child(form:form()):up();
+
+ event.room:broadcast(message, function (nick, occupant)
+ return occupant.role == "moderator";
+ end);
+ end
+end);
+
+module:hook("muc-voice-response", function(event)
+ local actor = event.stanza.attr.from;
+ local affected_occupant = event.room:get_occupant_by_real_jid(event.fields["muc#jid"]);
+
+ if event.occupant.role ~= "moderator" then
+ return;
+ end
+
+ if not event.fields["muc#request_allow"] then
+ return;
+ end
+
+ if not affected_occupant then
+ return;
+ end
+
+ if affected_occupant.role == "visitor" then
+ event.room:set_role(actor, affected_occupant.nick, "participant", "Voice granted");
+ end
+end);
+
+
return {
get = get_moderated;
set = set_moderated;
diff -r 62d533535334 plugins/muc/muc.lib.lua
--- a/plugins/muc/muc.lib.lua Thu Mar 24 13:57:58 2016 +0100
+++ b/plugins/muc/muc.lib.lua Mon Mar 28 10:53:44 2016 -0700
@@ -643,6 +643,34 @@
return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form;
end
+function room_mt:get_voice_form_layout()
+ local form = dataform.new({
+ {
+ name = "FORM_TYPE";
+ type = "hidden";
+ value = "http://jabber.org/protocol/muc#request";
+ },
+ {
+ name = "muc#jid";
+ type = "jid-single";
+ },
+ {
+ name = "muc#roomnick";
+ type = "text-single";
+ },
+ {
+ name = "muc#role";
+ type = "text-single";
+ },
+ {
+ name = "muc#request_allow";
+ type = "boolean";
+ }
+ });
+
+ return form;
+end
+
function room_mt:process_form(origin, stanza)
local form = stanza.tags[1]:get_child("x", "jabber:x:data");
if form.attr.type == "cancel" then
@@ -991,7 +1019,7 @@
return self:handle_groupchat_to_room(origin, stanza)
elseif type == "error" and is_kickable_error(stanza) then
return self:handle_kickable(origin, stanza)
- elseif type == nil then
+ elseif type == nil or type == "normal" then
local x = stanza:get_child("x", "http://jabber.org/protocol/muc#user");
if x then
local payload = x.tags[1];
@@ -1005,6 +1033,28 @@
origin.send(st.error_reply(stanza, "cancel", "bad-request"));
return true;
end
+
+ local form = stanza:get_child("x", "jabber:x:data");
+ if form and form.attr.type == "submit" then
+ local fields, errors, present = self:get_voice_form_layout():data(form);
+
+ if fields.FORM_TYPE == "http://jabber.org/protocol/muc#request" then
+ local occupant = self:get_occupant_by_real_jid(stanza.attr.from);
+ local event = {
+ room = self;
+ origin = origin;
+ stanza = stanza;
+ fields = fields;
+ occupant = occupant;
+ };
+ if occupant.role == "moderator" then
+ module:fire_event("muc-voice-response", event);
+ else
+ module:fire_event("muc-voice-request", event);
+ end
+ return true;
+ end
+ end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment