]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/IrcHelpers.pl
standard TODO: and FIXME:
[infobot.git] / src / IRC / IrcHelpers.pl
1 #
2 # IrcHooks.pl: IRC Hooks stuff.
3 #      Author: dms
4 #     Version: 20010413
5 #     Created: 20010413
6 #        NOTE: Based on code by Kevin Lenzo & Patrick Cole  (c) 1997
7 #
8
9 #######################################################################
10 ####### IRC HOOK HELPERS   IRC HOOK HELPERS   IRC HOOK HELPERS ########
11 #######################################################################
12
13 #####
14 # Usage: &hookMode($nick, $modes, @targets);
15 sub hookMode {
16     my ($nick, $modes, @targets) = @_;
17     my $parity  = 0;
18
19     if ($chan =~ tr/A-Z/a-z/) {
20         &VERB("hookMode: cased $chan.",2);
21     }
22
23     my $mode;
24     foreach $mode (split(//, $modes)) {
25         # sign.
26         if ($mode =~ /[-+]/) {
27             $parity = 1         if ($mode eq "+");
28             $parity = 0         if ($mode eq "-");
29             next;
30         }
31
32         # mode with target.
33         if ($mode =~ /[bklov]/) {
34             my $target = shift @targets;
35
36             if ($parity) {
37                 $chanstats{$chan}{'Op'}++    if ($mode eq "o");
38                 $chanstats{$chan}{'Ban'}++   if ($mode eq "b");
39             } else {
40                 $chanstats{$chan}{'Deop'}++  if ($mode eq "o");
41                 $chanstats{$chan}{'Unban'}++ if ($mode eq "b");
42             }
43
44             # modes w/ target affecting nick => cache it.
45             if ($mode =~ /[bov]/) {
46                 $channels{$chan}{$mode}{$target}++      if  $parity;
47                 delete $channels{$chan}{$mode}{$target} if !$parity;
48
49                 # lets do some custom stuff.
50                 if ($mode eq "o" and $parity) {
51                     if ($nick eq "ChanServ" and $target =~ /^\Q$ident\E$/i) {
52                         &VERB("hookmode: chanserv deopped us! asking",2);
53                         &chanServCheck($chan);
54                     }
55
56                     &chanLimitVerify($chan);
57                 }
58             }
59
60             if ($mode =~ /[l]/) {
61                 $channels{$chan}{$mode} = $target       if  $parity;
62                 delete $channels{$chan}{$mode}          if !$parity;
63             }
64         }
65
66         # important channel modes, targetless.
67         if ($mode =~ /[mt]/) {
68             $channels{$chan}{$mode}++                   if  $parity;
69             delete $channels{$chan}{$mode}              if !$parity;
70         }
71     }
72 }
73
74 sub hookMsg {
75     ($msgType, $chan, $who, $message) = @_;
76     my $skipmessage     = 0;
77     $addressed          = 0;
78     $addressedother     = 0;
79     $orig{message}      = $message;
80     $orig{who}          = $who;
81     $addrchar           = 0;
82
83     $message    =~ s/[\cA-\c_]//ig;     # strip control characters
84     $message    =~ s/^\s+//;            # initial whitespaces.
85     $who        =~ tr/A-Z/a-z/;         # lowercase.
86
87     &showProc();
88
89     # addressing.
90     if ($msgType =~ /private/) {
91         # private messages.
92         $addressed = 1;
93     } else {
94         # public messages.
95         # addressing revamped by the xk.
96         ### below needs to be fixed...
97         if (&IsParam("addressCharacter")) {
98             if ($message =~ s/^\Q$param{'addressCharacter'}\E//) {
99                 $addrchar  = 1;
100                 $addressed = 1;
101             }
102         }
103
104         if ($message =~ /^($mask{nick})([\;\:\>\, ]+) */) {
105             my $newmessage = $';
106             if ($1 =~ /^\Q$ident\E$/i) {
107                 $message   = $newmessage;
108                 $addressed = 1;
109             } else {
110                 # ignore messages addressed to other people or unaddressed.
111                 $skipmessage++ if ($2 ne "" and $2 !~ /^ /);
112             }
113         }
114     }
115
116     # Determine floodwho.
117     my $c       = "_default";
118     if ($msgType =~ /public/i) {
119         # public.
120         $floodwho = $c = lc $chan;
121     } elsif ($msgType =~ /private/i) {
122         # private.
123         $floodwho = lc $who;
124     } else {
125         # dcc?
126         &FIXME("floodwho = ???");
127     }
128
129     my $val = &getChanConfDefault("floodRepeat", "2:5", $c);
130     my ($count, $interval) = split /:/, $val;
131
132     # flood repeat protection.
133     if ($addressed) {
134         my $time = $flood{$floodwho}{$message} || 0;
135
136         if ($msgType eq "public" and (time() - $time < $interval)) {
137             ### public != personal who so the below is kind of pointless.
138             my @who;
139             foreach (keys %flood) {
140                 next if (/^\Q$floodwho\E$/);
141                 next if (defined $chan and /^\Q$chan\E$/);
142
143                 push(@who, grep /^\Q$message\E$/i, keys %{ $flood{$_} });
144             }
145
146             return if ($lobotomized);
147
148             if (!scalar @who) {
149                 push(@who,"Someone");
150             }
151             &msg($who,join(' ', @who)." already said that ". (time - $time) ." seconds ago" );
152
153             ### TODO: delete old floodwarn{} keys.
154             my $floodwarn = 0;
155             if (!exists $floodwarn{$floodwho}) {
156                 $floodwarn++;
157             } else {
158                 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
159             }
160
161             if ($floodwarn) {
162                 &status("FLOOD repetition detected from $floodwho.");
163                 $floodwarn{$floodwho} = time();
164             }
165
166             return;
167         }
168
169         if ($addrchar) {
170             &status("$b_cyan$who$ob is short-addressing me");
171         } elsif ($msgType eq "private") {       # private.
172             &status("$b_cyan$who$ob is /msg'ing me");
173         } else {                                # public?
174             &status("$b_cyan$who$ob is addressing me");
175         }
176
177         $flood{$floodwho}{$message} = time();
178     } elsif ($msgType eq "public" and &IsChanConf("kickOnRepeat")) {
179         # unaddressed, public only.
180
181         ### TODO: use a separate "short-time" hash.
182         my @data;
183         @data   = keys %{ $flood{$floodwho} } if (exists $flood{$floodwho});
184     }
185
186     $val = &getChanConfDefault("floodMessages", "5:30", $c);
187     ($count, $interval) = split /:/, $val;
188
189     # flood overflow protection.
190     if ($addressed) {
191         foreach (keys %{ $flood{$floodwho} }) {
192             next unless (time() - $flood{$floodwho}{$_} > $interval);
193             delete $flood{$floodwho}{$_};
194         }
195
196         my $i = scalar keys %{ $flood{$floodwho} };
197         if ($i > $count) {
198             my $expire = $param{'ignoreAutoExpire'} || 5;
199
200 #           &msg($who,"overflow of messages ($i > $count)");
201             &msg($who,"Too many queries from you, ignoring for $expire minutes.");
202             &status("FLOOD overflow detected from $floodwho; ignoring");
203
204             &ignoreAdd("*!$uh", $chan, $expire, "flood overflow auto-detected.");
205             return;
206         }
207
208         $flood{$floodwho}{$message} = time();
209     }
210
211     my @ignore;
212     if ($msgType =~ /public/i) {                    # public.
213         $talkchannel    = $chan;
214         &status("<$orig{who}/$chan> $orig{message}");
215         push(@ignore, keys %{ $ignore{$chan} }) if (exists $ignore{$chan});
216     } elsif ($msgType =~ /private/i) {             # private.
217         &status("[$orig{who}] $orig{message}");
218         $talkchannel    = undef;
219         $chan           = "_default";
220     } else {
221         &DEBUG("unknown msgType => $msgType.");
222     }
223     push(@ignore, keys %{ $ignore{"*"} }) if (exists $ignore{"*"});
224
225     if ((!$skipmessage or &IsChanConf("seenStoreAll") > 0) and
226         &IsChanConf("seen") > 0 and
227         $msgType =~ /public/
228     ) {
229         $seencache{$who}{'time'} = time();
230         $seencache{$who}{'nick'} = $orig{who};
231         $seencache{$who}{'host'} = $uh;
232         $seencache{$who}{'chan'} = $talkchannel;
233         $seencache{$who}{'msg'}  = $orig{message};
234         $seencache{$who}{'msgcount'}++;
235     }
236
237     return if ($skipmessage);
238     return unless (&IsParam("minVolunteerLength") or $addressed);
239
240     foreach (@ignore) {
241         s/\*/\\S*/g;
242
243         next unless (eval { $nuh =~ /^$_$/i } );
244
245         # better to ignore an extra message than to allow one to get
246         # through, although it would be better to go through ignore
247         # checking again.
248         if (time() - ($cache{ignoreCheckTime} || 0) > 60) {
249             &ignoreCheck();
250         }
251
252         &status("IGNORE <$who> $message");
253         return;
254     }
255
256     if (defined $nuh) {
257         if (!defined $userHandle) {
258             &DEBUG("line 1074: need verifyUser?");
259             &verifyUser($who, $nuh);
260         }
261     } else {
262         &DEBUG("hookMsg: 'nuh' not defined?");
263     }
264
265 ### For extra debugging purposes...
266     if ($_ = &process()) {
267 #       &DEBUG("IrcHooks: process returned '$_'.");
268     }
269
270     # hack to remove +o from ppl with +O flag.
271     if (exists $users{$userHandle} && exists $users{$userHandle}{FLAGS} &&
272         $users{$userHandle}{FLAGS} =~ /O/
273     ) {
274         $users{$userHandle}{FLAGS} =~ s/o//g;
275     }
276
277     return;
278 }
279
280 # this is basically run on on_join or on_quit
281 sub chanLimitVerify {
282     my($c)      = @_;
283     $chan       = $c;
284     my $l       = $channels{$chan}{'l'};
285
286     return unless (&IsChanConf("chanlimitcheck"));
287
288     if (scalar keys %netsplit) {
289         &WARN("clV: netsplit active (1, chan = $chan); skipping.");
290         return;
291     }
292
293     if (!defined $l) {
294         &DEBUG("$chan: running chanlimitCheck from chanLimitVerify.");
295         &chanlimitCheck();
296         return;
297     }
298
299     # only change it if it's not set.
300     my $plus  = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
301     my $count = scalar(keys %{ $channels{$chan}{''} });
302     my $int   = &getChanConfDefault("chanlimitcheckInterval", 10, $chan);
303
304     my $delta = $count + $plus - $l;
305 #   $delta    =~ s/^\-//;
306
307     if ($plus <= 3) {
308         &WARN("clc: stupid to have plus at $plus, fix it!");
309     }
310
311     if (exists $cache{chanlimitChange}{$chan}) {
312         if (time() - $cache{chanlimitChange}{$chan} < $int*60) {
313             return;
314         }
315     }
316
317     &chanServCheck($chan);
318
319     ### TODO: unify code with chanlimitcheck()
320     return if ($delta > 5);
321
322     &status("clc: big change in limit for $chan ($delta);".
323                 "going for it. (was: $l; now: ".($count+$plus).")");
324
325     $conn->mode($chan, "+l", $count+$plus);
326     $cache{chanlimitChange}{$chan} = time();
327 }
328
329 sub chanServCheck {
330     ($chan) = @_;
331
332     if (!defined $chan or $chan =~ /^\s*$/) {
333         &WARN("chanServCheck: chan == NULL.");
334         return 0;
335     }
336
337     if ($chan =~ tr/A-Z/a-z/) {
338         &DEBUG("chanServCheck: lowercased chan ($chan)");
339     }
340
341     if (! &IsChanConf("chanServ_ops") ) {
342         return 0;
343     }
344
345     &VERB("chanServCheck($chan) called.",2);
346
347     if ( &IsParam("nickServ_pass") and !$nickserv) {
348         $conn->who("NickServ");
349         return 0;
350     }
351
352     # check for first hash then for next hash.
353     # TODO: a function for &ischanop()? &isvoice()?
354     if (exists $channels{$chan} and exists $channels{$chan}{'o'}{$ident}) {
355         return 0;
356     }
357
358     &status("ChanServ ==> Requesting ops for $chan. (chanServCheck)");
359     &rawout("PRIVMSG ChanServ :OP $chan $ident");
360     return 1;
361 }
362
363 1;