2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
12 %chanconf %orig %ircPort %ircstats %last %netsplit);
13 use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
15 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
16 $pubcount $pubtime $pubsize);
17 use vars qw($b_blue $ob);
18 use vars qw(@joinchan @ircServers);
27 while (my $host = shift @ircServers) {
28 # JUST IN CASE. irq was complaining about this.
29 if ($lastrun == time()) {
30 &DEBUG("ircloop: hrm... lastrun == time()");
37 &DEBUG("ircloop: ircServers[x] = NULL.");
41 next unless (exists $ircPort{$host});
43 my $retval = &irc($host, $ircPort{$host});
44 next unless (defined $retval and $retval == 0);
47 if ($error % 3 == 0 and $error != 0) {
48 &status("IRC: Could not connect.");
54 &status("IRC: cannot connect to any IRC servers; stopping.");
60 &status("IRC: ok, done one cycle of IRC servers; trying again.");
67 my ($server,$port) = @_;
69 my $iaddr = inet_aton($server);
70 my $paddr = sockaddr_in($port, $iaddr);
71 my $proto = getprotobyname('tcp');
74 &status("Connecting to port $port of server $server ...");
77 if ($server =~ /\D$/) {
78 my $packed = scalar(gethostbyname($server));
80 if (!defined $packed) {
81 &status(" cannot resolve $server.");
85 my $resolve = inet_ntoa($packed);
86 &status(" resolved to $resolve.");
87 ### warning in Sys/Hostname line 78???
88 ### caused inside Net::IRC?
94 Nick => $param{'ircNick'},
97 Ircname => $param{'ircName'},
99 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
100 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
102 $conn = $irc->newconn(%args);
104 if (!defined $conn) {
105 &ERROR("IRC: connection failed.");
106 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
107 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
108 &ERROR("127.0.0.1 localhost localhost");
109 &ERROR("If this is still a problem, please contact the maintainer.");
115 # change internal timeout value for scheduler.
116 $irc->{_timeout} = 10; # how about 60?
117 # Net::IRC debugging.
120 $ircstats{'Server'} = "$server:$port";
123 $conn->add_handler('caction', \&on_action);
124 $conn->add_handler('cdcc', \&on_dcc);
125 $conn->add_handler('cping', \&on_ping);
126 $conn->add_handler('crping', \&on_ping_reply);
127 $conn->add_handler('cversion', \&on_version);
128 $conn->add_handler('crversion', \&on_crversion);
129 $conn->add_handler('dcc_open', \&on_dcc_open);
130 $conn->add_handler('dcc_close', \&on_dcc_close);
131 $conn->add_handler('chat', \&on_chat);
132 $conn->add_handler('msg', \&on_msg);
133 $conn->add_handler('public', \&on_public);
134 $conn->add_handler('join', \&on_join);
135 $conn->add_handler('part', \&on_part);
136 $conn->add_handler('topic', \&on_topic);
137 $conn->add_handler('invite', \&on_invite);
138 $conn->add_handler('kick', \&on_kick);
139 $conn->add_handler('mode', \&on_mode);
140 $conn->add_handler('nick', \&on_nick);
141 $conn->add_handler('quit', \&on_quit);
142 $conn->add_handler('notice', \&on_notice);
143 $conn->add_handler('whoischannels', \&on_whoischannels);
144 $conn->add_handler('useronchannel', \&on_useronchannel);
145 $conn->add_handler('whois', \&on_whois);
146 $conn->add_handler('other', \&on_other);
147 $conn->add_global_handler('disconnect', \&on_disconnect);
148 $conn->add_global_handler([251,252,253,254,255], \&on_init);
149 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
150 $conn->add_global_handler(303, \&on_ison); # notify.
151 $conn->add_global_handler(315, \&on_endofwho);
152 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
153 $conn->add_global_handler(324, \&on_modeis);
154 $conn->add_global_handler(333, \&on_topicinfo);
155 $conn->add_global_handler(352, \&on_who);
156 $conn->add_global_handler(353, \&on_names);
157 $conn->add_global_handler(366, \&on_endofnames);
158 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
159 $conn->add_global_handler(433, \&on_nick_taken);
160 $conn->add_global_handler(439, \&on_targettoofast);
161 # for proper joinnextChan behaviour
162 $conn->add_global_handler(471, \&on_chanfull);
163 $conn->add_global_handler(473, \&on_inviteonly);
164 $conn->add_global_handler(474, \&on_banned);
165 $conn->add_global_handler(475, \&on_badchankey);
166 $conn->add_global_handler(443, \&on_useronchan);
168 # end of handler stuff.
173 ######################################################################
174 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
175 ######################################################################
181 # slow down a bit if traffic is "high".
182 # need to take into account time of last message sent.
183 if ($last{buflen} > 256 and length($buf) > 256) {
187 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
189 $last{buflen} = length($buf);
196 &WARN("say: msg == $msg.");
200 &status("</$talkchannel> $msg");
201 if (&whatInterface() =~ /IRC/) {
202 $msg = "zero" if ($msg =~ /^0+$/);
205 if ($t == $pubtime) {
207 $pubsize += length $msg;
209 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
210 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
212 if ( ($pubcount % $i) == 0 and $pubcount) {
214 } elsif ($pubsize > $j) {
222 $pubsize = length $msg;
225 $conn->privmsg($talkchannel, $msg);
230 my ($nick, $msg) = @_;
231 if (!defined $nick) {
232 &ERROR("msg: nick == NULL.");
238 &WARN("msg: msg == $msg.");
242 &status(">$nick< $msg");
244 return unless (&whatInterface() =~ /IRC/);
247 if ($t == $msgtime) {
249 $msgsize += length $msg;
251 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
252 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
253 if ( ($msgcount % $i) == 0 and $msgcount) {
255 } elsif ($msgsize > $j) {
263 $msgsize = length $msg;
266 $conn->privmsg($nick, $msg);
269 # Usage: &action(nick || chan, txt);
271 my ($target, $txt) = @_;
273 &WARN("action: txt == NULL.");
277 if (length $txt > 480) {
278 &status("action: txt too long; truncating.");
279 chop($txt) while (length $txt > 480);
282 &status("* $ident/$target $txt");
283 $conn->me($target, $txt);
286 # Usage: ¬ice(nick || chan, txt);
288 my ($target, $txt) = @_;
290 &WARN("notice: txt == NULL.");
294 &status("-$target- $txt");
298 if ($t == $nottime) {
300 $notsize += length $txt;
302 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
303 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
305 if ( ($notcount % $i) == 0 and $notcount) {
307 } elsif ($notsize > $j) {
315 $notsize = length $txt;
318 $conn->notice($target, $txt);
322 my ($txt,$flag) = @_;
324 ### FIXME: flag not supported yet.
326 foreach (keys %{ $dcc{'CHAT'} }) {
327 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
332 ### perform commands.
335 # Usage: &performReply($reply);
339 if (!defined $reply or $reply =~ /^\s*$/) {
340 &DEBUG("performReply: reply == NULL.");
344 $reply =~ /([\.\?\s]+)$/;
346 &checkMsgType($reply);
348 if ($msgType eq 'public') {
349 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
350 $reply = "$orig{who}: ".$reply;
352 $reply = "$reply, ".$orig{who};
356 } elsif ($msgType eq 'private') {
358 $reply = "$reply, ".$orig{who};
362 } elsif ($msgType eq 'chat') {
363 if (!exists $dcc{'CHAT'}{$who}) {
364 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
367 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
370 &ERROR("PR: msgType invalid? ($msgType).");
375 sub performAddressedReply {
376 return unless ($addressed);
381 &performStrictReply(@_);
384 # Usage: &performStrictReply($reply);
385 sub performStrictReply {
388 &checkMsgType($reply);
390 if ($msgType eq 'private') {
392 } elsif ($msgType eq 'public') {
394 } elsif ($msgType eq 'chat') {
395 &dccsay(lc $who, $reply);
397 &ERROR("pSR: msgType invalid? ($msgType).");
402 my($who, $reply) = @_;
404 if (!defined $reply or $reply =~ /^\s*$/) {
405 &WARN("dccsay: reply == NULL.");
409 if (!exists $dcc{'CHAT'}{$who}) {
410 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
414 &status("=>$who<= $reply"); # dcc chat.
415 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
422 foreach $type (keys %dcc) {
423 &FIXME("dcc_close: $who");
424 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
425 next unless (scalar @who);
427 &DEBUG("dcc_close... close $who!");
433 my $key = &getChanConf("chankey", $chan) || "";
435 # forgot for about 2 years to implement channel keys when moving
436 # over to Net::IRC...
438 # hopefully validChan is right.
439 if (&validChan($chan)) {
440 &status("join: already on $chan");
442 &status("joining $b_blue$chan$ob");
444 return if ($conn->join($chan, $key));
446 &DEBUG("joinchan: join failed. trying connect!");
456 next if ($chan eq "");
457 $chan =~ tr/A-Z/a-z/; # lowercase.
459 if ($chan !~ /^$mask{chan}$/) {
460 &WARN("part: chan is invalid ($chan)");
464 &status("parting $chan");
465 if (!&validChan($chan)) {
466 &WARN("part: not on $chan; doing anyway");
471 # deletion of $channels{chan} is done in &entryEvt().
476 my ($chan, @modes) = @_;
477 my $modes = join(" ", @modes);
479 if (&validChan($chan) == 0) {
480 &ERROR("mode: invalid chan => '$chan'.");
484 &DEBUG("mode: MODE $chan $modes");
486 # should move to use Net::IRC's $conn->mode()... but too lazy.
487 rawout("MODE $chan $modes");
491 my ($chan, @who) = @_;
492 my $os = "o" x scalar(@who);
494 &mode($chan, "+$os @who");
498 my ($chan, @who) = @_;
499 my $os = "o" x scalar(@who);
501 &mode($chan, "-$os ".@who);
505 my ($nick,$chan,$msg) = @_;
506 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
508 if ($chan ne "" and &validChan($chan) == 0) {
509 &ERROR("kick: invalid channel $chan.");
513 $nick =~ tr/A-Z/a-z/;
515 foreach $chan (@chans) {
516 if (!&IsNickInChan($nick,$chan)) {
517 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
521 if (!exists $channels{$chan}{o}{$ident}) {
522 &status("kick: do not have ops on $chan :(");
526 &status("Kicking $nick from $chan.");
527 $conn->kick($chan, $nick, $msg);
532 my ($mask,$chan) = @_;
533 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
536 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
537 &ERROR("ban: invalid channel $chan.");
541 foreach $chan (@chans) {
542 if (!exists $channels{$chan}{o}{$ident}) {
543 &status("ban: do not have ops on $chan :(");
547 &status("Banning $mask from $chan.");
548 &rawout("MODE $chan +b $mask");
556 my ($mask,$chan) = @_;
557 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
560 &DEBUG("unban: mask = $mask, chan = @chans");
562 foreach $chan (@chans) {
563 if (!exists $channels{$chan}{o}{$ident}) {
564 &status("unBan: do not have ops on $chan :(");
568 &status("Removed ban $mask from $chan.");
569 &rawout("MODE $chan -b $mask");
578 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
580 $conn->quit($quitmsg);
582 &WARN("quit: could not quit!");
589 if (!defined $nick) {
590 &ERROR("nick: nick == NULL.");
594 if (defined $ident and $nick eq $ident) {
595 &WARN("nick: nick == ident == '$ident'.");
600 $bad++ if (exists $nuh{ $param{'ircNick'} });
601 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
604 &WARN("Nick: not going to try and get my nick back. [".
605 scalar(gmtime). "]");
606 # hrm... over time we lose track of our own nick.
610 if ($nick =~ /^$mask{nick}$/) {
611 &rawout("NICK ".$nick);
613 if (defined $ident) {
614 &status("nick: Changing nick to $nick (from $ident)");
615 # following shouldn't be here :(
618 &DEBUG("first time nick change.");
624 &DEBUG("nick: failed... why oh why (nick => $nick)");
630 my($who, $chan) = @_;
631 # todo: check if $who or $chan are invalid.
633 $conn->invite($who, $chan);
637 # Channel related functions...
640 # Usage: &joinNextChan();
642 if (scalar @joinchan) {
643 my $chan = shift @joinchan;
646 if (my $i = scalar @joinchan) {
647 &status("joinNextChan: $i chans to join.");
654 my @c = &getJoinChans();
655 if (exists $cache{joinTime} and scalar @c) {
656 my $delta = time() - $cache{joinTime} - 5;
657 my $timestr = &Time2String($delta);
658 my $rate = sprintf("%.1f", $delta / @c);
659 delete $cache{joinTime};
661 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
664 # chanserv check: global channels, in case we missed one.
665 foreach ( &ChanConfList("chanServ_ops") ) {
670 # Usage: &getNickInChans($nick);
675 foreach (keys %channels) {
676 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
683 # Usage: &getNicksInChan($chan);
688 return keys %{ $channels{$chan}{''} };
692 my ($nick,$chan) = @_;
694 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
697 &DEBUG("INIC: chan == NULL.");
701 if (&validChan($chan) == 0) {
702 &ERROR("INIC: invalid channel $chan.");
706 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
709 foreach (keys %channels) {
710 next unless (/[A-Z]/);
711 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
717 sub IsNickInAnyChan {
721 foreach $chan (keys %channels) {
722 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
728 # Usage: &validChan($chan);
730 # todo: use $c instead?
733 if (!defined $chan or $chan =~ /^\s*$/) {
737 if (lc $chan ne $chan) {
738 &WARN("validChan: lc chan != chan. ($chan); fixing.");
739 $chan =~ tr/A-Z/a-z/;
742 # it's possible that this check creates the hash if empty.
743 if (defined $channels{$chan} or exists $channels{$chan}) {
744 if ($chan =~ /^_?default$/) {
745 # &WARN("validC: chan cannot be _default! returning 0!");
756 # Usage: &delUserInfo($nick,@chans);
758 my ($nick,@chans) = @_;
761 foreach $chan (@chans) {
762 foreach $mode (keys %{ $channels{$chan} }) {
764 next unless (exists $channels{$chan}{$mode}{$nick});
766 delete $channels{$chan}{$mode}{$nick};
774 delete $channels{$chan};
781 @joinchan = &getJoinChans(1);
782 $cache{joinTime} = time();
790 foreach (keys %chanconf) {
791 next if ($_ eq "_default");
793 my $val = $chanconf{$_}{autojoin};
797 $skip++ if ($val eq "0");
812 $str = "channels not auto-joining: @skip (joining: @chans)";
814 $str = "auto-joining all chans: @chans";
817 &status("Chans: ".$str) if ($show);
823 # &DEBUG("closeDCC called.");
826 foreach $type (keys %dcc) {
827 next if ($type ne uc($type));
830 foreach $nick (keys %{ $dcc{$type} }) {
831 next unless (defined $nick);
832 &status("DCC CHAT: closing DCC $type to $nick.");
833 next unless (defined $dcc{$type}{$nick});
835 my $ref = $dcc{$type}{$nick};
836 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
837 $dcc{$type}{$nick}->close();
838 delete $dcc{$type}{$nick};
839 &DEBUG("after close for $nick");
846 my($who,$chan,$userhost) = @_;
848 return unless (&IsChanConf("joinfloodCheck"));
850 if (exists $netsplit{lc $who}) { # netsplit join.
851 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
854 if (exists $floodjoin{$chan}{$who}{Time}) {
855 &WARN("floodjoin{$chan}{$who} already exists?");
858 $floodjoin{$chan}{$who}{Time} = time();
859 $floodjoin{$chan}{$who}{Host} = $userhost;
862 foreach (keys %floodjoin) {
864 my $count = scalar keys %{ $floodjoin{$c} };
865 next unless ($count > 5);
866 &DEBUG("joinflood: count => $count");
869 foreach (keys %{ $floodjoin{$c} }) {
870 my $t = $floodjoin{$c}{$_}{Time};
871 next unless (defined $t);
875 &DEBUG("joinflood: time => $time");
878 &DEBUG("joinflood: new time => $time");
884 foreach $chan (keys %floodjoin) {
885 foreach $who (keys %{ $floodjoin{$chan} }) {
886 my $t = $floodjoin{$chan}{$who}{Time};
887 next unless (defined $t);
889 my $delta = $time - $t;
890 next unless ($delta > 10);
892 delete $floodjoin{$chan}{$who};
897 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
903 if (exists $nuh{$n}) {
904 return &makeHostMask($nuh{$n});
906 $cache{on_who_Hack} = 1;