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');
78 if ($server =~ /\D$/) {
79 my $packed = scalar(gethostbyname($server));
81 if (!defined $packed) {
82 &status(" cannot resolve $server.");
86 $resolve = inet_ntoa($packed);
87 ### warning in Sys/Hostname line 78???
88 ### caused inside Net::IRC?
90 &status("Connecting to port $port of server $server ($resolve) as $param{'ircNick'} ...");
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 &status("Connecting to port $port of server $server ($resolve) as $param{'ircNick2'} ...");
119 $args{'Nick'} = $param{'ircNick2'};
120 my $conn = $irc->newconn(%args);
121 $conn->maxlinelen($maxlinelen);
124 if ($param{'ircNick3'}) {
125 # prep for real multiple nick/server connects
126 # FIXME: all code should get nick/server out of self, not config
127 $args{'Nick'} = $param{'ircNick3'};
128 my $conn = $irc->newconn(%args);
129 $conn->maxlinelen($maxlinelen);
134 # change internal timeout value for scheduler.
135 $irc->{_timeout} = 10; # how about 60?
136 # Net::IRC debugging.
139 $ircstats{'Server'} = "$server:$port";
142 $conn->add_global_handler('caction', \&on_action);
143 $conn->add_global_handler('cdcc', \&on_dcc);
144 $conn->add_global_handler('cping', \&on_ping);
145 $conn->add_global_handler('crping', \&on_ping_reply);
146 $conn->add_global_handler('cversion', \&on_version);
147 $conn->add_global_handler('crversion', \&on_crversion);
148 $conn->add_global_handler('dcc_open', \&on_dcc_open);
149 $conn->add_global_handler('dcc_close', \&on_dcc_close);
150 $conn->add_global_handler('chat', \&on_chat);
151 $conn->add_global_handler('msg', \&on_msg);
152 $conn->add_global_handler('public', \&on_public);
153 $conn->add_global_handler('join', \&on_join);
154 $conn->add_global_handler('part', \&on_part);
155 $conn->add_global_handler('topic', \&on_topic);
156 $conn->add_global_handler('invite', \&on_invite);
157 $conn->add_global_handler('kick', \&on_kick);
158 $conn->add_global_handler('mode', \&on_mode);
159 $conn->add_global_handler('nick', \&on_nick);
160 $conn->add_global_handler('quit', \&on_quit);
161 $conn->add_global_handler('notice', \&on_notice);
162 $conn->add_global_handler('whoischannels', \&on_whoischannels);
163 $conn->add_global_handler('useronchannel', \&on_useronchannel);
164 $conn->add_global_handler('whois', \&on_whois);
165 $conn->add_global_handler('other', \&on_other);
166 $conn->add_global_handler('disconnect', \&on_disconnect);
167 $conn->add_global_handler([251,252,253,254,255], \&on_init);
168 # $conn->add_global_handler(302, \&on_init); # userhost
169 $conn->add_global_handler(303, \&on_ison); # notify.
170 $conn->add_global_handler(315, \&on_endofwho);
171 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
172 $conn->add_global_handler(324, \&on_modeis);
173 $conn->add_global_handler(333, \&on_topicinfo);
174 $conn->add_global_handler(352, \&on_who);
175 $conn->add_global_handler(353, \&on_names);
176 $conn->add_global_handler(366, \&on_endofnames);
177 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
178 $conn->add_global_handler(433, \&on_nick_taken);
179 $conn->add_global_handler(439, \&on_targettoofast);
180 # for proper joinnextChan behaviour
181 $conn->add_global_handler(471, \&on_chanfull);
182 $conn->add_global_handler(473, \&on_inviteonly);
183 $conn->add_global_handler(474, \&on_banned);
184 $conn->add_global_handler(475, \&on_badchankey);
185 $conn->add_global_handler(443, \&on_useronchan);
187 # end of handler stuff.
192 ######################################################################
193 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
194 ######################################################################
200 # slow down a bit if traffic is "high".
201 # need to take into account time of last message sent.
202 if ($last{buflen} > 256 and length($buf) > 256) {
206 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
208 $last{buflen} = length($buf);
213 my $mynick = $conn->nick();
216 &WARN("say: msg == $msg.");
221 &status("<$mynick/$talkchannel> $msg");
222 if (&whatInterface() =~ /IRC/) {
223 $msg = "zero" if ($msg =~ /^0+$/);
226 if ($t == $pubtime) {
228 $pubsize += length $msg;
230 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
231 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
233 if ( ($pubcount % $i) == 0 and $pubcount) {
235 } elsif ($pubsize > $j) {
243 $pubsize = length $msg;
246 $conn->privmsg($talkchannel, $msg);
251 my ($nick, $msg) = @_;
252 if (!defined $nick) {
253 &ERROR("msg: nick == NULL.");
259 &WARN("msg: msg == $msg.");
263 &status(">$nick< $msg");
265 return unless (&whatInterface() =~ /IRC/);
268 if ($t == $msgtime) {
270 $msgsize += length $msg;
272 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
273 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
274 if ( ($msgcount % $i) == 0 and $msgcount) {
276 } elsif ($msgsize > $j) {
284 $msgsize = length $msg;
287 $conn->privmsg($nick, $msg);
290 # Usage: &action(nick || chan, txt);
292 my ($target, $txt) = @_;
294 &WARN("action: txt == NULL.");
298 if (length $txt > 480) {
299 &status("action: txt too long; truncating.");
300 chop($txt) while (length $txt > 480);
303 &status("* $ident/$target $txt");
304 $conn->me($target, $txt);
307 # Usage: ¬ice(nick || chan, txt);
309 my ($target, $txt) = @_;
311 &WARN("notice: txt == NULL.");
315 &status("-$target- $txt");
319 if ($t == $nottime) {
321 $notsize += length $txt;
323 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
324 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
326 if ( ($notcount % $i) == 0 and $notcount) {
328 } elsif ($notsize > $j) {
336 $notsize = length $txt;
339 $conn->notice($target, $txt);
343 my ($txt,$flag) = @_;
345 ### FIXME: flag not supported yet.
347 foreach (keys %{ $dcc{'CHAT'} }) {
348 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
353 ### perform commands.
356 # Usage: &performReply($reply);
360 if (!defined $reply or $reply =~ /^\s*$/) {
361 &DEBUG("performReply: reply == NULL.");
365 $reply =~ /([\.\?\s]+)$/;
367 &checkMsgType($reply);
369 if ($msgType eq 'public') {
370 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
371 $reply = "$orig{who}: ".$reply;
373 $reply = "$reply, ".$orig{who};
377 } elsif ($msgType eq 'private') {
379 $reply = "$reply, ".$orig{who};
383 } elsif ($msgType eq 'chat') {
384 if (!exists $dcc{'CHAT'}{$who}) {
385 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
388 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
391 &ERROR("PR: msgType invalid? ($msgType).");
396 sub performAddressedReply {
397 return unless ($addressed);
402 &performStrictReply(@_);
405 # Usage: &performStrictReply($reply);
406 sub performStrictReply {
409 &checkMsgType($reply);
411 if ($msgType eq 'private') {
413 } elsif ($msgType eq 'public') {
415 } elsif ($msgType eq 'chat') {
416 &dccsay(lc $who, $reply);
418 &ERROR("pSR: msgType invalid? ($msgType).");
423 my($who, $reply) = @_;
425 if (!defined $reply or $reply =~ /^\s*$/) {
426 &WARN("dccsay: reply == NULL.");
430 if (!exists $dcc{'CHAT'}{$who}) {
431 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
435 &status("=>$who<= $reply"); # dcc chat.
436 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
443 foreach $type (keys %dcc) {
444 &FIXME("dcc_close: $who");
445 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
446 next unless (scalar @who);
448 &DEBUG("dcc_close... close $who!");
454 my $key = &getChanConf("chankey", $chan) || "";
456 # forgot for about 2 years to implement channel keys when moving
457 # over to Net::IRC...
459 # hopefully validChan is right.
460 if (&validChan($chan)) {
461 &status("join: already on $chan?");
464 &status("joining $b_blue$chan$ob");
466 return if ($conn->join($chan, $key));
467 return if (&validChan($chan));
469 &DEBUG("joinchan: join failed. trying connect!");
479 next if ($chan eq "");
480 $chan =~ tr/A-Z/a-z/; # lowercase.
482 if ($chan !~ /^$mask{chan}$/) {
483 &WARN("part: chan is invalid ($chan)");
487 &status("parting $chan");
488 if (!&validChan($chan)) {
489 &WARN("part: not on $chan; doing anyway");
494 # deletion of $channels{chan} is done in &entryEvt().
499 my ($chan, @modes) = @_;
500 my $modes = join(" ", @modes);
502 if (&validChan($chan) == 0) {
503 &ERROR("mode: invalid chan => '$chan'.");
507 &DEBUG("mode: MODE $chan $modes");
509 # should move to use Net::IRC's $conn->mode()... but too lazy.
510 rawout("MODE $chan $modes");
514 my ($chan, @who) = @_;
515 my $os = "o" x scalar(@who);
517 &mode($chan, "+$os @who");
521 my ($chan, @who) = @_;
522 my $os = "o" x scalar(@who);
524 &mode($chan, "-$os ".@who);
528 my ($nick,$chan,$msg) = @_;
529 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
531 if ($chan ne "" and &validChan($chan) == 0) {
532 &ERROR("kick: invalid channel $chan.");
536 $nick =~ tr/A-Z/a-z/;
538 foreach $chan (@chans) {
539 if (!&IsNickInChan($nick,$chan)) {
540 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
544 if (!exists $channels{$chan}{o}{$ident}) {
545 &status("kick: do not have ops on $chan :(");
549 &status("Kicking $nick from $chan.");
550 $conn->kick($chan, $nick, $msg);
555 my ($mask,$chan) = @_;
556 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
559 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
560 &ERROR("ban: invalid channel $chan.");
564 foreach $chan (@chans) {
565 if (!exists $channels{$chan}{o}{$ident}) {
566 &status("ban: do not have ops on $chan :(");
570 &status("Banning $mask from $chan.");
571 &rawout("MODE $chan +b $mask");
579 my ($mask,$chan) = @_;
580 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
583 &DEBUG("unban: mask = $mask, chan = @chans");
585 foreach $chan (@chans) {
586 if (!exists $channels{$chan}{o}{$ident}) {
587 &status("unBan: do not have ops on $chan :(");
591 &status("Removed ban $mask from $chan.");
592 &rawout("MODE $chan -b $mask");
602 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
603 $conn->quit($quitmsg);
605 &WARN("quit: could not quit!");
612 if (!defined $nick) {
613 &ERROR("nick: nick == NULL.");
617 if (defined $ident and $nick eq $ident) {
618 &WARN("nick: nick == ident == '$ident'.");
623 $bad++ if (exists $nuh{$conn->nick()});
624 $bad++ if (&IsNickInAnyChan($conn->nick()));
627 &WARN("Nick: not going to try and get my nick back. [".
628 scalar(gmtime). "]");
629 # hrm... over time we lose track of our own nick.
633 # FIXME broken for multiple nicks
634 # if ($nick =~ /^$mask{nick}$/) {
635 # &rawout("NICK ".$nick);
637 # if (defined $ident) {
638 # &status("nick: Changing nick to $nick (from $ident)");
639 # # following shouldn't be here :(
642 # &DEBUG("first time nick change.");
648 &DEBUG("nick: failed... why oh why (nick => $nick)");
654 my($who, $chan) = @_;
655 # TODO: check if $who or $chan are invalid.
657 $conn->invite($who, $chan);
661 # Channel related functions...
664 # Usage: &joinNextChan();
666 my @join = getJoinChans(1);
667 my $mynick = "UNDEF";
669 $mynick = $conn->nick() if $conn;
671 my $chan = shift @join;
674 if (my $i = scalar @join) {
675 &status("joinNextChan: $mynick $i chans to join.");
681 if (exists $cache{joinTime}) {
682 my $delta = time() - $cache{joinTime} - 5;
683 my $timestr = &Time2String($delta);
684 # FIXME: @join should be @in instead (hacked to 10)
685 #my $rate = sprintf("%.1f", $delta / @in);
686 my $rate = sprintf("%.1f", $delta / 10);
687 delete $cache{joinTime};
689 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
692 # chanserv check: global channels, in case we missed one.
693 foreach ( &ChanConfList("chanServ_ops") ) {
698 # Usage: &getNickInChans($nick);
703 foreach (keys %channels) {
704 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
711 # Usage: &getNicksInChan($chan);
716 return keys %{ $channels{$chan}{''} };
720 my ($nick,$chan) = @_;
722 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
725 &DEBUG("INIC: chan == NULL.");
729 if (&validChan($chan) == 0) {
730 &ERROR("INIC: invalid channel $chan.");
734 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
737 foreach (keys %channels) {
738 next unless (/[A-Z]/);
739 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
745 sub IsNickInAnyChan {
749 foreach $chan (keys %channels) {
750 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
756 # Usage: &validChan($chan);
758 # TODO: use $c instead?
761 if (!defined $chan or $chan =~ /^\s*$/) {
765 if (lc $chan ne $chan) {
766 &WARN("validChan: lc chan != chan. ($chan); fixing.");
767 $chan =~ tr/A-Z/a-z/;
770 # it's possible that this check creates the hash if empty.
771 if (defined $channels{$chan} or exists $channels{$chan}) {
772 if ($chan =~ /^_?default$/) {
773 # &WARN("validC: chan cannot be _default! returning 0!");
784 # Usage: &delUserInfo($nick,@chans);
786 my ($nick,@chans) = @_;
789 foreach $chan (@chans) {
790 foreach $mode (keys %{ $channels{$chan} }) {
792 next unless (exists $channels{$chan}{$mode}{$nick});
794 delete $channels{$chan}{$mode}{$nick};
802 delete $channels{$chan};
809 $cache{joinTime} = time();
819 # can't join any if not connected
820 return @join if (!$conn);
822 my $nick = $conn->nick();
824 foreach (keys %chanconf) {
825 next if ($_ eq "_default");
828 my $val = $chanconf{$_}{autojoin};
831 $skip++ if ($val eq "0");
833 # convert old +autojoin to autojoin <nick>
835 $chanconf{$_}{autojoin} = $val;
837 $skip++ if ($val ne $nick);
845 if (defined $channels{$_} or exists $channels{$_}) {
854 $str .= ' in:' . join(',', sort @in) if scalar @in;
855 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
856 $str .= ' join:' . join(',', sort @join) if scalar @join;
858 &status("Chans: ($nick)$str") if ($show);
864 # &DEBUG("closeDCC called.");
867 foreach $type (keys %dcc) {
868 next if ($type ne uc($type));
871 foreach $nick (keys %{ $dcc{$type} }) {
872 next unless (defined $nick);
873 &status("DCC CHAT: closing DCC $type to $nick.");
874 next unless (defined $dcc{$type}{$nick});
876 my $ref = $dcc{$type}{$nick};
877 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
878 $dcc{$type}{$nick}->close();
879 delete $dcc{$type}{$nick};
880 &DEBUG("after close for $nick");
887 my($who,$chan,$userhost) = @_;
889 return unless (&IsChanConf("joinfloodCheck"));
891 if (exists $netsplit{lc $who}) { # netsplit join.
892 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
895 if (exists $floodjoin{$chan}{$who}{Time}) {
896 &WARN("floodjoin{$chan}{$who} already exists?");
899 $floodjoin{$chan}{$who}{Time} = time();
900 $floodjoin{$chan}{$who}{Host} = $userhost;
903 foreach (keys %floodjoin) {
905 my $count = scalar keys %{ $floodjoin{$c} };
906 next unless ($count > 5);
907 &DEBUG("joinflood: count => $count");
910 foreach (keys %{ $floodjoin{$c} }) {
911 my $t = $floodjoin{$c}{$_}{Time};
912 next unless (defined $t);
916 &DEBUG("joinflood: time => $time");
919 &DEBUG("joinflood: new time => $time");
925 foreach $chan (keys %floodjoin) {
926 foreach $who (keys %{ $floodjoin{$chan} }) {
927 my $t = $floodjoin{$chan}{$who}{Time};
928 next unless (defined $t);
930 my $delta = $time - $t;
931 next unless ($delta > 10);
933 delete $floodjoin{$chan}{$who};
938 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
944 if (exists $nuh{$n}) {
945 return &makeHostMask($nuh{$n});
947 $cache{on_who_Hack} = 1;