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]*";
23 # JUST IN CASE. irq was complaining about this.
24 if ($lastrun == time()) {
25 &DEBUG("hrm... lastrun == time()");
31 foreach (@ircServers) {
33 &DEBUG("ircloop: ircServers[x] = NULL.");
37 next unless (exists $ircPort{$_});
39 my $retval = &irc($_, $ircPort{$_});
40 next unless (defined $retval and $retval == 0);
42 if ($error % 3 == 0 and $error != 0) {
43 &ERROR("CANNOT connect to this server; next!");
48 &ERROR("CANNOT connect to any irc server; stopping.");
56 my ($server,$port) = @_;
58 my $iaddr = inet_aton($server);
59 my $paddr = sockaddr_in($port, $iaddr);
60 my $proto = getprotobyname('tcp');
63 &status("Connecting to port $port of server $server ...");
66 if ($server =~ /\D$/) {
67 my $packed = scalar(gethostbyname($server));
69 if (!defined $packed) {
70 &status(" cannot resolve $server.");
74 my $resolve = inet_ntoa($packed);
75 &status(" resolved to $resolve.");
80 $conn = $irc->newconn(
81 Nick => $param{'ircNick'},
84 Ircname => $param{'ircName'},
85 LocalAddr => $param{'ircHost'},
89 &ERROR("irc: conn was not created!defined!!!");
95 # change internal timeout value for scheduler.
96 $irc->{_timeout} = 10; # how about 60?
98 $ircstats{'Server'} = "$server:$port";
101 $conn->add_handler('caction', \&on_action);
102 $conn->add_handler('cdcc', \&on_dcc);
103 $conn->add_handler('cping', \&on_ping);
104 $conn->add_handler('crping', \&on_ping_reply);
105 $conn->add_handler('cversion', \&on_version);
107 $conn->add_handler('dcc_open', \&on_dcc_open);
108 $conn->add_handler('dcc_close', \&on_dcc_close);
109 $conn->add_handler('chat', \&on_chat);
110 $conn->add_handler('msg', \&on_msg);
111 $conn->add_handler('public', \&on_public);
112 $conn->add_handler('join', \&on_join);
113 $conn->add_handler('part', \&on_part);
114 $conn->add_handler('topic', \&on_topic);
115 $conn->add_handler('invite', \&on_invite);
116 $conn->add_handler('kick', \&on_kick);
117 $conn->add_handler('mode', \&on_mode);
118 $conn->add_handler('nick', \&on_nick);
119 $conn->add_handler('quit', \&on_quit);
120 $conn->add_handler('notice', \&on_notice);
121 $conn->add_handler('whoisuser', \&on_whoisuser);
122 $conn->add_handler('other', \&on_other);
123 $conn->add_global_handler('disconnect', \&on_disconnect);
124 $conn->add_global_handler([251,252,253,254,255], \&on_init);
125 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
126 $conn->add_global_handler(324, \&on_modeis);
127 $conn->add_global_handler(333, \&on_topicinfo);
128 $conn->add_global_handler(352, \&on_who);
129 $conn->add_global_handler(353, \&on_names);
130 $conn->add_global_handler(366, \&on_endofnames);
131 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
132 $conn->add_global_handler(433, \&on_nick_taken);
133 $conn->add_global_handler(439, \&on_targettoofast);
134 # end of handler stuff.
139 ######################################################################
140 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
141 ######################################################################
147 # slow down a bit if traffic is "high".
148 # need to take into account time of last message sent.
149 if ($last{buflen} > 256 and length($buf) > 256) {
153 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
155 $last{buflen} = length($buf);
160 if (!defined $msg or $msg eq $noreply) {
162 &DEBUG("say: msg == $msg.");
166 if ($msg eq $last{say} and length($msg) > 256) {
167 &status("say: detected repeated message; skipping.");
172 &status("</$talkchannel> $msg");
173 if (&whatInterface() =~ /IRC/) {
174 $msg = "zero" if ($msg =~ /^0+$/);
176 $conn->privmsg($talkchannel, $msg);
181 my ($nick, $msg) = @_;
182 if (!defined $nick) {
183 &ERROR("msg: nick == NULL.");
187 if (!defined $msg or $msg eq $noreply) {
189 &DEBUG("msg: msg == $msg.");
193 if ($msg eq $last{msg} and length($msg) > 256) {
194 &status("msg: detected repeated message; skipping.");
199 &status(">$nick< $msg");
200 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
203 # Usage: &action(nick || chan, txt);
205 my ($target, $txt) = @_;
207 &DEBUG("action: txt == NULL.");
211 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
212 if (length $rawout > 510) {
213 &status("action: txt too long; truncating.");
215 chop($rawout) while (length($rawout) > 510);
219 &status("* $ident/$target $txt");
223 # Usage: &action(nick || chan, txt);
225 my ($target, $txt) = @_;
227 &DEBUG("action: txt == NULL.");
231 &status("-$target- $txt");
233 $conn->notice($target, $txt);
240 foreach (keys %{$dcc{'CHAT'}}) {
241 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
246 ### perform commands.
249 # Usage: &performReply($reply);
252 $reply =~ /([\.\?\s]+)$/;
254 &checkMsgType($reply);
256 if ($msgType eq 'public') {
257 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
258 $reply = "$orig{who}: ".$reply;
260 $reply = "$reply, ".$orig{who};
263 } elsif ($msgType eq 'private') {
267 $reply = "$reply, ".$orig{who};
270 } elsif ($msgType eq 'chat') {
271 if (!exists $dcc{'CHAT'}{$who}) {
272 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
275 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
277 &ERROR("PR: msgType invalid? ($msgType).");
282 sub performAddressedReply {
283 return unless ($addressed);
287 # Usage: &performStrictReply($reply);
288 sub performStrictReply {
291 &checkMsgType($reply);
293 if ($msgType eq 'private') {
295 } elsif ($msgType eq 'public') {
297 } elsif ($msgType eq 'chat') {
298 &dccsay($who,$reply);
300 &ERROR("pSR: msgType invalid? ($msgType).");
305 my ($who, $reply) = @_;
306 if (!exists $dcc{'CHAT'}{$who}) {
307 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
311 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
318 foreach $type (keys %dcc) {
319 &FIXME("dcc_close: $who");
320 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
321 next unless (scalar @who);
328 my $chan = lc $chankey;
330 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
334 &status("joining $b_blue$chan$ob");
336 if (&validChan($chan)) {
337 &status("join: already on $chan");
347 next if ($chan eq "");
348 $chan =~ tr/A-Z/a-z/; # lowercase.
350 &status("parting $chan");
351 if (!&validChan($chan)) {
352 &status("part: not on $chan");
356 rawout("PART $chan");
357 # deletion of $channels{chan} is done in &entryEvt().
362 my ($chan, @modes) = @_;
363 my $modes = join(" ", @modes);
365 if (&validChan($chan) == 0) {
366 &ERROR("mode: invalid chan => '$chan'.");
370 &DEBUG("MODE $chan $modes");
372 rawout("MODE $chan $modes");
376 my ($chan, @who) = @_;
377 my $os = "o" x scalar(@who);
379 &mode($chan, "+$os ".@who);
383 my ($chan, @who) = @_;
384 my $os = "o" x scalar(@who);
386 &mode($chan, "-$os ".@who);
390 my ($nick,$chan,$msg) = @_;
391 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
393 if ($chan ne "" and &validChan($chan) == 0) {
394 &ERROR("kick: invalid channel $chan.");
398 $nick =~ tr/A-Z/a-z/;
400 foreach $chan (@chans) {
401 if (!&IsNickInChan($nick,$chan)) {
402 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
406 if (!exists $channels{$chan}{o}{$ident}) {
407 &status("Kick: do not have ops on $chan :(");
411 &status("Kicking $nick from $chan.");
413 &rawout("KICK $chan $nick");
415 &rawout("KICK $chan $nick :$msg");
421 my ($mask,$chan) = @_;
422 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
424 if ($chan ne "" and &validChan($chan) == 0) {
425 &ERROR("ban: invalid channel $chan.");
429 $nick =~ tr/A-Z/a-z/;
431 foreach $chan (@chans) {
432 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
433 &status("Ban: $nick is not on $chan.");
437 if (!exists $channels{$chan}{o}{$ident}) {
438 &status("Ban: do not have ops on $chan :(");
442 &status("Banning $mask from $chan.");
443 &rawout("MODE $chan +b $mask");
449 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
450 $conn->quit($quitmsg);
456 if ($nick =~ /^$mask{nick}$/) {
457 rawout("NICK ".$nick);
465 my($who, $chan) = @_;
466 rawout("INVITE $who $chan");
471 # Channel related functions...
474 # Usage: &joinNextChan();
476 if (scalar @joinchan) {
477 my $chan = shift @joinchan;
480 if (my $i = scalar @joinchan) {
481 &status("joinNextChan: $i chans to join.");
484 return unless (&IsParam("chanServ_ops"));
486 &DEBUG("jNC: nickserv/chanserv not up?");
489 my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
490 foreach $chan (keys %channels) {
491 next unless (grep /^$chan$/i, @chans);
493 if (!exists $channels{$chan}{'o'}{$ident}) {
494 &status("ChanServ ==> Requesting ops for $chan.");
495 &rawout("PRIVMSG ChanServ :OP $chan $ident");
501 # Usage: &GetNickInChans($nick,$chan);
506 foreach (keys %channels) {
507 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
515 my ($nick,$chan) = @_;
517 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
519 if (&validChan($chan) == 0) {
520 &ERROR("INIC: invalid channel $chan.");
524 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
527 foreach (keys %channels) {
528 next unless (/[A-Z]/);
529 &DEBUG("hash channels contains mixed cased chan!!!");
535 sub IsNickInAnyChan {
538 foreach $chan (keys %channels) {
539 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
545 # Usage: &validChan($chan);
549 if (lc $chan ne $chan) {
550 &WARN("validChan: lc chan != chan. ($chan); fixing.");
551 $chan =~ tr/A-Z/a-z/;
554 if (exists $channels{$chan}) {
562 # Usage: &DeleteUserInfo($nick,@chans);
564 my ($nick,@chans) = @_;
567 foreach $chan (@chans) {
568 foreach $mode (keys %{$channels{$chan}}) {
570 next unless (exists $channels{$chan}{$mode}{$nick});
572 delete $channels{$chan}{$mode}{$nick};
580 delete $channels{$chan};
584 &DEBUG("clearIRCVars() called!");
587 @joinchan = split /[\t\s]+/, $param{'join_channels'};
596 return(keys %channels);
597 } elsif ($str =~ s/^ALL but //i) {
598 @chans = keys %channels;
599 foreach (split /[\s\t\,]+/, lc $str) {
600 @chans = grep !/^$_$/, @chans;
603 foreach (split /[\s\t\,]+/, lc $str) {
604 next unless (&validChan($_));
613 &DEBUG("closeDCC called.");
615 foreach $type (keys %dcc) {
616 next if ($type ne uc($type));
618 foreach (keys %{$dcc{$type}}) {
619 &DEBUG("closing DCC $type to $_ (FIXME).");
620 $dcc{$type}{$_}->close();
626 my($who,$chan,$userhost) = @_;
628 return unless (&IsParam("joinfloodCheck"));
630 if (exists $netsplit{lc $who}) { # netsplit join.
631 &DEBUG("jfC: $who was in netsnipe; not checking.");
634 if (exists $floodjoin{$chan}{$who}{Time}) {
635 &WARN("floodjoin{$chan}{$who} already exists?");
638 $floodjoin{$chan}{$who}{Time} = time();
639 $floodjoin{$chan}{$who}{Host} = $userhost;
642 foreach (keys %floodjoin) {
644 my $count = scalar keys %{ $floodjoin{$c} };
645 next unless ($count > 5);
646 &DEBUG("count => $count");
649 foreach (keys %{ $floodjoin{$c} }) {
650 $time += $floodjoin{$c}{$_}{Time};
652 &DEBUG("time => $time");
655 &DEBUG("new time => $time");
660 foreach $chan (keys %floodjoin) {
661 foreach $who (keys %{ $floodjoin{$chan} }) {
662 my $time = time() - $floodjoin{$chan}{$who}{Time};
663 next unless ($time > 10);
664 delete $floodjoin{$chan}{$who};
669 &DEBUG("jfC: $delete deleted.") if ($delete);