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("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 &DEBUG("say: msg == $msg.");
180 &status("</$talkchannel> $msg");
181 if (&whatInterface() =~ /IRC/) {
182 $msg = "zero" if ($msg =~ /^0+$/);
184 $conn->privmsg($talkchannel, $msg);
189 my ($nick, $msg) = @_;
190 if (!defined $nick) {
191 &ERROR("msg: nick == NULL.");
197 &DEBUG("msg: msg == $msg.");
201 &status(">$nick< $msg");
202 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
205 # Usage: &action(nick || chan, txt);
207 my ($target, $txt) = @_;
209 &DEBUG("action: txt == NULL.");
213 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
214 if (length $rawout > 510) {
215 &status("action: txt too long; truncating.");
217 chop($rawout) while (length($rawout) > 510);
221 &status("* $ident/$target $txt");
225 # Usage: &action(nick || chan, txt);
227 my ($target, $txt) = @_;
229 &DEBUG("action: txt == NULL.");
233 &status("-$target- $txt");
235 $conn->notice($target, $txt);
240 my ($txt,$flag) = @_;
242 ### FIXME: flag not supported yet.
244 foreach (keys %{$dcc{'CHAT'}}) {
245 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
250 ### perform commands.
253 # Usage: &performReply($reply);
256 $reply =~ /([\.\?\s]+)$/;
258 &checkMsgType($reply);
260 if ($msgType eq 'public') {
261 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
262 $reply = "$orig{who}: ".$reply;
264 $reply = "$reply, ".$orig{who};
267 } elsif ($msgType eq 'private') {
271 $reply = "$reply, ".$orig{who};
274 } elsif ($msgType eq 'chat') {
275 if (!exists $dcc{'CHAT'}{$who}) {
276 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
279 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
281 &ERROR("PR: msgType invalid? ($msgType).");
286 sub performAddressedReply {
287 return unless ($addressed);
292 &performStrictReply(@_);
295 # Usage: &performStrictReply($reply);
296 sub performStrictReply {
299 &checkMsgType($reply);
301 if ($msgType eq 'private') {
303 } elsif ($msgType eq 'public') {
305 } elsif ($msgType eq 'chat') {
306 &dccsay(lc $who,$reply);
308 &ERROR("pSR: msgType invalid? ($msgType).");
313 my ($who, $reply) = @_;
314 if (!exists $dcc{'CHAT'}{$who}) {
315 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
319 &status("=>$who<= $reply"); # dcc chat.
320 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
327 foreach $type (keys %dcc) {
328 &FIXME("dcc_close: $who");
329 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
330 next unless (scalar @who);
337 my $chan = lc $chankey;
339 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
343 &status("joining $b_blue$chan$ob");
345 if (&validChan($chan)) {
346 &status("join: already on $chan");
348 if (!$conn->join($chan)) {
349 &DEBUG("join failed. trying connect!");
359 next if ($chan eq "");
360 $chan =~ tr/A-Z/a-z/; # lowercase.
362 &status("parting $chan");
363 if (!&validChan($chan)) {
364 &status("part: not on $chan");
368 rawout("PART $chan");
369 # deletion of $channels{chan} is done in &entryEvt().
374 my ($chan, @modes) = @_;
375 my $modes = join(" ", @modes);
377 if (&validChan($chan) == 0) {
378 &ERROR("mode: invalid chan => '$chan'.");
382 &DEBUG("MODE $chan $modes");
384 rawout("MODE $chan $modes");
388 my ($chan, @who) = @_;
389 my $os = "o" x scalar(@who);
391 &mode($chan, "+$os @who");
395 my ($chan, @who) = @_;
396 my $os = "o" x scalar(@who);
398 &mode($chan, "-$os ".@who);
402 my ($nick,$chan,$msg) = @_;
403 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
405 if ($chan ne "" and &validChan($chan) == 0) {
406 &ERROR("kick: invalid channel $chan.");
410 $nick =~ tr/A-Z/a-z/;
412 foreach $chan (@chans) {
413 if (!&IsNickInChan($nick,$chan)) {
414 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
418 if (!exists $channels{$chan}{o}{$ident}) {
419 &status("Kick: do not have ops on $chan :(");
423 &status("Kicking $nick from $chan.");
425 &rawout("KICK $chan $nick");
427 &rawout("KICK $chan $nick :$msg");
433 my ($mask,$chan) = @_;
434 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
437 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
438 &ERROR("ban: invalid channel $chan.");
442 foreach $chan (@chans) {
443 if (!exists $channels{$chan}{o}{$ident}) {
444 &status("Ban: do not have ops on $chan :(");
448 &status("Banning $mask from $chan.");
449 &rawout("MODE $chan +b $mask");
457 my ($mask,$chan) = @_;
458 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
461 &DEBUG("unban: mask = $mask, chan = @chans");
463 foreach $chan (@chans) {
464 if (!exists $channels{$chan}{o}{$ident}) {
465 &status("unBan: do not have ops on $chan :(");
469 &status("Removed ban $mask from $chan.");
470 &rawout("MODE $chan -b $mask");
479 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
480 $conn->quit($quitmsg);
486 if ($nick =~ /^$mask{nick}$/) {
487 rawout("NICK ".$nick);
495 my($who, $chan) = @_;
496 rawout("INVITE $who $chan");
501 # Channel related functions...
504 # Usage: &joinNextChan();
506 if (scalar @joinchan) {
507 my $chan = shift @joinchan;
510 if (my $i = scalar @joinchan) {
511 &status("joinNextChan: $i chans to join.");
516 if (&IsParam("nickServ_pass") and $nickserv < 1) {
517 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
521 my %chan = &getChanConfList("chanServ");
522 foreach $chan (keys %chan) {
523 next unless ($chan{$chan} > 0);
525 if (!exists $channels{$chan}{'o'}{$ident}) {
526 &status("ChanServ ==> Requesting ops for $chan.");
527 &rawout("PRIVMSG ChanServ :OP $chan $ident");
532 # Usage: &GetNickInChans($nick,$chan);
537 foreach (keys %channels) {
538 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
545 # Usage: &GetNicksInChan($chan);
550 return keys %{ $channels{$chan}{''} };
554 my ($nick,$chan) = @_;
556 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
558 if (&validChan($chan) == 0) {
559 &ERROR("INIC: invalid channel $chan.");
563 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
566 foreach (keys %channels) {
567 next unless (/[A-Z]/);
568 &DEBUG("hash channels contains mixed cased chan!!!");
574 sub IsNickInAnyChan {
577 foreach $chan (keys %channels) {
578 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
584 # Usage: &validChan($chan);
588 if (lc $chan ne $chan) {
589 &WARN("validChan: lc chan != chan. ($chan); fixing.");
590 $chan =~ tr/A-Z/a-z/;
593 if (exists $channels{$chan}) {
601 # Usage: &DeleteUserInfo($nick,@chans);
603 my ($nick,@chans) = @_;
606 foreach $chan (@chans) {
607 foreach $mode (keys %{$channels{$chan}}) {
609 next unless (exists $channels{$chan}{$mode}{$nick});
611 delete $channels{$chan}{$mode}{$nick};
619 delete $channels{$chan};
623 &DEBUG("clearIRCVars() called!");
627 @joinchan = &getJoinChans();
634 foreach (keys %chanconf) {
635 next if ($_ eq "_default");
637 my $val = $chanconf{$_}{autojoin};
641 $skip++ if ($val eq "0");
655 &status("gJC: channels not auto-joining: @skip");
657 &status("gJC: auto-joining all chans.");
664 &DEBUG("closeDCC called.");
666 foreach $type (keys %dcc) {
667 next if ($type ne uc($type));
669 foreach $nick (keys %{$dcc{$type}}) {
670 next unless (defined $nick);
671 &DEBUG("closing DCC $type to $nick (FIXME).");
672 next unless (defined $dcc{$type}{$nick});
674 my $ref = $dcc{$type}{$nick};
675 &DEBUG("ref => $ref");
677 # $dcc{$type}{$nick}->close();
683 my($who,$chan,$userhost) = @_;
685 return unless (&IsChanConf("joinfloodCheck"));
687 if (exists $netsplit{lc $who}) { # netsplit join.
688 &DEBUG("jfC: $who was in netsplit; not checking.");
691 if (exists $floodjoin{$chan}{$who}{Time}) {
692 &WARN("floodjoin{$chan}{$who} already exists?");
695 $floodjoin{$chan}{$who}{Time} = time();
696 $floodjoin{$chan}{$who}{Host} = $userhost;
699 foreach (keys %floodjoin) {
701 my $count = scalar keys %{ $floodjoin{$c} };
702 next unless ($count > 5);
703 &DEBUG("count => $count");
706 foreach (keys %{ $floodjoin{$c} }) {
707 $time += $floodjoin{$c}{$_}{Time};
709 &DEBUG("time => $time");
712 &DEBUG("new time => $time");
717 foreach $chan (keys %floodjoin) {
718 foreach $who (keys %{ $floodjoin{$chan} }) {
719 my $time = time() - $floodjoin{$chan}{$who}{Time};
720 next unless ($time > 10);
721 delete $floodjoin{$chan}{$who};
726 &DEBUG("jfC: $delete deleted.") if ($delete);
732 &FIXME("getHostMask...");