]> jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_commands.c
Fix dates for ulined glines.
[irc/quakenet/newserv.git] / glines / glines_commands.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "../lib/version.h"
5 #include "../control/control.h"
6 #include "../lib/irc_string.h"
7 #include "../lib/splitline.h"
8 #include "../lib/strlfunc.h"
9 #include "../core/nsmalloc.h"
10 #include "../irc/irc.h"
11 #include "../localuser/localuser.h"
12 #include "../localuser/localuserchannel.h"
13 #include "glines.h"
14 #include "../trusts/trusts.h"
15
16 MODULE_VERSION("");
17
18 static void registercommands(int, void *);
19 static void deregistercommands(int, void *);
20
21 static int parse_gline_flags(nick *sender, const char *flagparam, int *overridesanity, int *overridelimit, int *simulate, int *coff) {
22 const char *pos;
23
24 *coff = 0;
25 *overridesanity = 0;
26 *overridelimit = 0;
27 *simulate = 0;
28
29 if (flagparam[0] == '-') {
30 *coff = 1;
31
32 for (pos = flagparam + 1; *pos; pos++) {
33 switch (*pos) {
34 case 'f':
35 *overridesanity = 1;
36 break;
37 case 'l':
38 *overridelimit = 1;
39 break;
40 case 'S':
41 *simulate = 1;
42 break;
43 default:
44 controlreply(sender, "Invalid flag specified: %c", *pos);
45 return 0;
46 }
47 }
48 }
49
50 return 1;
51 }
52
53 static int glines_cmdblock(void *source, int cargc, char **cargv) {
54 nick *sender = source;
55 nick *target;
56 int hits, duration, id;
57 int coff, overridesanity, overridelimit, simulate;
58 char *reason;
59 char creator[128];
60 glinebuf gbuf;
61
62 if (cargc < 1)
63 return CMD_USAGE;
64
65 if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &coff))
66 return CMD_ERROR;
67
68 if (cargc < 3 + coff)
69 return CMD_USAGE;
70
71 target = getnickbynick(cargv[coff]);
72
73 if (!target) {
74 controlreply(sender, "Sorry, couldn't find that user.");
75 return CMD_ERROR;
76 }
77
78 duration = durationtolong(cargv[coff + 1]);
79
80 if (duration <= 0) {
81 controlreply(sender, "Invalid duration specified.");
82 return CMD_ERROR;
83 }
84
85 rejoinline(cargv[coff + 2], cargc - coff - 2);
86 reason = cargv[coff + 2];
87
88 if (sender->auth)
89 snprintf(creator, sizeof(creator), "#%s", sender->authname);
90 else
91 strncpy(creator, controlid(sender), sizeof(creator));
92
93 glinebufinit(&gbuf, 0);
94 glinebufcommentv(&gbuf, "BLOCK", cargc + coff - 1, cargv);
95 glinebufaddbynick(&gbuf, target, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
96
97 glinebufspew(&gbuf, sender);
98
99 if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) {
100 glinebufabort(&gbuf);
101 controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines.");
102 return CMD_ERROR;
103 }
104
105 if (simulate) {
106 glinebufabort(&gbuf);
107 controlreply(sender, "Simulation complete. Not setting G-Lines.");
108 return CMD_ERROR;
109 }
110
111 glinebufcounthits(&gbuf, &hits, NULL);
112 id = glinebufcommit(&gbuf, 1);
113
114 controlwall(NO_OPER, NL_GLINES, "%s BLOCK'ed user '%s!%s@%s' for %s with reason '%s' (%d hits)", controlid(sender), target->nick, target->ident, target->host->name->content, longtoduration(duration, 0), reason, hits);
115
116 controlreply(sender, "Done. G-Line transaction ID: %d", id);
117
118 return CMD_OK;
119 }
120
121 static int glines_cmdgline(void *source, int cargc, char **cargv) {
122 nick *sender = source;
123 int duration, users, channels, id;
124 char *mask, *reason;
125 char creator[128];
126 int coff, overridesanity, overridelimit, simulate;
127 glinebuf gbuf;
128 #if SNIRCD_VERSION < 140
129 gline *gl;
130 #endif /* SNIRCD_VERSION */
131
132 if (cargc < 1)
133 return CMD_USAGE;
134
135 if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &coff))
136 return CMD_ERROR;
137
138 if (cargc < 3 + coff)
139 return CMD_USAGE;
140
141 mask = cargv[coff];
142
143 duration = durationtolong(cargv[coff + 1]);
144
145 if (duration <= 0) {
146 controlreply(sender, "Invalid duration specified.");
147 return CMD_ERROR;
148 }
149
150 rejoinline(cargv[coff + 2], cargc - coff - 2);
151 reason = cargv[coff + 2];
152
153 #if SNIRCD_VERSION < 140
154 gl = findgline(mask);
155
156 if (gl) {
157 /* warn opers that they can't modify this gline */
158 if (gl->flags & GLINE_ACTIVE) {
159 controlreply(sender, "Active G-Line already exists on %s - unable to modify", mask);
160 return CMD_ERROR;
161 }
162
163 controlreply(sender, "Reactivating existing gline on %s", mask);
164 }
165 #endif /* SNIRCD_VERSION */
166
167 if (sender->auth)
168 snprintf(creator, sizeof(creator), "#%s", sender->authname);
169 else
170 strncpy(creator, controlid(sender), sizeof(creator));
171
172 glinebufinit(&gbuf, 0);
173 glinebufcommentv(&gbuf, "GLINE", cargc + coff - 1, cargv);
174
175 if (!glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration)) {
176 controlreply(sender, "Invalid G-Line mask.");
177 return CMD_ERROR;
178 }
179
180 glinebufspew(&gbuf, sender);
181
182 if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) {
183 glinebufabort(&gbuf);
184 controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines.");
185 return CMD_ERROR;
186 }
187
188 if (simulate) {
189 glinebufabort(&gbuf);
190 controlreply(sender, "Simulation complete. Not setting G-Lines.");
191 return CMD_ERROR;
192 }
193
194 glinebufcounthits(&gbuf, &users, &channels);
195 id = glinebufcommit(&gbuf, 1);
196
197 controlwall(NO_OPER, NL_GLINES, "%s GLINE'd mask '%s' for %s with reason '%s' (hits %d users/%d channels)",
198 controlid(sender), mask, longtoduration(duration, 0), reason, users, channels);
199
200 controlreply(sender, "Done. G-Line transaction ID: %d", id);
201
202 return CMD_OK;
203 }
204
205 static int glines_cmdsmartgline(void *source, int cargc, char **cargv) {
206 nick *sender = source;
207 char *origmask;
208 char mask[512];
209 char *p, *user, *host;
210 struct irc_in_addr ip;
211 unsigned char bits;
212 int hits, duration;
213 int coff, overridesanity, overridelimit, simulate, id;
214 char *reason;
215 char creator[128];
216 glinebuf gbuf;
217
218 if (cargc < 1)
219 return CMD_USAGE;
220
221 if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &coff))
222 return CMD_ERROR;
223
224 if (cargc < 3 + coff)
225 return CMD_USAGE;
226
227 origmask = cargv[coff];
228
229 if (origmask[0] == '#' || origmask[0] == '&' || origmask[0] == '$') {
230 controlreply(sender, "Please use \"gline\" for badchan or realname glines.");
231 return CMD_ERROR;
232 }
233
234 duration = durationtolong(cargv[coff + 1]);
235
236 if (duration <= 0) {
237 controlreply(sender, "Invalid duration specified.");
238 return CMD_ERROR;
239 }
240
241 rejoinline(cargv[coff + 2], cargc - coff - 2);
242 reason = cargv[coff + 2];
243
244 strncpy(mask, origmask, sizeof(mask));
245
246 if (strchr(mask, '!')) {
247 controlreply(sender, "Use \"gline\" to place nick glines.");
248 return CMD_ERROR;
249 }
250
251 p = strchr(mask, '@');
252
253 if (!p) {
254 controlreply(sender, "Mask must contain a username (e.g. user@ip).");
255 return CMD_ERROR;
256 }
257
258 user = mask;
259 host = p + 1;
260 *p = '\0';
261
262 if (strchr(user, '*') || strchr(user, '?')) {
263 controlreply(sender, "Usernames may not contain wildcards.");
264 return CMD_ERROR;
265 }
266
267 if (!ipmask_parse(host, &ip, &bits)) {
268 controlreply(sender, "Invalid CIDR mask.");
269 return CMD_ERROR;
270 }
271
272 if (sender->auth)
273 snprintf(creator, sizeof(creator), "#%s", sender->authname);
274 else
275 strncpy(creator, controlid(sender), sizeof(creator));
276
277 glinebufinit(&gbuf, 0);
278 glinebufcommentv(&gbuf, "SMARTGLINE", cargc + coff - 1, cargv);
279 glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
280
281 glinebufspew(&gbuf, sender);
282
283 if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) {
284 glinebufabort(&gbuf);
285 controlreply(sender, "G-Lines failed sanity checks. Not setting G-Lines.");
286 return CMD_ERROR;
287 }
288
289 if (simulate) {
290 glinebufabort(&gbuf);
291 controlreply(sender, "Simulation complete. Not setting G-Lines.");
292 return CMD_ERROR;
293 }
294
295 glinebufcounthits(&gbuf, &hits, NULL);
296 id = glinebufcommit(&gbuf, 1);
297
298 controlwall(NO_OPER, NL_GLINES, "%s SMARTGLINE'd mask '%s' for %s with reason '%s' (%d hits)",
299 controlid(sender), cargv[0], longtoduration(duration, 0), reason, hits);
300
301 controlreply(sender, "Done. G-Line transaction ID: %d", id);
302
303 return CMD_OK;
304 }
305
306 static int glines_cmdungline(void *source, int cargc, char **cargv) {
307 nick *sender = source;
308 gline *gl;
309
310 if (cargc < 1)
311 return CMD_USAGE;
312
313 gl = findgline(cargv[0]);
314
315 if (!gl) {
316 controlreply(sender, "No such G-Line.");
317 return CMD_ERROR;
318 }
319
320 if (!(gl->flags & GLINE_ACTIVE)) {
321 controlreply(sender, "G-Line was already deactivated.");
322 return CMD_ERROR;
323 }
324
325 gline_deactivate(gl, 0, 1);
326
327 controlwall(NO_OPER, NL_GLINES, "%s UNGLINE'd mask '%s'", controlid(sender), cargv[0]);
328
329 controlreply(sender, "G-Line deactivated.");
330
331 return CMD_OK;
332 }
333
334 static int glines_cmddestroygline(void *source, int cargc, char **cargv) {
335 nick *sender = source;
336 gline *gl;
337
338 if (cargc < 1)
339 return CMD_USAGE;
340
341 gl = findgline(cargv[0]);
342
343 if (!gl) {
344 controlreply(sender, "No such G-Line.");
345 return CMD_ERROR;
346 }
347
348 gline_destroy(gl, 0, 1);
349
350 controlwall(NO_OPER, NL_GLINES, "%s DESTROYGLINE'd mask '%s'", controlid(sender), cargv[0]);
351
352 controlreply(sender, "G-Line destroyed.");
353
354 return CMD_OK;
355 }
356
357 static int glines_cmdclearchan(void *source, int cargc, char **cargv) {
358 nick *sender = source;
359 channel *cp;
360 nick *np;
361 char *reason = "Clearing channel.";
362 int mode, duration, i, slot, hits, id;
363 int coff, overridesanity, overridelimit, simulate;
364 array victims;
365 char creator[128];
366 glinebuf gbuf;
367
368 if (cargc < 1)
369 return CMD_USAGE;
370
371 if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &coff))
372 return CMD_ERROR;
373
374 if (cargc < 2 + coff)
375 return CMD_USAGE;
376
377 cp = findchannel(cargv[coff]);
378
379 if (!cp) {
380 controlreply(sender, "Couldn't find that channel.");
381 return CMD_ERROR;
382 }
383
384 if (strcmp(cargv[coff + 1], "kick") == 0)
385 mode = 0;
386 else if (strcmp(cargv[coff + 1], "kill") == 0)
387 mode = 1;
388 else if (strcmp(cargv[coff + 1], "gline") == 0)
389 mode = 2;
390 else if (strcmp(cargv[coff + 1], "glineall") == 0)
391 mode = 3;
392 else
393 return CMD_USAGE;
394
395 if (mode == 0 || mode == 1) {
396 if (cargc >= 3) {
397 rejoinline(cargv[coff + 2], cargc - coff - 2);
398 reason = cargv[coff + 2];
399 }
400 } else {
401 if (cargc < 3 + coff)
402 return CMD_USAGE;
403
404 duration = durationtolong(cargv[coff + 2]);
405
406 if (duration <= 0) {
407 controlreply(sender, "Invalid duration specified.");
408 return CMD_ERROR;
409 }
410
411 if (cargc >= 4 + coff) {
412 rejoinline(cargv[coff + 3], cargc - coff - 3);
413 reason = cargv[coff + 3];
414 }
415 }
416
417 array_init(&victims, sizeof(nick *));
418
419 /* we need to make a list of the channel users here because
420 * kicking/killing them will affect their position in the channel's
421 * user list. */
422 for (i = 0; i < cp->users->hashsize; i++) {
423 if (cp->users->content[i] == nouser)
424 continue;
425
426 np = getnickbynumeric(cp->users->content[i]);
427
428 if (!np)
429 continue;
430
431 if (IsService(np) || IsOper(np) || NickOnServiceServer(np))
432 continue;
433
434 slot = array_getfreeslot(&victims);
435 (((nick **)victims.content)[slot]) = np;
436 }
437
438 if (sender->auth)
439 snprintf(creator, sizeof(creator), "#%s", sender->authname);
440 else
441 strncpy(creator, controlid(sender), sizeof(creator));
442
443 glinebufinit(&gbuf, 0);
444 glinebufcommentv(&gbuf, "CLEARCHAN", cargc + coff - 1, cargv);
445
446 for (i = 0; i < victims.cursi; i++) {
447 np = ((nick **)victims.content)[i];
448
449 switch (mode) {
450 case 0:
451 if (simulate)
452 controlreply(sender, "user: %s!%s@%s r(%s)", np->nick, np->ident, np->host->name->content, np->realname->name->content);
453 else
454 localkickuser(NULL, cp, np, reason);
455
456 break;
457 case 1:
458 if (simulate)
459 controlreply(sender, "user: %s!%s@%s r(%s)", np->nick, np->ident, np->host->name->content, np->realname->name->content);
460 else
461 killuser(NULL, np, "%s", reason);
462
463 break;
464 case 2:
465 if (IsAccount(np))
466 break;
467 /* fall through */
468 case 3:
469 glinebufaddbynick(&gbuf, np, 0, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
470 break;
471 default:
472 assert(0);
473 }
474 }
475
476 if (mode != 0 && mode != 1) {
477 glinebufspew(&gbuf, sender);
478
479 if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) {
480 glinebufabort(&gbuf);
481 controlreply(sender, "G-Line failed sanity checks. Not setting G-Line.");
482 return CMD_ERROR;
483 }
484 }
485
486 if (simulate) {
487 glinebufabort(&gbuf);
488 controlreply(sender, "Simulation complete. Not clearing channel.");
489 return CMD_ERROR;
490 }
491
492 glinebufmerge(&gbuf);
493 glinebufcounthits(&gbuf, &hits, NULL);
494 id = glinebufcommit(&gbuf, 1);
495
496 array_free(&victims);
497
498 if (mode == 0 || mode == 1) {
499 controlwall(NO_OPER, NL_GLINES, "%s CLEARCHAN'd channel '%s' with mode '%s' and reason '%s'",
500 controlid(sender), cp->index->name->content, cargv[1], reason);
501 controlreply(sender, "Done.");
502 } else {
503 controlwall(NO_OPER, NL_GLINES, "%s CLEARCHAN'd channel '%s' with mode '%s', duration %s and reason '%s' (%d hits)",
504 controlid(sender), cp->index->name->content, cargv[1], longtoduration(duration, 0), reason, hits);
505 controlreply(sender, "Done. G-Line transaction ID: %d", id);
506 }
507
508 return CMD_OK;
509 }
510
511 static int glines_cmdtrustgline(void *source, int cargc, char **cargv) {
512 nick *sender = source;
513 trustgroup *tg;
514 trusthost *th;
515 int duration, hits;
516 int coff, overridesanity, overridelimit, simulate, id;
517 char *reason;
518 char mask[512];
519 char creator[128];
520 glinebuf gbuf;
521
522 if (cargc < 1)
523 return CMD_USAGE;
524
525 if (!parse_gline_flags(sender, cargv[0], &overridesanity, &overridelimit, &simulate, &coff))
526 return CMD_ERROR;
527
528 if (cargc < 4 + coff)
529 return CMD_USAGE;
530
531 tg = tg_strtotg(cargv[coff]);
532
533 if (!(tg->flags & TRUST_RELIABLE_USERNAME)) {
534 controlreply(sender, "Sorry, that trust group does not have the \"reliable username\" flag.");
535 return CMD_ERROR;
536 }
537
538 duration = durationtolong(cargv[coff + 2]);
539
540 if (duration <= 0) {
541 controlreply(sender, "Invalid duration specified.");
542 return CMD_ERROR;
543 }
544
545 rejoinline(cargv[coff + 3], cargc - coff - 3);
546 reason = cargv[coff + 3];
547
548 if (sender->auth)
549 snprintf(creator, sizeof(creator), "#%s", sender->authname);
550 else
551 strncpy(creator, controlid(sender), sizeof(creator));
552
553 glinebufinit(&gbuf, 0);
554 glinebufcommentv(&gbuf, "TRUSTGLINE", cargc + coff - 1, cargv);
555
556 for(th = tg->hosts; th; th = th->next) {
557 snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], trusts_cidr2str(&th->ip, th->bits));
558 glinebufadd(&gbuf, mask, creator, reason, getnettime() + duration, getnettime(), getnettime() + duration);
559 }
560
561 glinebufspew(&gbuf, sender);
562
563 if (!glinebufchecksane(&gbuf, sender, overridesanity, overridelimit)) {
564 glinebufabort(&gbuf);
565 controlreply(sender, "G-Line failed sanity checks. Not setting G-Line.");
566 return CMD_ERROR;
567 }
568
569 if (simulate) {
570 glinebufabort(&gbuf);
571 controlreply(sender, "Simulation complete. Not setting G-Lines.");
572 return CMD_ERROR;
573 }
574
575 glinebufcounthits(&gbuf, &hits, NULL);
576 id = glinebufcommit(&gbuf, 1);
577
578 controlwall(NO_OPER, NL_GLINES, "%s TRUSTGLINE'd user '%s' on trust group '%s' for %s with reason '%s' (%d hits)",
579 controlid(sender), cargv[1], tg->name->content, longtoduration(duration, 0), reason, hits);
580
581 controlreply(sender, "Done. G-Line transaction ID: %d", id);
582
583 return CMD_OK;
584 }
585
586 static int glines_cmdtrustungline(void *source, int cargc, char **cargv) {
587 nick *sender = source;
588 trustgroup *tg;
589 trusthost *th;
590 char mask[512];
591 gline *gl;
592 int count;
593
594 if (cargc < 2)
595 return CMD_USAGE;
596
597 tg = tg_strtotg(cargv[0]);
598
599 if (!(tg->flags & TRUST_RELIABLE_USERNAME)) {
600 controlreply(sender, "Sorry, that trust group does not have the \"reliable username\" flag.");
601 return CMD_ERROR;
602 }
603
604 count = 0;
605
606 for (th = tg->hosts; th; th = th->next) {
607 snprintf(mask, sizeof(mask), "*!%s@%s", cargv[1], trusts_cidr2str(&th->ip, th->bits));
608
609 gl = findgline(mask);
610
611 if (gl && (gl->flags & GLINE_ACTIVE)) {
612 gline_deactivate(gl, 0, 1);
613 count++;
614 }
615 }
616
617 controlwall(NO_OPER, NL_GLINES, "%s TRUSTUNGLINE'd user '%s' on trust group '%s' (%d G-Lines deactivated)",
618 controlid(sender), cargv[1], tg->name->content, count);
619
620 controlreply(sender, "Done.");
621
622 return CMD_OK;
623 }
624
625 static int glines_cmdglstats(void *source, int cargc, char **cargv) {
626 nick *sender = (nick*)source;
627 gline *gl, *next;
628 time_t curtime = getnettime();
629 int glinecount = 0, hostglinecount = 0, ipglinecount = 0, badchancount = 0, rnglinecount = 0;
630 int deactivecount = 0, activecount = 0;
631
632 for (gl = glinelist; gl; gl = next) {
633 next = gl->next;
634
635 if (gl->lifetime <= curtime) {
636 removegline(gl);
637 continue;
638 } else if (gl->expire <= curtime) {
639 gl->flags &= ~GLINE_ACTIVE;
640 }
641
642 if (gl->flags & GLINE_ACTIVE) {
643 activecount++;
644 } else {
645 deactivecount++;
646 }
647
648 if (gl->flags & GLINE_IPMASK)
649 ipglinecount++;
650 else if (gl->flags & GLINE_HOSTMASK)
651 hostglinecount++;
652 else if (gl->flags & GLINE_REALNAME)
653 rnglinecount++;
654 else if (gl->flags & GLINE_BADCHAN)
655 badchancount++;
656 glinecount++;
657 }
658
659 controlreply(sender, "Total G-Lines set: %d", glinecount);
660 controlreply(sender, "Hostmask G-Lines: %d", hostglinecount);
661 controlreply(sender, "IPMask G-Lines: %d", ipglinecount);
662 controlreply(sender, "Channel G-Lines: %d", badchancount);
663 controlreply(sender, "Realname G-Lines: %d", rnglinecount);
664
665 controlreply(sender, "Active G-Lines: %d", activecount);
666 controlreply(sender, "Inactive G-Lines: %d", deactivecount);
667
668 /* TODO show top 10 creators here */
669 /* TODO show unique creators count */
670 /* TODO show glines per create %8.1f", ccount?((float)gcount/(float)ccount):0 */
671 return CMD_OK;
672 }
673
674 static int glines_cmdglist(void *source, int cargc, char **cargv) {
675 nick *sender = (nick *)source;
676 gline *gl, *next;
677 time_t curtime = time(NULL);
678 int flags = 0;
679 char *mask;
680 int count = 0;
681 int limit = 500;
682 char expirestr[250], idstr[250];
683
684 if (cargc < 1 || (cargc == 1 && cargv[0][0] == '-')) {
685 controlreply(sender, "Syntax: glist [-flags] <mask>");
686 controlreply(sender, "Valid flags are:");
687 controlreply(sender, "-c: Count G-Lines.");
688 controlreply(sender, "-f: Find G-Lines active on <mask>.");
689 controlreply(sender, "-x: Find G-Lines matching <mask> exactly.");
690 controlreply(sender, "-R: Find G-lines on realnames.");
691 controlreply(sender, "-o: Search for glines by owner.");
692 controlreply(sender, "-r: Search for glines by reason.");
693 controlreply(sender, "-i: Include inactive glines.");
694 return CMD_ERROR;
695 }
696
697 mask = cargv[0];
698
699 if (cargc > 1) {
700 char* ch = cargv[0];
701
702 for (; *ch; ch++)
703 switch (*ch) {
704 case '-':
705 break;
706
707 case 'c':
708 flags |= GLIST_COUNT;
709 break;
710
711 case 'f':
712 flags |= GLIST_FIND;
713 break;
714
715 case 'x':
716 flags |= GLIST_EXACT;
717 break;
718
719 case 'r':
720 flags |= GLIST_REASON;
721 break;
722 case 'o':
723 flags |= GLIST_OWNER;
724 break;
725
726 case 'R':
727 flags |= GLIST_REALNAME;
728 break;
729
730 case 'i':
731 flags |= GLIST_INACTIVE;
732 break;
733
734 default:
735 controlreply(sender, "Invalid flag '%c'.", *ch);
736 return CMD_ERROR;
737 }
738
739 mask = cargv[1];
740 } else {
741 mask = cargv[0];
742 }
743
744 if ((flags & (GLIST_EXACT|GLIST_FIND)) == (GLIST_EXACT|GLIST_FIND)) {
745 controlreply(sender, "You cannot use -x and -f flags together.");
746 return CMD_ERROR;
747 }
748
749 if (!(flags & GLIST_COUNT))
750 controlreply(sender, "%-50s %-19s %-15s %-25s %s", "Mask:", "Expires in:", "Transaction ID:", "Creator:", "Reason:");
751
752 gline *searchgl = makegline(mask);
753
754 for (gl = glinelist; gl; gl = next) {
755 next = gl->next;
756
757 if (gl->lifetime <= curtime) {
758 removegline(gl);
759 continue;
760 } else if (gl->expire <= curtime) {
761 gl->flags &= ~GLINE_ACTIVE;
762 }
763
764 if (!(gl->flags & GLINE_ACTIVE)) {
765 if (!(flags & GLIST_INACTIVE)) {
766 continue;
767 }
768 }
769
770 if (flags & GLIST_REALNAME) {
771 if (!(gl->flags & GLINE_REALNAME))
772 continue;
773 if (flags & GLIST_EXACT) {
774 if (!glineequal(searchgl, gl)) {
775 continue;
776 }
777 } else if (flags & GLIST_FIND) {
778 if (!gline_match_mask(searchgl, gl)) {
779 continue;
780 }
781 }
782 } else {
783 if (gl->flags & GLINE_REALNAME)
784 continue;
785
786 if (flags & GLIST_REASON) {
787 if (flags & GLIST_EXACT) {
788 if (!gl->reason || ircd_strcmp(mask, gl->reason->content) != 0)
789 continue;
790 } else if (flags & GLIST_FIND) {
791 if (!gl->reason || match(gl->reason->content, mask))
792 continue;
793 } else if (!gl->reason || match(mask, gl->reason->content))
794 continue;
795 } else if (flags & GLIST_OWNER) {
796 if (flags & GLIST_EXACT) {
797 if (!gl->creator || ircd_strcmp(mask, gl->creator->content) != 0)
798 continue;
799 } else if (flags & GLIST_FIND) {
800 if (!gl->creator || match(gl->creator->content, mask))
801 continue;
802 } else if (!gl->creator || match(mask, gl->creator->content))
803 continue;
804 } else {
805 if (flags & GLIST_EXACT) {
806 if (!glineequal(searchgl, gl)) {
807 continue;
808 }
809 } else if (flags & GLIST_FIND) {
810 if (!gline_match_mask(searchgl, gl)) {
811 continue;
812 }
813 }
814 }
815 }
816
817 if (count == limit && !(flags & GLIST_COUNT))
818 controlreply(sender, "More than %d matches, list truncated.", limit);
819
820 count++;
821
822 if (!(flags & GLIST_COUNT) && count < limit) {
823 snprintf(expirestr, sizeof(expirestr), "%s", glinetostring(gl));
824 snprintf(idstr, sizeof(idstr), "%d", gl->glinebufid);
825 controlreply(sender, "%s%-49s %-19s %-15s %-25s %s",
826 (gl->flags & GLINE_ACTIVE) ? "+" : "-",
827 expirestr,
828 (gl->flags & GLINE_ACTIVE) ? (char*)longtoduration(gl->expire - curtime, 0) : "<inactive>",
829 gl->glinebufid ? idstr : "",
830 gl->creator ? gl->creator->content : "",
831 gl->reason ? gl->reason->content : "");
832 }
833 }
834
835 controlreply(sender, "%s%d G-Line%s found.", (flags & GLIST_COUNT) ? "" : "End of list - ", count, count == 1 ? "" : "s");
836
837 return CMD_OK;
838 }
839
840 static int glines_cmdglinelog(void *source, int cargc, char **cargv) {
841 nick *sender = source;
842 glinebuf *gbl;
843 gline *gl;
844 int i, id, count;
845 char timebuf[30];
846
847 id = 0;
848
849 if (cargc > 0) {
850 id = atoi(cargv[0]);
851
852 if (id == 0) {
853 controlreply(sender, "Invalid log ID.");
854 return CMD_ERROR;
855 }
856 }
857
858 controlreply(sender, "Time ID G-Lines User Hits Channel Hits Comment");
859
860 for (i = 0; i < MAXGLINELOG; i++) {
861 gbl = glinebuflog[(i + glinebuflogoffset + 1) % MAXGLINELOG];
862
863 if (!gbl)
864 continue;
865
866 if (id == 0 || gbl->id == id) {
867 count = 0;
868
869 for (gl = gbl->glines; gl; gl = gl->next)
870 count++;
871
872 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime((gbl->amend) ? &gbl->amend : &gbl->commit));
873 strncat(timebuf, (gbl->amend) ? "*" : " ", sizeof(timebuf));
874 controlreply(sender, "%-20s %-10d %-10d %-15d %-15d %s", timebuf, gbl->id, count, gbl->userhits, gbl->channelhits, gbl->comment ? gbl->comment->content : "(no comment)");
875 }
876
877 if (id != 0 && gbl->id == id) {
878 glinebufspew(gbl, sender);
879 controlreply(sender, "Done.");
880 return CMD_OK;
881 }
882 }
883
884 if (id == 0) {
885 controlreply(sender, "Done.");
886 } else {
887 controlreply(sender, "Log entry for ID %d not found.", id);
888 }
889
890 return CMD_OK;
891 }
892
893 static int glines_cmdglineundo(void *source, int cargc, char **cargv) {
894 nick *sender = source;
895 int id;
896
897 if (cargc < 1)
898 return CMD_USAGE;
899
900 id = atoi(cargv[0]);
901
902 if (id == 0 || !glinebufundo(id)) {
903 controlreply(sender, "Invalid log ID.");
904 return CMD_ERROR;
905 }
906
907 controlreply(sender, "Done.");
908
909 return CMD_OK;
910 }
911
912 static int glines_cmdsyncglines(void *source, int cargc, char **cargv) {
913 nick *sender = source;
914 gline *gl;
915 int count;
916
917 count = 0;
918
919 for (gl = glinelist; gl; gl = gl->next) {
920 gline_propagate(gl);
921 count++;
922 }
923
924 controlwall(NO_OPER, NL_GLINES, "%s SYNCGLINE'd %d G-Lines.",
925 controlid(sender), count);
926
927 controlreply(sender, "Done.");
928
929 return CMD_OK;
930 }
931
932 static int glines_cmdcleanupglines(void *source, int cargc, char **cargv) {
933 nick *sender = source;
934 gline **pnext, *gl;
935 int count;
936 time_t now;
937
938 count = 0;
939 time(&now);
940
941 for (pnext = &glinelist; *pnext;) {
942 gl = *pnext;
943
944 /* Remove inactivate glines that have been last changed more than a week ago */
945 if (!(gl->flags & GLINE_ACTIVE) && gl->lastmod < now - 7 * 24 * 60 * 60) {
946 gline_destroy(gl, 0, 1);
947 count++;
948 } else {
949 pnext = &((*pnext)->next);
950 }
951
952 if (!*pnext)
953 break;
954 }
955
956 controlwall(NO_OPER, NL_GLINES, "%s CLEANUPGLINES'd %d G-Lines.",
957 controlid(sender), count);
958
959 controlreply(sender, "Done.");
960
961 return CMD_OK;
962 }
963
964 static int commandsregistered;
965
966 static void registercommands(int hooknum, void *arg) {
967 if (commandsregistered)
968 return;
969 commandsregistered = 1;
970
971 registercontrolhelpcmd("block", NO_OPER, 4, glines_cmdblock, "Usage: block ?flags? <nick> <duration> <reason>\nSets a gline using an appropriate mask given the user's nickname.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit");
972 registercontrolhelpcmd("gline", NO_OPER, 4, glines_cmdgline, "Usage: gline ?flags? <mask> <duration> <reason>\nSets a gline.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit");
973 registercontrolhelpcmd("smartgline", NO_OPER, 4, glines_cmdsmartgline, "Usage: smartgline ?flags? <user@host> <duration> <reason>\nSets a gline. Automatically adjusts the mask depending on whether the specified mask is trusted.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit");
974 registercontrolhelpcmd("ungline", NO_OPER, 1, glines_cmdungline, "Usage: ungline <mask>\nDeactivates a gline.");
975 registercontrolhelpcmd("destroygline", NO_DEVELOPER, 1, glines_cmddestroygline, "Usage: destroygline <mask>\nDestroys a gline.");
976 registercontrolhelpcmd("clearchan", NO_OPER, 5, glines_cmdclearchan, "Usage: clearchan ?flags? <#channel> <how> <duration> ?reason?\nClears a channel.\nhow can be one of:\nkick - Kicks users.\nkill - Kills users.\ngline - Glines non-authed users (using an appropriate mask).\nglineall - Glines users.\nDuration is only valid when glining users. Reason defaults to \"Clearing channel.\".\nFlags (for glines) can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit");
977 registercontrolhelpcmd("trustgline", NO_OPER, 5, glines_cmdtrustgline, "Usage: trustgline ?flags? <#id|name> <user> <duration> <reason>\nSets a gline on the specified username for each host in the specified trust group. The username may contain wildcards.\nFlags can be one or more of:\n-f - bypass sanity checks\n-l - bypass hit limits\n-S - simulate who the glines would hit");
978 registercontrolhelpcmd("trustungline", NO_OPER, 2, glines_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nRemoves a gline that was previously set with trustgline.");
979 registercontrolhelpcmd("glstats", NO_OPER, 0, glines_cmdglstats, "Usage: glstat\nShows statistics about G-Lines.");
980 registercontrolhelpcmd("glist", NO_OPER, 2, glines_cmdglist, "Usage: glist [-flags] <mask>\nLists matching G-Lines.\nValid flags are:\n-c: Count G-Lines.\n-f: Find G-Lines active on <mask>.\n-x: Find G-Lines matching <mask> exactly.\n-R: Find G-lines on realnames.\n-o: Search for glines by owner.\n-r: Search for glines by reason.\n-i: Include inactive glines.");
981 registercontrolhelpcmd("glinelog", NO_OPER, 1, glines_cmdglinelog, "Usage: glinelog ?id?\nShows information about previous gline transactions.");
982 registercontrolhelpcmd("glineundo", NO_OPER, 1, glines_cmdglineundo, "Usage: glineundo ?id?\nUndoes a gline transaction.");
983 registercontrolhelpcmd("syncglines", NO_DEVELOPER, 0, glines_cmdsyncglines, "Usage: syncglines\nSends all G-Lines to all other servers.");
984 registercontrolhelpcmd("cleanupglines", NO_DEVELOPER, 0, glines_cmdcleanupglines, "Usage: cleanupglines\nDestroys all deactivated G-Lines.");
985 }
986
987 static void deregistercommands(int hooknum, void *arg) {
988 if (!commandsregistered)
989 return;
990 commandsregistered = 0;
991
992 deregistercontrolcmd("block", glines_cmdblock);
993 deregistercontrolcmd("gline", glines_cmdgline);
994 deregistercontrolcmd("smartgline", glines_cmdsmartgline);
995 deregistercontrolcmd("ungline", glines_cmdungline);
996 deregistercontrolcmd("destroygline", glines_cmddestroygline);
997 deregistercontrolcmd("clearchan", glines_cmdclearchan);
998 deregistercontrolcmd("trustgline", glines_cmdtrustgline);
999 deregistercontrolcmd("trustungline", glines_cmdtrustungline);
1000 deregistercontrolcmd("glstats", glines_cmdglstats);
1001 deregistercontrolcmd("glist", glines_cmdglist);
1002 deregistercontrolcmd("glinelog", glines_cmdglinelog);
1003 deregistercontrolcmd("glineundo", glines_cmdglineundo);
1004 deregistercontrolcmd("syncglines", glines_cmdsyncglines);
1005 deregistercontrolcmd("cleanupglines", glines_cmdcleanupglines);
1006 }
1007
1008 void _init(void) {
1009 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
1010 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
1011
1012 if (trustsdbloaded)
1013 registercommands(0, NULL);
1014 }
1015
1016 void _fini(void) {
1017 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
1018 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
1019
1020 deregistercommands(0, NULL);
1021 }