File: handle_command.lua

package info (click to toggle)
pvpgn 1.99.7.2.1%2Bdfsg-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 8,300 kB
  • sloc: cpp: 118,407; xml: 6,367; sh: 1,558; ansic: 1,300; perl: 604; cs: 382; python: 308; php: 130; awk: 73; makefile: 19
file content (120 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
--[[
	Copyright (C) 2014 HarpyWar (harpywar@gmail.com)
	
	This file is a part of the PvPGN Project http://pvpgn.pro
	Licensed under the same terms as Lua itself.
]]--


-- List of available lua commands
--  (To create a new command - create a new file in directory "commands")
local lua_command_table = {
	[1] = {
		["/w3motd"] = command_w3motd,

		-- Quiz
		["/quiz"] = command_quiz,
		
		-- GHost
		["/ghost"] = command_ghost,
		
		["/host"] = command_host,
		["/chost"] = command_chost,
		["/unhost"] = command_unhost,
		["/ping"] = command_ping, ["/p"] = command_ping,
		["/swap"] = command_swap,
		["/open"] = command_open_close,
		["/close"] = command_open_close,
		["/start"] = command_start_abort_pub_priv, 
		["/abort"] = command_start_abort_pub_priv,
		["/pub"] = command_start_abort_pub_priv,
		["/priv"] = command_start_abort_pub_priv,
		["/stats"] = command_stats,
	},
	[8] = {
		["/redirect"] = command_redirect,
	},
}


-- Global function to handle commands
--   ("return 1" from a command will allow next C++ code execution)
function handle_command(account, text)
	-- find command in table
	for cg,cmdlist in pairs(lua_command_table) do
		for cmd,func in pairs(cmdlist) do
			if string.starts(text, cmd) then
				
				-- check if command group is in account commandgroups
				if math_and(account_get_auth_command_groups(account.name), cg) == 0 then
					api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command is reserved for admins."))
					return -1
				end
				
				-- FIXME: we can use _G[func] if func is a text but not a function, 
				--        like ["/dotastats"] = "command_dotastats"
				--        and function command_dotastats can be defined below, not only before
				return func(account, text) 
			end
		end
	end
	return 1
end


-- Executes before executing any command
-- "return 0" stay with flood protection
-- "return 1" allow ignore flood protection
-- "return -1" will prevent next command execution silently
function handle_command_before(account, text)
	-- special users
	for k,username in pairs(config.flood_immunity_users) do
		if (username == account.name) then return 1 end
	end

	if (config.ghost) then
		-- ghost bots
		for k,username in pairs(config.ghost_bots) do
			if (username == account.name) then return 1 end
		end
	end
	
	return 0
end


-- Split command to arguments, 
--  index 0 is always a command name without a slash 
--  return table with arguments
function split_command(text, args_count)
	local count = args_count
	local result = {}
	local tmp = ""
	
	-- remove slash from the command
	if not string:empty(text) then
		text = string.sub(text, 2)
	end

	i = 0
	-- split by space
	for token in string.split(text) do
		if not string:empty(token) then 
			if (i < count) then
				result[i] = token 
				i = i + 1
			else
				if not string:empty(tmp) then
					tmp = tmp .. " "
				end
				tmp = tmp .. token
			end
		end
	end
	-- push remaining text at the end
	if not string:empty(tmp) then
    	result[count] = tmp
	end
	return result
end