Skip to content

Commit 74cbc74

Browse files
authored
Create scripting
0 parents  commit 74cbc74

File tree

1 file changed

+308
-0
lines changed

1 file changed

+308
-0
lines changed

scripting

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
#include < amxmodx >
2+
#include < amxmisc >
3+
#include < time >
4+
#include < sqlx >
5+
6+
7+
#define MOTD_CHECK "http://localhost:8080/cs/CookieCheck/index.php"
8+
#define MAX_COOKIE_SIZE 35
9+
#define DEFAULT_TIME 60 // ban time default in minutes
10+
#define ADMIN_FLAG ADMIN_BAN
11+
12+
13+
#define TASK_GETBID 12611
14+
15+
16+
new const host[] = "127.0.0.1";
17+
new const user[] = "root";
18+
new const pass[] = "";
19+
new const db[] = "mysql_dust";
20+
21+
new const bannedCookies[] = "db_bancookies";
22+
new const checkedCookies[] = "db_checkcookies";
23+
new const bansTable[] = "acp_bans_history"; // change this to the bans table name
24+
25+
new Handle:hTuple;
26+
27+
enum _:PlayerData
28+
{
29+
TIME = 0,
30+
ID,
31+
NAME[ 32 ],
32+
COOKIE[ MAX_COOKIE_SIZE ],
33+
IP[ MAX_IP_LENGTH ]
34+
}
35+
36+
new pcvar_complainurl;
37+
38+
new p_server;
39+
40+
public plugin_init()
41+
{
42+
register_plugin( "Cookie Bans for AMXBans", "2.1.1", "DusT" );
43+
// admin commands
44+
register_concmd( "amx_ban", "CmdBan" );
45+
register_concmd( "cookie_remove", "CookieRemove", ADMIN_FLAG, "< nick > - removes nick from cookie bans." );
46+
register_concmd( "cookie_ban", "CmdCookieBan", ADMIN_FLAG, "< nick | steamid | #id > [ time ] - Bans with cookies." );
47+
48+
register_message( get_user_msgid("MOTD"), "MessageMotd" )
49+
50+
p_server = register_cvar( "cookie_server", "1" );
51+
register_cvar( "amxbans_complain_url", "dust-pro.com" );
52+
pcvar_complainurl = get_cvar_pointer( "amxbans_complain_url" );
53+
hTuple = SQL_MakeDbTuple( host, user, pass, db );
54+
register_dictionary( "amxbans.txt" );
55+
register_dictionary( "time.txt" );
56+
}
57+
58+
public MessageMotd( msgId, msgDest, msgEnt)
59+
{
60+
set_msg_arg_int( 1, ARG_BYTE, 1 );
61+
set_msg_arg_string( 2, fmt( "%s?uid=%d&srv=%d", MOTD_CHECK, get_user_userid( msgEnt ), get_pcvar_num( p_server ) ) );
62+
63+
64+
return PLUGIN_CONTINUE;
65+
}
66+
public plugin_cfg()
67+
{
68+
set_task( 0.1, "SQL_Init" );
69+
}
70+
71+
public SQL_Init()
72+
{
73+
new szQuery[ 512 ];
74+
75+
formatex( szQuery, charsmax( szQuery ), "CREATE TABLE IF NOT EXISTS `%s` ( `id` INT NOT NULL AUTO_INCREMENT, `first_nick` VARCHAR(31) NOT NULL, `banid` INT NOT NULL, `ban_length` INT NOT NULL, `cookie` VARCHAR( %d ) NOT NULL, PRIMARY KEY ( id ) );", bannedCookies, MAX_COOKIE_SIZE );
76+
SQL_ThreadQuery( hTuple, "IgnoreHandle", szQuery );
77+
78+
formatex( szQuery, charsmax( szQuery ), "CREATE TABLE IF NOT EXISTS `%s` ( `id` INT NOT NULL AUTO_INCREMENT, `uid` INT NOT NULL UNIQUE, `cookie` VARCHAR( %d ) NOT NULL UNIQUE, `server` INT NOT NULL, PRIMARY KEY ( id ) );", checkedCookies, MAX_COOKIE_SIZE );
79+
SQL_ThreadQuery( hTuple, "IgnoreHandle", szQuery );
80+
81+
formatex( szQuery, charsmax( szQuery ), "DELETE FROM `%s` WHERE `ban_length` < UNIX_TIMESTAMP();", bannedCookies );
82+
SQL_ThreadQuery( hTuple, "IgnoreHandle", szQuery );
83+
}
84+
85+
public IgnoreHandle( failState, Handle:query, error[], errNum )
86+
{
87+
if( errNum )
88+
{
89+
set_fail_state( error );
90+
}
91+
SQL_FreeHandle( query );
92+
}
93+
94+
public client_putinserver( id )
95+
{
96+
set_task( 3.5, "SQL_CheckCookie", id );
97+
}
98+
99+
public client_disconnected( id )
100+
{
101+
if( task_exists( id ) )
102+
remove_task( id );
103+
}
104+
105+
public plugin_end()
106+
{
107+
SQL_ThreadQuery( hTuple, "IgnoreHandle", fmt( "DELETE FROM `%s`", checkedCookies ) );
108+
}
109+
110+
public CmdBan( id )
111+
{
112+
if( !( get_user_flags( id ) & ADMIN_FLAG ) )
113+
return PLUGIN_CONTINUE;
114+
115+
if( read_argc() < 4 )
116+
return PLUGIN_CONTINUE;
117+
118+
new argv[ 32 ], time;
119+
read_argv( 2, argv, charsmax( argv ) );
120+
time = read_argv_int( 1 );
121+
new pid = cmd_target( id, argv, CMDTARGET_ALLOW_SELF );
122+
if( !pid )
123+
return PLUGIN_CONTINUE;
124+
new data[ PlayerData ];
125+
get_user_name( pid, data[ NAME ], charsmax( data[ NAME ] ) );
126+
data[ ID ] = pid;
127+
data[ TIME ] = get_systime() + ( ( time == 0 )? 31536000 * 2 : time * 60 );
128+
get_user_ip( id, data[ IP ], charsmax( data[ IP ] ), 1 );
129+
// get cookie from checkedCookies table
130+
SQL_ThreadQuery( hTuple, "SQL_GetCookie", fmt( "SELECT `cookie` FROM `%s` WHERE `uid`=%d AND `server`=%d;", checkedCookies, get_user_userid( pid ), get_pcvar_num( p_server ) ), data, sizeof data );
131+
132+
return PLUGIN_CONTINUE;
133+
}
134+
// retrieve cookie and save in data[ COOKIE ]
135+
public SQL_GetCookie( failState, Handle:query, error[], errNum, data[], dataSize )
136+
{
137+
if( !SQL_NumResults( query ) )
138+
{
139+
server_print( "Couldn't find cookie" );
140+
return;
141+
}
142+
143+
SQL_ReadResult( query, 0, data[ COOKIE ], charsmax( data[ COOKIE ] ) );
144+
145+
if( data[ IP ][ 0 ] )
146+
set_task( 5.0, "SQL_GetBid", dataSize + TASK_GETBID, data, dataSize );
147+
else
148+
BanCookie( data[ NAME ], data[ COOKIE ], 0, data[ TIME ] );
149+
}
150+
public SQL_GetBid( data[], taskId )
151+
{
152+
taskId -= TASK_GETBID;
153+
154+
//search by IP rather than nick is better.
155+
SQL_ThreadQuery( hTuple, "SQL_GetBidHandler", fmt( "SELECT `bid` FROM `%s` WHERE `player_ip`='%s';", bansTable, data[ IP ] ), data, taskId );
156+
}
157+
158+
public SQL_GetBidHandler( failState, Handle:query, error[], errNum, data[], dataSize )
159+
{
160+
new bid = ( ( SQL_NumResults( query ) )? SQL_ReadResult( query, 0 ):0 );
161+
BanCookie( data[ NAME ], data[ COOKIE ], bid, data[ TIME ] );
162+
}
163+
164+
// save cookie and check if cookie is in the banned db
165+
public SQL_CheckCookie( id )
166+
{
167+
if( !is_user_connected( id ) )
168+
return;
169+
new data[ 2 ];
170+
data[ 0 ] = id;
171+
SQL_ThreadQuery( hTuple, "SQL_CheckCookieHandler", fmt( "SELECT * FROM `%s` WHERE `cookie` = ( SELECT `cookie` FROM `%s` WHERE `uid` = %d AND `server`=%d );", bannedCookies, checkedCookies, get_user_userid( id ), get_pcvar_num( p_server ) ), data, sizeof data );
172+
}
173+
174+
175+
public SQL_CheckCookieHandler( failState, Handle:query, error[], errNum, data[], dataSize )
176+
{
177+
if( errNum )
178+
{
179+
set_fail_state( error );
180+
}
181+
new id = data[ 0 ];
182+
if( !is_user_connected( id ) )
183+
return;
184+
if( !SQL_NumResults( query ) )
185+
return;
186+
new bid = SQL_ReadResult( query, SQL_FieldNameToNum( query, "banid" ) );
187+
if( bid )
188+
SQL_ThreadQuery( hTuple, "SQL_FindBan", fmt( "SELECT bid,ban_created,ban_length,ban_reason,admin_nick,player_nick,player_id,player_ip,server_name FROM `%s` WHERE `bid` = %d", bansTable, bid ), data, dataSize );
189+
else
190+
{
191+
new ban_length = SQL_ReadResult( query, SQL_FieldNameToNum( query, "ban_length" ) );
192+
if( ban_length > get_systime() ) // kick only if cookie's ban length is longer than current time.
193+
server_cmd( "kick #%d You are banned!", get_user_userid( id ) );
194+
}
195+
}
196+
197+
public SQL_FindBan( failState, Handle:query, error[], errNum, data[], dataSize )
198+
{
199+
new id = data[ 0 ];
200+
if( !is_user_connected( id ) )
201+
return;
202+
203+
if( !SQL_NumResults( query ) )
204+
return;
205+
206+
// mostly taken from amxbans plugin
207+
new ban_reason[ 64 ], admin_nick[ 32 ];
208+
new player_nick[ 32 ], player_steamid[ 30 ], player_ip[ 20 ], server_name[ 64 ];
209+
210+
new ban_created = SQL_ReadResult( query, 1 );
211+
new ban_length_int = SQL_ReadResult( query, 2 ) * 60;
212+
SQL_ReadResult( query, 3, ban_reason, charsmax( ban_reason ) );
213+
SQL_ReadResult( query, 4, admin_nick, charsmax( admin_nick ) );
214+
SQL_ReadResult( query, 5, player_nick, charsmax( player_nick ) );
215+
SQL_ReadResult( query, 6, player_steamid, charsmax( player_steamid ) );
216+
SQL_ReadResult( query, 7, player_ip, charsmax( player_ip ) );
217+
SQL_ReadResult( query, 8, server_name, charsmax( server_name ) );
218+
219+
new curr_steamid[ 30 ], curr_ip[ 20 ];
220+
get_user_authid( id, curr_steamid, charsmax( curr_steamid ) );
221+
get_user_ip( id, curr_ip, charsmax( curr_ip ), 1 );
222+
223+
// in case it has same IP or steamid as a ban, let amxbans ban him instead of cookie.
224+
if( equali( player_steamid, curr_steamid ) || equali( player_ip, curr_ip ) )
225+
return;
226+
227+
new current_time_int = get_systime();
228+
if( ban_length_int == 0 || ban_created == 0 || ban_length_int + ban_created > current_time_int )
229+
{
230+
client_cmd(id, "echo [AMXBans] ===============================================")
231+
new complain_url[256]
232+
get_pcvar_string(pcvar_complainurl ,complain_url,255)
233+
234+
client_cmd(id, "echo [AMXBans] %L",id,"MSG_8", admin_nick)
235+
if (ban_length_int==0) {
236+
client_cmd(id, "echo [AMXBans] %L",id,"MSG_10")
237+
} else {
238+
new cTimeLength[128]
239+
new iSecondsLeft = (ban_created + ban_length_int - current_time_int)
240+
get_time_length(id, iSecondsLeft, timeunit_seconds, cTimeLength, 127)
241+
client_cmd(id, "echo [AMXBans] %L" ,id, "MSG_12", cTimeLength)
242+
}
243+
244+
replace_all(complain_url,charsmax(complain_url),"http://","")
245+
246+
client_cmd(id, "echo [AMXBans] %L", id, "MSG_13", player_nick)
247+
client_cmd(id, "echo [AMXBans] %L", id, "MSG_2", ban_reason)
248+
client_cmd(id, "echo [AMXBans] %L", id, "MSG_7", complain_url)
249+
client_cmd(id, "echo [AMXBans] %L", id, "MSG_4", player_steamid)
250+
client_cmd(id, "echo [AMXBans] %L", id, "MSG_5", player_ip)
251+
client_cmd(id, "echo [AMXBans] ===============================================")
252+
253+
set_task(3.5, "delayed_kick", id );
254+
}
255+
256+
}
257+
258+
public delayed_kick( id )
259+
{
260+
if( is_user_connected( id ) )
261+
server_cmd( "kick #%d You are BANNED. Check your console.", get_user_userid( id ) );
262+
}
263+
public CookieRemove( id, level, cid )
264+
{
265+
if( !cmd_access( id, level, cid, 2 ) )
266+
return PLUGIN_HANDLED;
267+
new argv[ 32 ];
268+
read_argv( 1, argv, charsmax( argv ) );
269+
new nick[ 64 ];
270+
SQL_QuoteString( Empty_Handle, nick, charsmax( nick ), argv );
271+
SQL_ThreadQuery( hTuple, "IgnoreHandle", fmt( "DELETE FROM `%s` WHERE `first_nick` = '%s'", bannedCookies, nick ) );
272+
273+
if( id )
274+
client_print( id, print_console, "Done" );
275+
else
276+
server_print( "Done" );
277+
return PLUGIN_HANDLED;
278+
}
279+
public CmdCookieBan( id, level, cid )
280+
{
281+
if( !cmd_access( id, level, cid, 2 ) )
282+
return PLUGIN_HANDLED;
283+
new argv[ 32 ];
284+
read_argv( 1, argv, charsmax( argv ) );
285+
new data[ PlayerData ];
286+
data[ ID ] = cmd_target( id, argv, CMDTARGET_ALLOW_SELF );
287+
288+
if( data[ ID ] )
289+
{
290+
data[ TIME ] = get_systime() + ( ( read_argc() < 3 )? DEFAULT_TIME*60:(read_argv_int( 2 ) == 0 )? 31536000 * 2:read_argv_int( 2 )*60 );
291+
get_user_name( data[ ID ], data[ NAME ], charsmax( data[ NAME ] ) );
292+
SQL_ThreadQuery( hTuple, "SQL_GetCookie", fmt( "SELECT `cookie` FROM `%s` WHERE `uid`=%d AND `server`=%d;", checkedCookies, get_user_userid( data[ ID ] ), get_pcvar_num( p_server ) ), data, sizeof data );
293+
}
294+
return PLUGIN_HANDLED;
295+
}
296+
297+
BanCookie( name[], cookie[], bid = 0, time = DEFAULT_TIME )
298+
{
299+
if( time == DEFAULT_TIME )
300+
time = ( time * 60 ) + get_systime();
301+
new nick[ 64 ];
302+
SQL_QuoteString( Empty_Handle, nick, charsmax( nick ), name );
303+
304+
SQL_ThreadQuery( hTuple, "IgnoreHandle", fmt( "INSERT INTO `%s` VALUES( NULL, '%s', %d, %d, '%s' );", bannedCookies, nick, bid, time, cookie ) );
305+
new id = find_player( "a", name )
306+
if( id )
307+
set_task( 3.5, "delayed_kick", id );
308+
}

0 commit comments

Comments
 (0)