]> jfr.im git - irc/quakenet/newserv.git/blob - glines/glines_commands.c
Rename trusts_cidr2str to CIDRtostr() and move it to lib/irc_ipv6.c.
[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], CIDRtostr(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], CIDRtostr(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 if (cargc > 1) {
698 char* ch = cargv[0];
699
700 for (; *ch; ch++)
701 switch (*ch) {
702 case '-':
703 break;
704
705 case 'c':
706 flags |= GLIST_COUNT;
707 break;
708
709 case 'f':
710 flags |= GLIST_FIND;
711 break;
712
713 case 'x':
714 flags |= GLIST_EXACT;
715 break;
716
717 case 'r':
718 flags |= GLIST_REASON;
719 break;
720 case 'o':
721 flags |= GLIST_OWNER;
722 break;
723
724 case 'R':
725 flags |= GLIST_REALNAME;
726 break;
727
728 case 'i':
729 flags |= GLIST_INACTIVE;
730 break;
731
732 default:
733 controlreply(sender, "Invalid flag '%c'.", *ch);
734 return CMD_ERROR;
735 }
736
737 mask = cargv[1];
738 } else {
739 mask = cargv[0];
740 }
741
742 if ((flags & (GLIST_EXACT|GLIST_FIND)) == (GLIST_EXACT|GLIST_FIND)) {
743 controlreply(sender, "You cannot use -x and -f flags together.");
744 return CMD_ERROR;
745 }
746
747 if (!(flags & GLIST_COUNT))
748 controlreply(sender, "%-50s %-19s %-15s %-25s %s", "Mask:", "Expires in:", "Transaction ID:", "Creator:", "Reason:");
749
750 gline *searchgl = makegline(mask);
751
752 for (gl = glinelist; gl; gl = next) {
753 next = gl->next;
754
755 if (gl->lifetime <= curtime) {
756 removegline(gl);
757 continue;
758 } else if (gl->expire <= curtime) {
759 gl->flags &= ~GLINE_ACTIVE;
760 }
761
762 if (!(gl->flags & GLINE_ACTIVE)) {
763 if (!(flags & GLIST_INACTIVE)) {
764 continue;
765 }
766 }
767
768 if (flags & GLIST_REALNAME) {
769 if (!(gl->flags & GLINE_REALNAME))
770 continue;
771 if (flags & GLIST_EXACT) {
772 if (!glineequal(searchgl, gl)) {
773 continue;
774 }
775 } else if (flags & GLIST_FIND) {
776 if (!gline_match_mask(searchgl, gl)) {
777 continue;
778 }
779 }
780 } else {
781 if (gl->flags & GLINE_REALNAME)
782 continue;
783
784 if (flags & GLIST_REASON) {
785 if (flags & GLIST_EXACT) {
786 if (!gl->reason || ircd_strcmp(mask, gl->reason->content) != 0)
787 continue;
788 } else if (flags & GLIST_FIND) {
789 if (!gl->reason || match(gl->reason->content, mask))
790 continue;
791 } else if (!gl->reason || match(mask, gl->reason->content))
792 continue;
793 } else if (flags & GLIST_OWNER) {
794 if (flags & GLIST_EXACT) {
795 if (!gl->creator || ircd_strcmp(mask, gl->creator->content) != 0)
796 continue;
797 } else if (flags & GLIST_FIND) {
798 if (!gl->creator || match(gl->creator->content, mask))
799 continue;
800 } else if (!gl->creator || match(mask, gl->creator->content))
801 continue;
802 } else {
803 if (flags & GLIST_EXACT) {
804 if (!glineequal(searchgl, gl)) {
805 continue;
806 }
807 } else if (flags & GLIST_FIND) {
808 if (!gline_match_mask(searchgl, gl)) {
809 continue;
810 }
811 }
812 }
813 }
814
815 if (count == limit && !(flags & GLIST_COUNT))
816 controlreply(sender, "More than %d matches, list truncated.", limit);
817
818 count++;
819
820 if (!(flags & GLIST_COUNT) && count < limit) {
821 snprintf(expirestr, sizeof(expirestr), "%s", glinetostring(gl));
822 snprintf(idstr, sizeof(idstr), "%d", gl->glinebufid);
823 controlreply(sender, "%s%-49s %-19s %-15s %-25s %s",
824 (gl->flags & GLINE_ACTIVE) ? "+" : "-",
825 expirestr,
826 (gl->flags & GLINE_ACTIVE) ? (char*)longtoduration(gl->expire - curtime, 0) : "<inactive>",
827 gl->glinebufid ? idstr : "",
828 gl->creator ? gl->creator->content : "",
829 gl->reason ? gl->reason->content : "");
830 }
831 }
832
833 controlreply(sender, "%s%d G-Line%s found.", (flags & GLIST_COUNT) ? "" : "End of list - ", count, count == 1 ? "" : "s");
834
835 return CMD_OK;
836 }
837
838 static int glines_cmdglinelog(void *source, int cargc, char **cargv) {
839 nick *sender = source;
840 glinebuf *gbl;
841 gline *gl;
842 int i, id, count;
843 char timebuf[30];
844
845 id = 0;
846
847 if (cargc > 0) {
848 id = atoi(cargv[0]);
849
850 if (id == 0) {
851 controlreply(sender, "Invalid log ID.");
852 return CMD_ERROR;
853 }
854 }
855
856 controlreply(sender, "Time ID G-Lines User Hits Channel Hits Comment");
857
858 for (i = 0; i < MAXGLINELOG; i++) {
859 gbl = glinebuflog[(i + glinebuflogoffset + 1) % MAXGLINELOG];
860
861 if (!gbl)
862 continue;
863
864 if (id == 0 || gbl->id == id) {
865 count = 0;
866
867 for (gl = gbl->glines; gl; gl = gl->next)
868 count++;
869
870 strftime(timebuf, sizeof(timebuf), "%d/%m/%y %H:%M:%S", localtime((gbl->amend) ? &gbl->amend : &gbl->commit));
871 strncat(timebuf, (gbl->amend) ? "*" : " ", sizeof(timebuf));
872 controlreply(sender, "%-20s %-10d %-10d %-15d %-15d %s", timebuf, gbl->id, count, gbl->userhits, gbl->channelhits, gbl->comment ? gbl->comment->content : "(no comment)");
873 }
874
875 if (id != 0 && gbl->id == id) {
876 glinebufspew(gbl, sender);
877 controlreply(sender, "Done.");
878 return CMD_OK;
879 }
880 }
881
882 if (id == 0) {
883 controlreply(sender, "Done.");
884 } else {
885 controlreply(sender, "Log entry for ID %d not found.", id);
886 }
887
888 return CMD_OK;
889 }
890
891 static int glines_cmdglineundo(void *source, int cargc, char **cargv) {
892 nick *sender = source;
893 int id;
894
895 if (cargc < 1)
896 return CMD_USAGE;
897
898 id = atoi(cargv[0]);
899
900 if (id == 0 || !glinebufundo(id)) {
901 controlreply(sender, "Invalid log ID.");
902 return CMD_ERROR;
903 }
904
905 controlreply(sender, "Done.");
906
907 return CMD_OK;
908 }
909
910 static int glines_cmdsyncglines(void *source, int cargc, char **cargv) {
911 nick *sender = source;
912 gline *gl;
913 int count;
914
915 count = 0;
916
917 for (gl = glinelist; gl; gl = gl->next) {
918 gline_propagate(gl);
919 count++;
920 }
921
922 controlwall(NO_OPER, NL_GLINES, "%s SYNCGLINE'd %d G-Lines.",
923 controlid(sender), count);
924
925 controlreply(sender, "Done.");
926
927 return CMD_OK;
928 }
929
930 static int glines_cmdcleanupglines(void *source, int cargc, char **cargv) {
931 nick *sender = source;
932 gline **pnext, *gl;
933 int count;
934 time_t now;
935
936 count = 0;
937 time(&now);
938
939 for (pnext = &glinelist; *pnext;) {
940 gl = *pnext;
941
942 /* Remove inactivate glines that have been last changed more than a week ago */
943 if (!(gl->flags & GLINE_ACTIVE) && gl->lastmod < now - 7 * 24 * 60 * 60) {
944 gline_destroy(gl, 0, 1);
945 count++;
946 } else {
947 pnext = &((*pnext)->next);
948 }
949
950 if (!*pnext)
951 break;
952 }
953
954 controlwall(NO_OPER, NL_GLINES, "%s CLEANUPGLINES'd %d G-Lines.",
955 controlid(sender), count);
956
957 controlreply(sender, "Done.");
958
959 return CMD_OK;
960 }
961
962 static int commandsregistered;
963
964 static void registercommands(int hooknum, void *arg) {
965 if (commandsregistered)
966 return;
967 commandsregistered = 1;
968
969 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");
970 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");
971 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");
972 registercontrolhelpcmd("ungline", NO_OPER, 1, glines_cmdungline, "Usage: ungline <mask>\nDeactivates a gline.");
973 registercontrolhelpcmd("destroygline", NO_DEVELOPER, 1, glines_cmddestroygline, "Usage: destroygline <mask>\nDestroys a gline.");
974 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");
975 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");
976 registercontrolhelpcmd("trustungline", NO_OPER, 2, glines_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nRemoves a gline that was previously set with trustgline.");
977 registercontrolhelpcmd("glstats", NO_OPER, 0, glines_cmdglstats, "Usage: glstat\nShows statistics about G-Lines.");
978 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.");
979 registercontrolhelpcmd("glinelog", NO_OPER, 1, glines_cmdglinelog, "Usage: glinelog ?id?\nShows information about previous gline transactions.");
980 registercontrolhelpcmd("glineundo", NO_OPER, 1, glines_cmdglineundo, "Usage: glineundo ?id?\nUndoes a gline transaction.");
981 registercontrolhelpcmd("syncglines", NO_DEVELOPER, 0, glines_cmdsyncglines, "Usage: syncglines\nSends all G-Lines to all other servers.");
982 registercontrolhelpcmd("cleanupglines", NO_DEVELOPER, 0, glines_cmdcleanupglines, "Usage: cleanupglines\nDestroys all deactivated G-Lines.");
983 }
984
985 static void deregistercommands(int hooknum, void *arg) {
986 if (!commandsregistered)
987 return;
988 commandsregistered = 0;
989
990 deregistercontrolcmd("block", glines_cmdblock);
991 deregistercontrolcmd("gline", glines_cmdgline);
992 deregistercontrolcmd("smartgline", glines_cmdsmartgline);
993 deregistercontrolcmd("ungline", glines_cmdungline);
994 deregistercontrolcmd("destroygline", glines_cmddestroygline);
995 deregistercontrolcmd("clearchan", glines_cmdclearchan);
996 deregistercontrolcmd("trustgline", glines_cmdtrustgline);
997 deregistercontrolcmd("trustungline", glines_cmdtrustungline);
998 deregistercontrolcmd("glstats", glines_cmdglstats);
999 deregistercontrolcmd("glist", glines_cmdglist);
1000 deregistercontrolcmd("glinelog", glines_cmdglinelog);
1001 deregistercontrolcmd("glineundo", glines_cmdglineundo);
1002 deregistercontrolcmd("syncglines", glines_cmdsyncglines);
1003 deregistercontrolcmd("cleanupglines", glines_cmdcleanupglines);
1004 }
1005
1006 void _init(void) {
1007 registerhook(HOOK_TRUSTS_DB_LOADED, registercommands);
1008 registerhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
1009
1010 if (trustsdbloaded)
1011 registercommands(0, NULL);
1012 }
1013
1014 void _fini(void) {
1015 deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
1016 deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
1017
1018 deregistercommands(0, NULL);
1019 }