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