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 if ($msg eq $last{say} and length($msg) > 256) {
181 &status("say: detected repeated message; skipping.");
186 &status("</$talkchannel> $msg");
187 if (&whatInterface() =~ /IRC/) {
188 $msg = "zero" if ($msg =~ /^0+$/);
190 $conn->privmsg($talkchannel, $msg);
195 my ($nick, $msg) = @_;
196 if (!defined $nick) {
197 &ERROR("msg: nick == NULL.");
203 &DEBUG("msg: msg == $msg.");
207 if ($msg eq $last{msg} and length($msg) > 256) {
208 &status("msg: detected repeated message; skipping.");
213 &status(">$nick< $msg");
214 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
217 # Usage: &action(nick || chan, txt);
219 my ($target, $txt) = @_;
221 &DEBUG("action: txt == NULL.");
225 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
226 if (length $rawout > 510) {
227 &status("action: txt too long; truncating.");
229 chop($rawout) while (length($rawout) > 510);
233 &status("* $ident/$target $txt");
237 # Usage: &action(nick || chan, txt);
239 my ($target, $txt) = @_;
241 &DEBUG("action: txt == NULL.");
245 &status("-$target- $txt");
247 $conn->notice($target, $txt);
252 my ($txt,$flag) = @_;
254 ### FIXME: flag not supported yet.
256 foreach (keys %{$dcc{'CHAT'}}) {
257 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
262 ### perform commands.
265 # Usage: &performReply($reply);
268 $reply =~ /([\.\?\s]+)$/;
270 &checkMsgType($reply);
272 if ($msgType eq 'public') {
273 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
274 $reply = "$orig{who}: ".$reply;
276 $reply = "$reply, ".$orig{who};
279 } elsif ($msgType eq 'private') {
283 $reply = "$reply, ".$orig{who};
286 } elsif ($msgType eq 'chat') {
287 if (!exists $dcc{'CHAT'}{$who}) {
288 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
291 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
293 &ERROR("PR: msgType invalid? ($msgType).");
298 sub performAddressedReply {
299 return unless ($addressed);
304 &performStrictReply(@_);
307 # Usage: &performStrictReply($reply);
308 sub performStrictReply {
311 &checkMsgType($reply);
313 if ($msgType eq 'private') {
315 } elsif ($msgType eq 'public') {
317 } elsif ($msgType eq 'chat') {
318 &dccsay(lc $who,$reply);
320 &ERROR("pSR: msgType invalid? ($msgType).");
325 my ($who, $reply) = @_;
326 if (!exists $dcc{'CHAT'}{$who}) {
327 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
331 &status("=>$who<= $reply"); # dcc chat.
332 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
339 foreach $type (keys %dcc) {
340 &FIXME("dcc_close: $who");
341 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
342 next unless (scalar @who);
349 my $chan = lc $chankey;
351 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
355 &status("joining $b_blue$chan$ob");
357 if (&validChan($chan)) {
358 &status("join: already on $chan");
360 if (!$conn->join($chan)) {
361 &DEBUG("join failed. trying connect!");
371 next if ($chan eq "");
372 $chan =~ tr/A-Z/a-z/; # lowercase.
374 &status("parting $chan");
375 if (!&validChan($chan)) {
376 &status("part: not on $chan");
380 rawout("PART $chan");
381 # deletion of $channels{chan} is done in &entryEvt().
386 my ($chan, @modes) = @_;
387 my $modes = join(" ", @modes);
389 if (&validChan($chan) == 0) {
390 &ERROR("mode: invalid chan => '$chan'.");
394 &DEBUG("MODE $chan $modes");
396 rawout("MODE $chan $modes");
400 my ($chan, @who) = @_;
401 my $os = "o" x scalar(@who);
403 &mode($chan, "+$os @who");
407 my ($chan, @who) = @_;
408 my $os = "o" x scalar(@who);
410 &mode($chan, "-$os ".@who);
414 my ($nick,$chan,$msg) = @_;
415 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
417 if ($chan ne "" and &validChan($chan) == 0) {
418 &ERROR("kick: invalid channel $chan.");
422 $nick =~ tr/A-Z/a-z/;
424 foreach $chan (@chans) {
425 if (!&IsNickInChan($nick,$chan)) {
426 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
430 if (!exists $channels{$chan}{o}{$ident}) {
431 &status("Kick: do not have ops on $chan :(");
435 &status("Kicking $nick from $chan.");
437 &rawout("KICK $chan $nick");
439 &rawout("KICK $chan $nick :$msg");
445 my ($mask,$chan) = @_;
446 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
449 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
450 &ERROR("ban: invalid channel $chan.");
454 foreach $chan (@chans) {
455 if (!exists $channels{$chan}{o}{$ident}) {
456 &status("Ban: do not have ops on $chan :(");
460 &status("Banning $mask from $chan.");
461 &rawout("MODE $chan +b $mask");
469 my ($mask,$chan) = @_;
470 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
473 &DEBUG("unban: mask = $mask, chan = @chans");
475 foreach $chan (@chans) {
476 if (!exists $channels{$chan}{o}{$ident}) {
477 &status("unBan: do not have ops on $chan :(");
481 &status("Removed ban $mask from $chan.");
482 &rawout("MODE $chan -b $mask");
491 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
492 $conn->quit($quitmsg);
498 if ($nick =~ /^$mask{nick}$/) {
499 rawout("NICK ".$nick);
507 my($who, $chan) = @_;
508 rawout("INVITE $who $chan");
513 # Channel related functions...
516 # Usage: &joinNextChan();
518 if (scalar @joinchan) {
519 my $chan = shift @joinchan;
522 if (my $i = scalar @joinchan) {
523 &status("joinNextChan: $i chans to join.");
528 if (&IsParam("nickServ_pass") and $nickserv < 1) {
529 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
533 my %chan = &getChanConfList("chanServ");
534 foreach $chan (keys %chan) {
535 next unless ($chan{$chan} > 0);
537 if (!exists $channels{$chan}{'o'}{$ident}) {
538 &status("ChanServ ==> Requesting ops for $chan.");
539 &rawout("PRIVMSG ChanServ :OP $chan $ident");
544 # Usage: &GetNickInChans($nick,$chan);
549 foreach (keys %channels) {
550 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
557 # Usage: &GetNicksInChan($chan);
562 return keys %{ $channels{$chan}{''} };
566 my ($nick,$chan) = @_;
568 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
570 if (&validChan($chan) == 0) {
571 &ERROR("INIC: invalid channel $chan.");
575 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
578 foreach (keys %channels) {
579 next unless (/[A-Z]/);
580 &DEBUG("hash channels contains mixed cased chan!!!");
586 sub IsNickInAnyChan {
589 foreach $chan (keys %channels) {
590 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
596 # Usage: &validChan($chan);
600 if (lc $chan ne $chan) {
601 &WARN("validChan: lc chan != chan. ($chan); fixing.");
602 $chan =~ tr/A-Z/a-z/;
605 if (exists $channels{$chan}) {
613 # Usage: &DeleteUserInfo($nick,@chans);
615 my ($nick,@chans) = @_;
618 foreach $chan (@chans) {
619 foreach $mode (keys %{$channels{$chan}}) {
621 next unless (exists $channels{$chan}{$mode}{$nick});
623 delete $channels{$chan}{$mode}{$nick};
631 delete $channels{$chan};
635 &DEBUG("clearIRCVars() called!");
639 @joinchan = &getJoinChans();
646 foreach (keys %chanconf) {
647 next if ($_ eq "_default");
649 my $val = $chanconf{$_}{autojoin};
653 $skip++ if ($val eq "0");
667 &status("gJC: channels not auto-joining: @skip");
669 &status("gJC: auto-joining all chans.");
676 &DEBUG("closeDCC called.");
678 foreach $type (keys %dcc) {
679 next if ($type ne uc($type));
681 foreach $nick (keys %{$dcc{$type}}) {
682 next unless (defined $nick);
683 &DEBUG("closing DCC $type to $nick (FIXME).");
684 next unless (defined $dcc{$type}{$nick});
686 $dcc{$type}{$nick}->close();
692 my($who,$chan,$userhost) = @_;
694 return unless (&IsChanConf("joinfloodCheck"));
696 if (exists $netsplit{lc $who}) { # netsplit join.
697 &DEBUG("jfC: $who was in netsnipe; not checking.");
700 if (exists $floodjoin{$chan}{$who}{Time}) {
701 &WARN("floodjoin{$chan}{$who} already exists?");
704 $floodjoin{$chan}{$who}{Time} = time();
705 $floodjoin{$chan}{$who}{Host} = $userhost;
708 foreach (keys %floodjoin) {
710 my $count = scalar keys %{ $floodjoin{$c} };
711 next unless ($count > 5);
712 &DEBUG("count => $count");
715 foreach (keys %{ $floodjoin{$c} }) {
716 $time += $floodjoin{$c}{$_}{Time};
718 &DEBUG("time => $time");
721 &DEBUG("new time => $time");
726 foreach $chan (keys %floodjoin) {
727 foreach $who (keys %{ $floodjoin{$chan} }) {
728 my $time = time() - $floodjoin{$chan}{$who}{Time};
729 next unless ($time > 10);
730 delete $floodjoin{$chan}{$who};
735 &DEBUG("jfC: $delete deleted.") if ($delete);
741 &FIXME("getHostMask...");