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*';
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.");
84 $conn = $irc->newconn(
85 Nick => $param{'ircNick'},
88 Ircname => $param{'ircName'},
89 LocalAddr => $param{'ircHost'},
93 &ERROR("irc: conn was not created!defined!!!");
99 # change internal timeout value for scheduler.
100 $irc->{_timeout} = 10; # how about 60?
102 $ircstats{'Server'} = "$server:$port";
105 $conn->add_handler('caction', \&on_action);
106 $conn->add_handler('cdcc', \&on_dcc);
107 $conn->add_handler('cping', \&on_ping);
108 $conn->add_handler('crping', \&on_ping_reply);
109 $conn->add_handler('cversion', \&on_version);
110 $conn->add_handler('crversion', \&on_crversion);
111 $conn->add_handler('dcc_open', \&on_dcc_open);
112 $conn->add_handler('dcc_close', \&on_dcc_close);
113 $conn->add_handler('chat', \&on_chat);
114 $conn->add_handler('msg', \&on_msg);
115 $conn->add_handler('public', \&on_public);
116 $conn->add_handler('join', \&on_join);
117 $conn->add_handler('part', \&on_part);
118 $conn->add_handler('topic', \&on_topic);
119 $conn->add_handler('invite', \&on_invite);
120 $conn->add_handler('kick', \&on_kick);
121 $conn->add_handler('mode', \&on_mode);
122 $conn->add_handler('nick', \&on_nick);
123 $conn->add_handler('quit', \&on_quit);
124 $conn->add_handler('notice', \&on_notice);
125 $conn->add_handler('whoisuser', \&on_whoisuser);
126 $conn->add_handler('other', \&on_other);
127 $conn->add_global_handler('disconnect', \&on_disconnect);
128 $conn->add_global_handler([251,252,253,254,255], \&on_init);
129 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
130 $conn->add_global_handler(324, \&on_modeis);
131 $conn->add_global_handler(333, \&on_topicinfo);
132 $conn->add_global_handler(352, \&on_who);
133 $conn->add_global_handler(353, \&on_names);
134 $conn->add_global_handler(366, \&on_endofnames);
135 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
136 $conn->add_global_handler(433, \&on_nick_taken);
137 $conn->add_global_handler(439, \&on_targettoofast);
139 # end of handler stuff.
144 ######################################################################
145 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
146 ######################################################################
152 # slow down a bit if traffic is "high".
153 # need to take into account time of last message sent.
154 if ($last{buflen} > 256 and length($buf) > 256) {
158 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
160 $last{buflen} = length($buf);
167 &DEBUG("say: msg == $msg.");
171 if ($msg eq $last{say} and length($msg) > 256) {
172 &status("say: detected repeated message; skipping.");
177 &status("</$talkchannel> $msg");
178 if (&whatInterface() =~ /IRC/) {
179 $msg = "zero" if ($msg =~ /^0+$/);
181 $conn->privmsg($talkchannel, $msg);
186 my ($nick, $msg) = @_;
187 if (!defined $nick) {
188 &ERROR("msg: nick == NULL.");
194 &DEBUG("msg: msg == $msg.");
198 if ($msg eq $last{msg} and length($msg) > 256) {
199 &status("msg: detected repeated message; skipping.");
204 &status(">$nick< $msg");
205 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
208 # Usage: &action(nick || chan, txt);
210 my ($target, $txt) = @_;
212 &DEBUG("action: txt == NULL.");
216 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
217 if (length $rawout > 510) {
218 &status("action: txt too long; truncating.");
220 chop($rawout) while (length($rawout) > 510);
224 &status("* $ident/$target $txt");
228 # Usage: &action(nick || chan, txt);
230 my ($target, $txt) = @_;
232 &DEBUG("action: txt == NULL.");
236 &status("-$target- $txt");
238 $conn->notice($target, $txt);
243 my ($txt,$flag) = @_;
245 ### FIXME: flag not supported yet.
247 foreach (keys %{$dcc{'CHAT'}}) {
248 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
253 ### perform commands.
256 # Usage: &performReply($reply);
259 $reply =~ /([\.\?\s]+)$/;
261 &checkMsgType($reply);
263 if ($msgType eq 'public') {
264 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
265 $reply = "$orig{who}: ".$reply;
267 $reply = "$reply, ".$orig{who};
270 } elsif ($msgType eq 'private') {
274 $reply = "$reply, ".$orig{who};
277 } elsif ($msgType eq 'chat') {
278 if (!exists $dcc{'CHAT'}{$who}) {
279 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
282 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
284 &ERROR("PR: msgType invalid? ($msgType).");
289 sub performAddressedReply {
290 return unless ($addressed);
295 &performStrictReply(@_);
298 # Usage: &performStrictReply($reply);
299 sub performStrictReply {
302 &checkMsgType($reply);
304 if ($msgType eq 'private') {
306 } elsif ($msgType eq 'public') {
308 } elsif ($msgType eq 'chat') {
309 &dccsay($who,$reply);
311 &ERROR("pSR: msgType invalid? ($msgType).");
316 my ($who, $reply) = @_;
317 if (!exists $dcc{'CHAT'}{$who}) {
318 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
322 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
329 foreach $type (keys %dcc) {
330 &FIXME("dcc_close: $who");
331 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
332 next unless (scalar @who);
339 my $chan = lc $chankey;
341 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
345 &status("joining $b_blue$chan$ob");
347 if (&validChan($chan)) {
348 &status("join: already on $chan");
358 next if ($chan eq "");
359 $chan =~ tr/A-Z/a-z/; # lowercase.
361 &status("parting $chan");
362 if (!&validChan($chan)) {
363 &status("part: not on $chan");
367 rawout("PART $chan");
368 # deletion of $channels{chan} is done in &entryEvt().
373 my ($chan, @modes) = @_;
374 my $modes = join(" ", @modes);
376 if (&validChan($chan) == 0) {
377 &ERROR("mode: invalid chan => '$chan'.");
381 &DEBUG("MODE $chan $modes");
383 rawout("MODE $chan $modes");
387 my ($chan, @who) = @_;
388 my $os = "o" x scalar(@who);
390 &mode($chan, "+$os ".@who);
394 my ($chan, @who) = @_;
395 my $os = "o" x scalar(@who);
397 &mode($chan, "-$os ".@who);
401 my ($nick,$chan,$msg) = @_;
402 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
404 if ($chan ne "" and &validChan($chan) == 0) {
405 &ERROR("kick: invalid channel $chan.");
409 $nick =~ tr/A-Z/a-z/;
411 foreach $chan (@chans) {
412 if (!&IsNickInChan($nick,$chan)) {
413 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
417 if (!exists $channels{$chan}{o}{$ident}) {
418 &status("Kick: do not have ops on $chan :(");
422 &status("Kicking $nick from $chan.");
424 &rawout("KICK $chan $nick");
426 &rawout("KICK $chan $nick :$msg");
432 my ($mask,$chan) = @_;
433 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
436 if ($chan ne "" and &validChan($chan) == 0) {
437 &ERROR("ban: invalid channel $chan.");
441 $nick =~ tr/A-Z/a-z/;
443 foreach $chan (@chans) {
444 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
445 &status("Ban: $nick is not on $chan.");
449 if (!exists $channels{$chan}{o}{$ident}) {
450 &status("Ban: do not have ops on $chan :(");
454 &status("Banning $mask from $chan.");
455 &rawout("MODE $chan +b $mask");
464 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
465 $conn->quit($quitmsg);
471 if ($nick =~ /^$mask{nick}$/) {
472 rawout("NICK ".$nick);
480 my($who, $chan) = @_;
481 rawout("INVITE $who $chan");
486 # Channel related functions...
489 # Usage: &joinNextChan();
491 if (scalar @joinchan) {
492 my $chan = shift @joinchan;
495 if (my $i = scalar @joinchan) {
496 &status("joinNextChan: $i chans to join.");
502 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
506 my %chan = &getChanConfList("chanServ");
507 foreach $chan (keys %chan) {
508 next unless ($chan{$chan} > 0);
510 if (!exists $channels{$chan}{'o'}{$ident}) {
511 &status("ChanServ ==> Requesting ops for $chan.");
512 &rawout("PRIVMSG ChanServ :OP $chan $ident");
517 # Usage: &GetNickInChans($nick,$chan);
522 foreach (keys %channels) {
523 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
530 # Usage: &GetNicksInChan($chan);
535 return keys %{ $channels{$chan}{''} };
539 my ($nick,$chan) = @_;
541 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
543 if (&validChan($chan) == 0) {
544 &ERROR("INIC: invalid channel $chan.");
548 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
551 foreach (keys %channels) {
552 next unless (/[A-Z]/);
553 &DEBUG("hash channels contains mixed cased chan!!!");
559 sub IsNickInAnyChan {
562 foreach $chan (keys %channels) {
563 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
569 # Usage: &validChan($chan);
573 if (lc $chan ne $chan) {
574 &WARN("validChan: lc chan != chan. ($chan); fixing.");
575 $chan =~ tr/A-Z/a-z/;
578 if (exists $channels{$chan}) {
586 # Usage: &DeleteUserInfo($nick,@chans);
588 my ($nick,@chans) = @_;
591 foreach $chan (@chans) {
592 foreach $mode (keys %{$channels{$chan}}) {
594 next unless (exists $channels{$chan}{$mode}{$nick});
596 delete $channels{$chan}{$mode}{$nick};
604 delete $channels{$chan};
608 &DEBUG("clearIRCVars() called!");
612 @joinchan = &getJoinChans();
619 foreach (keys %chanconf) {
620 my $val = $chanconf{$_}{autojoin};
623 $skip++ if ($val eq "0");
637 &status("channels not auto-joining: @skip");
644 &DEBUG("closeDCC called.");
646 foreach $type (keys %dcc) {
647 next if ($type ne uc($type));
649 foreach (keys %{$dcc{$type}}) {
650 &DEBUG("closing DCC $type to $_ (FIXME).");
651 $dcc{$type}{$_}->close();
657 my($who,$chan,$userhost) = @_;
659 return unless (&IsParam("joinfloodCheck"));
661 if (exists $netsplit{lc $who}) { # netsplit join.
662 &DEBUG("jfC: $who was in netsnipe; not checking.");
665 if (exists $floodjoin{$chan}{$who}{Time}) {
666 &WARN("floodjoin{$chan}{$who} already exists?");
669 $floodjoin{$chan}{$who}{Time} = time();
670 $floodjoin{$chan}{$who}{Host} = $userhost;
673 foreach (keys %floodjoin) {
675 my $count = scalar keys %{ $floodjoin{$c} };
676 next unless ($count > 5);
677 &DEBUG("count => $count");
680 foreach (keys %{ $floodjoin{$c} }) {
681 $time += $floodjoin{$c}{$_}{Time};
683 &DEBUG("time => $time");
686 &DEBUG("new time => $time");
691 foreach $chan (keys %floodjoin) {
692 foreach $who (keys %{ $floodjoin{$chan} }) {
693 my $time = time() - $floodjoin{$chan}{$who}{Time};
694 next unless ($time > 10);
695 delete $floodjoin{$chan}{$who};
700 &DEBUG("jfC: $delete deleted.") if ($delete);
706 &FIXME("getHostMask...");