2 # IrcHooks.pl: IRC Hooks stuff.
6 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
9 #######################################################################
10 ####### IRC HOOK HELPERS IRC HOOK HELPERS IRC HOOK HELPERS ########
11 #######################################################################
14 # Usage: &hookMode($nick, $modes, @targets);
16 my ($nick, $modes, @targets) = @_;
19 if ($chan =~ tr/A-Z/a-z/) {
20 &VERB("hookMode: cased $chan.",2);
24 foreach $mode (split(//, $modes)) {
26 if ($mode =~ /[-+]/) {
27 $parity = 1 if ($mode eq "+");
28 $parity = 0 if ($mode eq "-");
33 if ($mode =~ /[bklov]/) {
34 my $target = shift @targets;
37 $chanstats{$chan}{'Op'}++ if ($mode eq "o");
38 $chanstats{$chan}{'Ban'}++ if ($mode eq "b");
40 $chanstats{$chan}{'Deop'}++ if ($mode eq "o");
41 $chanstats{$chan}{'Unban'}++ if ($mode eq "b");
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;
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);
56 &chanLimitVerify($chan);
61 $channels{$chan}{$mode} = $target if $parity;
62 delete $channels{$chan}{$mode} if !$parity;
66 # important channel modes, targetless.
67 if ($mode =~ /[mt]/) {
68 $channels{$chan}{$mode}++ if $parity;
69 delete $channels{$chan}{$mode} if !$parity;
75 ($msgType, $chan, $who, $message) = @_;
79 $orig{message} = $message;
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();
91 if ($msgType =~ /private/) {
96 # addressing revamped by the xk.
97 ### below needs to be fixed...
98 if (&IsParam("addressCharacter")) {
99 if ($message =~ s/^\Q$param{'addressCharacter'}\E//) {
105 if ($message =~ /^($mask{nick})([\;\:\>\, ]+) */) {
107 if ($1 =~ /^\Q$mynick\E$/i) {
108 $message = $newmessage;
111 # ignore messages addressed to other people or unaddressed.
112 $skipmessage++ if ($2 ne "" and $2 !~ /^ /);
117 # Determine floodwho.
119 if ($msgType =~ /public/i) {
121 $floodwho = $c = lc $chan;
122 } elsif ($msgType =~ /private/i) {
127 &FIXME("floodwho = ???");
130 my $val = &getChanConfDefault("floodRepeat", "2:5", $c);
131 my ($count, $interval) = split /:/, $val;
133 # flood repeat protection.
135 my $time = $flood{$floodwho}{$message} || 0;
137 if ($msgType eq "public" and (time() - $time < $interval)) {
138 ### public != personal who so the below is kind of pointless.
140 foreach (keys %flood) {
141 next if (/^\Q$floodwho\E$/);
142 next if (defined $chan and /^\Q$chan\E$/);
144 push(@who, grep /^\Q$message\E$/i, keys %{ $flood{$_} });
147 return if ($lobotomized);
150 push(@who,"Someone");
152 &msg($who,join(' ', @who)." already said that ". (time - $time) ." seconds ago" );
154 ### TODO: delete old floodwarn{} keys.
156 if (!exists $floodwarn{$floodwho}) {
159 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
163 &status("FLOOD repetition detected from $floodwho.");
164 $floodwarn{$floodwho} = time();
171 &status("$b_cyan$who$ob is short-addressing $mynick");
172 } elsif ($msgType eq "private") { # private.
173 &status("$b_cyan$who$ob is /msg'ing $mynick");
175 &status("$b_cyan$who$ob is addressing $mynick");
178 $flood{$floodwho}{$message} = time();
179 } elsif ($msgType eq "public" and &IsChanConf("kickOnRepeat")) {
180 # unaddressed, public only.
182 ### TODO: use a separate "short-time" hash.
184 @data = keys %{ $flood{$floodwho} } if (exists $flood{$floodwho});
187 $val = &getChanConfDefault("floodMessages", "5:30", $c);
188 ($count, $interval) = split /:/, $val;
190 # flood overflow protection.
192 foreach (keys %{ $flood{$floodwho} }) {
193 next unless (time() - $flood{$floodwho}{$_} > $interval);
194 delete $flood{$floodwho}{$_};
197 my $i = scalar keys %{ $flood{$floodwho} };
199 my $expire = $param{'ignoreAutoExpire'} || 5;
201 # &msg($who,"overflow of messages ($i > $count)");
202 &msg($who,"Too many queries from you, ignoring for $expire minutes.");
203 &status("FLOOD overflow detected from $floodwho; ignoring");
205 &ignoreAdd("*!$uh", $chan, $expire, "flood overflow auto-detected.");
209 $flood{$floodwho}{$message} = time();
213 if ($msgType =~ /public/i) { # public.
214 $talkchannel = $chan;
215 &status("<$orig{who}/$chan> $orig{message}");
216 push(@ignore, keys %{ $ignore{$chan} }) if (exists $ignore{$chan});
217 } elsif ($msgType =~ /private/i) { # private.
218 &status("[$orig{who}] $orig{message}");
219 $talkchannel = undef;
222 &DEBUG("unknown msgType => $msgType.");
224 push(@ignore, keys %{ $ignore{"*"} }) if (exists $ignore{"*"});
226 if ((!$skipmessage or &IsChanConf("seenStoreAll") > 0) and
227 &IsChanConf("seen") > 0 and
230 $seencache{$who}{'time'} = time();
231 $seencache{$who}{'nick'} = $orig{who};
232 $seencache{$who}{'host'} = $uh;
233 $seencache{$who}{'chan'} = $talkchannel;
234 $seencache{$who}{'msg'} = $orig{message};
235 $seencache{$who}{'msgcount'}++;
237 if (&IsChanConf("minVolunteerLength")) {
238 # FIXME hack to treat unaddressed as if using addrchar
241 return if ($skipmessage);
242 return unless ($addrchar or $addressed);
247 next unless (eval { $nuh =~ /^$_$/i } );
249 # better to ignore an extra message than to allow one to get
250 # through, although it would be better to go through ignore
252 if (time() - ($cache{ignoreCheckTime} || 0) > 60) {
256 &status("IGNORE <$who> $message");
261 if (!defined $userHandle) {
262 &DEBUG("line 1074: need verifyUser?");
263 &verifyUser($who, $nuh);
266 &DEBUG("hookMsg: 'nuh' not defined?");
269 ### For extra debugging purposes...
270 if ($_ = &process()) {
271 # &DEBUG("IrcHooks: process returned '$_'.");
274 # hack to remove +o from ppl with +O flag.
275 if (exists $users{$userHandle} && exists $users{$userHandle}{FLAGS} &&
276 $users{$userHandle}{FLAGS} =~ /O/
278 $users{$userHandle}{FLAGS} =~ s/o//g;
284 # this is basically run on on_join or on_quit
285 sub chanLimitVerify {
288 my $l = $channels{$chan}{'l'};
290 return unless (&IsChanConf("chanlimitcheck"));
292 if (scalar keys %netsplit) {
293 &WARN("clV: netsplit active (1, chan = $chan); skipping.");
298 &DEBUG("$chan: running chanlimitCheck from chanLimitVerify.");
303 # only change it if it's not set.
304 my $plus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
305 my $count = scalar(keys %{ $channels{$chan}{''} });
306 my $int = &getChanConfDefault("chanlimitcheckInterval", 10, $chan);
308 my $delta = $count + $plus - $l;
312 &WARN("clc: stupid to have plus at $plus, fix it!");
315 if (exists $cache{chanlimitChange}{$chan}) {
316 if (time() - $cache{chanlimitChange}{$chan} < $int*60) {
321 &chanServCheck($chan);
323 ### TODO: unify code with chanlimitcheck()
324 return if ($delta > 5);
326 &status("clc: big change in limit for $chan ($delta);".
327 "going for it. (was: $l; now: ".($count+$plus).")");
329 $conn->mode($chan, "+l", $count+$plus);
330 $cache{chanlimitChange}{$chan} = time();
336 if (!defined $chan or $chan =~ /^\s*$/) {
337 &WARN("chanServCheck: chan == NULL.");
341 if ($chan =~ tr/A-Z/a-z/) {
342 &DEBUG("chanServCheck: lowercased chan ($chan)");
345 if (! &IsChanConf("chanServ_ops") ) {
349 &VERB("chanServCheck($chan) called.",2);
351 if ( &IsParam("nickServ_pass") and !$nickserv) {
352 $conn->who("NickServ");
356 # check for first hash then for next hash.
357 # TODO: a function for &ischanop()? &isvoice()?
358 if (exists $channels{$chan} and exists $channels{$chan}{'o'}{$ident}) {
362 &status("ChanServ ==> Requesting ops for $chan. (chanServCheck)");
363 &rawout("PRIVMSG ChanServ :OP $chan $ident");