]> jfr.im git - irc/quakenet/newserv.git/blob - serverlist/serverlist.c
BUILD: specify all dependencies and upgrade to bionic
[irc/quakenet/newserv.git] / serverlist / serverlist.c
1 #include "../core/schedule.h"
2 #include "../irc/irc.h"
3 #include "../lib/irc_string.h"
4 #include "../localuser/localuserchannel.h"
5 #include "../control/control.h"
6 #include "../usercount/usercount.h"
7 #include "../lib/version.h"
8 #include "../serverlist/serverlist.h"
9 #include "../core/config.h"
10 #include "../lib/strlfunc.h"
11
12 MODULE_VERSION("")
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdarg.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <sys/time.h>
20 #include <pcre.h>
21
22 int cmd_serverlist(void *sender, int cargc, char **cargv);
23 void serverlist_hook_newserver(int hook, void *arg);
24 void serverlist_hook_lostserver(int hook, void *arg);
25 int serverlist_versionreply(void *source, int cargc, char **cargv);
26 void serverlist_pingservers(void *arg);
27 int serverlist_rpong(void *source, int cargc, char **cargv);
28
29 const flag servertypeflags[] = {
30 { 'c', SERVERTYPEFLAG_CLIENT_SERVER },
31 { 'h', SERVERTYPEFLAG_HUB },
32 { 's', SERVERTYPEFLAG_SERVICE },
33 { 'Q', SERVERTYPEFLAG_CHANSERV },
34 { 'S', SERVERTYPEFLAG_SPAMSCAN },
35 { 'X', SERVERTYPEFLAG_CRITICAL_SERVICE },
36 { '\0', 0 }
37 };
38
39 struct {
40 int used;
41 int lag;
42 sstring *version1;
43 sstring *version2;
44 flag_t type;
45 } serverinfo[MAXSERVERS];
46
47 void serverlist_doversion(void);
48
49 static sstring *s_server, *q_server;
50 static pcre *service_re, *hub_re, *not_client_re;
51
52 static pcre *compilefree(sstring *re) {
53 const char *err;
54 int erroffset;
55 pcre *r;
56
57 r = pcre_compile(re->content, 0, &err, &erroffset, NULL);
58 if(r == NULL) {
59 Error("serverlist", ERR_WARNING, "Unable to compile RE %s (offset: %d, reason: %s)", re->content, erroffset, err);
60 freesstring(re);
61 return NULL;
62 }
63
64 freesstring(re);
65 return r;
66 }
67
68 void _init(void) {
69 registercontrolhelpcmd("serverlist",NO_OPER,1,&cmd_serverlist,"Usage: serverlist [pattern]\nShows all currently connected servers");
70 /* hooks for serverlist */
71 registerhook(HOOK_SERVER_NEWSERVER, &serverlist_hook_newserver);
72 registerhook(HOOK_SERVER_LOSTSERVER, &serverlist_hook_lostserver);
73 int i;
74
75 q_server = getcopyconfigitem("serverlist", "q_server", "CServe.quakenet.org", HOSTLEN);
76 s_server = getcopyconfigitem("serverlist", "s_server", "services2.uk.quakenet.org", HOSTLEN);
77
78 service_re = compilefree(getcopyconfigitem("serverlist", "service_re", "^services\\d*\\..*$", 256));
79 hub_re = compilefree(getcopyconfigitem("serverlist", "hub_re", "^hub\\d*\\..*$", 256));
80 not_client_re = compilefree(getcopyconfigitem("serverlist", "not_client_re", "^(testserv\\d*\\.).*$", 256));
81
82 for (i = 0; i < MAXSERVERS; i++) {
83 if (serverlist[i].linkstate == LS_LINKED)
84 serverinfo[i].used = 1;
85 else
86 serverinfo[i].used = 0;
87
88 serverinfo[i].lag = -1;
89 serverinfo[i].version1 = NULL;
90 serverinfo[i].version2 = NULL;
91 serverinfo[i].type = getservertype(&serverlist[i]);
92 }
93 registernumerichandler(351, &serverlist_versionreply, 2);
94 registerserverhandler("RO", &serverlist_rpong, 4);
95
96 schedulerecurring(time(NULL)+1, 0, 60, &serverlist_pingservers, NULL);
97 }
98
99 void _fini(void) {
100 int i;
101 for (i = 0; i < MAXSERVERS; i++) {
102 if (serverinfo[i].used) {
103 freesstring(serverinfo[i].version1);
104 freesstring(serverinfo[i].version2);
105 }
106 }
107 deregisternumerichandler(351, &serverlist_versionreply);
108 deregisterserverhandler("RO", &serverlist_rpong);
109
110 deregisterhook(HOOK_SERVER_NEWSERVER, &serverlist_hook_newserver);
111 deregisterhook(HOOK_SERVER_LOSTSERVER, &serverlist_hook_lostserver);
112
113 deregistercontrolcmd("serverlist",cmd_serverlist);
114
115 deleteschedule(NULL, &serverlist_pingservers, NULL);
116
117 freesstring(q_server);
118 freesstring(s_server);
119 pcre_free(service_re);
120 pcre_free(hub_re);
121 pcre_free(not_client_re);
122 }
123
124 int cmd_serverlist(void *sender, int cargc, char **cargv) {
125 nick *np = (nick*)sender;
126 int a, i, ucount, acount, scount;
127 char lagstr[20];
128 char buf[512];
129
130 controlreply(np, "%-7s %-35s %5s/%5s/%-7s %-7s %-7s %-20s %-8s %-20s", "Numeric", "Hostname", "ECl", "Cl", "MaxCl", "Flags", "Type", "Connected for", "Lag", "Version");
131
132 scount = acount = 0;
133
134 for (i = 0; i < MAXSERVERS; i++) {
135 if (serverlist[i].linkstate == LS_LINKED && (cargc < 1 || match2strings(cargv[0], serverlist[i].name->content))) {
136 ucount = 0;
137
138 for (a = 0; a <= serverlist[i].maxusernum; a++)
139 if (servernicks[i][a] != NULL)
140 ucount++;
141
142 acount += ucount;
143 scount++;
144
145 if (serverinfo[i].lag == -1)
146 strcpy(lagstr, "-");
147 else
148 snprintf(lagstr, sizeof(lagstr), "%d", serverinfo[i].lag);
149
150 strlcpy(buf, printflags(serverinfo[i].type, servertypeflags), sizeof(buf));
151
152 controlreply(np, "%-7d %-35s %5d/%5d/%-7d %-7s %-7s %-20s %-8s %-20s - %s", i, serverlist[i].name->content,
153 servercount[i], ucount, serverlist[i].maxusernum,
154 printflags(serverlist[i].flags, smodeflags), buf,
155 longtoduration(getnettime() - serverlist[i].ts, 0),
156 lagstr,
157 serverinfo[i].version1 ? serverinfo[i].version1->content : "Unknown",
158 serverinfo[i].version2 ? serverinfo[i].version2->content : "Unknown");
159 }
160 }
161
162 controlreply(np, "--- End of list. %d users and %d servers on the net.", acount, scount);
163
164 /* update version info for next time */
165 serverlist_doversion();
166
167 return CMD_OK;
168 }
169
170 int serverlist_versionreply(void *source, int cargc, char **cargv) {
171 int num;
172
173 if (cargc < 6)
174 return CMD_OK;
175
176 num = numerictolong(cargv[0], 2);
177
178 if (serverinfo[num].used) {
179 freesstring(serverinfo[num].version1);
180 freesstring(serverinfo[num].version2);
181
182 serverinfo[num].version1 = getsstring(cargv[3], strlen(cargv[3]));
183 serverinfo[num].version2 = getsstring(cargv[5], strlen(cargv[5]));
184 }
185 return CMD_OK;
186 }
187
188 void serverlist_doversion(void) {
189 int i;
190 char *num1, *numeric;
191
192 if (mynick == NULL)
193 return;
194
195 for (i = 0; i < MAXSERVERS; i++) {
196 if (serverlist[i].linkstate == LS_LINKED && serverinfo[i].version1 == NULL) {
197 numeric = longtonumeric(mynick->numeric,5);
198 num1 = (char*)malloc(strlen(numeric) + 1); /* bleh.. longtonumeric() is using static variables */
199 strcpy(num1, numeric);
200
201 irc_send("%s V :%s", num1, longtonumeric(i,2));
202 free(num1);
203 }
204 }
205 }
206
207 void serverlist_hook_newserver(int hook, void *arg) {
208 char *num1, *numeric;
209 long num = (long)arg;
210
211 serverinfo[num].used = 1;
212 serverinfo[num].version1 = NULL;
213 serverinfo[num].version2 = NULL;
214 serverinfo[num].type = getservertype(&serverlist[num]);
215
216 if (mynick == NULL) /* bleh this is buggy... doesn't do it when mynick appears */
217 return;
218
219 numeric = longtonumeric(mynick->numeric,5);
220 num1 = (char*)malloc(strlen(numeric) + 1); /* bleh.. longtonumeric() is using static variables */
221 strcpy(num1, numeric);
222
223 irc_send("%s V :%s", num1, longtonumeric(num,2));
224
225 free(num1);
226 }
227
228 void serverlist_hook_lostserver(int hook, void *arg) {
229 long num = (long)arg;
230
231 serverinfo[num].used = 0;
232 freesstring(serverinfo[num].version1);
233 freesstring(serverinfo[num].version2);
234 }
235
236 flag_t getservertype(server *server) {
237 flag_t result = 0;
238 char *server_name;
239 int server_len;
240
241 if(server == NULL || server->name == NULL)
242 return 0;
243
244 server_name = server->name->content;
245 server_len = server->name->length;
246
247 if(server->flags & SMODE_SERVICE)
248 result|=SERVERTYPEFLAG_SERVICE;
249
250 if(!strcmp(server_name, q_server->content)) {
251 result|=SERVERTYPEFLAG_CHANSERV;
252 result|=SERVERTYPEFLAG_CRITICAL_SERVICE;
253 } else if(!strcmp(server_name, s_server->content)) {
254 result|=SERVERTYPEFLAG_SPAMSCAN;
255 result|=SERVERTYPEFLAG_CRITICAL_SERVICE;
256 } else {
257 if(service_re && (pcre_exec(service_re, NULL, server_name, server_len, 0, 0, NULL, 0) >= 0)) {
258 /* matches service re */
259 if((server->flags & SMODE_SERVICE) == 0) {
260 /* is not a service */
261 Error("serverlist", ERR_WARNING, "Non-service server (%s) matched service RE.", server_name);
262 }
263 } else {
264 /* does not match service re */
265 if((server->flags & SMODE_SERVICE) != 0) {
266 result|=SERVERTYPEFLAG_SERVICE;
267 Error("serverlist", ERR_WARNING, "Service server (%s) that does not match service RE.", server_name);
268 }
269 }
270 }
271
272 if(hub_re && (pcre_exec(hub_re, NULL, server_name, server_len, 0, 0, NULL, 0) >= 0)) {
273 if((server->flags & SMODE_HUB) != 0) {
274 result|=SERVERTYPEFLAG_HUB;
275 } else {
276 Error("serverlist", ERR_WARNING, "Server matched hub re but isn't a hub (%s).", server_name);
277 }
278 }
279
280 if(not_client_re && (pcre_exec(not_client_re, NULL, server_name, server_len, 0, 0, NULL, 0) >= 0)) {
281 /* noop */
282 } else if(result == 0) {
283 result|=SERVERTYPEFLAG_CLIENT_SERVER;
284 }
285
286 return result;
287 }
288
289 void serverlist_pingservers(void *arg) {
290 int i;
291 server *from, *to;
292 char fnum[10], tnum[10];
293 struct timeval tv;
294
295 if (!mynick)
296 return;
297
298 for(i=0;i<MAXSERVERS;i++) {
299 to = &serverlist[i];
300
301 if (to->parent == -1)
302 continue;
303
304 from = &serverlist[to->parent];
305
306 if (to->linkstate != LS_LINKED || from->linkstate != LS_LINKED)
307 continue;
308
309 strcpy(fnum, longtonumeric(to->parent,2));
310 strcpy(tnum, longtonumeric(i, 2));
311
312 if (from->parent == -1) { /* Are we the source? */
313 (void) gettimeofday(&tv, NULL);
314 irc_send("%s RI %s %s %jd %jd :RP", mynumeric->content, tnum, longtonumeric(mynick->numeric,5), (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
315 } else {
316 strcpy(fnum, longtonumeric(to->parent,2));
317 strcpy(tnum, longtonumeric(i, 2));
318 irc_send("%s RI %s %s :RP", longtonumeric(mynick->numeric,5), to->name->content, fnum);
319 }
320 }
321 }
322
323 int serverlist_rpong(void *source, int cargc, char **cargv) {
324 int to, lag;
325 struct timeval tv;
326
327 if (cargc < 4)
328 return CMD_ERROR;
329
330 if (cargc == 5) { /* From target to source server */
331 if (strcmp(cargv[4], "RP") != 0)
332 return CMD_OK;
333
334 to = numerictolong(source, 2);
335
336 (void) gettimeofday(&tv, NULL);
337 lag = (tv.tv_sec - strtoull(cargv[2], NULL, 10)) * 1000 + (tv.tv_usec - strtoull(cargv[3], NULL, 10)) / 1000;
338 } else { /* From source server to client */
339 if (strcmp(cargv[3], "RP") != 0)
340 return CMD_OK;
341
342 to = findserver(cargv[1]);
343
344 if (to == -1)
345 return CMD_ERROR;
346
347 lag = atoi(cargv[2]);
348 }
349
350 serverinfo[to].lag = lag;
351
352 return CMD_OK;
353 }