# NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
#
-if (&IsParam("useStrict")) { use strict; }
+use strict;
-use vars qw($nickserv);
-$nickserv = 0;
+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 $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(@ircServers);
+
+#use open ':utf8';
+#use open ':std';
-# 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*';
+$nickserv = 0;
+my $maxlinelen = 400;
sub ircloop {
my $error = 0;
sub irc {
my ($server,$port) = @_;
+ $irc = new Net::IRC;
+
+ # TODO: move all this to an sql table
my $iaddr = inet_aton($server);
my $paddr = sockaddr_in($port, $iaddr);
my $proto = getprotobyname('tcp');
- select STDOUT;
- &status("Connecting to port $port of server $server ...");
+ # why was this here?
+ #select STDOUT;
# host->ip.
+ my $resolve;
if ($server =~ /\D$/) {
my $packed = scalar(gethostbyname($server));
return 0;
}
- my $resolve = inet_ntoa($packed);
- &status(" resolved to $resolve.");
+ $resolve = inet_ntoa($packed);
### warning in Sys/Hostname line 78???
### caused inside Net::IRC?
}
- $irc = new Net::IRC;
-
my %args = (
Nick => $param{'ircNick'},
Server => $server,
$args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
$args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
- $conn = $irc->newconn(%args);
-
- if (!defined $conn) {
- &ERROR("internal: perl IRC connection object does not exist.");
- return 1;
+ foreach my $mynick (split ',', $param{'ircNick'}) {
+ &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
+ $args{'Nick'} = $mynick;
+ $conns{$mynick} = $irc->newconn(%args);
+ if (!defined $conns{$mynick}) {
+ &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.");
+ }
+ $conns{$mynick}->maxlinelen($maxlinelen);
+ # handler stuff.
+ $conns{$mynick}->add_global_handler('caction', \&on_action);
+ $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
+ $conns{$mynick}->add_global_handler('cping', \&on_ping);
+ $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
+ $conns{$mynick}->add_global_handler('cversion', \&on_version);
+ $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
+ $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
+ $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
+ $conns{$mynick}->add_global_handler('chat', \&on_chat);
+ $conns{$mynick}->add_global_handler('msg', \&on_msg);
+ $conns{$mynick}->add_global_handler('public', \&on_public);
+ $conns{$mynick}->add_global_handler('join', \&on_join);
+ $conns{$mynick}->add_global_handler('part', \&on_part);
+ $conns{$mynick}->add_global_handler('topic', \&on_topic);
+ $conns{$mynick}->add_global_handler('invite', \&on_invite);
+ $conns{$mynick}->add_global_handler('kick', \&on_kick);
+ $conns{$mynick}->add_global_handler('mode', \&on_mode);
+ $conns{$mynick}->add_global_handler('nick', \&on_nick);
+ $conns{$mynick}->add_global_handler('quit', \&on_quit);
+ $conns{$mynick}->add_global_handler('notice', \&on_notice);
+ $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
+ $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
+ $conns{$mynick}->add_global_handler('whois', \&on_whois);
+ $conns{$mynick}->add_global_handler('other', \&on_other);
+ $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
+ $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
+# $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
+ $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
+ $conns{$mynick}->add_global_handler(315, \&on_endofwho);
+ $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
+ $conns{$mynick}->add_global_handler(324, \&on_modeis);
+ $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
+ $conns{$mynick}->add_global_handler(352, \&on_who);
+ $conns{$mynick}->add_global_handler(353, \&on_names);
+ $conns{$mynick}->add_global_handler(366, \&on_endofnames);
+ $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
+ $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
+ $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
+ # for proper joinnextChan behaviour
+ $conns{$mynick}->add_global_handler(471, \&on_chanfull);
+ $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
+ $conns{$mynick}->add_global_handler(474, \&on_banned);
+ $conns{$mynick}->add_global_handler(475, \&on_badchankey);
+ $conns{$mynick}->add_global_handler(443, \&on_useronchan);
+ # end of handler stuff.
}
&clearIRCVars();
$ircstats{'Server'} = "$server:$port";
- # handler stuff.
- $conn->add_handler('caction', \&on_action);
- $conn->add_handler('cdcc', \&on_dcc);
- $conn->add_handler('cping', \&on_ping);
- $conn->add_handler('crping', \&on_ping_reply);
- $conn->add_handler('cversion', \&on_version);
- $conn->add_handler('crversion', \&on_crversion);
- $conn->add_handler('dcc_open', \&on_dcc_open);
- $conn->add_handler('dcc_close', \&on_dcc_close);
- $conn->add_handler('chat', \&on_chat);
- $conn->add_handler('msg', \&on_msg);
- $conn->add_handler('public', \&on_public);
- $conn->add_handler('join', \&on_join);
- $conn->add_handler('part', \&on_part);
- $conn->add_handler('topic', \&on_topic);
- $conn->add_handler('invite', \&on_invite);
- $conn->add_handler('kick', \&on_kick);
- $conn->add_handler('mode', \&on_mode);
- $conn->add_handler('nick', \&on_nick);
- $conn->add_handler('quit', \&on_quit);
- $conn->add_handler('notice', \&on_notice);
- $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(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);
- $conn->add_global_handler(353, \&on_names);
- $conn->add_global_handler(366, \&on_endofnames);
- $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
- $conn->add_global_handler(433, \&on_nick_taken);
- $conn->add_global_handler(439, \&on_targettoofast);
- # for proper joinnextChan behaviour
- $conn->add_global_handler(471, \&on_chanfull);
- $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);
+ # works? needs to actually do something
+ # should likely listen on a tcp port instead
+ #$irc->addfh(STDIN, \&on_stdin, "r");
- # end of handler stuff.
+ &status("starting main loop");
$irc->start;
}
sub say {
my ($msg) = @_;
+ my $mynick = $conn->nick();
if (!defined $msg) {
$msg ||= "NULL";
&WARN("say: msg == $msg.");
return;
}
- &status("</$talkchannel> $msg");
+
+ &status("<$mynick/$talkchannel> $msg");
if (&whatInterface() =~ /IRC/) {
$msg = "zero" if ($msg =~ /^0+$/);
my $t = time();
return;
}
- if ($msgType =~ /chat/i) {
- # todo: warn that we're using msg() to do DCC CHAT?
- &dccsay($nick, $msg);
- # todo: make dccsay deal with flood protection?
- return;
- }
-
&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;
- } elsif ($msgsize > $j) {
- sleep 1;
- $msgsize -= $j;
- }
+ 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);
sub action {
+ my $mynick = $conn->nick();
my ($target, $txt) = @_;
if (!defined $txt) {
&WARN("action: txt == NULL.");
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);
+ &status("* $mynick/$target $txt");
+ $conn->me($target, $txt);
}
# Usage: ¬ice(nick || chan, txt);
# Usage: &performReply($reply);
sub performReply {
my ($reply) = @_;
+
+ if (!defined $reply or $reply =~ /^\s*$/) {
+ &DEBUG("performReply: reply == NULL.");
+ return;
+ }
+
$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') {
$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}) {
&VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
return;
}
$conn->privmsg($dcc{'CHAT'}{$who}, $reply);
+
} else {
&ERROR("PR: msgType invalid? ($msgType).");
}
&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') {
}
sub joinchan {
- my ($chankey) = @_;
- my $chan = lc $chankey;
+ my ($chan, $key) = @_;
+ $key ||= &getChanConf("chankey", $chan);
+ $key ||= "";
- if ($chankey =~ s/^($mask{chan}),\S+/ /) {
- $chan = lc $1;
- }
+ # 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 {
- &status("joining $b_blue$chan$ob");
- if (!$conn->join($chan)) {
- &DEBUG("joinchan: join failed. trying connect!");
- &clearIRCVars();
- $conn->connect();
- }
+ &status("join: already on $chan?");
}
+ #} else {
+ &status("joining $b_blue$chan $key$ob");
+
+ return if ($conn->join($chan, $key));
+ return if (&validChan($chan));
+
+ &DEBUG("joinchan: join failed. trying connect!");
+ &clearIRCVars();
+ $conn->connect();
+ #}
}
sub part {
# next;
}
- rawout("PART $chan");
+ $conn->part($chan);
# deletion of $channels{chan} is done in &entryEvt().
}
}
&DEBUG("mode: MODE $chan $modes");
+ # should move to use Net::IRC's $conn->mode()... but too lazy.
rawout("MODE $chan $modes");
}
sub kick {
my ($nick,$chan,$msg) = @_;
my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
+ my $mynick = $conn->nick();
if ($chan ne "" and &validChan($chan) == 0) {
&ERROR("kick: invalid channel $chan.");
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 :(");
+ if (!exists $channels{$chan}{o}{$mynick}) {
+ &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);
}
}
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) {
}
foreach $chan (@chans) {
- if (!exists $channels{$chan}{o}{$ident}) {
- &status("Ban: do not have ops on $chan :(");
+ if (!exists $channels{$chan}{o}{$mynick}) {
+ &status("ban: do not have ops on $chan :(");
next;
}
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;
}
sub quit {
my ($quitmsg) = @_;
- &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
if (defined $conn) {
+ &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
$conn->quit($quitmsg);
} else {
&WARN("quit: could not 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{ $param{'ircNick'} });
- $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
+ 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(localtime). "]");
- 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;
}
- if ($nick =~ /^$mask{nick}$/) {
- rawout("NICK ".$nick);
-
- if (defined $ident) {
- &status("nick: Changing nick to $nick (from $ident)");
- } else {
- &DEBUG("first time nick change.");
- $ident = $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 (nick => $nick)");
-
+ &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
return 0;
}
sub invite {
my($who, $chan) = @_;
- rawout("INVITE $who $chan");
-}
+ # TODO: check if $who or $chan are invalid.
+ $conn->invite($who, $chan);
+}
##########
# Channel related functions...
# Usage: &joinNextChan();
sub joinNextChan {
- if (scalar @joinchan) {
- $chan = shift @joinchan;
- &joinchan($chan);
-
- if (my $i = scalar @joinchan) {
- &status("joinNextChan: $i chans to join.");
+ my $joined = 0;
+ foreach (sort keys %conns) {
+ $conn = $conns{$_};
+ my $mynick = $conn->nick();
+ my @join = getJoinChans(1);
+
+ if (scalar @join) {
+ my $chan = shift @join;
+ &joinchan($chan);
+
+ if (my $i = scalar @join) {
+ &status("joinNextChan: $mynick $i chans to join.");
+ }
+ $joined = 1;
}
-
- return;
}
+ return if $joined;
- # !scalar @joinchan:
- my @c = &getJoinChans();
- if (exists $cache{joinTime} and scalar @c) {
+ if (exists $cache{joinTime}) {
my $delta = time() - $cache{joinTime} - 5;
my $timestr = &Time2String($delta);
- my $rate = sprintf("%.1f", $delta / @c);
+ # FIXME: @join should be @in instead (hacked to 10)
+ #my $rate = sprintf("%.1f", $delta / @in);
+ my $rate = sprintf("%.1f", $delta / 10);
delete $cache{joinTime};
&status("time taken to join all chans: $timestr; rate: $rate sec/join");
$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;
sub IsNickInAnyChan {
my ($nick) = @_;
+ my $chan;
foreach $chan (keys %channels) {
next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
# Usage: &validChan($chan);
sub validChan {
- # todo: use $c instead?
+ # TODO: use $c instead?
my ($chan) = @_;
if (!defined $chan or $chan =~ /^\s*$/) {
# it's possible that this check creates the hash if empty.
if (defined $channels{$chan} or exists $channels{$chan}) {
- if ($chan eq "_default") {
+ if ($chan =~ /^_?default$/) {
# &WARN("validC: chan cannot be _default! returning 0!");
return 0;
}
}
sub clearIRCVars {
- &DEBUG("clearIRCVars() called!");
undef %channels;
undef %floodjoin;
- @joinchan = &getJoinChans(1);
$cache{joinTime} = time();
}
sub getJoinChans {
my($show) = @_;
- my @chans;
+
+ my @in;
my @skip;
+ my @join;
+
+ # can't join any if not connected
+ return @join if (!$conn);
+
+ my $nick = $conn->nick();
foreach (keys %chanconf) {
next if ($_ eq "_default");
- my $val = $chanconf{$_}{autojoin};
my $skip = 0;
+ my $val = $chanconf{$_}{autojoin};
if (defined $val) {
$skip++ if ($val eq "0");
+ if ($val eq "1") {
+ # convert old +autojoin to autojoin <nick>
+ $val = $nick;
+ $chanconf{$_}{autojoin} = $val;
+ }
+ $skip++ if ($val ne $nick);
} else {
$skip++;
}
if ($skip) {
push(@skip, $_);
- next;
+ } else {
+ if (defined $channels{$_} or exists $channels{$_}) {
+ push(@in, $_);
+ } else {
+ push(@join, $_);
+ }
}
-
- push(@chans, $_);
}
my $str;
- if (scalar @skip) {
- $str = "channels not auto-joining: @skip (joining: @chans)";
- } else {
- $str = "auto-joining all chans: @chans";
- }
+ $str .= ' in:' . join(',', sort @in) if scalar @in;
+ $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
+ $str .= ' join:' . join(',', sort @join) if scalar @join;
- &status("Chans: ".$str) if ($show);
+ &status("Chans: ($nick)$str") if ($show);
- return @chans;
+ return sort @join;
}
sub closeDCC {
# &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);
&status("DCC CHAT: closing DCC $type to $nick.");
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;
return &makeHostMask($nuh{$n});
} else {
$cache{on_who_Hack} = 1;
- &rawout("WHO $n");
+ $conn->who($n);
}
}