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('whoischannels', \&on_whoischannels);
139 $conn->add_handler('useronchannel', \&on_useronchannel);
140 $conn->add_handler('whois', \&on_whois);
141 $conn->add_handler('other', \&on_other);
142 $conn->add_global_handler('disconnect', \&on_disconnect);
143 $conn->add_global_handler([251,252,253,254,255], \&on_init);
144 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
145 $conn->add_global_handler(315, \&on_endofwho);
146 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
147 $conn->add_global_handler(324, \&on_modeis);
148 $conn->add_global_handler(333, \&on_topicinfo);
149 $conn->add_global_handler(352, \&on_who);
150 $conn->add_global_handler(353, \&on_names);
151 $conn->add_global_handler(366, \&on_endofnames);
152 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
153 $conn->add_global_handler(433, \&on_nick_taken);
154 $conn->add_global_handler(439, \&on_targettoofast);
155 # for proper joinnextChan behaviour
156 $conn->add_global_handler(471, \&on_chanfull);
157 $conn->add_global_handler(473, \&on_inviteonly);
158 $conn->add_global_handler(474, \&on_banned);
159 $conn->add_global_handler(475, \&on_badchankey);
161 # end of handler stuff.
166 ######################################################################
167 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
168 ######################################################################
174 # slow down a bit if traffic is "high".
175 # need to take into account time of last message sent.
176 if ($last{buflen} > 256 and length($buf) > 256) {
180 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
182 $last{buflen} = length($buf);
189 &WARN("say: msg == $msg.");
193 &status("</$talkchannel> $msg");
194 if (&whatInterface() =~ /IRC/) {
195 $msg = "zero" if ($msg =~ /^0+$/);
198 if ($t == $pubtime) {
200 $pubsize += length $msg;
202 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
203 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
205 if ( ($pubcount % $i) == 0 and $pubcount) {
207 } elsif ($pubsize > $j) {
215 $pubsize = length $msg;
218 $conn->privmsg($talkchannel, $msg);
223 my ($nick, $msg) = @_;
224 if (!defined $nick) {
225 &ERROR("msg: nick == NULL.");
231 &WARN("msg: msg == $msg.");
235 if ($msgType =~ /chat/i) {
236 # todo: warn that we're using msg() to do DCC CHAT?
237 &dccsay($nick, $msg);
238 # todo: make dccsay deal with flood protection?
242 &status(">$nick< $msg");
244 if (&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);
270 # Usage: &action(nick || chan, txt);
272 my ($target, $txt) = @_;
274 &WARN("action: txt == NULL.");
278 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
279 if (length $rawout > 510) {
280 &status("action: txt too long; truncating.");
282 chop($rawout) while (length($rawout) > 510);
286 &status("* $ident/$target $txt");
290 # Usage: ¬ice(nick || chan, txt);
292 my ($target, $txt) = @_;
294 &WARN("notice: txt == NULL.");
298 &status("-$target- $txt");
302 if ($t == $nottime) {
304 $notsize += length $txt;
306 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
307 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
309 if ( ($notcount % $i) == 0 and $notcount) {
311 } elsif ($notsize > $j) {
319 $notsize = length $txt;
322 $conn->notice($target, $txt);
326 my ($txt,$flag) = @_;
328 ### FIXME: flag not supported yet.
330 foreach (keys %{ $dcc{'CHAT'} }) {
331 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
336 ### perform commands.
339 # Usage: &performReply($reply);
342 $reply =~ /([\.\?\s]+)$/;
344 &checkMsgType($reply);
346 if ($msgType eq 'public') {
347 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
348 $reply = "$orig{who}: ".$reply;
350 $reply = "$reply, ".$orig{who};
353 } elsif ($msgType eq 'private') {
357 $reply = "$reply, ".$orig{who};
360 } elsif ($msgType eq 'chat') {
361 if (!exists $dcc{'CHAT'}{$who}) {
362 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
365 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
367 &ERROR("PR: msgType invalid? ($msgType).");
372 sub performAddressedReply {
373 return unless ($addressed);
378 &performStrictReply(@_);
381 # Usage: &performStrictReply($reply);
382 sub performStrictReply {
385 &checkMsgType($reply);
387 if ($msgType eq 'private') {
389 } elsif ($msgType eq 'public') {
391 } elsif ($msgType eq 'chat') {
392 &dccsay(lc $who, $reply);
394 &ERROR("pSR: msgType invalid? ($msgType).");
399 my($who, $reply) = @_;
401 if (!defined $reply or $reply =~ /^\s*$/) {
402 &WARN("dccsay: reply == NULL.");
406 if (!exists $dcc{'CHAT'}{$who}) {
407 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
411 &status("=>$who<= $reply"); # dcc chat.
412 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
419 foreach $type (keys %dcc) {
420 &FIXME("dcc_close: $who");
421 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
422 next unless (scalar @who);
424 &DEBUG("dcc_close... close $who!");
430 my $chan = lc $chankey;
432 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
436 # hopefully validChan is right.
437 if (&validChan($chan)) {
438 &status("join: already on $chan");
440 &status("joining $b_blue$chan$ob");
441 if (!$conn->join($chan)) {
442 &DEBUG("joinchan: join failed. trying connect!");
452 next if ($chan eq "");
453 $chan =~ tr/A-Z/a-z/; # lowercase.
455 if ($chan !~ /^$mask{chan}$/) {
456 &WARN("part: chan is invalid ($chan)");
460 &status("parting $chan");
461 if (!&validChan($chan)) {
462 &WARN("part: not on $chan; doing anyway");
466 rawout("PART $chan");
467 # deletion of $channels{chan} is done in &entryEvt().
472 my ($chan, @modes) = @_;
473 my $modes = join(" ", @modes);
475 if (&validChan($chan) == 0) {
476 &ERROR("mode: invalid chan => '$chan'.");
480 &DEBUG("mode: MODE $chan $modes");
482 rawout("MODE $chan $modes");
486 my ($chan, @who) = @_;
487 my $os = "o" x scalar(@who);
489 &mode($chan, "+$os @who");
493 my ($chan, @who) = @_;
494 my $os = "o" x scalar(@who);
496 &mode($chan, "-$os ".@who);
500 my ($nick,$chan,$msg) = @_;
501 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
503 if ($chan ne "" and &validChan($chan) == 0) {
504 &ERROR("kick: invalid channel $chan.");
508 $nick =~ tr/A-Z/a-z/;
510 foreach $chan (@chans) {
511 if (!&IsNickInChan($nick,$chan)) {
512 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
516 if (!exists $channels{$chan}{o}{$ident}) {
517 &status("Kick: do not have ops on $chan :(");
521 &status("Kicking $nick from $chan.");
523 &rawout("KICK $chan $nick");
525 &rawout("KICK $chan $nick :$msg");
531 my ($mask,$chan) = @_;
532 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
535 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
536 &ERROR("ban: invalid channel $chan.");
540 foreach $chan (@chans) {
541 if (!exists $channels{$chan}{o}{$ident}) {
542 &status("Ban: do not have ops on $chan :(");
546 &status("Banning $mask from $chan.");
547 &rawout("MODE $chan +b $mask");
555 my ($mask,$chan) = @_;
556 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
559 &DEBUG("unban: mask = $mask, chan = @chans");
561 foreach $chan (@chans) {
562 if (!exists $channels{$chan}{o}{$ident}) {
563 &status("unBan: do not have ops on $chan :(");
567 &status("Removed ban $mask from $chan.");
568 &rawout("MODE $chan -b $mask");
577 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
579 $conn->quit($quitmsg);
581 &WARN("quit: could not quit!");
588 if (!defined $nick) {
589 &ERROR("nick: nick == NULL.");
593 if (defined $ident and $nick eq $ident) {
594 &WARN("nick: nick == ident == '$ident'.");
598 $bad++ if (exists $nuh{ $param{'ircNick'} });
599 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
602 &WARN("Nick: not going to try and get my nick back. [".
603 scalar(localtime). "]");
607 if ($nick =~ /^$mask{nick}$/) {
608 rawout("NICK ".$nick);
610 if (defined $ident) {
611 &status("nick: Changing nick to $nick (from $ident)");
613 &DEBUG("first time nick change.");
619 &DEBUG("nick: failed... why oh why (nick => $nick)");
625 my($who, $chan) = @_;
626 rawout("INVITE $who $chan");
631 # Channel related functions...
634 # Usage: &joinNextChan();
636 if (scalar @joinchan) {
637 $chan = shift @joinchan;
640 if (my $i = scalar @joinchan) {
641 &status("joinNextChan: $i chans to join.");
648 my @c = &getJoinChans();
649 if (exists $cache{joinTime} and scalar @c) {
650 my $delta = time() - $cache{joinTime} - 5;
651 my $timestr = &Time2String($delta);
652 my $rate = sprintf("%.1f", $delta / @c);
653 delete $cache{joinTime};
655 &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
658 # chanserv check: global channels, in case we missed one.
659 foreach ( &ChanConfList("chanServ_ops") ) {
664 # Usage: &getNickInChans($nick);
669 foreach (keys %channels) {
670 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
677 # Usage: &getNicksInChan($chan);
682 return keys %{ $channels{$chan}{''} };
686 my ($nick,$chan) = @_;
688 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
690 if (&validChan($chan) == 0) {
691 &ERROR("INIC: invalid channel $chan.");
695 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
698 foreach (keys %channels) {
699 next unless (/[A-Z]/);
700 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
706 sub IsNickInAnyChan {
709 foreach $chan (keys %channels) {
710 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
716 # Usage: &validChan($chan);
720 if (lc $chan ne $chan) {
721 &WARN("validChan: lc chan != chan. ($chan); fixing.");
722 $chan =~ tr/A-Z/a-z/;
725 if (exists $channels{$chan}) {
733 # Usage: &delUserInfo($nick,@chans);
735 my ($nick,@chans) = @_;
738 foreach $chan (@chans) {
739 foreach $mode (keys %{ $channels{$chan} }) {
741 next unless (exists $channels{$chan}{$mode}{$nick});
743 delete $channels{$chan}{$mode}{$nick};
751 delete $channels{$chan};
755 # &DEBUG("clearIRCVars() called!");
759 @joinchan = &getJoinChans(1);
760 $cache{joinTime} = time();
768 foreach (keys %chanconf) {
769 next if ($_ eq "_default");
771 my $val = $chanconf{$_}{autojoin};
775 $skip++ if ($val eq "0");
790 $str = "channels not auto-joining: @skip (joining: @chans)";
792 $str = "auto-joining all chans: @chans";
795 &status("Chans: ".$str) if ($show);
801 # &DEBUG("closeDCC called.");
803 foreach $type (keys %dcc) {
804 next if ($type ne uc($type));
806 foreach $nick (keys %{ $dcc{$type} }) {
807 next unless (defined $nick);
808 &status("DCC CHAT: closing DCC $type to $nick.");
809 next unless (defined $dcc{$type}{$nick});
811 my $ref = $dcc{$type}{$nick};
812 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
813 $dcc{$type}{$nick}->close();
814 delete $dcc{$type}{$nick};
815 &DEBUG("after close for $nick");
822 my($who,$chan,$userhost) = @_;
824 return unless (&IsChanConf("joinfloodCheck"));
826 if (exists $netsplit{lc $who}) { # netsplit join.
827 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
830 if (exists $floodjoin{$chan}{$who}{Time}) {
831 &WARN("floodjoin{$chan}{$who} already exists?");
834 $floodjoin{$chan}{$who}{Time} = time();
835 $floodjoin{$chan}{$who}{Host} = $userhost;
838 foreach (keys %floodjoin) {
840 my $count = scalar keys %{ $floodjoin{$c} };
841 next unless ($count > 5);
842 &DEBUG("joinflood: count => $count");
845 foreach (keys %{ $floodjoin{$c} }) {
846 $time += $floodjoin{$c}{$_}{Time};
848 &DEBUG("joinflood: time => $time");
851 &DEBUG("joinflood: new time => $time");
857 foreach $chan (keys %floodjoin) {
858 foreach $who (keys %{ $floodjoin{$chan} }) {
859 my $t = $floodjoin{$chan}{$who}{Time};
860 next unless (defined $t);
862 my $delta = $time - $t;
863 next unless ($delta > 10);
865 delete $floodjoin{$chan}{$who};
870 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
876 if (exists $nuh{$n}) {
877 return &makeHostMask($nuh{$n});
879 $cache{on_who_Hack} = 1;