2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
12 %chanconf %orig %ircPort %ircstats %last %netsplit);
13 use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
15 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
16 $pubcount $pubtime $pubsize);
17 use vars qw($b_blue $ob);
18 use vars qw(@ircServers);
28 while (my $host = shift @ircServers) {
29 # JUST IN CASE. irq was complaining about this.
30 if ($lastrun == time()) {
31 &DEBUG("ircloop: hrm... lastrun == time()");
38 &DEBUG("ircloop: ircServers[x] = NULL.");
42 next unless (exists $ircPort{$host});
44 my $retval = &irc($host, $ircPort{$host});
45 next unless (defined $retval and $retval == 0);
48 if ($error % 3 == 0 and $error != 0) {
49 &status("IRC: Could not connect.");
55 &status("IRC: cannot connect to any IRC servers; stopping.");
61 &status("IRC: ok, done one cycle of IRC servers; trying again.");
68 my ($server,$port) = @_;
70 my $iaddr = inet_aton($server);
71 my $paddr = sockaddr_in($port, $iaddr);
72 my $proto = getprotobyname('tcp');
75 &status("Connecting to port $port of server $server ...");
78 if ($server =~ /\D$/) {
79 my $packed = scalar(gethostbyname($server));
81 if (!defined $packed) {
82 &status(" cannot resolve $server.");
86 my $resolve = inet_ntoa($packed);
87 &status(" resolved to $resolve.");
88 ### warning in Sys/Hostname line 78???
89 ### caused inside Net::IRC?
95 Nick => $param{'ircNick'},
98 Ircname => $param{'ircName'},
100 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
101 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
103 my $conn = $irc->newconn(%args);
105 if (!defined $conn) {
106 &ERROR("IRC: connection failed.");
107 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
108 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
109 &ERROR("127.0.0.1 localhost localhost");
110 &ERROR("If this is still a problem, please contact the maintainer.");
113 $conn->maxlinelen($maxlinelen);
115 if ($param{'ircNick2'}) {
116 # prep for real multiple nick/server connects
117 # FIXME: all code should get nick/server out of self, not config
118 $args{'Nick'} = $param{'ircNick2'};
119 my $conn = $irc->newconn(%args);
120 $conn->maxlinelen($maxlinelen);
123 if ($param{'ircNick3'}) {
124 # prep for real multiple nick/server connects
125 # FIXME: all code should get nick/server out of self, not config
126 $args{'Nick'} = $param{'ircNick3'};
127 my $conn = $irc->newconn(%args);
128 $conn->maxlinelen($maxlinelen);
133 # change internal timeout value for scheduler.
134 $irc->{_timeout} = 10; # how about 60?
135 # Net::IRC debugging.
138 $ircstats{'Server'} = "$server:$port";
141 $conn->add_global_handler('caction', \&on_action);
142 $conn->add_global_handler('cdcc', \&on_dcc);
143 $conn->add_global_handler('cping', \&on_ping);
144 $conn->add_global_handler('crping', \&on_ping_reply);
145 $conn->add_global_handler('cversion', \&on_version);
146 $conn->add_global_handler('crversion', \&on_crversion);
147 $conn->add_global_handler('dcc_open', \&on_dcc_open);
148 $conn->add_global_handler('dcc_close', \&on_dcc_close);
149 $conn->add_global_handler('chat', \&on_chat);
150 $conn->add_global_handler('msg', \&on_msg);
151 $conn->add_global_handler('public', \&on_public);
152 $conn->add_global_handler('join', \&on_join);
153 $conn->add_global_handler('part', \&on_part);
154 $conn->add_global_handler('topic', \&on_topic);
155 $conn->add_global_handler('invite', \&on_invite);
156 $conn->add_global_handler('kick', \&on_kick);
157 $conn->add_global_handler('mode', \&on_mode);
158 $conn->add_global_handler('nick', \&on_nick);
159 $conn->add_global_handler('quit', \&on_quit);
160 $conn->add_global_handler('notice', \&on_notice);
161 $conn->add_global_handler('whoischannels', \&on_whoischannels);
162 $conn->add_global_handler('useronchannel', \&on_useronchannel);
163 $conn->add_global_handler('whois', \&on_whois);
164 $conn->add_global_handler('other', \&on_other);
165 $conn->add_global_handler('disconnect', \&on_disconnect);
166 $conn->add_global_handler([251,252,253,254,255], \&on_init);
167 # $conn->add_global_handler(302, \&on_init); # userhost
168 $conn->add_global_handler(303, \&on_ison); # notify.
169 $conn->add_global_handler(315, \&on_endofwho);
170 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
171 $conn->add_global_handler(324, \&on_modeis);
172 $conn->add_global_handler(333, \&on_topicinfo);
173 $conn->add_global_handler(352, \&on_who);
174 $conn->add_global_handler(353, \&on_names);
175 $conn->add_global_handler(366, \&on_endofnames);
176 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
177 $conn->add_global_handler(433, \&on_nick_taken);
178 $conn->add_global_handler(439, \&on_targettoofast);
179 # for proper joinnextChan behaviour
180 $conn->add_global_handler(471, \&on_chanfull);
181 $conn->add_global_handler(473, \&on_inviteonly);
182 $conn->add_global_handler(474, \&on_banned);
183 $conn->add_global_handler(475, \&on_badchankey);
184 $conn->add_global_handler(443, \&on_useronchan);
186 # end of handler stuff.
191 ######################################################################
192 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
193 ######################################################################
199 # slow down a bit if traffic is "high".
200 # need to take into account time of last message sent.
201 if ($last{buflen} > 256 and length($buf) > 256) {
205 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
207 $last{buflen} = length($buf);
212 my $mynick = $conn->nick();
215 &WARN("say: msg == $msg.");
220 &status("<$mynick/$talkchannel> $msg");
221 if (&whatInterface() =~ /IRC/) {
222 $msg = "zero" if ($msg =~ /^0+$/);
225 if ($t == $pubtime) {
227 $pubsize += length $msg;
229 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
230 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
232 if ( ($pubcount % $i) == 0 and $pubcount) {
234 } elsif ($pubsize > $j) {
242 $pubsize = length $msg;
245 $conn->privmsg($talkchannel, $msg);
250 my ($nick, $msg) = @_;
251 if (!defined $nick) {
252 &ERROR("msg: nick == NULL.");
258 &WARN("msg: msg == $msg.");
262 &status(">$nick< $msg");
264 return unless (&whatInterface() =~ /IRC/);
267 if ($t == $msgtime) {
269 $msgsize += length $msg;
271 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
272 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
273 if ( ($msgcount % $i) == 0 and $msgcount) {
275 } elsif ($msgsize > $j) {
283 $msgsize = length $msg;
286 $conn->privmsg($nick, $msg);
289 # Usage: &action(nick || chan, txt);
291 my ($target, $txt) = @_;
293 &WARN("action: txt == NULL.");
297 if (length $txt > 480) {
298 &status("action: txt too long; truncating.");
299 chop($txt) while (length $txt > 480);
302 &status("* $ident/$target $txt");
303 $conn->me($target, $txt);
306 # Usage: ¬ice(nick || chan, txt);
308 my ($target, $txt) = @_;
310 &WARN("notice: txt == NULL.");
314 &status("-$target- $txt");
318 if ($t == $nottime) {
320 $notsize += length $txt;
322 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
323 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
325 if ( ($notcount % $i) == 0 and $notcount) {
327 } elsif ($notsize > $j) {
335 $notsize = length $txt;
338 $conn->notice($target, $txt);
342 my ($txt,$flag) = @_;
344 ### FIXME: flag not supported yet.
346 foreach (keys %{ $dcc{'CHAT'} }) {
347 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
352 ### perform commands.
355 # Usage: &performReply($reply);
359 if (!defined $reply or $reply =~ /^\s*$/) {
360 &DEBUG("performReply: reply == NULL.");
364 $reply =~ /([\.\?\s]+)$/;
366 &checkMsgType($reply);
368 if ($msgType eq 'public') {
369 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
370 $reply = "$orig{who}: ".$reply;
372 $reply = "$reply, ".$orig{who};
376 } elsif ($msgType eq 'private') {
378 $reply = "$reply, ".$orig{who};
382 } elsif ($msgType eq 'chat') {
383 if (!exists $dcc{'CHAT'}{$who}) {
384 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
387 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
390 &ERROR("PR: msgType invalid? ($msgType).");
395 sub performAddressedReply {
396 return unless ($addressed);
401 &performStrictReply(@_);
404 # Usage: &performStrictReply($reply);
405 sub performStrictReply {
408 &checkMsgType($reply);
410 if ($msgType eq 'private') {
412 } elsif ($msgType eq 'public') {
414 } elsif ($msgType eq 'chat') {
415 &dccsay(lc $who, $reply);
417 &ERROR("pSR: msgType invalid? ($msgType).");
422 my($who, $reply) = @_;
424 if (!defined $reply or $reply =~ /^\s*$/) {
425 &WARN("dccsay: reply == NULL.");
429 if (!exists $dcc{'CHAT'}{$who}) {
430 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
434 &status("=>$who<= $reply"); # dcc chat.
435 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
442 foreach $type (keys %dcc) {
443 &FIXME("dcc_close: $who");
444 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
445 next unless (scalar @who);
447 &DEBUG("dcc_close... close $who!");
453 my $key = &getChanConf("chankey", $chan) || "";
455 # forgot for about 2 years to implement channel keys when moving
456 # over to Net::IRC...
458 # hopefully validChan is right.
459 if (&validChan($chan)) {
460 &status("join: already on $chan?");
463 &status("joining $b_blue$chan$ob");
465 return if ($conn->join($chan, $key));
466 return if (&validChan($chan));
468 &DEBUG("joinchan: join failed. trying connect!");
478 next if ($chan eq "");
479 $chan =~ tr/A-Z/a-z/; # lowercase.
481 if ($chan !~ /^$mask{chan}$/) {
482 &WARN("part: chan is invalid ($chan)");
486 &status("parting $chan");
487 if (!&validChan($chan)) {
488 &WARN("part: not on $chan; doing anyway");
493 # deletion of $channels{chan} is done in &entryEvt().
498 my ($chan, @modes) = @_;
499 my $modes = join(" ", @modes);
501 if (&validChan($chan) == 0) {
502 &ERROR("mode: invalid chan => '$chan'.");
506 &DEBUG("mode: MODE $chan $modes");
508 # should move to use Net::IRC's $conn->mode()... but too lazy.
509 rawout("MODE $chan $modes");
513 my ($chan, @who) = @_;
514 my $os = "o" x scalar(@who);
516 &mode($chan, "+$os @who");
520 my ($chan, @who) = @_;
521 my $os = "o" x scalar(@who);
523 &mode($chan, "-$os ".@who);
527 my ($nick,$chan,$msg) = @_;
528 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
530 if ($chan ne "" and &validChan($chan) == 0) {
531 &ERROR("kick: invalid channel $chan.");
535 $nick =~ tr/A-Z/a-z/;
537 foreach $chan (@chans) {
538 if (!&IsNickInChan($nick,$chan)) {
539 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
543 if (!exists $channels{$chan}{o}{$ident}) {
544 &status("kick: do not have ops on $chan :(");
548 &status("Kicking $nick from $chan.");
549 $conn->kick($chan, $nick, $msg);
554 my ($mask,$chan) = @_;
555 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
558 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
559 &ERROR("ban: invalid channel $chan.");
563 foreach $chan (@chans) {
564 if (!exists $channels{$chan}{o}{$ident}) {
565 &status("ban: do not have ops on $chan :(");
569 &status("Banning $mask from $chan.");
570 &rawout("MODE $chan +b $mask");
578 my ($mask,$chan) = @_;
579 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
582 &DEBUG("unban: mask = $mask, chan = @chans");
584 foreach $chan (@chans) {
585 if (!exists $channels{$chan}{o}{$ident}) {
586 &status("unBan: do not have ops on $chan :(");
590 &status("Removed ban $mask from $chan.");
591 &rawout("MODE $chan -b $mask");
601 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
602 $conn->quit($quitmsg);
604 &WARN("quit: could not quit!");
611 if (!defined $nick) {
612 &ERROR("nick: nick == NULL.");
616 if (defined $ident and $nick eq $ident) {
617 &WARN("nick: nick == ident == '$ident'.");
622 $bad++ if (exists $nuh{$conn->nick()});
623 $bad++ if (&IsNickInAnyChan($conn->nick()));
626 &WARN("Nick: not going to try and get my nick back. [".
627 scalar(gmtime). "]");
628 # hrm... over time we lose track of our own nick.
632 # FIXME broken for multiple nicks
633 # if ($nick =~ /^$mask{nick}$/) {
634 # &rawout("NICK ".$nick);
636 # if (defined $ident) {
637 # &status("nick: Changing nick to $nick (from $ident)");
638 # # following shouldn't be here :(
641 # &DEBUG("first time nick change.");
647 &DEBUG("nick: failed... why oh why (nick => $nick)");
653 my($who, $chan) = @_;
654 # TODO: check if $who or $chan are invalid.
656 $conn->invite($who, $chan);
660 # Channel related functions...
663 # Usage: &joinNextChan();
665 my @join = getJoinChans(1);
666 my $mynick = "UNDEFINED";
668 $mynick = $conn->nick() if $conn;
670 my $chan = shift @join;
673 if (my $i = scalar @join) {
674 &status("joinNextChan: $mynick $i chans to join.");
680 if (exists $cache{joinTime}) {
681 my $delta = time() - $cache{joinTime} - 5;
682 my $timestr = &Time2String($delta);
683 # FIXME: @join should be @in instead (hacked to 10)
684 #my $rate = sprintf("%.1f", $delta / @in);
685 my $rate = sprintf("%.1f", $delta / 10);
686 delete $cache{joinTime};
688 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
691 # chanserv check: global channels, in case we missed one.
692 foreach ( &ChanConfList("chanServ_ops") ) {
697 # Usage: &getNickInChans($nick);
702 foreach (keys %channels) {
703 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
710 # Usage: &getNicksInChan($chan);
715 return keys %{ $channels{$chan}{''} };
719 my ($nick,$chan) = @_;
721 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
724 &DEBUG("INIC: chan == NULL.");
728 if (&validChan($chan) == 0) {
729 &ERROR("INIC: invalid channel $chan.");
733 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
736 foreach (keys %channels) {
737 next unless (/[A-Z]/);
738 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
744 sub IsNickInAnyChan {
748 foreach $chan (keys %channels) {
749 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
755 # Usage: &validChan($chan);
757 # TODO: use $c instead?
760 if (!defined $chan or $chan =~ /^\s*$/) {
764 if (lc $chan ne $chan) {
765 &WARN("validChan: lc chan != chan. ($chan); fixing.");
766 $chan =~ tr/A-Z/a-z/;
769 # it's possible that this check creates the hash if empty.
770 if (defined $channels{$chan} or exists $channels{$chan}) {
771 if ($chan =~ /^_?default$/) {
772 # &WARN("validC: chan cannot be _default! returning 0!");
783 # Usage: &delUserInfo($nick,@chans);
785 my ($nick,@chans) = @_;
788 foreach $chan (@chans) {
789 foreach $mode (keys %{ $channels{$chan} }) {
791 next unless (exists $channels{$chan}{$mode}{$nick});
793 delete $channels{$chan}{$mode}{$nick};
801 delete $channels{$chan};
808 $cache{joinTime} = time();
818 # can't join any if not connected
819 return @join if (!$conn);
821 my $nick = $conn->nick();
823 foreach (keys %chanconf) {
824 next if ($_ eq "_default");
827 my $val = $chanconf{$_}{autojoin};
830 $skip++ if ($val eq "0");
832 # convert old +autojoin to autojoin <nick>
834 $chanconf{$_}{autojoin} = $val;
836 $skip++ if ($val ne $nick);
844 if (defined $channels{$_} or exists $channels{$_}) {
853 $str .= ' in:' . join(',', sort @in) if scalar @in;
854 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
855 $str .= ' join:' . join(',', sort @join) if scalar @join;
857 &status("Chans: ($nick)$str") if ($show);
863 # &DEBUG("closeDCC called.");
866 foreach $type (keys %dcc) {
867 next if ($type ne uc($type));
870 foreach $nick (keys %{ $dcc{$type} }) {
871 next unless (defined $nick);
872 &status("DCC CHAT: closing DCC $type to $nick.");
873 next unless (defined $dcc{$type}{$nick});
875 my $ref = $dcc{$type}{$nick};
876 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
877 $dcc{$type}{$nick}->close();
878 delete $dcc{$type}{$nick};
879 &DEBUG("after close for $nick");
886 my($who,$chan,$userhost) = @_;
888 return unless (&IsChanConf("joinfloodCheck"));
890 if (exists $netsplit{lc $who}) { # netsplit join.
891 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
894 if (exists $floodjoin{$chan}{$who}{Time}) {
895 &WARN("floodjoin{$chan}{$who} already exists?");
898 $floodjoin{$chan}{$who}{Time} = time();
899 $floodjoin{$chan}{$who}{Host} = $userhost;
902 foreach (keys %floodjoin) {
904 my $count = scalar keys %{ $floodjoin{$c} };
905 next unless ($count > 5);
906 &DEBUG("joinflood: count => $count");
909 foreach (keys %{ $floodjoin{$c} }) {
910 my $t = $floodjoin{$c}{$_}{Time};
911 next unless (defined $t);
915 &DEBUG("joinflood: time => $time");
918 &DEBUG("joinflood: new time => $time");
924 foreach $chan (keys %floodjoin) {
925 foreach $who (keys %{ $floodjoin{$chan} }) {
926 my $t = $floodjoin{$chan}{$who}{Time};
927 next unless (defined $t);
929 my $delta = $time - $t;
930 next unless ($delta > 10);
932 delete $floodjoin{$chan}{$who};
937 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
943 if (exists $nuh{$n}) {
944 return &makeHostMask($nuh{$n});
946 $cache{on_who_Hack} = 1;