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(324, \&on_modeis);
140 $conn->add_global_handler(333, \&on_topicinfo);
141 $conn->add_global_handler(352, \&on_who);
142 $conn->add_global_handler(353, \&on_names);
143 $conn->add_global_handler(366, \&on_endofnames);
144 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
145 $conn->add_global_handler(433, \&on_nick_taken);
146 $conn->add_global_handler(439, \&on_targettoofast);
148 # end of handler stuff.
153 ######################################################################
154 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
155 ######################################################################
161 # slow down a bit if traffic is "high".
162 # need to take into account time of last message sent.
163 if ($last{buflen} > 256 and length($buf) > 256) {
167 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
169 $last{buflen} = length($buf);
176 &WARN("say: msg == $msg.");
180 &status("</$talkchannel> $msg");
181 if (&whatInterface() =~ /IRC/) {
182 $msg = "zero" if ($msg =~ /^0+$/);
185 if ($t == $pubtime) {
187 $pubsize += length $msg;
189 if ( ($pubcount % 4) == 0 and $pubcount) {
191 } elsif ($pubsize > 1500) {
199 $pubsize = length $msg;
202 $conn->privmsg($talkchannel, $msg);
207 my ($nick, $msg) = @_;
208 if (!defined $nick) {
209 &ERROR("msg: nick == NULL.");
215 &WARN("msg: msg == $msg.");
219 &status(">$nick< $msg");
221 if (&whatInterface() =~ /IRC/) {
224 if ($t == $msgtime) {
226 $msgsize += length $msg;
228 if ( ($msgcount % 4) == 0 and $msgcount) {
230 } elsif ($msgsize > 1000) {
238 $msgsize = length $msg;
241 $conn->privmsg($nick, $msg);
245 # Usage: &action(nick || chan, txt);
247 my ($target, $txt) = @_;
249 &WARN("action: txt == NULL.");
253 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
254 if (length $rawout > 510) {
255 &status("action: txt too long; truncating.");
257 chop($rawout) while (length($rawout) > 510);
261 &status("* $ident/$target $txt");
265 # Usage: ¬ice(nick || chan, txt);
267 my ($target, $txt) = @_;
269 &WARN("notice: txt == NULL.");
273 &status("-$target- $txt");
277 if ($t == $nottime) {
279 $notsize += length $txt;
281 if ( ($notcount % 4) == 0 and $notcount) {
283 } elsif ($notsize > 1500) {
291 $notsize = length $txt;
294 $conn->notice($target, $txt);
299 my ($txt,$flag) = @_;
301 ### FIXME: flag not supported yet.
303 foreach (keys %{$dcc{'CHAT'}}) {
304 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
309 ### perform commands.
312 # Usage: &performReply($reply);
315 $reply =~ /([\.\?\s]+)$/;
317 &checkMsgType($reply);
319 if ($msgType eq 'public') {
320 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
321 $reply = "$orig{who}: ".$reply;
323 $reply = "$reply, ".$orig{who};
326 } elsif ($msgType eq 'private') {
330 $reply = "$reply, ".$orig{who};
333 } elsif ($msgType eq 'chat') {
334 if (!exists $dcc{'CHAT'}{$who}) {
335 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
338 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
340 &ERROR("PR: msgType invalid? ($msgType).");
345 sub performAddressedReply {
346 return unless ($addressed);
351 &performStrictReply(@_);
354 # Usage: &performStrictReply($reply);
355 sub performStrictReply {
358 &checkMsgType($reply);
360 if ($msgType eq 'private') {
362 } elsif ($msgType eq 'public') {
364 } elsif ($msgType eq 'chat') {
365 &dccsay(lc $who,$reply);
367 &ERROR("pSR: msgType invalid? ($msgType).");
372 my ($who, $reply) = @_;
373 if (!exists $dcc{'CHAT'}{$who}) {
374 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
378 &status("=>$who<= $reply"); # dcc chat.
379 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
386 foreach $type (keys %dcc) {
387 &FIXME("dcc_close: $who");
388 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
389 next unless (scalar @who);
396 my $chan = lc $chankey;
398 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
402 &status("joining $b_blue$chan$ob");
404 if (&validChan($chan)) {
405 &status("join: already on $chan");
407 if (!$conn->join($chan)) {
408 &DEBUG("joinchan: join failed. trying connect!");
418 next if ($chan eq "");
419 $chan =~ tr/A-Z/a-z/; # lowercase.
421 &status("parting $chan");
422 if (!&validChan($chan)) {
423 &status("part: not on $chan");
427 rawout("PART $chan");
428 # deletion of $channels{chan} is done in &entryEvt().
433 my ($chan, @modes) = @_;
434 my $modes = join(" ", @modes);
436 if (&validChan($chan) == 0) {
437 &ERROR("mode: invalid chan => '$chan'.");
441 &DEBUG("mode: MODE $chan $modes");
443 rawout("MODE $chan $modes");
447 my ($chan, @who) = @_;
448 my $os = "o" x scalar(@who);
450 &mode($chan, "+$os @who");
454 my ($chan, @who) = @_;
455 my $os = "o" x scalar(@who);
457 &mode($chan, "-$os ".@who);
461 my ($nick,$chan,$msg) = @_;
462 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
464 if ($chan ne "" and &validChan($chan) == 0) {
465 &ERROR("kick: invalid channel $chan.");
469 $nick =~ tr/A-Z/a-z/;
471 foreach $chan (@chans) {
472 if (!&IsNickInChan($nick,$chan)) {
473 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
477 if (!exists $channels{$chan}{o}{$ident}) {
478 &status("Kick: do not have ops on $chan :(");
482 &status("Kicking $nick from $chan.");
484 &rawout("KICK $chan $nick");
486 &rawout("KICK $chan $nick :$msg");
492 my ($mask,$chan) = @_;
493 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
496 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
497 &ERROR("ban: invalid channel $chan.");
501 foreach $chan (@chans) {
502 if (!exists $channels{$chan}{o}{$ident}) {
503 &status("Ban: do not have ops on $chan :(");
507 &status("Banning $mask from $chan.");
508 &rawout("MODE $chan +b $mask");
516 my ($mask,$chan) = @_;
517 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
520 &DEBUG("unban: mask = $mask, chan = @chans");
522 foreach $chan (@chans) {
523 if (!exists $channels{$chan}{o}{$ident}) {
524 &status("unBan: do not have ops on $chan :(");
528 &status("Removed ban $mask from $chan.");
529 &rawout("MODE $chan -b $mask");
538 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
539 $conn->quit($quitmsg);
545 if ($nick =~ /^$mask{nick}$/) {
546 &DEBUG("nick: Changing nick to $nick (from $ident)");
547 rawout("NICK ".$nick);
550 &DEBUG("nick: failed... why oh why (nick => $nick)");
556 my($who, $chan) = @_;
557 rawout("INVITE $who $chan");
562 # Channel related functions...
565 # Usage: &joinNextChan();
567 if (scalar @joinchan) {
568 my $chan = shift @joinchan;
571 if (my $i = scalar @joinchan) {
572 &status("joinNextChan: $i chans to join.");
577 if (&IsParam("nickServ_pass") and $nickserv < 1) {
578 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
582 my %chan = &getChanConfList("chanServ");
583 foreach $chan (keys %chan) {
584 next unless ($chan{$chan} > 0);
586 if (!exists $channels{$chan}{'o'}{$ident}) {
587 &status("ChanServ ==> Requesting ops for $chan. (1)");
588 &rawout("PRIVMSG ChanServ :OP $chan $ident");
593 # Usage: &GetNickInChans($nick,$chan);
598 foreach (keys %channels) {
599 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
606 # Usage: &GetNicksInChan($chan);
611 return keys %{ $channels{$chan}{''} };
615 my ($nick,$chan) = @_;
617 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
619 if (&validChan($chan) == 0) {
620 &ERROR("INIC: invalid channel $chan.");
624 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
627 foreach (keys %channels) {
628 next unless (/[A-Z]/);
629 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
635 sub IsNickInAnyChan {
638 foreach $chan (keys %channels) {
639 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
645 # Usage: &validChan($chan);
649 if (lc $chan ne $chan) {
650 &WARN("validChan: lc chan != chan. ($chan); fixing.");
651 $chan =~ tr/A-Z/a-z/;
654 if (exists $channels{$chan}) {
662 # Usage: &DeleteUserInfo($nick,@chans);
664 my ($nick,@chans) = @_;
667 foreach $chan (@chans) {
668 foreach $mode (keys %{$channels{$chan}}) {
670 next unless (exists $channels{$chan}{$mode}{$nick});
672 delete $channels{$chan}{$mode}{$nick};
680 delete $channels{$chan};
684 ### &DEBUG("clearIRCVars() called!");
688 @joinchan = &getJoinChans();
695 foreach (keys %chanconf) {
696 next if ($_ eq "_default");
698 my $val = $chanconf{$_}{autojoin};
702 $skip++ if ($val eq "0");
716 &status("gJC: channels not auto-joining: @skip");
718 &status("gJC: auto-joining all chans.");
725 ### &DEBUG("closeDCC called.");
727 foreach $type (keys %dcc) {
728 next if ($type ne uc($type));
730 foreach $nick (keys %{$dcc{$type}}) {
731 next unless (defined $nick);
732 &DEBUG("closing DCC $type to $nick (FIXME).");
733 next unless (defined $dcc{$type}{$nick});
735 my $ref = $dcc{$type}{$nick};
736 &DEBUG("ref => $ref");
738 # $dcc{$type}{$nick}->close();
744 my($who,$chan,$userhost) = @_;
746 return unless (&IsChanConf("joinfloodCheck"));
748 if (exists $netsplit{lc $who}) { # netsplit join.
749 &DEBUG("jfC: $who was in netsplit; not checking.");
752 if (exists $floodjoin{$chan}{$who}{Time}) {
753 &WARN("floodjoin{$chan}{$who} already exists?");
756 $floodjoin{$chan}{$who}{Time} = time();
757 $floodjoin{$chan}{$who}{Host} = $userhost;
760 foreach (keys %floodjoin) {
762 my $count = scalar keys %{ $floodjoin{$c} };
763 next unless ($count > 5);
764 &DEBUG("joinflood: count => $count");
767 foreach (keys %{ $floodjoin{$c} }) {
768 $time += $floodjoin{$c}{$_}{Time};
770 &DEBUG("joinflood: time => $time");
773 &DEBUG("joinflood: new time => $time");
778 foreach $chan (keys %floodjoin) {
779 foreach $who (keys %{ $floodjoin{$chan} }) {
780 my $time = time() - $floodjoin{$chan}{$who}{Time};
781 next unless ($time > 10);
782 delete $floodjoin{$chan}{$who};
787 &DEBUG("jfC: $delete deleted.") if ($delete);
793 &FIXME("getHostMask...");