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