]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Irc.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / IRC / Irc.pl
index c1c2753cb7f44cae694e69108527e776d6a32085..c89c0db41616a95abce9df3a0a9564d9612d509c 100644 (file)
@@ -6,19 +6,28 @@
 #
 
 use strict;
+
 no strict 'refs';
+no strict 'subs'; # IN/STDIN
 
 use vars qw(%floodjoin %nuh %dcc %cache %conns %channels %param %mask
        %chanconf %orig %ircPort %ircstats %last %netsplit);
-use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
-       $addressed);
+use vars qw($irc $nickserv $conn $msgType $who $talkchannel
+       $addressed $postprocess);
 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
                $pubcount $pubtime $pubsize);
 use vars qw($b_blue $ob);
 use vars qw(@ircServers);
 
+#use open ':utf8';
+#use open ':std';
+
 $nickserv      = 0;
-my $maxlinelen = 400;
+# It's probably closer to 510, but let's be cautious until we calculate it extensively.
+my $maxlinelen = 490;
+
+# Keep track of last time we displayed Chans: to avoid spam in logs
+my $lastChansTime = 0;
 
 sub ircloop {
     my $error  = 0;
@@ -170,6 +179,12 @@ sub irc {
 
     $ircstats{'Server'}        = "$server:$port";
 
+    # works? needs to actually do something
+    # should likely listen on a tcp port instead
+    #$irc->addfh(STDIN, \&on_stdin, 'r');
+
+    &status("starting main loop");
+
     $irc->start;
 }
 
@@ -181,7 +196,7 @@ sub rawout {
     my ($buf) = @_;
     $buf =~ s/\n//gi;
 
-    # slow down a bit if traffic is "high".
+    # slow down a bit if traffic is 'high'.
     # need to take into account time of last message sent.
     if ($last{buflen} > 256 and length($buf) > 256) {
        sleep 1;
@@ -196,39 +211,55 @@ sub say {
     my ($msg) = @_;
     my $mynick = $conn->nick();
     if (!defined $msg) {
-       $msg ||= "NULL";
+       $msg ||= 'NULL';
        &WARN("say: msg == $msg.");
        return;
     }
 
+    if (&getChanConf('silent', $talkchannel) and not
+      (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
+       &DEBUG("say: silent in $talkchannel, not saying $msg");
+       return;
+    }
+
+    if ( $postprocess ) {
+       undef $postprocess;
+    } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
+       &DEBUG("say: $postprocess $msg");
+       &parseCmdHook($postprocess . ' ' . $msg);
+       undef $postprocess;
+       return;
+    }
 
     &status("<$mynick/$talkchannel> $msg");
-    if (&whatInterface() =~ /IRC/) {
-       $msg    = "zero" if ($msg =~ /^0+$/);
-       my $t   = time();
-
-       if ($t == $pubtime) {
-           $pubcount++;
-           $pubsize += length $msg;
-
-           my $i = &getChanConfDefault("sendPublicLimitLines", 3);
-           my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
-
-           if ( ($pubcount % $i) == 0 and $pubcount) {
-               sleep 1;
-           } elsif ($pubsize > $j) {
-               sleep 1;
-               $pubsize -= $j;
-           }
 
-       } else {
-           $pubcount   = 0;
-           $pubtime    = $t;
-           $pubsize    = length $msg;
+    return unless (&whatInterface() =~ /IRC/);
+
+    $msg = 'zero' if ($msg =~ /^0+$/);
+
+    my $t = time();
+
+    if ($t == $pubtime) {
+       $pubcount++;
+       $pubsize += length $msg;
+
+       my $i = &getChanConfDefault('sendPublicLimitLines', 3, $chan);
+       my $j = &getChanConfDefault('sendPublicLimitBytes', 1000, $chan);
+
+       if ( ($pubcount % $i) == 0 and $pubcount) {
+           sleep 1;
+       } elsif ($pubsize > $j) {
+           sleep 1;
+           $pubsize -= $j;
        }
 
-       $conn->privmsg($talkchannel, $msg);
+    } else {
+       $pubcount       = 0;
+       $pubtime        = $t;
+       $pubsize        = length $msg;
     }
+
+    $conn->privmsg($talkchannel, $msg);
 }
 
 sub msg {
@@ -239,11 +270,18 @@ sub msg {
     }
 
     if (!defined $msg) {
-       $msg ||= "NULL";
+       $msg ||= 'NULL';
        &WARN("msg: msg == $msg.");
        return;
     }
 
+    # some say() end up here (eg +help)
+    if (&getChanConf('silent', $nick) and not
+       (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
+       &DEBUG("msg: silent in $nick, not saying $msg");
+       return;
+    }
+
     &status(">$nick< $msg");
 
     return unless (&whatInterface() =~ /IRC/);
@@ -253,8 +291,8 @@ sub msg {
        $msgcount++;
        $msgsize += length $msg;
 
-       my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
-       my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
+       my $i = &getChanConfDefault('sendPrivateLimitLines', 3, $chan);
+       my $j = &getChanConfDefault('sendPrivateLimitBytes', 1000, $chan);
        if ( ($msgcount % $i) == 0 and $msgcount) {
            sleep 1;
        } elsif ($msgsize > $j) {
@@ -273,18 +311,25 @@ sub msg {
 
 # Usage: &action(nick || chan, txt);
 sub action {
+    my $mynick = $conn->nick();
     my ($target, $txt) = @_;
     if (!defined $txt) {
        &WARN("action: txt == NULL.");
        return;
     }
 
+    if (&getChanConf('silent', $target) and not
+       (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
+       &DEBUG("action: silent in $target, not doing $txt");
+       return;
+    }
+
     if (length $txt > 480) {
        &status("action: txt too long; truncating.");
        chop($txt) while (length $txt > 480);
     }
 
-    &status("* $ident/$target $txt");
+    &status("* $mynick/$target $txt");
     $conn->me($target, $txt);
 }
 
@@ -304,8 +349,8 @@ sub notice {
        $notcount++;
        $notsize += length $txt;
 
-       my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
-       my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
+       my $i = &getChanConfDefault('sendNoticeLimitLines', 3, $chan);
+       my $j = &getChanConfDefault('sendNoticeLimitBytes', 1000, $chan);
 
        if ( ($notcount % $i) == 0 and $notcount) {
            sleep 1;
@@ -348,6 +393,11 @@ sub performReply {
 
     $reply =~ /([\.\?\s]+)$/;
 
+    # FIXME need real throttling....
+    if (length($reply) > $maxlinelen - 30) {
+       $reply = substr($reply, 0, $maxlinelen - 33);
+       $reply =~ s/ [^ ]*?$/ .../;
+    }
     &checkMsgType($reply);
 
     if ($msgType eq 'public') {
@@ -382,14 +432,15 @@ sub performAddressedReply {
     &performReply(@_);
 }
 
-sub pSReply {
-    &performStrictReply(@_);
-}
-
 # Usage: &performStrictReply($reply);
 sub performStrictReply {
     my ($reply) = @_;
 
+    # FIXME need real throttling....
+    if (length($reply) > $maxlinelen - 30) {
+       $reply = substr($reply, 0, $maxlinelen - 33);
+       $reply =~ s/ [^ ]*?$/ .../;
+    }
     &checkMsgType($reply);
 
     if ($msgType eq 'private') {
@@ -434,8 +485,9 @@ sub dcc_close {
 }
 
 sub joinchan {
-    my ($chan) = @_;
-    my $key    = &getChanConf("chankey", $chan) || "";
+    my ($chan, $key) = @_;
+    $key ||= &getChanConf('chankey', $chan);
+    $key ||= '';
 
     # forgot for about 2 years to implement channel keys when moving
     # over to Net::IRC...
@@ -445,10 +497,10 @@ sub joinchan {
        &status("join: already on $chan?");
     }
     #} else {
-       &status("joining $b_blue$chan$ob");
+       &status("joining $b_blue$chan $key$ob");
 
        return if ($conn->join($chan, $key));
-       return if (&validChan($chan));
+       return if (&validChan($chan));
 
        &DEBUG("joinchan: join failed. trying connect!");
        &clearIRCVars();
@@ -460,7 +512,7 @@ sub part {
     my $chan;
 
     foreach $chan (@_) {
-       next if ($chan eq "");
+       next if ($chan eq '');
        $chan =~ tr/A-Z/a-z/;   # lowercase.
 
        if ($chan !~ /^$mask{chan}$/) {
@@ -496,23 +548,24 @@ sub mode {
 
 sub op {
     my ($chan, @who) = @_;
-    my $os     = "o" x scalar(@who);
+    my $os     = 'o' x scalar(@who);
 
     &mode($chan, "+$os @who");
 }
 
 sub deop {
     my ($chan, @who) = @_;
-    my $os = "o" x scalar(@who);
+    my $os = 'o' x scalar(@who);
 
     &mode($chan, "-$os ".@who);
 }
 
 sub kick {
     my ($nick,$chan,$msg) = @_;
-    my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
+    my (@chans) = ($chan eq '') ? (keys %channels) : lc($chan);
+    my $mynick = $conn->nick();
 
-    if ($chan ne "" and &validChan($chan) == 0) {
+    if ($chan ne '' and &validChan($chan) == 0) {
        &ERROR("kick: invalid channel $chan.");
        return;
     }
@@ -525,7 +578,7 @@ sub kick {
            next;
        }
 
-       if (!exists $channels{$chan}{o}{$ident}) {
+       if (!exists $channels{$chan}{o}{$mynick}) {
            &status("kick: do not have ops on $chan :(");
            next;
        }
@@ -538,6 +591,7 @@ sub kick {
 sub ban {
     my ($mask,$chan) = @_;
     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
+    my $mynick = $conn->nick();
     my $ban    = 0;
 
     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
@@ -546,7 +600,7 @@ sub ban {
     }
 
     foreach $chan (@chans) {
-       if (!exists $channels{$chan}{o}{$ident}) {
+       if (!exists $channels{$chan}{o}{$mynick}) {
            &status("ban: do not have ops on $chan :(");
            next;
        }
@@ -562,12 +616,13 @@ sub ban {
 sub unban {
     my ($mask,$chan) = @_;
     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
+    my $mynick = $conn->nick();
     my $ban    = 0;
 
     &DEBUG("unban: mask = $mask, chan = @chans");
 
     foreach $chan (@chans) {
-       if (!exists $channels{$chan}{o}{$ident}) {
+       if (!exists $channels{$chan}{o}{$mynick}) {
            &status("unBan: do not have ops on $chan :(");
            next;
        }
@@ -591,46 +646,36 @@ sub quit {
 }
 
 sub nick {
-    my ($nick) = @_;
+    my ($newnick) = @_;
+    my $mynick = $conn->nick();
 
-    if (!defined $nick) {
+    if (!defined $newnick) {
        &ERROR("nick: nick == NULL.");
        return;
     }
 
-    if (defined $ident and $nick eq $ident) {
-       &WARN("nick: nick == ident == '$ident'.");
+    if (!defined $mynick) {
+       &WARN("nick: mynick == NULL.");
        return;
     }
 
-    my $bad     = 0;
-    $bad++ if (exists $nuh{$conn->nick()});
-    $bad++ if (&IsNickInAnyChan($conn->nick()));
+    my $bad = 0;
+    $bad++ if (exists $nuh{$newnick});
+    $bad++ if (&IsNickInAnyChan($newnick));
 
     if ($bad) {
-       &WARN("Nick: not going to try and get my nick back. [".
-               scalar(gmtime). "]");
-# hrm... over time we lose track of our own nick.
-#      return;
+       &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
+       # hrm... over time we lose track of our own nick.
+       #return;
     }
 
-# FIXME broken for multiple nicks
-#    if ($nick =~ /^$mask{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;
-#      }
-#
-#      return 1;
-#    }
-    &DEBUG("nick: failed... why oh why (nick => $nick)");
-
+    if ($newnick =~ /^$mask{nick}$/) {
+       &status("nick: Changing nick from $mynick to $newnick");
+       # ->nick() will NOT change cause we are using rawout?
+       &rawout("NICK $newnick");
+       return 1;
+    }
+    &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
     return 0;
 }
 
@@ -677,7 +722,7 @@ sub joinNextChan {
     }
 
     # chanserv check: global channels, in case we missed one.
-    foreach ( &ChanConfList("chanServ_ops") ) {
+    foreach ( &ChanConfList('chanServ_ops') ) {
        &chanServCheck($_);
     }
 }
@@ -797,31 +842,40 @@ sub clearIRCVars {
 }
 
 sub getJoinChans {
-    my($show)  = @_;
+    # $show should contain the min number of seconds between display
+    # of the Chans: status line. Use 0 to disable
+    my $show = shift;
 
     my @in;
     my @skip;
     my @join;
 
+    # Display "Chans:" only if more than $show seconds since last display
+    if (time() - $lastChansTime > $show) {
+       $lastChansTime = time();
+    } else {
+       $show = 0; # Don't display since < 15min since last
+    }
+
     # can't join any if not connected
     return @join if (!$conn);
 
     my $nick = $conn->nick();
 
     foreach (keys %chanconf) {
-       next if ($_ eq "_default");
+       next if ($_ eq '_default');
 
        my $skip = 0;
        my $val = $chanconf{$_}{autojoin};
 
        if (defined $val) {
-           $skip++ if ($val eq "0");
-           if ($val eq "1") {
+           $skip++ if ($val eq '0');
+           if ($val eq '1') {
                # convert old +autojoin to autojoin <nick>
-               $val = $nick;
+               $val = lc $nick;
                $chanconf{$_}{autojoin} = $val;
            }
-           $skip++ if ($val ne $nick);
+           $skip++ if (lc $val ne lc $nick);
        } else {
            $skip++;
        }
@@ -873,7 +927,7 @@ sub closeDCC {
 sub joinfloodCheck {
     my($who,$chan,$userhost) = @_;
 
-    return unless (&IsChanConf("joinfloodCheck"));
+    return unless (&IsChanConf('joinfloodCheck') > 0);
 
     if (exists $netsplit{lc $who}) {   # netsplit join.
        &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
@@ -937,3 +991,5 @@ sub getHostMask {
 }
 
 1;
+
+# vim:ts=4:sw=4:expandtab:tw=80