plugins/tell.lua

Tue, 09 Nov 2010 01:53:26 +0100

author
Kim Alvefur <zash@zash.se>
date
Tue, 09 Nov 2010 01:53:26 +0100
changeset 40
313c0662100d
parent 30
7bfe0d739b1c
child 41
b00d05814d0d
permissions
-rw-r--r--

plugins.tell: Optinally deliver !tells only in the same room.

local st = require "util.stanza";
local tellings = {};

function riddim.plugins.tell(bot)
	local sameroom = bot.config.tell_in_same_room;
	bot:hook("commands/tell", function (command)
		if not command.room then
			return "This command is only available in groupchats.";
		end
		if not command.param then
			return "If you want me to tell someone something then do so";
		end

		local s, e = command.param:find(" ");

		if not s then
			return "if you have nothing to say to "..command.param..", then leave me alone, please";
		end

		local nick = command.param:sub(0, s - 1);
		local msg = command.param:sub(s + 1);

		if nick == command.sender.nick then
			return "Tell yourself.";
		end

		for tmp,_ in pairs(command.room.occupants) do
			if tmp == nick then
				return "" .. nick .. " is currently online!";
			end
		end

		local nick_id = sameroom and command.room.jid .. "/" .. nick or nick;

		if tellings[nick_id] == nil then
			tellings[nick_id] = {};
		end
		tellings[nick_id][#tellings[nick_id] + 1] = {from=command.sender.nick, msg=msg};
		return "Ok!";
	end);

	bot:hook("commands/untell", function (command)
		if not command.room then
			return "This command is only available in groupchats.";
		end
		if not command.param then
			return "If you changed your mind tell me for who";
		end

		local s, e = command.param:find(" ");
		local nick;
		local id;

		-- parameter parsing
		if not s then
			nick = command.param;
			id = nil;
		else
			nick = command.param:sub(0, s - 1);
			id = command.param:sub(s + 1);
		end

		-- no message for that user
		local nick_id = sameroom and command.room.jid .. "/" .. nick or nick;
		if not tellings[nick_id] then
			return "I have no messages for "..nick;
		end

		-- no message id and message for that user
		if id == nil then
			local response = "I am supposed to relay the following message to "..nick.." :";
			for index,msg in ipairs(tellings[nick_id]) do
				response = response .. "\n#"..index.." : "..msg.msg;
			end
			return response;
		end

		-- check the message id is valid
		local number = #tellings[nick_id];
		id = tonumber(id)
		if id == nil or id < 1 or id > number then
			return "I need a valid message #id .. sigh !!\n"..nick.." has "..number.." message(s)";
		end

		if tellings[nick_id][id].from ~= command.sender.nick then
			return "you never said that, "..tellings[nick_id][id].from.." did !";
		end

		-- remove the message
		if number > 1 then
			tellings[nick_id][id] = tellings[nick_id][number];
			tellings[nick_id][number] = nil;
			return "what was I supposed to tell "..nick.." again ?";
		else
			tellings[nick_id] = nil;
			return "who is "..nick.." anyway ?";
		end
	end);

	bot:hook("groupchat/joined", function (room)
		room:hook("occupant-joined", function (occupant)
			local nick_id = sameroom and room.jid .. "/" .. occupant.nick or occupant.nick;
			if(tellings[nick_id] ~= nil) then
				for _,msg in ipairs(tellings[nick_id]) do
					room:send_message(occupant.nick .. ": Welcome back! " .. msg.from .. " told me, to tell you, \"" .. msg.msg .. "\".");
				end
				tellings[nick_id] = nil;
			end
		end);
	end);
end

mercurial