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 );
526 # forgot for about 2 years to implement channel keys when moving
527 # over to Net::IRC...
529 # hopefully validChan is right.
530 if ( &validChan($chan) ) {
531 &status("join: already on $chan?");
535 &status("joining $b_blue$chan $key$ob");
537 return if ( $conn->join( $chan, $key ) );
538 return if ( &validChan($chan) );
540 &DEBUG('joinchan: join failed. trying connect!');
551 next if ( $chan eq '' );
552 $chan =~ tr/A-Z/a-z/; # lowercase.
554 if ( $chan !~ /^$mask{chan}$/ ) {
555 &WARN("part: chan is invalid ($chan)");
559 &status("parting $chan");
560 if ( !&validChan($chan) ) {
561 &WARN("part: not on $chan; doing anyway");
568 # deletion of $channels{chan} is done in &entryEvt().
573 my ( $chan, @modes ) = @_;
574 my $modes = join( ' ', @modes );
576 if ( &validChan($chan) == 0 ) {
577 &ERROR("mode: invalid chan => '$chan'.");
581 &DEBUG("mode: MODE $chan $modes");
583 # should move to use Net::IRC's $conn->mode()... but too lazy.
584 rawout("MODE $chan $modes");
588 my ( $chan, @who ) = @_;
589 my $os = 'o' x scalar(@who);
591 &mode( $chan, "+$os @who" );
595 my ( $chan, @who ) = @_;
596 my $os = 'o' x scalar(@who);
598 &mode( $chan, "-$os " . @who );
602 my ( $nick, $chan, $msg ) = @_;
603 my (@chans) = ( $chan eq '' ) ? ( keys %channels ) : lc($chan);
604 my $mynick = $conn->nick();
606 if ( $chan ne '' and &validChan($chan) == 0 ) {
607 &ERROR("kick: invalid channel $chan.");
611 $nick =~ tr/A-Z/a-z/;
613 foreach $chan (@chans) {
614 if ( !&IsNickInChan( $nick, $chan ) ) {
615 &status("kick: $nick is not on $chan.") if ( scalar @chans == 1 );
619 if ( !exists $channels{$chan}{o}{$mynick} ) {
620 &status("kick: do not have ops on $chan :(");
624 &status("Kicking $nick from $chan.");
625 $conn->kick( $chan, $nick, $msg );
630 my ( $mask, $chan ) = @_;
631 my (@chans) = ( $chan =~ /^\*?$/ ) ? ( keys %channels ) : lc($chan);
632 my $mynick = $conn->nick();
635 if ( $chan !~ /^\*?$/ and &validChan($chan) == 0 ) {
636 &ERROR("ban: invalid channel $chan.");
640 foreach $chan (@chans) {
641 if ( !exists $channels{$chan}{o}{$mynick} ) {
642 &status("ban: do not have ops on $chan :(");
646 &status("Banning $mask from $chan.");
647 &rawout("MODE $chan +b $mask");
655 my ( $mask, $chan ) = @_;
656 my (@chans) = ( $chan =~ /^\*?$/ ) ? ( keys %channels ) : lc($chan);
657 my $mynick = $conn->nick();
660 &DEBUG("unban: mask = $mask, chan = @chans");
662 foreach $chan (@chans) {
663 if ( !exists $channels{$chan}{o}{$mynick} ) {
664 &status("unBan: do not have ops on $chan :(");
668 &status("Removed ban $mask from $chan.");
669 &rawout("MODE $chan -b $mask");
678 if ( defined $conn ) {
679 &status( 'QUIT ' . $conn->nick() . " has quit IRC ($quitmsg)" );
680 $conn->quit($quitmsg);
683 &WARN('quit: could not quit!');
689 my $mynick = $conn->nick();
691 if ( !defined $newnick ) {
692 &ERROR('nick: nick == NULL.');
696 if ( !defined $mynick ) {
697 &WARN('nick: mynick == NULL.');
702 $bad++ if ( exists $nuh{$newnick} );
703 $bad++ if ( &IsNickInAnyChan($newnick) );
706 &WARN( "Nick: not going to try to change from $mynick to $newnick. ["
710 # hrm... over time we lose track of our own nick.
714 if ( $newnick =~ /^$mask{nick}$/ ) {
715 &status("nick: Changing nick from $mynick to $newnick");
717 # ->nick() will NOT change cause we are using rawout?
718 &rawout("NICK $newnick");
721 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
726 my ( $who, $chan ) = @_;
728 # TODO: check if $who or $chan are invalid.
730 $conn->invite( $who, $chan );
734 # Channel related functions...
737 # Usage: &joinNextChan();
740 foreach ( sort keys %conns ) {
742 my $mynick = $conn->nick();
743 my @join = getJoinChans(1);
745 if ( scalar @join ) {
746 my $chan = shift @join;
749 if ( my $i = scalar @join ) {
750 &status("joinNextChan: $mynick $i chans to join.");
757 if ( exists $cache{joinTime} ) {
758 my $delta = time() - $cache{joinTime} - 5;
759 my $timestr = &Time2String($delta);
761 # FIXME: @join should be @in instead (hacked to 10)
762 #my $rate = sprintf('%.1f', $delta / @in);
763 my $rate = sprintf( '%.1f', $delta / 10 );
764 delete $cache{joinTime};
766 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
769 # chanserv check: global channels, in case we missed one.
770 foreach ( &ChanConfList('chanServ_ops') ) {
775 # Usage: &getNickInChans($nick);
780 foreach ( keys %channels ) {
781 next unless ( grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} } );
788 # Usage: &getNicksInChan($chan);
793 return keys %{ $channels{$chan}{''} };
797 my ( $nick, $chan ) = @_;
799 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
801 if ( $chan =~ /^$/ ) {
802 &DEBUG('INIC: chan == NULL.');
806 if ( &validChan($chan) == 0 ) {
807 &ERROR("INIC: invalid channel $chan.");
811 if ( grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} } ) {
815 foreach ( keys %channels ) {
816 next unless (/[A-Z]/);
817 &DEBUG('iNIC: hash channels contains mixed cased chan!!!');
823 sub IsNickInAnyChan {
827 foreach $chan ( keys %channels ) {
828 next unless ( grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} } );
834 # Usage: &validChan($chan);
837 # TODO: use $c instead?
840 if ( !defined $chan or $chan =~ /^\s*$/ ) {
844 if ( lc $chan ne $chan ) {
845 &WARN("validChan: lc chan != chan. ($chan); fixing.");
846 $chan =~ tr/A-Z/a-z/;
849 # it's possible that this check creates the hash if empty.
850 if ( defined $channels{$chan} or exists $channels{$chan} ) {
851 if ( $chan =~ /^_?default$/ ) {
853 # &WARN('validC: chan cannot be _default! returning 0!');
865 # Usage: &delUserInfo($nick,@chans);
867 my ( $nick, @chans ) = @_;
870 foreach $chan (@chans) {
871 foreach $mode ( keys %{ $channels{$chan} } ) {
874 next unless ( exists $channels{$chan}{$mode}{$nick} );
876 delete $channels{$chan}{$mode}{$nick};
884 delete $channels{$chan};
891 $cache{joinTime} = time();
896 # $show should contain the min number of seconds between display
897 # of the Chans: status line. Use 0 to disable
904 # Display 'Chans:' only if more than $show seconds since last display
905 if ( time() - $lastChansTime > $show ) {
906 $lastChansTime = time();
909 $show = 0; # Don't display since < 15min since last
912 # can't join any if not connected
913 return @join if ( !$conn );
915 my $nick = $conn->nick();
917 foreach ( keys %chanconf ) {
918 next if ( $_ eq '_default' );
921 my $val = $chanconf{$_}{autojoin};
923 if ( defined $val ) {
924 $skip++ if ( $val eq '0' );
927 # convert old +autojoin to autojoin <nick>
929 $chanconf{$_}{autojoin} = $val;
931 $skip++ if ( lc $val ne lc $nick );
941 if ( defined $channels{$_} or exists $channels{$_} ) {
951 $str .= ' in:' . join( ',', sort @in ) if scalar @in;
952 $str .= ' skip:' . join( ',', sort @skip ) if scalar @skip;
953 $str .= ' join:' . join( ',', sort @join ) if scalar @join;
955 &status("Chans: ($nick)$str") if ($show);
962 # &DEBUG('closeDCC called.');
965 foreach $type ( keys %dcc ) {
966 next if ( $type ne uc($type) );
969 foreach $nick ( keys %{ $dcc{$type} } ) {
970 next unless ( defined $nick );
971 &status("DCC CHAT: closing DCC $type to $nick.");
972 next unless ( defined $dcc{$type}{$nick} );
974 my $ref = $dcc{$type}{$nick};
975 &dccsay( $nick, "bye bye, $nick" ) if ( $type =~ /^chat$/i );
976 $dcc{$type}{$nick}->close();
977 delete $dcc{$type}{$nick};
978 &DEBUG("after close for $nick");
985 my ( $who, $chan, $userhost ) = @_;
987 return unless ( &IsChanConf('joinfloodCheck') > 0 );
989 if ( exists $netsplit{ lc $who } ) { # netsplit join.
990 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
993 if ( exists $floodjoin{$chan}{$who}{Time} ) {
994 &WARN("floodjoin{$chan}{$who} already exists?");
997 $floodjoin{$chan}{$who}{Time} = time();
998 $floodjoin{$chan}{$who}{Host} = $userhost;
1001 foreach ( keys %floodjoin ) {
1003 my $count = scalar keys %{ $floodjoin{$c} };
1004 next unless ( $count > 5 );
1005 &DEBUG("joinflood: count => $count");
1008 foreach ( keys %{ $floodjoin{$c} } ) {
1009 my $t = $floodjoin{$c}{$_}{Time};
1010 next unless ( defined $t );
1014 &DEBUG("joinflood: time => $time");
1017 &DEBUG("joinflood: new time => $time");
1023 foreach $chan ( keys %floodjoin ) {
1024 foreach $who ( keys %{ $floodjoin{$chan} } ) {
1025 my $t = $floodjoin{$chan}{$who}{Time};
1026 next unless ( defined $t );
1028 my $delta = $time - $t;
1029 next unless ( $delta > 10 );
1031 delete $floodjoin{$chan}{$who};
1036 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
1042 if ( exists $nuh{$n} ) {
1043 return &makeHostMask( $nuh{$n} );
1046 $cache{on_who_Hack} = 1;
1053 # vim:ts=4:sw=4:expandtab:tw=80