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.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157. | #include <amxmodx>
#include <sockets>
#include <httpdl>
#define PLUGIN "Blacklisted.pl auto updater"
#define VERSION "1.0"
#define AUTHOR "Bentski & Sn!ff3r"
new dlinfo[MAX_DOWNLOADS + 1][5];
new dlpath[MAX_DOWNLOADS + 1][256];
new ndownloading;
new fwd_dlcomplete, fwd_result;
new banlist[] = "blacklisted.cfg";
new enabled, hour, minute, autoexec;
public plugin_init() {
//register_plugin("HTTP Download", "1.0", "Bentski");
register_plugin(PLUGIN, VERSION, AUTHOR)
fwd_dlcomplete = CreateMultiForward("dlcomplete", ET_IGNORE, FP_CELL, FP_STRING);
enabled = register_cvar("amx_autobl","1");
hour = register_cvar("amx_autobl_hour","5");
minute = register_cvar("amx_autobl_minute","30");
autoexec = register_cvar("amx_autobl_autoexec","1");
register_cvar("autobl",VERSION,FCVAR_SERVER);
if(get_pcvar_num(enabled)) {
set_task(60.0,"check_time",_,_,_,"b");
if(get_pcvar_num(autoexec)) {
server_cmd("exec %s",banlist);
}
}
}
public plugin_natives() {
register_library("httpdl");
register_native("download", "native_download", 1);
}
public native_download(url[], path[]) {
new slot = 0;
while(slot <= MAX_DOWNLOADS && dlinfo[slot][0] != 0)
slot++;
if(slot == MAX_DOWNLOADS) {
log_amx("Maksymalna ilosc plikow przekroczona (%d)", MAX_DOWNLOADS);
return 0;
}
param_convert(1);
param_convert(2);
new u[256], p[256];
copy(u, 7, url);
if(equal(u, "http://"))
copy(u, 248, url[7]);
else copy(u, 255, url);
new pos = 0;
new len = strlen(u);
while (++pos < len && u[pos] != '/') { }
copy(p, 255, u[pos + 1]);
copy(u, pos, u);
new error = 0;
new socket = dlinfo[slot][2] = socket_open(u, 80, SOCKET_TCP, error);
switch(error) {
case 0: {
new msg[512];
format(msg, 511, "GET /%s HTTP/1.1^r^nHost: %s^r^n^r^n", p, u);
socket_send(socket, msg, 512);
copy(dlpath[slot], 255, path);
dlinfo[slot][3] = fopen(path, "wb");
dlinfo[slot][0] = 1;
dlinfo[slot][4] = 0;
ndownloading++;
if(ndownloading == 1)
set_task(0.2, "download_task", 3318, _, _, "b");
new id = dlinfo[slot][1] = random_num(1, 65535);
return id;
}
case 1: log_amx("[HTTP] Blad przy tworzeniu socket'a");
case 2: log_amx("[HTTP] Nie mozna odnalezc domeny");
case 3: log_amx("[HTTP] Nie mozna polaczyc z %s:80", u);
}
return 0;
}
public download_task() {
for(new i = 0; i < MAX_DOWNLOADS; i++) {
if(dlinfo[i][0] == 0)
continue;
new socket = dlinfo[i][2];
new f = dlinfo[i][3];
if(socket_change(socket)) {
new buffer[1024];
new len = socket_recv(socket, buffer, 1024);
if(dlinfo[i][4] == 0) { // if first packet then cut the header
new pos = contain(buffer, "^r^n^r^n");
if(pos > -1) {
for(new j = pos + 4; j < len; j++)
fputc(f, buffer[j]);
dlinfo[i][4]++;
continue;
}
}
// is there a better way to write binary data to a file? :S
for(new j = 0; j < len; j++)
fputc(f, buffer[j]);
dlinfo[i][4]++;
continue;
}
fclose(f);
ExecuteForward(fwd_dlcomplete, fwd_result, dlinfo[i][1], dlpath[i]);
dlinfo[i][0] = 0;
ndownloading--;
if(ndownloading == 0)
remove_task(3318);
socket_close(socket);
}
}
public check_time() {
new arg1[4];
get_time("%H",arg1,3);
if(get_pcvar_num(hour) == str_to_num(arg1)) {
get_time("%M",arg1,3);
if(get_pcvar_num(minute) == str_to_num(arg1)) {
update_list();
}
}
}
public update_list() {
if(file_exists(banlist))
delete_file(banlist)
download("http://www.blacklisted.pl/getconfig.html", banlist);
}
public dlcomplete(id, file[]) {
log_amx("Zakonczono pobieranie listy %s",banlist);
fix_file();
}
public fix_file() {
if(write_file(banlist,"// Automatyczny update listy",0)) { // fix first line, this line will be empty, so maybe add a comment :) ?
new num = file_size(banlist,1);
if(write_file(banlist,"",num - 3)) { // remove 0 from eof
log_amx("Zakonczono poprawe pliku %s",banlist);
server_cmd("exec %s",banlist); // yeah, file updated, so execute it!
}
}
else {
log_amx("Blad przy poprawnianiu pliku %s",banlist);
}
} |