#
use strict;
+
no strict 'refs';
+no strict 'subs'; # IN/STDIN
-use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
+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
+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';
+
$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'});
- my $conn = $irc->newconn(%args);
-
- if (!defined $conn) {
- &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;
- }
-
- if ($param{'ircNick2'}) {
- # prep for real multiple nick/server connects
- # FIXME: all code should get nick/server out of self, not config
- $args{'Nick'} = $param{'ircNick2'};
- my $conn = $irc->newconn(%args);
- }
-
- if ($param{'ircNick3'}) {
- # prep for real multiple nick/server connects
- # FIXME: all code should get nick/server out of self, not config
- $args{'Nick'} = $param{'ircNick3'};
- my $conn = $irc->newconn(%args);
+ 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_global_handler('caction', \&on_action);
- $conn->add_global_handler('cdcc', \&on_dcc);
- $conn->add_global_handler('cping', \&on_ping);
- $conn->add_global_handler('crping', \&on_ping_reply);
- $conn->add_global_handler('cversion', \&on_version);
- $conn->add_global_handler('crversion', \&on_crversion);
- $conn->add_global_handler('dcc_open', \&on_dcc_open);
- $conn->add_global_handler('dcc_close', \&on_dcc_close);
- $conn->add_global_handler('chat', \&on_chat);
- $conn->add_global_handler('msg', \&on_msg);
- $conn->add_global_handler('public', \&on_public);
- $conn->add_global_handler('join', \&on_join);
- $conn->add_global_handler('part', \&on_part);
- $conn->add_global_handler('topic', \&on_topic);
- $conn->add_global_handler('invite', \&on_invite);
- $conn->add_global_handler('kick', \&on_kick);
- $conn->add_global_handler('mode', \&on_mode);
- $conn->add_global_handler('nick', \&on_nick);
- $conn->add_global_handler('quit', \&on_quit);
- $conn->add_global_handler('notice', \&on_notice);
- $conn->add_global_handler('whoischannels', \&on_whoischannels);
- $conn->add_global_handler('useronchannel', \&on_useronchannel);
- $conn->add_global_handler('whois', \&on_whois);
- $conn->add_global_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(302, \&on_init); # userhost
- $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);
- $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);
+ $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();
# Usage: &action(nick || chan, txt);
sub action {
+ my $mynick = $conn->nick();
my ($target, $txt) = @_;
if (!defined $txt) {
&WARN("action: txt == NULL.");
chop($txt) while (length $txt > 480);
}
- &status("* $ident/$target $txt");
+ &status("* $mynick/$target $txt");
$conn->me($target, $txt);
}
$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') {
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 ($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...
&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));
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.");
next;
}
- if (!exists $channels{$chan}{o}{$ident}) {
+ if (!exists $channels{$chan}{o}{$mynick}) {
&status("kick: do not have ops on $chan :(");
next;
}
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}) {
+ 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 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;
}
- 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;
- }
-
+ 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;
}
# Usage: &joinNextChan();
sub joinNextChan {
- my @join = getJoinChans(1);
- my $mynick = "UNDEFINED";
-
- $mynick = $conn->nick() if $conn;
- if (scalar @join) {
- my $chan = shift @join;
- &joinchan($chan);
-
- if (my $i = scalar @join) {
- &status("joinNextChan: $mynick $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;
if (exists $cache{joinTime}) {
my $delta = time() - $cache{joinTime} - 5;