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 # static scalar variables.
11 $mask{ip} = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
12 $mask{host} = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
13 $mask{chan} = '[\#\&]\S*';
14 my $isnick1 = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
15 my $isnick2 = '0-9\-';
16 $mask{nick} = "[$isnick1]{1}[$isnick1$isnick2]*";
17 $mask{nuh} = '\S*!\S*\@\S*';
24 # JUST IN CASE. irq was complaining about this.
25 if ($lastrun == time()) {
26 &DEBUG("hrm... lastrun == time()");
32 foreach (@ircServers) {
34 &DEBUG("ircloop: ircServers[x] = NULL.");
38 next unless (exists $ircPort{$_});
40 my $retval = &irc($_, $ircPort{$_});
41 next unless (defined $retval and $retval == 0);
43 if ($error % 3 == 0 and $error != 0) {
44 &ERROR("CANNOT connect to this server; next!");
49 &ERROR("CANNOT connect to any irc server; stopping.");
57 my ($server,$port) = @_;
59 my $iaddr = inet_aton($server);
60 my $paddr = sockaddr_in($port, $iaddr);
61 my $proto = getprotobyname('tcp');
64 &status("Connecting to port $port of server $server ...");
67 if ($server =~ /\D$/) {
68 my $packed = scalar(gethostbyname($server));
70 if (!defined $packed) {
71 &status(" cannot resolve $server.");
75 my $resolve = inet_ntoa($packed);
76 &status(" resolved to $resolve.");
81 $conn = $irc->newconn(
82 Nick => $param{'ircNick'},
85 Ircname => $param{'ircName'},
86 LocalAddr => $param{'ircHost'},
90 &ERROR("irc: conn was not created!defined!!!");
96 # change internal timeout value for scheduler.
97 $irc->{_timeout} = 10; # how about 60?
99 $ircstats{'Server'} = "$server:$port";
102 $conn->add_handler('caction', \&on_action);
103 $conn->add_handler('cdcc', \&on_dcc);
104 $conn->add_handler('cping', \&on_ping);
105 $conn->add_handler('crping', \&on_ping_reply);
106 $conn->add_handler('cversion', \&on_version);
107 $conn->add_handler('crversion', \&on_crversion);
108 $conn->add_handler('dcc_open', \&on_dcc_open);
109 $conn->add_handler('dcc_close', \&on_dcc_close);
110 $conn->add_handler('chat', \&on_chat);
111 $conn->add_handler('msg', \&on_msg);
112 $conn->add_handler('public', \&on_public);
113 $conn->add_handler('join', \&on_join);
114 $conn->add_handler('part', \&on_part);
115 $conn->add_handler('topic', \&on_topic);
116 $conn->add_handler('invite', \&on_invite);
117 $conn->add_handler('kick', \&on_kick);
118 $conn->add_handler('mode', \&on_mode);
119 $conn->add_handler('nick', \&on_nick);
120 $conn->add_handler('quit', \&on_quit);
121 $conn->add_handler('notice', \&on_notice);
122 $conn->add_handler('whoisuser', \&on_whoisuser);
123 $conn->add_handler('other', \&on_other);
124 $conn->add_global_handler('disconnect', \&on_disconnect);
125 $conn->add_global_handler([251,252,253,254,255], \&on_init);
126 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
127 $conn->add_global_handler(324, \&on_modeis);
128 $conn->add_global_handler(333, \&on_topicinfo);
129 $conn->add_global_handler(352, \&on_who);
130 $conn->add_global_handler(353, \&on_names);
131 $conn->add_global_handler(366, \&on_endofnames);
132 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
133 $conn->add_global_handler(433, \&on_nick_taken);
134 $conn->add_global_handler(439, \&on_targettoofast);
136 # end of handler stuff.
141 ######################################################################
142 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
143 ######################################################################
149 # slow down a bit if traffic is "high".
150 # need to take into account time of last message sent.
151 if ($last{buflen} > 256 and length($buf) > 256) {
155 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
157 $last{buflen} = length($buf);
164 &DEBUG("say: msg == $msg.");
168 if ($msg eq $last{say} and length($msg) > 256) {
169 &status("say: detected repeated message; skipping.");
174 &status("</$talkchannel> $msg");
175 if (&whatInterface() =~ /IRC/) {
176 $msg = "zero" if ($msg =~ /^0+$/);
178 $conn->privmsg($talkchannel, $msg);
183 my ($nick, $msg) = @_;
184 if (!defined $nick) {
185 &ERROR("msg: nick == NULL.");
191 &DEBUG("msg: msg == $msg.");
195 if ($msg eq $last{msg} and length($msg) > 256) {
196 &status("msg: detected repeated message; skipping.");
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($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 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
326 foreach $type (keys %dcc) {
327 &FIXME("dcc_close: $who");
328 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
329 next unless (scalar @who);
336 my $chan = lc $chankey;
338 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
342 &status("joining $b_blue$chan$ob");
344 if (&validChan($chan)) {
345 &status("join: already on $chan");
355 next if ($chan eq "");
356 $chan =~ tr/A-Z/a-z/; # lowercase.
358 &status("parting $chan");
359 if (!&validChan($chan)) {
360 &status("part: not on $chan");
364 rawout("PART $chan");
365 # deletion of $channels{chan} is done in &entryEvt().
370 my ($chan, @modes) = @_;
371 my $modes = join(" ", @modes);
373 if (&validChan($chan) == 0) {
374 &ERROR("mode: invalid chan => '$chan'.");
378 &DEBUG("MODE $chan $modes");
380 rawout("MODE $chan $modes");
384 my ($chan, @who) = @_;
385 my $os = "o" x scalar(@who);
387 &mode($chan, "+$os ".@who);
391 my ($chan, @who) = @_;
392 my $os = "o" x scalar(@who);
394 &mode($chan, "-$os ".@who);
398 my ($nick,$chan,$msg) = @_;
399 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
401 if ($chan ne "" and &validChan($chan) == 0) {
402 &ERROR("kick: invalid channel $chan.");
406 $nick =~ tr/A-Z/a-z/;
408 foreach $chan (@chans) {
409 if (!&IsNickInChan($nick,$chan)) {
410 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
414 if (!exists $channels{$chan}{o}{$ident}) {
415 &status("Kick: do not have ops on $chan :(");
419 &status("Kicking $nick from $chan.");
421 &rawout("KICK $chan $nick");
423 &rawout("KICK $chan $nick :$msg");
429 my ($mask,$chan) = @_;
430 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
433 if ($chan ne "" and &validChan($chan) == 0) {
434 &ERROR("ban: invalid channel $chan.");
438 $nick =~ tr/A-Z/a-z/;
440 foreach $chan (@chans) {
441 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
442 &status("Ban: $nick is not on $chan.");
446 if (!exists $channels{$chan}{o}{$ident}) {
447 &status("Ban: do not have ops on $chan :(");
451 &status("Banning $mask from $chan.");
452 &rawout("MODE $chan +b $mask");
461 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
462 $conn->quit($quitmsg);
468 if ($nick =~ /^$mask{nick}$/) {
469 rawout("NICK ".$nick);
477 my($who, $chan) = @_;
478 rawout("INVITE $who $chan");
483 # Channel related functions...
486 # Usage: &joinNextChan();
488 if (scalar @joinchan) {
489 my $chan = shift @joinchan;
492 if (my $i = scalar @joinchan) {
493 &status("joinNextChan: $i chans to join.");
499 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
503 my %chan = &getChanConfList("chanServ");
504 foreach $chan (keys %chan) {
505 next unless ($chan{$chan} > 0);
507 if (!exists $channels{$chan}{'o'}{$ident}) {
508 &status("ChanServ ==> Requesting ops for $chan.");
509 &rawout("PRIVMSG ChanServ :OP $chan $ident");
514 # Usage: &GetNickInChans($nick,$chan);
519 foreach (keys %channels) {
520 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
527 # Usage: &GetNicksInChan($chan);
532 return keys %{ $channels{$chan}{''} };
536 my ($nick,$chan) = @_;
538 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
540 if (&validChan($chan) == 0) {
541 &ERROR("INIC: invalid channel $chan.");
545 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
548 foreach (keys %channels) {
549 next unless (/[A-Z]/);
550 &DEBUG("hash channels contains mixed cased chan!!!");
556 sub IsNickInAnyChan {
559 foreach $chan (keys %channels) {
560 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
566 # Usage: &validChan($chan);
570 if (lc $chan ne $chan) {
571 &WARN("validChan: lc chan != chan. ($chan); fixing.");
572 $chan =~ tr/A-Z/a-z/;
575 if (exists $channels{$chan}) {
583 # Usage: &DeleteUserInfo($nick,@chans);
585 my ($nick,@chans) = @_;
588 foreach $chan (@chans) {
589 foreach $mode (keys %{$channels{$chan}}) {
591 next unless (exists $channels{$chan}{$mode}{$nick});
593 delete $channels{$chan}{$mode}{$nick};
601 delete $channels{$chan};
605 &DEBUG("clearIRCVars() called!");
609 @joinchan = &getJoinChans();
616 foreach (keys %chanconf) {
617 my $val = $chanconf{$_}{autojoin};
620 $skip++ if ($val eq "0");
634 &status("channels not auto-joining: @skip");
641 &DEBUG("closeDCC called.");
643 foreach $type (keys %dcc) {
644 next if ($type ne uc($type));
646 foreach (keys %{$dcc{$type}}) {
647 &DEBUG("closing DCC $type to $_ (FIXME).");
648 $dcc{$type}{$_}->close();
654 my($who,$chan,$userhost) = @_;
656 return unless (&IsParam("joinfloodCheck"));
658 if (exists $netsplit{lc $who}) { # netsplit join.
659 &DEBUG("jfC: $who was in netsnipe; not checking.");
662 if (exists $floodjoin{$chan}{$who}{Time}) {
663 &WARN("floodjoin{$chan}{$who} already exists?");
666 $floodjoin{$chan}{$who}{Time} = time();
667 $floodjoin{$chan}{$who}{Host} = $userhost;
670 foreach (keys %floodjoin) {
672 my $count = scalar keys %{ $floodjoin{$c} };
673 next unless ($count > 5);
674 &DEBUG("count => $count");
677 foreach (keys %{ $floodjoin{$c} }) {
678 $time += $floodjoin{$c}{$_}{Time};
680 &DEBUG("time => $time");
683 &DEBUG("new time => $time");
688 foreach $chan (keys %floodjoin) {
689 foreach $who (keys %{ $floodjoin{$chan} }) {
690 my $time = time() - $floodjoin{$chan}{$who}{Time};
691 next unless ($time > 10);
692 delete $floodjoin{$chan}{$who};
697 &DEBUG("jfC: $delete deleted.") if ($delete);
703 &FIXME("getHostMask...");