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.");
80 ### warning in Sys/Hostname line 78???
81 ### caused inside Net::IRC?
86 $conn = $irc->newconn(
87 Nick => $param{'ircNick'},
90 Ircname => $param{'ircName'},
91 LocalAddr => $param{'ircHost'},
95 &ERROR("irc: conn was not created!defined!!!");
101 # change internal timeout value for scheduler.
102 $irc->{_timeout} = 10; # how about 60?
104 $ircstats{'Server'} = "$server:$port";
107 $conn->add_handler('caction', \&on_action);
108 $conn->add_handler('cdcc', \&on_dcc);
109 $conn->add_handler('cping', \&on_ping);
110 $conn->add_handler('crping', \&on_ping_reply);
111 $conn->add_handler('cversion', \&on_version);
112 $conn->add_handler('crversion', \&on_crversion);
113 $conn->add_handler('dcc_open', \&on_dcc_open);
114 $conn->add_handler('dcc_close', \&on_dcc_close);
115 $conn->add_handler('chat', \&on_chat);
116 $conn->add_handler('msg', \&on_msg);
117 $conn->add_handler('public', \&on_public);
118 $conn->add_handler('join', \&on_join);
119 $conn->add_handler('part', \&on_part);
120 $conn->add_handler('topic', \&on_topic);
121 $conn->add_handler('invite', \&on_invite);
122 $conn->add_handler('kick', \&on_kick);
123 $conn->add_handler('mode', \&on_mode);
124 $conn->add_handler('nick', \&on_nick);
125 $conn->add_handler('quit', \&on_quit);
126 $conn->add_handler('notice', \&on_notice);
127 $conn->add_handler('whoisuser', \&on_whoisuser);
128 $conn->add_handler('other', \&on_other);
129 $conn->add_global_handler('disconnect', \&on_disconnect);
130 $conn->add_global_handler([251,252,253,254,255], \&on_init);
131 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
132 $conn->add_global_handler(324, \&on_modeis);
133 $conn->add_global_handler(333, \&on_topicinfo);
134 $conn->add_global_handler(352, \&on_who);
135 $conn->add_global_handler(353, \&on_names);
136 $conn->add_global_handler(366, \&on_endofnames);
137 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
138 $conn->add_global_handler(433, \&on_nick_taken);
139 $conn->add_global_handler(439, \&on_targettoofast);
141 # end of handler stuff.
146 ######################################################################
147 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
148 ######################################################################
154 # slow down a bit if traffic is "high".
155 # need to take into account time of last message sent.
156 if ($last{buflen} > 256 and length($buf) > 256) {
160 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
162 $last{buflen} = length($buf);
169 &DEBUG("say: msg == $msg.");
173 if ($msg eq $last{say} and length($msg) > 256) {
174 &status("say: detected repeated message; skipping.");
179 &status("</$talkchannel> $msg");
180 if (&whatInterface() =~ /IRC/) {
181 $msg = "zero" if ($msg =~ /^0+$/);
183 $conn->privmsg($talkchannel, $msg);
188 my ($nick, $msg) = @_;
189 if (!defined $nick) {
190 &ERROR("msg: nick == NULL.");
196 &DEBUG("msg: msg == $msg.");
200 if ($msg eq $last{msg} and length($msg) > 256) {
201 &status("msg: detected repeated message; skipping.");
206 &status(">$nick< $msg");
207 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
210 # Usage: &action(nick || chan, txt);
212 my ($target, $txt) = @_;
214 &DEBUG("action: txt == NULL.");
218 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
219 if (length $rawout > 510) {
220 &status("action: txt too long; truncating.");
222 chop($rawout) while (length($rawout) > 510);
226 &status("* $ident/$target $txt");
230 # Usage: &action(nick || chan, txt);
232 my ($target, $txt) = @_;
234 &DEBUG("action: txt == NULL.");
238 &status("-$target- $txt");
240 $conn->notice($target, $txt);
245 my ($txt,$flag) = @_;
247 ### FIXME: flag not supported yet.
249 foreach (keys %{$dcc{'CHAT'}}) {
250 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
255 ### perform commands.
258 # Usage: &performReply($reply);
261 $reply =~ /([\.\?\s]+)$/;
263 &checkMsgType($reply);
265 if ($msgType eq 'public') {
266 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
267 $reply = "$orig{who}: ".$reply;
269 $reply = "$reply, ".$orig{who};
272 } elsif ($msgType eq 'private') {
276 $reply = "$reply, ".$orig{who};
279 } elsif ($msgType eq 'chat') {
280 if (!exists $dcc{'CHAT'}{$who}) {
281 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
284 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
286 &ERROR("PR: msgType invalid? ($msgType).");
291 sub performAddressedReply {
292 return unless ($addressed);
297 &performStrictReply(@_);
300 # Usage: &performStrictReply($reply);
301 sub performStrictReply {
304 &checkMsgType($reply);
306 if ($msgType eq 'private') {
308 } elsif ($msgType eq 'public') {
310 } elsif ($msgType eq 'chat') {
311 &dccsay($who,$reply);
313 &ERROR("pSR: msgType invalid? ($msgType).");
318 my ($who, $reply) = @_;
319 if (!exists $dcc{'CHAT'}{$who}) {
320 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
324 &status("=>$who<= $reply"); # dcc chat.
325 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
332 foreach $type (keys %dcc) {
333 &FIXME("dcc_close: $who");
334 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
335 next unless (scalar @who);
342 my $chan = lc $chankey;
344 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
348 &status("joining $b_blue$chan$ob");
350 if (&validChan($chan)) {
351 &status("join: already on $chan");
361 next if ($chan eq "");
362 $chan =~ tr/A-Z/a-z/; # lowercase.
364 &status("parting $chan");
365 if (!&validChan($chan)) {
366 &status("part: not on $chan");
370 rawout("PART $chan");
371 # deletion of $channels{chan} is done in &entryEvt().
376 my ($chan, @modes) = @_;
377 my $modes = join(" ", @modes);
379 if (&validChan($chan) == 0) {
380 &ERROR("mode: invalid chan => '$chan'.");
384 &DEBUG("MODE $chan $modes");
386 rawout("MODE $chan $modes");
390 my ($chan, @who) = @_;
391 my $os = "o" x scalar(@who);
393 &mode($chan, "+$os ".@who);
397 my ($chan, @who) = @_;
398 my $os = "o" x scalar(@who);
400 &mode($chan, "-$os ".@who);
404 my ($nick,$chan,$msg) = @_;
405 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
407 if ($chan ne "" and &validChan($chan) == 0) {
408 &ERROR("kick: invalid channel $chan.");
412 $nick =~ tr/A-Z/a-z/;
414 foreach $chan (@chans) {
415 if (!&IsNickInChan($nick,$chan)) {
416 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
420 if (!exists $channels{$chan}{o}{$ident}) {
421 &status("Kick: do not have ops on $chan :(");
425 &status("Kicking $nick from $chan.");
427 &rawout("KICK $chan $nick");
429 &rawout("KICK $chan $nick :$msg");
435 my ($mask,$chan) = @_;
436 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
439 if ($chan ne "" and &validChan($chan) == 0) {
440 &ERROR("ban: invalid channel $chan.");
444 $nick =~ tr/A-Z/a-z/;
446 foreach $chan (@chans) {
447 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
448 &status("Ban: $nick is not on $chan.");
452 if (!exists $channels{$chan}{o}{$ident}) {
453 &status("Ban: do not have ops on $chan :(");
457 &status("Banning $mask from $chan.");
458 &rawout("MODE $chan +b $mask");
467 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
468 $conn->quit($quitmsg);
474 if ($nick =~ /^$mask{nick}$/) {
475 rawout("NICK ".$nick);
483 my($who, $chan) = @_;
484 rawout("INVITE $who $chan");
489 # Channel related functions...
492 # Usage: &joinNextChan();
494 if (scalar @joinchan) {
495 my $chan = shift @joinchan;
498 if (my $i = scalar @joinchan) {
499 &status("joinNextChan: $i chans to join.");
504 if (&IsParam("nickServ_pass") and $nickserv < 1) {
505 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
509 my %chan = &getChanConfList("chanServ");
510 foreach $chan (keys %chan) {
511 next unless ($chan{$chan} > 0);
513 if (!exists $channels{$chan}{'o'}{$ident}) {
514 &status("ChanServ ==> Requesting ops for $chan.");
515 &rawout("PRIVMSG ChanServ :OP $chan $ident");
520 # Usage: &GetNickInChans($nick,$chan);
525 foreach (keys %channels) {
526 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
533 # Usage: &GetNicksInChan($chan);
538 return keys %{ $channels{$chan}{''} };
542 my ($nick,$chan) = @_;
544 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
546 if (&validChan($chan) == 0) {
547 &ERROR("INIC: invalid channel $chan.");
551 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
554 foreach (keys %channels) {
555 next unless (/[A-Z]/);
556 &DEBUG("hash channels contains mixed cased chan!!!");
562 sub IsNickInAnyChan {
565 foreach $chan (keys %channels) {
566 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
572 # Usage: &validChan($chan);
576 if (lc $chan ne $chan) {
577 &WARN("validChan: lc chan != chan. ($chan); fixing.");
578 $chan =~ tr/A-Z/a-z/;
581 if (exists $channels{$chan}) {
589 # Usage: &DeleteUserInfo($nick,@chans);
591 my ($nick,@chans) = @_;
594 foreach $chan (@chans) {
595 foreach $mode (keys %{$channels{$chan}}) {
597 next unless (exists $channels{$chan}{$mode}{$nick});
599 delete $channels{$chan}{$mode}{$nick};
607 delete $channels{$chan};
611 &DEBUG("clearIRCVars() called!");
615 @joinchan = &getJoinChans();
622 foreach (keys %chanconf) {
623 next if ($_ eq "_default");
625 my $val = $chanconf{$_}{autojoin};
629 $skip++ if ($val eq "0");
643 &status("channels not auto-joining: @skip");
645 &status("auto-joining all chans.");
652 &DEBUG("closeDCC called.");
654 foreach $type (keys %dcc) {
655 next if ($type ne uc($type));
657 foreach (keys %{$dcc{$type}}) {
658 &DEBUG("closing DCC $type to $_ (FIXME).");
659 $dcc{$type}{$_}->close();
665 my($who,$chan,$userhost) = @_;
667 return unless (&IsChanConf("joinfloodCheck"));
669 if (exists $netsplit{lc $who}) { # netsplit join.
670 &DEBUG("jfC: $who was in netsnipe; not checking.");
673 if (exists $floodjoin{$chan}{$who}{Time}) {
674 &WARN("floodjoin{$chan}{$who} already exists?");
677 $floodjoin{$chan}{$who}{Time} = time();
678 $floodjoin{$chan}{$who}{Host} = $userhost;
681 foreach (keys %floodjoin) {
683 my $count = scalar keys %{ $floodjoin{$c} };
684 next unless ($count > 5);
685 &DEBUG("count => $count");
688 foreach (keys %{ $floodjoin{$c} }) {
689 $time += $floodjoin{$c}{$_}{Time};
691 &DEBUG("time => $time");
694 &DEBUG("new time => $time");
699 foreach $chan (keys %floodjoin) {
700 foreach $who (keys %{ $floodjoin{$chan} }) {
701 my $time = time() - $floodjoin{$chan}{$who}{Time};
702 next unless ($time > 10);
703 delete $floodjoin{$chan}{$who};
708 &DEBUG("jfC: $delete deleted.") if ($delete);
714 &FIXME("getHostMask...");