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(@ircServers);
28 while (my $host = shift @ircServers) {
29 # JUST IN CASE. irq was complaining about this.
30 if ($lastrun == time()) {
31 &DEBUG("ircloop: hrm... lastrun == time()");
38 &DEBUG("ircloop: ircServers[x] = NULL.");
42 next unless (exists $ircPort{$host});
44 my $retval = &irc($host, $ircPort{$host});
45 next unless (defined $retval and $retval == 0);
48 if ($error % 3 == 0 and $error != 0) {
49 &status("IRC: Could not connect.");
55 &status("IRC: cannot connect to any IRC servers; stopping.");
61 &status("IRC: ok, done one cycle of IRC servers; trying again.");
68 my ($server,$port) = @_;
70 my $iaddr = inet_aton($server);
71 my $paddr = sockaddr_in($port, $iaddr);
72 my $proto = getprotobyname('tcp');
75 &status("Connecting to port $port of server $server ...");
78 if ($server =~ /\D$/) {
79 my $packed = scalar(gethostbyname($server));
81 if (!defined $packed) {
82 &status(" cannot resolve $server.");
86 my $resolve = inet_ntoa($packed);
87 &status(" resolved to $resolve.");
88 ### warning in Sys/Hostname line 78???
89 ### caused inside Net::IRC?
95 Nick => $param{'ircNick'},
98 Ircname => $param{'ircName'},
100 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
101 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
103 my $conn = $irc->newconn(%args);
105 if (!defined $conn) {
106 &ERROR("IRC: connection failed.");
107 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
108 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
109 &ERROR("127.0.0.1 localhost localhost");
110 &ERROR("If this is still a problem, please contact the maintainer.");
113 $conn->maxlinelen($maxlinelen);
115 if ($param{'ircNick2'}) {
116 # prep for real multiple nick/server connects
117 # FIXME: all code should get nick/server out of self, not config
118 $args{'Nick'} = $param{'ircNick2'};
119 my $conn = $irc->newconn(%args);
120 $conn->maxlinelen($maxlinelen);
123 if ($param{'ircNick3'}) {
124 # prep for real multiple nick/server connects
125 # FIXME: all code should get nick/server out of self, not config
126 $args{'Nick'} = $param{'ircNick3'};
127 my $conn = $irc->newconn(%args);
128 $conn->maxlinelen($maxlinelen);
133 # change internal timeout value for scheduler.
134 $irc->{_timeout} = 10; # how about 60?
135 # Net::IRC debugging.
138 $ircstats{'Server'} = "$server:$port";
141 $conn->add_global_handler('caction', \&on_action);
142 $conn->add_global_handler('cdcc', \&on_dcc);
143 $conn->add_global_handler('cping', \&on_ping);
144 $conn->add_global_handler('crping', \&on_ping_reply);
145 $conn->add_global_handler('cversion', \&on_version);
146 $conn->add_global_handler('crversion', \&on_crversion);
147 $conn->add_global_handler('dcc_open', \&on_dcc_open);
148 $conn->add_global_handler('dcc_close', \&on_dcc_close);
149 $conn->add_global_handler('chat', \&on_chat);
150 $conn->add_global_handler('msg', \&on_msg);
151 $conn->add_global_handler('public', \&on_public);
152 $conn->add_global_handler('join', \&on_join);
153 $conn->add_global_handler('part', \&on_part);
154 $conn->add_global_handler('topic', \&on_topic);
155 $conn->add_global_handler('invite', \&on_invite);
156 $conn->add_global_handler('kick', \&on_kick);
157 $conn->add_global_handler('mode', \&on_mode);
158 $conn->add_global_handler('nick', \&on_nick);
159 $conn->add_global_handler('quit', \&on_quit);
160 $conn->add_global_handler('notice', \&on_notice);
161 $conn->add_global_handler('whoischannels', \&on_whoischannels);
162 $conn->add_global_handler('useronchannel', \&on_useronchannel);
163 $conn->add_global_handler('whois', \&on_whois);
164 $conn->add_global_handler('other', \&on_other);
165 $conn->add_global_handler('disconnect', \&on_disconnect);
166 $conn->add_global_handler([251,252,253,254,255], \&on_init);
167 # $conn->add_global_handler(302, \&on_init); # userhost
168 $conn->add_global_handler(303, \&on_ison); # notify.
169 $conn->add_global_handler(315, \&on_endofwho);
170 $conn->add_global_handler(422, \&on_endofwho); # nomotd.
171 $conn->add_global_handler(324, \&on_modeis);
172 $conn->add_global_handler(333, \&on_topicinfo);
173 $conn->add_global_handler(352, \&on_who);
174 $conn->add_global_handler(353, \&on_names);
175 $conn->add_global_handler(366, \&on_endofnames);
176 $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
177 $conn->add_global_handler(433, \&on_nick_taken);
178 $conn->add_global_handler(439, \&on_targettoofast);
179 # for proper joinnextChan behaviour
180 $conn->add_global_handler(471, \&on_chanfull);
181 $conn->add_global_handler(473, \&on_inviteonly);
182 $conn->add_global_handler(474, \&on_banned);
183 $conn->add_global_handler(475, \&on_badchankey);
184 $conn->add_global_handler(443, \&on_useronchan);
186 # end of handler stuff.
191 ######################################################################
192 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
193 ######################################################################
199 # slow down a bit if traffic is "high".
200 # need to take into account time of last message sent.
201 if ($last{buflen} > 256 and length($buf) > 256) {
205 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
207 $last{buflen} = length($buf);
214 &WARN("say: msg == $msg.");
218 &status("</$talkchannel> $msg");
219 if (&whatInterface() =~ /IRC/) {
220 $msg = "zero" if ($msg =~ /^0+$/);
223 if ($t == $pubtime) {
225 $pubsize += length $msg;
227 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
228 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
230 if ( ($pubcount % $i) == 0 and $pubcount) {
232 } elsif ($pubsize > $j) {
240 $pubsize = length $msg;
243 $conn->privmsg($talkchannel, $msg);
248 my ($nick, $msg) = @_;
249 if (!defined $nick) {
250 &ERROR("msg: nick == NULL.");
256 &WARN("msg: msg == $msg.");
260 &status(">$nick< $msg");
262 return unless (&whatInterface() =~ /IRC/);
265 if ($t == $msgtime) {
267 $msgsize += length $msg;
269 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
270 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
271 if ( ($msgcount % $i) == 0 and $msgcount) {
273 } elsif ($msgsize > $j) {
281 $msgsize = length $msg;
284 $conn->privmsg($nick, $msg);
287 # Usage: &action(nick || chan, txt);
289 my ($target, $txt) = @_;
291 &WARN("action: txt == NULL.");
295 if (length $txt > 480) {
296 &status("action: txt too long; truncating.");
297 chop($txt) while (length $txt > 480);
300 &status("* $ident/$target $txt");
301 $conn->me($target, $txt);
304 # Usage: ¬ice(nick || chan, txt);
306 my ($target, $txt) = @_;
308 &WARN("notice: txt == NULL.");
312 &status("-$target- $txt");
316 if ($t == $nottime) {
318 $notsize += length $txt;
320 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
321 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
323 if ( ($notcount % $i) == 0 and $notcount) {
325 } elsif ($notsize > $j) {
333 $notsize = length $txt;
336 $conn->notice($target, $txt);
340 my ($txt,$flag) = @_;
342 ### FIXME: flag not supported yet.
344 foreach (keys %{ $dcc{'CHAT'} }) {
345 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
350 ### perform commands.
353 # Usage: &performReply($reply);
357 if (!defined $reply or $reply =~ /^\s*$/) {
358 &DEBUG("performReply: reply == NULL.");
362 $reply =~ /([\.\?\s]+)$/;
364 &checkMsgType($reply);
366 if ($msgType eq 'public') {
367 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
368 $reply = "$orig{who}: ".$reply;
370 $reply = "$reply, ".$orig{who};
374 } elsif ($msgType eq 'private') {
376 $reply = "$reply, ".$orig{who};
380 } elsif ($msgType eq 'chat') {
381 if (!exists $dcc{'CHAT'}{$who}) {
382 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
385 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
388 &ERROR("PR: msgType invalid? ($msgType).");
393 sub performAddressedReply {
394 return unless ($addressed);
399 &performStrictReply(@_);
402 # Usage: &performStrictReply($reply);
403 sub performStrictReply {
406 &checkMsgType($reply);
408 if ($msgType eq 'private') {
410 } elsif ($msgType eq 'public') {
412 } elsif ($msgType eq 'chat') {
413 &dccsay(lc $who, $reply);
415 &ERROR("pSR: msgType invalid? ($msgType).");
420 my($who, $reply) = @_;
422 if (!defined $reply or $reply =~ /^\s*$/) {
423 &WARN("dccsay: reply == NULL.");
427 if (!exists $dcc{'CHAT'}{$who}) {
428 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
432 &status("=>$who<= $reply"); # dcc chat.
433 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
440 foreach $type (keys %dcc) {
441 &FIXME("dcc_close: $who");
442 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
443 next unless (scalar @who);
445 &DEBUG("dcc_close... close $who!");
451 my $key = &getChanConf("chankey", $chan) || "";
453 # forgot for about 2 years to implement channel keys when moving
454 # over to Net::IRC...
456 # hopefully validChan is right.
457 if (&validChan($chan)) {
458 &status("join: already on $chan?");
461 &status("joining $b_blue$chan$ob");
463 return if ($conn->join($chan, $key));
464 return if (&validChan($chan));
466 &DEBUG("joinchan: join failed. trying connect!");
476 next if ($chan eq "");
477 $chan =~ tr/A-Z/a-z/; # lowercase.
479 if ($chan !~ /^$mask{chan}$/) {
480 &WARN("part: chan is invalid ($chan)");
484 &status("parting $chan");
485 if (!&validChan($chan)) {
486 &WARN("part: not on $chan; doing anyway");
491 # deletion of $channels{chan} is done in &entryEvt().
496 my ($chan, @modes) = @_;
497 my $modes = join(" ", @modes);
499 if (&validChan($chan) == 0) {
500 &ERROR("mode: invalid chan => '$chan'.");
504 &DEBUG("mode: MODE $chan $modes");
506 # should move to use Net::IRC's $conn->mode()... but too lazy.
507 rawout("MODE $chan $modes");
511 my ($chan, @who) = @_;
512 my $os = "o" x scalar(@who);
514 &mode($chan, "+$os @who");
518 my ($chan, @who) = @_;
519 my $os = "o" x scalar(@who);
521 &mode($chan, "-$os ".@who);
525 my ($nick,$chan,$msg) = @_;
526 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
528 if ($chan ne "" and &validChan($chan) == 0) {
529 &ERROR("kick: invalid channel $chan.");
533 $nick =~ tr/A-Z/a-z/;
535 foreach $chan (@chans) {
536 if (!&IsNickInChan($nick,$chan)) {
537 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
541 if (!exists $channels{$chan}{o}{$ident}) {
542 &status("kick: do not have ops on $chan :(");
546 &status("Kicking $nick from $chan.");
547 $conn->kick($chan, $nick, $msg);
552 my ($mask,$chan) = @_;
553 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
556 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
557 &ERROR("ban: invalid channel $chan.");
561 foreach $chan (@chans) {
562 if (!exists $channels{$chan}{o}{$ident}) {
563 &status("ban: do not have ops on $chan :(");
567 &status("Banning $mask from $chan.");
568 &rawout("MODE $chan +b $mask");
576 my ($mask,$chan) = @_;
577 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
580 &DEBUG("unban: mask = $mask, chan = @chans");
582 foreach $chan (@chans) {
583 if (!exists $channels{$chan}{o}{$ident}) {
584 &status("unBan: do not have ops on $chan :(");
588 &status("Removed ban $mask from $chan.");
589 &rawout("MODE $chan -b $mask");
599 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
600 $conn->quit($quitmsg);
602 &WARN("quit: could not quit!");
609 if (!defined $nick) {
610 &ERROR("nick: nick == NULL.");
614 if (defined $ident and $nick eq $ident) {
615 &WARN("nick: nick == ident == '$ident'.");
620 $bad++ if (exists $nuh{$conn->nick()});
621 $bad++ if (&IsNickInAnyChan($conn->nick()));
624 &WARN("Nick: not going to try and get my nick back. [".
625 scalar(gmtime). "]");
626 # hrm... over time we lose track of our own nick.
630 # FIXME broken for multiple nicks
631 # if ($nick =~ /^$mask{nick}$/) {
632 # &rawout("NICK ".$nick);
634 # if (defined $ident) {
635 # &status("nick: Changing nick to $nick (from $ident)");
636 # # following shouldn't be here :(
639 # &DEBUG("first time nick change.");
645 &DEBUG("nick: failed... why oh why (nick => $nick)");
651 my($who, $chan) = @_;
652 # TODO: check if $who or $chan are invalid.
654 $conn->invite($who, $chan);
658 # Channel related functions...
661 # Usage: &joinNextChan();
663 my @join = getJoinChans(1);
664 my $mynick = "UNDEFINED";
666 $mynick = $conn->nick() if $conn;
668 my $chan = shift @join;
671 if (my $i = scalar @join) {
672 &status("joinNextChan: $mynick $i chans to join.");
678 if (exists $cache{joinTime}) {
679 my $delta = time() - $cache{joinTime} - 5;
680 my $timestr = &Time2String($delta);
681 # FIXME: @join should be @in instead (hacked to 10)
682 #my $rate = sprintf("%.1f", $delta / @in);
683 my $rate = sprintf("%.1f", $delta / 10);
684 delete $cache{joinTime};
686 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
689 # chanserv check: global channels, in case we missed one.
690 foreach ( &ChanConfList("chanServ_ops") ) {
695 # Usage: &getNickInChans($nick);
700 foreach (keys %channels) {
701 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
708 # Usage: &getNicksInChan($chan);
713 return keys %{ $channels{$chan}{''} };
717 my ($nick,$chan) = @_;
719 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
722 &DEBUG("INIC: chan == NULL.");
726 if (&validChan($chan) == 0) {
727 &ERROR("INIC: invalid channel $chan.");
731 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
734 foreach (keys %channels) {
735 next unless (/[A-Z]/);
736 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
742 sub IsNickInAnyChan {
746 foreach $chan (keys %channels) {
747 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
753 # Usage: &validChan($chan);
755 # TODO: use $c instead?
758 if (!defined $chan or $chan =~ /^\s*$/) {
762 if (lc $chan ne $chan) {
763 &WARN("validChan: lc chan != chan. ($chan); fixing.");
764 $chan =~ tr/A-Z/a-z/;
767 # it's possible that this check creates the hash if empty.
768 if (defined $channels{$chan} or exists $channels{$chan}) {
769 if ($chan =~ /^_?default$/) {
770 # &WARN("validC: chan cannot be _default! returning 0!");
781 # Usage: &delUserInfo($nick,@chans);
783 my ($nick,@chans) = @_;
786 foreach $chan (@chans) {
787 foreach $mode (keys %{ $channels{$chan} }) {
789 next unless (exists $channels{$chan}{$mode}{$nick});
791 delete $channels{$chan}{$mode}{$nick};
799 delete $channels{$chan};
806 $cache{joinTime} = time();
816 # can't join any if not connected
817 return @join if (!$conn);
819 my $nick = $conn->nick();
821 foreach (keys %chanconf) {
822 next if ($_ eq "_default");
825 my $val = $chanconf{$_}{autojoin};
828 $skip++ if ($val eq "0");
830 # convert old +autojoin to autojoin <nick>
832 $chanconf{$_}{autojoin} = $val;
834 $skip++ if ($val ne $nick);
842 if (defined $channels{$_} or exists $channels{$_}) {
851 $str .= ' in:' . join(',', sort @in) if scalar @in;
852 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
853 $str .= ' join:' . join(',', sort @join) if scalar @join;
855 &status("Chans: ($nick)$str") if ($show);
861 # &DEBUG("closeDCC called.");
864 foreach $type (keys %dcc) {
865 next if ($type ne uc($type));
868 foreach $nick (keys %{ $dcc{$type} }) {
869 next unless (defined $nick);
870 &status("DCC CHAT: closing DCC $type to $nick.");
871 next unless (defined $dcc{$type}{$nick});
873 my $ref = $dcc{$type}{$nick};
874 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
875 $dcc{$type}{$nick}->close();
876 delete $dcc{$type}{$nick};
877 &DEBUG("after close for $nick");
884 my($who,$chan,$userhost) = @_;
886 return unless (&IsChanConf("joinfloodCheck"));
888 if (exists $netsplit{lc $who}) { # netsplit join.
889 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
892 if (exists $floodjoin{$chan}{$who}{Time}) {
893 &WARN("floodjoin{$chan}{$who} already exists?");
896 $floodjoin{$chan}{$who}{Time} = time();
897 $floodjoin{$chan}{$who}{Host} = $userhost;
900 foreach (keys %floodjoin) {
902 my $count = scalar keys %{ $floodjoin{$c} };
903 next unless ($count > 5);
904 &DEBUG("joinflood: count => $count");
907 foreach (keys %{ $floodjoin{$c} }) {
908 my $t = $floodjoin{$c}{$_}{Time};
909 next unless (defined $t);
913 &DEBUG("joinflood: time => $time");
916 &DEBUG("joinflood: new time => $time");
922 foreach $chan (keys %floodjoin) {
923 foreach $who (keys %{ $floodjoin{$chan} }) {
924 my $t = $floodjoin{$chan}{$who}{Time};
925 next unless (defined $t);
927 my $delta = $time - $t;
928 next unless ($delta > 10);
930 delete $floodjoin{$chan}{$who};
935 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
941 if (exists $nuh{$n}) {
942 return &makeHostMask($nuh{$n});
944 $cache{on_who_Hack} = 1;