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 my $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 $conn = $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?");
450 &status("joining $b_blue$chan$ob");
452 return if ($conn->join($chan, $key));
453 return if (&validChan($chan));
455 &DEBUG("joinchan: join failed. trying connect!");
465 next if ($chan eq "");
466 $chan =~ tr/A-Z/a-z/; # lowercase.
468 if ($chan !~ /^$mask{chan}$/) {
469 &WARN("part: chan is invalid ($chan)");
473 &status("parting $chan");
474 if (!&validChan($chan)) {
475 &WARN("part: not on $chan; doing anyway");
480 # deletion of $channels{chan} is done in &entryEvt().
485 my ($chan, @modes) = @_;
486 my $modes = join(" ", @modes);
488 if (&validChan($chan) == 0) {
489 &ERROR("mode: invalid chan => '$chan'.");
493 &DEBUG("mode: MODE $chan $modes");
495 # should move to use Net::IRC's $conn->mode()... but too lazy.
496 rawout("MODE $chan $modes");
500 my ($chan, @who) = @_;
501 my $os = "o" x scalar(@who);
503 &mode($chan, "+$os @who");
507 my ($chan, @who) = @_;
508 my $os = "o" x scalar(@who);
510 &mode($chan, "-$os ".@who);
514 my ($nick,$chan,$msg) = @_;
515 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
517 if ($chan ne "" and &validChan($chan) == 0) {
518 &ERROR("kick: invalid channel $chan.");
522 $nick =~ tr/A-Z/a-z/;
524 foreach $chan (@chans) {
525 if (!&IsNickInChan($nick,$chan)) {
526 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
530 if (!exists $channels{$chan}{o}{$ident}) {
531 &status("kick: do not have ops on $chan :(");
535 &status("Kicking $nick from $chan.");
536 $conn->kick($chan, $nick, $msg);
541 my ($mask,$chan) = @_;
542 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
545 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
546 &ERROR("ban: invalid channel $chan.");
550 foreach $chan (@chans) {
551 if (!exists $channels{$chan}{o}{$ident}) {
552 &status("ban: do not have ops on $chan :(");
556 &status("Banning $mask from $chan.");
557 &rawout("MODE $chan +b $mask");
565 my ($mask,$chan) = @_;
566 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
569 &DEBUG("unban: mask = $mask, chan = @chans");
571 foreach $chan (@chans) {
572 if (!exists $channels{$chan}{o}{$ident}) {
573 &status("unBan: do not have ops on $chan :(");
577 &status("Removed ban $mask from $chan.");
578 &rawout("MODE $chan -b $mask");
588 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
589 $conn->quit($quitmsg);
591 &WARN("quit: could not quit!");
598 if (!defined $nick) {
599 &ERROR("nick: nick == NULL.");
603 if (defined $ident and $nick eq $ident) {
604 &WARN("nick: nick == ident == '$ident'.");
609 $bad++ if (exists $nuh{$conn->nick()});
610 $bad++ if (&IsNickInAnyChan($conn->nick()));
613 &WARN("Nick: not going to try and get my nick back. [".
614 scalar(gmtime). "]");
615 # hrm... over time we lose track of our own nick.
619 if ($nick =~ /^$mask{nick}$/) {
620 &rawout("NICK ".$nick);
622 if (defined $ident) {
623 &status("nick: Changing nick to $nick (from $ident)");
624 # following shouldn't be here :(
627 &DEBUG("first time nick change.");
633 &DEBUG("nick: failed... why oh why (nick => $nick)");
639 my($who, $chan) = @_;
640 # TODO: check if $who or $chan are invalid.
642 $conn->invite($who, $chan);
646 # Channel related functions...
649 # Usage: &joinNextChan();
651 if (scalar @joinchan) {
652 my $chan = shift @joinchan;
655 if (my $i = scalar @joinchan) {
656 &status("joinNextChan: $i chans to join.");
663 my @c = &getJoinChans();
664 if (exists $cache{joinTime} and scalar @c) {
665 my $delta = time() - $cache{joinTime} - 5;
666 my $timestr = &Time2String($delta);
667 my $rate = sprintf("%.1f", $delta / @c);
668 delete $cache{joinTime};
670 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
673 # chanserv check: global channels, in case we missed one.
674 foreach ( &ChanConfList("chanServ_ops") ) {
679 # Usage: &getNickInChans($nick);
684 foreach (keys %channels) {
685 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
692 # Usage: &getNicksInChan($chan);
697 return keys %{ $channels{$chan}{''} };
701 my ($nick,$chan) = @_;
703 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
706 &DEBUG("INIC: chan == NULL.");
710 if (&validChan($chan) == 0) {
711 &ERROR("INIC: invalid channel $chan.");
715 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
718 foreach (keys %channels) {
719 next unless (/[A-Z]/);
720 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
726 sub IsNickInAnyChan {
730 foreach $chan (keys %channels) {
731 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
737 # Usage: &validChan($chan);
739 # TODO: use $c instead?
742 if (!defined $chan or $chan =~ /^\s*$/) {
746 if (lc $chan ne $chan) {
747 &WARN("validChan: lc chan != chan. ($chan); fixing.");
748 $chan =~ tr/A-Z/a-z/;
751 # it's possible that this check creates the hash if empty.
752 if (defined $channels{$chan} or exists $channels{$chan}) {
753 if ($chan =~ /^_?default$/) {
754 # &WARN("validC: chan cannot be _default! returning 0!");
765 # Usage: &delUserInfo($nick,@chans);
767 my ($nick,@chans) = @_;
770 foreach $chan (@chans) {
771 foreach $mode (keys %{ $channels{$chan} }) {
773 next unless (exists $channels{$chan}{$mode}{$nick});
775 delete $channels{$chan}{$mode}{$nick};
783 delete $channels{$chan};
790 @joinchan = &getJoinChans(1);
791 $cache{joinTime} = time();
799 foreach (keys %chanconf) {
800 next if ($_ eq "_default");
803 my $val = $chanconf{$_}{autojoin};
806 $skip++ if ($val eq "0");
807 if (($conn) and ($val eq "1")) {
808 # convert old +autojoin to autojoin <nick>
809 $val = $conn->nick();
810 $chanconf{$_}{autojoin} = $val;
818 } elsif (($conn) and ($conn->nick() eq $val)) {
827 $str = "channels not auto-joining: @skip (joining: @chans)";
829 $str = "auto-joining all chans: @chans";
832 &status("Chans: ".$str) if ($show);
838 # &DEBUG("closeDCC called.");
841 foreach $type (keys %dcc) {
842 next if ($type ne uc($type));
845 foreach $nick (keys %{ $dcc{$type} }) {
846 next unless (defined $nick);
847 &status("DCC CHAT: closing DCC $type to $nick.");
848 next unless (defined $dcc{$type}{$nick});
850 my $ref = $dcc{$type}{$nick};
851 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
852 $dcc{$type}{$nick}->close();
853 delete $dcc{$type}{$nick};
854 &DEBUG("after close for $nick");
861 my($who,$chan,$userhost) = @_;
863 return unless (&IsChanConf("joinfloodCheck"));
865 if (exists $netsplit{lc $who}) { # netsplit join.
866 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
869 if (exists $floodjoin{$chan}{$who}{Time}) {
870 &WARN("floodjoin{$chan}{$who} already exists?");
873 $floodjoin{$chan}{$who}{Time} = time();
874 $floodjoin{$chan}{$who}{Host} = $userhost;
877 foreach (keys %floodjoin) {
879 my $count = scalar keys %{ $floodjoin{$c} };
880 next unless ($count > 5);
881 &DEBUG("joinflood: count => $count");
884 foreach (keys %{ $floodjoin{$c} }) {
885 my $t = $floodjoin{$c}{$_}{Time};
886 next unless (defined $t);
890 &DEBUG("joinflood: time => $time");
893 &DEBUG("joinflood: new time => $time");
899 foreach $chan (keys %floodjoin) {
900 foreach $who (keys %{ $floodjoin{$chan} }) {
901 my $t = $floodjoin{$chan}{$who}{Time};
902 next unless (defined $t);
904 my $delta = $time - $t;
905 next unless ($delta > 10);
907 delete $floodjoin{$chan}{$who};
912 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
918 if (exists $nuh{$n}) {
919 return &makeHostMask($nuh{$n});
921 $cache{on_who_Hack} = 1;