mod_muc_bot/mod_muc_bot.lua
changeset 4567 30f2d7c3f946
child 4568 d25f0fea270f
equal deleted inserted replaced
4566:28c16c93d79a 4567:30f2d7c3f946
       
     1 local st = require "util.stanza";
       
     2 local jid = require "util.jid";
       
     3 
       
     4 local bots = module:get_option_set("known_bots", {});
       
     5 
       
     6 module:hook("muc-occupant-groupchat", function(event)
       
     7 	if event.occupant then return end -- skip messages from actual occupants
       
     8 	local room = event.room;
       
     9 
       
    10 	if bots:contains(jid.bare(event.from)) or bots:contains(jid.host(event.from)) then
       
    11 
       
    12 		local nick = room:get_registered_nick(jid);
       
    13 
       
    14 		if not nick then
       
    15 			-- Allow bot to specify its own nick, but we're appending '[bot]' to it.
       
    16 			-- FIXME HATS!!!
       
    17 			nick = event.stanza:get_child_text("nick", "http://jabber.org/protocol/nick");
       
    18 			nick = (nick or jid.bare(event.from)) .. "[bot]";
       
    19 		end
       
    20 
       
    21 		local virtual_occupant_jid = jid.prep(room.jid .. "/" .. nick, true);
       
    22 		if not virtual_occupant_jid then
       
    23 			module:send(st.error_reply(event.stanza, "modify", "jid-malformed", "Nickname must pass strict validation", room.jid));
       
    24 			return true;
       
    25 		end
       
    26 
       
    27 		-- Inject virtual occupant to trick all the other hooks on this event that
       
    28 		-- this is an actual legitimate participant.
       
    29 		-- XXX Hack!
       
    30 		event.occupant = {
       
    31 			nick = virtual_occupant_jid;
       
    32 			bare_jid = jid.bare(event.from);
       
    33 			role = "participant";
       
    34 		};
       
    35 
       
    36 	end
       
    37 end, 66);
       
    38 
       
    39 module:hook("muc-occupant-pre-join", function(event)
       
    40 	local room = event.room;
       
    41 	local nick = jid.resource(event.occupant.nick);
       
    42 	if nick:sub(-5, -1) == "[bot]" then
       
    43 		event.origin.send(st.error_reply(event.stanza, "modify", "policy-violation", "Only known bots may use the [bot] suffix", room.jid));
       
    44 		return true;
       
    45 	end
       
    46 end, 3);
       
    47 
       
    48 module:hook("muc-occupant-pre-change", function(event)
       
    49 	local room = event.room;
       
    50 	local nick = jid.resource(event.dest_occupant.nick);
       
    51 	if nick:sub(-5, -1) == "[bot]" then
       
    52 		event.origin.send(st.error_reply(event.stanza, "modify", "policy-violation", "Only known bots may use the [bot] suffix", room.jid));
       
    53 		return true;
       
    54 	end
       
    55 end, 3);
       
    56 
       
    57 assert(string.sub("foo[bot]", -5, -1) == "[bot]", "substring indicies, how do they work?");