]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/RootWarn.pl
cb3d967499637c15df3185a86e56ddd85b3cfe13
[infobot.git] / src / Modules / RootWarn.pl
1 #
2 # RootWarn.pl: Warn people about usage of root on IRC.
3 #      Author: dms
4 #     Version: v0.3 (20000923)
5 #     Created: 19991008
6 #
7
8 use strict;
9
10 use vars qw(%channels %param);
11 use vars qw($dbh $found $ident);
12
13 sub rootWarn {
14     my ($nick,$user,$host,$chan) = @_;
15     my $n       = lc $nick;
16     my $attempt = &dbGet("rootwarn", "attempt", "nick=".&dbQuote($n) ) || 0;
17     my $warnmode        = &getChanConf("rootWarnMode");
18
19     if ($attempt == 0) {        # first timer.
20         if (defined $warnmode and $warnmode =~ /quiet/i) {
21             &status("rootwarn: Detected root user; notifying user");
22         } else {
23             &status("rootwarn: Detected root user; notifying nick and channel.");
24             &msg($chan, "ROO".("O" x int(rand 68))."T has landed!");
25         }
26
27         if ($_ = &getFactoid("root")) {
28             &msg($nick, "root is $_");
29         } else {
30             &status("root needs to be defined in database.");
31         }
32
33     } elsif ($attempt < 2) {    # 2nd/3rd time occurrance.
34         &status("RootWarn: not first time root user; msg'ing $nick.");
35         if ($_ = &getFactoid("root again")) {
36             &msg($nick, $_);
37         } else {
38             &status("root again needs to be defined in database.");
39         }
40
41     } else {                    # >3rd time occurrance.
42
43         # disable this for the time being.
44         if (0 and $warnmode =~ /aggressive/i) {
45             if ($channels{$chan}{'o'}{$ident}) {
46                 &status("RootWarn: $nick... sigh... bye bye.");
47                 rawout("MODE $chan +b *!root\@$host");  # ban
48                 &kick($chan,$nick,"bye bye");
49             }
50         }
51     }
52
53     $attempt++;
54     ### TODO: OPTIMIZE THIS.
55     # ok... don't record the attempt if nick==root.
56     return if ($nick eq "root");
57
58     &dbSet("rootwarn", { nick => lc($nick) }, {
59         attempt => $attempt,
60         time    => time(),
61         host    => $user."\@".$host,
62         channel => $chan,
63     } );
64
65     return;
66 }
67
68 # Extras function.
69 # todo: support arguments to get info on a particular nick?
70 sub CmdrootWarn {
71     my $reply;
72     my $count = &countKeys("rootwarn");
73
74     if ($count == 0) {
75         &performReply("no-one has been warned about root, woohoo");
76         return;
77     }
78
79     # reply #1.
80     $reply = "there ".&fixPlural("has",$count) ." been \002$count\002 ".
81                 &fixPlural("rooter",$count) ." warned about root.";
82
83     if ($param{'DBType'} !~ /^(pg|my)sql$/i) {
84         &FIXME("rootwarn does not yet support non-{my,pg}sql.");
85         return;
86     }
87
88     # reply #2.
89     $found = 0;
90     my $query = "SELECT attempt FROM rootwarn WHERE attempt > 2";
91     my $sth = $dbh->prepare($query);
92     $sth->execute;
93
94     while (my @row = $sth->fetchrow_array) {
95         $found++;
96     }
97
98     $sth->finish;
99
100     if ($found) {
101         $reply .= " Of which, \002$found\002 ".
102                 &fixPlural("rooter",$found)." ".
103                 &fixPlural("has",$found).
104                 " done it at least 3 times.";
105     }
106
107     &performStrictReply($reply);
108 }
109
110 1;