plugins.opdown: New plugin that gives configurable room administrative commands.

Sat, 20 Nov 2010 18:42:42 +0100

author
Kim Alvefur <zash@zash.se>
date
Sat, 20 Nov 2010 18:42:42 +0100
changeset 47
b84572a2e6cb
parent 46
1f0fa4af29b8
child 48
222b0b70baf3

plugins.opdown: New plugin that gives configurable room administrative commands.

plugins/opdown.lua file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/opdown.lua	Sat Nov 20 18:42:42 2010 +0100
@@ -0,0 +1,76 @@
+local jid_bare = require "util.jid".bare;
+
+function riddim.plugins.opdown(bot)
+	local admin = bot.config.admin;
+
+	-- To allow anyone other than the admin to use a command,
+	-- simply add them to the table, like
+	-- opdown_map = {
+	--   op = { role = "moderator", "operator@host" }, -- operator is allowed to use !op
+	--   "admin@host" -- allowed to use all commands
+	-- }
+	-- also, bot.config.admin is allowed to do anything
+
+	local command_map = bot.config.opdown_map or {
+		owner = { affiliation =       "owner" };
+		admin = { affiliation =       "admin" };
+		op    = { role        =   "moderator" };
+		member= { affiliation =      "member" };
+		down  = { role        = "participant",
+		          affiliation =        "none" };
+		--ban   = { affiliation =     "outcast" };
+		--kick  = { role        =        "none" };
+	}
+
+	function opdown(command)
+		if not command.room then
+			return "This command is only available in groupchats.";
+		end
+
+		local what = command_map[command.command];
+		if not what then return end
+		local room = command.room;
+		local who = command.param or command.sender.nick;
+		local commander = command.sender;
+		local actor = jid_bare(command.sender.real_jid);
+
+		if not actor then
+			return "I don't know who you really are?";
+		end
+
+		if actor ~= admin then
+			local allow = false;
+			for i = 1,#what do
+				if what[i] == actor then
+					allow = true;
+					break;
+				end
+			end
+			if not allow then
+				for i = 1,#command_map do
+					if command_map[i] == actor then
+						allow = true;
+						break;
+					end
+				end
+			end
+			if not allow then
+				return "I can't let you do that!";
+			end
+		end
+
+		if command.room.occupants[who] then
+			if what.role then
+				command.room:set_role(who, what.role, "As commanded");
+			end
+			if what.affiliation then
+				command.room:set_affiliation(who, what.affiliation, "As commanded");
+			end
+		end
+	end
+
+	for k in pairs(command_map) do
+		bot:hook("commands/" .. k, opdown);
+	end
+end
+

mercurial