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