2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
12 %chanconf %orig %ircPort %ircstats %last %netsplit);
13 use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
15 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
16 $pubcount $pubtime $pubsize);
17 use vars qw($b_blue $ob);
18 use vars qw(@joinchan @ircServers);
27 while (my $host = shift @ircServers) {
28 # JUST IN CASE. irq was complaining about this.
29 if ($lastrun == time()) {
30 &DEBUG("ircloop: hrm... lastrun == time()");
37 &DEBUG("ircloop: ircServers[x] = NULL.");
41 next unless (exists $ircPort{$host});
43 my $retval = &irc($host, $ircPort{$host});
44 next unless (defined $retval and $retval == 0);
47 if ($error % 3 == 0 and $error != 0) {
48 &status("IRC: Could not connect.");
54 &status("IRC: cannot connect to any IRC servers; stopping.");
60 &status("IRC: ok, done one cycle of IRC servers; trying again.");
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?
94 Nick => $param{'ircNick'},
97 Ircname => $param{'ircName'},
99 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
100 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
102 $conn = $irc->newconn(%args);
104 if (!defined $conn) {
105 &ERROR("IRC: connection failed.");
106 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
107 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
108 &ERROR("127.0.0.1 localhost localhost");
109 &ERROR("If this is still a problem, please contact the maintainer.");
113 if ($param{'ircNick2'}) {
114 # prep for real multiple nick/server connects
115 # FIXME: all locations should get nick/server out of self, not config
116 $args{'Nick'} = $param{'ircNick2'};
117 my $conn2 = $irc->newconn(%args);
122 # change internal timeout value for scheduler.
123 $irc->{_timeout} = 10; # how about 60?
124 # Net::IRC debugging.
127 $ircstats{'Server'} = "$server:$port";
130 $conn->add_global_handler('caction', \&on_action);
131 $conn->add_global_handler('cdcc', \&on_dcc);
132 $conn->add_global_handler('cping', \&on_ping);
133 $conn->add_global_handler('crping', \&on_ping_reply);
134 $conn->add_global_handler('cversion', \&on_version);
135 $conn->add_global_handler('crversion', \&on_crversion);
136 $conn->add_global_handler('dcc_open', \&on_dcc_open);
137 $conn->add_global_handler('dcc_close', \&on_dcc_close);
138 $conn->add_global_handler('chat', \&on_chat);
139 $conn->add_global_handler('msg', \&on_msg);
140 $conn->add_global_handler('public', \&on_public);
141 $conn->add_global_handler('join', \&on_join);
142 $conn->add_global_handler('part', \&on_part);
143 $conn->add_global_handler('topic', \&on_topic);
144 $conn->add_global_handler('invite', \&on_invite);
145 $conn->add_global_handler('kick', \&on_kick);
146 $conn->add_global_handler('mode', \&on_mode);
147 $conn->add_global_handler('nick', \&on_nick);
148 $conn->add_global_handler('quit', \&on_quit);
149 $conn->add_global_handler('notice', \&on_notice);
150 $conn->add_global_handler('whoischannels', \&on_whoischannels);
151 $conn->add_global_handler('useronchannel', \&on_useronchannel);
152 $conn->add_global_handler('whois', \&on_whois);
153 $conn->add_global_handler('other', \&on_other);
154 $conn->add_global_handler('disconnect', \&on_disconnect);
155 $conn->add_global_handler([251,252,253,254,255], \&on_init);
156 # $conn->add_global_handler(302, \&on_init); # userhost
157 $conn->add_global_handler(303, \&on_ison); # notify.
158 $conn->add_global_handler(315, \&on_endofwho);
159 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
160 $conn->add_global_handler(324, \&on_modeis);
161 $conn->add_global_handler(333, \&on_topicinfo);
162 $conn->add_global_handler(352, \&on_who);
163 $conn->add_global_handler(353, \&on_names);
164 $conn->add_global_handler(366, \&on_endofnames);
165 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
166 $conn->add_global_handler(433, \&on_nick_taken);
167 $conn->add_global_handler(439, \&on_targettoofast);
168 # for proper joinnextChan behaviour
169 $conn->add_global_handler(471, \&on_chanfull);
170 $conn->add_global_handler(473, \&on_inviteonly);
171 $conn->add_global_handler(474, \&on_banned);
172 $conn->add_global_handler(475, \&on_badchankey);
173 $conn->add_global_handler(443, \&on_useronchan);
175 # end of handler stuff.
180 ######################################################################
181 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
182 ######################################################################
188 # slow down a bit if traffic is "high".
189 # need to take into account time of last message sent.
190 if ($last{buflen} > 256 and length($buf) > 256) {
194 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
196 $last{buflen} = length($buf);
203 &WARN("say: msg == $msg.");
207 &status("</$talkchannel> $msg");
208 if (&whatInterface() =~ /IRC/) {
209 $msg = "zero" if ($msg =~ /^0+$/);
212 if ($t == $pubtime) {
214 $pubsize += length $msg;
216 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
217 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
219 if ( ($pubcount % $i) == 0 and $pubcount) {
221 } elsif ($pubsize > $j) {
229 $pubsize = length $msg;
232 $conn->privmsg($talkchannel, $msg);
237 my ($nick, $msg) = @_;
238 if (!defined $nick) {
239 &ERROR("msg: nick == NULL.");
245 &WARN("msg: msg == $msg.");
249 &status(">$nick< $msg");
251 return unless (&whatInterface() =~ /IRC/);
254 if ($t == $msgtime) {
256 $msgsize += length $msg;
258 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
259 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
260 if ( ($msgcount % $i) == 0 and $msgcount) {
262 } elsif ($msgsize > $j) {
270 $msgsize = length $msg;
273 $conn->privmsg($nick, $msg);
276 # Usage: &action(nick || chan, txt);
278 my ($target, $txt) = @_;
280 &WARN("action: txt == NULL.");
284 if (length $txt > 480) {
285 &status("action: txt too long; truncating.");
286 chop($txt) while (length $txt > 480);
289 &status("* $ident/$target $txt");
290 $conn->me($target, $txt);
293 # Usage: ¬ice(nick || chan, txt);
295 my ($target, $txt) = @_;
297 &WARN("notice: txt == NULL.");
301 &status("-$target- $txt");
305 if ($t == $nottime) {
307 $notsize += length $txt;
309 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
310 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
312 if ( ($notcount % $i) == 0 and $notcount) {
314 } elsif ($notsize > $j) {
322 $notsize = length $txt;
325 $conn->notice($target, $txt);
329 my ($txt,$flag) = @_;
331 ### FIXME: flag not supported yet.
333 foreach (keys %{ $dcc{'CHAT'} }) {
334 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
339 ### perform commands.
342 # Usage: &performReply($reply);
346 if (!defined $reply or $reply =~ /^\s*$/) {
347 &DEBUG("performReply: reply == NULL.");
351 $reply =~ /([\.\?\s]+)$/;
353 &checkMsgType($reply);
355 if ($msgType eq 'public') {
356 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
357 $reply = "$orig{who}: ".$reply;
359 $reply = "$reply, ".$orig{who};
363 } elsif ($msgType eq 'private') {
365 $reply = "$reply, ".$orig{who};
369 } elsif ($msgType eq 'chat') {
370 if (!exists $dcc{'CHAT'}{$who}) {
371 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
374 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
377 &ERROR("PR: msgType invalid? ($msgType).");
382 sub performAddressedReply {
383 return unless ($addressed);
388 &performStrictReply(@_);
391 # Usage: &performStrictReply($reply);
392 sub performStrictReply {
395 &checkMsgType($reply);
397 if ($msgType eq 'private') {
399 } elsif ($msgType eq 'public') {
401 } elsif ($msgType eq 'chat') {
402 &dccsay(lc $who, $reply);
404 &ERROR("pSR: msgType invalid? ($msgType).");
409 my($who, $reply) = @_;
411 if (!defined $reply or $reply =~ /^\s*$/) {
412 &WARN("dccsay: reply == NULL.");
416 if (!exists $dcc{'CHAT'}{$who}) {
417 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
421 &status("=>$who<= $reply"); # dcc chat.
422 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
429 foreach $type (keys %dcc) {
430 &FIXME("dcc_close: $who");
431 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
432 next unless (scalar @who);
434 &DEBUG("dcc_close... close $who!");
440 my $key = &getChanConf("chankey", $chan) || "";
442 # forgot for about 2 years to implement channel keys when moving
443 # over to Net::IRC...
445 # hopefully validChan is right.
446 if (&validChan($chan)) {
447 &status("join: already on $chan");
449 &status("joining $b_blue$chan$ob");
451 return if ($conn->join($chan, $key));
453 &DEBUG("joinchan: join failed. trying connect!");
463 next if ($chan eq "");
464 $chan =~ tr/A-Z/a-z/; # lowercase.
466 if ($chan !~ /^$mask{chan}$/) {
467 &WARN("part: chan is invalid ($chan)");
471 &status("parting $chan");
472 if (!&validChan($chan)) {
473 &WARN("part: not on $chan; doing anyway");
478 # deletion of $channels{chan} is done in &entryEvt().
483 my ($chan, @modes) = @_;
484 my $modes = join(" ", @modes);
486 if (&validChan($chan) == 0) {
487 &ERROR("mode: invalid chan => '$chan'.");
491 &DEBUG("mode: MODE $chan $modes");
493 # should move to use Net::IRC's $conn->mode()... but too lazy.
494 rawout("MODE $chan $modes");
498 my ($chan, @who) = @_;
499 my $os = "o" x scalar(@who);
501 &mode($chan, "+$os @who");
505 my ($chan, @who) = @_;
506 my $os = "o" x scalar(@who);
508 &mode($chan, "-$os ".@who);
512 my ($nick,$chan,$msg) = @_;
513 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
515 if ($chan ne "" and &validChan($chan) == 0) {
516 &ERROR("kick: invalid channel $chan.");
520 $nick =~ tr/A-Z/a-z/;
522 foreach $chan (@chans) {
523 if (!&IsNickInChan($nick,$chan)) {
524 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
528 if (!exists $channels{$chan}{o}{$ident}) {
529 &status("kick: do not have ops on $chan :(");
533 &status("Kicking $nick from $chan.");
534 $conn->kick($chan, $nick, $msg);
539 my ($mask,$chan) = @_;
540 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
543 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
544 &ERROR("ban: invalid channel $chan.");
548 foreach $chan (@chans) {
549 if (!exists $channels{$chan}{o}{$ident}) {
550 &status("ban: do not have ops on $chan :(");
554 &status("Banning $mask from $chan.");
555 &rawout("MODE $chan +b $mask");
563 my ($mask,$chan) = @_;
564 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
567 &DEBUG("unban: mask = $mask, chan = @chans");
569 foreach $chan (@chans) {
570 if (!exists $channels{$chan}{o}{$ident}) {
571 &status("unBan: do not have ops on $chan :(");
575 &status("Removed ban $mask from $chan.");
576 &rawout("MODE $chan -b $mask");
585 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
587 $conn->quit($quitmsg);
589 &WARN("quit: could not quit!");
596 if (!defined $nick) {
597 &ERROR("nick: nick == NULL.");
601 if (defined $ident and $nick eq $ident) {
602 &WARN("nick: nick == ident == '$ident'.");
607 $bad++ if (exists $nuh{ $param{'ircNick'} });
608 $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
611 &WARN("Nick: not going to try and get my nick back. [".
612 scalar(gmtime). "]");
613 # hrm... over time we lose track of our own nick.
617 if ($nick =~ /^$mask{nick}$/) {
618 &rawout("NICK ".$nick);
620 if (defined $ident) {
621 &status("nick: Changing nick to $nick (from $ident)");
622 # following shouldn't be here :(
625 &DEBUG("first time nick change.");
631 &DEBUG("nick: failed... why oh why (nick => $nick)");
637 my($who, $chan) = @_;
638 # TODO: check if $who or $chan are invalid.
640 $conn->invite($who, $chan);
644 # Channel related functions...
647 # Usage: &joinNextChan();
649 if (scalar @joinchan) {
650 my $chan = shift @joinchan;
653 if (my $i = scalar @joinchan) {
654 &status("joinNextChan: $i chans to join.");
661 my @c = &getJoinChans();
662 if (exists $cache{joinTime} and scalar @c) {
663 my $delta = time() - $cache{joinTime} - 5;
664 my $timestr = &Time2String($delta);
665 my $rate = sprintf("%.1f", $delta / @c);
666 delete $cache{joinTime};
668 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
671 # chanserv check: global channels, in case we missed one.
672 foreach ( &ChanConfList("chanServ_ops") ) {
677 # Usage: &getNickInChans($nick);
682 foreach (keys %channels) {
683 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
690 # Usage: &getNicksInChan($chan);
695 return keys %{ $channels{$chan}{''} };
699 my ($nick,$chan) = @_;
701 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
704 &DEBUG("INIC: chan == NULL.");
708 if (&validChan($chan) == 0) {
709 &ERROR("INIC: invalid channel $chan.");
713 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
716 foreach (keys %channels) {
717 next unless (/[A-Z]/);
718 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
724 sub IsNickInAnyChan {
728 foreach $chan (keys %channels) {
729 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
735 # Usage: &validChan($chan);
737 # TODO: use $c instead?
740 if (!defined $chan or $chan =~ /^\s*$/) {
744 if (lc $chan ne $chan) {
745 &WARN("validChan: lc chan != chan. ($chan); fixing.");
746 $chan =~ tr/A-Z/a-z/;
749 # it's possible that this check creates the hash if empty.
750 if (defined $channels{$chan} or exists $channels{$chan}) {
751 if ($chan =~ /^_?default$/) {
752 # &WARN("validC: chan cannot be _default! returning 0!");
763 # Usage: &delUserInfo($nick,@chans);
765 my ($nick,@chans) = @_;
768 foreach $chan (@chans) {
769 foreach $mode (keys %{ $channels{$chan} }) {
771 next unless (exists $channels{$chan}{$mode}{$nick});
773 delete $channels{$chan}{$mode}{$nick};
781 delete $channels{$chan};
788 @joinchan = &getJoinChans(1);
789 $cache{joinTime} = time();
797 foreach (keys %chanconf) {
798 next if ($_ eq "_default");
800 my $val = $chanconf{$_}{autojoin};
804 $skip++ if ($val eq "0");
819 $str = "channels not auto-joining: @skip (joining: @chans)";
821 $str = "auto-joining all chans: @chans";
824 &status("Chans: ".$str) if ($show);
830 # &DEBUG("closeDCC called.");
833 foreach $type (keys %dcc) {
834 next if ($type ne uc($type));
837 foreach $nick (keys %{ $dcc{$type} }) {
838 next unless (defined $nick);
839 &status("DCC CHAT: closing DCC $type to $nick.");
840 next unless (defined $dcc{$type}{$nick});
842 my $ref = $dcc{$type}{$nick};
843 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
844 $dcc{$type}{$nick}->close();
845 delete $dcc{$type}{$nick};
846 &DEBUG("after close for $nick");
853 my($who,$chan,$userhost) = @_;
855 return unless (&IsChanConf("joinfloodCheck"));
857 if (exists $netsplit{lc $who}) { # netsplit join.
858 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
861 if (exists $floodjoin{$chan}{$who}{Time}) {
862 &WARN("floodjoin{$chan}{$who} already exists?");
865 $floodjoin{$chan}{$who}{Time} = time();
866 $floodjoin{$chan}{$who}{Host} = $userhost;
869 foreach (keys %floodjoin) {
871 my $count = scalar keys %{ $floodjoin{$c} };
872 next unless ($count > 5);
873 &DEBUG("joinflood: count => $count");
876 foreach (keys %{ $floodjoin{$c} }) {
877 my $t = $floodjoin{$c}{$_}{Time};
878 next unless (defined $t);
882 &DEBUG("joinflood: time => $time");
885 &DEBUG("joinflood: new time => $time");
891 foreach $chan (keys %floodjoin) {
892 foreach $who (keys %{ $floodjoin{$chan} }) {
893 my $t = $floodjoin{$chan}{$who}{Time};
894 next unless (defined $t);
896 my $delta = $time - $t;
897 next unless ($delta > 10);
899 delete $floodjoin{$chan}{$who};
904 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
910 if (exists $nuh{$n}) {
911 return &makeHostMask($nuh{$n});
913 $cache{on_who_Hack} = 1;