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 # JUST IN CASE. irq was complaining about this.
28 if ($lastrun == time()) {
29 &DEBUG("hrm... lastrun == time()");
35 foreach (@ircServers) {
37 &DEBUG("ircloop: ircServers[x] = NULL.");
41 next unless (exists $ircPort{$_});
43 my $retval = &irc($_, $ircPort{$_});
44 next unless (defined $retval and $retval == 0);
46 if ($error % 3 == 0 and $error != 0) {
47 &ERROR("CANNOT connect to this server; next!");
52 &ERROR("CANNOT connect to any irc server; stopping.");
60 my ($server,$port) = @_;
62 my $iaddr = inet_aton($server);
63 my $paddr = sockaddr_in($port, $iaddr);
64 my $proto = getprotobyname('tcp');
67 &status("Connecting to port $port of server $server ...");
70 if ($server =~ /\D$/) {
71 my $packed = scalar(gethostbyname($server));
73 if (!defined $packed) {
74 &status(" cannot resolve $server.");
78 my $resolve = inet_ntoa($packed);
79 &status(" resolved to $resolve.");
80 ### warning in Sys/Hostname line 78???
81 ### caused inside Net::IRC?
86 $conn = $irc->newconn(
87 Nick => $param{'ircNick'},
90 Ircname => $param{'ircName'},
91 LocalAddr => $param{'ircHost'},
95 &ERROR("irc: conn was not created!defined!!!");
101 # change internal timeout value for scheduler.
102 $irc->{_timeout} = 10; # how about 60?
104 $ircstats{'Server'} = "$server:$port";
107 $conn->add_handler('caction', \&on_action);
108 $conn->add_handler('cdcc', \&on_dcc);
109 $conn->add_handler('cping', \&on_ping);
110 $conn->add_handler('crping', \&on_ping_reply);
111 $conn->add_handler('cversion', \&on_version);
112 $conn->add_handler('crversion', \&on_crversion);
113 $conn->add_handler('dcc_open', \&on_dcc_open);
114 $conn->add_handler('dcc_close', \&on_dcc_close);
115 $conn->add_handler('chat', \&on_chat);
116 $conn->add_handler('msg', \&on_msg);
117 $conn->add_handler('public', \&on_public);
118 $conn->add_handler('join', \&on_join);
119 $conn->add_handler('part', \&on_part);
120 $conn->add_handler('topic', \&on_topic);
121 $conn->add_handler('invite', \&on_invite);
122 $conn->add_handler('kick', \&on_kick);
123 $conn->add_handler('mode', \&on_mode);
124 $conn->add_handler('nick', \&on_nick);
125 $conn->add_handler('quit', \&on_quit);
126 $conn->add_handler('notice', \&on_notice);
127 $conn->add_handler('whoisuser', \&on_whoisuser);
128 $conn->add_handler('other', \&on_other);
129 $conn->add_global_handler('disconnect', \&on_disconnect);
130 $conn->add_global_handler([251,252,253,254,255], \&on_init);
131 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
132 $conn->add_global_handler(324, \&on_modeis);
133 $conn->add_global_handler(333, \&on_topicinfo);
134 $conn->add_global_handler(352, \&on_who);
135 $conn->add_global_handler(353, \&on_names);
136 $conn->add_global_handler(366, \&on_endofnames);
137 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
138 $conn->add_global_handler(433, \&on_nick_taken);
139 $conn->add_global_handler(439, \&on_targettoofast);
141 # end of handler stuff.
146 ######################################################################
147 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
148 ######################################################################
154 # slow down a bit if traffic is "high".
155 # need to take into account time of last message sent.
156 if ($last{buflen} > 256 and length($buf) > 256) {
160 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
162 $last{buflen} = length($buf);
169 &DEBUG("say: msg == $msg.");
173 if ($msg eq $last{say} and length($msg) > 256) {
174 &status("say: detected repeated message; skipping.");
179 &status("</$talkchannel> $msg");
180 if (&whatInterface() =~ /IRC/) {
181 $msg = "zero" if ($msg =~ /^0+$/);
183 $conn->privmsg($talkchannel, $msg);
188 my ($nick, $msg) = @_;
189 if (!defined $nick) {
190 &ERROR("msg: nick == NULL.");
196 &DEBUG("msg: msg == $msg.");
200 if ($msg eq $last{msg} and length($msg) > 256) {
201 &status("msg: detected repeated message; skipping.");
206 &status(">$nick< $msg");
207 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
210 # Usage: &action(nick || chan, txt);
212 my ($target, $txt) = @_;
214 &DEBUG("action: txt == NULL.");
218 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
219 if (length $rawout > 510) {
220 &status("action: txt too long; truncating.");
222 chop($rawout) while (length($rawout) > 510);
226 &status("* $ident/$target $txt");
230 # Usage: &action(nick || chan, txt);
232 my ($target, $txt) = @_;
234 &DEBUG("action: txt == NULL.");
238 &status("-$target- $txt");
240 $conn->notice($target, $txt);
245 my ($txt,$flag) = @_;
247 ### FIXME: flag not supported yet.
249 foreach (keys %{$dcc{'CHAT'}}) {
250 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
255 ### perform commands.
258 # Usage: &performReply($reply);
261 $reply =~ /([\.\?\s]+)$/;
263 &checkMsgType($reply);
265 if ($msgType eq 'public') {
266 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
267 $reply = "$orig{who}: ".$reply;
269 $reply = "$reply, ".$orig{who};
272 } elsif ($msgType eq 'private') {
276 $reply = "$reply, ".$orig{who};
279 } elsif ($msgType eq 'chat') {
280 if (!exists $dcc{'CHAT'}{$who}) {
281 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
284 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
286 &ERROR("PR: msgType invalid? ($msgType).");
291 sub performAddressedReply {
292 return unless ($addressed);
297 &performStrictReply(@_);
300 # Usage: &performStrictReply($reply);
301 sub performStrictReply {
304 &checkMsgType($reply);
306 if ($msgType eq 'private') {
308 } elsif ($msgType eq 'public') {
310 } elsif ($msgType eq 'chat') {
311 &dccsay($who,$reply);
313 &ERROR("pSR: msgType invalid? ($msgType).");
318 my ($who, $reply) = @_;
319 if (!exists $dcc{'CHAT'}{$who}) {
320 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
324 &status("=>$who<= $reply"); # dcc chat.
325 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
332 foreach $type (keys %dcc) {
333 &FIXME("dcc_close: $who");
334 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
335 next unless (scalar @who);
342 my $chan = lc $chankey;
344 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
348 &status("joining $b_blue$chan$ob");
350 if (&validChan($chan)) {
351 &status("join: already on $chan");
353 if (!$conn->join($chan)) {
354 &DEBUG("join failed. trying connect!");
364 next if ($chan eq "");
365 $chan =~ tr/A-Z/a-z/; # lowercase.
367 &status("parting $chan");
368 if (!&validChan($chan)) {
369 &status("part: not on $chan");
373 rawout("PART $chan");
374 # deletion of $channels{chan} is done in &entryEvt().
379 my ($chan, @modes) = @_;
380 my $modes = join(" ", @modes);
382 if (&validChan($chan) == 0) {
383 &ERROR("mode: invalid chan => '$chan'.");
387 &DEBUG("MODE $chan $modes");
389 rawout("MODE $chan $modes");
393 my ($chan, @who) = @_;
394 my $os = "o" x scalar(@who);
396 &mode($chan, "+$os ".@who);
400 my ($chan, @who) = @_;
401 my $os = "o" x scalar(@who);
403 &mode($chan, "-$os ".@who);
407 my ($nick,$chan,$msg) = @_;
408 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
410 if ($chan ne "" and &validChan($chan) == 0) {
411 &ERROR("kick: invalid channel $chan.");
415 $nick =~ tr/A-Z/a-z/;
417 foreach $chan (@chans) {
418 if (!&IsNickInChan($nick,$chan)) {
419 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
423 if (!exists $channels{$chan}{o}{$ident}) {
424 &status("Kick: do not have ops on $chan :(");
428 &status("Kicking $nick from $chan.");
430 &rawout("KICK $chan $nick");
432 &rawout("KICK $chan $nick :$msg");
438 my ($mask,$chan) = @_;
439 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
442 if ($chan ne "" and &validChan($chan) == 0) {
443 &ERROR("ban: invalid channel $chan.");
447 $nick =~ tr/A-Z/a-z/;
449 foreach $chan (@chans) {
450 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
451 &status("Ban: $nick is not on $chan.");
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");
470 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
471 $conn->quit($quitmsg);
477 if ($nick =~ /^$mask{nick}$/) {
478 rawout("NICK ".$nick);
486 my($who, $chan) = @_;
487 rawout("INVITE $who $chan");
492 # Channel related functions...
495 # Usage: &joinNextChan();
497 if (scalar @joinchan) {
498 my $chan = shift @joinchan;
501 if (my $i = scalar @joinchan) {
502 &status("joinNextChan: $i chans to join.");
507 if (&IsParam("nickServ_pass") and $nickserv < 1) {
508 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
512 my %chan = &getChanConfList("chanServ");
513 foreach $chan (keys %chan) {
514 next unless ($chan{$chan} > 0);
516 if (!exists $channels{$chan}{'o'}{$ident}) {
517 &status("ChanServ ==> Requesting ops for $chan.");
518 &rawout("PRIVMSG ChanServ :OP $chan $ident");
523 # Usage: &GetNickInChans($nick,$chan);
528 foreach (keys %channels) {
529 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
536 # Usage: &GetNicksInChan($chan);
541 return keys %{ $channels{$chan}{''} };
545 my ($nick,$chan) = @_;
547 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
549 if (&validChan($chan) == 0) {
550 &ERROR("INIC: invalid channel $chan.");
554 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
557 foreach (keys %channels) {
558 next unless (/[A-Z]/);
559 &DEBUG("hash channels contains mixed cased chan!!!");
565 sub IsNickInAnyChan {
568 foreach $chan (keys %channels) {
569 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
575 # Usage: &validChan($chan);
579 if (lc $chan ne $chan) {
580 &WARN("validChan: lc chan != chan. ($chan); fixing.");
581 $chan =~ tr/A-Z/a-z/;
584 if (exists $channels{$chan}) {
592 # Usage: &DeleteUserInfo($nick,@chans);
594 my ($nick,@chans) = @_;
597 foreach $chan (@chans) {
598 foreach $mode (keys %{$channels{$chan}}) {
600 next unless (exists $channels{$chan}{$mode}{$nick});
602 delete $channels{$chan}{$mode}{$nick};
610 delete $channels{$chan};
614 &DEBUG("clearIRCVars() called!");
618 @joinchan = &getJoinChans();
625 foreach (keys %chanconf) {
626 next if ($_ eq "_default");
628 my $val = $chanconf{$_}{autojoin};
632 $skip++ if ($val eq "0");
646 &status("gJC: channels not auto-joining: @skip");
648 &status("gJC: auto-joining all chans.");
655 &DEBUG("closeDCC called.");
657 foreach $type (keys %dcc) {
658 next if ($type ne uc($type));
660 foreach $nick (keys %{$dcc{$type}}) {
661 next unless (defined $nick);
662 &DEBUG("closing DCC $type to $nick (FIXME).");
663 next unless (defined $dcc{$type}{$nick});
665 $dcc{$type}{$nick}->close();
671 my($who,$chan,$userhost) = @_;
673 return unless (&IsChanConf("joinfloodCheck"));
675 if (exists $netsplit{lc $who}) { # netsplit join.
676 &DEBUG("jfC: $who was in netsnipe; not checking.");
679 if (exists $floodjoin{$chan}{$who}{Time}) {
680 &WARN("floodjoin{$chan}{$who} already exists?");
683 $floodjoin{$chan}{$who}{Time} = time();
684 $floodjoin{$chan}{$who}{Host} = $userhost;
687 foreach (keys %floodjoin) {
689 my $count = scalar keys %{ $floodjoin{$c} };
690 next unless ($count > 5);
691 &DEBUG("count => $count");
694 foreach (keys %{ $floodjoin{$c} }) {
695 $time += $floodjoin{$c}{$_}{Time};
697 &DEBUG("time => $time");
700 &DEBUG("new time => $time");
705 foreach $chan (keys %floodjoin) {
706 foreach $who (keys %{ $floodjoin{$chan} }) {
707 my $time = time() - $floodjoin{$chan}{$who}{Time};
708 next unless ($time > 10);
709 delete $floodjoin{$chan}{$who};
714 &DEBUG("jfC: $delete deleted.") if ($delete);
720 &FIXME("getHostMask...");