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);
26 # It's probably closer to 510, but let's be cautious until we calculate it extensively.
29 # Keep track of last time we displayed Chans: to avoid spam in logs
30 my $lastChansTime = 0;
37 while (my $host = shift @ircServers) {
38 # JUST IN CASE. irq was complaining about this.
39 if ($lastrun == time()) {
40 &DEBUG("ircloop: hrm... lastrun == time()");
47 &DEBUG("ircloop: ircServers[x] = NULL.");
51 next unless (exists $ircPort{$host});
53 my $retval = &irc($host, $ircPort{$host});
54 next unless (defined $retval and $retval == 0);
57 if ($error % 3 == 0 and $error != 0) {
58 &status("IRC: Could not connect.");
64 &status("IRC: cannot connect to any IRC servers; stopping.");
70 &status("IRC: ok, done one cycle of IRC servers; trying again.");
77 my ($server,$port) = @_;
81 # TODO: move all this to an sql table
82 my $iaddr = inet_aton($server);
83 my $paddr = sockaddr_in($port, $iaddr);
84 my $proto = getprotobyname('tcp');
91 if ($server =~ /\D$/) {
92 my $packed = scalar(gethostbyname($server));
94 if (!defined $packed) {
95 &status(" cannot resolve $server.");
99 $resolve = inet_ntoa($packed);
100 ### warning in Sys/Hostname line 78???
101 ### caused inside Net::IRC?
105 Nick => $param{'ircNick'},
108 Ircname => $param{'ircName'},
110 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
111 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
113 foreach my $mynick (split ',', $param{'ircNick'}) {
114 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
115 $args{'Nick'} = $mynick;
116 $conns{$mynick} = $irc->newconn(%args);
117 if (!defined $conns{$mynick}) {
118 &ERROR("IRC: connection failed.");
119 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
120 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
121 &ERROR("127.0.0.1 localhost localhost");
122 &ERROR("If this is still a problem, please contact the maintainer.");
124 $conns{$mynick}->maxlinelen($maxlinelen);
126 $conns{$mynick}->add_global_handler('caction', \&on_action);
127 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
128 $conns{$mynick}->add_global_handler('cping', \&on_ping);
129 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
130 $conns{$mynick}->add_global_handler('cversion', \&on_version);
131 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
132 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
133 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
134 $conns{$mynick}->add_global_handler('chat', \&on_chat);
135 $conns{$mynick}->add_global_handler('msg', \&on_msg);
136 $conns{$mynick}->add_global_handler('public', \&on_public);
137 $conns{$mynick}->add_global_handler('join', \&on_join);
138 $conns{$mynick}->add_global_handler('part', \&on_part);
139 $conns{$mynick}->add_global_handler('topic', \&on_topic);
140 $conns{$mynick}->add_global_handler('invite', \&on_invite);
141 $conns{$mynick}->add_global_handler('kick', \&on_kick);
142 $conns{$mynick}->add_global_handler('mode', \&on_mode);
143 $conns{$mynick}->add_global_handler('nick', \&on_nick);
144 $conns{$mynick}->add_global_handler('quit', \&on_quit);
145 $conns{$mynick}->add_global_handler('notice', \&on_notice);
146 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
147 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
148 $conns{$mynick}->add_global_handler('whois', \&on_whois);
149 $conns{$mynick}->add_global_handler('other', \&on_other);
150 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
151 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
152 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
153 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
154 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
155 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
156 $conns{$mynick}->add_global_handler(324, \&on_modeis);
157 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
158 $conns{$mynick}->add_global_handler(352, \&on_who);
159 $conns{$mynick}->add_global_handler(353, \&on_names);
160 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
161 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
162 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
163 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
164 # for proper joinnextChan behaviour
165 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
166 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
167 $conns{$mynick}->add_global_handler(474, \&on_banned);
168 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
169 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
170 # end of handler stuff.
175 # change internal timeout value for scheduler.
176 $irc->{_timeout} = 10; # how about 60?
177 # Net::IRC debugging.
180 $ircstats{'Server'} = "$server:$port";
182 # works? needs to actually do something
183 # should likely listen on a tcp port instead
184 #$irc->addfh(STDIN, \&on_stdin, 'r');
186 &status("starting main loop");
191 ######################################################################
192 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
193 ######################################################################
199 # slow down a bit if traffic is 'high'.
200 # need to take into account time of last message sent.
201 if ($last{buflen} > 256 and length($buf) > 256) {
205 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
207 $last{buflen} = length($buf);
212 my $mynick = $conn->nick();
215 &WARN("say: msg == $msg.");
219 if (&getChanConf('silent', $talkchannel) and not
220 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
221 &DEBUG("say: silent in $talkchannel, not saying $msg");
225 if ( $postprocess ) {
227 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
228 &DEBUG("say: $postprocess $msg");
229 &parseCmdHook($postprocess . ' ' . $msg);
234 &status("<$mynick/$talkchannel> $msg");
236 return unless (&whatInterface() =~ /IRC/);
238 $msg = 'zero' if ($msg =~ /^0+$/);
242 if ($t == $pubtime) {
244 $pubsize += length $msg;
246 my $i = &getChanConfDefault('sendPublicLimitLines', 3, $chan);
247 my $j = &getChanConfDefault('sendPublicLimitBytes', 1000, $chan);
249 if ( ($pubcount % $i) == 0 and $pubcount) {
251 } elsif ($pubsize > $j) {
259 $pubsize = length $msg;
262 $conn->privmsg($talkchannel, $msg);
266 my ($nick, $msg) = @_;
267 if (!defined $nick) {
268 &ERROR("msg: nick == NULL.");
274 &WARN("msg: msg == $msg.");
278 # some say() end up here (eg +help)
279 if (&getChanConf('silent', $nick) and not
280 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
281 &DEBUG("msg: silent in $nick, not saying $msg");
285 &status(">$nick< $msg");
287 return unless (&whatInterface() =~ /IRC/);
290 if ($t == $msgtime) {
292 $msgsize += length $msg;
294 my $i = &getChanConfDefault('sendPrivateLimitLines', 3, $chan);
295 my $j = &getChanConfDefault('sendPrivateLimitBytes', 1000, $chan);
296 if ( ($msgcount % $i) == 0 and $msgcount) {
298 } elsif ($msgsize > $j) {
306 $msgsize = length $msg;
309 $conn->privmsg($nick, $msg);
312 # Usage: &action(nick || chan, txt);
314 my $mynick = $conn->nick();
315 my ($target, $txt) = @_;
317 &WARN("action: txt == NULL.");
321 if (&getChanConf('silent', $target) and not
322 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
323 &DEBUG("action: silent in $target, not doing $txt");
327 if (length $txt > 480) {
328 &status("action: txt too long; truncating.");
329 chop($txt) while (length $txt > 480);
332 &status("* $mynick/$target $txt");
333 $conn->me($target, $txt);
336 # Usage: ¬ice(nick || chan, txt);
338 my ($target, $txt) = @_;
340 &WARN("notice: txt == NULL.");
344 &status("-$target- $txt");
348 if ($t == $nottime) {
350 $notsize += length $txt;
352 my $i = &getChanConfDefault('sendNoticeLimitLines', 3, $chan);
353 my $j = &getChanConfDefault('sendNoticeLimitBytes', 1000, $chan);
355 if ( ($notcount % $i) == 0 and $notcount) {
357 } elsif ($notsize > $j) {
365 $notsize = length $txt;
368 $conn->notice($target, $txt);
372 my ($txt,$flag) = @_;
374 ### FIXME: flag not supported yet.
376 foreach (keys %{ $dcc{'CHAT'} }) {
377 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
382 ### perform commands.
385 # Usage: &performReply($reply);
389 if (!defined $reply or $reply =~ /^\s*$/) {
390 &DEBUG("performReply: reply == NULL.");
394 $reply =~ /([\.\?\s]+)$/;
396 # FIXME need real throttling....
397 if (length($reply) > $maxlinelen - 30) {
398 $reply = substr($reply, 0, $maxlinelen - 33);
399 $reply =~ s/ [^ ]*?$/ .../;
401 &checkMsgType($reply);
403 if ($msgType eq 'public') {
404 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
405 $reply = "$orig{who}: ".$reply;
407 $reply = "$reply, ".$orig{who};
411 } elsif ($msgType eq 'private') {
413 $reply = "$reply, ".$orig{who};
417 } elsif ($msgType eq 'chat') {
418 if (!exists $dcc{'CHAT'}{$who}) {
419 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
422 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
425 &ERROR("PR: msgType invalid? ($msgType).");
430 sub performAddressedReply {
431 return unless ($addressed);
435 # Usage: &performStrictReply($reply);
436 sub performStrictReply {
439 # FIXME need real throttling....
440 if (length($reply) > $maxlinelen - 30) {
441 $reply = substr($reply, 0, $maxlinelen - 33);
442 $reply =~ s/ [^ ]*?$/ .../;
444 &checkMsgType($reply);
446 if ($msgType eq 'private') {
448 } elsif ($msgType eq 'public') {
450 } elsif ($msgType eq 'chat') {
451 &dccsay(lc $who, $reply);
453 &ERROR("pSR: msgType invalid? ($msgType).");
458 my($who, $reply) = @_;
460 if (!defined $reply or $reply =~ /^\s*$/) {
461 &WARN("dccsay: reply == NULL.");
465 if (!exists $dcc{'CHAT'}{$who}) {
466 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
470 &status("=>$who<= $reply"); # dcc chat.
471 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
478 foreach $type (keys %dcc) {
479 &FIXME("dcc_close: $who");
480 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
481 next unless (scalar @who);
483 &DEBUG("dcc_close... close $who!");
488 my ($chan, $key) = @_;
489 $key ||= &getChanConf('chankey', $chan);
492 # forgot for about 2 years to implement channel keys when moving
493 # over to Net::IRC...
495 # hopefully validChan is right.
496 if (&validChan($chan)) {
497 &status("join: already on $chan?");
500 &status("joining $b_blue$chan $key$ob");
502 return if ($conn->join($chan, $key));
503 return if (&validChan($chan));
505 &DEBUG("joinchan: join failed. trying connect!");
515 next if ($chan eq '');
516 $chan =~ tr/A-Z/a-z/; # lowercase.
518 if ($chan !~ /^$mask{chan}$/) {
519 &WARN("part: chan is invalid ($chan)");
523 &status("parting $chan");
524 if (!&validChan($chan)) {
525 &WARN("part: not on $chan; doing anyway");
530 # deletion of $channels{chan} is done in &entryEvt().
535 my ($chan, @modes) = @_;
536 my $modes = join(" ", @modes);
538 if (&validChan($chan) == 0) {
539 &ERROR("mode: invalid chan => '$chan'.");
543 &DEBUG("mode: MODE $chan $modes");
545 # should move to use Net::IRC's $conn->mode()... but too lazy.
546 rawout("MODE $chan $modes");
550 my ($chan, @who) = @_;
551 my $os = 'o' x scalar(@who);
553 &mode($chan, "+$os @who");
557 my ($chan, @who) = @_;
558 my $os = 'o' x scalar(@who);
560 &mode($chan, "-$os ".@who);
564 my ($nick,$chan,$msg) = @_;
565 my (@chans) = ($chan eq '') ? (keys %channels) : lc($chan);
566 my $mynick = $conn->nick();
568 if ($chan ne '' and &validChan($chan) == 0) {
569 &ERROR("kick: invalid channel $chan.");
573 $nick =~ tr/A-Z/a-z/;
575 foreach $chan (@chans) {
576 if (!&IsNickInChan($nick,$chan)) {
577 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
581 if (!exists $channels{$chan}{o}{$mynick}) {
582 &status("kick: do not have ops on $chan :(");
586 &status("Kicking $nick from $chan.");
587 $conn->kick($chan, $nick, $msg);
592 my ($mask,$chan) = @_;
593 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
594 my $mynick = $conn->nick();
597 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
598 &ERROR("ban: invalid channel $chan.");
602 foreach $chan (@chans) {
603 if (!exists $channels{$chan}{o}{$mynick}) {
604 &status("ban: do not have ops on $chan :(");
608 &status("Banning $mask from $chan.");
609 &rawout("MODE $chan +b $mask");
617 my ($mask,$chan) = @_;
618 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
619 my $mynick = $conn->nick();
622 &DEBUG("unban: mask = $mask, chan = @chans");
624 foreach $chan (@chans) {
625 if (!exists $channels{$chan}{o}{$mynick}) {
626 &status("unBan: do not have ops on $chan :(");
630 &status("Removed ban $mask from $chan.");
631 &rawout("MODE $chan -b $mask");
641 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
642 $conn->quit($quitmsg);
644 &WARN("quit: could not quit!");
650 my $mynick = $conn->nick();
652 if (!defined $newnick) {
653 &ERROR("nick: nick == NULL.");
657 if (!defined $mynick) {
658 &WARN("nick: mynick == NULL.");
663 $bad++ if (exists $nuh{$newnick});
664 $bad++ if (&IsNickInAnyChan($newnick));
667 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
668 # hrm... over time we lose track of our own nick.
672 if ($newnick =~ /^$mask{nick}$/) {
673 &status("nick: Changing nick from $mynick to $newnick");
674 # ->nick() will NOT change cause we are using rawout?
675 &rawout("NICK $newnick");
678 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
683 my($who, $chan) = @_;
684 # TODO: check if $who or $chan are invalid.
686 $conn->invite($who, $chan);
690 # Channel related functions...
693 # Usage: &joinNextChan();
696 foreach (sort keys %conns) {
698 my $mynick = $conn->nick();
699 my @join = getJoinChans(1);
702 my $chan = shift @join;
705 if (my $i = scalar @join) {
706 &status("joinNextChan: $mynick $i chans to join.");
713 if (exists $cache{joinTime}) {
714 my $delta = time() - $cache{joinTime} - 5;
715 my $timestr = &Time2String($delta);
716 # FIXME: @join should be @in instead (hacked to 10)
717 #my $rate = sprintf("%.1f", $delta / @in);
718 my $rate = sprintf("%.1f", $delta / 10);
719 delete $cache{joinTime};
721 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
724 # chanserv check: global channels, in case we missed one.
725 foreach ( &ChanConfList('chanServ_ops') ) {
730 # Usage: &getNickInChans($nick);
735 foreach (keys %channels) {
736 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
743 # Usage: &getNicksInChan($chan);
748 return keys %{ $channels{$chan}{''} };
752 my ($nick,$chan) = @_;
754 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
757 &DEBUG("INIC: chan == NULL.");
761 if (&validChan($chan) == 0) {
762 &ERROR("INIC: invalid channel $chan.");
766 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
769 foreach (keys %channels) {
770 next unless (/[A-Z]/);
771 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
777 sub IsNickInAnyChan {
781 foreach $chan (keys %channels) {
782 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
788 # Usage: &validChan($chan);
790 # TODO: use $c instead?
793 if (!defined $chan or $chan =~ /^\s*$/) {
797 if (lc $chan ne $chan) {
798 &WARN("validChan: lc chan != chan. ($chan); fixing.");
799 $chan =~ tr/A-Z/a-z/;
802 # it's possible that this check creates the hash if empty.
803 if (defined $channels{$chan} or exists $channels{$chan}) {
804 if ($chan =~ /^_?default$/) {
805 # &WARN("validC: chan cannot be _default! returning 0!");
816 # Usage: &delUserInfo($nick,@chans);
818 my ($nick,@chans) = @_;
821 foreach $chan (@chans) {
822 foreach $mode (keys %{ $channels{$chan} }) {
824 next unless (exists $channels{$chan}{$mode}{$nick});
826 delete $channels{$chan}{$mode}{$nick};
834 delete $channels{$chan};
841 $cache{joinTime} = time();
845 # $show should contain the min number of seconds between display
846 # of the Chans: status line. Use 0 to disable
853 # Display "Chans:" only if more than $show seconds since last display
854 if (time() - $lastChansTime > $show) {
855 $lastChansTime = time();
857 $show = 0; # Don't display since < 15min since last
860 # can't join any if not connected
861 return @join if (!$conn);
863 my $nick = $conn->nick();
865 foreach (keys %chanconf) {
866 next if ($_ eq '_default');
869 my $val = $chanconf{$_}{autojoin};
872 $skip++ if ($val eq '0');
874 # convert old +autojoin to autojoin <nick>
876 $chanconf{$_}{autojoin} = $val;
878 $skip++ if (lc $val ne lc $nick);
886 if (defined $channels{$_} or exists $channels{$_}) {
895 $str .= ' in:' . join(',', sort @in) if scalar @in;
896 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
897 $str .= ' join:' . join(',', sort @join) if scalar @join;
899 &status("Chans: ($nick)$str") if ($show);
905 # &DEBUG("closeDCC called.");
908 foreach $type (keys %dcc) {
909 next if ($type ne uc($type));
912 foreach $nick (keys %{ $dcc{$type} }) {
913 next unless (defined $nick);
914 &status("DCC CHAT: closing DCC $type to $nick.");
915 next unless (defined $dcc{$type}{$nick});
917 my $ref = $dcc{$type}{$nick};
918 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
919 $dcc{$type}{$nick}->close();
920 delete $dcc{$type}{$nick};
921 &DEBUG("after close for $nick");
928 my($who,$chan,$userhost) = @_;
930 return unless (&IsChanConf('joinfloodCheck') > 0);
932 if (exists $netsplit{lc $who}) { # netsplit join.
933 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
936 if (exists $floodjoin{$chan}{$who}{Time}) {
937 &WARN("floodjoin{$chan}{$who} already exists?");
940 $floodjoin{$chan}{$who}{Time} = time();
941 $floodjoin{$chan}{$who}{Host} = $userhost;
944 foreach (keys %floodjoin) {
946 my $count = scalar keys %{ $floodjoin{$c} };
947 next unless ($count > 5);
948 &DEBUG("joinflood: count => $count");
951 foreach (keys %{ $floodjoin{$c} }) {
952 my $t = $floodjoin{$c}{$_}{Time};
953 next unless (defined $t);
957 &DEBUG("joinflood: time => $time");
960 &DEBUG("joinflood: new time => $time");
966 foreach $chan (keys %floodjoin) {
967 foreach $who (keys %{ $floodjoin{$chan} }) {
968 my $t = $floodjoin{$chan}{$who}{Time};
969 next unless (defined $t);
971 my $delta = $time - $t;
972 next unless ($delta > 10);
974 delete $floodjoin{$chan}{$who};
979 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
985 if (exists $nuh{$n}) {
986 return &makeHostMask($nuh{$n});
988 $cache{on_who_Hack} = 1;