]>
Commit | Line | Data |
---|---|---|
8a25ddc6 P |
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" | |
7f32dbdf P |
17 | #include "../lib/version.h" |
18 | ||
19 | MODULE_VERSION("") | |
8a25ddc6 P |
20 | |
21 | /* control's nick */ | |
22 | extern nick *mynick; | |
23 | ||
24 | int cfext; | |
25 | int cfnext; | |
e8966eea | 26 | int cffailedinit; |
8a25ddc6 P |
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); | |
8a25ddc6 P |
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"); | |
e8966eea | 68 | cffailedinit = 1; |
8a25ddc6 P |
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 | ||
95ff6b03 P |
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"); | |
8a25ddc6 | 76 | #if CFDEBUG |
182917df | 77 | registercontrolhelpcmd("cfsample", NO_DEVELOPER, 1, &cfcmd_debugsample, "DEBUG Command - must not be loaded on live instances"); |
95ff6b03 | 78 | registercontrolhelpcmd("cfexpire", NO_DEVELOPER, 1, &cfcmd_debugexpire, "DEBUG Command - must not be loaded on live instances"); |
8a25ddc6 | 79 | #endif |
95ff6b03 P |
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"); | |
8a25ddc6 | 82 | |
95ff6b03 P |
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)"); | |
8a25ddc6 P |
85 | #if CFDEBUG |
86 | /* should we disable this in the 'final' build? */ | |
95ff6b03 | 87 | /* registercontrolcmd("requestop", 0, 2, &cfcmd_requestop); */ |
8a25ddc6 | 88 | #endif |
95ff6b03 P |
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"); | |
8a25ddc6 P |
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); | |
8a25ddc6 P |
101 | |
102 | cf_loadchanfix(); | |
103 | ||
e8966eea | 104 | cffailedinit = 0; |
8a25ddc6 P |
105 | } |
106 | } | |
107 | ||
108 | void _fini() { | |
e8966eea | 109 | if (cffailedinit == 0) { |
8a25ddc6 P |
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 | ||
8a25ddc6 P |
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); | |
1b0a2e39 | 126 | deregistercontrolcmd("chanfix", &cfcmd_chanfix); |
8a25ddc6 P |
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); | |
8a25ddc6 | 143 | } |
eb67af65 C |
144 | |
145 | if (cfext >= 0) { | |
146 | releasechanext(cfext); | |
147 | } | |
148 | ||
149 | if (cfnext >= 0) { | |
150 | releasenickext(cfnext); | |
151 | } | |
8a25ddc6 P |
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 | ||
182917df | 188 | controlreply(np, "%d. type: %s hash: 0x%lx lastopped: %lu uh: %s score: %d", |
8a25ddc6 P |
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; | |
8a25ddc6 P |
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) { | |
182917df C |
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]); | |
8a25ddc6 P |
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; | |
8a25ddc6 P |
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); | |
182917df | 327 | controlreply(np, "Scores of \"best ops\" on %s are:", cargv[0]); |
8a25ddc6 P |
328 | |
329 | for (i=0;i<count;i++) { | |
182917df | 330 | controlreply(np, " %d", rolist[i]->score); |
8a25ddc6 P |
331 | } |
332 | ||
8a25ddc6 P |
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); | |
182917df | 350 | controlreply(np, "Scores of current ops on %s are:", cargv[0]); |
8a25ddc6 P |
351 | |
352 | for (a=0;a<min(i,20);a++) { | |
353 | if (scores[a] == 0) | |
354 | break; | |
355 | ||
182917df | 356 | controlreply(np, " %d", scores[a]); |
8a25ddc6 P |
357 | } |
358 | ||
359 | free(scores); | |
8a25ddc6 P |
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]; | |
182917df | 371 | int i,count; |
8a25ddc6 | 372 | time_t ct; |
182917df C |
373 | struct tm *tm; |
374 | char date[50]; | |
8a25ddc6 P |
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; | |
182917df C |
417 | tm = gmtime(&ct); |
418 | strftime(date,sizeof(date),"%d-%m-%Y %H:%M:%S",tm); | |
8a25ddc6 | 419 | controlreply(np, "%3d %5d %-7s %1s%-16s %s Last seen: %s", i + 1, rolist[i]->score, |
182917df | 420 | (rolist[i]->type == CFACCOUNT) ? "account" : "host", "", "!NONE!", |
8a25ddc6 | 421 | rolist[i]->uh ? rolist[i]->uh->content : "!UNKNOWN!", date); |
8a25ddc6 P |
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() > 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; | |
182917df | 480 | int i, count, ops; |
8a25ddc6 P |
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 | ||
182917df C |
507 | for(i=0;i<cp->users->hashsize;i++) { |
508 | if(cp->users->content[i] != nouser) { | |
509 | np2 = getnickbynumeric(cp->users->content[i]); | |
8a25ddc6 P |
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() > 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; | |
8a25ddc6 P |
682 | regop *ro, *roh; |
683 | struct timeval start; | |
684 | struct timeval end; | |
685 | char buf[USERLEN+1+HOSTLEN+1]; | |
686 | ||
687 | now = getnettime(); | |
688 | ||
689 | cfuhost = cfscore = cfnewro = 0; | |
690 | ||
691 | if (sp_countsplitservers() > CFMAXSPLITSERVERS) | |
692 | return; | |
693 | ||
694 | gettimeofday(&start, NULL); | |
695 | ||
696 | for (i=0; i<CHANNELHASHSIZE; i++) { | |
697 | for (cip=chantable[i]; cip; cip=cip->next) { | |
8a25ddc6 P |
698 | cp = cip->channel; |
699 | ||
700 | if (!cp || cp->users->totalusers < CFMINUSERS) | |
701 | continue; | |
702 | ||
703 | for (a=0;a<cp->users->hashsize;a++) { | |
704 | if ((cp->users->content[a] != nouser) && (cp->users->content[a] & CUMODE_OP)) { | |
705 | np = getnickbynumeric(cp->users->content[a]); | |
706 | ||
cd8719e8 GB |
707 | if (np) |
708 | continue; | |
709 | ||
8a25ddc6 P |
710 | #if !CFDEBUG |
711 | if (IsService(np)) | |
712 | continue; | |
713 | #endif | |
714 | ||
715 | roh = ro = cf_findregop(np, cip, CFACCOUNT | CFHOST); | |
716 | ||
717 | if ((ro == NULL || (IsAccount(np) && ro->type == CFHOST)) && | |
718 | !cf_hasauthedcloneonchan(np, cp)) { | |
719 | ro = cf_createregop(np, cip); | |
720 | cfnewro++; | |
721 | } | |
722 | ||
723 | /* lastopped == now if the user has clones, we obviously | |
724 | * don't want to give them points in this case */ | |
725 | if (ro && ro->lastopped != now) { | |
726 | if (ro->type != CFHOST || !cf_hasauthedcloneonchan(np, cp)) { | |
727 | ro->score++; | |
728 | cfscore++; | |
729 | } | |
730 | ||
731 | /* merge any matching CFHOST records */ | |
732 | if (roh && roh->type == CFHOST && ro->type == CFACCOUNT) { | |
733 | /* hmm */ | |
734 | ro->score += roh->score; | |
735 | ||
736 | cf_deleteregop(cip, roh); | |
737 | } | |
738 | ||
739 | /* store the user's account/host if we have to */ | |
740 | if (ro->uh == NULL && ro->score >= CFMINSCOREUH) { | |
741 | if (ro->type == CFACCOUNT) | |
742 | ro->uh = getsstring(np->authname, ACCOUNTLEN); | |
743 | else { | |
182917df | 744 | snprintf(buf, sizeof(buf), "%s@%s", np->ident, np->host->name->content); |
8a25ddc6 P |
745 | roh->uh = getsstring(buf, USERLEN+1+HOSTLEN); |
746 | } | |
747 | ||
748 | cfuhost++; | |
749 | } | |
750 | ||
751 | ro->lastopped = now; | |
752 | } | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
757 | ||
758 | cp = findchannel("#qnet.chanfix"); | |
759 | ||
760 | if (cp) { | |
761 | gettimeofday(&end, NULL); | |
762 | ||
763 | diff = (end.tv_sec * 1000 + end.tv_usec / 1000) - | |
764 | (start.tv_sec * 1000 + start.tv_usec / 1000); | |
765 | ||
766 | sendmessagetochannel(mynick, cp, "sampled chanfix scores, assigned %d new" | |
767 | " points, %d new regops, %d user@hosts added, deltaT: %dms", cfscore, | |
768 | cfnewro, cfuhost, diff); | |
769 | } | |
770 | } | |
771 | ||
772 | void cfsched_doexpire(void *arg) { | |
773 | channel *cp; | |
774 | chanindex *cip; | |
775 | chanindex *ncip; | |
776 | chanfix *cf; | |
777 | int i,a,cfscore,cfregop,cffreeuh,diff; | |
778 | regop **rolist; | |
779 | regop *ro; | |
780 | struct timeval start; | |
781 | struct timeval end; | |
9b145387 | 782 | time_t currenttime; |
8a25ddc6 P |
783 | |
784 | cffreeuh = cfscore = cfregop = 0; | |
785 | ||
786 | gettimeofday(&start, NULL); | |
9b145387 | 787 | currenttime=getnettime(); |
8a25ddc6 P |
788 | |
789 | for (i=0; i<CHANNELHASHSIZE; i++) { | |
790 | for (cip=chantable[i]; cip; cip=cip->next) { | |
791 | cf = (chanfix*)cip->exts[cfext]; | |
792 | ||
793 | if (cf) { | |
794 | rolist = (regop**)cf->regops.content; | |
795 | ||
796 | for (a=0;a<cf->regops.cursi;a++) { | |
797 | ro = rolist[a]; | |
798 | ||
182917df | 799 | if (((currenttime - ro->lastopped) > (2 * CFSAMPLEINTERVAL)) && ro->score) { |
8a25ddc6 P |
800 | ro->score--; |
801 | cfscore++; | |
802 | } | |
803 | ||
182917df | 804 | if ((ro->score < CFMINSCOREUH) && ro->uh) { |
8a25ddc6 P |
805 | freesstring(ro->uh); |
806 | ro->uh = NULL; | |
807 | ||
808 | cffreeuh++; | |
809 | } | |
810 | ||
182917df | 811 | if (ro->score == 0 || ro->lastopped < (currenttime - CFREMEMBEROPS)) { |
8a25ddc6 P |
812 | cf_deleteregop(cip, ro); |
813 | cfregop++; | |
814 | } | |
815 | } | |
816 | } | |
817 | } | |
818 | } | |
819 | ||
820 | /* stolen from channel/channelindex.c */ | |
821 | for (i=0;i<CHANNELHASHSIZE;i++) { | |
822 | for (cip=chantable[i];cip;cip=ncip) { | |
823 | /* CAREFUL: deleting items from chains you're walking is bad */ | |
824 | ncip=cip->next; | |
825 | ||
826 | /* try to delete it if there's no chanfix* pointer */ | |
827 | if (cip->exts[cfext] == NULL) | |
828 | releasechanindex(cip); | |
829 | } | |
830 | } | |
831 | ||
832 | cp = findchannel("#qnet.chanfix"); | |
833 | ||
834 | if (cp) { | |
835 | gettimeofday(&end, NULL); | |
836 | ||
837 | diff = (end.tv_sec * 1000 + end.tv_usec / 1000) - | |
838 | (start.tv_sec * 1000 + start.tv_usec / 1000); | |
839 | ||
840 | sendmessagetochannel(mynick, cp, "expired chanfix scores, purged %d points," | |
841 | " scrapped %6d regops, %d user@hosts freed, deltaT: %dms", cfscore, | |
842 | cfregop, cffreeuh, diff); | |
843 | } | |
844 | ||
845 | } | |
846 | ||
847 | void cfsched_dosave(void *arg) { | |
848 | cf_storechanfix(); | |
849 | } | |
850 | ||
851 | #if CFAUTOFIX | |
852 | void cfhook_autofix(int hook, void *arg) { | |
853 | int a, count; | |
854 | void **args = (void**)arg; | |
855 | channel *cp; | |
856 | ||
857 | if (hook == HOOK_CHANNEL_DEOPPED || hook == HOOK_CHANNEL_PART || | |
858 | hook == HOOK_CHANNEL_KICK || hook == HOOK_CHANNEL_JOIN) { | |
859 | cp = args[0]; | |
860 | ||
861 | if (sp_countsplitservers() > 0) | |
862 | return; | |
863 | ||
864 | for(a=0;a<cp->users->hashsize;a++) { | |
865 | if (cp->users->content[a] != nouser) { | |
866 | count++; | |
867 | ||
868 | if (cp->users->content[a] & CUMODE_OP) | |
869 | return; | |
870 | } | |
871 | } | |
872 | ||
873 | /* don't fix small channels.. it's inaccurate and | |
874 | * they could just cycle the channel */ | |
875 | if (count < 4) | |
876 | return; | |
877 | ||
878 | cf_fixchannel((channel*)args[0]); | |
879 | } | |
880 | } | |
881 | #endif | |
882 | ||
883 | void cfhook_statsreport(int hook, void *arg) { | |
884 | char buf[300]; | |
885 | int i,a,rc,mc,memory; | |
886 | chanindex *cip; | |
8a25ddc6 P |
887 | chanfix *cf; |
888 | ||
c3db6f7e | 889 | if ((long)arg > 2) { |
8a25ddc6 P |
890 | memory = rc = mc = 0; |
891 | ||
892 | for (i=0; i<CHANNELHASHSIZE; i++) { | |
893 | for (cip=chantable[i]; cip; cip=cip->next) { | |
894 | if ((cf = cip->exts[cfext]) != NULL) { | |
895 | for (a=0;a<cf->regops.cursi;a++) { | |
896 | memory += sizeof(regop) + sizeof(regop*); | |
897 | ||
898 | if (((regop**)cf->regops.content)[a]->uh) | |
899 | memory += sizeof(sstring) + strlen(((regop**)cf->regops.content)[a]->uh->content) + 1; | |
900 | ||
901 | rc++; | |
902 | } | |
903 | ||
904 | memory += sizeof(chanfix); | |
905 | ||
906 | mc++; | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
182917df C |
911 | snprintf(buf, sizeof(buf), "Chanfix : %6d registered ops, %9d monitored channels. %9d" |
912 | " kbytes of memory used", rc, mc, (memory / 1024)); | |
8a25ddc6 P |
913 | triggerhook(HOOK_CORE_STATSREPLY, buf); |
914 | } | |
915 | } | |
916 | ||
917 | void cfhook_auth(int hook, void *arg) { | |
918 | nick *np = (nick*)arg; | |
919 | ||
920 | /* Invalidate the user's hash */ | |
8a25ddc6 P |
921 | np->exts[cfnext] = NULL; |
922 | ||
923 | /* Calculate the new hash */ | |
924 | cf_gethash(np, CFACCOUNT); | |
925 | } | |
926 | ||
8a25ddc6 P |
927 | /* Returns the hash of a specific user (np), type can be either CFACCOUNT, |
928 | CFHOST or both (binary or'd values). cf_gethash will also cache the user's | |
929 | hash in a nick extension */ | |
930 | unsigned long cf_gethash(nick *np, int type) { | |
931 | char buf[USERLEN+1+HOSTLEN+1]; | |
932 | unsigned long hash; | |
933 | ||
182917df | 934 | if (IsAccount(np) && (type & CFACCOUNT)) { |
8a25ddc6 | 935 | if (np->exts[cfnext] == NULL) { |
182917df | 936 | np->exts[cfnext] = (void *)crc32(np->authname); |
8a25ddc6 P |
937 | } |
938 | ||
182917df | 939 | return (unsigned long)np->exts[cfnext]; |
8a25ddc6 P |
940 | } else if (type == CFACCOUNT) |
941 | return 0; /* this should not happen */ | |
942 | ||
943 | if (type & CFHOST) { | |
944 | if (!IsAccount(np) && np->exts[cfnext]) | |
182917df | 945 | return (unsigned long)np->exts[cfnext]; |
8a25ddc6 | 946 | else { |
182917df | 947 | snprintf(buf, sizeof(buf), "%s@%s", np->ident, np->host->name->content); |
8a25ddc6 P |
948 | hash = crc32(buf); |
949 | ||
950 | /* if the user is not authed, update the hash */ | |
951 | if (!IsAccount(np)) { | |
182917df | 952 | np->exts[cfnext] = (void *)hash; |
8a25ddc6 P |
953 | } |
954 | ||
955 | return hash; | |
956 | } | |
957 | } | |
958 | ||
959 | return 0; /* should not happen */ | |
960 | } | |
961 | ||
962 | /* This seems to be a lot faster than using sprintf */ | |
963 | int cf_equhost(const char *uhost, const char *user, const char *host) { | |
964 | char *p = strchr(uhost, '@'); | |
965 | ||
966 | /* We assume the uhost contains a @ - which it should do in all cases */ | |
967 | /* if (!p) | |
968 | return 0; */ | |
969 | ||
970 | if (ircd_strncmp(uhost, user, p - uhost) == 0 && ircd_strcmp(p + 1, host) == 0) | |
971 | return 1; | |
972 | else | |
973 | return 0; | |
974 | } | |
975 | ||
976 | /* Why do we actually store the users' real hosts/accounts instead of hashes? | |
977 | * - it allows operators to see the hosts/accounts in 'chanoplist' even if the | |
978 | * users are not online | |
979 | * - it avoids hash collisions (could be avoided with md5/sha1/etc.) | |
980 | */ | |
981 | int cf_cmpregopnick(regop *ro, nick *np) { | |
982 | if (ro->uh != NULL) { | |
983 | if (ro->type == CFACCOUNT && IsAccount(np)) | |
984 | return (ro->hash == cf_gethash(np, CFACCOUNT) && | |
985 | strcmp(ro->uh->content, np->authname) == 0); | |
986 | else { | |
987 | return (ro->hash == cf_gethash(np, CFHOST) && | |
988 | cf_equhost(ro->uh->content, np->ident, np->host->name->content)); | |
989 | } | |
990 | } else { | |
991 | if (ro->type == CFACCOUNT && IsAccount(np)) | |
992 | return (ro->hash == cf_gethash(np, CFACCOUNT)); | |
993 | else | |
994 | return (ro->hash == cf_gethash(np, CFHOST)); | |
995 | } | |
996 | } | |
997 | ||
998 | nick *cf_findnick(regop *ro, chanindex *cip) { | |
999 | chanfix *cf = cip->exts[cfext]; | |
1000 | channel *cp = cip->channel; | |
1001 | nick *np2; | |
1002 | int a; | |
1003 | ||
1004 | if (cf == NULL || cp == NULL) | |
1005 | return NULL; | |
1006 | ||
1007 | if (ro->type == CFACCOUNT) { | |
1008 | for(a=0;a<cp->users->hashsize;a++) { | |
1009 | if(cp->users->content[a] != nouser) { | |
1010 | np2 = getnickbynumeric(cp->users->content[a]); | |
1011 | ||
1012 | if (!IsAccount(np2)) | |
1013 | continue; | |
1014 | ||
1015 | if (cf_cmpregopnick(ro, np2)) | |
1016 | return np2; | |
1017 | } | |
1018 | } | |
1019 | } | |
1020 | ||
1021 | if (ro->type == CFHOST) { | |
1022 | for(a=0;a<cp->users->hashsize;a++) { | |
1023 | if(cp->users->content[a] != nouser) { | |
1024 | np2 = getnickbynumeric(cp->users->content[a]); | |
1025 | ||
1026 | if (cf_cmpregopnick(ro, np2)) | |
1027 | return np2; | |
1028 | } | |
1029 | } | |
1030 | } | |
1031 | ||
1032 | return NULL; | |
1033 | } | |
1034 | ||
1035 | regop *cf_findregop(nick *np, chanindex *cip, int type) { | |
1036 | chanfix *cf = cip->exts[cfext]; | |
1037 | regop *ro; | |
1038 | int i, ty; | |
1039 | ||
1040 | if (cf == NULL) | |
1041 | return NULL; | |
1042 | ||
182917df | 1043 | if (IsAccount(np) && (type & CFACCOUNT)) |
8a25ddc6 P |
1044 | ty = CFACCOUNT; |
1045 | else | |
1046 | ty = CFHOST; | |
1047 | ||
1048 | for (i=0;i<cf->regops.cursi;i++) { | |
1049 | ro = ((regop**)cf->regops.content)[i]; | |
1050 | ||
1051 | if (ro->type == ty && cf_cmpregopnick(ro, np)) | |
1052 | return ro; | |
1053 | } | |
1054 | ||
1055 | /* try using the uhost if we didn't find a user with the right account */ | |
182917df | 1056 | if (ty == CFACCOUNT && (type & CFHOST)) |
8a25ddc6 P |
1057 | return cf_findregop(np, cip, CFHOST); |
1058 | else | |
1059 | return NULL; | |
1060 | ||
1061 | return NULL; | |
1062 | } | |
1063 | ||
1064 | regop *cf_createregop(nick *np, chanindex *cip) { | |
1065 | chanfix *cf = cip->exts[cfext]; | |
1066 | int slot, type; | |
1067 | regop **rolist; | |
1068 | ||
1069 | if (cf == NULL) { | |
1070 | cf = (chanfix*)malloc(sizeof(chanfix)); | |
1071 | cf->index = cip; | |
1072 | ||
1073 | array_init(&(cf->regops), sizeof(regop*)); | |
1074 | ||
1075 | cip->exts[cfext] = cf; | |
1076 | } | |
1077 | ||
1078 | slot = array_getfreeslot(&(cf->regops)); | |
1079 | ||
1080 | rolist = (regop**)cf->regops.content; | |
1081 | ||
1082 | rolist[slot] = (regop*)malloc(sizeof(regop)); | |
1083 | ||
1084 | if (IsAccount(np)) | |
1085 | type = CFACCOUNT; | |
1086 | else | |
1087 | type = CFHOST; | |
1088 | ||
1089 | rolist[slot]->type = type; | |
1090 | rolist[slot]->hash = cf_gethash(np, type); | |
1091 | rolist[slot]->uh = NULL; | |
1092 | rolist[slot]->lastopped = 0; | |
1093 | rolist[slot]->score = 0; | |
1094 | ||
1095 | return rolist[slot]; | |
1096 | } | |
1097 | ||
1098 | void cf_deleteregop(chanindex *cip, regop *ro) { | |
1099 | chanfix *cf = cip->exts[cfext]; | |
1100 | int a; | |
1101 | ||
1102 | if (cf == NULL) | |
1103 | return; | |
1104 | ||
1105 | for (a=0;a<cf->regops.cursi;a++) { | |
1106 | if (((regop**)cf->regops.content)[a] == ro) { | |
1107 | freesstring(((regop**)cf->regops.content)[a]->uh); | |
1108 | free(((regop**)cf->regops.content)[a]); | |
1109 | array_delslot(&(cf->regops), a); | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | /* get rid of chanfix* if there are no more regops */ | |
1114 | if (cf->regops.cursi == 0) { | |
1115 | array_free(&(cf->regops)); | |
1116 | free(cf); | |
1117 | cip->exts[cfext] = NULL; | |
1118 | ||
1119 | /* we could try to free the chanindex* here | |
1120 | but that would make cfsched_dosample a lot more | |
1121 | complicated */ | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | int cf_fixchannel(channel *cp) { | |
1126 | int a,i; | |
1127 | nick *np; | |
1128 | modechanges changes; | |
1129 | regop *rolist[50]; | |
1130 | int count,ops; | |
1131 | chanfix *cf = cp->index->exts[cfext]; | |
1132 | ||
1133 | if (cf == NULL) | |
1134 | return CFX_NOCHANFIX; | |
1135 | ||
1136 | localsetmodeinit(&changes, cp, mynick); | |
1137 | ||
1138 | count = 0; | |
1139 | ||
1140 | /* reop services first and deop other users */ | |
1141 | for(a=0;a<cp->users->hashsize;a++) { | |
1142 | if(cp->users->content[a] != nouser) { | |
1143 | np = getnickbynumeric(cp->users->content[a]); | |
1144 | ||
93b87578 | 1145 | if (IsService(np) && (np->nick[1] == '\0')) { |
8a25ddc6 P |
1146 | localdosetmode_nick(&changes, np, MC_OP); |
1147 | count++; | |
1148 | } else | |
1149 | localdosetmode_nick(&changes, np, MC_DEOP); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | /* don't reop users if we've already opped some services */ | |
1154 | #if !CFDEBUG | |
d1cb1fd3 CP |
1155 | if (count > 0) { |
1156 | localsetmodeflush(&changes, 1); | |
1b0a2e39 | 1157 | return CFX_FIXED; |
d1cb1fd3 | 1158 | } |
8a25ddc6 P |
1159 | #endif |
1160 | ||
1161 | /* now get a sorted list of regops */ | |
1162 | ops = cf_getsortedregops(cf, 50, rolist); | |
1163 | ||
1164 | /* and op some of them */ | |
1165 | for (i=0;i<ops;i++) { | |
182917df | 1166 | if (count >= CFMAXOPS || rolist[i]->score < (rolist[0]->score / 2)) |
8a25ddc6 P |
1167 | break; |
1168 | ||
f940478e | 1169 | if (rolist[i]->score < CFMINSCORE && i != 0 ) |
8a25ddc6 P |
1170 | continue; |
1171 | ||
1172 | np = cf_findnick(rolist[i], cp->index); | |
1173 | ||
1174 | /* only if it's not a service, so we don't screw up 'count' */ | |
93b87578 | 1175 | if (np && !(IsService(np) && np->nick[1] == '\0')) { |
8a25ddc6 P |
1176 | localdosetmode_nick(&changes, np, MC_OP); |
1177 | ||
1178 | count++; | |
1179 | } | |
1180 | } | |
1181 | ||
1182 | localsetmodeflush(&changes, 1); | |
1183 | ||
8a25ddc6 P |
1184 | if (count == CFMAXOPS) |
1185 | return CFX_FIXED; | |
1186 | else if (count == 0) | |
1187 | return CFX_NOUSERSAVAILABLE; | |
1188 | else | |
1189 | return CFX_FIXEDFEWOPS; | |
1190 | } | |
1191 | ||
1192 | int cf_storechanfix(void) { | |
1193 | FILE *cfdata; | |
1194 | regop *ro; | |
1195 | chanfix *cf; | |
1196 | chanindex *cip; | |
1197 | char srcfile[300]; | |
1198 | char dstfile[300]; | |
1199 | int a, i, count = 0; | |
1200 | ||
182917df | 1201 | snprintf(dstfile, sizeof(dstfile), "%s.%d", CFSTORAGE, CFSAVEFILES); |
8a25ddc6 P |
1202 | unlink(dstfile); |
1203 | ||
1204 | for (i = CFSAVEFILES; i > 0; i--) { | |
182917df C |
1205 | snprintf(srcfile, sizeof(srcfile), "%s.%i", CFSTORAGE, i - 1); |
1206 | snprintf(dstfile, sizeof(dstfile), "%s.%i", CFSTORAGE, i); | |
8a25ddc6 P |
1207 | rename(srcfile, dstfile); |
1208 | } | |
1209 | ||
182917df | 1210 | snprintf(srcfile, sizeof(srcfile), "%s.0", CFSTORAGE); |
8a25ddc6 P |
1211 | cfdata = fopen(srcfile, "w"); |
1212 | ||
1213 | if (cfdata == NULL) | |
1214 | return 0; | |
1215 | ||
1216 | for (i=0; i<CHANNELHASHSIZE; i++) { | |
1217 | for (cip=chantable[i]; cip; cip=cip->next) { | |
1218 | if ((cf = cip->exts[cfext]) != NULL) { | |
1219 | for (a=0;a<cf->regops.cursi;a++) { | |
1220 | ro = ((regop**)cf->regops.content)[a]; | |
1221 | ||
1222 | if (ro->uh) | |
182917df C |
1223 | fprintf(cfdata, "%s %d %lu %lu %d %s\n", cip->name->content, |
1224 | ro->type, ro->hash, ro->lastopped, ro->score, ro->uh->content); | |
8a25ddc6 | 1225 | else |
182917df C |
1226 | fprintf(cfdata, "%s %d %lu %lu %d\n", cip->name->content, |
1227 | ro->type, ro->hash, ro->lastopped, ro->score); | |
8a25ddc6 P |
1228 | count++; |
1229 | } | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | fclose(cfdata); | |
1235 | ||
1236 | return count; | |
1237 | } | |
1238 | ||
1239 | /* channel type hash lastopped score host */ | |
1240 | int cf_parseline(char *line) { | |
1241 | chanindex *cip; | |
1242 | chanfix *cf; | |
1243 | int count; | |
1244 | int slot; | |
1245 | char chan[CHANNELLEN+1]; | |
182917df C |
1246 | int type, score; |
1247 | unsigned long hash; | |
1248 | time_t lastopped; | |
8a25ddc6 | 1249 | char host[USERLEN+1+HOSTLEN+1]; |
8a25ddc6 P |
1250 | regop **rolist; |
1251 | ||
182917df | 1252 | count = sscanf(line, "%s %d %lu %lu %d %s", chan, &type, &hash, &lastopped, &score, host); |
8a25ddc6 P |
1253 | |
1254 | if (count < 5) | |
1255 | return 0; /* invalid chanfix record */ | |
1256 | ||
1257 | cip = findorcreatechanindex(chan); | |
1258 | ||
1259 | cf = cip->exts[cfext]; | |
1260 | ||
1261 | if (cf == NULL) { | |
1262 | cf = (chanfix*)malloc(sizeof(chanfix)); | |
1263 | cf->index = cip; | |
1264 | ||
1265 | array_init(&(cf->regops), sizeof(regop*)); | |
1266 | ||
1267 | cip->exts[cfext] = cf; | |
1268 | } | |
1269 | ||
1270 | slot = array_getfreeslot(&(cf->regops)); | |
1271 | ||
1272 | rolist = (regop**)cf->regops.content; | |
1273 | ||
1274 | rolist[slot] = (regop*)malloc(sizeof(regop)); | |
1275 | ||
1276 | rolist[slot]->type = type; | |
1277 | rolist[slot]->hash = hash; | |
1278 | rolist[slot]->lastopped = lastopped; | |
1279 | rolist[slot]->score = score; | |
1280 | ||
182917df | 1281 | if (count >= 6) |
8a25ddc6 P |
1282 | rolist[slot]->uh = getsstring(host, USERLEN+1+HOSTLEN); |
1283 | else | |
1284 | rolist[slot]->uh = NULL; | |
1285 | ||
1286 | return 1; | |
1287 | } | |
1288 | ||
1289 | int cf_loadchanfix(void) { | |
1290 | char line[4096]; | |
1291 | FILE *cfdata; | |
1292 | char srcfile[300]; | |
1293 | int count; | |
1294 | ||
1295 | cf_free(); | |
1296 | ||
182917df | 1297 | snprintf(srcfile, sizeof(srcfile), "%s.0", CFSTORAGE); |
8a25ddc6 P |
1298 | cfdata = fopen(srcfile, "r"); |
1299 | ||
1300 | if (cfdata == NULL) | |
1301 | return 0; | |
1302 | ||
1303 | count = 0; | |
1304 | ||
1305 | while (!feof(cfdata)) { | |
1306 | if (fgets(line, sizeof(line), cfdata) == NULL) | |
1307 | break; | |
1308 | ||
1309 | if (line[strlen(line) - 1] == '\n') | |
1310 | line[strlen(line) - 1] = '\0'; | |
1311 | ||
1312 | if (line[strlen(line) - 1] == '\r') | |
1313 | line[strlen(line) - 1] = '\0'; | |
1314 | ||
1315 | if (line[0] != '\0') { | |
1316 | if (cf_parseline(line)) | |
1317 | count++; | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | fclose(cfdata); | |
1322 | ||
1323 | return count; | |
1324 | } | |
1325 | ||
1326 | /* functions for users of this module */ | |
1327 | chanfix *cf_findchanfix(chanindex *cip) { | |
1328 | return cip->exts[cfext]; | |
1329 | } | |
1330 | ||
1331 | int cf_wouldreop(nick *np, channel *cp) { | |
1332 | int i,topscore = 0; | |
1333 | regop **rolist; | |
1334 | regop *ro; | |
1335 | chanfix *cf = cp->index->exts[cfext]; | |
1336 | ||
1337 | if (cf == NULL) | |
1338 | return 1; /* too bad, we can't do anything about it */ | |
1339 | ||
1340 | ro = cf_findregop(np, cp->index, CFACCOUNT | CFHOST); | |
1341 | ||
1342 | if (ro == NULL) | |
1343 | return 0; | |
1344 | ||
1345 | rolist = (regop**)cf->regops.content; | |
1346 | ||
1347 | for (i=0; i<cf->regops.cursi; i++) | |
1348 | if (rolist[i]->score > topscore) | |
1349 | topscore = rolist[i]->score; | |
1350 | ||
1351 | if (ro->score > topscore / 2 && ro->score > CFMINSCORE) | |
1352 | return 1; | |
1353 | else | |
1354 | return 0; | |
1355 | } |