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 if ($msgType =~ /chat/i) {
233 # todo: warn that we're using msg() to do DCC CHAT?
234 &dccsay($nick, $msg);
235 # todo: make dccsay deal with flood protection?
239 &status(">$nick< $msg");
241 if (&whatInterface() =~ /IRC/) {
244 if ($t == $msgtime) {
246 $msgsize += length $msg;
248 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
249 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
250 if ( ($msgcount % $i) == 0 and $msgcount) {
252 } elsif ($msgsize > $j) {
260 $msgsize = length $msg;
263 if ($msgType =~ /private/i) { # hack.
264 $conn->privmsg($nick, $msg);
267 &DEBUG("msg: msgType is unknown!");
272 # Usage: &action(nick || chan, txt);
274 my ($target, $txt) = @_;
276 &WARN("action: txt == NULL.");
280 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
281 if (length $rawout > 510) {
282 &status("action: txt too long; truncating.");
284 chop($rawout) while (length($rawout) > 510);
288 &status("* $ident/$target $txt");
292 # Usage: ¬ice(nick || chan, txt);
294 my ($target, $txt) = @_;
296 &WARN("notice: txt == NULL.");
300 &status("-$target- $txt");
304 if ($t == $nottime) {
306 $notsize += length $txt;
308 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
309 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
311 if ( ($notcount % $i) == 0 and $notcount) {
313 } elsif ($notsize > $j) {
321 $notsize = length $txt;
324 $conn->notice($target, $txt);
328 my ($txt,$flag) = @_;
330 ### FIXME: flag not supported yet.
332 foreach (keys %{ $dcc{'CHAT'} }) {
333 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
338 ### perform commands.
341 # Usage: &performReply($reply);
344 $reply =~ /([\.\?\s]+)$/;
346 &checkMsgType($reply);
348 if ($msgType eq 'public') {
349 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
350 $reply = "$orig{who}: ".$reply;
352 $reply = "$reply, ".$orig{who};
355 } elsif ($msgType eq 'private') {
359 $reply = "$reply, ".$orig{who};
362 } elsif ($msgType eq 'chat') {
363 if (!exists $dcc{'CHAT'}{$who}) {
364 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
367 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
369 &ERROR("PR: msgType invalid? ($msgType).");
374 sub performAddressedReply {
375 return unless ($addressed);
380 &performStrictReply(@_);
383 # Usage: &performStrictReply($reply);
384 sub performStrictReply {
387 &checkMsgType($reply);
389 if ($msgType eq 'private') {
391 } elsif ($msgType eq 'public') {
393 } elsif ($msgType eq 'chat') {
394 &dccsay(lc $who, $reply);
396 &ERROR("pSR: msgType invalid? ($msgType).");
401 my($who, $reply) = @_;
403 if (!defined $reply or $reply =~ /^\s*$/) {
404 &WARN("dccsay: reply == NULL.");
408 if (!exists $dcc{'CHAT'}{$who}) {
409 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
413 &status("=>$who<= $reply"); # dcc chat.
414 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
421 foreach $type (keys %dcc) {
422 &FIXME("dcc_close: $who");
423 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
424 next unless (scalar @who);
426 &DEBUG("dcc_close... close $who!");
432 my $chan = lc $chankey;
434 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
438 &status("joining $b_blue$chan$ob");
440 if (&validChan($chan)) {
441 &status("join: already on $chan");
443 if (!$conn->join($chan)) {
444 &DEBUG("joinchan: join failed. trying connect!");
454 next if ($chan eq "");
455 $chan =~ tr/A-Z/a-z/; # lowercase.
457 &status("parting $chan");
458 if (!&validChan($chan)) {
459 &WARN("part: not on $chan; doing anyway");
463 rawout("PART $chan");
464 # deletion of $channels{chan} is done in &entryEvt().
469 my ($chan, @modes) = @_;
470 my $modes = join(" ", @modes);
472 if (&validChan($chan) == 0) {
473 &ERROR("mode: invalid chan => '$chan'.");
477 &DEBUG("mode: MODE $chan $modes");
479 rawout("MODE $chan $modes");
483 my ($chan, @who) = @_;
484 my $os = "o" x scalar(@who);
486 &mode($chan, "+$os @who");
490 my ($chan, @who) = @_;
491 my $os = "o" x scalar(@who);
493 &mode($chan, "-$os ".@who);
497 my ($nick,$chan,$msg) = @_;
498 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
500 if ($chan ne "" and &validChan($chan) == 0) {
501 &ERROR("kick: invalid channel $chan.");
505 $nick =~ tr/A-Z/a-z/;
507 foreach $chan (@chans) {
508 if (!&IsNickInChan($nick,$chan)) {
509 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
513 if (!exists $channels{$chan}{o}{$ident}) {
514 &status("Kick: do not have ops on $chan :(");
518 &status("Kicking $nick from $chan.");
520 &rawout("KICK $chan $nick");
522 &rawout("KICK $chan $nick :$msg");
528 my ($mask,$chan) = @_;
529 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
532 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
533 &ERROR("ban: invalid channel $chan.");
537 foreach $chan (@chans) {
538 if (!exists $channels{$chan}{o}{$ident}) {
539 &status("Ban: do not have ops on $chan :(");
543 &status("Banning $mask from $chan.");
544 &rawout("MODE $chan +b $mask");
552 my ($mask,$chan) = @_;
553 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
556 &DEBUG("unban: mask = $mask, chan = @chans");
558 foreach $chan (@chans) {
559 if (!exists $channels{$chan}{o}{$ident}) {
560 &status("unBan: do not have ops on $chan :(");
564 &status("Removed ban $mask from $chan.");
565 &rawout("MODE $chan -b $mask");
574 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
576 $conn->quit($quitmsg);
578 &WARN("quit: could not quit!");
585 if (!defined $nick) {
586 &ERROR("nick: nick == NULL.");
590 if (defined $ident and $nick eq $ident) {
591 &WARN("nick: nick == ident == '$ident'.");
595 $bad++ if (exists $nuh{ $param{'ircNick'} });
596 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
599 &WARN("Nick: not going to try and get my nick back. [".
600 scalar(localtime). "]");
604 if ($nick =~ /^$mask{nick}$/) {
605 rawout("NICK ".$nick);
607 if (defined $ident) {
608 &status("nick: Changing nick to $nick (from $ident)");
610 &DEBUG("first time nick change.");
616 &DEBUG("nick: failed... why oh why (nick => $nick)");
622 my($who, $chan) = @_;
623 rawout("INVITE $who $chan");
628 # Channel related functions...
631 # Usage: &joinNextChan();
633 if (scalar @joinchan) {
634 $chan = shift @joinchan;
637 if (my $i = scalar @joinchan) {
638 &status("joinNextChan: $i chans to join.");
645 my @c = &getJoinChans();
646 if (exists $cache{joinTime} and scalar @c) {
647 my $delta = time() - $cache{joinTime} - 5;
648 my $timestr = &Time2String($delta);
649 my $rate = sprintf("%.1f", $delta / @c);
650 delete $cache{joinTime};
652 &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
655 # chanserv check: global channels, in case we missed one.
656 foreach ( &ChanConfList("chanServ_ops") ) {
661 # Usage: &getNickInChans($nick);
666 foreach (keys %channels) {
667 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
674 # Usage: &getNicksInChan($chan);
679 return keys %{ $channels{$chan}{''} };
683 my ($nick,$chan) = @_;
685 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
687 if (&validChan($chan) == 0) {
688 &ERROR("INIC: invalid channel $chan.");
692 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
695 foreach (keys %channels) {
696 next unless (/[A-Z]/);
697 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
703 sub IsNickInAnyChan {
706 foreach $chan (keys %channels) {
707 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
713 # Usage: &validChan($chan);
717 if (lc $chan ne $chan) {
718 &WARN("validChan: lc chan != chan. ($chan); fixing.");
719 $chan =~ tr/A-Z/a-z/;
722 if (exists $channels{$chan}) {
730 # Usage: &delUserInfo($nick,@chans);
732 my ($nick,@chans) = @_;
735 foreach $chan (@chans) {
736 foreach $mode (keys %{ $channels{$chan} }) {
738 next unless (exists $channels{$chan}{$mode}{$nick});
740 delete $channels{$chan}{$mode}{$nick};
748 delete $channels{$chan};
752 # &DEBUG("clearIRCVars() called!");
756 @joinchan = &getJoinChans(1);
757 $cache{joinTime} = time();
765 foreach (keys %chanconf) {
766 next if ($_ eq "_default");
768 my $val = $chanconf{$_}{autojoin};
772 $skip++ if ($val eq "0");
787 $str = "channels not auto-joining: @skip (joining: @chans)";
789 $str = "auto-joining all chans: @chans";
792 &status("Chans: ".$str) if ($show);
798 # &DEBUG("closeDCC called.");
800 foreach $type (keys %dcc) {
801 next if ($type ne uc($type));
803 foreach $nick (keys %{ $dcc{$type} }) {
804 next unless (defined $nick);
805 &status("DCC CHAT: closing DCC $type to $nick.");
806 next unless (defined $dcc{$type}{$nick});
808 my $ref = $dcc{$type}{$nick};
809 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
810 $dcc{$type}{$nick}->close();
811 delete $dcc{$type}{$nick};
812 &DEBUG("after close for $nick");
819 my($who,$chan,$userhost) = @_;
821 return unless (&IsChanConf("joinfloodCheck"));
823 if (exists $netsplit{lc $who}) { # netsplit join.
824 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
827 if (exists $floodjoin{$chan}{$who}{Time}) {
828 &WARN("floodjoin{$chan}{$who} already exists?");
831 $floodjoin{$chan}{$who}{Time} = time();
832 $floodjoin{$chan}{$who}{Host} = $userhost;
835 foreach (keys %floodjoin) {
837 my $count = scalar keys %{ $floodjoin{$c} };
838 next unless ($count > 5);
839 &DEBUG("joinflood: count => $count");
842 foreach (keys %{ $floodjoin{$c} }) {
843 $time += $floodjoin{$c}{$_}{Time};
845 &DEBUG("joinflood: time => $time");
848 &DEBUG("joinflood: new time => $time");
853 foreach $chan (keys %floodjoin) {
854 foreach $who (keys %{ $floodjoin{$chan} }) {
855 my $time = time() - $floodjoin{$chan}{$who}{Time};
856 next unless ($time > 10);
857 delete $floodjoin{$chan}{$who};
862 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
868 if (exists $nuh{$n}) {
869 return &makeHostMask($nuh{$n});
871 $cache{on_who_Hack} = 1;