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 ( $postprocess ) {
217 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
218 &DEBUG("say: $postprocess $msg");
219 &parseCmdHook($postprocess . ' ' . $msg);
224 &status("<$mynick/$talkchannel> $msg");
226 return unless (&whatInterface() =~ /IRC/);
228 $msg = "zero" if ($msg =~ /^0+$/);
232 if ($t == $pubtime) {
234 $pubsize += length $msg;
236 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
237 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
239 if ( ($pubcount % $i) == 0 and $pubcount) {
241 } elsif ($pubsize > $j) {
249 $pubsize = length $msg;
252 $conn->privmsg($talkchannel, $msg);
256 my ($nick, $msg) = @_;
257 if (!defined $nick) {
258 &ERROR("msg: nick == NULL.");
264 &WARN("msg: msg == $msg.");
268 &status(">$nick< $msg");
270 return unless (&whatInterface() =~ /IRC/);
273 if ($t == $msgtime) {
275 $msgsize += length $msg;
277 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
278 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
279 if ( ($msgcount % $i) == 0 and $msgcount) {
281 } elsif ($msgsize > $j) {
289 $msgsize = length $msg;
292 $conn->privmsg($nick, $msg);
295 # Usage: &action(nick || chan, txt);
297 my $mynick = $conn->nick();
298 my ($target, $txt) = @_;
300 &WARN("action: txt == NULL.");
304 if (length $txt > 480) {
305 &status("action: txt too long; truncating.");
306 chop($txt) while (length $txt > 480);
309 &status("* $mynick/$target $txt");
310 $conn->me($target, $txt);
313 # Usage: ¬ice(nick || chan, txt);
315 my ($target, $txt) = @_;
317 &WARN("notice: txt == NULL.");
321 &status("-$target- $txt");
325 if ($t == $nottime) {
327 $notsize += length $txt;
329 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
330 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
332 if ( ($notcount % $i) == 0 and $notcount) {
334 } elsif ($notsize > $j) {
342 $notsize = length $txt;
345 $conn->notice($target, $txt);
349 my ($txt,$flag) = @_;
351 ### FIXME: flag not supported yet.
353 foreach (keys %{ $dcc{'CHAT'} }) {
354 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
359 ### perform commands.
362 # Usage: &performReply($reply);
366 if (!defined $reply or $reply =~ /^\s*$/) {
367 &DEBUG("performReply: reply == NULL.");
371 $reply =~ /([\.\?\s]+)$/;
373 # FIXME need real throttling....
374 if (length($reply) > $maxlinelen - 30) {
375 $reply = substr($reply, 0, $maxlinelen - 33);
376 $reply =~ s/ [^ ]*?$/ .../;
378 &checkMsgType($reply);
380 if ($msgType eq 'public') {
381 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
382 $reply = "$orig{who}: ".$reply;
384 $reply = "$reply, ".$orig{who};
388 } elsif ($msgType eq 'private') {
390 $reply = "$reply, ".$orig{who};
394 } elsif ($msgType eq 'chat') {
395 if (!exists $dcc{'CHAT'}{$who}) {
396 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
399 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
402 &ERROR("PR: msgType invalid? ($msgType).");
407 sub performAddressedReply {
408 return unless ($addressed);
412 # Usage: &performStrictReply($reply);
413 sub performStrictReply {
416 # FIXME need real throttling....
417 if (length($reply) > $maxlinelen - 30) {
418 $reply = substr($reply, 0, $maxlinelen - 33);
419 $reply =~ s/ [^ ]*?$/ .../;
421 &checkMsgType($reply);
423 if ($msgType eq 'private') {
425 } elsif ($msgType eq 'public') {
427 } elsif ($msgType eq 'chat') {
428 &dccsay(lc $who, $reply);
430 &ERROR("pSR: msgType invalid? ($msgType).");
435 my($who, $reply) = @_;
437 if (!defined $reply or $reply =~ /^\s*$/) {
438 &WARN("dccsay: reply == NULL.");
442 if (!exists $dcc{'CHAT'}{$who}) {
443 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
447 &status("=>$who<= $reply"); # dcc chat.
448 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
455 foreach $type (keys %dcc) {
456 &FIXME("dcc_close: $who");
457 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
458 next unless (scalar @who);
460 &DEBUG("dcc_close... close $who!");
465 my ($chan, $key) = @_;
466 $key ||= &getChanConf("chankey", $chan);
469 # forgot for about 2 years to implement channel keys when moving
470 # over to Net::IRC...
472 # hopefully validChan is right.
473 if (&validChan($chan)) {
474 &status("join: already on $chan?");
477 &status("joining $b_blue$chan $key$ob");
479 return if ($conn->join($chan, $key));
480 return if (&validChan($chan));
482 &DEBUG("joinchan: join failed. trying connect!");
492 next if ($chan eq "");
493 $chan =~ tr/A-Z/a-z/; # lowercase.
495 if ($chan !~ /^$mask{chan}$/) {
496 &WARN("part: chan is invalid ($chan)");
500 &status("parting $chan");
501 if (!&validChan($chan)) {
502 &WARN("part: not on $chan; doing anyway");
507 # deletion of $channels{chan} is done in &entryEvt().
512 my ($chan, @modes) = @_;
513 my $modes = join(" ", @modes);
515 if (&validChan($chan) == 0) {
516 &ERROR("mode: invalid chan => '$chan'.");
520 &DEBUG("mode: MODE $chan $modes");
522 # should move to use Net::IRC's $conn->mode()... but too lazy.
523 rawout("MODE $chan $modes");
527 my ($chan, @who) = @_;
528 my $os = "o" x scalar(@who);
530 &mode($chan, "+$os @who");
534 my ($chan, @who) = @_;
535 my $os = "o" x scalar(@who);
537 &mode($chan, "-$os ".@who);
541 my ($nick,$chan,$msg) = @_;
542 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
543 my $mynick = $conn->nick();
545 if ($chan ne "" and &validChan($chan) == 0) {
546 &ERROR("kick: invalid channel $chan.");
550 $nick =~ tr/A-Z/a-z/;
552 foreach $chan (@chans) {
553 if (!&IsNickInChan($nick,$chan)) {
554 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
558 if (!exists $channels{$chan}{o}{$mynick}) {
559 &status("kick: do not have ops on $chan :(");
563 &status("Kicking $nick from $chan.");
564 $conn->kick($chan, $nick, $msg);
569 my ($mask,$chan) = @_;
570 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
571 my $mynick = $conn->nick();
574 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
575 &ERROR("ban: invalid channel $chan.");
579 foreach $chan (@chans) {
580 if (!exists $channels{$chan}{o}{$mynick}) {
581 &status("ban: do not have ops on $chan :(");
585 &status("Banning $mask from $chan.");
586 &rawout("MODE $chan +b $mask");
594 my ($mask,$chan) = @_;
595 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
596 my $mynick = $conn->nick();
599 &DEBUG("unban: mask = $mask, chan = @chans");
601 foreach $chan (@chans) {
602 if (!exists $channels{$chan}{o}{$mynick}) {
603 &status("unBan: do not have ops on $chan :(");
607 &status("Removed ban $mask from $chan.");
608 &rawout("MODE $chan -b $mask");
618 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
619 $conn->quit($quitmsg);
621 &WARN("quit: could not quit!");
627 my $mynick = $conn->nick();
629 if (!defined $newnick) {
630 &ERROR("nick: nick == NULL.");
634 if (!defined $mynick) {
635 &WARN("nick: mynick == NULL.");
640 $bad++ if (exists $nuh{$newnick});
641 $bad++ if (&IsNickInAnyChan($newnick));
644 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
645 # hrm... over time we lose track of our own nick.
649 if ($newnick =~ /^$mask{nick}$/) {
650 &status("nick: Changing nick from $mynick to $newnick");
651 # ->nick() will NOT change cause we are using rawout?
652 &rawout("NICK $newnick");
655 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
660 my($who, $chan) = @_;
661 # TODO: check if $who or $chan are invalid.
663 $conn->invite($who, $chan);
667 # Channel related functions...
670 # Usage: &joinNextChan();
673 foreach (sort keys %conns) {
675 my $mynick = $conn->nick();
676 my @join = getJoinChans(1);
679 my $chan = shift @join;
682 if (my $i = scalar @join) {
683 &status("joinNextChan: $mynick $i chans to join.");
690 if (exists $cache{joinTime}) {
691 my $delta = time() - $cache{joinTime} - 5;
692 my $timestr = &Time2String($delta);
693 # FIXME: @join should be @in instead (hacked to 10)
694 #my $rate = sprintf("%.1f", $delta / @in);
695 my $rate = sprintf("%.1f", $delta / 10);
696 delete $cache{joinTime};
698 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
701 # chanserv check: global channels, in case we missed one.
702 foreach ( &ChanConfList("chanServ_ops") ) {
707 # Usage: &getNickInChans($nick);
712 foreach (keys %channels) {
713 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
720 # Usage: &getNicksInChan($chan);
725 return keys %{ $channels{$chan}{''} };
729 my ($nick,$chan) = @_;
731 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
734 &DEBUG("INIC: chan == NULL.");
738 if (&validChan($chan) == 0) {
739 &ERROR("INIC: invalid channel $chan.");
743 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
746 foreach (keys %channels) {
747 next unless (/[A-Z]/);
748 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
754 sub IsNickInAnyChan {
758 foreach $chan (keys %channels) {
759 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
765 # Usage: &validChan($chan);
767 # TODO: use $c instead?
770 if (!defined $chan or $chan =~ /^\s*$/) {
774 if (lc $chan ne $chan) {
775 &WARN("validChan: lc chan != chan. ($chan); fixing.");
776 $chan =~ tr/A-Z/a-z/;
779 # it's possible that this check creates the hash if empty.
780 if (defined $channels{$chan} or exists $channels{$chan}) {
781 if ($chan =~ /^_?default$/) {
782 # &WARN("validC: chan cannot be _default! returning 0!");
793 # Usage: &delUserInfo($nick,@chans);
795 my ($nick,@chans) = @_;
798 foreach $chan (@chans) {
799 foreach $mode (keys %{ $channels{$chan} }) {
801 next unless (exists $channels{$chan}{$mode}{$nick});
803 delete $channels{$chan}{$mode}{$nick};
811 delete $channels{$chan};
818 $cache{joinTime} = time();
828 # can't join any if not connected
829 return @join if (!$conn);
831 my $nick = $conn->nick();
833 foreach (keys %chanconf) {
834 next if ($_ eq "_default");
837 my $val = $chanconf{$_}{autojoin};
840 $skip++ if ($val eq "0");
842 # convert old +autojoin to autojoin <nick>
844 $chanconf{$_}{autojoin} = $val;
846 $skip++ if ($val ne $nick);
854 if (defined $channels{$_} or exists $channels{$_}) {
863 #$str .= ' in:' . join(',', sort @in) if scalar @in;
864 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
865 $str .= ' join:' . join(',', sort @join) if scalar @join;
867 &status("Chans: ($nick)$str") if ($show);
873 # &DEBUG("closeDCC called.");
876 foreach $type (keys %dcc) {
877 next if ($type ne uc($type));
880 foreach $nick (keys %{ $dcc{$type} }) {
881 next unless (defined $nick);
882 &status("DCC CHAT: closing DCC $type to $nick.");
883 next unless (defined $dcc{$type}{$nick});
885 my $ref = $dcc{$type}{$nick};
886 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
887 $dcc{$type}{$nick}->close();
888 delete $dcc{$type}{$nick};
889 &DEBUG("after close for $nick");
896 my($who,$chan,$userhost) = @_;
898 return unless (&IsChanConf("joinfloodCheck"));
900 if (exists $netsplit{lc $who}) { # netsplit join.
901 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
904 if (exists $floodjoin{$chan}{$who}{Time}) {
905 &WARN("floodjoin{$chan}{$who} already exists?");
908 $floodjoin{$chan}{$who}{Time} = time();
909 $floodjoin{$chan}{$who}{Host} = $userhost;
912 foreach (keys %floodjoin) {
914 my $count = scalar keys %{ $floodjoin{$c} };
915 next unless ($count > 5);
916 &DEBUG("joinflood: count => $count");
919 foreach (keys %{ $floodjoin{$c} }) {
920 my $t = $floodjoin{$c}{$_}{Time};
921 next unless (defined $t);
925 &DEBUG("joinflood: time => $time");
928 &DEBUG("joinflood: new time => $time");
934 foreach $chan (keys %floodjoin) {
935 foreach $who (keys %{ $floodjoin{$chan} }) {
936 my $t = $floodjoin{$chan}{$who}{Time};
937 next unless (defined $t);
939 my $delta = $time - $t;
940 next unless ($delta > 10);
942 delete $floodjoin{$chan}{$who};
947 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
953 if (exists $nuh{$n}) {
954 return &makeHostMask($nuh{$n});
956 $cache{on_who_Hack} = 1;