X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FIRC%2FIrc.pl;h=ad557ae6cc9dd8ec75037aedc794193fada20894;hb=f7cae48a17d6decd0a9bd997188271daa0a885b1;hp=49c931db836df443719adcb48033078e654f3722;hpb=f31097ab45dca4b19e255801c966e9cd552eece4;p=infobot.git diff --git a/src/IRC/Irc.pl b/src/IRC/Irc.pl index 49c931d..ad557ae 100644 --- a/src/IRC/Irc.pl +++ b/src/IRC/Irc.pl @@ -5,20 +5,20 @@ # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997 # -if (&IsParam("useStrict")) { use strict; } +use strict; +no strict 'refs'; + +use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask + %chanconf %orig %ircPort %ircstats %last %netsplit); +use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel + $addressed); +use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize + $pubcount $pubtime $pubsize); +use vars qw($b_blue $ob); +use vars qw(@joinchan @ircServers); -use vars qw($nickserv); $nickserv = 0; -# static scalar variables. -$mask{ip} = '(\d+)\.(\d+)\.(\d+)\.(\d+)'; -$mask{host} = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+'; -$mask{chan} = '[\#\&]\S*|_default'; -my $isnick1 = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\'; -my $isnick2 = '0-9\-'; -$mask{nick} = "[$isnick1]{1}[$isnick1$isnick2]*"; -$mask{nuh} = '\S*!\S*\@\S*'; - sub ircloop { my $error = 0; my $lastrun = 0; @@ -97,11 +97,16 @@ sub irc { Ircname => $param{'ircName'}, ); $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'}); + $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'}); $conn = $irc->newconn(%args); if (!defined $conn) { - &ERROR("irc: conn was not created!defined!!!"); + &ERROR("IRC: connection failed."); + &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work"); + &ERROR("Please check /etc/hosts to see if you have a localhost line like:"); + &ERROR("127.0.0.1 localhost localhost"); + &ERROR("If this is still a problem, please contact the maintainer."); return 1; } @@ -135,12 +140,16 @@ sub irc { $conn->add_handler('nick', \&on_nick); $conn->add_handler('quit', \&on_quit); $conn->add_handler('notice', \&on_notice); - $conn->add_handler('whoisuser', \&on_whoisuser); + $conn->add_handler('whoischannels', \&on_whoischannels); + $conn->add_handler('useronchannel', \&on_useronchannel); + $conn->add_handler('whois', \&on_whois); $conn->add_handler('other', \&on_other); $conn->add_global_handler('disconnect', \&on_disconnect); $conn->add_global_handler([251,252,253,254,255], \&on_init); ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init); + $conn->add_global_handler(303, \&on_ison); # notify. $conn->add_global_handler(315, \&on_endofwho); + $conn->add_global_handler(422, \&on_endofwho); # nomotd. $conn->add_global_handler(324, \&on_modeis); $conn->add_global_handler(333, \&on_topicinfo); $conn->add_global_handler(352, \&on_who); @@ -154,6 +163,7 @@ sub irc { $conn->add_global_handler(473, \&on_inviteonly); $conn->add_global_handler(474, \&on_banned); $conn->add_global_handler(475, \&on_badchankey); + $conn->add_global_handler(443, \&on_useronchan); # end of handler stuff. @@ -201,11 +211,9 @@ sub say { if ( ($pubcount % $i) == 0 and $pubcount) { sleep 1; - $pubsleep++; } elsif ($pubsize > $j) { sleep 1; $pubsize -= $j; - $pubsleep++; } } else { @@ -233,32 +241,29 @@ sub msg { &status(">$nick< $msg"); - if (&whatInterface() =~ /IRC/) { - my $t = time(); - - if ($t == $msgtime) { - $msgcount++; - $msgsize += length $msg; + return unless (&whatInterface() =~ /IRC/); + my $t = time(); - my $i = &getChanConfDefault("sendPrivateLimitLines", 3); - my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000); - if ( ($msgcount % $i) == 0 and $msgcount) { - sleep 1; - $msgsleep++; - } elsif ($msgsize > $j) { - sleep 1; - $msgsize -= $j; - $msgsleep++; - } + if ($t == $msgtime) { + $msgcount++; + $msgsize += length $msg; - } else { - $msgcount = 0; - $msgtime = $t; - $msgsize = length $msg; + my $i = &getChanConfDefault("sendPrivateLimitLines", 3); + my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000); + if ( ($msgcount % $i) == 0 and $msgcount) { + sleep 1; + } elsif ($msgsize > $j) { + sleep 1; + $msgsize -= $j; } - $conn->privmsg($nick, $msg); + } else { + $msgcount = 0; + $msgtime = $t; + $msgsize = length $msg; } + + $conn->privmsg($nick, $msg); } # Usage: &action(nick || chan, txt); @@ -269,16 +274,13 @@ sub action { return; } - my $rawout = "PRIVMSG $target :\001ACTION $txt\001"; - if (length $rawout > 510) { + if (length $txt > 480) { &status("action: txt too long; truncating."); - - chop($rawout) while (length($rawout) > 510); - $rawout .= "\001"; + chop($txt) while (length $txt > 480); } &status("* $ident/$target $txt"); - rawout($rawout); + $conn->me($target, $txt); } # Usage: ¬ice(nick || chan, txt); @@ -302,11 +304,9 @@ sub notice { if ( ($notcount % $i) == 0 and $notcount) { sleep 1; - $notsleep++; } elsif ($notsize > $j) { sleep 1; $notsize -= $j; - $notsleep++; } } else { @@ -335,6 +335,12 @@ sub DCCBroadcast { # Usage: &performReply($reply); sub performReply { my ($reply) = @_; + + if (!defined $reply or $reply =~ /^\s*$/) { + &DEBUG("performReply: reply == NULL."); + return; + } + $reply =~ /([\.\?\s]+)$/; &checkMsgType($reply); @@ -346,19 +352,20 @@ sub performReply { $reply = "$reply, ".$orig{who}; } &say($reply); + } elsif ($msgType eq 'private') { - if (rand() < 0.5) { - $reply = $reply; - } else { + if (rand() > 0.5) { $reply = "$reply, ".$orig{who}; } &msg($who, $reply); + } elsif ($msgType eq 'chat') { if (!exists $dcc{'CHAT'}{$who}) { - &WARN("pSR: dcc{'CHAT'}{$who} does not exist."); + &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2); return; } $conn->privmsg($dcc{'CHAT'}{$who}, $reply); + } else { &ERROR("PR: msgType invalid? ($msgType)."); } @@ -385,17 +392,23 @@ sub performStrictReply { } elsif ($msgType eq 'public') { &say($reply); } elsif ($msgType eq 'chat') { - &dccsay(lc $who,$reply); + &dccsay(lc $who, $reply); } else { &ERROR("pSR: msgType invalid? ($msgType)."); } } sub dccsay { - my ($who, $reply) = @_; + my($who, $reply) = @_; + + if (!defined $reply or $reply =~ /^\s*$/) { + &WARN("dccsay: reply == NULL."); + return; + } + if (!exists $dcc{'CHAT'}{$who}) { - &WARN("pSR: dcc{'CHAT'}{$who} does not exist."); - return ''; + &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2); + return; } &status("=>$who<= $reply"); # dcc chat. @@ -411,26 +424,28 @@ sub dcc_close { my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} }; next unless (scalar @who); $who = $who[0]; + &DEBUG("dcc_close... close $who!"); } } sub joinchan { - my ($chankey) = @_; - my $chan = lc $chankey; - - if ($chankey =~ s/^($mask{chan}),\S+/ /) { - $chan = lc $1; - } + my ($chan) = @_; + my $key = &getChanConf("chankey", $chan) || ""; - &status("joining $b_blue$chan$ob"); + # forgot for about 2 years to implement channel keys when moving + # over to Net::IRC... + # hopefully validChan is right. if (&validChan($chan)) { &status("join: already on $chan"); } else { - if (!$conn->join($chan)) { - &DEBUG("joinchan: join failed. trying connect!"); - $conn->connect(); - } + &status("joining $b_blue$chan$ob"); + + return if ($conn->join($chan, $key)); + + &DEBUG("joinchan: join failed. trying connect!"); + &clearIRCVars(); + $conn->connect(); } } @@ -441,13 +456,18 @@ sub part { next if ($chan eq ""); $chan =~ tr/A-Z/a-z/; # lowercase. + if ($chan !~ /^$mask{chan}$/) { + &WARN("part: chan is invalid ($chan)"); + next; + } + &status("parting $chan"); if (!&validChan($chan)) { &WARN("part: not on $chan; doing anyway"); # next; } - rawout("PART $chan"); + $conn->part($chan); # deletion of $channels{chan} is done in &entryEvt(). } } @@ -463,6 +483,7 @@ sub mode { &DEBUG("mode: MODE $chan $modes"); + # should move to use Net::IRC's $conn->mode()... but too lazy. rawout("MODE $chan $modes"); } @@ -493,21 +514,17 @@ sub kick { foreach $chan (@chans) { if (!&IsNickInChan($nick,$chan)) { - &status("Kick: $nick is not on $chan.") if (scalar @chans == 1); + &status("kick: $nick is not on $chan.") if (scalar @chans == 1); next; } if (!exists $channels{$chan}{o}{$ident}) { - &status("Kick: do not have ops on $chan :("); + &status("kick: do not have ops on $chan :("); next; } &status("Kicking $nick from $chan."); - if ($msg eq "") { - &rawout("KICK $chan $nick"); - } else { - &rawout("KICK $chan $nick :$msg"); - } + $conn->kick($chan, $nick, $msg); } } @@ -523,7 +540,7 @@ sub ban { foreach $chan (@chans) { if (!exists $channels{$chan}{o}{$ident}) { - &status("Ban: do not have ops on $chan :("); + &status("ban: do not have ops on $chan :("); next; } @@ -576,6 +593,7 @@ sub nick { if (defined $ident and $nick eq $ident) { &WARN("nick: nick == ident == '$ident'."); + return; } my $bad = 0; @@ -584,15 +602,18 @@ sub nick { if ($bad) { &WARN("Nick: not going to try and get my nick back. [". - scalar(localtime). "]"); - return; + scalar(gmtime). "]"); +# hrm... over time we lose track of our own nick. +# return; } if ($nick =~ /^$mask{nick}$/) { - rawout("NICK ".$nick); + &rawout("NICK ".$nick); if (defined $ident) { &status("nick: Changing nick to $nick (from $ident)"); + # following shouldn't be here :( + $ident = $nick; } else { &DEBUG("first time nick change."); $ident = $nick; @@ -607,9 +628,10 @@ sub nick { sub invite { my($who, $chan) = @_; - rawout("INVITE $who $chan"); -} + # todo: check if $who or $chan are invalid. + $conn->invite($who, $chan); +} ########## # Channel related functions... @@ -618,7 +640,7 @@ sub invite { # Usage: &joinNextChan(); sub joinNextChan { if (scalar @joinchan) { - $chan = shift @joinchan; + my $chan = shift @joinchan; &joinchan($chan); if (my $i = scalar @joinchan) { @@ -626,7 +648,6 @@ sub joinNextChan { } return; - } # !scalar @joinchan: @@ -637,7 +658,7 @@ sub joinNextChan { my $rate = sprintf("%.1f", $delta / @c); delete $cache{joinTime}; - &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join"); + &status("time taken to join all chans: $timestr; rate: $rate sec/join"); } # chanserv check: global channels, in case we missed one. @@ -672,6 +693,11 @@ sub IsNickInChan { $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately. + if ($chan =~ /^$/) { + &DEBUG("INIC: chan == NULL."); + return 0; + } + if (&validChan($chan) == 0) { &ERROR("INIC: invalid channel $chan."); return 0; @@ -690,6 +716,7 @@ sub IsNickInChan { sub IsNickInAnyChan { my ($nick) = @_; + my $chan; foreach $chan (keys %channels) { next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }); @@ -700,14 +727,25 @@ sub IsNickInAnyChan { # Usage: &validChan($chan); sub validChan { + # todo: use $c instead? my ($chan) = @_; + if (!defined $chan or $chan =~ /^\s*$/) { + return 0; + } + if (lc $chan ne $chan) { &WARN("validChan: lc chan != chan. ($chan); fixing."); $chan =~ tr/A-Z/a-z/; } - if (exists $channels{$chan}) { + # it's possible that this check creates the hash if empty. + if (defined $channels{$chan} or exists $channels{$chan}) { + if ($chan =~ /^_?default$/) { +# &WARN("validC: chan cannot be _default! returning 0!"); + return 0; + } + return 1; } else { return 0; @@ -737,7 +775,6 @@ sub clearChanVars { } sub clearIRCVars { -# &DEBUG("clearIRCVars() called!"); undef %channels; undef %floodjoin; @@ -783,21 +820,25 @@ sub getJoinChans { } sub closeDCC { -### &DEBUG("closeDCC called."); +# &DEBUG("closeDCC called."); + my $type; foreach $type (keys %dcc) { next if ($type ne uc($type)); - + + my $nick; foreach $nick (keys %{ $dcc{$type} }) { next unless (defined $nick); - &DEBUG("closing DCC $type to $nick (FIXME)."); + &status("DCC CHAT: closing DCC $type to $nick."); next unless (defined $dcc{$type}{$nick}); my $ref = $dcc{$type}{$nick}; - &DEBUG("ref => $ref"); - -# $dcc{$type}{$nick}->close(); + &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i); + $dcc{$type}{$nick}->close(); + delete $dcc{$type}{$nick}; + &DEBUG("after close for $nick"); } + delete $dcc{$type}; } } @@ -826,7 +867,10 @@ sub joinfloodCheck { my $time; foreach (keys %{ $floodjoin{$c} }) { - $time += $floodjoin{$c}{$_}{Time}; + my $t = $floodjoin{$c}{$_}{Time}; + next unless (defined $t); + + $time += $t; } &DEBUG("joinflood: time => $time"); $time /= $count; @@ -836,10 +880,15 @@ sub joinfloodCheck { ### Clean it up. my $delete = 0; + my $time = time(); foreach $chan (keys %floodjoin) { foreach $who (keys %{ $floodjoin{$chan} }) { - my $time = time() - $floodjoin{$chan}{$who}{Time}; - next unless ($time > 10); + my $t = $floodjoin{$chan}{$who}{Time}; + next unless (defined $t); + + my $delta = $time - $t; + next unless ($delta > 10); + delete $floodjoin{$chan}{$who}; $delete++; } @@ -851,7 +900,12 @@ sub joinfloodCheck { sub getHostMask { my($n) = @_; - &FIXME("getHostMask($n) called..."); + if (exists $nuh{$n}) { + return &makeHostMask($nuh{$n}); + } else { + $cache{on_who_Hack} = 1; + $conn->who($n); + } } 1;