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 &status(">$nick< $msg");
275 return unless (&whatInterface() =~ /IRC/);
278 if ($t == $msgtime) {
280 $msgsize += length $msg;
282 my $i = &getChanConfDefault("sendPrivateLimitLines", 3, $chan);
283 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000, $chan);
284 if ( ($msgcount % $i) == 0 and $msgcount) {
286 } elsif ($msgsize > $j) {
294 $msgsize = length $msg;
297 $conn->privmsg($nick, $msg);
300 # Usage: &action(nick || chan, txt);
302 my $mynick = $conn->nick();
303 my ($target, $txt) = @_;
305 &WARN("action: txt == NULL.");
309 if (length $txt > 480) {
310 &status("action: txt too long; truncating.");
311 chop($txt) while (length $txt > 480);
314 &status("* $mynick/$target $txt");
315 $conn->me($target, $txt);
318 # Usage: ¬ice(nick || chan, txt);
320 my ($target, $txt) = @_;
322 &WARN("notice: txt == NULL.");
326 &status("-$target- $txt");
330 if ($t == $nottime) {
332 $notsize += length $txt;
334 my $i = &getChanConfDefault("sendNoticeLimitLines", 3, $chan);
335 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000, $chan);
337 if ( ($notcount % $i) == 0 and $notcount) {
339 } elsif ($notsize > $j) {
347 $notsize = length $txt;
350 $conn->notice($target, $txt);
354 my ($txt,$flag) = @_;
356 ### FIXME: flag not supported yet.
358 foreach (keys %{ $dcc{'CHAT'} }) {
359 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
364 ### perform commands.
367 # Usage: &performReply($reply);
371 if (!defined $reply or $reply =~ /^\s*$/) {
372 &DEBUG("performReply: reply == NULL.");
376 $reply =~ /([\.\?\s]+)$/;
378 # FIXME need real throttling....
379 if (length($reply) > $maxlinelen - 30) {
380 $reply = substr($reply, 0, $maxlinelen - 33);
381 $reply =~ s/ [^ ]*?$/ .../;
383 &checkMsgType($reply);
385 if ($msgType eq 'public') {
386 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
387 $reply = "$orig{who}: ".$reply;
389 $reply = "$reply, ".$orig{who};
393 } elsif ($msgType eq 'private') {
395 $reply = "$reply, ".$orig{who};
399 } elsif ($msgType eq 'chat') {
400 if (!exists $dcc{'CHAT'}{$who}) {
401 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
404 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
407 &ERROR("PR: msgType invalid? ($msgType).");
412 sub performAddressedReply {
413 return unless ($addressed);
417 # Usage: &performStrictReply($reply);
418 sub performStrictReply {
421 # FIXME need real throttling....
422 if (length($reply) > $maxlinelen - 30) {
423 $reply = substr($reply, 0, $maxlinelen - 33);
424 $reply =~ s/ [^ ]*?$/ .../;
426 &checkMsgType($reply);
428 if ($msgType eq 'private') {
430 } elsif ($msgType eq 'public') {
432 } elsif ($msgType eq 'chat') {
433 &dccsay(lc $who, $reply);
435 &ERROR("pSR: msgType invalid? ($msgType).");
440 my($who, $reply) = @_;
442 if (!defined $reply or $reply =~ /^\s*$/) {
443 &WARN("dccsay: reply == NULL.");
447 if (!exists $dcc{'CHAT'}{$who}) {
448 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
452 &status("=>$who<= $reply"); # dcc chat.
453 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
460 foreach $type (keys %dcc) {
461 &FIXME("dcc_close: $who");
462 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
463 next unless (scalar @who);
465 &DEBUG("dcc_close... close $who!");
470 my ($chan, $key) = @_;
471 $key ||= &getChanConf("chankey", $chan);
474 # forgot for about 2 years to implement channel keys when moving
475 # over to Net::IRC...
477 # hopefully validChan is right.
478 if (&validChan($chan)) {
479 &status("join: already on $chan?");
482 &status("joining $b_blue$chan $key$ob");
484 return if ($conn->join($chan, $key));
485 return if (&validChan($chan));
487 &DEBUG("joinchan: join failed. trying connect!");
497 next if ($chan eq "");
498 $chan =~ tr/A-Z/a-z/; # lowercase.
500 if ($chan !~ /^$mask{chan}$/) {
501 &WARN("part: chan is invalid ($chan)");
505 &status("parting $chan");
506 if (!&validChan($chan)) {
507 &WARN("part: not on $chan; doing anyway");
512 # deletion of $channels{chan} is done in &entryEvt().
517 my ($chan, @modes) = @_;
518 my $modes = join(" ", @modes);
520 if (&validChan($chan) == 0) {
521 &ERROR("mode: invalid chan => '$chan'.");
525 &DEBUG("mode: MODE $chan $modes");
527 # should move to use Net::IRC's $conn->mode()... but too lazy.
528 rawout("MODE $chan $modes");
532 my ($chan, @who) = @_;
533 my $os = "o" x scalar(@who);
535 &mode($chan, "+$os @who");
539 my ($chan, @who) = @_;
540 my $os = "o" x scalar(@who);
542 &mode($chan, "-$os ".@who);
546 my ($nick,$chan,$msg) = @_;
547 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
548 my $mynick = $conn->nick();
550 if ($chan ne "" and &validChan($chan) == 0) {
551 &ERROR("kick: invalid channel $chan.");
555 $nick =~ tr/A-Z/a-z/;
557 foreach $chan (@chans) {
558 if (!&IsNickInChan($nick,$chan)) {
559 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
563 if (!exists $channels{$chan}{o}{$mynick}) {
564 &status("kick: do not have ops on $chan :(");
568 &status("Kicking $nick from $chan.");
569 $conn->kick($chan, $nick, $msg);
574 my ($mask,$chan) = @_;
575 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
576 my $mynick = $conn->nick();
579 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
580 &ERROR("ban: invalid channel $chan.");
584 foreach $chan (@chans) {
585 if (!exists $channels{$chan}{o}{$mynick}) {
586 &status("ban: do not have ops on $chan :(");
590 &status("Banning $mask from $chan.");
591 &rawout("MODE $chan +b $mask");
599 my ($mask,$chan) = @_;
600 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
601 my $mynick = $conn->nick();
604 &DEBUG("unban: mask = $mask, chan = @chans");
606 foreach $chan (@chans) {
607 if (!exists $channels{$chan}{o}{$mynick}) {
608 &status("unBan: do not have ops on $chan :(");
612 &status("Removed ban $mask from $chan.");
613 &rawout("MODE $chan -b $mask");
623 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
624 $conn->quit($quitmsg);
626 &WARN("quit: could not quit!");
632 my $mynick = $conn->nick();
634 if (!defined $newnick) {
635 &ERROR("nick: nick == NULL.");
639 if (!defined $mynick) {
640 &WARN("nick: mynick == NULL.");
645 $bad++ if (exists $nuh{$newnick});
646 $bad++ if (&IsNickInAnyChan($newnick));
649 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
650 # hrm... over time we lose track of our own nick.
654 if ($newnick =~ /^$mask{nick}$/) {
655 &status("nick: Changing nick from $mynick to $newnick");
656 # ->nick() will NOT change cause we are using rawout?
657 &rawout("NICK $newnick");
660 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
665 my($who, $chan) = @_;
666 # TODO: check if $who or $chan are invalid.
668 $conn->invite($who, $chan);
672 # Channel related functions...
675 # Usage: &joinNextChan();
678 foreach (sort keys %conns) {
680 my $mynick = $conn->nick();
681 my @join = getJoinChans(1);
684 my $chan = shift @join;
687 if (my $i = scalar @join) {
688 &status("joinNextChan: $mynick $i chans to join.");
695 if (exists $cache{joinTime}) {
696 my $delta = time() - $cache{joinTime} - 5;
697 my $timestr = &Time2String($delta);
698 # FIXME: @join should be @in instead (hacked to 10)
699 #my $rate = sprintf("%.1f", $delta / @in);
700 my $rate = sprintf("%.1f", $delta / 10);
701 delete $cache{joinTime};
703 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
706 # chanserv check: global channels, in case we missed one.
707 foreach ( &ChanConfList("chanServ_ops") ) {
712 # Usage: &getNickInChans($nick);
717 foreach (keys %channels) {
718 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
725 # Usage: &getNicksInChan($chan);
730 return keys %{ $channels{$chan}{''} };
734 my ($nick,$chan) = @_;
736 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
739 &DEBUG("INIC: chan == NULL.");
743 if (&validChan($chan) == 0) {
744 &ERROR("INIC: invalid channel $chan.");
748 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
751 foreach (keys %channels) {
752 next unless (/[A-Z]/);
753 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
759 sub IsNickInAnyChan {
763 foreach $chan (keys %channels) {
764 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
770 # Usage: &validChan($chan);
772 # TODO: use $c instead?
775 if (!defined $chan or $chan =~ /^\s*$/) {
779 if (lc $chan ne $chan) {
780 &WARN("validChan: lc chan != chan. ($chan); fixing.");
781 $chan =~ tr/A-Z/a-z/;
784 # it's possible that this check creates the hash if empty.
785 if (defined $channels{$chan} or exists $channels{$chan}) {
786 if ($chan =~ /^_?default$/) {
787 # &WARN("validC: chan cannot be _default! returning 0!");
798 # Usage: &delUserInfo($nick,@chans);
800 my ($nick,@chans) = @_;
803 foreach $chan (@chans) {
804 foreach $mode (keys %{ $channels{$chan} }) {
806 next unless (exists $channels{$chan}{$mode}{$nick});
808 delete $channels{$chan}{$mode}{$nick};
816 delete $channels{$chan};
823 $cache{joinTime} = time();
833 # can't join any if not connected
834 return @join if (!$conn);
836 my $nick = $conn->nick();
838 foreach (keys %chanconf) {
839 next if ($_ eq "_default");
842 my $val = $chanconf{$_}{autojoin};
845 $skip++ if ($val eq "0");
847 # convert old +autojoin to autojoin <nick>
849 $chanconf{$_}{autojoin} = $val;
851 $skip++ if (lc $val ne lc $nick);
859 if (defined $channels{$_} or exists $channels{$_}) {
868 #$str .= ' in:' . join(',', sort @in) if scalar @in;
869 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
870 $str .= ' join:' . join(',', sort @join) if scalar @join;
872 &status("Chans: ($nick)$str") if ($show);
878 # &DEBUG("closeDCC called.");
881 foreach $type (keys %dcc) {
882 next if ($type ne uc($type));
885 foreach $nick (keys %{ $dcc{$type} }) {
886 next unless (defined $nick);
887 &status("DCC CHAT: closing DCC $type to $nick.");
888 next unless (defined $dcc{$type}{$nick});
890 my $ref = $dcc{$type}{$nick};
891 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
892 $dcc{$type}{$nick}->close();
893 delete $dcc{$type}{$nick};
894 &DEBUG("after close for $nick");
901 my($who,$chan,$userhost) = @_;
903 return unless (&IsChanConf("joinfloodCheck") > 0);
905 if (exists $netsplit{lc $who}) { # netsplit join.
906 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
909 if (exists $floodjoin{$chan}{$who}{Time}) {
910 &WARN("floodjoin{$chan}{$who} already exists?");
913 $floodjoin{$chan}{$who}{Time} = time();
914 $floodjoin{$chan}{$who}{Host} = $userhost;
917 foreach (keys %floodjoin) {
919 my $count = scalar keys %{ $floodjoin{$c} };
920 next unless ($count > 5);
921 &DEBUG("joinflood: count => $count");
924 foreach (keys %{ $floodjoin{$c} }) {
925 my $t = $floodjoin{$c}{$_}{Time};
926 next unless (defined $t);
930 &DEBUG("joinflood: time => $time");
933 &DEBUG("joinflood: new time => $time");
939 foreach $chan (keys %floodjoin) {
940 foreach $who (keys %{ $floodjoin{$chan} }) {
941 my $t = $floodjoin{$chan}{$who}{Time};
942 next unless (defined $t);
944 my $delta = $time - $t;
945 next unless ($delta > 10);
947 delete $floodjoin{$chan}{$who};
952 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
958 if (exists $nuh{$n}) {
959 return &makeHostMask($nuh{$n});
961 $cache{on_who_Hack} = 1;