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'});
101 $conn = $irc->newconn(%args);
103 if (!defined $conn) {
104 &ERROR("irc: conn was not created!defined!!!");
110 # change internal timeout value for scheduler.
111 $irc->{_timeout} = 10; # how about 60?
112 # Net::IRC debugging.
115 $ircstats{'Server'} = "$server:$port";
118 $conn->add_handler('caction', \&on_action);
119 $conn->add_handler('cdcc', \&on_dcc);
120 $conn->add_handler('cping', \&on_ping);
121 $conn->add_handler('crping', \&on_ping_reply);
122 $conn->add_handler('cversion', \&on_version);
123 $conn->add_handler('crversion', \&on_crversion);
124 $conn->add_handler('dcc_open', \&on_dcc_open);
125 $conn->add_handler('dcc_close', \&on_dcc_close);
126 $conn->add_handler('chat', \&on_chat);
127 $conn->add_handler('msg', \&on_msg);
128 $conn->add_handler('public', \&on_public);
129 $conn->add_handler('join', \&on_join);
130 $conn->add_handler('part', \&on_part);
131 $conn->add_handler('topic', \&on_topic);
132 $conn->add_handler('invite', \&on_invite);
133 $conn->add_handler('kick', \&on_kick);
134 $conn->add_handler('mode', \&on_mode);
135 $conn->add_handler('nick', \&on_nick);
136 $conn->add_handler('quit', \&on_quit);
137 $conn->add_handler('notice', \&on_notice);
138 $conn->add_handler('whoisuser', \&on_whoisuser);
139 $conn->add_handler('other', \&on_other);
140 $conn->add_global_handler('disconnect', \&on_disconnect);
141 $conn->add_global_handler([251,252,253,254,255], \&on_init);
142 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
143 $conn->add_global_handler(315, \&on_endofwho);
144 $conn->add_global_handler(324, \&on_modeis);
145 $conn->add_global_handler(333, \&on_topicinfo);
146 $conn->add_global_handler(352, \&on_who);
147 $conn->add_global_handler(353, \&on_names);
148 $conn->add_global_handler(366, \&on_endofnames);
149 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
150 $conn->add_global_handler(433, \&on_nick_taken);
151 $conn->add_global_handler(439, \&on_targettoofast);
152 # for proper joinnextChan behaviour
153 $conn->add_global_handler(471, \&on_chanfull);
154 $conn->add_global_handler(473, \&on_inviteonly);
155 $conn->add_global_handler(474, \&on_banned);
156 $conn->add_global_handler(475, \&on_badchankey);
158 # end of handler stuff.
163 ######################################################################
164 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
165 ######################################################################
171 # slow down a bit if traffic is "high".
172 # need to take into account time of last message sent.
173 if ($last{buflen} > 256 and length($buf) > 256) {
177 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
179 $last{buflen} = length($buf);
186 &WARN("say: msg == $msg.");
190 &status("</$talkchannel> $msg");
191 if (&whatInterface() =~ /IRC/) {
192 $msg = "zero" if ($msg =~ /^0+$/);
195 if ($t == $pubtime) {
197 $pubsize += length $msg;
199 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
200 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
202 if ( ($pubcount % $i) == 0 and $pubcount) {
204 } elsif ($pubsize > $j) {
212 $pubsize = length $msg;
215 $conn->privmsg($talkchannel, $msg);
220 my ($nick, $msg) = @_;
221 if (!defined $nick) {
222 &ERROR("msg: nick == NULL.");
228 &WARN("msg: msg == $msg.");
232 &status(">$nick< $msg");
234 if (&whatInterface() =~ /IRC/) {
237 if ($t == $msgtime) {
239 $msgsize += length $msg;
241 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
242 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
243 if ( ($msgcount % $i) == 0 and $msgcount) {
245 } elsif ($msgsize > $j) {
253 $msgsize = length $msg;
256 $conn->privmsg($nick, $msg);
260 # Usage: &action(nick || chan, txt);
262 my ($target, $txt) = @_;
264 &WARN("action: txt == NULL.");
268 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
269 if (length $rawout > 510) {
270 &status("action: txt too long; truncating.");
272 chop($rawout) while (length($rawout) > 510);
276 &status("* $ident/$target $txt");
280 # Usage: ¬ice(nick || chan, txt);
282 my ($target, $txt) = @_;
284 &WARN("notice: txt == NULL.");
288 &status("-$target- $txt");
292 if ($t == $nottime) {
294 $notsize += length $txt;
296 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
297 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
299 if ( ($notcount % $i) == 0 and $notcount) {
301 } elsif ($notsize > $j) {
309 $notsize = length $txt;
312 $conn->notice($target, $txt);
316 my ($txt,$flag) = @_;
318 ### FIXME: flag not supported yet.
320 foreach (keys %{ $dcc{'CHAT'} }) {
321 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
326 ### perform commands.
329 # Usage: &performReply($reply);
332 $reply =~ /([\.\?\s]+)$/;
334 &checkMsgType($reply);
336 if ($msgType eq 'public') {
337 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
338 $reply = "$orig{who}: ".$reply;
340 $reply = "$reply, ".$orig{who};
343 } elsif ($msgType eq 'private') {
347 $reply = "$reply, ".$orig{who};
350 } elsif ($msgType eq 'chat') {
351 if (!exists $dcc{'CHAT'}{$who}) {
352 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
355 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
357 &ERROR("PR: msgType invalid? ($msgType).");
362 sub performAddressedReply {
363 return unless ($addressed);
368 &performStrictReply(@_);
371 # Usage: &performStrictReply($reply);
372 sub performStrictReply {
375 &checkMsgType($reply);
377 if ($msgType eq 'private') {
379 } elsif ($msgType eq 'public') {
381 } elsif ($msgType eq 'chat') {
382 &dccsay(lc $who,$reply);
384 &ERROR("pSR: msgType invalid? ($msgType).");
389 my ($who, $reply) = @_;
390 if (!exists $dcc{'CHAT'}{$who}) {
391 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
395 &status("=>$who<= $reply"); # dcc chat.
396 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
403 foreach $type (keys %dcc) {
404 &FIXME("dcc_close: $who");
405 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
406 next unless (scalar @who);
413 my $chan = lc $chankey;
415 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
419 &status("joining $b_blue$chan$ob");
421 if (&validChan($chan)) {
422 &status("join: already on $chan");
424 if (!$conn->join($chan)) {
425 &DEBUG("joinchan: join failed. trying connect!");
435 next if ($chan eq "");
436 $chan =~ tr/A-Z/a-z/; # lowercase.
438 &status("parting $chan");
439 if (!&validChan($chan)) {
440 &WARN("part: not on $chan; doing anyway");
444 rawout("PART $chan");
445 # deletion of $channels{chan} is done in &entryEvt().
450 my ($chan, @modes) = @_;
451 my $modes = join(" ", @modes);
453 if (&validChan($chan) == 0) {
454 &ERROR("mode: invalid chan => '$chan'.");
458 &DEBUG("mode: MODE $chan $modes");
460 rawout("MODE $chan $modes");
464 my ($chan, @who) = @_;
465 my $os = "o" x scalar(@who);
467 &mode($chan, "+$os @who");
471 my ($chan, @who) = @_;
472 my $os = "o" x scalar(@who);
474 &mode($chan, "-$os ".@who);
478 my ($nick,$chan,$msg) = @_;
479 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
481 if ($chan ne "" and &validChan($chan) == 0) {
482 &ERROR("kick: invalid channel $chan.");
486 $nick =~ tr/A-Z/a-z/;
488 foreach $chan (@chans) {
489 if (!&IsNickInChan($nick,$chan)) {
490 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
494 if (!exists $channels{$chan}{o}{$ident}) {
495 &status("Kick: do not have ops on $chan :(");
499 &status("Kicking $nick from $chan.");
501 &rawout("KICK $chan $nick");
503 &rawout("KICK $chan $nick :$msg");
509 my ($mask,$chan) = @_;
510 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
513 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
514 &ERROR("ban: invalid channel $chan.");
518 foreach $chan (@chans) {
519 if (!exists $channels{$chan}{o}{$ident}) {
520 &status("Ban: do not have ops on $chan :(");
524 &status("Banning $mask from $chan.");
525 &rawout("MODE $chan +b $mask");
533 my ($mask,$chan) = @_;
534 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
537 &DEBUG("unban: mask = $mask, chan = @chans");
539 foreach $chan (@chans) {
540 if (!exists $channels{$chan}{o}{$ident}) {
541 &status("unBan: do not have ops on $chan :(");
545 &status("Removed ban $mask from $chan.");
546 &rawout("MODE $chan -b $mask");
555 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
557 $conn->quit($quitmsg);
559 &WARN("quit: could not quit!");
566 if (!defined $nick) {
567 &ERROR("nick: nick == NULL.");
571 if (defined $ident and $nick eq $ident) {
572 &WARN("nick: nick == ident == '$ident'.");
576 $bad++ if (exists $nuh{ $param{'ircNick'} });
577 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
580 &WARN("Nick: not going to try and get my nick back. [".
581 scalar(localtime). "]");
585 if ($nick =~ /^$mask{nick}$/) {
586 rawout("NICK ".$nick);
588 if (defined $ident) {
589 &status("nick: Changing nick to $nick (from $ident)");
591 &DEBUG("first time nick change.");
597 &DEBUG("nick: failed... why oh why (nick => $nick)");
603 my($who, $chan) = @_;
604 rawout("INVITE $who $chan");
609 # Channel related functions...
612 # Usage: &joinNextChan();
614 if (scalar @joinchan) {
615 $chan = shift @joinchan;
618 if (my $i = scalar @joinchan) {
619 &status("joinNextChan: $i chans to join.");
626 my @c = &getJoinChans();
627 if (exists $cache{joinTime} and scalar @c) {
628 my $delta = time() - $cache{joinTime} - 5;
629 my $timestr = &Time2String($delta);
630 my $rate = sprintf("%.1f", $delta / @c);
631 delete $cache{joinTime};
633 &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
636 # chanserv check: global channels, in case we missed one.
637 foreach ( &ChanConfList("chanServ_ops") ) {
642 # Usage: &getNickInChans($nick);
647 foreach (keys %channels) {
648 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
655 # Usage: &getNicksInChan($chan);
660 return keys %{ $channels{$chan}{''} };
664 my ($nick,$chan) = @_;
666 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
668 if (&validChan($chan) == 0) {
669 &ERROR("INIC: invalid channel $chan.");
673 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
676 foreach (keys %channels) {
677 next unless (/[A-Z]/);
678 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
684 sub IsNickInAnyChan {
687 foreach $chan (keys %channels) {
688 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
694 # Usage: &validChan($chan);
698 if (lc $chan ne $chan) {
699 &WARN("validChan: lc chan != chan. ($chan); fixing.");
700 $chan =~ tr/A-Z/a-z/;
703 if (exists $channels{$chan}) {
711 # Usage: &delUserInfo($nick,@chans);
713 my ($nick,@chans) = @_;
716 foreach $chan (@chans) {
717 foreach $mode (keys %{ $channels{$chan} }) {
719 next unless (exists $channels{$chan}{$mode}{$nick});
721 delete $channels{$chan}{$mode}{$nick};
729 delete $channels{$chan};
733 # &DEBUG("clearIRCVars() called!");
737 @joinchan = &getJoinChans(1);
738 $cache{joinTime} = time();
746 foreach (keys %chanconf) {
747 next if ($_ eq "_default");
749 my $val = $chanconf{$_}{autojoin};
753 $skip++ if ($val eq "0");
768 $str = "channels not auto-joining: @skip (joining: @chans)";
770 $str = "auto-joining all chans: @chans";
773 &status("Chans: ".$str) if ($show);
779 ### &DEBUG("closeDCC called.");
781 foreach $type (keys %dcc) {
782 next if ($type ne uc($type));
784 foreach $nick (keys %{ $dcc{$type} }) {
785 next unless (defined $nick);
786 &DEBUG("closing DCC $type to $nick (FIXME).");
787 next unless (defined $dcc{$type}{$nick});
789 my $ref = $dcc{$type}{$nick};
790 &DEBUG("ref => $ref");
792 # $dcc{$type}{$nick}->close();
798 my($who,$chan,$userhost) = @_;
800 return unless (&IsChanConf("joinfloodCheck"));
802 if (exists $netsplit{lc $who}) { # netsplit join.
803 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
806 if (exists $floodjoin{$chan}{$who}{Time}) {
807 &WARN("floodjoin{$chan}{$who} already exists?");
810 $floodjoin{$chan}{$who}{Time} = time();
811 $floodjoin{$chan}{$who}{Host} = $userhost;
814 foreach (keys %floodjoin) {
816 my $count = scalar keys %{ $floodjoin{$c} };
817 next unless ($count > 5);
818 &DEBUG("joinflood: count => $count");
821 foreach (keys %{ $floodjoin{$c} }) {
822 $time += $floodjoin{$c}{$_}{Time};
824 &DEBUG("joinflood: time => $time");
827 &DEBUG("joinflood: new time => $time");
832 foreach $chan (keys %floodjoin) {
833 foreach $who (keys %{ $floodjoin{$chan} }) {
834 my $time = time() - $floodjoin{$chan}{$who}{Time};
835 next unless ($time > 10);
836 delete $floodjoin{$chan}{$who};
841 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
847 &FIXME("getHostMask($n) called...");