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);
33 while (my $host = shift @ircServers) {
34 # JUST IN CASE. irq was complaining about this.
35 if ($lastrun == time()) {
36 &DEBUG("ircloop: hrm... lastrun == time()");
43 &DEBUG("ircloop: ircServers[x] = NULL.");
47 next unless (exists $ircPort{$host});
49 my $retval = &irc($host, $ircPort{$host});
50 next unless (defined $retval and $retval == 0);
53 if ($error % 3 == 0 and $error != 0) {
54 &status("IRC: Could not connect.");
60 &status("IRC: cannot connect to any IRC servers; stopping.");
66 &status("IRC: ok, done one cycle of IRC servers; trying again.");
73 my ($server,$port) = @_;
77 # TODO: move all this to an sql table
78 my $iaddr = inet_aton($server);
79 my $paddr = sockaddr_in($port, $iaddr);
80 my $proto = getprotobyname('tcp');
87 if ($server =~ /\D$/) {
88 my $packed = scalar(gethostbyname($server));
90 if (!defined $packed) {
91 &status(" cannot resolve $server.");
95 $resolve = inet_ntoa($packed);
96 ### warning in Sys/Hostname line 78???
97 ### caused inside Net::IRC?
101 Nick => $param{'ircNick'},
104 Ircname => $param{'ircName'},
106 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
107 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
109 foreach my $mynick (split ',', $param{'ircNick'}) {
110 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
111 $args{'Nick'} = $mynick;
112 $conns{$mynick} = $irc->newconn(%args);
113 if (!defined $conns{$mynick}) {
114 &ERROR("IRC: connection failed.");
115 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
116 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
117 &ERROR("127.0.0.1 localhost localhost");
118 &ERROR("If this is still a problem, please contact the maintainer.");
120 $conns{$mynick}->maxlinelen($maxlinelen);
122 $conns{$mynick}->add_global_handler('caction', \&on_action);
123 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
124 $conns{$mynick}->add_global_handler('cping', \&on_ping);
125 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
126 $conns{$mynick}->add_global_handler('cversion', \&on_version);
127 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
128 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
129 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
130 $conns{$mynick}->add_global_handler('chat', \&on_chat);
131 $conns{$mynick}->add_global_handler('msg', \&on_msg);
132 $conns{$mynick}->add_global_handler('public', \&on_public);
133 $conns{$mynick}->add_global_handler('join', \&on_join);
134 $conns{$mynick}->add_global_handler('part', \&on_part);
135 $conns{$mynick}->add_global_handler('topic', \&on_topic);
136 $conns{$mynick}->add_global_handler('invite', \&on_invite);
137 $conns{$mynick}->add_global_handler('kick', \&on_kick);
138 $conns{$mynick}->add_global_handler('mode', \&on_mode);
139 $conns{$mynick}->add_global_handler('nick', \&on_nick);
140 $conns{$mynick}->add_global_handler('quit', \&on_quit);
141 $conns{$mynick}->add_global_handler('notice', \&on_notice);
142 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
143 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
144 $conns{$mynick}->add_global_handler('whois', \&on_whois);
145 $conns{$mynick}->add_global_handler('other', \&on_other);
146 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
147 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
148 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
149 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
150 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
151 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
152 $conns{$mynick}->add_global_handler(324, \&on_modeis);
153 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
154 $conns{$mynick}->add_global_handler(352, \&on_who);
155 $conns{$mynick}->add_global_handler(353, \&on_names);
156 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
157 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
158 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
159 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
160 # for proper joinnextChan behaviour
161 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
162 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
163 $conns{$mynick}->add_global_handler(474, \&on_banned);
164 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
165 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
166 # end of handler stuff.
171 # change internal timeout value for scheduler.
172 $irc->{_timeout} = 10; # how about 60?
173 # Net::IRC debugging.
176 $ircstats{'Server'} = "$server:$port";
178 # works? needs to actually do something
179 # should likely listen on a tcp port instead
180 #$irc->addfh(STDIN, \&on_stdin, "r");
182 &status("starting main loop");
187 ######################################################################
188 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
189 ######################################################################
195 # slow down a bit if traffic is "high".
196 # need to take into account time of last message sent.
197 if ($last{buflen} > 256 and length($buf) > 256) {
201 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
203 $last{buflen} = length($buf);
208 my $mynick = $conn->nick();
211 &WARN("say: msg == $msg.");
215 if (&getChanConf('silent', $talkchannel)) {
216 &DEBUG("say: silent in $talkchannel, not saying $msg");
220 if ( $postprocess ) {
222 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
223 &DEBUG("say: $postprocess $msg");
224 &parseCmdHook($postprocess . ' ' . $msg);
229 &status("<$mynick/$talkchannel> $msg");
231 return unless (&whatInterface() =~ /IRC/);
233 $msg = "zero" if ($msg =~ /^0+$/);
237 if ($t == $pubtime) {
239 $pubsize += length $msg;
241 my $i = &getChanConfDefault("sendPublicLimitLines", 3, $chan);
242 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000, $chan);
244 if ( ($pubcount % $i) == 0 and $pubcount) {
246 } elsif ($pubsize > $j) {
254 $pubsize = length $msg;
257 $conn->privmsg($talkchannel, $msg);
261 my ($nick, $msg) = @_;
262 if (!defined $nick) {
263 &ERROR("msg: nick == NULL.");
269 &WARN("msg: msg == $msg.");
273 # some say() end up here (eg +help)
274 if (&getChanConf('silent', $nick)) {
275 &DEBUG("say: silent in $nick, not saying $msg");
279 &status(">$nick< $msg");
281 return unless (&whatInterface() =~ /IRC/);
284 if ($t == $msgtime) {
286 $msgsize += length $msg;
288 my $i = &getChanConfDefault("sendPrivateLimitLines", 3, $chan);
289 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000, $chan);
290 if ( ($msgcount % $i) == 0 and $msgcount) {
292 } elsif ($msgsize > $j) {
300 $msgsize = length $msg;
303 $conn->privmsg($nick, $msg);
306 # Usage: &action(nick || chan, txt);
308 my $mynick = $conn->nick();
309 my ($target, $txt) = @_;
311 &WARN("action: txt == NULL.");
315 if (length $txt > 480) {
316 &status("action: txt too long; truncating.");
317 chop($txt) while (length $txt > 480);
320 &status("* $mynick/$target $txt");
321 $conn->me($target, $txt);
324 # Usage: ¬ice(nick || chan, txt);
326 my ($target, $txt) = @_;
328 &WARN("notice: txt == NULL.");
332 &status("-$target- $txt");
336 if ($t == $nottime) {
338 $notsize += length $txt;
340 my $i = &getChanConfDefault("sendNoticeLimitLines", 3, $chan);
341 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000, $chan);
343 if ( ($notcount % $i) == 0 and $notcount) {
345 } elsif ($notsize > $j) {
353 $notsize = length $txt;
356 $conn->notice($target, $txt);
360 my ($txt,$flag) = @_;
362 ### FIXME: flag not supported yet.
364 foreach (keys %{ $dcc{'CHAT'} }) {
365 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
370 ### perform commands.
373 # Usage: &performReply($reply);
377 if (!defined $reply or $reply =~ /^\s*$/) {
378 &DEBUG("performReply: reply == NULL.");
382 $reply =~ /([\.\?\s]+)$/;
384 # FIXME need real throttling....
385 if (length($reply) > $maxlinelen - 30) {
386 $reply = substr($reply, 0, $maxlinelen - 33);
387 $reply =~ s/ [^ ]*?$/ .../;
389 &checkMsgType($reply);
391 if ($msgType eq 'public') {
392 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
393 $reply = "$orig{who}: ".$reply;
395 $reply = "$reply, ".$orig{who};
399 } elsif ($msgType eq 'private') {
401 $reply = "$reply, ".$orig{who};
405 } elsif ($msgType eq 'chat') {
406 if (!exists $dcc{'CHAT'}{$who}) {
407 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
410 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
413 &ERROR("PR: msgType invalid? ($msgType).");
418 sub performAddressedReply {
419 return unless ($addressed);
423 # Usage: &performStrictReply($reply);
424 sub performStrictReply {
427 # FIXME need real throttling....
428 if (length($reply) > $maxlinelen - 30) {
429 $reply = substr($reply, 0, $maxlinelen - 33);
430 $reply =~ s/ [^ ]*?$/ .../;
432 &checkMsgType($reply);
434 if ($msgType eq 'private') {
436 } elsif ($msgType eq 'public') {
438 } elsif ($msgType eq 'chat') {
439 &dccsay(lc $who, $reply);
441 &ERROR("pSR: msgType invalid? ($msgType).");
446 my($who, $reply) = @_;
448 if (!defined $reply or $reply =~ /^\s*$/) {
449 &WARN("dccsay: reply == NULL.");
453 if (!exists $dcc{'CHAT'}{$who}) {
454 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
458 &status("=>$who<= $reply"); # dcc chat.
459 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
466 foreach $type (keys %dcc) {
467 &FIXME("dcc_close: $who");
468 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
469 next unless (scalar @who);
471 &DEBUG("dcc_close... close $who!");
476 my ($chan, $key) = @_;
477 $key ||= &getChanConf("chankey", $chan);
480 # forgot for about 2 years to implement channel keys when moving
481 # over to Net::IRC...
483 # hopefully validChan is right.
484 if (&validChan($chan)) {
485 &status("join: already on $chan?");
488 &status("joining $b_blue$chan $key$ob");
490 return if ($conn->join($chan, $key));
491 return if (&validChan($chan));
493 &DEBUG("joinchan: join failed. trying connect!");
503 next if ($chan eq "");
504 $chan =~ tr/A-Z/a-z/; # lowercase.
506 if ($chan !~ /^$mask{chan}$/) {
507 &WARN("part: chan is invalid ($chan)");
511 &status("parting $chan");
512 if (!&validChan($chan)) {
513 &WARN("part: not on $chan; doing anyway");
518 # deletion of $channels{chan} is done in &entryEvt().
523 my ($chan, @modes) = @_;
524 my $modes = join(" ", @modes);
526 if (&validChan($chan) == 0) {
527 &ERROR("mode: invalid chan => '$chan'.");
531 &DEBUG("mode: MODE $chan $modes");
533 # should move to use Net::IRC's $conn->mode()... but too lazy.
534 rawout("MODE $chan $modes");
538 my ($chan, @who) = @_;
539 my $os = "o" x scalar(@who);
541 &mode($chan, "+$os @who");
545 my ($chan, @who) = @_;
546 my $os = "o" x scalar(@who);
548 &mode($chan, "-$os ".@who);
552 my ($nick,$chan,$msg) = @_;
553 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
554 my $mynick = $conn->nick();
556 if ($chan ne "" and &validChan($chan) == 0) {
557 &ERROR("kick: invalid channel $chan.");
561 $nick =~ tr/A-Z/a-z/;
563 foreach $chan (@chans) {
564 if (!&IsNickInChan($nick,$chan)) {
565 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
569 if (!exists $channels{$chan}{o}{$mynick}) {
570 &status("kick: do not have ops on $chan :(");
574 &status("Kicking $nick from $chan.");
575 $conn->kick($chan, $nick, $msg);
580 my ($mask,$chan) = @_;
581 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
582 my $mynick = $conn->nick();
585 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
586 &ERROR("ban: invalid channel $chan.");
590 foreach $chan (@chans) {
591 if (!exists $channels{$chan}{o}{$mynick}) {
592 &status("ban: do not have ops on $chan :(");
596 &status("Banning $mask from $chan.");
597 &rawout("MODE $chan +b $mask");
605 my ($mask,$chan) = @_;
606 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
607 my $mynick = $conn->nick();
610 &DEBUG("unban: mask = $mask, chan = @chans");
612 foreach $chan (@chans) {
613 if (!exists $channels{$chan}{o}{$mynick}) {
614 &status("unBan: do not have ops on $chan :(");
618 &status("Removed ban $mask from $chan.");
619 &rawout("MODE $chan -b $mask");
629 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
630 $conn->quit($quitmsg);
632 &WARN("quit: could not quit!");
638 my $mynick = $conn->nick();
640 if (!defined $newnick) {
641 &ERROR("nick: nick == NULL.");
645 if (!defined $mynick) {
646 &WARN("nick: mynick == NULL.");
651 $bad++ if (exists $nuh{$newnick});
652 $bad++ if (&IsNickInAnyChan($newnick));
655 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
656 # hrm... over time we lose track of our own nick.
660 if ($newnick =~ /^$mask{nick}$/) {
661 &status("nick: Changing nick from $mynick to $newnick");
662 # ->nick() will NOT change cause we are using rawout?
663 &rawout("NICK $newnick");
666 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
671 my($who, $chan) = @_;
672 # TODO: check if $who or $chan are invalid.
674 $conn->invite($who, $chan);
678 # Channel related functions...
681 # Usage: &joinNextChan();
684 foreach (sort keys %conns) {
686 my $mynick = $conn->nick();
687 my @join = getJoinChans(1);
690 my $chan = shift @join;
693 if (my $i = scalar @join) {
694 &status("joinNextChan: $mynick $i chans to join.");
701 if (exists $cache{joinTime}) {
702 my $delta = time() - $cache{joinTime} - 5;
703 my $timestr = &Time2String($delta);
704 # FIXME: @join should be @in instead (hacked to 10)
705 #my $rate = sprintf("%.1f", $delta / @in);
706 my $rate = sprintf("%.1f", $delta / 10);
707 delete $cache{joinTime};
709 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
712 # chanserv check: global channels, in case we missed one.
713 foreach ( &ChanConfList("chanServ_ops") ) {
718 # Usage: &getNickInChans($nick);
723 foreach (keys %channels) {
724 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
731 # Usage: &getNicksInChan($chan);
736 return keys %{ $channels{$chan}{''} };
740 my ($nick,$chan) = @_;
742 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
745 &DEBUG("INIC: chan == NULL.");
749 if (&validChan($chan) == 0) {
750 &ERROR("INIC: invalid channel $chan.");
754 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
757 foreach (keys %channels) {
758 next unless (/[A-Z]/);
759 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
765 sub IsNickInAnyChan {
769 foreach $chan (keys %channels) {
770 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
776 # Usage: &validChan($chan);
778 # TODO: use $c instead?
781 if (!defined $chan or $chan =~ /^\s*$/) {
785 if (lc $chan ne $chan) {
786 &WARN("validChan: lc chan != chan. ($chan); fixing.");
787 $chan =~ tr/A-Z/a-z/;
790 # it's possible that this check creates the hash if empty.
791 if (defined $channels{$chan} or exists $channels{$chan}) {
792 if ($chan =~ /^_?default$/) {
793 # &WARN("validC: chan cannot be _default! returning 0!");
804 # Usage: &delUserInfo($nick,@chans);
806 my ($nick,@chans) = @_;
809 foreach $chan (@chans) {
810 foreach $mode (keys %{ $channels{$chan} }) {
812 next unless (exists $channels{$chan}{$mode}{$nick});
814 delete $channels{$chan}{$mode}{$nick};
822 delete $channels{$chan};
829 $cache{joinTime} = time();
839 # can't join any if not connected
840 return @join if (!$conn);
842 my $nick = $conn->nick();
844 foreach (keys %chanconf) {
845 next if ($_ eq "_default");
848 my $val = $chanconf{$_}{autojoin};
851 $skip++ if ($val eq "0");
853 # convert old +autojoin to autojoin <nick>
855 $chanconf{$_}{autojoin} = $val;
857 $skip++ if (lc $val ne lc $nick);
865 if (defined $channels{$_} or exists $channels{$_}) {
874 #$str .= ' in:' . join(',', sort @in) if scalar @in;
875 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
876 $str .= ' join:' . join(',', sort @join) if scalar @join;
878 &status("Chans: ($nick)$str") if ($show);
884 # &DEBUG("closeDCC called.");
887 foreach $type (keys %dcc) {
888 next if ($type ne uc($type));
891 foreach $nick (keys %{ $dcc{$type} }) {
892 next unless (defined $nick);
893 &status("DCC CHAT: closing DCC $type to $nick.");
894 next unless (defined $dcc{$type}{$nick});
896 my $ref = $dcc{$type}{$nick};
897 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
898 $dcc{$type}{$nick}->close();
899 delete $dcc{$type}{$nick};
900 &DEBUG("after close for $nick");
907 my($who,$chan,$userhost) = @_;
909 return unless (&IsChanConf("joinfloodCheck") > 0);
911 if (exists $netsplit{lc $who}) { # netsplit join.
912 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
915 if (exists $floodjoin{$chan}{$who}{Time}) {
916 &WARN("floodjoin{$chan}{$who} already exists?");
919 $floodjoin{$chan}{$who}{Time} = time();
920 $floodjoin{$chan}{$who}{Host} = $userhost;
923 foreach (keys %floodjoin) {
925 my $count = scalar keys %{ $floodjoin{$c} };
926 next unless ($count > 5);
927 &DEBUG("joinflood: count => $count");
930 foreach (keys %{ $floodjoin{$c} }) {
931 my $t = $floodjoin{$c}{$_}{Time};
932 next unless (defined $t);
936 &DEBUG("joinflood: time => $time");
939 &DEBUG("joinflood: new time => $time");
945 foreach $chan (keys %floodjoin) {
946 foreach $who (keys %{ $floodjoin{$chan} }) {
947 my $t = $floodjoin{$chan}{$who}{Time};
948 next unless (defined $t);
950 my $delta = $time - $t;
951 next unless ($delta > 10);
953 delete $floodjoin{$chan}{$who};
958 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
964 if (exists $nuh{$n}) {
965 return &makeHostMask($nuh{$n});
967 $cache{on_who_Hack} = 1;