2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 no strict 'subs'; # IN/STDIN
13 use vars qw(%floodjoin %nuh %dcc %cache %conns %channels %param %mask
14 %chanconf %orig %ircPort %ircstats %last %netsplit);
15 use vars qw($irc $nickserv $conn $msgType $who $talkchannel
16 $addressed $postprocess);
17 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
18 $pubcount $pubtime $pubsize);
19 use vars qw($b_blue $ob);
20 use vars qw(@ircServers);
27 # It's probably closer to 510, but let's be cautious until we calculate it extensively.
30 # Keep track of last time we displayed Chans: to avoid spam in logs
31 my $lastChansTime = 0;
38 while ( my $host = shift @ircServers ) {
40 # JUST IN CASE. irq was complaining about this.
41 if ( $lastrun == time() ) {
42 &DEBUG('ircloop: hrm... lastrun == time()');
48 if ( !defined $host ) {
49 &DEBUG('ircloop: ircServers[x] = NULL.');
53 next unless ( exists $ircPort{$host} );
55 my $retval = &irc( $host, $ircPort{$host} );
56 next unless ( defined $retval and $retval == 0 );
59 if ( $error % 3 == 0 and $error != 0 ) {
60 &status('IRC: Could not connect.');
65 if ( $error >= 3 * 2 ) {
66 &status('IRC: cannot connect to any IRC servers; stopping.');
72 &status('IRC: ok, done one cycle of IRC servers; trying again.');
79 my ( $server, $port ) = @_;
83 # TODO: move all this to an sql table
84 my $iaddr = inet_aton($server);
85 my $paddr = sockaddr_in( $port, $iaddr );
86 my $proto = getprotobyname('tcp');
93 if ( $server =~ /\D$/ ) {
94 my $packed = scalar( gethostbyname($server) );
96 if ( !defined $packed ) {
97 &status(" cannot resolve $server.");
101 $resolve = inet_ntoa($packed);
102 ### warning in Sys/Hostname line 78???
103 ### caused inside Net::IRC?
107 Nick => $param{'ircNick'},
110 Ircname => $param{'ircName'},
112 $args{'LocalAddr'} = $param{'ircHost'} if ( $param{'ircHost'} );
113 $args{'Password'} = $param{'ircPasswd'} if ( $param{'ircPasswd'} );
115 foreach my $mynick ( split ',', $param{'ircNick'} ) {
117 "Connecting to port $port of server $server ($resolve) as $mynick ..."
119 $args{'Nick'} = $mynick;
120 $conns{$mynick} = $irc->newconn(%args);
121 if ( !defined $conns{$mynick} ) {
122 &ERROR('IRC: connection failed.');
124 "add \"set ircHost 0.0.0.0\" to your config. If that does not work"
127 'Please check /etc/hosts to see if you have a localhost line like:'
129 &ERROR('127.0.0.1 localhost localhost');
131 'If this is still a problem, please contact the maintainer.');
133 $conns{$mynick}->maxlinelen($maxlinelen);
136 $conns{$mynick}->add_global_handler( 'caction', \&on_action );
137 $conns{$mynick}->add_global_handler( 'cdcc', \&on_dcc );
138 $conns{$mynick}->add_global_handler( 'cping', \&on_ping );
139 $conns{$mynick}->add_global_handler( 'crping', \&on_ping_reply );
140 $conns{$mynick}->add_global_handler( 'cversion', \&on_version );
141 $conns{$mynick}->add_global_handler( 'crversion', \&on_crversion );
142 $conns{$mynick}->add_global_handler( 'dcc_open', \&on_dcc_open );
143 $conns{$mynick}->add_global_handler( 'dcc_close', \&on_dcc_close );
144 $conns{$mynick}->add_global_handler( 'chat', \&on_chat );
145 $conns{$mynick}->add_global_handler( 'msg', \&on_msg );
146 $conns{$mynick}->add_global_handler( 'public', \&on_public );
147 $conns{$mynick}->add_global_handler( 'join', \&on_join );
148 $conns{$mynick}->add_global_handler( 'part', \&on_part );
149 $conns{$mynick}->add_global_handler( 'topic', \&on_topic );
150 $conns{$mynick}->add_global_handler( 'invite', \&on_invite );
151 $conns{$mynick}->add_global_handler( 'kick', \&on_kick );
152 $conns{$mynick}->add_global_handler( 'mode', \&on_mode );
153 $conns{$mynick}->add_global_handler( 'nick', \&on_nick );
154 $conns{$mynick}->add_global_handler( 'quit', \&on_quit );
155 $conns{$mynick}->add_global_handler( 'notice', \&on_notice );
157 ->add_global_handler( 'whoischannels', \&on_whoischannels );
159 ->add_global_handler( 'useronchannel', \&on_useronchannel );
160 $conns{$mynick}->add_global_handler( 'whois', \&on_whois );
161 $conns{$mynick}->add_global_handler( 'other', \&on_other );
162 $conns{$mynick}->add_global_handler( 'disconnect', \&on_disconnect );
164 ->add_global_handler( [ 251, 252, 253, 254, 255 ], \&on_init );
166 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
167 $conns{$mynick}->add_global_handler( 303, \&on_ison ); # notify.
168 $conns{$mynick}->add_global_handler( 315, \&on_endofwho );
169 $conns{$mynick}->add_global_handler( 422, \&on_endofwho ); # nomotd.
170 $conns{$mynick}->add_global_handler( 324, \&on_modeis );
171 $conns{$mynick}->add_global_handler( 333, \&on_topicinfo );
172 $conns{$mynick}->add_global_handler( 352, \&on_who );
173 $conns{$mynick}->add_global_handler( 353, \&on_names );
174 $conns{$mynick}->add_global_handler( 366, \&on_endofnames );
175 $conns{$mynick}->add_global_handler( "001", \&on_connected )
177 $conns{$mynick}->add_global_handler( 433, \&on_nick_taken );
178 $conns{$mynick}->add_global_handler( 439, \&on_targettoofast );
180 # for proper joinnextChan behaviour
181 $conns{$mynick}->add_global_handler( 471, \&on_chanfull );
182 $conns{$mynick}->add_global_handler( 473, \&on_inviteonly );
183 $conns{$mynick}->add_global_handler( 474, \&on_banned );
184 $conns{$mynick}->add_global_handler( 475, \&on_badchankey );
185 $conns{$mynick}->add_global_handler( 443, \&on_useronchan );
187 # end of handler stuff.
192 # change internal timeout value for scheduler.
193 $irc->{_timeout} = 10; # how about 60?
194 # Net::IRC debugging.
197 $ircstats{'Server'} = "$server:$port";
199 # works? needs to actually do something
200 # should likely listen on a tcp port instead
201 #$irc->addfh(STDIN, \&on_stdin, 'r');
203 &status('starting main loop');
208 ######################################################################
209 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
210 ######################################################################
216 # slow down a bit if traffic is 'high'.
217 # need to take into account time of last message sent.
218 if ( $last{buflen} > 256 and length($buf) > 256 ) {
222 $conn->sl($buf) if ( &whatInterface() =~ /IRC/ );
224 $last{buflen} = length($buf);
229 my $mynick = $conn->nick();
230 if ( !defined $msg ) {
232 &WARN("say: msg == $msg.");
236 if ( &getChanConf( 'silent', $talkchannel )
237 and not( &IsFlag('s') and &verifyUser( $who, $nuh{ lc $who } ) ) )
239 &DEBUG("say: silent in $talkchannel, not saying $msg");
246 elsif ( $postprocess = &getChanConf( 'postprocess', $talkchannel ) ) {
247 &DEBUG("say: $postprocess $msg");
248 &parseCmdHook( $postprocess . ' ' . $msg );
253 &status("<$mynick/$talkchannel> $msg");
255 return unless ( &whatInterface() =~ /IRC/ );
257 $msg = 'zero' if ( $msg =~ /^0+$/ );
261 if ( $t == $pubtime ) {
263 $pubsize += length $msg;
265 my $i = &getChanConfDefault( 'sendPublicLimitLines', 3, $chan );
266 my $j = &getChanConfDefault( 'sendPublicLimitBytes', 1000, $chan );
268 if ( ( $pubcount % $i ) == 0 and $pubcount ) {
271 elsif ( $pubsize > $j ) {
280 $pubsize = length $msg;
283 $conn->privmsg( $talkchannel, $msg );
287 my ( $nick, $msg ) = @_;
288 if ( !defined $nick ) {
289 &ERROR('msg: nick == NULL.');
293 if ( !defined $msg ) {
295 &WARN("msg: msg == $msg.");
299 # some say() end up here (eg +help)
300 if ( &getChanConf( 'silent', $nick )
301 and not( &IsFlag('s') and &verifyUser( $who, $nuh{ lc $who } ) ) )
303 &DEBUG("msg: silent in $nick, not saying $msg");
307 &status(">$nick< $msg");
309 return unless ( &whatInterface() =~ /IRC/ );
312 if ( $t == $msgtime ) {
314 $msgsize += length $msg;
316 my $i = &getChanConfDefault( 'sendPrivateLimitLines', 3, $chan );
317 my $j = &getChanConfDefault( 'sendPrivateLimitBytes', 1000, $chan );
318 if ( ( $msgcount % $i ) == 0 and $msgcount ) {
321 elsif ( $msgsize > $j ) {
330 $msgsize = length $msg;
333 $conn->privmsg( $nick, $msg );
336 # Usage: &action(nick || chan, txt);
338 my $mynick = $conn->nick();
339 my ( $target, $txt ) = @_;
340 if ( !defined $txt ) {
341 &WARN('action: txt == NULL.');
345 if ( &getChanConf( 'silent', $target )
346 and not( &IsFlag('s') and &verifyUser( $who, $nuh{ lc $who } ) ) )
348 &DEBUG("action: silent in $target, not doing $txt");
352 if ( length $txt > 480 ) {
353 &status('action: txt too long; truncating.');
354 chop($txt) while ( length $txt > 480 );
357 &status("* $mynick/$target $txt");
358 $conn->me( $target, $txt );
361 # Usage: ¬ice(nick || chan, txt);
363 my ( $target, $txt ) = @_;
364 if ( !defined $txt ) {
365 &WARN('notice: txt == NULL.');
369 &status("-$target- $txt");
373 if ( $t == $nottime ) {
375 $notsize += length $txt;
377 my $i = &getChanConfDefault( 'sendNoticeLimitLines', 3, $chan );
378 my $j = &getChanConfDefault( 'sendNoticeLimitBytes', 1000, $chan );
380 if ( ( $notcount % $i ) == 0 and $notcount ) {
383 elsif ( $notsize > $j ) {
392 $notsize = length $txt;
395 $conn->notice( $target, $txt );
399 my ( $txt, $flag ) = @_;
401 ### FIXME: flag not supported yet.
403 foreach ( keys %{ $dcc{'CHAT'} } ) {
404 $conn->privmsg( $dcc{'CHAT'}{$_}, $txt );
409 ### perform commands.
412 # Usage: &performReply($reply);
416 if ( !defined $reply or $reply =~ /^\s*$/ ) {
417 &DEBUG('performReply: reply == NULL.');
421 $reply =~ /([\.\?\s]+)$/;
423 # FIXME need real throttling....
424 if ( length($reply) > $maxlinelen - 30 ) {
425 $reply = substr( $reply, 0, $maxlinelen - 33 );
426 $reply =~ s/ [^ ]*?$/ .../;
428 &checkMsgType($reply);
430 if ( $msgType eq 'public' ) {
431 if ( rand() < 0.5 or $reply =~ /[\.\?]$/ ) {
432 $reply = "$orig{who}: " . $reply;
435 $reply = "$reply, " . $orig{who};
440 elsif ( $msgType eq 'private' ) {
441 if ( rand() > 0.5 ) {
442 $reply = "$reply, " . $orig{who};
444 &msg( $who, $reply );
447 elsif ( $msgType eq 'chat' ) {
448 if ( !exists $dcc{'CHAT'}{$who} ) {
449 &VERB( "pSR: dcc{'CHAT'}{$who} does not exist.", 2 );
452 $conn->privmsg( $dcc{'CHAT'}{$who}, $reply );
456 &ERROR("PR: msgType invalid? ($msgType).");
461 sub performAddressedReply {
462 return unless ($addressed);
466 # Usage: &performStrictReply($reply);
467 sub performStrictReply {
470 # FIXME need real throttling....
471 if ( length($reply) > $maxlinelen - 30 ) {
472 $reply = substr( $reply, 0, $maxlinelen - 33 );
473 $reply =~ s/ [^ ]*?$/ .../;
475 &checkMsgType($reply);
477 if ( $msgType eq 'private' ) {
478 &msg( $who, $reply );
480 elsif ( $msgType eq 'public' ) {
483 elsif ( $msgType eq 'chat' ) {
484 &dccsay( lc $who, $reply );
487 &ERROR("pSR: msgType invalid? ($msgType).");
492 my ( $who, $reply ) = @_;
494 if ( !defined $reply or $reply =~ /^\s*$/ ) {
495 &WARN('dccsay: reply == NULL.');
499 if ( !exists $dcc{'CHAT'}{$who} ) {
500 &VERB( "pSR: dcc{'CHAT'}{$who} does not exist. (2)", 2 );
504 &status("=>$who<= $reply"); # dcc chat.
505 $conn->privmsg( $dcc{'CHAT'}{$who}, $reply );
512 foreach $type ( keys %dcc ) {
513 &FIXME("dcc_close: $who");
514 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
515 next unless ( scalar @who );
517 &DEBUG("dcc_close... close $who!");
522 my ( $chan, $key ) = @_;
523 $key ||= &getChanConf( 'chankey', $chan );
525 my $mynick = $conn->nick();
527 # forgot for about 2 years to implement channel keys when moving
528 # over to Net::IRC...
530 # hopefully validChan is right.
531 if ( &validChan($chan) ) {
532 &status("join: already on $chan?");
536 &status("$mynick joining $b_blue$chan $key$ob");
538 return if ( $conn->join( $chan, $key ) );
539 return if ( &validChan($chan) );
541 &DEBUG('joinchan: join failed. trying connect!');
552 next if ( $chan eq '' );
553 $chan =~ tr/A-Z/a-z/; # lowercase.
555 if ( $chan !~ /^$mask{chan}$/ ) {
556 &WARN("part: chan is invalid ($chan)");
560 &status("parting $chan");
561 if ( !&validChan($chan) ) {
562 &WARN("part: not on $chan; doing anyway");
569 # deletion of $channels{chan} is done in &entryEvt().
574 my ( $chan, @modes ) = @_;
575 my $modes = join( ' ', @modes );
577 if ( &validChan($chan) == 0 ) {
578 &ERROR("mode: invalid chan => '$chan'.");
582 &DEBUG("mode: MODE $chan $modes");
584 # should move to use Net::IRC's $conn->mode()... but too lazy.
585 rawout("MODE $chan $modes");
589 my ( $chan, @who ) = @_;
590 my $os = 'o' x scalar(@who);
592 &mode( $chan, "+$os @who" );
596 my ( $chan, @who ) = @_;
597 my $os = 'o' x scalar(@who);
599 &mode( $chan, "-$os " . @who );
603 my ( $nick, $chan, $msg ) = @_;
604 my (@chans) = ( $chan eq '' ) ? ( keys %channels ) : lc($chan);
605 my $mynick = $conn->nick();
607 if ( $chan ne '' and &validChan($chan) == 0 ) {
608 &ERROR("kick: invalid channel $chan.");
612 $nick =~ tr/A-Z/a-z/;
614 foreach $chan (@chans) {
615 if ( !&IsNickInChan( $nick, $chan ) ) {
616 &status("kick: $nick is not on $chan.") if ( scalar @chans == 1 );
620 if ( !exists $channels{$chan}{o}{$mynick} ) {
621 &status("kick: do not have ops on $chan :(");
625 &status("Kicking $nick from $chan.");
626 $conn->kick( $chan, $nick, $msg );
631 my ( $mask, $chan ) = @_;
632 my (@chans) = ( $chan =~ /^\*?$/ ) ? ( keys %channels ) : lc($chan);
633 my $mynick = $conn->nick();
636 if ( $chan !~ /^\*?$/ and &validChan($chan) == 0 ) {
637 &ERROR("ban: invalid channel $chan.");
641 foreach $chan (@chans) {
642 if ( !exists $channels{$chan}{o}{$mynick} ) {
643 &status("ban: do not have ops on $chan :(");
647 &status("Banning $mask from $chan.");
648 &rawout("MODE $chan +b $mask");
656 my ( $mask, $chan ) = @_;
657 my (@chans) = ( $chan =~ /^\*?$/ ) ? ( keys %channels ) : lc($chan);
658 my $mynick = $conn->nick();
661 &DEBUG("unban: mask = $mask, chan = @chans");
663 foreach $chan (@chans) {
664 if ( !exists $channels{$chan}{o}{$mynick} ) {
665 &status("unBan: do not have ops on $chan :(");
669 &status("Removed ban $mask from $chan.");
670 &rawout("MODE $chan -b $mask");
679 if ( defined $conn ) {
680 &status( 'QUIT ' . $conn->nick() . " has quit IRC ($quitmsg)" );
681 $conn->quit($quitmsg);
684 &WARN('quit: could not quit!');
690 my $mynick = $conn->nick();
692 if ( !defined $newnick ) {
693 &ERROR('nick: nick == NULL.');
697 if ( !defined $mynick ) {
698 &WARN('nick: mynick == NULL.');
703 $bad++ if ( exists $nuh{$newnick} );
704 $bad++ if ( &IsNickInAnyChan($newnick) );
707 &WARN( "Nick: not going to try to change from $mynick to $newnick. ["
711 # hrm... over time we lose track of our own nick.
715 if ( $newnick =~ /^$mask{nick}$/ ) {
716 &status("nick: Changing nick from $mynick to $newnick");
718 # ->nick() will NOT change cause we are using rawout?
719 &rawout("NICK $newnick");
722 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
727 my ( $who, $chan ) = @_;
729 # TODO: check if $who or $chan are invalid.
731 $conn->invite( $who, $chan );
735 # Channel related functions...
738 # Usage: &joinNextChan();
741 foreach ( sort keys %conns ) {
743 my $mynick = $conn->nick();
744 my @join = getJoinChans(1);
746 if ( scalar @join ) {
747 my $chan = shift @join;
750 if ( my $i = scalar @join ) {
751 &status("joinNextChan: $mynick $i chans to join.");
758 if ( exists $cache{joinTime} ) {
759 my $delta = time() - $cache{joinTime} - 5;
760 my $timestr = &Time2String($delta);
762 # FIXME: @join should be @in instead (hacked to 10)
763 #my $rate = sprintf('%.1f', $delta / @in);
764 my $rate = sprintf( '%.1f', $delta / 10 );
765 delete $cache{joinTime};
767 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
770 # chanserv check: global channels, in case we missed one.
771 foreach ( &ChanConfList('chanServ_ops') ) {
776 # Usage: &getNickInChans($nick);
781 foreach ( keys %channels ) {
782 next unless ( grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} } );
789 # Usage: &getNicksInChan($chan);
794 return keys %{ $channels{$chan}{''} };
798 my ( $nick, $chan ) = @_;
800 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
802 if ( $chan =~ /^$/ ) {
803 &DEBUG('INIC: chan == NULL.');
807 if ( &validChan($chan) == 0 ) {
808 &ERROR("INIC: invalid channel $chan.");
812 if ( grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} } ) {
816 foreach ( keys %channels ) {
817 next unless (/[A-Z]/);
818 &DEBUG('iNIC: hash channels contains mixed cased chan!!!');
824 sub IsNickInAnyChan {
828 foreach $chan ( keys %channels ) {
829 next unless ( grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} } );
835 # Usage: &validChan($chan);
838 # TODO: use $c instead?
841 if ( !defined $chan or $chan =~ /^\s*$/ ) {
845 if ( lc $chan ne $chan ) {
846 &WARN("validChan: lc chan != chan. ($chan); fixing.");
847 $chan =~ tr/A-Z/a-z/;
850 # it's possible that this check creates the hash if empty.
851 if ( defined $channels{$chan} or exists $channels{$chan} ) {
852 if ( $chan =~ /^_?default$/ ) {
854 # &WARN('validC: chan cannot be _default! returning 0!');
866 # Usage: &delUserInfo($nick,@chans);
868 my ( $nick, @chans ) = @_;
871 foreach $chan (@chans) {
872 foreach $mode ( keys %{ $channels{$chan} } ) {
875 next unless ( exists $channels{$chan}{$mode}{$nick} );
877 delete $channels{$chan}{$mode}{$nick};
885 delete $channels{$chan};
892 $cache{joinTime} = time();
897 # $show should contain the min number of seconds between display
898 # of the Chans: status line. Use 0 to disable
905 # Display 'Chans:' only if more than $show seconds since last display
906 if ( time() - $lastChansTime > $show ) {
907 $lastChansTime = time();
910 $show = 0; # Don't display since < 15min since last
913 # can't join any if not connected
914 return @join if ( !$conn );
916 my $nick = $conn->nick();
918 foreach ( keys %chanconf ) {
919 next if ( $_ eq '_default' );
922 my $val = $chanconf{$_}{autojoin};
924 if ( defined $val ) {
925 $skip++ if ( $val eq '0' );
928 # convert old +autojoin to autojoin <nick>
930 $chanconf{$_}{autojoin} = $val;
932 $skip++ if ( lc $val ne lc $nick );
942 if ( defined $channels{$_} or exists $channels{$_} ) {
952 $str .= ' in:' . join( ',', sort @in ) if scalar @in;
953 $str .= ' skip:' . join( ',', sort @skip ) if scalar @skip;
954 $str .= ' join:' . join( ',', sort @join ) if scalar @join;
956 &status("Chans: ($nick)$str") if ($show);
963 # &DEBUG('closeDCC called.');
966 foreach $type ( keys %dcc ) {
967 next if ( $type ne uc($type) );
970 foreach $nick ( keys %{ $dcc{$type} } ) {
971 next unless ( defined $nick );
972 &status("DCC CHAT: closing DCC $type to $nick.");
973 next unless ( defined $dcc{$type}{$nick} );
975 my $ref = $dcc{$type}{$nick};
976 &dccsay( $nick, "bye bye, $nick" ) if ( $type =~ /^chat$/i );
977 $dcc{$type}{$nick}->close();
978 delete $dcc{$type}{$nick};
979 &DEBUG("after close for $nick");
986 my ( $who, $chan, $userhost ) = @_;
988 return unless ( &IsChanConf('joinfloodCheck') > 0 );
990 if ( exists $netsplit{ lc $who } ) { # netsplit join.
991 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
994 if ( exists $floodjoin{$chan}{$who}{Time} ) {
995 &WARN("floodjoin{$chan}{$who} already exists?");
998 $floodjoin{$chan}{$who}{Time} = time();
999 $floodjoin{$chan}{$who}{Host} = $userhost;
1002 foreach ( keys %floodjoin ) {
1004 my $count = scalar keys %{ $floodjoin{$c} };
1005 next unless ( $count > 5 );
1006 &DEBUG("joinflood: count => $count");
1009 foreach ( keys %{ $floodjoin{$c} } ) {
1010 my $t = $floodjoin{$c}{$_}{Time};
1011 next unless ( defined $t );
1015 &DEBUG("joinflood: time => $time");
1018 &DEBUG("joinflood: new time => $time");
1024 foreach $chan ( keys %floodjoin ) {
1025 foreach $who ( keys %{ $floodjoin{$chan} } ) {
1026 my $t = $floodjoin{$chan}{$who}{Time};
1027 next unless ( defined $t );
1029 my $delta = $time - $t;
1030 next unless ( $delta > 10 );
1032 delete $floodjoin{$chan}{$who};
1037 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
1043 if ( exists $nuh{$n} ) {
1044 return &makeHostMask( $nuh{$n} );
1047 $cache{on_who_Hack} = 1;
1054 # vim:ts=4:sw=4:expandtab:tw=80