Debian Codegraph @

lua/handle_command.lua

raw symbols SCIP
1 --[[
2 Copyright (C) 2014 HarpyWar ([email protected])
3
4 This file is a part of the PvPGN Project http://pvpgn.pro
5 Licensed under the same terms as Lua itself.
6 ]]--
7
8
9 -- List of available lua commands
10 -- (To create a new command - create a new file in directory "commands")
11 local lua_command_table = {
12 [1] = {
13 ["/w3motd"] = command_w3motd,
14
15 -- Quiz
16 ["/quiz"] = command_quiz,
17
18 -- GHost
19 ["/ghost"] = command_ghost,
20
21 ["/host"] = command_host,
22 ["/chost"] = command_chost,
23 ["/unhost"] = command_unhost,
24 ["/ping"] = command_ping, ["/p"] = command_ping,
25 ["/swap"] = command_swap,
26 ["/open"] = command_open_close,
27 ["/close"] = command_open_close,
28 ["/start"] = command_start_abort_pub_priv,
29 ["/abort"] = command_start_abort_pub_priv,
30 ["/pub"] = command_start_abort_pub_priv,
31 ["/priv"] = command_start_abort_pub_priv,
32 ["/stats"] = command_stats,
33 },
34 [8] = {
35 ["/redirect"] = command_redirect,
36 },
37 }
38
39
40 -- Global function to handle commands
41 -- ("return 1" from a command will allow next C++ code execution)
42 function handle_command(account, text)
43 -- find command in table
44 for cg,cmdlist in pairs(lua_command_table) do
45 for cmd,func in pairs(cmdlist) do
46 if string.starts(text, cmd) then
47
48 -- check if command group is in account commandgroups
49 if math_and(account_get_auth_command_groups(account.name), cg) == 0 then
50 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command is reserved for admins."))
51 return -1
52 end
53
54 -- FIXME: we can use _G[func] if func is a text but not a function,
55 -- like ["/dotastats"] = "command_dotastats"
56 -- and function command_dotastats can be defined below, not only before
57 return func(account, text)
58 end
59 end
60 end
61 return 1
62 end
63
64
65 -- Executes before executing any command
66 -- "return 0" stay with flood protection
67 -- "return 1" allow ignore flood protection
68 -- "return -1" will prevent next command execution silently
69 function handle_command_before(account, text)
70 -- special users
71 for k,username in pairs(config.flood_immunity_users) do
72 if (username == account.name) then return 1 end
73 end
74
75 if (config.ghost) then
76 -- ghost bots
77 for k,username in pairs(config.ghost_bots) do
78 if (username == account.name) then return 1 end
79 end
80 end
81
82 return 0
83 end
84
85
86 -- Split command to arguments,
87 -- index 0 is always a command name without a slash
88 -- return table with arguments
89 function split_command(text, args_count)
90 local count = args_count
91 local result = {}
92 local tmp = ""
93
94 -- remove slash from the command
95 if not string:empty(text) then
96 text = string.sub(text, 2)
97 end
98
99 i = 0
100 -- split by space
101 for token in string.split(text) do
102 if not string:empty(token) then
103 if (i < count) then
104 result[i] = token
105 i = i + 1
106 else
107 if not string:empty(tmp) then
108 tmp = tmp .. " "
109 end
110 tmp = tmp .. token
111 end
112 end
113 end
114 -- push remaining text at the end
115 if not string:empty(tmp) then
116 result[count] = tmp
117 end
118 return result
119 end
120