Debian Codegraph @

modules/chanfix/fix.c

raw
1 /* chanfix - channel fixing service
2 *
3 * Copyright (c) 2010-2016 Atheme Development Group
4 *
5 */
6
7 #include "atheme.h"
8 #include "chanfix.h"
9
10 static unsigned int count_ops(channel_t *c)
11 {
12 unsigned int i = 0;
13 mowgli_node_t *n;
14
15 return_val_if_fail(c != NULL, 0);
16
17 MOWGLI_ITER_FOREACH(n, c->members.head)
18 {
19 chanuser_t *cu = n->data;
20
21 if (cu->modes & CSTATUS_OP)
22 i++;
23 }
24
25 return i;
26 }
27
28 static bool chanfix_should_handle(chanfix_channel_t *cfchan, channel_t *c)
29 {
30 mychan_t *mc;
31 unsigned int n;
32
33 return_val_if_fail(cfchan != NULL, false);
34
35 if (c == NULL)
36 return false;
37
38 if ((mc = mychan_find(c->name)) != NULL)
39 return false;
40
41 n = count_ops(c);
42 /* enough ops, don't touch it */
43 if (n >= CHANFIX_OP_THRESHHOLD)
44 return false;
45 /* only start a fix for opless channels, and consider a fix done
46 * after CHANFIX_FIX_TIME if any ops were given
47 */
48 if (n > 0 && (cfchan->fix_started == 0 ||
49 CURRTIME - cfchan->fix_started > CHANFIX_FIX_TIME))
50 return false;
51
52 return true;
53 }
54
55 static unsigned int chanfix_calculate_score(chanfix_oprecord_t *orec)
56 {
57 unsigned int base;
58
59 return_val_if_fail(orec != NULL, 0);
60
61 base = orec->age;
62 if (orec->entity != NULL)
63 base *= CHANFIX_ACCOUNT_WEIGHT;
64
65 return base;
66 }
67
68 static void chanfix_lower_ts(chanfix_channel_t *chan)
69 {
70 channel_t *ch;
71 chanuser_t *cfu;
72 mowgli_node_t *n;
73
74 ch = chan->chan;
75 if (ch == NULL)
76 return;
77
78 /* Apply mode change locally only, chan_lowerts() will propagate. */
79 channel_mode_va(NULL, ch, 2, "-ilk", "*");
80
81 chan->ts--;
82 ch->ts = chan->ts;
83
84 MOWGLI_ITER_FOREACH(n, ch->members.head)
85 {
86 chanuser_t *cu = n->data;
87 cu->modes = 0;
88 }
89
90 chan_lowerts(ch, chanfix->me);
91 cfu = chanuser_add(ch, CLIENT_NAME(chanfix->me));
92 cfu->modes |= CSTATUS_OP;
93
94 msg(chanfix->me->nick, chan->name, "I only joined to remove modes.");
95
96 part(chan->name, chanfix->me->nick);
97 }
98
99 static unsigned int chanfix_get_highscore(chanfix_channel_t *chan)
100 {
101 unsigned int highscore = 0;
102 mowgli_node_t *n;
103
104 MOWGLI_ITER_FOREACH(n, chan->oprecords.head)
105 {
106 unsigned int score;
107 chanfix_oprecord_t *orec = n->data;
108
109 score = chanfix_calculate_score(orec);
110 if (score > highscore)
111 highscore = score;
112 }
113
114 return highscore;
115 }
116
117 static unsigned int chanfix_get_threshold(chanfix_channel_t *chan)
118 {
119 unsigned int highscore, t, threshold;
120
121 highscore = chanfix_get_highscore(chan);
122
123 t = CURRTIME - chan->fix_started;
124 if (t > CHANFIX_FIX_TIME)
125 t = CHANFIX_FIX_TIME;
126 threshold = highscore * (CHANFIX_INITIAL_STEP +
127 (CHANFIX_FINAL_STEP - CHANFIX_INITIAL_STEP) *
128 t / CHANFIX_FIX_TIME);
129 if (threshold == 0)
130 threshold = 1;
131 return threshold;
132 }
133
134 static bool chanfix_fix_channel(chanfix_channel_t *chan)
135 {
136 channel_t *ch;
137 mowgli_node_t *n;
138 unsigned int threshold, opped = 0;
139
140 ch = chan->chan;
141 if (ch == NULL)
142 return false;
143
144 /* op users who have X% of the highest score. */
145 threshold = chanfix_get_threshold(chan);
146 MOWGLI_ITER_FOREACH(n, ch->members.head)
147 {
148 chanuser_t *cu = n->data;
149 chanfix_oprecord_t *orec;
150 unsigned int score;
151
152 if (cu->user == chanfix->me)
153 continue;
154 if (cu->modes & CSTATUS_OP)
155 continue;
156
157 orec = chanfix_oprecord_find(chan, cu->user);
158 if (orec == NULL)
159 continue;
160
161 score = chanfix_calculate_score(orec);
162
163 if (score >= threshold)
164 {
165 if (opped == 0)
166 join(chan->name, chanfix->me->nick);
167 modestack_mode_param(chanfix->me->nick, chan->chan, MTYPE_ADD, 'o', CLIENT_NAME(cu->user));
168 cu->modes |= CSTATUS_OP;
169 opped++;
170 }
171 }
172
173 if (opped == 0)
174 return false;
175
176 /* flush the modestacker. */
177 modestack_flush_channel(ch);
178
179 /* now report the damage */
180 msg(chanfix->me->nick, chan->name, "\2%d\2 client%s should have been opped.", opped, opped != 1 ? "s" : "");
181
182 /* if this is the services log channel, continue to occupy it after the fix */
183 if (ch->flags & CHAN_LOG)
184 return true;
185
186 /* fix done, leave. */
187 part(chan->name, chanfix->me->nick);
188
189 return true;
190 }
191
192 static bool chanfix_can_start_fix(chanfix_channel_t *chan)
193 {
194 channel_t *ch;
195 mowgli_node_t *n;
196 unsigned int threshold;
197
198 ch = chan->chan;
199 if (ch == NULL)
200 return false;
201
202 threshold = chanfix_get_highscore(chan) * CHANFIX_FINAL_STEP;
203 MOWGLI_ITER_FOREACH(n, ch->members.head)
204 {
205 chanuser_t *cu = n->data;
206 chanfix_oprecord_t *orec;
207 unsigned int score;
208
209 if (cu->user == chanfix->me)
210 continue;
211 if (cu->modes & CSTATUS_OP)
212 return false;
213
214 orec = chanfix_oprecord_find(chan, cu->user);
215 if (orec == NULL)
216 continue;
217
218 score = chanfix_calculate_score(orec);
219 if (score >= threshold)
220 return true;
221 }
222 return false;
223 }
224
225 static void chanfix_clear_bans(channel_t *ch)
226 {
227 bool joined = false;
228 mowgli_node_t *n, *tn;
229
230 return_if_fail(ch != NULL);
231
232 if (ch->modes & CMODE_INVITE)
233 {
234 if (!joined)
235 {
236 joined = true;
237 join(ch->name, chanfix->me->nick);
238 }
239 channel_mode_va(chanfix->me, ch, 1, "-i");
240 }
241 if (ch->limit > 0)
242 {
243 if (!joined)
244 {
245 joined = true;
246 join(ch->name, chanfix->me->nick);
247 }
248 channel_mode_va(chanfix->me, ch, 1, "-l");
249 }
250 if (ch->key != NULL)
251 {
252 if (!joined)
253 {
254 joined = true;
255 join(ch->name, chanfix->me->nick);
256 }
257 channel_mode_va(chanfix->me, ch, 2, "-k", "*");
258 }
259 MOWGLI_ITER_FOREACH_SAFE(n, tn, ch->bans.head)
260 {
261 chanban_t *cb = n->data;
262
263 if (cb->type != 'b')
264 continue;
265
266 if (!joined)
267 {
268 joined = true;
269 join(ch->name, chanfix->me->nick);
270 }
271 modestack_mode_param(chanfix->me->nick, ch, MTYPE_DEL,
272 'b', cb->mask);
273 chanban_delete(cb);
274 }
275 if (!joined)
276 return;
277
278 modestack_flush_channel(ch);
279 msg(chanfix->me->nick, ch->name, "I only joined to remove modes.");
280 part(ch->name, chanfix->me->nick);
281 }
282
283 /*************************************************************************************/
284
285 bool chanfix_do_autofix;
286
287 void chanfix_autofix_ev(void *unused)
288 {
289 chanfix_channel_t *chan;
290 mowgli_patricia_iteration_state_t state;
291
292 MOWGLI_PATRICIA_FOREACH(chan, &state, chanfix_channels)
293 {
294 if (!chanfix_do_autofix && !chan->fix_requested)
295 continue;
296
297 if (chanfix_should_handle(chan, chan->chan))
298 {
299 if (chan->fix_started == 0)
300 {
301 if (chanfix_can_start_fix(chan))
302 {
303 slog(LG_INFO, "chanfix_autofix_ev(): fixing %s automatically.", chan->name);
304 chan->fix_started = CURRTIME;
305 /* If we are opping some users
306 * immediately, they can handle it.
307 * Otherwise, remove bans to allow
308 * users with higher scores to join.
309 */
310 if (!chanfix_fix_channel(chan))
311 chanfix_clear_bans(chan->chan);
312 }
313 else
314 {
315 /* No scored ops yet, remove bans
316 * to allow them to join.
317 */
318 chanfix_clear_bans(chan->chan);
319 }
320 }
321 else
322 {
323 /* Continue trying to give ops.
324 * If the channel is still or again opless,
325 * remove bans to allow ops to join.
326 */
327 if (!chanfix_fix_channel(chan) &&
328 count_ops(chan->chan) == 0)
329 chanfix_clear_bans(chan->chan);
330 }
331 }
332 else
333 {
334 chan->fix_requested = false;
335 chan->fix_started = 0;
336 }
337 }
338 }
339
340 /*************************************************************************************/
341
342 static void chanfix_cmd_fix(sourceinfo_t *si, int parc, char *parv[])
343 {
344 chanfix_channel_t *chan;
345 unsigned int highscore;
346
347 if (parv[0] == NULL)
348 {
349 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "CHANFIX");
350 command_fail(si, fault_needmoreparams, _("To fix a channel: CHANFIX <#channel>"));
351 return;
352 }
353
354 if (!channel_find(parv[0]))
355 {
356 command_fail(si, fault_nosuch_target, _("Channel \2%s\2 does not exist."), parv[0]);
357 return;
358 }
359
360 if ((chan = chanfix_channel_find(parv[0])) == NULL)
361 {
362 command_fail(si, fault_nosuch_target, _("No CHANFIX record available for \2%s\2; try again later."),
363 parv[0]);
364 return;
365 }
366
367 if (mychan_find(parv[0]))
368 {
369 command_fail(si, fault_nosuch_target, _("\2%s\2 is already registered."), parv[0]);
370 return;
371 }
372
373 highscore = chanfix_get_highscore(chan);
374 if (highscore < CHANFIX_MIN_FIX_SCORE)
375 {
376 command_fail(si, fault_nosuch_target, _("Scores for \2%s\2 are too low (< %u) for a fix."),
377 parv[0], CHANFIX_MIN_FIX_SCORE);
378 return;
379 }
380
381 chanfix_lower_ts(chan);
382 chan->fix_requested = true;
383
384 logcommand(si, CMDLOG_ADMIN, "CHANFIX: \2%s\2", parv[0]);
385
386 command_success_nodata(si, _("Fix request has been acknowledged for \2%s\2."), parv[0]);
387 }
388
389 command_t cmd_chanfix = { "CHANFIX", N_("Manually chanfix a channel."), PRIV_CHAN_ADMIN, 1, chanfix_cmd_fix, { .path = "chanfix/chanfix" } };
390
391 static int chanfix_compare_records(mowgli_node_t *a, mowgli_node_t *b, void *unused)
392 {
393 chanfix_oprecord_t *ta = a->data;
394 chanfix_oprecord_t *tb = b->data;
395
396 return tb->age - ta->age;
397 }
398
399 static void chanfix_cmd_scores(sourceinfo_t *si, int parc, char *parv[])
400 {
401 mowgli_node_t *n;
402 chanfix_channel_t *chan;
403 int i = 0;
404 unsigned int count = 20;
405
406 if (parv[0] == NULL)
407 {
408 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SCORES");
409 command_fail(si, fault_needmoreparams, _("To view CHANFIX scores for a channel: SCORES <#channel>"));
410 return;
411 }
412
413 if ((chan = chanfix_channel_find(parv[0])) == NULL)
414 {
415 command_fail(si, fault_nosuch_target, _("No CHANFIX record available for \2%s\2; try again later."),
416 parv[0]);
417 return;
418 }
419
420 /* sort records by score. */
421 mowgli_list_sort(&chan->oprecords, chanfix_compare_records, NULL);
422
423 if (count > MOWGLI_LIST_LENGTH(&chan->oprecords))
424 count = MOWGLI_LIST_LENGTH(&chan->oprecords);
425
426 if (count == 0)
427 {
428 command_success_nodata(si, _("There are no scores in the CHANFIX database for \2%s\2."), chan->name);
429 return;
430 }
431
432 command_success_nodata(si, _("Top \2%d\2 scores for \2%s\2 in the database:"), count, chan->name);
433
434 command_success_nodata(si, "%-3s %-50s %s", _("Num"), _("Account/Hostmask"), _("Score"));
435 command_success_nodata(si, "%-3s %-50s %s", "---", "--------------------------------------------------", "-----");
436
437 MOWGLI_ITER_FOREACH(n, chan->oprecords.head)
438 {
439 char buf[BUFSIZE];
440 unsigned int score;
441 chanfix_oprecord_t *orec = n->data;
442
443 score = chanfix_calculate_score(orec);
444
445 snprintf(buf, BUFSIZE, "%s@%s", orec->user, orec->host);
446
447 command_success_nodata(si, "%-3d %-50s %d", ++i, orec->entity ? orec->entity->name : buf, score);
448 }
449
450 command_success_nodata(si, "%-3s %-50s %s", "---", "--------------------------------------------------", "-----");
451 command_success_nodata(si, _("End of \2SCORES\2 listing for \2%s\2."), chan->name);
452 }
453
454 command_t cmd_scores = { "SCORES", N_("List channel scores."), PRIV_CHAN_AUSPEX, 1, chanfix_cmd_scores, { .path = "chanfix/scores" } };
455
456 static void chanfix_cmd_info(sourceinfo_t *si, int parc, char *parv[])
457 {
458 chanfix_oprecord_t *orec;
459 chanfix_channel_t *chan;
460 struct tm tm;
461 char strfbuf[BUFSIZE];
462 unsigned int highscore = 0;
463 metadata_t *md;
464
465 if (parv[0] == NULL)
466 {
467 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SCORES");
468 command_fail(si, fault_needmoreparams, _("To view CHANFIX scores for a channel: SCORES <#channel>"));
469 return;
470 }
471
472 if ((chan = chanfix_channel_find(parv[0])) == NULL)
473 {
474 command_fail(si, fault_nosuch_target, _("No CHANFIX record available for \2%s\2; try again later."),
475 parv[0]);
476 return;
477 }
478
479 /* sort records by score. */
480 mowgli_list_sort(&chan->oprecords, chanfix_compare_records, NULL);
481
482 command_success_nodata(si, _("Information on \2%s\2:"), chan->name);
483
484 tm = *localtime(&chan->ts);
485 strftime(strfbuf, sizeof strfbuf, TIME_FORMAT, &tm);
486
487 command_success_nodata(si, _("Creation time: %s"), strfbuf);
488
489 if (chan->oprecords.head != NULL)
490 {
491 orec = chan->oprecords.head->data;
492 highscore = chanfix_calculate_score(orec);
493 }
494
495 command_success_nodata(si, _("Highest score: \2%u\2"), highscore);
496 command_success_nodata(si, _("Usercount : \2%zu\2"), chan->chan ? MOWGLI_LIST_LENGTH(&chan->chan->members) : 0);
497 command_success_nodata(si, _("Initial step : \2%.0f%%\2 of \2%u\2 (\2%0.1f\2)"), CHANFIX_INITIAL_STEP * 100, highscore, (highscore * CHANFIX_INITIAL_STEP));
498 command_success_nodata(si, _("Current step : \2%u\2"), chanfix_get_threshold(chan));
499 command_success_nodata(si, _("Final step : \2%.0f%%\2 of \2%u\2 (\2%0.1f\2)"), CHANFIX_FINAL_STEP * 100, highscore, (highscore * CHANFIX_FINAL_STEP));
500 command_success_nodata(si, _("Needs fixing : \2%s\2"), chanfix_should_handle(chan, chan->chan) ? "YES" : "NO");
501 command_success_nodata(si, _("Now fixing : \2%s\2"),
502 chan->fix_started ? "YES" : "NO");
503
504 if ((md = metadata_find(chan, "private:mark:setter")) != NULL)
505 {
506 const char *setter = md->value;
507 const char *reason;
508 time_t ts;
509
510 md = metadata_find(chan, "private:mark:reason");
511 reason = md != NULL ? md->value : "unknown";
512
513 md = metadata_find(chan, "private:mark:timestamp");
514 ts = md != NULL ? atoi(md->value) : 0;
515
516 tm = *localtime(&ts);
517 strftime(strfbuf, sizeof strfbuf, TIME_FORMAT, &tm);
518
519 command_success_nodata(si, _("%s was \2MARKED\2 by %s on %s (%s)"), chan->name, setter, strfbuf, reason);
520 }
521
522 command_success_nodata(si, _("\2*** End of Info ***\2"));
523 }
524
525 command_t cmd_info = { "INFO", N_("List information on channel."), PRIV_CHAN_AUSPEX, 1, chanfix_cmd_info, { .path = "chanfix/info" } };
526
527 /* MARK <channel> ON|OFF [reason] */
528 static void chanfix_cmd_mark(sourceinfo_t *si, int parc, char *parv[])
529 {
530 char *target = parv[0];
531 char *action = parv[1];
532 char *info = parv[2];
533 chanfix_channel_t *chan;
534
535 if (!target || !action)
536 {
537 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "MARK");
538 command_fail(si, fault_needmoreparams, _("Usage: MARK <#channel> <ON|OFF> [note]"));
539 return;
540 }
541
542 if (target[0] != '#')
543 {
544 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "MARK");
545 return;
546 }
547
548 if ((chan = chanfix_channel_find(parv[0])) == NULL)
549 {
550 command_fail(si, fault_nosuch_target, _("No CHANFIX record available for \2%s\2; try again later."),
551 parv[0]);
552 return;
553 }
554
555 if (!strcasecmp(action, "ON"))
556 {
557 if (!info)
558 {
559 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "MARK");
560 command_fail(si, fault_needmoreparams, _("Usage: MARK <#channel> ON <note>"));
561 return;
562 }
563
564 if (metadata_find(chan, "private:mark:setter"))
565 {
566 command_fail(si, fault_nochange, _("\2%s\2 is already marked."), target);
567 return;
568 }
569
570 metadata_add(chan, "private:mark:setter", get_oper_name(si));
571 metadata_add(chan, "private:mark:reason", info);
572 metadata_add(chan, "private:mark:timestamp", number_to_string(CURRTIME));
573
574 logcommand(si, CMDLOG_ADMIN, "MARK:ON: \2%s\2 (reason: \2%s\2)", chan->name, info);
575 command_success_nodata(si, _("\2%s\2 is now marked."), target);
576 }
577 else if (!strcasecmp(action, "OFF"))
578 {
579 if (!metadata_find(chan, "private:mark:setter"))
580 {
581 command_fail(si, fault_nochange, _("\2%s\2 is not marked."), target);
582 return;
583 }
584
585 metadata_delete(chan, "private:mark:setter");
586 metadata_delete(chan, "private:mark:reason");
587 metadata_delete(chan, "private:mark:timestamp");
588
589 logcommand(si, CMDLOG_ADMIN, "MARK:OFF: \2%s\2", chan->name);
590 command_success_nodata(si, _("\2%s\2 is now unmarked."), target);
591 }
592 else
593 {
594 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "MARK");
595 command_fail(si, fault_badparams, _("Usage: MARK <#channel> <ON|OFF> [note]"));
596 }
597 }
598
599 command_t cmd_mark = { "MARK", N_("Adds a note to a channel."), PRIV_MARK, 3, chanfix_cmd_mark, { .path = "chanfix/mark" } };
600
601 /* HELP <command> [params] */
602 static void chanfix_cmd_help(sourceinfo_t *si, int parc, char *parv[])
603 {
604 char *command = parv[0];
605
606 if (!command)
607 {
608 command_success_nodata(si, _("***** \2%s Help\2 *****"), si->service->nick);
609 command_success_nodata(si, _("\2%s\2 allows for simple channel operator management."), si->service->nick);
610 command_success_nodata(si, " ");
611 command_success_nodata(si, _("For more information on a command, type:"));
612 command_success_nodata(si, "\2/%s%s help <command>\2", (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
613 command_success_nodata(si, " ");
614
615 command_help(si, si->service->commands);
616
617 command_success_nodata(si, _("***** \2End of Help\2 *****"));
618 return;
619 }
620
621 /* take the command through the hash table */
622 help_display(si, si->service, command, si->service->commands);
623 }
624
625 command_t cmd_help = { "HELP", N_(N_("Displays contextual help information.")), AC_NONE, 1, chanfix_cmd_help, { .path = "help" } };
626
627 void chanfix_can_register(hook_channel_register_check_t *req)
628 {
629 chanfix_channel_t *chan;
630 chanfix_oprecord_t *orec;
631 unsigned int highscore, score;
632
633 return_if_fail(req != NULL);
634
635 if (req->approved)
636 return;
637
638 chan = chanfix_channel_find(req->name);
639 if (chan == NULL)
640 return;
641 highscore = chanfix_get_highscore(chan);
642 if (highscore < CHANFIX_MIN_FIX_SCORE)
643 return;
644
645 if (req->si->su != NULL)
646 orec = chanfix_oprecord_find(chan, req->si->su);
647 else
648 orec = NULL;
649 score = orec != NULL ? chanfix_calculate_score(orec) : 0;
650
651 if (score < highscore * CHANFIX_FINAL_STEP)
652 {
653 /* If /msg chanfix chanfix would work, only allow users it
654 * could possibly op to register the channel.
655 */
656 if (has_priv(req->si, PRIV_CHAN_ADMIN))
657 {
658 slog(LG_INFO, "chanfix_can_register(): forced registration of %s by %s",
659 req->name,
660 req->si->smu != NULL ?
661 entity(req->si->smu)->name : "??");
662 return;
663 }
664 req->approved = 1;
665 command_fail(req->si, fault_noprivs, _("Your chanfix score is too low to register \2%s\2."), req->name);
666 }
667 }