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 &DEBUG("ircloop: after irc()");
46 next unless (defined $retval and $retval == 0);
50 if ($error % 3 == 0 and $error != 0) {
51 &ERROR("CANNOT connect to this server; next!");
56 &ERROR("CANNOT connect to any irc server; stopping.");
61 &DEBUG("ircloop: end... going back.");
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?
93 $conn = $irc->newconn(
94 Nick => $param{'ircNick'},
97 Ircname => $param{'ircName'},
98 LocalAddr => $param{'ircHost'},
101 if (!defined $conn) {
102 &ERROR("irc: conn was not created!defined!!!");
108 # change internal timeout value for scheduler.
109 $irc->{_timeout} = 10; # how about 60?
111 $ircstats{'Server'} = "$server:$port";
114 $conn->add_handler('caction', \&on_action);
115 $conn->add_handler('cdcc', \&on_dcc);
116 $conn->add_handler('cping', \&on_ping);
117 $conn->add_handler('crping', \&on_ping_reply);
118 $conn->add_handler('cversion', \&on_version);
119 $conn->add_handler('crversion', \&on_crversion);
120 $conn->add_handler('dcc_open', \&on_dcc_open);
121 $conn->add_handler('dcc_close', \&on_dcc_close);
122 $conn->add_handler('chat', \&on_chat);
123 $conn->add_handler('msg', \&on_msg);
124 $conn->add_handler('public', \&on_public);
125 $conn->add_handler('join', \&on_join);
126 $conn->add_handler('part', \&on_part);
127 $conn->add_handler('topic', \&on_topic);
128 $conn->add_handler('invite', \&on_invite);
129 $conn->add_handler('kick', \&on_kick);
130 $conn->add_handler('mode', \&on_mode);
131 $conn->add_handler('nick', \&on_nick);
132 $conn->add_handler('quit', \&on_quit);
133 $conn->add_handler('notice', \&on_notice);
134 $conn->add_handler('whoisuser', \&on_whoisuser);
135 $conn->add_handler('other', \&on_other);
136 $conn->add_global_handler('disconnect', \&on_disconnect);
137 $conn->add_global_handler([251,252,253,254,255], \&on_init);
138 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
139 $conn->add_global_handler(315, \&on_endofwho);
140 $conn->add_global_handler(324, \&on_modeis);
141 $conn->add_global_handler(333, \&on_topicinfo);
142 $conn->add_global_handler(352, \&on_who);
143 $conn->add_global_handler(353, \&on_names);
144 $conn->add_global_handler(366, \&on_endofnames);
145 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
146 $conn->add_global_handler(433, \&on_nick_taken);
147 $conn->add_global_handler(439, \&on_targettoofast);
148 # for proper joinnextChan behaviour
149 $conn->add_global_handler(471, \&on_chanfull);
150 $conn->add_global_handler(473, \&on_inviteonly);
151 $conn->add_global_handler(474, \&on_banned);
152 $conn->add_global_handler(475, \&on_badchankey);
154 # end of handler stuff.
159 ######################################################################
160 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
161 ######################################################################
167 # slow down a bit if traffic is "high".
168 # need to take into account time of last message sent.
169 if ($last{buflen} > 256 and length($buf) > 256) {
173 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
175 $last{buflen} = length($buf);
182 &WARN("say: msg == $msg.");
186 &status("</$talkchannel> $msg");
187 if (&whatInterface() =~ /IRC/) {
188 $msg = "zero" if ($msg =~ /^0+$/);
191 if ($t == $pubtime) {
193 $pubsize += length $msg;
195 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
196 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
198 if ( ($pubcount % $i) == 0 and $pubcount) {
200 } elsif ($pubsize > $j) {
208 $pubsize = length $msg;
211 $conn->privmsg($talkchannel, $msg);
216 my ($nick, $msg) = @_;
217 if (!defined $nick) {
218 &ERROR("msg: nick == NULL.");
224 &WARN("msg: msg == $msg.");
228 &status(">$nick< $msg");
230 if (&whatInterface() =~ /IRC/) {
233 if ($t == $msgtime) {
235 $msgsize += length $msg;
237 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
238 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
239 if ( ($msgcount % $i) == 0 and $msgcount) {
241 } elsif ($msgsize > $j) {
249 $msgsize = length $msg;
252 $conn->privmsg($nick, $msg);
256 # Usage: &action(nick || chan, txt);
258 my ($target, $txt) = @_;
260 &WARN("action: txt == NULL.");
264 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
265 if (length $rawout > 510) {
266 &status("action: txt too long; truncating.");
268 chop($rawout) while (length($rawout) > 510);
272 &status("* $ident/$target $txt");
276 # Usage: ¬ice(nick || chan, txt);
278 my ($target, $txt) = @_;
280 &WARN("notice: txt == NULL.");
284 &status("-$target- $txt");
288 if ($t == $nottime) {
290 $notsize += length $txt;
292 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
293 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
295 if ( ($notcount % $i) == 0 and $notcount) {
297 } elsif ($notsize > $j) {
305 $notsize = length $txt;
308 $conn->notice($target, $txt);
312 my ($txt,$flag) = @_;
314 ### FIXME: flag not supported yet.
316 foreach (keys %{ $dcc{'CHAT'} }) {
317 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
322 ### perform commands.
325 # Usage: &performReply($reply);
328 $reply =~ /([\.\?\s]+)$/;
330 &checkMsgType($reply);
332 if ($msgType eq 'public') {
333 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
334 $reply = "$orig{who}: ".$reply;
336 $reply = "$reply, ".$orig{who};
339 } elsif ($msgType eq 'private') {
343 $reply = "$reply, ".$orig{who};
346 } elsif ($msgType eq 'chat') {
347 if (!exists $dcc{'CHAT'}{$who}) {
348 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
351 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
353 &ERROR("PR: msgType invalid? ($msgType).");
358 sub performAddressedReply {
359 return unless ($addressed);
364 &performStrictReply(@_);
367 # Usage: &performStrictReply($reply);
368 sub performStrictReply {
371 &checkMsgType($reply);
373 if ($msgType eq 'private') {
375 } elsif ($msgType eq 'public') {
377 } elsif ($msgType eq 'chat') {
378 &dccsay(lc $who,$reply);
380 &ERROR("pSR: msgType invalid? ($msgType).");
385 my ($who, $reply) = @_;
386 if (!exists $dcc{'CHAT'}{$who}) {
387 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
391 &status("=>$who<= $reply"); # dcc chat.
392 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
399 foreach $type (keys %dcc) {
400 &FIXME("dcc_close: $who");
401 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
402 next unless (scalar @who);
409 my $chan = lc $chankey;
411 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
415 &status("joining $b_blue$chan$ob");
417 if (&validChan($chan)) {
418 &status("join: already on $chan");
420 if (!$conn->join($chan)) {
421 &DEBUG("joinchan: join failed. trying connect!");
431 next if ($chan eq "");
432 $chan =~ tr/A-Z/a-z/; # lowercase.
434 &status("parting $chan");
435 if (!&validChan($chan)) {
436 &WARN("part: not on $chan; doing anyway");
440 rawout("PART $chan");
441 # deletion of $channels{chan} is done in &entryEvt().
446 my ($chan, @modes) = @_;
447 my $modes = join(" ", @modes);
449 if (&validChan($chan) == 0) {
450 &ERROR("mode: invalid chan => '$chan'.");
454 &DEBUG("mode: MODE $chan $modes");
456 rawout("MODE $chan $modes");
460 my ($chan, @who) = @_;
461 my $os = "o" x scalar(@who);
463 &mode($chan, "+$os @who");
467 my ($chan, @who) = @_;
468 my $os = "o" x scalar(@who);
470 &mode($chan, "-$os ".@who);
474 my ($nick,$chan,$msg) = @_;
475 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
477 if ($chan ne "" and &validChan($chan) == 0) {
478 &ERROR("kick: invalid channel $chan.");
482 $nick =~ tr/A-Z/a-z/;
484 foreach $chan (@chans) {
485 if (!&IsNickInChan($nick,$chan)) {
486 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
490 if (!exists $channels{$chan}{o}{$ident}) {
491 &status("Kick: do not have ops on $chan :(");
495 &status("Kicking $nick from $chan.");
497 &rawout("KICK $chan $nick");
499 &rawout("KICK $chan $nick :$msg");
505 my ($mask,$chan) = @_;
506 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
509 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
510 &ERROR("ban: invalid channel $chan.");
514 foreach $chan (@chans) {
515 if (!exists $channels{$chan}{o}{$ident}) {
516 &status("Ban: do not have ops on $chan :(");
520 &status("Banning $mask from $chan.");
521 &rawout("MODE $chan +b $mask");
529 my ($mask,$chan) = @_;
530 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
533 &DEBUG("unban: mask = $mask, chan = @chans");
535 foreach $chan (@chans) {
536 if (!exists $channels{$chan}{o}{$ident}) {
537 &status("unBan: do not have ops on $chan :(");
541 &status("Removed ban $mask from $chan.");
542 &rawout("MODE $chan -b $mask");
551 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
552 $conn->quit($quitmsg);
558 if ($nick =~ /^$mask{nick}$/) {
559 &DEBUG("nick: Changing nick to $nick (from $ident)");
560 rawout("NICK ".$nick);
561 # unfortunately, on_nick doesn't catch ourself.
565 &DEBUG("nick: failed... why oh why (nick => $nick)");
571 my($who, $chan) = @_;
572 rawout("INVITE $who $chan");
577 # Channel related functions...
580 # Usage: &joinNextChan();
582 &DEBUG("joinNextChan called.");
584 if (scalar @joinchan) {
585 $chan = shift @joinchan;
588 if (my $i = scalar @joinchan) {
589 &status("joinNextChan: $i chans to join.");
593 if (exists $cache{joinTime}) {
594 my $delta = time() - $cache{joinTime};
595 my $timestr = &Time2String($delta);
596 my $rate = sprintf("%.1f", $delta / &getJoinChans() );
597 delete $cache{joinTime};
599 &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
601 # chanserv check: global channels, in case we missed one.
603 foreach ( &ChanConfList("chanServ_ops") ) {
609 # Usage: &GetNickInChans($nick,$chan);
614 foreach (keys %channels) {
615 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
622 # Usage: &GetNicksInChan($chan);
627 return keys %{ $channels{$chan}{''} };
631 my ($nick,$chan) = @_;
633 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
635 if (&validChan($chan) == 0) {
636 &ERROR("INIC: invalid channel $chan.");
640 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
643 foreach (keys %channels) {
644 next unless (/[A-Z]/);
645 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
651 sub IsNickInAnyChan {
654 foreach $chan (keys %channels) {
655 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
661 # Usage: &validChan($chan);
665 if (lc $chan ne $chan) {
666 &WARN("validChan: lc chan != chan. ($chan); fixing.");
667 $chan =~ tr/A-Z/a-z/;
670 if (exists $channels{$chan}) {
678 # Usage: &DeleteUserInfo($nick,@chans);
680 my ($nick,@chans) = @_;
683 foreach $chan (@chans) {
684 foreach $mode (keys %{ $channels{$chan} }) {
686 next unless (exists $channels{$chan}{$mode}{$nick});
688 delete $channels{$chan}{$mode}{$nick};
696 delete $channels{$chan};
700 ### &DEBUG("clearIRCVars() called!");
704 @joinchan = &getJoinChans(1);
705 $cache{joinTime} = time();
713 foreach (keys %chanconf) {
714 next if ($_ eq "_default");
716 my $val = $chanconf{$_}{autojoin};
720 $skip++ if ($val eq "0");
735 $str = "gJC: channels not auto-joining: @skip";
737 $str = "gJC: auto-joining all chans.";
740 &status($str) if ($show);
746 ### &DEBUG("closeDCC called.");
748 foreach $type (keys %dcc) {
749 next if ($type ne uc($type));
751 foreach $nick (keys %{ $dcc{$type} }) {
752 next unless (defined $nick);
753 &DEBUG("closing DCC $type to $nick (FIXME).");
754 next unless (defined $dcc{$type}{$nick});
756 my $ref = $dcc{$type}{$nick};
757 &DEBUG("ref => $ref");
759 # $dcc{$type}{$nick}->close();
765 my($who,$chan,$userhost) = @_;
767 return unless (&IsChanConf("joinfloodCheck"));
769 if (exists $netsplit{lc $who}) { # netsplit join.
770 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
773 if (exists $floodjoin{$chan}{$who}{Time}) {
774 &WARN("floodjoin{$chan}{$who} already exists?");
777 $floodjoin{$chan}{$who}{Time} = time();
778 $floodjoin{$chan}{$who}{Host} = $userhost;
781 foreach (keys %floodjoin) {
783 my $count = scalar keys %{ $floodjoin{$c} };
784 next unless ($count > 5);
785 &DEBUG("joinflood: count => $count");
788 foreach (keys %{ $floodjoin{$c} }) {
789 $time += $floodjoin{$c}{$_}{Time};
791 &DEBUG("joinflood: time => $time");
794 &DEBUG("joinflood: new time => $time");
799 foreach $chan (keys %floodjoin) {
800 foreach $who (keys %{ $floodjoin{$chan} }) {
801 my $time = time() - $floodjoin{$chan}{$who}{Time};
802 next unless ($time > 10);
803 delete $floodjoin{$chan}{$who};
808 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
814 &FIXME("getHostMask($n) called...");