2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
10 use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
11 %chanconf %orig %ircPort %ircstats %last %netsplit);
12 use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
14 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
15 $pubcount $pubtime $pubsize);
16 use vars qw($b_blue $ob);
17 use vars qw(@joinchan @ircServers);
19 # static scalar variables.
20 $mask{ip} = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
21 $mask{host} = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
22 $mask{chan} = '[\#\&]\S*|_default';
23 my $isnick1 = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
24 my $isnick2 = '0-9\-';
25 $mask{nick} = "[$isnick1]{1}[$isnick1$isnick2]*";
26 $mask{nuh} = '\S*!\S*\@\S*';
35 while (my $host = shift @ircServers) {
36 # JUST IN CASE. irq was complaining about this.
37 if ($lastrun == time()) {
38 &DEBUG("ircloop: hrm... lastrun == time()");
45 &DEBUG("ircloop: ircServers[x] = NULL.");
49 next unless (exists $ircPort{$host});
51 my $retval = &irc($host, $ircPort{$host});
52 next unless (defined $retval and $retval == 0);
55 if ($error % 3 == 0 and $error != 0) {
56 &status("IRC: Could not connect.");
62 &status("IRC: cannot connect to any IRC servers; stopping.");
68 &status("IRC: ok, done one cycle of IRC servers; trying again.");
75 my ($server,$port) = @_;
77 my $iaddr = inet_aton($server);
78 my $paddr = sockaddr_in($port, $iaddr);
79 my $proto = getprotobyname('tcp');
82 &status("Connecting to port $port of server $server ...");
85 if ($server =~ /\D$/) {
86 my $packed = scalar(gethostbyname($server));
88 if (!defined $packed) {
89 &status(" cannot resolve $server.");
93 my $resolve = inet_ntoa($packed);
94 &status(" resolved to $resolve.");
95 ### warning in Sys/Hostname line 78???
96 ### caused inside Net::IRC?
102 Nick => $param{'ircNick'},
105 Ircname => $param{'ircName'},
107 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
108 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
110 $conn = $irc->newconn(%args);
112 if (!defined $conn) {
113 &ERROR("internal: perl IRC connection object does not exist.");
119 # change internal timeout value for scheduler.
120 $irc->{_timeout} = 10; # how about 60?
121 # Net::IRC debugging.
124 $ircstats{'Server'} = "$server:$port";
127 $conn->add_handler('caction', \&on_action);
128 $conn->add_handler('cdcc', \&on_dcc);
129 $conn->add_handler('cping', \&on_ping);
130 $conn->add_handler('crping', \&on_ping_reply);
131 $conn->add_handler('cversion', \&on_version);
132 $conn->add_handler('crversion', \&on_crversion);
133 $conn->add_handler('dcc_open', \&on_dcc_open);
134 $conn->add_handler('dcc_close', \&on_dcc_close);
135 $conn->add_handler('chat', \&on_chat);
136 $conn->add_handler('msg', \&on_msg);
137 $conn->add_handler('public', \&on_public);
138 $conn->add_handler('join', \&on_join);
139 $conn->add_handler('part', \&on_part);
140 $conn->add_handler('topic', \&on_topic);
141 $conn->add_handler('invite', \&on_invite);
142 $conn->add_handler('kick', \&on_kick);
143 $conn->add_handler('mode', \&on_mode);
144 $conn->add_handler('nick', \&on_nick);
145 $conn->add_handler('quit', \&on_quit);
146 $conn->add_handler('notice', \&on_notice);
147 $conn->add_handler('whoischannels', \&on_whoischannels);
148 $conn->add_handler('useronchannel', \&on_useronchannel);
149 $conn->add_handler('whois', \&on_whois);
150 $conn->add_handler('other', \&on_other);
151 $conn->add_global_handler('disconnect', \&on_disconnect);
152 $conn->add_global_handler([251,252,253,254,255], \&on_init);
153 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
154 $conn->add_global_handler(303, \&on_ison); # notify.
155 $conn->add_global_handler(315, \&on_endofwho);
156 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
157 $conn->add_global_handler(324, \&on_modeis);
158 $conn->add_global_handler(333, \&on_topicinfo);
159 $conn->add_global_handler(352, \&on_who);
160 $conn->add_global_handler(353, \&on_names);
161 $conn->add_global_handler(366, \&on_endofnames);
162 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
163 $conn->add_global_handler(433, \&on_nick_taken);
164 $conn->add_global_handler(439, \&on_targettoofast);
165 # for proper joinnextChan behaviour
166 $conn->add_global_handler(471, \&on_chanfull);
167 $conn->add_global_handler(473, \&on_inviteonly);
168 $conn->add_global_handler(474, \&on_banned);
169 $conn->add_global_handler(475, \&on_badchankey);
170 $conn->add_global_handler(443, \&on_useronchan);
172 # end of handler stuff.
177 ######################################################################
178 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
179 ######################################################################
185 # slow down a bit if traffic is "high".
186 # need to take into account time of last message sent.
187 if ($last{buflen} > 256 and length($buf) > 256) {
191 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
193 $last{buflen} = length($buf);
200 &WARN("say: msg == $msg.");
204 &status("</$talkchannel> $msg");
205 if (&whatInterface() =~ /IRC/) {
206 $msg = "zero" if ($msg =~ /^0+$/);
209 if ($t == $pubtime) {
211 $pubsize += length $msg;
213 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
214 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
216 if ( ($pubcount % $i) == 0 and $pubcount) {
218 } elsif ($pubsize > $j) {
226 $pubsize = length $msg;
229 $conn->privmsg($talkchannel, $msg);
234 my ($nick, $msg) = @_;
235 if (!defined $nick) {
236 &ERROR("msg: nick == NULL.");
242 &WARN("msg: msg == $msg.");
246 if ($msgType =~ /chat/i) {
247 # todo: warn that we're using msg() to do DCC CHAT?
248 &dccsay($nick, $msg);
249 # todo: make dccsay deal with flood protection?
253 &status(">$nick< $msg");
255 if (&whatInterface() =~ /IRC/) {
258 if ($t == $msgtime) {
260 $msgsize += length $msg;
262 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
263 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
264 if ( ($msgcount % $i) == 0 and $msgcount) {
266 } elsif ($msgsize > $j) {
274 $msgsize = length $msg;
277 $conn->privmsg($nick, $msg);
281 # Usage: &action(nick || chan, txt);
283 my ($target, $txt) = @_;
285 &WARN("action: txt == NULL.");
289 if (length $txt > 480) {
290 &status("action: txt too long; truncating.");
291 chop($txt) while (length $txt > 480);
294 &status("* $ident/$target $txt");
295 $conn->me($target, $txt);
298 # Usage: ¬ice(nick || chan, txt);
300 my ($target, $txt) = @_;
302 &WARN("notice: txt == NULL.");
306 &status("-$target- $txt");
310 if ($t == $nottime) {
312 $notsize += length $txt;
314 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
315 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
317 if ( ($notcount % $i) == 0 and $notcount) {
319 } elsif ($notsize > $j) {
327 $notsize = length $txt;
330 $conn->notice($target, $txt);
334 my ($txt,$flag) = @_;
336 ### FIXME: flag not supported yet.
338 foreach (keys %{ $dcc{'CHAT'} }) {
339 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
344 ### perform commands.
347 # Usage: &performReply($reply);
350 $reply =~ /([\.\?\s]+)$/;
352 &checkMsgType($reply);
354 if ($msgType eq 'public') {
355 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
356 $reply = "$orig{who}: ".$reply;
358 $reply = "$reply, ".$orig{who};
361 } elsif ($msgType eq 'private') {
365 $reply = "$reply, ".$orig{who};
368 } elsif ($msgType eq 'chat') {
369 if (!exists $dcc{'CHAT'}{$who}) {
370 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
373 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
375 &ERROR("PR: msgType invalid? ($msgType).");
380 sub performAddressedReply {
381 return unless ($addressed);
386 &performStrictReply(@_);
389 # Usage: &performStrictReply($reply);
390 sub performStrictReply {
393 &checkMsgType($reply);
395 if ($msgType eq 'private') {
397 } elsif ($msgType eq 'public') {
399 } elsif ($msgType eq 'chat') {
400 &dccsay(lc $who, $reply);
402 &ERROR("pSR: msgType invalid? ($msgType).");
407 my($who, $reply) = @_;
409 if (!defined $reply or $reply =~ /^\s*$/) {
410 &WARN("dccsay: reply == NULL.");
414 if (!exists $dcc{'CHAT'}{$who}) {
415 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
419 &status("=>$who<= $reply"); # dcc chat.
420 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
427 foreach $type (keys %dcc) {
428 &FIXME("dcc_close: $who");
429 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
430 next unless (scalar @who);
432 &DEBUG("dcc_close... close $who!");
438 my $key = &getChanConf("chankey", $chan) || "";
440 # forgot for about 2 years to implement channel keys when moving
441 # over to Net::IRC...
443 # hopefully validChan is right.
444 if (&validChan($chan)) {
445 &status("join: already on $chan");
447 &status("joining $b_blue$chan$ob");
449 return if ($conn->join($chan, $key));
451 &DEBUG("joinchan: join failed. trying connect!");
461 next if ($chan eq "");
462 $chan =~ tr/A-Z/a-z/; # lowercase.
464 if ($chan !~ /^$mask{chan}$/) {
465 &WARN("part: chan is invalid ($chan)");
469 &status("parting $chan");
470 if (!&validChan($chan)) {
471 &WARN("part: not on $chan; doing anyway");
476 # deletion of $channels{chan} is done in &entryEvt().
481 my ($chan, @modes) = @_;
482 my $modes = join(" ", @modes);
484 if (&validChan($chan) == 0) {
485 &ERROR("mode: invalid chan => '$chan'.");
489 &DEBUG("mode: MODE $chan $modes");
491 # should move to use Net::IRC's $conn->mode()... but too lazy.
492 rawout("MODE $chan $modes");
496 my ($chan, @who) = @_;
497 my $os = "o" x scalar(@who);
499 &mode($chan, "+$os @who");
503 my ($chan, @who) = @_;
504 my $os = "o" x scalar(@who);
506 &mode($chan, "-$os ".@who);
510 my ($nick,$chan,$msg) = @_;
511 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
513 if ($chan ne "" and &validChan($chan) == 0) {
514 &ERROR("kick: invalid channel $chan.");
518 $nick =~ tr/A-Z/a-z/;
520 foreach $chan (@chans) {
521 if (!&IsNickInChan($nick,$chan)) {
522 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
526 if (!exists $channels{$chan}{o}{$ident}) {
527 &status("kick: do not have ops on $chan :(");
531 &status("Kicking $nick from $chan.");
532 $conn->kick($chan, $nick, $msg);
537 my ($mask,$chan) = @_;
538 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
541 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
542 &ERROR("ban: invalid channel $chan.");
546 foreach $chan (@chans) {
547 if (!exists $channels{$chan}{o}{$ident}) {
548 &status("ban: do not have ops on $chan :(");
552 &status("Banning $mask from $chan.");
553 &rawout("MODE $chan +b $mask");
561 my ($mask,$chan) = @_;
562 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
565 &DEBUG("unban: mask = $mask, chan = @chans");
567 foreach $chan (@chans) {
568 if (!exists $channels{$chan}{o}{$ident}) {
569 &status("unBan: do not have ops on $chan :(");
573 &status("Removed ban $mask from $chan.");
574 &rawout("MODE $chan -b $mask");
583 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
585 $conn->quit($quitmsg);
587 &WARN("quit: could not quit!");
594 if (!defined $nick) {
595 &ERROR("nick: nick == NULL.");
599 if (defined $ident and $nick eq $ident) {
600 &WARN("nick: nick == ident == '$ident'.");
604 $bad++ if (exists $nuh{ $param{'ircNick'} });
605 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
608 &WARN("Nick: not going to try and get my nick back. [".
609 scalar(gmtime). "]");
610 # hrm... over time we lose track of our own nick.
614 if ($nick =~ /^$mask{nick}$/) {
615 rawout("NICK ".$nick);
617 if (defined $ident) {
618 &status("nick: Changing nick to $nick (from $ident)");
619 # following shouldn't be here :(
622 &DEBUG("first time nick change.");
628 &DEBUG("nick: failed... why oh why (nick => $nick)");
634 my($who, $chan) = @_;
635 # todo: check if $who or $chan are invalid.
637 $conn->invite($who, $chan);
641 # Channel related functions...
644 # Usage: &joinNextChan();
646 if (scalar @joinchan) {
647 my $chan = shift @joinchan;
650 if (my $i = scalar @joinchan) {
651 &status("joinNextChan: $i chans to join.");
658 my @c = &getJoinChans();
659 if (exists $cache{joinTime} and scalar @c) {
660 my $delta = time() - $cache{joinTime} - 5;
661 my $timestr = &Time2String($delta);
662 my $rate = sprintf("%.1f", $delta / @c);
663 delete $cache{joinTime};
665 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
668 # chanserv check: global channels, in case we missed one.
669 foreach ( &ChanConfList("chanServ_ops") ) {
674 # Usage: &getNickInChans($nick);
679 foreach (keys %channels) {
680 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
687 # Usage: &getNicksInChan($chan);
692 return keys %{ $channels{$chan}{''} };
696 my ($nick,$chan) = @_;
698 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
700 if (&validChan($chan) == 0) {
701 &ERROR("INIC: invalid channel $chan.");
705 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
708 foreach (keys %channels) {
709 next unless (/[A-Z]/);
710 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
716 sub IsNickInAnyChan {
720 foreach $chan (keys %channels) {
721 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
727 # Usage: &validChan($chan);
729 # todo: use $c instead?
732 if (!defined $chan or $chan =~ /^\s*$/) {
736 if (lc $chan ne $chan) {
737 &WARN("validChan: lc chan != chan. ($chan); fixing.");
738 $chan =~ tr/A-Z/a-z/;
741 # it's possible that this check creates the hash if empty.
742 if (defined $channels{$chan} or exists $channels{$chan}) {
743 if ($chan =~ /^_?default$/) {
744 # &WARN("validC: chan cannot be _default! returning 0!");
755 # Usage: &delUserInfo($nick,@chans);
757 my ($nick,@chans) = @_;
760 foreach $chan (@chans) {
761 foreach $mode (keys %{ $channels{$chan} }) {
763 next unless (exists $channels{$chan}{$mode}{$nick});
765 delete $channels{$chan}{$mode}{$nick};
773 delete $channels{$chan};
777 &DEBUG("clearIRCVars() called!");
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 $time += $floodjoin{$c}{$_}{Time};
872 &DEBUG("joinflood: time => $time");
875 &DEBUG("joinflood: new time => $time");
881 foreach $chan (keys %floodjoin) {
882 foreach $who (keys %{ $floodjoin{$chan} }) {
883 my $t = $floodjoin{$chan}{$who}{Time};
884 next unless (defined $t);
886 my $delta = $time - $t;
887 next unless ($delta > 10);
889 delete $floodjoin{$chan}{$who};
894 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
900 if (exists $nuh{$n}) {
901 return &makeHostMask($nuh{$n});
903 $cache{on_who_Hack} = 1;