plugins/opdown.lua

1
local jid_bare = require "prosody.util.jid".bare;
2
 
3
function riddim.plugins.opdown(bot)
4
	local admin = bot.config.admin;
5
 
6
	-- To allow anyone other than the admin to use a command,
7
	-- simply add them to the table, like
8
	-- opdown_map = {
9
	--   op = { role = "moderator", "operator@host" }, -- operator is allowed to use !op
10
	--   "admin@host" -- allowed to use all commands
11
	-- }
12
	-- also, bot.config.admin is allowed to do anything
13
 
14
	local command_map = bot.config.opdown_map or {
15
		owner = { affiliation =       "owner" };
16
		admin = { affiliation =       "admin" };
17
		op    = { role        =   "moderator" };
18
		member= { affiliation =      "member" };
19
		down  = { role        = "participant",
20
		          affiliation =        "none" };
21
		--ban   = { affiliation =     "outcast" };
22
		--kick  = { role        =        "none" };
23
	}
24
 
25
	function opdown(command)
26
		if not command.room then
27
			return "This command is only available in groupchats.";
28
		end
29
 
30
		local what = command_map[command.command];
31
		if not what then return end
32
		local room = command.room;
33
		local who = command.param or command.sender.nick;
34
		local commander = command.sender;
35
		local actor = jid_bare(command.sender.real_jid);
36
 
37
		if not actor then
38
			return "I don't know who you really are?";
39
		end
40
 
41
		if actor ~= admin then
42
			local allow = false;
43
			for i = 1,#what do
44
				if what[i] == actor then
45
					allow = true;
46
					break;
47
				end
48
			end
49
			if not allow then
50
				for i = 1,#command_map do
51
					if command_map[i] == actor then
52
						allow = true;
53
						break;
54
					end
55
				end
56
			end
57
			if not allow then
58
				return "I can't let you do that!";
59
			end
60
		end
61
 
62
		if command.room.occupants[who] then
63
			if what.role then
64
				command.room:set_role(who, what.role, "As commanded");
65
			end
66
			if what.affiliation then
67
				command.room:set_affiliation(who, what.affiliation, "As commanded");
68
			end
69
		end
70
	end
71
 
72
	for k in pairs(command_map) do
73
		bot:hook("commands/" .. k, opdown);
74
	end
75
end