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