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) {
205 } elsif ($pubsize > $j) {
214 $pubsize = length $msg;
217 $conn->privmsg($talkchannel, $msg);
222 my ($nick, $msg) = @_;
223 if (!defined $nick) {
224 &ERROR("msg: nick == NULL.");
230 &WARN("msg: msg == $msg.");
234 &status(">$nick< $msg");
236 if (&whatInterface() =~ /IRC/) {
239 if ($t == $msgtime) {
241 $msgsize += length $msg;
243 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
244 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
245 if ( ($msgcount % $i) == 0 and $msgcount) {
248 } elsif ($msgsize > $j) {
257 $msgsize = length $msg;
260 $conn->privmsg($nick, $msg);
264 # Usage: &action(nick || chan, txt);
266 my ($target, $txt) = @_;
268 &WARN("action: txt == NULL.");
272 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
273 if (length $rawout > 510) {
274 &status("action: txt too long; truncating.");
276 chop($rawout) while (length($rawout) > 510);
280 &status("* $ident/$target $txt");
284 # Usage: ¬ice(nick || chan, txt);
286 my ($target, $txt) = @_;
288 &WARN("notice: txt == NULL.");
292 &status("-$target- $txt");
296 if ($t == $nottime) {
298 $notsize += length $txt;
300 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
301 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
303 if ( ($notcount % $i) == 0 and $notcount) {
306 } 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);
338 $reply =~ /([\.\?\s]+)$/;
340 &checkMsgType($reply);
342 if ($msgType eq 'public') {
343 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
344 $reply = "$orig{who}: ".$reply;
346 $reply = "$reply, ".$orig{who};
349 } elsif ($msgType eq 'private') {
353 $reply = "$reply, ".$orig{who};
356 } elsif ($msgType eq 'chat') {
357 if (!exists $dcc{'CHAT'}{$who}) {
358 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
361 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
363 &ERROR("PR: msgType invalid? ($msgType).");
368 sub performAddressedReply {
369 return unless ($addressed);
374 &performStrictReply(@_);
377 # Usage: &performStrictReply($reply);
378 sub performStrictReply {
381 &checkMsgType($reply);
383 if ($msgType eq 'private') {
385 } elsif ($msgType eq 'public') {
387 } elsif ($msgType eq 'chat') {
388 &dccsay(lc $who,$reply);
390 &ERROR("pSR: msgType invalid? ($msgType).");
395 my ($who, $reply) = @_;
396 if (!exists $dcc{'CHAT'}{$who}) {
397 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
401 &status("=>$who<= $reply"); # dcc chat.
402 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
409 foreach $type (keys %dcc) {
410 &FIXME("dcc_close: $who");
411 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
412 next unless (scalar @who);
419 my $chan = lc $chankey;
421 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
425 &status("joining $b_blue$chan$ob");
427 if (&validChan($chan)) {
428 &status("join: already on $chan");
430 if (!$conn->join($chan)) {
431 &DEBUG("joinchan: join failed. trying connect!");
441 next if ($chan eq "");
442 $chan =~ tr/A-Z/a-z/; # lowercase.
444 &status("parting $chan");
445 if (!&validChan($chan)) {
446 &WARN("part: not on $chan; doing anyway");
450 rawout("PART $chan");
451 # deletion of $channels{chan} is done in &entryEvt().
456 my ($chan, @modes) = @_;
457 my $modes = join(" ", @modes);
459 if (&validChan($chan) == 0) {
460 &ERROR("mode: invalid chan => '$chan'.");
464 &DEBUG("mode: MODE $chan $modes");
466 rawout("MODE $chan $modes");
470 my ($chan, @who) = @_;
471 my $os = "o" x scalar(@who);
473 &mode($chan, "+$os @who");
477 my ($chan, @who) = @_;
478 my $os = "o" x scalar(@who);
480 &mode($chan, "-$os ".@who);
484 my ($nick,$chan,$msg) = @_;
485 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
487 if ($chan ne "" and &validChan($chan) == 0) {
488 &ERROR("kick: invalid channel $chan.");
492 $nick =~ tr/A-Z/a-z/;
494 foreach $chan (@chans) {
495 if (!&IsNickInChan($nick,$chan)) {
496 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
500 if (!exists $channels{$chan}{o}{$ident}) {
501 &status("Kick: do not have ops on $chan :(");
505 &status("Kicking $nick from $chan.");
507 &rawout("KICK $chan $nick");
509 &rawout("KICK $chan $nick :$msg");
515 my ($mask,$chan) = @_;
516 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
519 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
520 &ERROR("ban: invalid channel $chan.");
524 foreach $chan (@chans) {
525 if (!exists $channels{$chan}{o}{$ident}) {
526 &status("Ban: do not have ops on $chan :(");
530 &status("Banning $mask from $chan.");
531 &rawout("MODE $chan +b $mask");
539 my ($mask,$chan) = @_;
540 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
543 &DEBUG("unban: mask = $mask, chan = @chans");
545 foreach $chan (@chans) {
546 if (!exists $channels{$chan}{o}{$ident}) {
547 &status("unBan: do not have ops on $chan :(");
551 &status("Removed ban $mask from $chan.");
552 &rawout("MODE $chan -b $mask");
561 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
563 $conn->quit($quitmsg);
565 &WARN("quit: could not quit!");
572 if (!defined $nick) {
573 &ERROR("nick: nick == NULL.");
577 if (defined $ident and $nick eq $ident) {
578 &WARN("nick: nick == ident == '$ident'.");
582 $bad++ if (exists $nuh{ $param{'ircNick'} });
583 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
586 &WARN("Nick: not going to try and get my nick back. [".
587 scalar(localtime). "]");
591 if ($nick =~ /^$mask{nick}$/) {
592 rawout("NICK ".$nick);
594 if (defined $ident) {
595 &status("nick: Changing nick to $nick (from $ident)");
597 &DEBUG("first time nick change.");
603 &DEBUG("nick: failed... why oh why (nick => $nick)");
609 my($who, $chan) = @_;
610 rawout("INVITE $who $chan");
615 # Channel related functions...
618 # Usage: &joinNextChan();
620 if (scalar @joinchan) {
621 $chan = shift @joinchan;
624 if (my $i = scalar @joinchan) {
625 &status("joinNextChan: $i chans to join.");
633 my @c = &getJoinChans();
634 if (exists $cache{joinTime} and scalar @c) {
635 my $delta = time() - $cache{joinTime} - 5;
636 my $timestr = &Time2String($delta);
637 my $rate = sprintf("%.1f", $delta / @c);
638 delete $cache{joinTime};
640 &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
643 # chanserv check: global channels, in case we missed one.
644 foreach ( &ChanConfList("chanServ_ops") ) {
649 # Usage: &getNickInChans($nick);
654 foreach (keys %channels) {
655 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
662 # Usage: &getNicksInChan($chan);
667 return keys %{ $channels{$chan}{''} };
671 my ($nick,$chan) = @_;
673 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
675 if (&validChan($chan) == 0) {
676 &ERROR("INIC: invalid channel $chan.");
680 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
683 foreach (keys %channels) {
684 next unless (/[A-Z]/);
685 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
691 sub IsNickInAnyChan {
694 foreach $chan (keys %channels) {
695 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
701 # Usage: &validChan($chan);
705 if (lc $chan ne $chan) {
706 &WARN("validChan: lc chan != chan. ($chan); fixing.");
707 $chan =~ tr/A-Z/a-z/;
710 if (exists $channels{$chan}) {
718 # Usage: &delUserInfo($nick,@chans);
720 my ($nick,@chans) = @_;
723 foreach $chan (@chans) {
724 foreach $mode (keys %{ $channels{$chan} }) {
726 next unless (exists $channels{$chan}{$mode}{$nick});
728 delete $channels{$chan}{$mode}{$nick};
736 delete $channels{$chan};
740 # &DEBUG("clearIRCVars() called!");
744 @joinchan = &getJoinChans(1);
745 $cache{joinTime} = time();
753 foreach (keys %chanconf) {
754 next if ($_ eq "_default");
756 my $val = $chanconf{$_}{autojoin};
760 $skip++ if ($val eq "0");
775 $str = "channels not auto-joining: @skip (joining: @chans)";
777 $str = "auto-joining all chans: @chans";
780 &status("Chans: ".$str) if ($show);
786 ### &DEBUG("closeDCC called.");
788 foreach $type (keys %dcc) {
789 next if ($type ne uc($type));
791 foreach $nick (keys %{ $dcc{$type} }) {
792 next unless (defined $nick);
793 &DEBUG("closing DCC $type to $nick (FIXME).");
794 next unless (defined $dcc{$type}{$nick});
796 my $ref = $dcc{$type}{$nick};
797 &DEBUG("ref => $ref");
799 # $dcc{$type}{$nick}->close();
805 my($who,$chan,$userhost) = @_;
807 return unless (&IsChanConf("joinfloodCheck"));
809 if (exists $netsplit{lc $who}) { # netsplit join.
810 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
813 if (exists $floodjoin{$chan}{$who}{Time}) {
814 &WARN("floodjoin{$chan}{$who} already exists?");
817 $floodjoin{$chan}{$who}{Time} = time();
818 $floodjoin{$chan}{$who}{Host} = $userhost;
821 foreach (keys %floodjoin) {
823 my $count = scalar keys %{ $floodjoin{$c} };
824 next unless ($count > 5);
825 &DEBUG("joinflood: count => $count");
828 foreach (keys %{ $floodjoin{$c} }) {
829 $time += $floodjoin{$c}{$_}{Time};
831 &DEBUG("joinflood: time => $time");
834 &DEBUG("joinflood: new time => $time");
839 foreach $chan (keys %floodjoin) {
840 foreach $who (keys %{ $floodjoin{$chan} }) {
841 my $time = time() - $floodjoin{$chan}{$who}{Time};
842 next unless ($time > 10);
843 delete $floodjoin{$chan}{$who};
848 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
854 &FIXME("getHostMask($n) called...");