]> jfr.im git - irc/quakenet/newserv.git/blob - chanfix/chanfix.c
SPLITLIST/SERVERLIST: Add logic to determine the type of server.
[irc/quakenet/newserv.git] / chanfix / chanfix.c
1 /* shroud's chanfix */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <time.h>
8 #include <sys/time.h>
9 #include "chanfix.h"
10 #include "../splitlist/splitlist.h"
11 #include "../localuser/localuserchannel.h"
12 #include "../core/schedule.h"
13 #include "../core/error.h"
14 #include "../nick/nick.h"
15 #include "../lib/irc_string.h"
16 #include "../control/control.h"
17 #include "../lib/version.h"
18
19 MODULE_VERSION("")
20
21 /* control's nick */
22 extern nick *mynick;
23
24 int cfext;
25 int cfnext;
26 int cffailedinit;
27
28 /* user accessible commands */
29 int cfcmd_debug(void *source, int cargc, char **cargv);
30 int cfcmd_debughistogram(void *source, int cargc, char **cargv);
31 int cfcmd_debugsample(void *source, int cargc, char **cargv);
32 int cfcmd_debugexpire(void *source, int cargc, char **cargv);
33 int cfcmd_chanopstat(void *source, int cargc, char **cargv);
34 int cfcmd_chanoplist(void *source, int cargc, char **cargv);
35 int cfcmd_chanfix(void *source, int cargc, char **cargv);
36 int cfcmd_showregs(void *source, int cargc, char **cargv);
37 int cfcmd_requestop(void *source, int cargc, char **cargv);
38 int cfcmd_save(void *source, int cargc, char **cargv);
39 int cfcmd_load(void *source, int cargc, char **cargv);
40
41 /* scheduled events */
42 void cfsched_dosample(void *arg);
43 void cfsched_doexpire(void *arg);
44 void cfsched_dosave(void *arg);
45
46 /* hooks */
47 void cfhook_autofix(int hook, void *arg);
48 void cfhook_statsreport(int hook, void *arg);
49 void cfhook_auth(int hook, void *arg);
50
51 /* helper functions */
52 regop *cf_createregop(nick *np, chanindex *cip);
53 void cf_deleteregop(chanindex *cip, regop *ro);
54 unsigned long cf_gethash(nick *np, int type);
55
56 int cf_storechanfix(void);
57 int cf_loadchanfix(void);
58 void cf_free(void);
59
60 #define min(a,b) ((a > b) ? b : a)
61
62 void _init() {
63 cfext = registerchanext("chanfix");
64 cfnext = registernickext("chanfix");
65
66 if (cfext < 0 || cfnext < 0) {
67 Error("chanfix", ERR_ERROR, "Couldn't register channel and/or nick extension");
68 cffailedinit = 1;
69 } else {
70 schedulerecurring(time(NULL), 0, CFSAMPLEINTERVAL, &cfsched_dosample, NULL);
71 schedulerecurring(time(NULL), 0, CFEXPIREINTERVAL, &cfsched_doexpire, NULL);
72 schedulerecurring(time(NULL), 0, CFAUTOSAVEINTERVAL, &cfsched_dosave, NULL);
73
74 registercontrolhelpcmd("cfdebug", NO_DEVELOPER, 1, &cfcmd_debug, "Display Debug Information on chanfix data for channel");
75 registercontrolhelpcmd("cfhistogram", NO_DEVELOPER, 1, &cfcmd_debughistogram, "Display Debug Histogram of chanfix data for channel");
76 #if CFDEBUG
77 registercontrolhelpcmd("cfsample", NO_DEVELOPER, 1, &cfcmd_debugsample, "DEBUG Command - must not be loaded on live instances");
78 registercontrolhelpcmd("cfexpire", NO_DEVELOPER, 1, &cfcmd_debugexpire, "DEBUG Command - must not be loaded on live instances");
79 #endif
80 registercontrolhelpcmd("chanopstat", NO_OPER, 1, &cfcmd_chanopstat, "Shows chanop statistics for a given channel");
81 registercontrolhelpcmd("chanoplist", NO_OPER, 1, &cfcmd_chanoplist, "Shows lists of known chanops, including scores");
82
83 registercontrolhelpcmd("chanfix", NO_OPER, 1, &cfcmd_chanfix, "Perform a chanfix on a channel to op known users only");
84 registercontrolhelpcmd("showregs", NO_OPER, 1, &cfcmd_showregs, "Show regular ops known on a channel (including services)");
85 #if CFDEBUG
86 /* should we disable this in the 'final' build? */
87 /* registercontrolcmd("requestop", 0, 2, &cfcmd_requestop); */
88 #endif
89 registercontrolhelpcmd("cfsave", NO_DEVELOPER, 0, &cfcmd_save, "Force save of chanfix data");
90 registercontrolhelpcmd("cfload", NO_DEVELOPER, 0, &cfcmd_load, "Force load of chanfix data");
91
92 #if CFAUTOFIX
93 registerhook(HOOK_CHANNEL_DEOPPED, &cfhook_autofix);
94 registerhook(HOOK_CHANNEL_PART, &cfhook_autofix);
95 registerhook(HOOK_CHANNEL_KICK, &cfhook_autofix);
96 registerhook(HOOK_CHANNEL_JOIN, &cfhook_autofix);
97 #endif
98
99 registerhook(HOOK_CORE_STATSREQUEST, &cfhook_statsreport);
100 registerhook(HOOK_NICK_ACCOUNT, &cfhook_auth);
101
102 cf_loadchanfix();
103
104 cffailedinit = 0;
105 }
106 }
107
108 void _fini() {
109 if (cffailedinit == 0) {
110 deleteschedule(NULL, &cfsched_dosample, NULL);
111 deleteschedule(NULL, &cfsched_doexpire, NULL);
112 deleteschedule(NULL, &cfsched_dosave, NULL);
113
114 cf_storechanfix();
115
116 cf_free();
117
118 deregistercontrolcmd("cfdebug", &cfcmd_debug);
119 deregistercontrolcmd("cfhistogram", &cfcmd_debughistogram);
120 #if CFDEBUG
121 deregistercontrolcmd("cfsample", &cfcmd_debugsample);
122 deregistercontrolcmd("cfexpire", &cfcmd_debugexpire);
123 #endif
124 deregistercontrolcmd("chanopstat", &cfcmd_chanopstat);
125 deregistercontrolcmd("chanoplist", &cfcmd_chanoplist);
126 deregistercontrolcmd("chanfix", &cfcmd_chanfix);
127 deregistercontrolcmd("showregs", &cfcmd_showregs);
128 #if CFDEBUG
129 // deregistercontrolcmd("requestop", &cfcmd_requestop);
130 #endif
131 deregistercontrolcmd("cfsave", &cfcmd_save);
132 deregistercontrolcmd("cfload", &cfcmd_load);
133
134 #if CFAUTOFIX
135 deregisterhook(HOOK_CHANNEL_DEOPPED, &cfhook_autofix);
136 deregisterhook(HOOK_CHANNEL_PART, &cfhook_autofix);
137 deregisterhook(HOOK_CHANNEL_KICK, &cfhook_autofix);
138 deregisterhook(HOOK_CHANNEL_JOIN, &cfhook_autofix);
139 #endif
140
141 deregisterhook(HOOK_CORE_STATSREQUEST, &cfhook_statsreport);
142 deregisterhook(HOOK_NICK_ACCOUNT, &cfhook_auth);
143 }
144
145 if (cfext >= 0) {
146 releasechanext(cfext);
147 }
148
149 if (cfnext >= 0) {
150 releasenickext(cfnext);
151 }
152 }
153
154 int cfcmd_debug(void *source, int cargc, char **cargv) {
155 nick *np = (nick*)source;
156 chanindex *cip;
157 chanfix *cf;
158 regop *ro;
159 int i;
160
161 if (cargc < 1) {
162 controlreply(np, "Syntax: cfdebug <#channel>");
163
164 return CMD_ERROR;
165 }
166
167 cip = findchanindex(cargv[0]);
168
169 if (cip == NULL) {
170 controlreply(np, "No such channel.");
171
172 return CMD_ERROR;
173 } else
174 controlreply(np, "Found channel %s. Retrieving chanfix information...", cargv[0]);
175
176 cf = cip->exts[cfext];
177
178 if (cf == NULL) {
179 controlreply(np, "No chanfix information for %s", cargv[0]);
180
181 return CMD_ERROR;
182 } else
183 controlreply(np, "Found chanfix information. Dumping...");
184
185 for (i=0;i<cf->regops.cursi;i++) {
186 ro = ((regop**)cf->regops.content)[i];
187
188 controlreply(np, "%d. type: %s hash: 0x%lx lastopped: %lu uh: %s score: %d",
189 i + 1, ro->type == CFACCOUNT ? "CFACCOUNT" : "CFHOST", ro->hash,
190 ro->lastopped, ro->uh ? ro->uh->content : "(unknown)", ro->score);
191 }
192
193 controlreply(np, "Done.");
194
195 return CMD_OK;
196 }
197
198 int cfcmd_debughistogram(void *source, int cargc, char **cargv) {
199 nick *np = (nick*)source;
200 int i,a,score;
201 chanindex* cip;
202 chanfix *cf;
203 int histogram[10001]; /* 625 (lines) * 16 (columns/line) + 1 (for histogram[0]) */
204
205 for (i = 0; i < 10001; i++)
206 histogram[i] = 0;
207
208 for (i=0; i<CHANNELHASHSIZE; i++) {
209 for (cip=chantable[i]; cip; cip=cip->next) {
210 if ((cf = cip->exts[cfext]) != NULL) {
211 for (a=0;a<cf->regops.cursi;a++) {
212 score = ((regop**)cf->regops.content)[a]->score;
213
214 if (score < 10001)
215 histogram[score]++;
216 }
217 }
218 }
219 }
220
221 controlreply(np, "--- Histogram of chanfix scores");
222
223 for (i = 1; i < 10001; i += 16) {
224 controlreply(np, "%6d: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
225 i, histogram[i], histogram[i+1], histogram[i+2], histogram[i+3], histogram[i+4],
226 histogram[i+5], histogram[i+6], histogram[i+7], histogram[i+8], histogram[i+9],
227 histogram[i+10], histogram[i+11], histogram[i+12], histogram[i+13], histogram[i+14],
228 histogram[i+15]);
229 }
230
231 controlreply(np, "--- End of histogram");
232
233 return CMD_OK;
234 }
235
236 int cfcmd_debugsample(void *source, int cargc, char **cargv) {
237 cfsched_dosample(NULL);
238
239 controlreply((nick*)source, "Done.");
240
241 return CMD_OK;
242 }
243
244 int cfcmd_debugexpire(void *source, int cargc, char **cargv) {
245 cfsched_doexpire(NULL);
246
247 controlreply((nick*)source, "Done.");
248
249 return CMD_OK;
250 }
251
252 /* used for qsorting int arrays */
253 int cmpint(const void *a, const void *b) {
254 int p = *(int*)a;
255 int q = *(int*)b;
256
257 if (p > q)
258 return -1;
259 else if (p < q)
260 return 1;
261 else
262 return 0;
263 }
264
265 /* used for qsorting regop* arrays */
266 int cmpregop(const void *a, const void *b) {
267 regop *p = *(regop**)a;
268 regop *q = *(regop**)b;
269
270 if (p->score > q->score)
271 return -1;
272 else if (p->score < q->score)
273 return 1;
274 else
275 return 0;
276 }
277
278 int cf_getsortedregops(chanfix *cf, int max, regop **list) {
279 int i;
280
281 if (cf == NULL)
282 return 0;
283
284 qsort(cf->regops.content, cf->regops.cursi, sizeof(regop*), cmpregop);
285
286 for (i = 0; i < min(max, cf->regops.cursi); i++) {
287 list[i] = ((regop**)cf->regops.content)[i];
288 }
289
290 return i;
291 }
292
293 int cfcmd_chanopstat(void *source, int cargc, char **cargv) {
294 nick *np = (nick*)source;
295 nick *np2;
296 channel *cp;
297 chanfix *cf;
298 regop *ro;
299 regop *rolist[10];
300 int i, a, count;
301 int *scores;
302
303 if (cargc < 1) {
304 controlreply(np, "Syntax: chanopstat <#channel>");
305
306 return CMD_ERROR;
307 }
308
309 cp = findchannel(cargv[0]);
310
311 if (cp == NULL) {
312 controlreply(np, "No such channel.");
313
314 return CMD_ERROR;
315 }
316
317 cf = cp->index->exts[cfext];
318
319 if (cf == NULL) {
320 controlreply(np, "No chanfix information for %s", cargv[0]);
321
322 return CMD_ERROR;
323 }
324
325 /* known ops */
326 count = cf_getsortedregops(cf, 10, rolist);
327 controlreply(np, "Scores of \"best ops\" on %s are:", cargv[0]);
328
329 for (i=0;i<count;i++) {
330 controlreply(np, " %d", rolist[i]->score);
331 }
332
333 /* current ops */
334 scores = (int*)malloc(sizeof(int) * cp->users->hashsize);
335
336 i = 0;
337
338 for (a=0;a<cp->users->hashsize;a++) {
339 if ((cp->users->content[a] != nouser) && (cp->users->content[a] & CUMODE_OP)) {
340 np2 = getnickbynumeric(cp->users->content[a]);
341
342 ro = cf_findregop(np2, cp->index, CFACCOUNT | CFHOST);
343
344 if (ro)
345 scores[i++] = ro->score;
346 }
347 }
348
349 qsort(scores, i, sizeof(int), &cmpint);
350 controlreply(np, "Scores of current ops on %s are:", cargv[0]);
351
352 for (a=0;a<min(i,20);a++) {
353 if (scores[a] == 0)
354 break;
355
356 controlreply(np, " %d", scores[a]);
357 }
358
359 free(scores);
360 controlreply(np, "Done.");
361
362 return CMD_OK;
363 }
364
365 int cfcmd_chanoplist(void *source, int cargc, char **cargv) {
366 nick *np = (nick*)source;
367 nick *np2;
368 chanindex *cip;
369 chanfix *cf;
370 regop *rolist[50];
371 int i,count;
372 time_t ct;
373 struct tm *tm;
374 char date[50];
375 unsigned long *hand;
376
377 if (cargc < 1) {
378 controlreply(np, "Syntax: chanoplist <#channel>");
379
380 return CMD_ERROR;
381 }
382
383 cip = findchanindex(cargv[0]);
384
385 if (cip == NULL) {
386 controlreply(np, "No such channel.");
387
388 return CMD_ERROR;
389 }
390
391 cf = cip->exts[cfext];
392
393 if (cf == NULL) {
394 controlreply(np, "No chanfix information for %s", cargv[0]);
395
396 return CMD_ERROR;
397 }
398
399 count = cf_getsortedregops(cf, 50, rolist);
400
401 controlreply(np, "Pos Score Type User/Last seen");
402
403 for (i=0;i<count;i++) {
404 np2 = cf_findnick(rolist[i], cip);
405
406 if (np2 != NULL) {
407 hand = getnumerichandlefromchanhash(cip->channel->users, np2->numeric);
408
409 /* hand should be non-null in all cases */
410 if (hand != NULL)
411 controlreply(np, "%3d %5d %-7s %1s%-16s %s@%s (%s)", i + 1, rolist[i]->score,
412 (rolist[i]->type == CFACCOUNT) ? "account" : "host",
413 (*hand & CUMODE_OP) ? "@" : "", np2->nick, np2->ident,
414 np2->host->name->content, np2->realname->name->content);
415 } else {
416 ct = rolist[i]->lastopped;
417 tm = gmtime(&ct);
418 strftime(date,sizeof(date),"%d-%m-%Y %H:%M:%S",tm);
419 controlreply(np, "%3d %5d %-7s %1s%-16s %s Last seen: %s", i + 1, rolist[i]->score,
420 (rolist[i]->type == CFACCOUNT) ? "account" : "host", "", "!NONE!",
421 rolist[i]->uh ? rolist[i]->uh->content : "!UNKNOWN!", date);
422 }
423 }
424
425 controlreply(np, "Done.");
426
427 return CMD_OK;
428 }
429
430 int cfcmd_chanfix(void *source, int cargc, char **cargv) {
431 nick *np = (nick*)source;
432 channel *cp;
433 int ret;
434
435 if (cargc < 1) {
436 controlreply(np, "Syntax: chanfix <#channel>");
437
438 return CMD_ERROR;
439 }
440
441 cp = findchannel(cargv[0]);
442
443 if (cp == NULL) {
444 controlreply(np, "No such channel.");
445
446 return CMD_ERROR;
447 }
448
449 if (sp_countsplitservers(SERVERTYPEFLAG_USER_STATE) > 0) {
450 controlreply(np, "Chanfix cannot be used during a netsplit.");
451
452 return CMD_ERROR;
453 }
454
455 ret = cf_fixchannel(cp);
456
457 switch (ret) {
458 case CFX_FIXED:
459 controlreply(np, "Channel was fixed.");
460 break;
461 case CFX_NOCHANFIX:
462 controlreply(np, "Channel cannot be fixed: no chanfix information");
463 break;
464 case CFX_FIXEDFEWOPS:
465 controlreply(np, "Channel was fixed but only a few ops could be found and reopped.");
466 break;
467 default:
468 /* oops */
469 break;
470 }
471
472 return CMD_OK;
473 }
474
475 int cfcmd_showregs(void *source, int cargc, char **cargv) {
476 nick *np = (nick*)source;
477 nick *np2;
478 chanfix *cf;
479 channel *cp;
480 int i, count, ops;
481 regop *rolist[50];
482
483 if (cargc < 1) {
484 controlreply(np, "Syntax: showregs <#channel>");
485
486 return CMD_ERROR;
487 }
488
489 cp = findchannel(cargv[0]);
490
491 if (cp == NULL) {
492 controlreply(np, "No such channel.");
493
494 return CMD_ERROR;
495 }
496
497 cf = cp->index->exts[cfext];
498
499 if (cf == NULL) {
500 controlreply(np, "No chanfix information for %s", cargv[0]);
501
502 return CMD_ERROR;
503 }
504
505 count = 0;
506
507 for(i=0;i<cp->users->hashsize;i++) {
508 if(cp->users->content[i] != nouser) {
509 np2 = getnickbynumeric(cp->users->content[i]);
510
511 if (IsService(np2)) {
512 controlreply(np, "%s (service)", np2->nick);
513 count++;
514 }
515 }
516 }
517
518 /* now get a sorted list of regops */
519 ops = cf_getsortedregops(cf, 50, rolist);
520
521 /* and show some of them */
522 for (i=0;i<ops;i++) {
523 if (count >= CFMAXOPS || rolist[i]->score < rolist[0]->score / 2 || rolist[i]->score < CFMINSCORE)
524 break;
525
526 np2 = cf_findnick(rolist[i], cp->index);
527
528 if (np2) {
529 controlreply(np, "%s (%s)", np2->nick, rolist[i]->type == CFACCOUNT ? "account" : "host");
530
531 count++;
532 }
533 }
534
535 controlreply((nick*)source, "--- End of list: %d users listed", count);
536
537 return CMD_OK;
538 }
539
540 int cfcmd_requestop(void *source, int cargc, char **cargv) {
541 nick *np = (nick*)source;
542 nick *user = np;
543 channel *cp;
544 int ret, a;
545 unsigned long *hand;
546 modechanges changes;
547
548 if (cargc < 1) {
549 controlreply(np, "Syntax: requestop <#channel> [nick]");
550
551 return CMD_ERROR;
552 }
553
554 cp = findchannel(cargv[0]);
555
556 if (cp == NULL) {
557 controlreply(np, "No such channel.");
558
559 return CMD_ERROR;
560 }
561
562 if (cargc > 1) {
563 user = getnickbynick(cargv[1]);
564
565 if (!user) {
566 controlreply(np, "No such user.");
567
568 return CMD_ERROR;
569 }
570 }
571
572 hand = getnumerichandlefromchanhash(cp->users, user->numeric);
573
574 if (hand == NULL) {
575 controlreply(np, "User %s is not on channel %s.", user->nick, cargv[0]);
576
577 return CMD_ERROR;
578 }
579
580 for (a=0;a<cp->users->hashsize;a++) {
581 if ((cp->users->content[a] != nouser) && (cp->users->content[a] & CUMODE_OP)) {
582 controlreply(np, "There are ops on channel %s. This command can only be"
583 " used if there are no ops.", cargv[0]);
584
585 return CMD_ERROR;
586 }
587 }
588
589 if (sp_countsplitservers(SERVERTYPEFLAG_USER_STATE) > 0) {
590 controlreply(np, "One or more servers are currently split. Wait until the"
591 " netsplit is over and try again.");
592
593 return CMD_ERROR;
594 }
595
596 if (cf_wouldreop(user, cp)) {
597 localsetmodeinit(&changes, cp, mynick);
598 localdosetmode_nick(&changes, user, MC_OP);
599 localsetmodeflush(&changes, 1);
600
601 controlreply(np, "Chanfix opped you on the specified channel.");
602 } else {
603 ret = cf_fixchannel(cp);
604
605 if (ret == CFX_NOUSERSAVAILABLE)
606 controlreply(np, "Chanfix knows regular ops for that channel. They will"
607 " be opped when they return.");
608 else
609 controlreply(np, "Chanfix has opped known ops for that channel.");
610 }
611
612 return CMD_OK;
613 }
614
615 int cfcmd_save(void *source, int cargc, char **cargv) {
616 nick *np = (nick*)source;
617 int count;
618
619 count = cf_storechanfix();
620
621 controlreply(np, "%d chanfix records saved.", count);
622
623 return CMD_OK;
624 }
625
626 void cf_free(void) {
627 int a, i;
628 chanfix *cf;
629 chanindex *cip;
630
631 /* free old stuff */
632 for (i=0; i<CHANNELHASHSIZE; i++) {
633 for (cip=chantable[i]; cip; cip=cip->next) {
634 if ((cf = cip->exts[cfext]) != NULL) {
635 for (a=0;a<cf->regops.cursi;a++) {
636 freesstring(((regop**)cf->regops.content)[a]->uh);
637 free(((regop**)cf->regops.content)[a]);
638 }
639
640 array_free(&(((chanfix*)cip->exts[cfext])->regops));
641 }
642
643 free(cip->exts[cfext]);
644 cip->exts[cfext] = NULL;
645 }
646 }
647 }
648
649 int cfcmd_load(void *source, int cargc, char **cargv) {
650 nick *np = (nick*)source;
651 int count;
652
653 count = cf_loadchanfix();
654
655 controlreply(np, "%d chanfix records loaded.", count);
656
657 return CMD_OK;
658 }
659
660 int cf_hasauthedcloneonchan(nick *np, channel *cp) {
661 nick *jp;
662 unsigned long *hand;
663
664 for (jp = np->host->nicks; jp; jp = jp->nextbyhost) {
665 if (!IsAccount(jp) || jp->numeric == np->numeric)
666 continue;
667
668 hand = getnumerichandlefromchanhash(cp->users, jp->numeric);
669
670 if (hand && (*hand & CUMODE_OP) && strcmp(np->ident, jp->ident) == 0)
671 return 1;
672 }
673
674 return 0;
675 }
676
677 void cfsched_dosample(void *arg) {
678 int i,a,now,cfscore,cfnewro,cfuhost,diff;
679 channel *cp;
680 chanindex *cip;
681 nick *np;
682 chanfix *cf;
683 regop *ro, *roh;
684 struct timeval start;
685 struct timeval end;
686 char buf[USERLEN+1+HOSTLEN+1];
687
688 now = getnettime();
689
690 cfuhost = cfscore = cfnewro = 0;
691
692 if (sp_countsplitservers(SERVERTYPEFLAG_USER_STATE) > CFMAXSPLITSERVERS)
693 return;
694
695 gettimeofday(&start, NULL);
696
697 for (i=0; i<CHANNELHASHSIZE; i++) {
698 for (cip=chantable[i]; cip; cip=cip->next) {
699 cf = (chanfix*)cip->exts[cfext];
700 cp = cip->channel;
701
702 if (!cp || cp->users->totalusers < CFMINUSERS)
703 continue;
704
705 for (a=0;a<cp->users->hashsize;a++) {
706 if ((cp->users->content[a] != nouser) && (cp->users->content[a] & CUMODE_OP)) {
707 np = getnickbynumeric(cp->users->content[a]);
708
709 #if !CFDEBUG
710 if (IsService(np))
711 continue;
712 #endif
713
714 roh = ro = cf_findregop(np, cip, CFACCOUNT | CFHOST);
715
716 if ((ro == NULL || (IsAccount(np) && ro->type == CFHOST)) &&
717 !cf_hasauthedcloneonchan(np, cp)) {
718 ro = cf_createregop(np, cip);
719 cfnewro++;
720 }
721
722 /* lastopped == now if the user has clones, we obviously
723 * don't want to give them points in this case */
724 if (ro && ro->lastopped != now) {
725 if (ro->type != CFHOST || !cf_hasauthedcloneonchan(np, cp)) {
726 ro->score++;
727 cfscore++;
728 }
729
730 /* merge any matching CFHOST records */
731 if (roh && roh->type == CFHOST && ro->type == CFACCOUNT) {
732 /* hmm */
733 ro->score += roh->score;
734
735 cf_deleteregop(cip, roh);
736 }
737
738 /* store the user's account/host if we have to */
739 if (ro->uh == NULL && ro->score >= CFMINSCOREUH) {
740 if (ro->type == CFACCOUNT)
741 ro->uh = getsstring(np->authname, ACCOUNTLEN);
742 else {
743 snprintf(buf, sizeof(buf), "%s@%s", np->ident, np->host->name->content);
744 roh->uh = getsstring(buf, USERLEN+1+HOSTLEN);
745 }
746
747 cfuhost++;
748 }
749
750 ro->lastopped = now;
751 }
752 }
753 }
754 }
755 }
756
757 cp = findchannel("#qnet.chanfix");
758
759 if (cp) {
760 gettimeofday(&end, NULL);
761
762 diff = (end.tv_sec * 1000 + end.tv_usec / 1000) -
763 (start.tv_sec * 1000 + start.tv_usec / 1000);
764
765 sendmessagetochannel(mynick, cp, "sampled chanfix scores, assigned %d new"
766 " points, %d new regops, %d user@hosts added, deltaT: %dms", cfscore,
767 cfnewro, cfuhost, diff);
768 }
769 }
770
771 void cfsched_doexpire(void *arg) {
772 channel *cp;
773 chanindex *cip;
774 chanindex *ncip;
775 chanfix *cf;
776 int i,a,cfscore,cfregop,cffreeuh,diff;
777 regop **rolist;
778 regop *ro;
779 struct timeval start;
780 struct timeval end;
781 time_t currenttime;
782
783 cffreeuh = cfscore = cfregop = 0;
784
785 gettimeofday(&start, NULL);
786 currenttime=getnettime();
787
788 for (i=0; i<CHANNELHASHSIZE; i++) {
789 for (cip=chantable[i]; cip; cip=cip->next) {
790 cf = (chanfix*)cip->exts[cfext];
791
792 if (cf) {
793 rolist = (regop**)cf->regops.content;
794
795 for (a=0;a<cf->regops.cursi;a++) {
796 ro = rolist[a];
797
798 if (((currenttime - ro->lastopped) > (2 * CFSAMPLEINTERVAL)) && ro->score) {
799 ro->score--;
800 cfscore++;
801 }
802
803 if ((ro->score < CFMINSCOREUH) && ro->uh) {
804 freesstring(ro->uh);
805 ro->uh = NULL;
806
807 cffreeuh++;
808 }
809
810 if (ro->score == 0 || ro->lastopped < (currenttime - CFREMEMBEROPS)) {
811 cf_deleteregop(cip, ro);
812 cfregop++;
813 }
814 }
815 }
816 }
817 }
818
819 /* stolen from channel/channelindex.c */
820 for (i=0;i<CHANNELHASHSIZE;i++) {
821 for (cip=chantable[i];cip;cip=ncip) {
822 /* CAREFUL: deleting items from chains you're walking is bad */
823 ncip=cip->next;
824
825 /* try to delete it if there's no chanfix* pointer */
826 if (cip->exts[cfext] == NULL)
827 releasechanindex(cip);
828 }
829 }
830
831 cp = findchannel("#qnet.chanfix");
832
833 if (cp) {
834 gettimeofday(&end, NULL);
835
836 diff = (end.tv_sec * 1000 + end.tv_usec / 1000) -
837 (start.tv_sec * 1000 + start.tv_usec / 1000);
838
839 sendmessagetochannel(mynick, cp, "expired chanfix scores, purged %d points,"
840 " scrapped %6d regops, %d user@hosts freed, deltaT: %dms", cfscore,
841 cfregop, cffreeuh, diff);
842 }
843
844 }
845
846 void cfsched_dosave(void *arg) {
847 cf_storechanfix();
848 }
849
850 #if CFAUTOFIX
851 void cfhook_autofix(int hook, void *arg) {
852 int a, count;
853 void **args = (void**)arg;
854 channel *cp;
855
856 if (hook == HOOK_CHANNEL_DEOPPED || hook == HOOK_CHANNEL_PART ||
857 hook == HOOK_CHANNEL_KICK || hook == HOOK_CHANNEL_JOIN) {
858 cp = args[0];
859
860 if (sp_countsplitservers(SERVERTYPEFLAG_USER_STATE) > 0)
861 return;
862
863 for(a=0;a<cp->users->hashsize;a++) {
864 if (cp->users->content[a] != nouser) {
865 count++;
866
867 if (cp->users->content[a] & CUMODE_OP)
868 return;
869 }
870 }
871
872 /* don't fix small channels.. it's inaccurate and
873 * they could just cycle the channel */
874 if (count < 4)
875 return;
876
877 cf_fixchannel((channel*)args[0]);
878 }
879 }
880 #endif
881
882 void cfhook_statsreport(int hook, void *arg) {
883 char buf[300];
884 int i,a,rc,mc,memory;
885 chanindex *cip;
886 chanfix *cf;
887
888 if ((long)arg > 2) {
889 memory = rc = mc = 0;
890
891 for (i=0; i<CHANNELHASHSIZE; i++) {
892 for (cip=chantable[i]; cip; cip=cip->next) {
893 if ((cf = cip->exts[cfext]) != NULL) {
894 for (a=0;a<cf->regops.cursi;a++) {
895 memory += sizeof(regop) + sizeof(regop*);
896
897 if (((regop**)cf->regops.content)[a]->uh)
898 memory += sizeof(sstring) + strlen(((regop**)cf->regops.content)[a]->uh->content) + 1;
899
900 rc++;
901 }
902
903 memory += sizeof(chanfix);
904
905 mc++;
906 }
907 }
908 }
909
910 snprintf(buf, sizeof(buf), "Chanfix : %6d registered ops, %9d monitored channels. %9d"
911 " kbytes of memory used", rc, mc, (memory / 1024));
912 triggerhook(HOOK_CORE_STATSREPLY, buf);
913 }
914 }
915
916 void cfhook_auth(int hook, void *arg) {
917 nick *np = (nick*)arg;
918
919 /* Invalidate the user's hash */
920 np->exts[cfnext] = NULL;
921
922 /* Calculate the new hash */
923 cf_gethash(np, CFACCOUNT);
924 }
925
926 /* Returns the hash of a specific user (np), type can be either CFACCOUNT,
927 CFHOST or both (binary or'd values). cf_gethash will also cache the user's
928 hash in a nick extension */
929 unsigned long cf_gethash(nick *np, int type) {
930 char buf[USERLEN+1+HOSTLEN+1];
931 unsigned long hash;
932
933 if (IsAccount(np) && (type & CFACCOUNT)) {
934 if (np->exts[cfnext] == NULL) {
935 np->exts[cfnext] = (void *)crc32(np->authname);
936 }
937
938 return (unsigned long)np->exts[cfnext];
939 } else if (type == CFACCOUNT)
940 return 0; /* this should not happen */
941
942 if (type & CFHOST) {
943 if (!IsAccount(np) && np->exts[cfnext])
944 return (unsigned long)np->exts[cfnext];
945 else {
946 snprintf(buf, sizeof(buf), "%s@%s", np->ident, np->host->name->content);
947 hash = crc32(buf);
948
949 /* if the user is not authed, update the hash */
950 if (!IsAccount(np)) {
951 np->exts[cfnext] = (void *)hash;
952 }
953
954 return hash;
955 }
956 }
957
958 return 0; /* should not happen */
959 }
960
961 /* This seems to be a lot faster than using sprintf */
962 int cf_equhost(const char *uhost, const char *user, const char *host) {
963 char *p = strchr(uhost, '@');
964
965 /* We assume the uhost contains a @ - which it should do in all cases */
966 /* if (!p)
967 return 0; */
968
969 if (ircd_strncmp(uhost, user, p - uhost) == 0 && ircd_strcmp(p + 1, host) == 0)
970 return 1;
971 else
972 return 0;
973 }
974
975 /* Why do we actually store the users' real hosts/accounts instead of hashes?
976 * - it allows operators to see the hosts/accounts in 'chanoplist' even if the
977 * users are not online
978 * - it avoids hash collisions (could be avoided with md5/sha1/etc.)
979 */
980 int cf_cmpregopnick(regop *ro, nick *np) {
981 if (ro->uh != NULL) {
982 if (ro->type == CFACCOUNT && IsAccount(np))
983 return (ro->hash == cf_gethash(np, CFACCOUNT) &&
984 strcmp(ro->uh->content, np->authname) == 0);
985 else {
986 return (ro->hash == cf_gethash(np, CFHOST) &&
987 cf_equhost(ro->uh->content, np->ident, np->host->name->content));
988 }
989 } else {
990 if (ro->type == CFACCOUNT && IsAccount(np))
991 return (ro->hash == cf_gethash(np, CFACCOUNT));
992 else
993 return (ro->hash == cf_gethash(np, CFHOST));
994 }
995 }
996
997 nick *cf_findnick(regop *ro, chanindex *cip) {
998 chanfix *cf = cip->exts[cfext];
999 channel *cp = cip->channel;
1000 nick *np2;
1001 int a;
1002
1003 if (cf == NULL || cp == NULL)
1004 return NULL;
1005
1006 if (ro->type == CFACCOUNT) {
1007 for(a=0;a<cp->users->hashsize;a++) {
1008 if(cp->users->content[a] != nouser) {
1009 np2 = getnickbynumeric(cp->users->content[a]);
1010
1011 if (!IsAccount(np2))
1012 continue;
1013
1014 if (cf_cmpregopnick(ro, np2))
1015 return np2;
1016 }
1017 }
1018 }
1019
1020 if (ro->type == CFHOST) {
1021 for(a=0;a<cp->users->hashsize;a++) {
1022 if(cp->users->content[a] != nouser) {
1023 np2 = getnickbynumeric(cp->users->content[a]);
1024
1025 if (cf_cmpregopnick(ro, np2))
1026 return np2;
1027 }
1028 }
1029 }
1030
1031 return NULL;
1032 }
1033
1034 regop *cf_findregop(nick *np, chanindex *cip, int type) {
1035 chanfix *cf = cip->exts[cfext];
1036 regop *ro;
1037 int i, ty;
1038
1039 if (cf == NULL)
1040 return NULL;
1041
1042 if (IsAccount(np) && (type & CFACCOUNT))
1043 ty = CFACCOUNT;
1044 else
1045 ty = CFHOST;
1046
1047 for (i=0;i<cf->regops.cursi;i++) {
1048 ro = ((regop**)cf->regops.content)[i];
1049
1050 if (ro->type == ty && cf_cmpregopnick(ro, np))
1051 return ro;
1052 }
1053
1054 /* try using the uhost if we didn't find a user with the right account */
1055 if (ty == CFACCOUNT && (type & CFHOST))
1056 return cf_findregop(np, cip, CFHOST);
1057 else
1058 return NULL;
1059
1060 return NULL;
1061 }
1062
1063 regop *cf_createregop(nick *np, chanindex *cip) {
1064 chanfix *cf = cip->exts[cfext];
1065 int slot, type;
1066 regop **rolist;
1067
1068 if (cf == NULL) {
1069 cf = (chanfix*)malloc(sizeof(chanfix));
1070 cf->index = cip;
1071
1072 array_init(&(cf->regops), sizeof(regop*));
1073
1074 cip->exts[cfext] = cf;
1075 }
1076
1077 slot = array_getfreeslot(&(cf->regops));
1078
1079 rolist = (regop**)cf->regops.content;
1080
1081 rolist[slot] = (regop*)malloc(sizeof(regop));
1082
1083 if (IsAccount(np))
1084 type = CFACCOUNT;
1085 else
1086 type = CFHOST;
1087
1088 rolist[slot]->type = type;
1089 rolist[slot]->hash = cf_gethash(np, type);
1090 rolist[slot]->uh = NULL;
1091 rolist[slot]->lastopped = 0;
1092 rolist[slot]->score = 0;
1093
1094 return rolist[slot];
1095 }
1096
1097 void cf_deleteregop(chanindex *cip, regop *ro) {
1098 chanfix *cf = cip->exts[cfext];
1099 int a;
1100
1101 if (cf == NULL)
1102 return;
1103
1104 for (a=0;a<cf->regops.cursi;a++) {
1105 if (((regop**)cf->regops.content)[a] == ro) {
1106 freesstring(((regop**)cf->regops.content)[a]->uh);
1107 free(((regop**)cf->regops.content)[a]);
1108 array_delslot(&(cf->regops), a);
1109 }
1110 }
1111
1112 /* get rid of chanfix* if there are no more regops */
1113 if (cf->regops.cursi == 0) {
1114 array_free(&(cf->regops));
1115 free(cf);
1116 cip->exts[cfext] = NULL;
1117
1118 /* we could try to free the chanindex* here
1119 but that would make cfsched_dosample a lot more
1120 complicated */
1121 }
1122 }
1123
1124 int cf_fixchannel(channel *cp) {
1125 int a,i;
1126 nick *np;
1127 modechanges changes;
1128 regop *rolist[50];
1129 int count,ops;
1130 chanfix *cf = cp->index->exts[cfext];
1131
1132 if (cf == NULL)
1133 return CFX_NOCHANFIX;
1134
1135 localsetmodeinit(&changes, cp, mynick);
1136
1137 count = 0;
1138
1139 /* reop services first and deop other users */
1140 for(a=0;a<cp->users->hashsize;a++) {
1141 if(cp->users->content[a] != nouser) {
1142 np = getnickbynumeric(cp->users->content[a]);
1143
1144 if (IsService(np) && (np->nick[1] == '\0')) {
1145 localdosetmode_nick(&changes, np, MC_OP);
1146 count++;
1147 } else
1148 localdosetmode_nick(&changes, np, MC_DEOP);
1149 }
1150 }
1151
1152 /* don't reop users if we've already opped some services */
1153 #if !CFDEBUG
1154 if (count > 0) {
1155 localsetmodeflush(&changes, 1);
1156 return CFX_FIXED;
1157 }
1158 #endif
1159
1160 /* now get a sorted list of regops */
1161 ops = cf_getsortedregops(cf, 50, rolist);
1162
1163 /* and op some of them */
1164 for (i=0;i<ops;i++) {
1165 if (count >= CFMAXOPS || rolist[i]->score < (rolist[0]->score / 2))
1166 break;
1167
1168 if (rolist[i]->score < CFMINSCORE && i != 0 )
1169 continue;
1170
1171 np = cf_findnick(rolist[i], cp->index);
1172
1173 /* only if it's not a service, so we don't screw up 'count' */
1174 if (np && !(IsService(np) && np->nick[1] == '\0')) {
1175 localdosetmode_nick(&changes, np, MC_OP);
1176
1177 count++;
1178 }
1179 }
1180
1181 localsetmodeflush(&changes, 1);
1182
1183 if (count == CFMAXOPS)
1184 return CFX_FIXED;
1185 else if (count == 0)
1186 return CFX_NOUSERSAVAILABLE;
1187 else
1188 return CFX_FIXEDFEWOPS;
1189 }
1190
1191 int cf_storechanfix(void) {
1192 FILE *cfdata;
1193 regop *ro;
1194 chanfix *cf;
1195 chanindex *cip;
1196 char srcfile[300];
1197 char dstfile[300];
1198 int a, i, count = 0;
1199
1200 snprintf(dstfile, sizeof(dstfile), "%s.%d", CFSTORAGE, CFSAVEFILES);
1201 unlink(dstfile);
1202
1203 for (i = CFSAVEFILES; i > 0; i--) {
1204 snprintf(srcfile, sizeof(srcfile), "%s.%i", CFSTORAGE, i - 1);
1205 snprintf(dstfile, sizeof(dstfile), "%s.%i", CFSTORAGE, i);
1206 rename(srcfile, dstfile);
1207 }
1208
1209 snprintf(srcfile, sizeof(srcfile), "%s.0", CFSTORAGE);
1210 cfdata = fopen(srcfile, "w");
1211
1212 if (cfdata == NULL)
1213 return 0;
1214
1215 for (i=0; i<CHANNELHASHSIZE; i++) {
1216 for (cip=chantable[i]; cip; cip=cip->next) {
1217 if ((cf = cip->exts[cfext]) != NULL) {
1218 for (a=0;a<cf->regops.cursi;a++) {
1219 ro = ((regop**)cf->regops.content)[a];
1220
1221 if (ro->uh)
1222 fprintf(cfdata, "%s %d %lu %lu %d %s\n", cip->name->content,
1223 ro->type, ro->hash, ro->lastopped, ro->score, ro->uh->content);
1224 else
1225 fprintf(cfdata, "%s %d %lu %lu %d\n", cip->name->content,
1226 ro->type, ro->hash, ro->lastopped, ro->score);
1227 count++;
1228 }
1229 }
1230 }
1231 }
1232
1233 fclose(cfdata);
1234
1235 return count;
1236 }
1237
1238 /* channel type hash lastopped score host */
1239 int cf_parseline(char *line) {
1240 chanindex *cip;
1241 chanfix *cf;
1242 int count;
1243 int slot;
1244 char chan[CHANNELLEN+1];
1245 int type, score;
1246 unsigned long hash;
1247 time_t lastopped;
1248 char host[USERLEN+1+HOSTLEN+1];
1249 regop **rolist;
1250
1251 count = sscanf(line, "%s %d %lu %lu %d %s", chan, &type, &hash, &lastopped, &score, host);
1252
1253 if (count < 5)
1254 return 0; /* invalid chanfix record */
1255
1256 cip = findorcreatechanindex(chan);
1257
1258 cf = cip->exts[cfext];
1259
1260 if (cf == NULL) {
1261 cf = (chanfix*)malloc(sizeof(chanfix));
1262 cf->index = cip;
1263
1264 array_init(&(cf->regops), sizeof(regop*));
1265
1266 cip->exts[cfext] = cf;
1267 }
1268
1269 slot = array_getfreeslot(&(cf->regops));
1270
1271 rolist = (regop**)cf->regops.content;
1272
1273 rolist[slot] = (regop*)malloc(sizeof(regop));
1274
1275 rolist[slot]->type = type;
1276 rolist[slot]->hash = hash;
1277 rolist[slot]->lastopped = lastopped;
1278 rolist[slot]->score = score;
1279
1280 if (count >= 6)
1281 rolist[slot]->uh = getsstring(host, USERLEN+1+HOSTLEN);
1282 else
1283 rolist[slot]->uh = NULL;
1284
1285 return 1;
1286 }
1287
1288 int cf_loadchanfix(void) {
1289 char line[4096];
1290 FILE *cfdata;
1291 char srcfile[300];
1292 int count;
1293
1294 cf_free();
1295
1296 snprintf(srcfile, sizeof(srcfile), "%s.0", CFSTORAGE);
1297 cfdata = fopen(srcfile, "r");
1298
1299 if (cfdata == NULL)
1300 return 0;
1301
1302 count = 0;
1303
1304 while (!feof(cfdata)) {
1305 if (fgets(line, sizeof(line), cfdata) == NULL)
1306 break;
1307
1308 if (line[strlen(line) - 1] == '\n')
1309 line[strlen(line) - 1] = '\0';
1310
1311 if (line[strlen(line) - 1] == '\r')
1312 line[strlen(line) - 1] = '\0';
1313
1314 if (line[0] != '\0') {
1315 if (cf_parseline(line))
1316 count++;
1317 }
1318 }
1319
1320 fclose(cfdata);
1321
1322 return count;
1323 }
1324
1325 /* functions for users of this module */
1326 chanfix *cf_findchanfix(chanindex *cip) {
1327 return cip->exts[cfext];
1328 }
1329
1330 int cf_wouldreop(nick *np, channel *cp) {
1331 int i,topscore = 0;
1332 regop **rolist;
1333 regop *ro;
1334 chanfix *cf = cp->index->exts[cfext];
1335
1336 if (cf == NULL)
1337 return 1; /* too bad, we can't do anything about it */
1338
1339 ro = cf_findregop(np, cp->index, CFACCOUNT | CFHOST);
1340
1341 if (ro == NULL)
1342 return 0;
1343
1344 rolist = (regop**)cf->regops.content;
1345
1346 for (i=0; i<cf->regops.cursi; i++)
1347 if (rolist[i]->score > topscore)
1348 topscore = rolist[i]->score;
1349
1350 if (ro->score > topscore / 2 && ro->score > CFMINSCORE)
1351 return 1;
1352 else
1353 return 0;
1354 }