2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
8 if (&IsParam("useStrict")) { use strict; }
10 use vars qw($nickserv);
13 # static scalar variables.
14 $mask{ip} = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
15 $mask{host} = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
16 $mask{chan} = '[\#\&]\S*|_default';
17 my $isnick1 = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
18 my $isnick2 = '0-9\-';
19 $mask{nick} = "[$isnick1]{1}[$isnick1$isnick2]*";
20 $mask{nuh} = '\S*!\S*\@\S*';
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: conn was not created!defined!!!");
111 # change internal timeout value for scheduler.
112 $irc->{_timeout} = 10; # how about 60?
113 # Net::IRC debugging.
116 $ircstats{'Server'} = "$server:$port";
119 $conn->add_handler('caction', \&on_action);
120 $conn->add_handler('cdcc', \&on_dcc);
121 $conn->add_handler('cping', \&on_ping);
122 $conn->add_handler('crping', \&on_ping_reply);
123 $conn->add_handler('cversion', \&on_version);
124 $conn->add_handler('crversion', \&on_crversion);
125 $conn->add_handler('dcc_open', \&on_dcc_open);
126 $conn->add_handler('dcc_close', \&on_dcc_close);
127 $conn->add_handler('chat', \&on_chat);
128 $conn->add_handler('msg', \&on_msg);
129 $conn->add_handler('public', \&on_public);
130 $conn->add_handler('join', \&on_join);
131 $conn->add_handler('part', \&on_part);
132 $conn->add_handler('topic', \&on_topic);
133 $conn->add_handler('invite', \&on_invite);
134 $conn->add_handler('kick', \&on_kick);
135 $conn->add_handler('mode', \&on_mode);
136 $conn->add_handler('nick', \&on_nick);
137 $conn->add_handler('quit', \&on_quit);
138 $conn->add_handler('notice', \&on_notice);
139 $conn->add_handler('whoischannels', \&on_whoischannels);
140 $conn->add_handler('useronchannel', \&on_useronchannel);
141 $conn->add_handler('whois', \&on_whois);
142 $conn->add_handler('other', \&on_other);
143 $conn->add_global_handler('disconnect', \&on_disconnect);
144 $conn->add_global_handler([251,252,253,254,255], \&on_init);
145 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
146 $conn->add_global_handler(315, \&on_endofwho);
147 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
148 $conn->add_global_handler(324, \&on_modeis);
149 $conn->add_global_handler(333, \&on_topicinfo);
150 $conn->add_global_handler(352, \&on_who);
151 $conn->add_global_handler(353, \&on_names);
152 $conn->add_global_handler(366, \&on_endofnames);
153 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
154 $conn->add_global_handler(433, \&on_nick_taken);
155 $conn->add_global_handler(439, \&on_targettoofast);
156 # for proper joinnextChan behaviour
157 $conn->add_global_handler(471, \&on_chanfull);
158 $conn->add_global_handler(473, \&on_inviteonly);
159 $conn->add_global_handler(474, \&on_banned);
160 $conn->add_global_handler(475, \&on_badchankey);
162 # end of handler stuff.
167 ######################################################################
168 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
169 ######################################################################
175 # slow down a bit if traffic is "high".
176 # need to take into account time of last message sent.
177 if ($last{buflen} > 256 and length($buf) > 256) {
181 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
183 $last{buflen} = length($buf);
190 &WARN("say: msg == $msg.");
194 &status("</$talkchannel> $msg");
195 if (&whatInterface() =~ /IRC/) {
196 $msg = "zero" if ($msg =~ /^0+$/);
199 if ($t == $pubtime) {
201 $pubsize += length $msg;
203 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
204 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
206 if ( ($pubcount % $i) == 0 and $pubcount) {
208 } elsif ($pubsize > $j) {
216 $pubsize = length $msg;
219 $conn->privmsg($talkchannel, $msg);
224 my ($nick, $msg) = @_;
225 if (!defined $nick) {
226 &ERROR("msg: nick == NULL.");
232 &WARN("msg: msg == $msg.");
236 if ($msgType =~ /chat/i) {
237 # todo: warn that we're using msg() to do DCC CHAT?
238 &dccsay($nick, $msg);
239 # todo: make dccsay deal with flood protection?
243 &status(">$nick< $msg");
245 if (&whatInterface() =~ /IRC/) {
248 if ($t == $msgtime) {
250 $msgsize += length $msg;
252 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
253 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
254 if ( ($msgcount % $i) == 0 and $msgcount) {
256 } elsif ($msgsize > $j) {
264 $msgsize = length $msg;
267 $conn->privmsg($nick, $msg);
271 # Usage: &action(nick || chan, txt);
273 my ($target, $txt) = @_;
275 &WARN("action: txt == NULL.");
279 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
280 if (length $rawout > 510) {
281 &status("action: txt too long; truncating.");
283 chop($rawout) while (length($rawout) > 510);
287 &status("* $ident/$target $txt");
291 # Usage: ¬ice(nick || chan, txt);
293 my ($target, $txt) = @_;
295 &WARN("notice: txt == NULL.");
299 &status("-$target- $txt");
303 if ($t == $nottime) {
305 $notsize += length $txt;
307 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
308 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
310 if ( ($notcount % $i) == 0 and $notcount) {
312 } elsif ($notsize > $j) {
320 $notsize = length $txt;
323 $conn->notice($target, $txt);
327 my ($txt,$flag) = @_;
329 ### FIXME: flag not supported yet.
331 foreach (keys %{ $dcc{'CHAT'} }) {
332 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
337 ### perform commands.
340 # Usage: &performReply($reply);
343 $reply =~ /([\.\?\s]+)$/;
345 &checkMsgType($reply);
347 if ($msgType eq 'public') {
348 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
349 $reply = "$orig{who}: ".$reply;
351 $reply = "$reply, ".$orig{who};
354 } elsif ($msgType eq 'private') {
358 $reply = "$reply, ".$orig{who};
361 } elsif ($msgType eq 'chat') {
362 if (!exists $dcc{'CHAT'}{$who}) {
363 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
366 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
368 &ERROR("PR: msgType invalid? ($msgType).");
373 sub performAddressedReply {
374 return unless ($addressed);
379 &performStrictReply(@_);
382 # Usage: &performStrictReply($reply);
383 sub performStrictReply {
386 &checkMsgType($reply);
388 if ($msgType eq 'private') {
390 } elsif ($msgType eq 'public') {
392 } elsif ($msgType eq 'chat') {
393 &dccsay(lc $who, $reply);
395 &ERROR("pSR: msgType invalid? ($msgType).");
400 my($who, $reply) = @_;
402 if (!defined $reply or $reply =~ /^\s*$/) {
403 &WARN("dccsay: reply == NULL.");
407 if (!exists $dcc{'CHAT'}{$who}) {
408 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
412 &status("=>$who<= $reply"); # dcc chat.
413 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
420 foreach $type (keys %dcc) {
421 &FIXME("dcc_close: $who");
422 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
423 next unless (scalar @who);
425 &DEBUG("dcc_close... close $who!");
431 my $chan = lc $chankey;
433 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
437 # hopefully validChan is right.
438 if (&validChan($chan)) {
439 &status("join: already on $chan");
441 &status("joining $b_blue$chan$ob");
442 if (!$conn->join($chan)) {
443 &DEBUG("joinchan: join failed. trying connect!");
454 next if ($chan eq "");
455 $chan =~ tr/A-Z/a-z/; # lowercase.
457 if ($chan !~ /^$mask{chan}$/) {
458 &WARN("part: chan is invalid ($chan)");
462 &status("parting $chan");
463 if (!&validChan($chan)) {
464 &WARN("part: not on $chan; doing anyway");
468 rawout("PART $chan");
469 # deletion of $channels{chan} is done in &entryEvt().
474 my ($chan, @modes) = @_;
475 my $modes = join(" ", @modes);
477 if (&validChan($chan) == 0) {
478 &ERROR("mode: invalid chan => '$chan'.");
482 &DEBUG("mode: MODE $chan $modes");
484 rawout("MODE $chan $modes");
488 my ($chan, @who) = @_;
489 my $os = "o" x scalar(@who);
491 &mode($chan, "+$os @who");
495 my ($chan, @who) = @_;
496 my $os = "o" x scalar(@who);
498 &mode($chan, "-$os ".@who);
502 my ($nick,$chan,$msg) = @_;
503 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
505 if ($chan ne "" and &validChan($chan) == 0) {
506 &ERROR("kick: invalid channel $chan.");
510 $nick =~ tr/A-Z/a-z/;
512 foreach $chan (@chans) {
513 if (!&IsNickInChan($nick,$chan)) {
514 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
518 if (!exists $channels{$chan}{o}{$ident}) {
519 &status("Kick: do not have ops on $chan :(");
523 &status("Kicking $nick from $chan.");
525 &rawout("KICK $chan $nick");
527 &rawout("KICK $chan $nick :$msg");
533 my ($mask,$chan) = @_;
534 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
537 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
538 &ERROR("ban: invalid channel $chan.");
542 foreach $chan (@chans) {
543 if (!exists $channels{$chan}{o}{$ident}) {
544 &status("Ban: do not have ops on $chan :(");
548 &status("Banning $mask from $chan.");
549 &rawout("MODE $chan +b $mask");
557 my ($mask,$chan) = @_;
558 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
561 &DEBUG("unban: mask = $mask, chan = @chans");
563 foreach $chan (@chans) {
564 if (!exists $channels{$chan}{o}{$ident}) {
565 &status("unBan: do not have ops on $chan :(");
569 &status("Removed ban $mask from $chan.");
570 &rawout("MODE $chan -b $mask");
579 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
581 $conn->quit($quitmsg);
583 &WARN("quit: could not quit!");
590 if (!defined $nick) {
591 &ERROR("nick: nick == NULL.");
595 if (defined $ident and $nick eq $ident) {
596 &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(localtime). "]");
609 if ($nick =~ /^$mask{nick}$/) {
610 rawout("NICK ".$nick);
612 if (defined $ident) {
613 &status("nick: Changing nick to $nick (from $ident)");
615 &DEBUG("first time nick change.");
621 &DEBUG("nick: failed... why oh why (nick => $nick)");
627 my($who, $chan) = @_;
628 rawout("INVITE $who $chan");
633 # Channel related functions...
636 # Usage: &joinNextChan();
638 if (scalar @joinchan) {
639 $chan = shift @joinchan;
642 if (my $i = scalar @joinchan) {
643 &status("joinNextChan: $i chans to join.");
650 my @c = &getJoinChans();
651 if (exists $cache{joinTime} and scalar @c) {
652 my $delta = time() - $cache{joinTime} - 5;
653 my $timestr = &Time2String($delta);
654 my $rate = sprintf("%.1f", $delta / @c);
655 delete $cache{joinTime};
657 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
660 # chanserv check: global channels, in case we missed one.
661 foreach ( &ChanConfList("chanServ_ops") ) {
666 # Usage: &getNickInChans($nick);
671 foreach (keys %channels) {
672 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
679 # Usage: &getNicksInChan($chan);
684 return keys %{ $channels{$chan}{''} };
688 my ($nick,$chan) = @_;
690 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
692 if (&validChan($chan) == 0) {
693 &ERROR("INIC: invalid channel $chan.");
697 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
700 foreach (keys %channels) {
701 next unless (/[A-Z]/);
702 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
708 sub IsNickInAnyChan {
711 foreach $chan (keys %channels) {
712 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
718 # Usage: &validChan($chan);
722 if (lc $chan ne $chan) {
723 &WARN("validChan: lc chan != chan. ($chan); fixing.");
724 $chan =~ tr/A-Z/a-z/;
727 if (exists $channels{$chan}) {
728 if ($chan eq "_default") {
729 # &WARN("validC: chan cannot be _default! returning 0!");
740 # Usage: &delUserInfo($nick,@chans);
742 my ($nick,@chans) = @_;
745 foreach $chan (@chans) {
746 foreach $mode (keys %{ $channels{$chan} }) {
748 next unless (exists $channels{$chan}{$mode}{$nick});
750 delete $channels{$chan}{$mode}{$nick};
758 delete $channels{$chan};
762 &DEBUG("clearIRCVars() called!");
766 @joinchan = &getJoinChans(1);
767 $cache{joinTime} = time();
775 foreach (keys %chanconf) {
776 next if ($_ eq "_default");
778 my $val = $chanconf{$_}{autojoin};
782 $skip++ if ($val eq "0");
797 $str = "channels not auto-joining: @skip (joining: @chans)";
799 $str = "auto-joining all chans: @chans";
802 &status("Chans: ".$str) if ($show);
808 # &DEBUG("closeDCC called.");
810 foreach $type (keys %dcc) {
811 next if ($type ne uc($type));
813 foreach $nick (keys %{ $dcc{$type} }) {
814 next unless (defined $nick);
815 &status("DCC CHAT: closing DCC $type to $nick.");
816 next unless (defined $dcc{$type}{$nick});
818 my $ref = $dcc{$type}{$nick};
819 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
820 $dcc{$type}{$nick}->close();
821 delete $dcc{$type}{$nick};
822 &DEBUG("after close for $nick");
829 my($who,$chan,$userhost) = @_;
831 return unless (&IsChanConf("joinfloodCheck"));
833 if (exists $netsplit{lc $who}) { # netsplit join.
834 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
837 if (exists $floodjoin{$chan}{$who}{Time}) {
838 &WARN("floodjoin{$chan}{$who} already exists?");
841 $floodjoin{$chan}{$who}{Time} = time();
842 $floodjoin{$chan}{$who}{Host} = $userhost;
845 foreach (keys %floodjoin) {
847 my $count = scalar keys %{ $floodjoin{$c} };
848 next unless ($count > 5);
849 &DEBUG("joinflood: count => $count");
852 foreach (keys %{ $floodjoin{$c} }) {
853 $time += $floodjoin{$c}{$_}{Time};
855 &DEBUG("joinflood: time => $time");
858 &DEBUG("joinflood: new time => $time");
864 foreach $chan (keys %floodjoin) {
865 foreach $who (keys %{ $floodjoin{$chan} }) {
866 my $t = $floodjoin{$chan}{$who}{Time};
867 next unless (defined $t);
869 my $delta = $time - $t;
870 next unless ($delta > 10);
872 delete $floodjoin{$chan}{$who};
877 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
883 if (exists $nuh{$n}) {
884 return &makeHostMask($nuh{$n});
886 $cache{on_who_Hack} = 1;