]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/RootWarn.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[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 = &sqlSelect('rootwarn', 'attempt', { nick => $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 8))."T has landed!");
25         }
26
27         if ($_ = &getFactoid('root')) {
28             &msg($nick, "RootWarn: $attempt : $_");
29         } else {
30             &status('"root" needs to be defined in database.');
31         }
32
33     } elsif ($attempt < 2) {    # 2nd/3rd time occurrance.
34         if ($_ = &getFactoid('root again')) {
35             &status("RootWarn: not first time root user; msg'ing $nick.");
36             &msg($nick, "RootWarn: $attempt : $_");
37         } else {
38             &status('"root again" needs to be defined in database.');
39         }
40
41     } else {                    # >3rd time occurrance.
42         # disable this for the time being.
43         if (0 and $warnmode =~ /aggressive/i) {
44             if ($channels{$chan}{'o'}{$ident}) {
45                 &status("RootWarn: $nick... sigh... bye bye.");
46                 rawout("MODE $chan +b *!root\@$host");  # ban
47                 &kick($chan,$nick,'bye bye');
48             }
49         } elsif ($_ = &getFactoid('root again')) {
50             &status("RootWarn: $attempt times; msg'ing $nick.");
51             &msg($nick, "RootWarn: $attempt : $_");
52         } else {
53             &status("root again needs to be defined in database.");
54         }
55     }
56
57     $attempt++;
58     ### TODO: OPTIMIZE THIS.
59     # ok... don't record the attempt if nick==root.
60     return if ($nick eq 'root');
61
62     &sqlSet('rootwarn', { nick => lc($nick) }, {
63         attempt => $attempt,
64         time    => time(),
65         host    => $user."\@".$host,
66         channel => $chan,
67     } );
68
69     return;
70 }
71
72 # Extras function.
73 # TODO: support arguments to get info on a particular nick?
74 sub CmdrootWarn {
75     my $reply;
76     my $count = &countKeys('rootwarn');
77
78     if ($count == 0) {
79         &performReply("no-one has been warned about root, woohoo");
80         return;
81     }
82
83     # reply #1.
84     $reply = 'there '.&fixPlural('has',$count) ." been \002$count\002 ".
85                 &fixPlural('rooter',$count) ." warned about root.";
86
87     if ($param{'DBType'} !~ /^(pg|my)sql$/i) {
88         &FIXME("rootwarn does not yet support non-{my,pg}sql.");
89         return;
90     }
91
92     # reply #2.
93     $found = 0;
94     my $query = "SELECT attempt FROM rootwarn WHERE attempt > 2";
95     my $sth = $dbh->prepare($query);
96     $sth->execute;
97
98     while (my @row = $sth->fetchrow_array) {
99         $found++;
100     }
101
102     $sth->finish;
103
104     if ($found) {
105         $reply .= " Of which, \002$found\002 ".
106                 &fixPlural('rooter',$found).' '.
107                 &fixPlural('has',$found).
108                 " done it at least 3 times.";
109     }
110
111     &performStrictReply($reply);
112 }
113
114 1;
115
116 # vim:ts=4:sw=4:expandtab:tw=80