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("IRC: connection failed.");
106 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
107 &ERROR("127.0.0.1 localhost localhost");
108 &ERROR("If this is still a problem, please contact the maintainer.");
114 # change internal timeout value for scheduler.
115 $irc->{_timeout} = 10; # how about 60?
116 # Net::IRC debugging.
119 $ircstats{'Server'} = "$server:$port";
122 $conn->add_handler('caction', \&on_action);
123 $conn->add_handler('cdcc', \&on_dcc);
124 $conn->add_handler('cping', \&on_ping);
125 $conn->add_handler('crping', \&on_ping_reply);
126 $conn->add_handler('cversion', \&on_version);
127 $conn->add_handler('crversion', \&on_crversion);
128 $conn->add_handler('dcc_open', \&on_dcc_open);
129 $conn->add_handler('dcc_close', \&on_dcc_close);
130 $conn->add_handler('chat', \&on_chat);
131 $conn->add_handler('msg', \&on_msg);
132 $conn->add_handler('public', \&on_public);
133 $conn->add_handler('join', \&on_join);
134 $conn->add_handler('part', \&on_part);
135 $conn->add_handler('topic', \&on_topic);
136 $conn->add_handler('invite', \&on_invite);
137 $conn->add_handler('kick', \&on_kick);
138 $conn->add_handler('mode', \&on_mode);
139 $conn->add_handler('nick', \&on_nick);
140 $conn->add_handler('quit', \&on_quit);
141 $conn->add_handler('notice', \&on_notice);
142 $conn->add_handler('whoischannels', \&on_whoischannels);
143 $conn->add_handler('useronchannel', \&on_useronchannel);
144 $conn->add_handler('whois', \&on_whois);
145 $conn->add_handler('other', \&on_other);
146 $conn->add_global_handler('disconnect', \&on_disconnect);
147 $conn->add_global_handler([251,252,253,254,255], \&on_init);
148 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
149 $conn->add_global_handler(303, \&on_ison); # notify.
150 $conn->add_global_handler(315, \&on_endofwho);
151 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
152 $conn->add_global_handler(324, \&on_modeis);
153 $conn->add_global_handler(333, \&on_topicinfo);
154 $conn->add_global_handler(352, \&on_who);
155 $conn->add_global_handler(353, \&on_names);
156 $conn->add_global_handler(366, \&on_endofnames);
157 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
158 $conn->add_global_handler(433, \&on_nick_taken);
159 $conn->add_global_handler(439, \&on_targettoofast);
160 # for proper joinnextChan behaviour
161 $conn->add_global_handler(471, \&on_chanfull);
162 $conn->add_global_handler(473, \&on_inviteonly);
163 $conn->add_global_handler(474, \&on_banned);
164 $conn->add_global_handler(475, \&on_badchankey);
165 $conn->add_global_handler(443, \&on_useronchan);
167 # end of handler stuff.
172 ######################################################################
173 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
174 ######################################################################
180 # slow down a bit if traffic is "high".
181 # need to take into account time of last message sent.
182 if ($last{buflen} > 256 and length($buf) > 256) {
186 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
188 $last{buflen} = length($buf);
195 &WARN("say: msg == $msg.");
199 &status("</$talkchannel> $msg");
200 if (&whatInterface() =~ /IRC/) {
201 $msg = "zero" if ($msg =~ /^0+$/);
204 if ($t == $pubtime) {
206 $pubsize += length $msg;
208 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
209 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
211 if ( ($pubcount % $i) == 0 and $pubcount) {
213 } elsif ($pubsize > $j) {
221 $pubsize = length $msg;
224 $conn->privmsg($talkchannel, $msg);
229 my ($nick, $msg) = @_;
230 if (!defined $nick) {
231 &ERROR("msg: nick == NULL.");
237 &WARN("msg: msg == $msg.");
241 &status(">$nick< $msg");
243 return unless (&whatInterface() =~ /IRC/);
246 if ($t == $msgtime) {
248 $msgsize += length $msg;
250 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
251 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
252 if ( ($msgcount % $i) == 0 and $msgcount) {
254 } elsif ($msgsize > $j) {
262 $msgsize = length $msg;
265 $conn->privmsg($nick, $msg);
268 # Usage: &action(nick || chan, txt);
270 my ($target, $txt) = @_;
272 &WARN("action: txt == NULL.");
276 if (length $txt > 480) {
277 &status("action: txt too long; truncating.");
278 chop($txt) while (length $txt > 480);
281 &status("* $ident/$target $txt");
282 $conn->me($target, $txt);
285 # Usage: ¬ice(nick || chan, txt);
287 my ($target, $txt) = @_;
289 &WARN("notice: txt == NULL.");
293 &status("-$target- $txt");
297 if ($t == $nottime) {
299 $notsize += length $txt;
301 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
302 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
304 if ( ($notcount % $i) == 0 and $notcount) {
306 } elsif ($notsize > $j) {
314 $notsize = length $txt;
317 $conn->notice($target, $txt);
321 my ($txt,$flag) = @_;
323 ### FIXME: flag not supported yet.
325 foreach (keys %{ $dcc{'CHAT'} }) {
326 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
331 ### perform commands.
334 # Usage: &performReply($reply);
338 if (!defined $reply or $reply =~ /^\s*$/) {
339 &DEBUG("performReply: reply == NULL.");
343 $reply =~ /([\.\?\s]+)$/;
345 &checkMsgType($reply);
347 if ($msgType eq 'public') {
348 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
349 $reply = "$orig{who}: ".$reply;
351 $reply = "$reply, ".$orig{who};
355 } elsif ($msgType eq 'private') {
357 $reply = "$reply, ".$orig{who};
361 } elsif ($msgType eq 'chat') {
362 if (!exists $dcc{'CHAT'}{$who}) {
363 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
366 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
369 &ERROR("PR: msgType invalid? ($msgType).");
374 sub performAddressedReply {
375 return unless ($addressed);
380 &performStrictReply(@_);
383 # Usage: &performStrictReply($reply);
384 sub performStrictReply {
387 &checkMsgType($reply);
389 if ($msgType eq 'private') {
391 } elsif ($msgType eq 'public') {
393 } elsif ($msgType eq 'chat') {
394 &dccsay(lc $who, $reply);
396 &ERROR("pSR: msgType invalid? ($msgType).");
401 my($who, $reply) = @_;
403 if (!defined $reply or $reply =~ /^\s*$/) {
404 &WARN("dccsay: reply == NULL.");
408 if (!exists $dcc{'CHAT'}{$who}) {
409 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
413 &status("=>$who<= $reply"); # dcc chat.
414 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
421 foreach $type (keys %dcc) {
422 &FIXME("dcc_close: $who");
423 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
424 next unless (scalar @who);
426 &DEBUG("dcc_close... close $who!");
432 my $key = &getChanConf("chankey", $chan) || "";
434 # forgot for about 2 years to implement channel keys when moving
435 # over to Net::IRC...
437 # hopefully validChan is right.
438 if (&validChan($chan)) {
439 &status("join: already on $chan");
441 &status("joining $b_blue$chan$ob");
443 return if ($conn->join($chan, $key));
445 &DEBUG("joinchan: join failed. trying connect!");
455 next if ($chan eq "");
456 $chan =~ tr/A-Z/a-z/; # lowercase.
458 if ($chan !~ /^$mask{chan}$/) {
459 &WARN("part: chan is invalid ($chan)");
463 &status("parting $chan");
464 if (!&validChan($chan)) {
465 &WARN("part: not on $chan; doing anyway");
470 # deletion of $channels{chan} is done in &entryEvt().
475 my ($chan, @modes) = @_;
476 my $modes = join(" ", @modes);
478 if (&validChan($chan) == 0) {
479 &ERROR("mode: invalid chan => '$chan'.");
483 &DEBUG("mode: MODE $chan $modes");
485 # should move to use Net::IRC's $conn->mode()... but too lazy.
486 rawout("MODE $chan $modes");
490 my ($chan, @who) = @_;
491 my $os = "o" x scalar(@who);
493 &mode($chan, "+$os @who");
497 my ($chan, @who) = @_;
498 my $os = "o" x scalar(@who);
500 &mode($chan, "-$os ".@who);
504 my ($nick,$chan,$msg) = @_;
505 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
507 if ($chan ne "" and &validChan($chan) == 0) {
508 &ERROR("kick: invalid channel $chan.");
512 $nick =~ tr/A-Z/a-z/;
514 foreach $chan (@chans) {
515 if (!&IsNickInChan($nick,$chan)) {
516 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
520 if (!exists $channels{$chan}{o}{$ident}) {
521 &status("kick: do not have ops on $chan :(");
525 &status("Kicking $nick from $chan.");
526 $conn->kick($chan, $nick, $msg);
531 my ($mask,$chan) = @_;
532 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
535 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
536 &ERROR("ban: invalid channel $chan.");
540 foreach $chan (@chans) {
541 if (!exists $channels{$chan}{o}{$ident}) {
542 &status("ban: do not have ops on $chan :(");
546 &status("Banning $mask from $chan.");
547 &rawout("MODE $chan +b $mask");
555 my ($mask,$chan) = @_;
556 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
559 &DEBUG("unban: mask = $mask, chan = @chans");
561 foreach $chan (@chans) {
562 if (!exists $channels{$chan}{o}{$ident}) {
563 &status("unBan: do not have ops on $chan :(");
567 &status("Removed ban $mask from $chan.");
568 &rawout("MODE $chan -b $mask");
577 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
579 $conn->quit($quitmsg);
581 &WARN("quit: could not quit!");
588 if (!defined $nick) {
589 &ERROR("nick: nick == NULL.");
593 if (defined $ident and $nick eq $ident) {
594 &WARN("nick: nick == ident == '$ident'.");
599 $bad++ if (exists $nuh{ $param{'ircNick'} });
600 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
603 &WARN("Nick: not going to try and get my nick back. [".
604 scalar(gmtime). "]");
605 # hrm... over time we lose track of our own nick.
609 if ($nick =~ /^$mask{nick}$/) {
610 &rawout("NICK ".$nick);
612 if (defined $ident) {
613 &status("nick: Changing nick to $nick (from $ident)");
614 # following shouldn't be here :(
617 &DEBUG("first time nick change.");
623 &DEBUG("nick: failed... why oh why (nick => $nick)");
629 my($who, $chan) = @_;
630 # todo: check if $who or $chan are invalid.
632 $conn->invite($who, $chan);
636 # Channel related functions...
639 # Usage: &joinNextChan();
641 if (scalar @joinchan) {
642 my $chan = shift @joinchan;
645 if (my $i = scalar @joinchan) {
646 &status("joinNextChan: $i chans to join.");
653 my @c = &getJoinChans();
654 if (exists $cache{joinTime} and scalar @c) {
655 my $delta = time() - $cache{joinTime} - 5;
656 my $timestr = &Time2String($delta);
657 my $rate = sprintf("%.1f", $delta / @c);
658 delete $cache{joinTime};
660 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
663 # chanserv check: global channels, in case we missed one.
664 foreach ( &ChanConfList("chanServ_ops") ) {
669 # Usage: &getNickInChans($nick);
674 foreach (keys %channels) {
675 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
682 # Usage: &getNicksInChan($chan);
687 return keys %{ $channels{$chan}{''} };
691 my ($nick,$chan) = @_;
693 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
696 &DEBUG("INIC: chan == NULL.");
700 if (&validChan($chan) == 0) {
701 &ERROR("INIC: invalid channel $chan.");
705 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
708 foreach (keys %channels) {
709 next unless (/[A-Z]/);
710 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
716 sub IsNickInAnyChan {
720 foreach $chan (keys %channels) {
721 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
727 # Usage: &validChan($chan);
729 # todo: use $c instead?
732 if (!defined $chan or $chan =~ /^\s*$/) {
736 if (lc $chan ne $chan) {
737 &WARN("validChan: lc chan != chan. ($chan); fixing.");
738 $chan =~ tr/A-Z/a-z/;
741 # it's possible that this check creates the hash if empty.
742 if (defined $channels{$chan} or exists $channels{$chan}) {
743 if ($chan =~ /^_?default$/) {
744 # &WARN("validC: chan cannot be _default! returning 0!");
755 # Usage: &delUserInfo($nick,@chans);
757 my ($nick,@chans) = @_;
760 foreach $chan (@chans) {
761 foreach $mode (keys %{ $channels{$chan} }) {
763 next unless (exists $channels{$chan}{$mode}{$nick});
765 delete $channels{$chan}{$mode}{$nick};
773 delete $channels{$chan};
780 @joinchan = &getJoinChans(1);
781 $cache{joinTime} = time();
789 foreach (keys %chanconf) {
790 next if ($_ eq "_default");
792 my $val = $chanconf{$_}{autojoin};
796 $skip++ if ($val eq "0");
811 $str = "channels not auto-joining: @skip (joining: @chans)";
813 $str = "auto-joining all chans: @chans";
816 &status("Chans: ".$str) if ($show);
822 # &DEBUG("closeDCC called.");
825 foreach $type (keys %dcc) {
826 next if ($type ne uc($type));
829 foreach $nick (keys %{ $dcc{$type} }) {
830 next unless (defined $nick);
831 &status("DCC CHAT: closing DCC $type to $nick.");
832 next unless (defined $dcc{$type}{$nick});
834 my $ref = $dcc{$type}{$nick};
835 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
836 $dcc{$type}{$nick}->close();
837 delete $dcc{$type}{$nick};
838 &DEBUG("after close for $nick");
845 my($who,$chan,$userhost) = @_;
847 return unless (&IsChanConf("joinfloodCheck"));
849 if (exists $netsplit{lc $who}) { # netsplit join.
850 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
853 if (exists $floodjoin{$chan}{$who}{Time}) {
854 &WARN("floodjoin{$chan}{$who} already exists?");
857 $floodjoin{$chan}{$who}{Time} = time();
858 $floodjoin{$chan}{$who}{Host} = $userhost;
861 foreach (keys %floodjoin) {
863 my $count = scalar keys %{ $floodjoin{$c} };
864 next unless ($count > 5);
865 &DEBUG("joinflood: count => $count");
868 foreach (keys %{ $floodjoin{$c} }) {
869 my $t = $floodjoin{$c}{$_}{Time};
870 next unless (defined $t);
874 &DEBUG("joinflood: time => $time");
877 &DEBUG("joinflood: new time => $time");
883 foreach $chan (keys %floodjoin) {
884 foreach $who (keys %{ $floodjoin{$chan} }) {
885 my $t = $floodjoin{$chan}{$who}{Time};
886 next unless (defined $t);
888 my $delta = $time - $t;
889 next unless ($delta > 10);
891 delete $floodjoin{$chan}{$who};
896 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
902 if (exists $nuh{$n}) {
903 return &makeHostMask($nuh{$n});
905 $cache{on_who_Hack} = 1;