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).");
307 my ($who, $reply) = @_;
308 if (!exists $dcc{'CHAT'}{$who}) {
309 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
313 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
320 foreach $type (keys %dcc) {
321 &FIXME("dcc_close: $who");
322 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
323 next unless (scalar @who);
330 my $chan = lc $chankey;
332 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
336 &status("joining $b_blue$chan$ob");
338 if (&validChan($chan)) {
339 &status("join: already on $chan");
349 next if ($chan eq "");
350 $chan =~ tr/A-Z/a-z/; # lowercase.
352 &status("parting $chan");
353 if (!&validChan($chan)) {
354 &status("part: not on $chan");
358 rawout("PART $chan");
359 # deletion of $channels{chan} is done in &entryEvt().
364 my ($chan, @modes) = @_;
365 my $modes = join(" ", @modes);
367 if (&validChan($chan) == 0) {
368 &ERROR("mode: invalid chan => '$chan'.");
372 &DEBUG("MODE $chan $modes");
374 rawout("MODE $chan $modes");
378 my ($chan, @who) = @_;
379 my $os = "o" x scalar(@who);
381 &mode($chan, "+$os ".@who);
385 my ($chan, @who) = @_;
386 my $os = "o" x scalar(@who);
388 &mode($chan, "-$os ".@who);
392 my ($nick,$chan,$msg) = @_;
393 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
395 if ($chan ne "" and &validChan($chan) == 0) {
396 &ERROR("kick: invalid channel $chan.");
400 $nick =~ tr/A-Z/a-z/;
402 foreach $chan (@chans) {
403 if (!&IsNickInChan($nick,$chan)) {
404 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
408 if (!exists $channels{$chan}{o}{$ident}) {
409 &status("Kick: do not have ops on $chan :(");
413 &status("Kicking $nick from $chan.");
415 &rawout("KICK $chan $nick");
417 &rawout("KICK $chan $nick :$msg");
423 my ($mask,$chan) = @_;
424 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
426 if ($chan ne "" and &validChan($chan) == 0) {
427 &ERROR("ban: invalid channel $chan.");
431 $nick =~ tr/A-Z/a-z/;
433 foreach $chan (@chans) {
434 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
435 &status("Ban: $nick is not on $chan.");
439 if (!exists $channels{$chan}{o}{$ident}) {
440 &status("Ban: do not have ops on $chan :(");
444 &status("Banning $mask from $chan.");
445 &rawout("MODE $chan +b $mask");
451 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
452 $conn->quit($quitmsg);
458 if ($nick =~ /^$mask{nick}$/) {
459 rawout("NICK ".$nick);
467 my($who, $chan) = @_;
468 rawout("INVITE $who $chan");
473 # Channel related functions...
476 # Usage: &joinNextChan();
478 if (scalar @joinchan) {
479 my $chan = shift @joinchan;
482 if (my $i = scalar @joinchan) {
483 &status("joinNextChan: $i chans to join.");
486 return unless (&IsParam("chanServ_ops"));
488 &DEBUG("jNC: nickserv/chanserv not up?");
491 my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
492 foreach $chan (keys %channels) {
493 next unless (grep /^$chan$/i, @chans);
495 if (!exists $channels{$chan}{'o'}{$ident}) {
496 &status("ChanServ ==> Requesting ops for $chan.");
497 &rawout("PRIVMSG ChanServ :OP $chan $ident");
503 # Usage: &GetNickInChans($nick,$chan);
508 foreach (keys %channels) {
509 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
517 my ($nick,$chan) = @_;
519 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
521 if (&validChan($chan) == 0) {
522 &ERROR("INIC: invalid channel $chan.");
526 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
529 foreach (keys %channels) {
530 next unless (/[A-Z]/);
531 &DEBUG("hash channels contains mixed cased chan!!!");
537 sub IsNickInAnyChan {
540 foreach $chan (keys %channels) {
541 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
547 # Usage: &validChan($chan);
551 if (lc $chan ne $chan) {
552 &WARN("validChan: lc chan != chan. ($chan); fixing.");
553 $chan =~ tr/A-Z/a-z/;
556 if (exists $channels{$chan}) {
564 # Usage: &DeleteUserInfo($nick,@chans);
566 my ($nick,@chans) = @_;
569 foreach $chan (@chans) {
570 foreach $mode (keys %{$channels{$chan}}) {
572 next unless (exists $channels{$chan}{$mode}{$nick});
574 delete $channels{$chan}{$mode}{$nick};
582 delete $channels{$chan};
586 &DEBUG("clearIRCVars() called!");
589 @joinchan = split /[\t\s]+/, $param{'join_channels'};
598 return(keys %channels);
599 } elsif ($str =~ s/^ALL but //i) {
600 @chans = keys %channels;
601 foreach (split /[\s\t\,]+/, lc $str) {
602 @chans = grep !/^$_$/, @chans;
605 foreach (split /[\s\t\,]+/, lc $str) {
606 next unless (&validChan($_));
615 &DEBUG("closeDCC called.");
617 foreach $type (keys %dcc) {
618 next if ($type ne uc($type));
620 foreach (keys %{$dcc{$type}}) {
621 &DEBUG("closing DCC $type to $_ (FIXME).");
622 $dcc{$type}{$_}->close();
628 my($who,$chan,$userhost) = @_;
630 return unless (&IsParam("joinfloodCheck"));
632 if (exists $netsplit{lc $who}) { # netsplit join.
633 &DEBUG("jfC: $who was in netsnipe; not checking.");
636 if (exists $floodjoin{$chan}{$who}{Time}) {
637 &WARN("floodjoin{$chan}{$who} already exists?");
640 $floodjoin{$chan}{$who}{Time} = time();
641 $floodjoin{$chan}{$who}{Host} = $userhost;
644 foreach (keys %floodjoin) {
646 my $count = scalar keys %{ $floodjoin{$c} };
647 next unless ($count > 5);
648 &DEBUG("count => $count");
651 foreach (keys %{ $floodjoin{$c} }) {
652 $time += $floodjoin{$c}{$_}{Time};
654 &DEBUG("time => $time");
657 &DEBUG("new time => $time");
662 foreach $chan (keys %floodjoin) {
663 foreach $who (keys %{ $floodjoin{$chan} }) {
664 my $time = time() - $floodjoin{$chan}{$who}{Time};
665 next unless ($time > 10);
666 delete $floodjoin{$chan}{$who};
671 &DEBUG("jfC: $delete deleted.") if ($delete);