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(@joinchan @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 $conn = $irc->newconn(%args);
104 if (!defined $conn) {
105 &ERROR("internal: perl IRC connection object does not exist.");
111 # change internal timeout value for scheduler.
112 $irc->{_timeout} = 10; # how about 60?
113 # Net::IRC debugging.
116 $ircstats{'Server'} = "$server:$port";
119 $conn->add_handler('caction', \&on_action);
120 $conn->add_handler('cdcc', \&on_dcc);
121 $conn->add_handler('cping', \&on_ping);
122 $conn->add_handler('crping', \&on_ping_reply);
123 $conn->add_handler('cversion', \&on_version);
124 $conn->add_handler('crversion', \&on_crversion);
125 $conn->add_handler('dcc_open', \&on_dcc_open);
126 $conn->add_handler('dcc_close', \&on_dcc_close);
127 $conn->add_handler('chat', \&on_chat);
128 $conn->add_handler('msg', \&on_msg);
129 $conn->add_handler('public', \&on_public);
130 $conn->add_handler('join', \&on_join);
131 $conn->add_handler('part', \&on_part);
132 $conn->add_handler('topic', \&on_topic);
133 $conn->add_handler('invite', \&on_invite);
134 $conn->add_handler('kick', \&on_kick);
135 $conn->add_handler('mode', \&on_mode);
136 $conn->add_handler('nick', \&on_nick);
137 $conn->add_handler('quit', \&on_quit);
138 $conn->add_handler('notice', \&on_notice);
139 $conn->add_handler('whoischannels', \&on_whoischannels);
140 $conn->add_handler('useronchannel', \&on_useronchannel);
141 $conn->add_handler('whois', \&on_whois);
142 $conn->add_handler('other', \&on_other);
143 $conn->add_global_handler('disconnect', \&on_disconnect);
144 $conn->add_global_handler([251,252,253,254,255], \&on_init);
145 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
146 $conn->add_global_handler(303, \&on_ison); # notify.
147 $conn->add_global_handler(315, \&on_endofwho);
148 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
149 $conn->add_global_handler(324, \&on_modeis);
150 $conn->add_global_handler(333, \&on_topicinfo);
151 $conn->add_global_handler(352, \&on_who);
152 $conn->add_global_handler(353, \&on_names);
153 $conn->add_global_handler(366, \&on_endofnames);
154 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
155 $conn->add_global_handler(433, \&on_nick_taken);
156 $conn->add_global_handler(439, \&on_targettoofast);
157 # for proper joinnextChan behaviour
158 $conn->add_global_handler(471, \&on_chanfull);
159 $conn->add_global_handler(473, \&on_inviteonly);
160 $conn->add_global_handler(474, \&on_banned);
161 $conn->add_global_handler(475, \&on_badchankey);
162 $conn->add_global_handler(443, \&on_useronchan);
164 # end of handler stuff.
169 ######################################################################
170 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
171 ######################################################################
177 # slow down a bit if traffic is "high".
178 # need to take into account time of last message sent.
179 if ($last{buflen} > 256 and length($buf) > 256) {
183 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
185 $last{buflen} = length($buf);
192 &WARN("say: msg == $msg.");
196 &status("</$talkchannel> $msg");
197 if (&whatInterface() =~ /IRC/) {
198 $msg = "zero" if ($msg =~ /^0+$/);
201 if ($t == $pubtime) {
203 $pubsize += length $msg;
205 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
206 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
208 if ( ($pubcount % $i) == 0 and $pubcount) {
210 } elsif ($pubsize > $j) {
218 $pubsize = length $msg;
221 $conn->privmsg($talkchannel, $msg);
226 my ($nick, $msg) = @_;
227 if (!defined $nick) {
228 &ERROR("msg: nick == NULL.");
234 &WARN("msg: msg == $msg.");
238 &status(">$nick< $msg");
240 return unless (&whatInterface() =~ /IRC/);
243 if ($t == $msgtime) {
245 $msgsize += length $msg;
247 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
248 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
249 if ( ($msgcount % $i) == 0 and $msgcount) {
251 } elsif ($msgsize > $j) {
259 $msgsize = length $msg;
262 $conn->privmsg($nick, $msg);
265 # Usage: &action(nick || chan, txt);
267 my ($target, $txt) = @_;
269 &WARN("action: txt == NULL.");
273 if (length $txt > 480) {
274 &status("action: txt too long; truncating.");
275 chop($txt) while (length $txt > 480);
278 &status("* $ident/$target $txt");
279 $conn->me($target, $txt);
282 # Usage: ¬ice(nick || chan, txt);
284 my ($target, $txt) = @_;
286 &WARN("notice: txt == NULL.");
290 &status("-$target- $txt");
294 if ($t == $nottime) {
296 $notsize += length $txt;
298 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
299 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
301 if ( ($notcount % $i) == 0 and $notcount) {
303 } elsif ($notsize > $j) {
311 $notsize = length $txt;
314 $conn->notice($target, $txt);
318 my ($txt,$flag) = @_;
320 ### FIXME: flag not supported yet.
322 foreach (keys %{ $dcc{'CHAT'} }) {
323 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
328 ### perform commands.
331 # Usage: &performReply($reply);
335 if (!defined $reply or $reply =~ /^\s*$/) {
336 &DEBUG("performReply: reply == NULL.");
340 $reply =~ /([\.\?\s]+)$/;
342 &checkMsgType($reply);
344 if ($msgType eq 'public') {
345 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
346 $reply = "$orig{who}: ".$reply;
348 $reply = "$reply, ".$orig{who};
352 } elsif ($msgType eq 'private') {
354 $reply = "$reply, ".$orig{who};
358 } elsif ($msgType eq 'chat') {
359 if (!exists $dcc{'CHAT'}{$who}) {
360 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
363 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
366 &ERROR("PR: msgType invalid? ($msgType).");
371 sub performAddressedReply {
372 return unless ($addressed);
377 &performStrictReply(@_);
380 # Usage: &performStrictReply($reply);
381 sub performStrictReply {
384 &checkMsgType($reply);
386 if ($msgType eq 'private') {
388 } elsif ($msgType eq 'public') {
390 } elsif ($msgType eq 'chat') {
391 &dccsay(lc $who, $reply);
393 &ERROR("pSR: msgType invalid? ($msgType).");
398 my($who, $reply) = @_;
400 if (!defined $reply or $reply =~ /^\s*$/) {
401 &WARN("dccsay: reply == NULL.");
405 if (!exists $dcc{'CHAT'}{$who}) {
406 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
410 &status("=>$who<= $reply"); # dcc chat.
411 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
418 foreach $type (keys %dcc) {
419 &FIXME("dcc_close: $who");
420 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
421 next unless (scalar @who);
423 &DEBUG("dcc_close... close $who!");
429 my $key = &getChanConf("chankey", $chan) || "";
431 # forgot for about 2 years to implement channel keys when moving
432 # over to Net::IRC...
434 # hopefully validChan is right.
435 if (&validChan($chan)) {
436 &status("join: already on $chan");
438 &status("joining $b_blue$chan$ob");
440 return if ($conn->join($chan, $key));
442 &DEBUG("joinchan: join failed. trying connect!");
452 next if ($chan eq "");
453 $chan =~ tr/A-Z/a-z/; # lowercase.
455 if ($chan !~ /^$mask{chan}$/) {
456 &WARN("part: chan is invalid ($chan)");
460 &status("parting $chan");
461 if (!&validChan($chan)) {
462 &WARN("part: not on $chan; doing anyway");
467 # deletion of $channels{chan} is done in &entryEvt().
472 my ($chan, @modes) = @_;
473 my $modes = join(" ", @modes);
475 if (&validChan($chan) == 0) {
476 &ERROR("mode: invalid chan => '$chan'.");
480 &DEBUG("mode: MODE $chan $modes");
482 # should move to use Net::IRC's $conn->mode()... but too lazy.
483 rawout("MODE $chan $modes");
487 my ($chan, @who) = @_;
488 my $os = "o" x scalar(@who);
490 &mode($chan, "+$os @who");
494 my ($chan, @who) = @_;
495 my $os = "o" x scalar(@who);
497 &mode($chan, "-$os ".@who);
501 my ($nick,$chan,$msg) = @_;
502 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
504 if ($chan ne "" and &validChan($chan) == 0) {
505 &ERROR("kick: invalid channel $chan.");
509 $nick =~ tr/A-Z/a-z/;
511 foreach $chan (@chans) {
512 if (!&IsNickInChan($nick,$chan)) {
513 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
517 if (!exists $channels{$chan}{o}{$ident}) {
518 &status("kick: do not have ops on $chan :(");
522 &status("Kicking $nick from $chan.");
523 $conn->kick($chan, $nick, $msg);
528 my ($mask,$chan) = @_;
529 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
532 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
533 &ERROR("ban: invalid channel $chan.");
537 foreach $chan (@chans) {
538 if (!exists $channels{$chan}{o}{$ident}) {
539 &status("ban: do not have ops on $chan :(");
543 &status("Banning $mask from $chan.");
544 &rawout("MODE $chan +b $mask");
552 my ($mask,$chan) = @_;
553 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
556 &DEBUG("unban: mask = $mask, chan = @chans");
558 foreach $chan (@chans) {
559 if (!exists $channels{$chan}{o}{$ident}) {
560 &status("unBan: do not have ops on $chan :(");
564 &status("Removed ban $mask from $chan.");
565 &rawout("MODE $chan -b $mask");
574 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
576 $conn->quit($quitmsg);
578 &WARN("quit: could not quit!");
585 if (!defined $nick) {
586 &ERROR("nick: nick == NULL.");
590 if (defined $ident and $nick eq $ident) {
591 &WARN("nick: nick == ident == '$ident'.");
596 $bad++ if (exists $nuh{ $param{'ircNick'} });
597 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
600 &WARN("Nick: not going to try and get my nick back. [".
601 scalar(gmtime). "]");
602 # hrm... over time we lose track of our own nick.
606 if ($nick =~ /^$mask{nick}$/) {
607 &rawout("NICK ".$nick);
609 if (defined $ident) {
610 &status("nick: Changing nick to $nick (from $ident)");
611 # following shouldn't be here :(
614 &DEBUG("first time nick change.");
620 &DEBUG("nick: failed... why oh why (nick => $nick)");
626 my($who, $chan) = @_;
627 # todo: check if $who or $chan are invalid.
629 $conn->invite($who, $chan);
633 # Channel related functions...
636 # Usage: &joinNextChan();
638 if (scalar @joinchan) {
639 my $chan = shift @joinchan;
642 if (my $i = scalar @joinchan) {
643 &status("joinNextChan: $i chans to join.");
650 my @c = &getJoinChans();
651 if (exists $cache{joinTime} and scalar @c) {
652 my $delta = time() - $cache{joinTime} - 5;
653 my $timestr = &Time2String($delta);
654 my $rate = sprintf("%.1f", $delta / @c);
655 delete $cache{joinTime};
657 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
660 # chanserv check: global channels, in case we missed one.
661 foreach ( &ChanConfList("chanServ_ops") ) {
666 # Usage: &getNickInChans($nick);
671 foreach (keys %channels) {
672 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
679 # Usage: &getNicksInChan($chan);
684 return keys %{ $channels{$chan}{''} };
688 my ($nick,$chan) = @_;
690 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
693 &DEBUG("INIC: chan == NULL.");
697 if (&validChan($chan) == 0) {
698 &ERROR("INIC: invalid channel $chan.");
702 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
705 foreach (keys %channels) {
706 next unless (/[A-Z]/);
707 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
713 sub IsNickInAnyChan {
717 foreach $chan (keys %channels) {
718 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
724 # Usage: &validChan($chan);
726 # todo: use $c instead?
729 if (!defined $chan or $chan =~ /^\s*$/) {
733 if (lc $chan ne $chan) {
734 &WARN("validChan: lc chan != chan. ($chan); fixing.");
735 $chan =~ tr/A-Z/a-z/;
738 # it's possible that this check creates the hash if empty.
739 if (defined $channels{$chan} or exists $channels{$chan}) {
740 if ($chan =~ /^_?default$/) {
741 # &WARN("validC: chan cannot be _default! returning 0!");
752 # Usage: &delUserInfo($nick,@chans);
754 my ($nick,@chans) = @_;
757 foreach $chan (@chans) {
758 foreach $mode (keys %{ $channels{$chan} }) {
760 next unless (exists $channels{$chan}{$mode}{$nick});
762 delete $channels{$chan}{$mode}{$nick};
770 delete $channels{$chan};
777 @joinchan = &getJoinChans(1);
778 $cache{joinTime} = time();
786 foreach (keys %chanconf) {
787 next if ($_ eq "_default");
789 my $val = $chanconf{$_}{autojoin};
793 $skip++ if ($val eq "0");
808 $str = "channels not auto-joining: @skip (joining: @chans)";
810 $str = "auto-joining all chans: @chans";
813 &status("Chans: ".$str) if ($show);
819 # &DEBUG("closeDCC called.");
822 foreach $type (keys %dcc) {
823 next if ($type ne uc($type));
826 foreach $nick (keys %{ $dcc{$type} }) {
827 next unless (defined $nick);
828 &status("DCC CHAT: closing DCC $type to $nick.");
829 next unless (defined $dcc{$type}{$nick});
831 my $ref = $dcc{$type}{$nick};
832 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
833 $dcc{$type}{$nick}->close();
834 delete $dcc{$type}{$nick};
835 &DEBUG("after close for $nick");
842 my($who,$chan,$userhost) = @_;
844 return unless (&IsChanConf("joinfloodCheck"));
846 if (exists $netsplit{lc $who}) { # netsplit join.
847 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
850 if (exists $floodjoin{$chan}{$who}{Time}) {
851 &WARN("floodjoin{$chan}{$who} already exists?");
854 $floodjoin{$chan}{$who}{Time} = time();
855 $floodjoin{$chan}{$who}{Host} = $userhost;
858 foreach (keys %floodjoin) {
860 my $count = scalar keys %{ $floodjoin{$c} };
861 next unless ($count > 5);
862 &DEBUG("joinflood: count => $count");
865 foreach (keys %{ $floodjoin{$c} }) {
866 my $t = $floodjoin{$c}{$_}{Time};
867 next unless (defined $t);
871 &DEBUG("joinflood: time => $time");
874 &DEBUG("joinflood: new time => $time");
880 foreach $chan (keys %floodjoin) {
881 foreach $who (keys %{ $floodjoin{$chan} }) {
882 my $t = $floodjoin{$chan}{$who}{Time};
883 next unless (defined $t);
885 my $delta = $time - $t;
886 next unless ($delta > 10);
888 delete $floodjoin{$chan}{$who};
893 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
899 if (exists $nuh{$n}) {
900 return &makeHostMask($nuh{$n});
902 $cache{on_who_Hack} = 1;