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);
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);
135 # end of handler stuff.
140 ######################################################################
141 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
142 ######################################################################
148 # slow down a bit if traffic is "high".
149 # need to take into account time of last message sent.
150 if ($last{buflen} > 256 and length($buf) > 256) {
154 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
156 $last{buflen} = length($buf);
163 &DEBUG("say: msg == $msg.");
167 if ($msg eq $last{say} and length($msg) > 256) {
168 &status("say: detected repeated message; skipping.");
173 &status("</$talkchannel> $msg");
174 if (&whatInterface() =~ /IRC/) {
175 $msg = "zero" if ($msg =~ /^0+$/);
177 $conn->privmsg($talkchannel, $msg);
182 my ($nick, $msg) = @_;
183 if (!defined $nick) {
184 &ERROR("msg: nick == NULL.");
190 &DEBUG("msg: msg == $msg.");
194 if ($msg eq $last{msg} and length($msg) > 256) {
195 &status("msg: detected repeated message; skipping.");
200 &status(">$nick< $msg");
201 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
204 # Usage: &action(nick || chan, txt);
206 my ($target, $txt) = @_;
208 &DEBUG("action: txt == NULL.");
212 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
213 if (length $rawout > 510) {
214 &status("action: txt too long; truncating.");
216 chop($rawout) while (length($rawout) > 510);
220 &status("* $ident/$target $txt");
224 # Usage: &action(nick || chan, txt);
226 my ($target, $txt) = @_;
228 &DEBUG("action: txt == NULL.");
232 &status("-$target- $txt");
234 $conn->notice($target, $txt);
239 my ($txt,$flag) = @_;
241 ### FIXME: flag not supported yet.
243 foreach (keys %{$dcc{'CHAT'}}) {
244 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
249 ### perform commands.
252 # Usage: &performReply($reply);
255 $reply =~ /([\.\?\s]+)$/;
257 &checkMsgType($reply);
259 if ($msgType eq 'public') {
260 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
261 $reply = "$orig{who}: ".$reply;
263 $reply = "$reply, ".$orig{who};
266 } elsif ($msgType eq 'private') {
270 $reply = "$reply, ".$orig{who};
273 } elsif ($msgType eq 'chat') {
274 if (!exists $dcc{'CHAT'}{$who}) {
275 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
278 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
280 &ERROR("PR: msgType invalid? ($msgType).");
285 sub performAddressedReply {
286 return unless ($addressed);
291 &performStrictReply(@_);
294 # Usage: &performStrictReply($reply);
295 sub performStrictReply {
298 &checkMsgType($reply);
300 if ($msgType eq 'private') {
302 } elsif ($msgType eq 'public') {
304 } elsif ($msgType eq 'chat') {
305 &dccsay($who,$reply);
307 &ERROR("pSR: msgType invalid? ($msgType).");
312 my ($who, $reply) = @_;
313 if (!exists $dcc{'CHAT'}{$who}) {
314 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
318 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
325 foreach $type (keys %dcc) {
326 &FIXME("dcc_close: $who");
327 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
328 next unless (scalar @who);
335 my $chan = lc $chankey;
337 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
341 &status("joining $b_blue$chan$ob");
343 if (&validChan($chan)) {
344 &status("join: already on $chan");
354 next if ($chan eq "");
355 $chan =~ tr/A-Z/a-z/; # lowercase.
357 &status("parting $chan");
358 if (!&validChan($chan)) {
359 &status("part: not on $chan");
363 rawout("PART $chan");
364 # deletion of $channels{chan} is done in &entryEvt().
369 my ($chan, @modes) = @_;
370 my $modes = join(" ", @modes);
372 if (&validChan($chan) == 0) {
373 &ERROR("mode: invalid chan => '$chan'.");
377 &DEBUG("MODE $chan $modes");
379 rawout("MODE $chan $modes");
383 my ($chan, @who) = @_;
384 my $os = "o" x scalar(@who);
386 &mode($chan, "+$os ".@who);
390 my ($chan, @who) = @_;
391 my $os = "o" x scalar(@who);
393 &mode($chan, "-$os ".@who);
397 my ($nick,$chan,$msg) = @_;
398 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
400 if ($chan ne "" and &validChan($chan) == 0) {
401 &ERROR("kick: invalid channel $chan.");
405 $nick =~ tr/A-Z/a-z/;
407 foreach $chan (@chans) {
408 if (!&IsNickInChan($nick,$chan)) {
409 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
413 if (!exists $channels{$chan}{o}{$ident}) {
414 &status("Kick: do not have ops on $chan :(");
418 &status("Kicking $nick from $chan.");
420 &rawout("KICK $chan $nick");
422 &rawout("KICK $chan $nick :$msg");
428 my ($mask,$chan) = @_;
429 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
431 if ($chan ne "" and &validChan($chan) == 0) {
432 &ERROR("ban: invalid channel $chan.");
436 $nick =~ tr/A-Z/a-z/;
438 foreach $chan (@chans) {
439 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
440 &status("Ban: $nick is not on $chan.");
444 if (!exists $channels{$chan}{o}{$ident}) {
445 &status("Ban: do not have ops on $chan :(");
449 &status("Banning $mask from $chan.");
450 &rawout("MODE $chan +b $mask");
456 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
457 $conn->quit($quitmsg);
463 if ($nick =~ /^$mask{nick}$/) {
464 rawout("NICK ".$nick);
472 my($who, $chan) = @_;
473 rawout("INVITE $who $chan");
478 # Channel related functions...
481 # Usage: &joinNextChan();
483 if (scalar @joinchan) {
484 my $chan = shift @joinchan;
487 if (my $i = scalar @joinchan) {
488 &status("joinNextChan: $i chans to join.");
491 return unless (&IsParam("chanServ_ops"));
493 &DEBUG("jNC: nickserv/chanserv not up?");
496 my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
497 foreach $chan (keys %channels) {
498 next unless (grep /^$chan$/i, @chans);
500 if (!exists $channels{$chan}{'o'}{$ident}) {
501 &status("ChanServ ==> Requesting ops for $chan.");
502 &rawout("PRIVMSG ChanServ :OP $chan $ident");
508 # Usage: &GetNickInChans($nick,$chan);
513 foreach (keys %channels) {
514 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
521 # Usage: &GetNicksInChan($chan);
526 return keys %{ $channels{$chan}{''} };
530 my ($nick,$chan) = @_;
532 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
534 if (&validChan($chan) == 0) {
535 &ERROR("INIC: invalid channel $chan.");
539 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
542 foreach (keys %channels) {
543 next unless (/[A-Z]/);
544 &DEBUG("hash channels contains mixed cased chan!!!");
550 sub IsNickInAnyChan {
553 foreach $chan (keys %channels) {
554 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
560 # Usage: &validChan($chan);
564 if (lc $chan ne $chan) {
565 &WARN("validChan: lc chan != chan. ($chan); fixing.");
566 $chan =~ tr/A-Z/a-z/;
569 if (exists $channels{$chan}) {
577 # Usage: &DeleteUserInfo($nick,@chans);
579 my ($nick,@chans) = @_;
582 foreach $chan (@chans) {
583 foreach $mode (keys %{$channels{$chan}}) {
585 next unless (exists $channels{$chan}{$mode}{$nick});
587 delete $channels{$chan}{$mode}{$nick};
595 delete $channels{$chan};
599 &DEBUG("clearIRCVars() called!");
603 @joinchan = &getJoinChans();
610 foreach (keys %chanconf) {
611 my $val = $chanconf{$_}{autojoin};
614 $skip++ if ($val eq "0");
628 &status("channels not auto-joining: @skip");
635 &DEBUG("closeDCC called.");
637 foreach $type (keys %dcc) {
638 next if ($type ne uc($type));
640 foreach (keys %{$dcc{$type}}) {
641 &DEBUG("closing DCC $type to $_ (FIXME).");
642 $dcc{$type}{$_}->close();
648 my($who,$chan,$userhost) = @_;
650 return unless (&IsParam("joinfloodCheck"));
652 if (exists $netsplit{lc $who}) { # netsplit join.
653 &DEBUG("jfC: $who was in netsnipe; not checking.");
656 if (exists $floodjoin{$chan}{$who}{Time}) {
657 &WARN("floodjoin{$chan}{$who} already exists?");
660 $floodjoin{$chan}{$who}{Time} = time();
661 $floodjoin{$chan}{$who}{Host} = $userhost;
664 foreach (keys %floodjoin) {
666 my $count = scalar keys %{ $floodjoin{$c} };
667 next unless ($count > 5);
668 &DEBUG("count => $count");
671 foreach (keys %{ $floodjoin{$c} }) {
672 $time += $floodjoin{$c}{$_}{Time};
674 &DEBUG("time => $time");
677 &DEBUG("new time => $time");
682 foreach $chan (keys %floodjoin) {
683 foreach $who (keys %{ $floodjoin{$chan} }) {
684 my $time = time() - $floodjoin{$chan}{$who}{Time};
685 next unless ($time > 10);
686 delete $floodjoin{$chan}{$who};
691 &DEBUG("jfC: $delete deleted.") if ($delete);
697 &FIXME("getHostMask...");