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);
27 while (my $host = shift @ircServers) {
28 # JUST IN CASE. irq was complaining about this.
29 if ($lastrun == time()) {
30 &DEBUG("ircloop: hrm... lastrun == time()");
37 &DEBUG("ircloop: ircServers[x] = NULL.");
41 next unless (exists $ircPort{$host});
43 my $retval = &irc($host, $ircPort{$host});
44 next unless (defined $retval and $retval == 0);
47 if ($error % 3 == 0 and $error != 0) {
48 &status("IRC: Could not connect.");
54 &status("IRC: cannot connect to any IRC servers; stopping.");
60 &status("IRC: ok, done one cycle of IRC servers; trying again.");
67 my ($server,$port) = @_;
69 my $iaddr = inet_aton($server);
70 my $paddr = sockaddr_in($port, $iaddr);
71 my $proto = getprotobyname('tcp');
74 &status("Connecting to port $port of server $server ...");
77 if ($server =~ /\D$/) {
78 my $packed = scalar(gethostbyname($server));
80 if (!defined $packed) {
81 &status(" cannot resolve $server.");
85 my $resolve = inet_ntoa($packed);
86 &status(" resolved to $resolve.");
87 ### warning in Sys/Hostname line 78???
88 ### caused inside Net::IRC?
94 Nick => $param{'ircNick'},
97 Ircname => $param{'ircName'},
99 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
100 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
102 my $conn = $irc->newconn(%args);
104 if (!defined $conn) {
105 &ERROR("IRC: connection failed.");
106 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
107 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
108 &ERROR("127.0.0.1 localhost localhost");
109 &ERROR("If this is still a problem, please contact the maintainer.");
113 if ($param{'ircNick2'}) {
114 # prep for real multiple nick/server connects
115 # FIXME: all code should get nick/server out of self, not config
116 $args{'Nick'} = $param{'ircNick2'};
117 my $conn = $irc->newconn(%args);
120 if ($param{'ircNick3'}) {
121 # prep for real multiple nick/server connects
122 # FIXME: all code should get nick/server out of self, not config
123 $args{'Nick'} = $param{'ircNick3'};
124 my $conn = $irc->newconn(%args);
129 # change internal timeout value for scheduler.
130 $irc->{_timeout} = 10; # how about 60?
131 # Net::IRC debugging.
134 $ircstats{'Server'} = "$server:$port";
137 $conn->add_global_handler('caction', \&on_action);
138 $conn->add_global_handler('cdcc', \&on_dcc);
139 $conn->add_global_handler('cping', \&on_ping);
140 $conn->add_global_handler('crping', \&on_ping_reply);
141 $conn->add_global_handler('cversion', \&on_version);
142 $conn->add_global_handler('crversion', \&on_crversion);
143 $conn->add_global_handler('dcc_open', \&on_dcc_open);
144 $conn->add_global_handler('dcc_close', \&on_dcc_close);
145 $conn->add_global_handler('chat', \&on_chat);
146 $conn->add_global_handler('msg', \&on_msg);
147 $conn->add_global_handler('public', \&on_public);
148 $conn->add_global_handler('join', \&on_join);
149 $conn->add_global_handler('part', \&on_part);
150 $conn->add_global_handler('topic', \&on_topic);
151 $conn->add_global_handler('invite', \&on_invite);
152 $conn->add_global_handler('kick', \&on_kick);
153 $conn->add_global_handler('mode', \&on_mode);
154 $conn->add_global_handler('nick', \&on_nick);
155 $conn->add_global_handler('quit', \&on_quit);
156 $conn->add_global_handler('notice', \&on_notice);
157 $conn->add_global_handler('whoischannels', \&on_whoischannels);
158 $conn->add_global_handler('useronchannel', \&on_useronchannel);
159 $conn->add_global_handler('whois', \&on_whois);
160 $conn->add_global_handler('other', \&on_other);
161 $conn->add_global_handler('disconnect', \&on_disconnect);
162 $conn->add_global_handler([251,252,253,254,255], \&on_init);
163 # $conn->add_global_handler(302, \&on_init); # userhost
164 $conn->add_global_handler(303, \&on_ison); # notify.
165 $conn->add_global_handler(315, \&on_endofwho);
166 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
167 $conn->add_global_handler(324, \&on_modeis);
168 $conn->add_global_handler(333, \&on_topicinfo);
169 $conn->add_global_handler(352, \&on_who);
170 $conn->add_global_handler(353, \&on_names);
171 $conn->add_global_handler(366, \&on_endofnames);
172 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
173 $conn->add_global_handler(433, \&on_nick_taken);
174 $conn->add_global_handler(439, \&on_targettoofast);
175 # for proper joinnextChan behaviour
176 $conn->add_global_handler(471, \&on_chanfull);
177 $conn->add_global_handler(473, \&on_inviteonly);
178 $conn->add_global_handler(474, \&on_banned);
179 $conn->add_global_handler(475, \&on_badchankey);
180 $conn->add_global_handler(443, \&on_useronchan);
182 # end of handler stuff.
187 ######################################################################
188 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
189 ######################################################################
195 # slow down a bit if traffic is "high".
196 # need to take into account time of last message sent.
197 if ($last{buflen} > 256 and length($buf) > 256) {
201 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
203 $last{buflen} = length($buf);
210 &WARN("say: msg == $msg.");
214 &status("</$talkchannel> $msg");
215 if (&whatInterface() =~ /IRC/) {
216 $msg = "zero" if ($msg =~ /^0+$/);
219 if ($t == $pubtime) {
221 $pubsize += length $msg;
223 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
224 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
226 if ( ($pubcount % $i) == 0 and $pubcount) {
228 } elsif ($pubsize > $j) {
236 $pubsize = length $msg;
239 $conn->privmsg($talkchannel, $msg);
244 my ($nick, $msg) = @_;
245 if (!defined $nick) {
246 &ERROR("msg: nick == NULL.");
252 &WARN("msg: msg == $msg.");
256 &status(">$nick< $msg");
258 return unless (&whatInterface() =~ /IRC/);
261 if ($t == $msgtime) {
263 $msgsize += length $msg;
265 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
266 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
267 if ( ($msgcount % $i) == 0 and $msgcount) {
269 } elsif ($msgsize > $j) {
277 $msgsize = length $msg;
280 $conn->privmsg($nick, $msg);
283 # Usage: &action(nick || chan, txt);
285 my ($target, $txt) = @_;
287 &WARN("action: txt == NULL.");
291 if (length $txt > 480) {
292 &status("action: txt too long; truncating.");
293 chop($txt) while (length $txt > 480);
296 &status("* $ident/$target $txt");
297 $conn->me($target, $txt);
300 # Usage: ¬ice(nick || chan, txt);
302 my ($target, $txt) = @_;
304 &WARN("notice: txt == NULL.");
308 &status("-$target- $txt");
312 if ($t == $nottime) {
314 $notsize += length $txt;
316 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
317 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
319 if ( ($notcount % $i) == 0 and $notcount) {
321 } elsif ($notsize > $j) {
329 $notsize = length $txt;
332 $conn->notice($target, $txt);
336 my ($txt,$flag) = @_;
338 ### FIXME: flag not supported yet.
340 foreach (keys %{ $dcc{'CHAT'} }) {
341 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
346 ### perform commands.
349 # Usage: &performReply($reply);
353 if (!defined $reply or $reply =~ /^\s*$/) {
354 &DEBUG("performReply: reply == NULL.");
358 $reply =~ /([\.\?\s]+)$/;
360 &checkMsgType($reply);
362 if ($msgType eq 'public') {
363 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
364 $reply = "$orig{who}: ".$reply;
366 $reply = "$reply, ".$orig{who};
370 } elsif ($msgType eq 'private') {
372 $reply = "$reply, ".$orig{who};
376 } elsif ($msgType eq 'chat') {
377 if (!exists $dcc{'CHAT'}{$who}) {
378 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
381 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
384 &ERROR("PR: msgType invalid? ($msgType).");
389 sub performAddressedReply {
390 return unless ($addressed);
395 &performStrictReply(@_);
398 # Usage: &performStrictReply($reply);
399 sub performStrictReply {
402 &checkMsgType($reply);
404 if ($msgType eq 'private') {
406 } elsif ($msgType eq 'public') {
408 } elsif ($msgType eq 'chat') {
409 &dccsay(lc $who, $reply);
411 &ERROR("pSR: msgType invalid? ($msgType).");
416 my($who, $reply) = @_;
418 if (!defined $reply or $reply =~ /^\s*$/) {
419 &WARN("dccsay: reply == NULL.");
423 if (!exists $dcc{'CHAT'}{$who}) {
424 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
428 &status("=>$who<= $reply"); # dcc chat.
429 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
436 foreach $type (keys %dcc) {
437 &FIXME("dcc_close: $who");
438 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
439 next unless (scalar @who);
441 &DEBUG("dcc_close... close $who!");
447 my $key = &getChanConf("chankey", $chan) || "";
449 # forgot for about 2 years to implement channel keys when moving
450 # over to Net::IRC...
452 # hopefully validChan is right.
453 if (&validChan($chan)) {
454 &status("join: already on $chan?");
457 &status("joining $b_blue$chan$ob");
459 return if ($conn->join($chan, $key));
460 return if (&validChan($chan));
462 &DEBUG("joinchan: join failed. trying connect!");
472 next if ($chan eq "");
473 $chan =~ tr/A-Z/a-z/; # lowercase.
475 if ($chan !~ /^$mask{chan}$/) {
476 &WARN("part: chan is invalid ($chan)");
480 &status("parting $chan");
481 if (!&validChan($chan)) {
482 &WARN("part: not on $chan; doing anyway");
487 # deletion of $channels{chan} is done in &entryEvt().
492 my ($chan, @modes) = @_;
493 my $modes = join(" ", @modes);
495 if (&validChan($chan) == 0) {
496 &ERROR("mode: invalid chan => '$chan'.");
500 &DEBUG("mode: MODE $chan $modes");
502 # should move to use Net::IRC's $conn->mode()... but too lazy.
503 rawout("MODE $chan $modes");
507 my ($chan, @who) = @_;
508 my $os = "o" x scalar(@who);
510 &mode($chan, "+$os @who");
514 my ($chan, @who) = @_;
515 my $os = "o" x scalar(@who);
517 &mode($chan, "-$os ".@who);
521 my ($nick,$chan,$msg) = @_;
522 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
524 if ($chan ne "" and &validChan($chan) == 0) {
525 &ERROR("kick: invalid channel $chan.");
529 $nick =~ tr/A-Z/a-z/;
531 foreach $chan (@chans) {
532 if (!&IsNickInChan($nick,$chan)) {
533 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
537 if (!exists $channels{$chan}{o}{$ident}) {
538 &status("kick: do not have ops on $chan :(");
542 &status("Kicking $nick from $chan.");
543 $conn->kick($chan, $nick, $msg);
548 my ($mask,$chan) = @_;
549 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
552 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
553 &ERROR("ban: invalid channel $chan.");
557 foreach $chan (@chans) {
558 if (!exists $channels{$chan}{o}{$ident}) {
559 &status("ban: do not have ops on $chan :(");
563 &status("Banning $mask from $chan.");
564 &rawout("MODE $chan +b $mask");
572 my ($mask,$chan) = @_;
573 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
576 &DEBUG("unban: mask = $mask, chan = @chans");
578 foreach $chan (@chans) {
579 if (!exists $channels{$chan}{o}{$ident}) {
580 &status("unBan: do not have ops on $chan :(");
584 &status("Removed ban $mask from $chan.");
585 &rawout("MODE $chan -b $mask");
595 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
596 $conn->quit($quitmsg);
598 &WARN("quit: could not quit!");
605 if (!defined $nick) {
606 &ERROR("nick: nick == NULL.");
610 if (defined $ident and $nick eq $ident) {
611 &WARN("nick: nick == ident == '$ident'.");
616 $bad++ if (exists $nuh{$conn->nick()});
617 $bad++ if (&IsNickInAnyChan($conn->nick()));
620 &WARN("Nick: not going to try and get my nick back. [".
621 scalar(gmtime). "]");
622 # hrm... over time we lose track of our own nick.
626 if ($nick =~ /^$mask{nick}$/) {
627 &rawout("NICK ".$nick);
629 if (defined $ident) {
630 &status("nick: Changing nick to $nick (from $ident)");
631 # following shouldn't be here :(
634 &DEBUG("first time nick change.");
640 &DEBUG("nick: failed... why oh why (nick => $nick)");
646 my($who, $chan) = @_;
647 # TODO: check if $who or $chan are invalid.
649 $conn->invite($who, $chan);
653 # Channel related functions...
656 # Usage: &joinNextChan();
658 my @join = getJoinChans(1);
659 my $mynick = "UNDEFINED";
661 $mynick = $conn->nick() if $conn;
663 my $chan = shift @join;
666 if (my $i = scalar @join) {
667 &status("joinNextChan: $mynick $i chans to join.");
673 if (exists $cache{joinTime}) {
674 my $delta = time() - $cache{joinTime} - 5;
675 my $timestr = &Time2String($delta);
676 # FIXME: @join should be @in instead (hacked to 10)
677 #my $rate = sprintf("%.1f", $delta / @in);
678 my $rate = sprintf("%.1f", $delta / 10);
679 delete $cache{joinTime};
681 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
684 # chanserv check: global channels, in case we missed one.
685 foreach ( &ChanConfList("chanServ_ops") ) {
690 # Usage: &getNickInChans($nick);
695 foreach (keys %channels) {
696 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
703 # Usage: &getNicksInChan($chan);
708 return keys %{ $channels{$chan}{''} };
712 my ($nick,$chan) = @_;
714 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
717 &DEBUG("INIC: chan == NULL.");
721 if (&validChan($chan) == 0) {
722 &ERROR("INIC: invalid channel $chan.");
726 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
729 foreach (keys %channels) {
730 next unless (/[A-Z]/);
731 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
737 sub IsNickInAnyChan {
741 foreach $chan (keys %channels) {
742 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
748 # Usage: &validChan($chan);
750 # TODO: use $c instead?
753 if (!defined $chan or $chan =~ /^\s*$/) {
757 if (lc $chan ne $chan) {
758 &WARN("validChan: lc chan != chan. ($chan); fixing.");
759 $chan =~ tr/A-Z/a-z/;
762 # it's possible that this check creates the hash if empty.
763 if (defined $channels{$chan} or exists $channels{$chan}) {
764 if ($chan =~ /^_?default$/) {
765 # &WARN("validC: chan cannot be _default! returning 0!");
776 # Usage: &delUserInfo($nick,@chans);
778 my ($nick,@chans) = @_;
781 foreach $chan (@chans) {
782 foreach $mode (keys %{ $channels{$chan} }) {
784 next unless (exists $channels{$chan}{$mode}{$nick});
786 delete $channels{$chan}{$mode}{$nick};
794 delete $channels{$chan};
801 $cache{joinTime} = time();
811 # can't join any if not connected
812 return @join if (!$conn);
814 my $nick = $conn->nick();
816 foreach (keys %chanconf) {
817 next if ($_ eq "_default");
820 my $val = $chanconf{$_}{autojoin};
823 $skip++ if ($val eq "0");
825 # convert old +autojoin to autojoin <nick>
827 $chanconf{$_}{autojoin} = $val;
829 $skip++ if ($val ne $nick);
837 if (defined $channels{$_} or exists $channels{$_}) {
846 $str .= ' in:' . join(',', sort @in) if scalar @in;
847 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
848 $str .= ' join:' . join(',', sort @join) if scalar @join;
850 &status("Chans: ($nick)$str") if ($show);
856 # &DEBUG("closeDCC called.");
859 foreach $type (keys %dcc) {
860 next if ($type ne uc($type));
863 foreach $nick (keys %{ $dcc{$type} }) {
864 next unless (defined $nick);
865 &status("DCC CHAT: closing DCC $type to $nick.");
866 next unless (defined $dcc{$type}{$nick});
868 my $ref = $dcc{$type}{$nick};
869 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
870 $dcc{$type}{$nick}->close();
871 delete $dcc{$type}{$nick};
872 &DEBUG("after close for $nick");
879 my($who,$chan,$userhost) = @_;
881 return unless (&IsChanConf("joinfloodCheck"));
883 if (exists $netsplit{lc $who}) { # netsplit join.
884 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
887 if (exists $floodjoin{$chan}{$who}{Time}) {
888 &WARN("floodjoin{$chan}{$who} already exists?");
891 $floodjoin{$chan}{$who}{Time} = time();
892 $floodjoin{$chan}{$who}{Host} = $userhost;
895 foreach (keys %floodjoin) {
897 my $count = scalar keys %{ $floodjoin{$c} };
898 next unless ($count > 5);
899 &DEBUG("joinflood: count => $count");
902 foreach (keys %{ $floodjoin{$c} }) {
903 my $t = $floodjoin{$c}{$_}{Time};
904 next unless (defined $t);
908 &DEBUG("joinflood: time => $time");
911 &DEBUG("joinflood: new time => $time");
917 foreach $chan (keys %floodjoin) {
918 foreach $who (keys %{ $floodjoin{$chan} }) {
919 my $t = $floodjoin{$chan}{$who}{Time};
920 next unless (defined $t);
922 my $delta = $time - $t;
923 next unless ($delta > 10);
925 delete $floodjoin{$chan}{$who};
930 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
936 if (exists $nuh{$n}) {
937 return &makeHostMask($nuh{$n});
939 $cache{on_who_Hack} = 1;