]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/IrcHelpers.pl
- ircTextCounters stuff moved into a separate function.
[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) {                # public.
119         $floodwho = $c = lc $chan;
120     } elsif ($msgType =~ /private/i) {  # private.
121         $floodwho = lc $who;
122     } else {                            # dcc?
123         &DEBUG("FIXME: floodwho = ???");
124     }
125
126     my $val = &getChanConfDefault("floodRepeat", "2:5", $c);
127     my ($count, $interval) = split /:/, $val;
128
129     # flood repeat protection.
130     if ($addressed) {
131         my $time = $flood{$floodwho}{$message} || 0;
132
133         if ($msgType eq "public" and (time() - $time < $interval)) {
134             ### public != personal who so the below is kind of pointless.
135             my @who;
136             foreach (keys %flood) {
137                 next if (/^\Q$floodwho\E$/);
138                 next if (defined $chan and /^\Q$chan\E$/);
139
140                 push(@who, grep /^\Q$message\E$/i, keys %{ $flood{$_} });
141             }
142
143             return if ($lobotomized);
144
145             if (scalar @who) {
146                 &msg($who, "you already said what ".
147                                 join(' ', @who)." have said.");
148             } else {
149                 &msg($who,"Someone already said that ". (time - $time) ." seconds ago" );
150             }
151
152             ### TODO: delete old floodwarn{} keys.
153             my $floodwarn = 0;
154             if (!exists $floodwarn{$floodwho}) {
155                 $floodwarn++;
156             } else {
157                 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
158             }
159
160             if ($floodwarn) {
161                 &status("FLOOD repetition detected from $floodwho.");
162                 $floodwarn{$floodwho} = time();
163             }
164
165             return;
166         }
167
168         if ($addrchar) {
169             &status("$b_cyan$who$ob is short-addressing me");
170         } elsif ($msgType eq "private") {       # private.
171             &status("$b_cyan$who$ob is /msg'ing me");
172         } else {                                # public?
173             &status("$b_cyan$who$ob is addressing me");
174         }
175
176         $flood{$floodwho}{$message} = time();
177     } elsif ($msgType eq "public" and &IsChanConf("kickOnRepeat")) {
178         # unaddressed, public only.
179
180         ### TODO: use a separate "short-time" hash.
181         my @data;
182         @data   = keys %{ $flood{$floodwho} } if (exists $flood{$floodwho});
183     }
184
185     $val = &getChanConfDefault("floodMessages", "5:30", $c);
186     ($count, $interval) = split /:/, $val;
187
188     # flood overflow protection.
189     if ($addressed) {
190         foreach (keys %{ $flood{$floodwho} }) {
191             next unless (time() - $flood{$floodwho}{$_} > $interval);
192             delete $flood{$floodwho}{$_};
193         }
194
195         my $i = scalar keys %{ $flood{$floodwho} };
196         if ($i > $count) {
197             my $expire = $param{'ignoreAutoExpire'} || 5;
198
199 #           &msg($who,"overflow of messages ($i > $count)");
200             &msg($who,"Too many queries from you, ignoring for $expire minutes.");
201             &status("FLOOD overflow detected from $floodwho; ignoring");
202
203             &ignoreAdd("*!$uh", $chan, $expire, "flood overflow auto-detected.");
204             return;
205         }
206
207         $flood{$floodwho}{$message} = time();
208     }
209
210     my @ignore;
211     if ($msgType =~ /public/i) {                    # public.
212         $talkchannel    = $chan;
213         &status("<$orig{who}/$chan> $orig{message}");
214         push(@ignore, keys %{ $ignore{$chan} }) if (exists $ignore{$chan});
215     } elsif ($msgType =~ /private/i) {             # private.
216         &status("[$orig{who}] $orig{message}");
217         $talkchannel    = undef;
218         $chan           = "_default";
219     } else {
220         &DEBUG("unknown msgType => $msgType.");
221     }
222     push(@ignore, keys %{ $ignore{"*"} }) if (exists $ignore{"*"});
223
224     if ((!$skipmessage or &IsChanConf("seenStoreAll")) and
225         &IsChanConf("seen") and
226         $msgType =~ /public/
227     ) {
228         $seencache{$who}{'time'} = time();
229         $seencache{$who}{'nick'} = $orig{who};
230         $seencache{$who}{'host'} = $uh;
231         $seencache{$who}{'chan'} = $talkchannel;
232         $seencache{$who}{'msg'}  = $orig{message};
233         $seencache{$who}{'msgcount'}++;
234     }
235
236     return if ($skipmessage);
237     return unless (&IsParam("minVolunteerLength") or $addressed);
238
239     foreach (@ignore) {
240         s/\*/\\S*/g;
241
242         next unless (eval { $nuh =~ /^$_$/i } );
243
244         # better to ignore an extra message than to allow one to get
245         # through, although it would be better to go through ignore
246         # checking again.
247         if (time() - ($cache{ignoreCheckTime} || 0) > 60) {
248             &ignoreCheck();
249         }
250
251         &status("IGNORE <$who> $message");
252         return;
253     }
254
255     if (defined $nuh) {
256         if (!defined $userHandle) {
257             &DEBUG("line 1074: need verifyUser?");
258             &verifyUser($who, $nuh);
259         }
260     } else {
261         &DEBUG("hookMsg: 'nuh' not defined?");
262     }
263
264 ### For extra debugging purposes...
265     if ($_ = &process()) {
266 #       &DEBUG("IrcHooks: process returned '$_'.");
267     }
268
269     # hack to remove +o from ppl with +O flag.
270     if (exists $users{$userHandle} && exists $users{$userHandle}{FLAGS} &&
271         $users{$userHandle}{FLAGS} =~ /O/
272     ) {
273         $users{$userHandle}{FLAGS} =~ s/o//g;
274     }
275
276     return;
277 }
278
279 # this is basically run on on_join or on_quit
280 sub chanLimitVerify {
281     my($c)      = @_;
282     $chan       = $c;
283     my $l       = $channels{$chan}{'l'};
284
285     return unless (&IsChanConf("chanlimitcheck"));
286
287     if (scalar keys %netsplit) {
288         &WARN("clV: netsplit active (1, chan = $chan); skipping.");
289         return;
290     }
291
292     if (!defined $l) {
293         &DEBUG("$chan: running chanlimitCheck from chanLimitVerify.");
294         &chanlimitCheck();
295         return;
296     }
297
298     # only change it if it's not set.
299     my $plus  = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
300     my $count = scalar(keys %{ $channels{$chan}{''} });
301     my $int   = &getChanConfDefault("chanlimitcheckInterval", 10, $chan);
302
303     my $delta = $count + $plus - $l;
304 #   $delta    =~ s/^\-//;
305
306     if ($plus <= 3) {
307         &WARN("clc: stupid to have plus at $plus, fix it!");
308     }
309
310     if (exists $cache{chanlimitChange}{$chan}) {
311         if (time() - $cache{chanlimitChange}{$chan} < $int*60) {
312             return;
313         }
314     }
315
316     &chanServCheck($chan);
317
318     ### todo: unify code with chanlimitcheck()
319     return if ($delta > 5);
320
321     &status("clc: big change in limit for $chan ($delta);".
322                 "going for it. (was: $l; now: ".($count+$plus).")");
323
324     $conn->mode($chan, "+l", $count+$plus);
325     $cache{chanlimitChange}{$chan} = time();
326 }
327
328 sub chanServCheck {
329     ($chan) = @_;
330
331     if (!defined $chan or $chan =~ /^\s*$/) {
332         &WARN("chanServCheck: chan == NULL.");
333         return 0;
334     }
335
336     if ($chan =~ tr/A-Z/a-z/) {
337         &DEBUG("chanServCheck: lowercased chan ($chan)");
338     }
339
340     if (! &IsChanConf("chanServ_ops") ) {
341         return 0;
342     }
343
344     &VERB("chanServCheck($chan) called.",2);
345
346     if ( &IsParam("nickServ_pass") and !$nickserv) {
347         &DEBUG("chanServ_ops($chan): nickserv enabled but not alive? (ircCheck)");
348         $conn->who("NickServ");
349         return 0;
350     }
351     # check for first hash then for next hash.
352     # todo: a function for &ischanop()? &isvoice()?
353     if (exists $channels{$chan} and exists $channels{$chan}{'o'}{$ident}) {
354         return 0;
355     }
356
357     &status("ChanServ ==> Requesting ops for $chan. (chanServCheck)");
358     &rawout("PRIVMSG ChanServ :OP $chan $ident");
359     return 1;
360 }
361
362 1;