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*|_default';
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 while (my $host = shift @ircServers) {
28 # JUST IN CASE. irq was complaining about this.
29 if ($lastrun == time()) {
30 &DEBUG("hrm... lastrun == time()");
37 &DEBUG("ircloop: ircServers[x] = NULL.");
41 next unless (exists $ircPort{$host});
43 my $retval = &irc($host, $ircPort{$host});
44 &DEBUG("ircloop: after irc()");
46 next unless (defined $retval and $retval == 0);
50 if ($error % 3 == 0 and $error != 0) {
51 &ERROR("CANNOT connect to this server; next!");
56 &ERROR("CANNOT connect to any irc server; stopping.");
61 &DEBUG("ircloop: end... going back.");
67 my ($server,$port) = @_;
69 my $iaddr = inet_aton($server);
70 my $paddr = sockaddr_in($port, $iaddr);
71 my $proto = getprotobyname('tcp');
74 &status("Connecting to port $port of server $server ...");
77 if ($server =~ /\D$/) {
78 my $packed = scalar(gethostbyname($server));
80 if (!defined $packed) {
81 &status(" cannot resolve $server.");
85 my $resolve = inet_ntoa($packed);
86 &status(" resolved to $resolve.");
87 ### warning in Sys/Hostname line 78???
88 ### caused inside Net::IRC?
93 $conn = $irc->newconn(
94 Nick => $param{'ircNick'},
97 Ircname => $param{'ircName'},
98 LocalAddr => $param{'ircHost'},
101 if (!defined $conn) {
102 &ERROR("irc: conn was not created!defined!!!");
108 # change internal timeout value for scheduler.
109 $irc->{_timeout} = 10; # how about 60?
111 $ircstats{'Server'} = "$server:$port";
114 $conn->add_handler('caction', \&on_action);
115 $conn->add_handler('cdcc', \&on_dcc);
116 $conn->add_handler('cping', \&on_ping);
117 $conn->add_handler('crping', \&on_ping_reply);
118 $conn->add_handler('cversion', \&on_version);
119 $conn->add_handler('crversion', \&on_crversion);
120 $conn->add_handler('dcc_open', \&on_dcc_open);
121 $conn->add_handler('dcc_close', \&on_dcc_close);
122 $conn->add_handler('chat', \&on_chat);
123 $conn->add_handler('msg', \&on_msg);
124 $conn->add_handler('public', \&on_public);
125 $conn->add_handler('join', \&on_join);
126 $conn->add_handler('part', \&on_part);
127 $conn->add_handler('topic', \&on_topic);
128 $conn->add_handler('invite', \&on_invite);
129 $conn->add_handler('kick', \&on_kick);
130 $conn->add_handler('mode', \&on_mode);
131 $conn->add_handler('nick', \&on_nick);
132 $conn->add_handler('quit', \&on_quit);
133 $conn->add_handler('notice', \&on_notice);
134 $conn->add_handler('whoisuser', \&on_whoisuser);
135 $conn->add_handler('other', \&on_other);
136 $conn->add_global_handler('disconnect', \&on_disconnect);
137 $conn->add_global_handler([251,252,253,254,255], \&on_init);
138 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
139 $conn->add_global_handler(324, \&on_modeis);
140 $conn->add_global_handler(333, \&on_topicinfo);
141 $conn->add_global_handler(352, \&on_who);
142 $conn->add_global_handler(353, \&on_names);
143 $conn->add_global_handler(366, \&on_endofnames);
144 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
145 $conn->add_global_handler(433, \&on_nick_taken);
146 $conn->add_global_handler(439, \&on_targettoofast);
148 # end of handler stuff.
153 ######################################################################
154 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
155 ######################################################################
161 # slow down a bit if traffic is "high".
162 # need to take into account time of last message sent.
163 if ($last{buflen} > 256 and length($buf) > 256) {
167 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
169 $last{buflen} = length($buf);
176 &DEBUG("say: msg == $msg.");
180 &status("</$talkchannel> $msg");
181 if (&whatInterface() =~ /IRC/) {
182 $msg = "zero" if ($msg =~ /^0+$/);
185 if ($t == $pubtime) {
187 $pubsize += length $msg;
189 if ($pubcount % 4 and $pubcount) {
191 } elsif ($pubsize > 1500) {
199 $pubsize = length $msg;
202 $conn->privmsg($talkchannel, $msg);
207 my ($nick, $msg) = @_;
208 if (!defined $nick) {
209 &ERROR("msg: nick == NULL.");
215 &DEBUG("msg: msg == $msg.");
219 &status(">$nick< $msg");
221 if (&whatInterface() =~ /IRC/) {
224 if ($t == $msgtime) {
226 $msgsize += length $msg;
228 if ($msgcount % 4 and $msgcount) {
230 } elsif ($msgsize > 1000) {
238 $msgsize = length $msg;
241 $conn->privmsg($nick, $msg);
245 # Usage: &action(nick || chan, txt);
247 my ($target, $txt) = @_;
249 &DEBUG("action: txt == NULL.");
253 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
254 if (length $rawout > 510) {
255 &status("action: txt too long; truncating.");
257 chop($rawout) while (length($rawout) > 510);
261 &status("* $ident/$target $txt");
265 # Usage: &action(nick || chan, txt);
267 my ($target, $txt) = @_;
269 &DEBUG("action: txt == NULL.");
273 &status("-$target- $txt");
275 $conn->notice($target, $txt);
280 my ($txt,$flag) = @_;
282 ### FIXME: flag not supported yet.
284 foreach (keys %{$dcc{'CHAT'}}) {
285 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
290 ### perform commands.
293 # Usage: &performReply($reply);
296 $reply =~ /([\.\?\s]+)$/;
298 &checkMsgType($reply);
300 if ($msgType eq 'public') {
301 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
302 $reply = "$orig{who}: ".$reply;
304 $reply = "$reply, ".$orig{who};
307 } elsif ($msgType eq 'private') {
311 $reply = "$reply, ".$orig{who};
314 } elsif ($msgType eq 'chat') {
315 if (!exists $dcc{'CHAT'}{$who}) {
316 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
319 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
321 &ERROR("PR: msgType invalid? ($msgType).");
326 sub performAddressedReply {
327 return unless ($addressed);
332 &performStrictReply(@_);
335 # Usage: &performStrictReply($reply);
336 sub performStrictReply {
339 &checkMsgType($reply);
341 if ($msgType eq 'private') {
343 } elsif ($msgType eq 'public') {
345 } elsif ($msgType eq 'chat') {
346 &dccsay(lc $who,$reply);
348 &ERROR("pSR: msgType invalid? ($msgType).");
353 my ($who, $reply) = @_;
354 if (!exists $dcc{'CHAT'}{$who}) {
355 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
359 &status("=>$who<= $reply"); # dcc chat.
360 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
367 foreach $type (keys %dcc) {
368 &FIXME("dcc_close: $who");
369 my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
370 next unless (scalar @who);
377 my $chan = lc $chankey;
379 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
383 &status("joining $b_blue$chan$ob");
385 if (&validChan($chan)) {
386 &status("join: already on $chan");
388 if (!$conn->join($chan)) {
389 &DEBUG("join failed. trying connect!");
399 next if ($chan eq "");
400 $chan =~ tr/A-Z/a-z/; # lowercase.
402 &status("parting $chan");
403 if (!&validChan($chan)) {
404 &status("part: not on $chan");
408 rawout("PART $chan");
409 # deletion of $channels{chan} is done in &entryEvt().
414 my ($chan, @modes) = @_;
415 my $modes = join(" ", @modes);
417 if (&validChan($chan) == 0) {
418 &ERROR("mode: invalid chan => '$chan'.");
422 &DEBUG("MODE $chan $modes");
424 rawout("MODE $chan $modes");
428 my ($chan, @who) = @_;
429 my $os = "o" x scalar(@who);
431 &mode($chan, "+$os @who");
435 my ($chan, @who) = @_;
436 my $os = "o" x scalar(@who);
438 &mode($chan, "-$os ".@who);
442 my ($nick,$chan,$msg) = @_;
443 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
445 if ($chan ne "" and &validChan($chan) == 0) {
446 &ERROR("kick: invalid channel $chan.");
450 $nick =~ tr/A-Z/a-z/;
452 foreach $chan (@chans) {
453 if (!&IsNickInChan($nick,$chan)) {
454 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
458 if (!exists $channels{$chan}{o}{$ident}) {
459 &status("Kick: do not have ops on $chan :(");
463 &status("Kicking $nick from $chan.");
465 &rawout("KICK $chan $nick");
467 &rawout("KICK $chan $nick :$msg");
473 my ($mask,$chan) = @_;
474 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
477 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
478 &ERROR("ban: invalid channel $chan.");
482 foreach $chan (@chans) {
483 if (!exists $channels{$chan}{o}{$ident}) {
484 &status("Ban: do not have ops on $chan :(");
488 &status("Banning $mask from $chan.");
489 &rawout("MODE $chan +b $mask");
497 my ($mask,$chan) = @_;
498 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
501 &DEBUG("unban: mask = $mask, chan = @chans");
503 foreach $chan (@chans) {
504 if (!exists $channels{$chan}{o}{$ident}) {
505 &status("unBan: do not have ops on $chan :(");
509 &status("Removed ban $mask from $chan.");
510 &rawout("MODE $chan -b $mask");
519 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
520 $conn->quit($quitmsg);
526 if ($nick =~ /^$mask{nick}$/) {
527 &DEBUG("Changing nick to $nick (from $ident)");
528 rawout("NICK ".$nick);
531 &DEBUG("nick failed... why oh why (nick => $nick)");
537 my($who, $chan) = @_;
538 rawout("INVITE $who $chan");
543 # Channel related functions...
546 # Usage: &joinNextChan();
548 if (scalar @joinchan) {
549 my $chan = shift @joinchan;
552 if (my $i = scalar @joinchan) {
553 &status("joinNextChan: $i chans to join.");
558 if (&IsParam("nickServ_pass") and $nickserv < 1) {
559 &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
563 my %chan = &getChanConfList("chanServ");
564 foreach $chan (keys %chan) {
565 next unless ($chan{$chan} > 0);
567 if (!exists $channels{$chan}{'o'}{$ident}) {
568 &status("ChanServ ==> Requesting ops for $chan.");
569 &rawout("PRIVMSG ChanServ :OP $chan $ident");
574 # Usage: &GetNickInChans($nick,$chan);
579 foreach (keys %channels) {
580 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
587 # Usage: &GetNicksInChan($chan);
592 return keys %{ $channels{$chan}{''} };
596 my ($nick,$chan) = @_;
598 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
600 if (&validChan($chan) == 0) {
601 &ERROR("INIC: invalid channel $chan.");
605 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
608 foreach (keys %channels) {
609 next unless (/[A-Z]/);
610 &DEBUG("hash channels contains mixed cased chan!!!");
616 sub IsNickInAnyChan {
619 foreach $chan (keys %channels) {
620 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
626 # Usage: &validChan($chan);
630 if (lc $chan ne $chan) {
631 &WARN("validChan: lc chan != chan. ($chan); fixing.");
632 $chan =~ tr/A-Z/a-z/;
635 if (exists $channels{$chan}) {
643 # Usage: &DeleteUserInfo($nick,@chans);
645 my ($nick,@chans) = @_;
648 foreach $chan (@chans) {
649 foreach $mode (keys %{$channels{$chan}}) {
651 next unless (exists $channels{$chan}{$mode}{$nick});
653 delete $channels{$chan}{$mode}{$nick};
661 delete $channels{$chan};
665 &DEBUG("clearIRCVars() called!");
669 @joinchan = &getJoinChans();
676 foreach (keys %chanconf) {
677 next if ($_ eq "_default");
679 my $val = $chanconf{$_}{autojoin};
683 $skip++ if ($val eq "0");
697 &status("gJC: channels not auto-joining: @skip");
699 &status("gJC: auto-joining all chans.");
706 &DEBUG("closeDCC called.");
708 foreach $type (keys %dcc) {
709 next if ($type ne uc($type));
711 foreach $nick (keys %{$dcc{$type}}) {
712 next unless (defined $nick);
713 &DEBUG("closing DCC $type to $nick (FIXME).");
714 next unless (defined $dcc{$type}{$nick});
716 my $ref = $dcc{$type}{$nick};
717 &DEBUG("ref => $ref");
719 # $dcc{$type}{$nick}->close();
725 my($who,$chan,$userhost) = @_;
727 return unless (&IsChanConf("joinfloodCheck"));
729 if (exists $netsplit{lc $who}) { # netsplit join.
730 &DEBUG("jfC: $who was in netsplit; not checking.");
733 if (exists $floodjoin{$chan}{$who}{Time}) {
734 &WARN("floodjoin{$chan}{$who} already exists?");
737 $floodjoin{$chan}{$who}{Time} = time();
738 $floodjoin{$chan}{$who}{Host} = $userhost;
741 foreach (keys %floodjoin) {
743 my $count = scalar keys %{ $floodjoin{$c} };
744 next unless ($count > 5);
745 &DEBUG("count => $count");
748 foreach (keys %{ $floodjoin{$c} }) {
749 $time += $floodjoin{$c}{$_}{Time};
751 &DEBUG("time => $time");
754 &DEBUG("new time => $time");
759 foreach $chan (keys %floodjoin) {
760 foreach $who (keys %{ $floodjoin{$chan} }) {
761 my $time = time() - $floodjoin{$chan}{$who}{Time};
762 next unless ($time > 10);
763 delete $floodjoin{$chan}{$who};
768 &DEBUG("jfC: $delete deleted.") if ($delete);
774 &FIXME("getHostMask...");