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 %conns %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) = @_;
72 # TODO: move all this to an sql table
73 my $iaddr = inet_aton($server);
74 my $paddr = sockaddr_in($port, $iaddr);
75 my $proto = getprotobyname('tcp');
82 if ($server =~ /\D$/) {
83 my $packed = scalar(gethostbyname($server));
85 if (!defined $packed) {
86 &status(" cannot resolve $server.");
90 $resolve = inet_ntoa($packed);
91 ### warning in Sys/Hostname line 78???
92 ### caused inside Net::IRC?
96 Nick => $param{'ircNick'},
99 Ircname => $param{'ircName'},
101 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
102 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
104 foreach my $mynick (split ',', $param{'ircNick'}) {
105 &DEBUG("got $mynick");
106 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
107 $args{'Nick'} = $mynick;
108 $conns{$mynick} = $irc->newconn(%args);
109 if (!defined $conns{$mynick}) {
110 &ERROR("IRC: connection failed.");
111 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
112 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
113 &ERROR("127.0.0.1 localhost localhost");
114 &ERROR("If this is still a problem, please contact the maintainer.");
116 $conns{$mynick}->maxlinelen($maxlinelen);
118 $conns{$mynick}->add_global_handler('caction', \&on_action);
119 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
120 $conns{$mynick}->add_global_handler('cping', \&on_ping);
121 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
122 $conns{$mynick}->add_global_handler('cversion', \&on_version);
123 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
124 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
125 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
126 $conns{$mynick}->add_global_handler('chat', \&on_chat);
127 $conns{$mynick}->add_global_handler('msg', \&on_msg);
128 $conns{$mynick}->add_global_handler('public', \&on_public);
129 $conns{$mynick}->add_global_handler('join', \&on_join);
130 $conns{$mynick}->add_global_handler('part', \&on_part);
131 $conns{$mynick}->add_global_handler('topic', \&on_topic);
132 $conns{$mynick}->add_global_handler('invite', \&on_invite);
133 $conns{$mynick}->add_global_handler('kick', \&on_kick);
134 $conns{$mynick}->add_global_handler('mode', \&on_mode);
135 $conns{$mynick}->add_global_handler('nick', \&on_nick);
136 $conns{$mynick}->add_global_handler('quit', \&on_quit);
137 $conns{$mynick}->add_global_handler('notice', \&on_notice);
138 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
139 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
140 $conns{$mynick}->add_global_handler('whois', \&on_whois);
141 $conns{$mynick}->add_global_handler('other', \&on_other);
142 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
143 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
144 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
145 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
146 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
147 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
148 $conns{$mynick}->add_global_handler(324, \&on_modeis);
149 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
150 $conns{$mynick}->add_global_handler(352, \&on_who);
151 $conns{$mynick}->add_global_handler(353, \&on_names);
152 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
153 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
154 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
155 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
156 # for proper joinnextChan behaviour
157 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
158 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
159 $conns{$mynick}->add_global_handler(474, \&on_banned);
160 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
161 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
162 # end of handler stuff.
167 # change internal timeout value for scheduler.
168 $irc->{_timeout} = 10; # how about 60?
169 # Net::IRC debugging.
172 $ircstats{'Server'} = "$server:$port";
177 ######################################################################
178 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
179 ######################################################################
185 # slow down a bit if traffic is "high".
186 # need to take into account time of last message sent.
187 if ($last{buflen} > 256 and length($buf) > 256) {
191 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
193 $last{buflen} = length($buf);
198 my $mynick = $conn->nick();
201 &WARN("say: msg == $msg.");
206 &status("<$mynick/$talkchannel> $msg");
207 if (&whatInterface() =~ /IRC/) {
208 $msg = "zero" if ($msg =~ /^0+$/);
211 if ($t == $pubtime) {
213 $pubsize += length $msg;
215 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
216 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
218 if ( ($pubcount % $i) == 0 and $pubcount) {
220 } elsif ($pubsize > $j) {
228 $pubsize = length $msg;
231 $conn->privmsg($talkchannel, $msg);
236 my ($nick, $msg) = @_;
237 if (!defined $nick) {
238 &ERROR("msg: nick == NULL.");
244 &WARN("msg: msg == $msg.");
248 &status(">$nick< $msg");
250 return unless (&whatInterface() =~ /IRC/);
253 if ($t == $msgtime) {
255 $msgsize += length $msg;
257 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
258 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
259 if ( ($msgcount % $i) == 0 and $msgcount) {
261 } elsif ($msgsize > $j) {
269 $msgsize = length $msg;
272 $conn->privmsg($nick, $msg);
275 # Usage: &action(nick || chan, txt);
277 my ($target, $txt) = @_;
279 &WARN("action: txt == NULL.");
283 if (length $txt > 480) {
284 &status("action: txt too long; truncating.");
285 chop($txt) while (length $txt > 480);
288 &status("* $ident/$target $txt");
289 $conn->me($target, $txt);
292 # Usage: ¬ice(nick || chan, txt);
294 my ($target, $txt) = @_;
296 &WARN("notice: txt == NULL.");
300 &status("-$target- $txt");
304 if ($t == $nottime) {
306 $notsize += length $txt;
308 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
309 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
311 if ( ($notcount % $i) == 0 and $notcount) {
313 } elsif ($notsize > $j) {
321 $notsize = length $txt;
324 $conn->notice($target, $txt);
328 my ($txt,$flag) = @_;
330 ### FIXME: flag not supported yet.
332 foreach (keys %{ $dcc{'CHAT'} }) {
333 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
338 ### perform commands.
341 # Usage: &performReply($reply);
345 if (!defined $reply or $reply =~ /^\s*$/) {
346 &DEBUG("performReply: reply == NULL.");
350 $reply =~ /([\.\?\s]+)$/;
352 &checkMsgType($reply);
354 if ($msgType eq 'public') {
355 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
356 $reply = "$orig{who}: ".$reply;
358 $reply = "$reply, ".$orig{who};
362 } elsif ($msgType eq 'private') {
364 $reply = "$reply, ".$orig{who};
368 } elsif ($msgType eq 'chat') {
369 if (!exists $dcc{'CHAT'}{$who}) {
370 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
373 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
376 &ERROR("PR: msgType invalid? ($msgType).");
381 sub performAddressedReply {
382 return unless ($addressed);
387 &performStrictReply(@_);
390 # Usage: &performStrictReply($reply);
391 sub performStrictReply {
394 &checkMsgType($reply);
396 if ($msgType eq 'private') {
398 } elsif ($msgType eq 'public') {
400 } elsif ($msgType eq 'chat') {
401 &dccsay(lc $who, $reply);
403 &ERROR("pSR: msgType invalid? ($msgType).");
408 my($who, $reply) = @_;
410 if (!defined $reply or $reply =~ /^\s*$/) {
411 &WARN("dccsay: reply == NULL.");
415 if (!exists $dcc{'CHAT'}{$who}) {
416 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
420 &status("=>$who<= $reply"); # dcc chat.
421 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
428 foreach $type (keys %dcc) {
429 &FIXME("dcc_close: $who");
430 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
431 next unless (scalar @who);
433 &DEBUG("dcc_close... close $who!");
439 my $key = &getChanConf("chankey", $chan) || "";
441 # forgot for about 2 years to implement channel keys when moving
442 # over to Net::IRC...
444 # hopefully validChan is right.
445 if (&validChan($chan)) {
446 &status("join: already on $chan?");
449 &status("joining $b_blue$chan$ob");
451 return if ($conn->join($chan, $key));
452 return if (&validChan($chan));
454 &DEBUG("joinchan: join failed. trying connect!");
464 next if ($chan eq "");
465 $chan =~ tr/A-Z/a-z/; # lowercase.
467 if ($chan !~ /^$mask{chan}$/) {
468 &WARN("part: chan is invalid ($chan)");
472 &status("parting $chan");
473 if (!&validChan($chan)) {
474 &WARN("part: not on $chan; doing anyway");
479 # deletion of $channels{chan} is done in &entryEvt().
484 my ($chan, @modes) = @_;
485 my $modes = join(" ", @modes);
487 if (&validChan($chan) == 0) {
488 &ERROR("mode: invalid chan => '$chan'.");
492 &DEBUG("mode: MODE $chan $modes");
494 # should move to use Net::IRC's $conn->mode()... but too lazy.
495 rawout("MODE $chan $modes");
499 my ($chan, @who) = @_;
500 my $os = "o" x scalar(@who);
502 &mode($chan, "+$os @who");
506 my ($chan, @who) = @_;
507 my $os = "o" x scalar(@who);
509 &mode($chan, "-$os ".@who);
513 my ($nick,$chan,$msg) = @_;
514 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
516 if ($chan ne "" and &validChan($chan) == 0) {
517 &ERROR("kick: invalid channel $chan.");
521 $nick =~ tr/A-Z/a-z/;
523 foreach $chan (@chans) {
524 if (!&IsNickInChan($nick,$chan)) {
525 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
529 if (!exists $channels{$chan}{o}{$ident}) {
530 &status("kick: do not have ops on $chan :(");
534 &status("Kicking $nick from $chan.");
535 $conn->kick($chan, $nick, $msg);
540 my ($mask,$chan) = @_;
541 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
544 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
545 &ERROR("ban: invalid channel $chan.");
549 foreach $chan (@chans) {
550 if (!exists $channels{$chan}{o}{$ident}) {
551 &status("ban: do not have ops on $chan :(");
555 &status("Banning $mask from $chan.");
556 &rawout("MODE $chan +b $mask");
564 my ($mask,$chan) = @_;
565 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
568 &DEBUG("unban: mask = $mask, chan = @chans");
570 foreach $chan (@chans) {
571 if (!exists $channels{$chan}{o}{$ident}) {
572 &status("unBan: do not have ops on $chan :(");
576 &status("Removed ban $mask from $chan.");
577 &rawout("MODE $chan -b $mask");
587 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
588 $conn->quit($quitmsg);
590 &WARN("quit: could not quit!");
597 if (!defined $nick) {
598 &ERROR("nick: nick == NULL.");
602 if (defined $ident and $nick eq $ident) {
603 &WARN("nick: nick == ident == '$ident'.");
608 $bad++ if (exists $nuh{$conn->nick()});
609 $bad++ if (&IsNickInAnyChan($conn->nick()));
612 &WARN("Nick: not going to try and get my nick back. [".
613 scalar(gmtime). "]");
614 # hrm... over time we lose track of our own nick.
618 # FIXME broken for multiple nicks
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 my $mynick = "UNDEF";
652 $mynick = $conn->nick() if $conn;
653 my @join = getJoinChans(1);
656 my $chan = shift @join;
659 if (my $i = scalar @join) {
660 &status("joinNextChan: $mynick $i chans to join.");
665 if (exists $cache{joinTime}) {
666 my $delta = time() - $cache{joinTime} - 5;
667 my $timestr = &Time2String($delta);
668 # FIXME: @join should be @in instead (hacked to 10)
669 #my $rate = sprintf("%.1f", $delta / @in);
670 my $rate = sprintf("%.1f", $delta / 10);
671 delete $cache{joinTime};
673 &status("time taken for $mynick to join all chans: $timestr; rate: $rate sec/join");
676 # chanserv check: global channels, in case we missed one.
677 foreach ( &ChanConfList("chanServ_ops") ) {
682 # Usage: &getNickInChans($nick);
687 foreach (keys %channels) {
688 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
695 # Usage: &getNicksInChan($chan);
700 return keys %{ $channels{$chan}{''} };
704 my ($nick,$chan) = @_;
706 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
709 &DEBUG("INIC: chan == NULL.");
713 if (&validChan($chan) == 0) {
714 &ERROR("INIC: invalid channel $chan.");
718 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
721 foreach (keys %channels) {
722 next unless (/[A-Z]/);
723 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
729 sub IsNickInAnyChan {
733 foreach $chan (keys %channels) {
734 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
740 # Usage: &validChan($chan);
742 # TODO: use $c instead?
745 if (!defined $chan or $chan =~ /^\s*$/) {
749 if (lc $chan ne $chan) {
750 &WARN("validChan: lc chan != chan. ($chan); fixing.");
751 $chan =~ tr/A-Z/a-z/;
754 # it's possible that this check creates the hash if empty.
755 if (defined $channels{$chan} or exists $channels{$chan}) {
756 if ($chan =~ /^_?default$/) {
757 # &WARN("validC: chan cannot be _default! returning 0!");
768 # Usage: &delUserInfo($nick,@chans);
770 my ($nick,@chans) = @_;
773 foreach $chan (@chans) {
774 foreach $mode (keys %{ $channels{$chan} }) {
776 next unless (exists $channels{$chan}{$mode}{$nick});
778 delete $channels{$chan}{$mode}{$nick};
786 delete $channels{$chan};
793 $cache{joinTime} = time();
803 # can't join any if not connected
804 return @join if (!$conn);
806 my $nick = $conn->nick();
808 foreach (keys %chanconf) {
809 next if ($_ eq "_default");
812 my $val = $chanconf{$_}{autojoin};
815 $skip++ if ($val eq "0");
817 # convert old +autojoin to autojoin <nick>
819 $chanconf{$_}{autojoin} = $val;
821 $skip++ if ($val ne $nick);
829 if (defined $channels{$_} or exists $channels{$_}) {
838 $str .= ' in:' . join(',', sort @in) if scalar @in;
839 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
840 $str .= ' join:' . join(',', sort @join) if scalar @join;
842 &status("Chans: ($nick)$str") if ($show);
848 # &DEBUG("closeDCC called.");
851 foreach $type (keys %dcc) {
852 next if ($type ne uc($type));
855 foreach $nick (keys %{ $dcc{$type} }) {
856 next unless (defined $nick);
857 &status("DCC CHAT: closing DCC $type to $nick.");
858 next unless (defined $dcc{$type}{$nick});
860 my $ref = $dcc{$type}{$nick};
861 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
862 $dcc{$type}{$nick}->close();
863 delete $dcc{$type}{$nick};
864 &DEBUG("after close for $nick");
871 my($who,$chan,$userhost) = @_;
873 return unless (&IsChanConf("joinfloodCheck"));
875 if (exists $netsplit{lc $who}) { # netsplit join.
876 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
879 if (exists $floodjoin{$chan}{$who}{Time}) {
880 &WARN("floodjoin{$chan}{$who} already exists?");
883 $floodjoin{$chan}{$who}{Time} = time();
884 $floodjoin{$chan}{$who}{Host} = $userhost;
887 foreach (keys %floodjoin) {
889 my $count = scalar keys %{ $floodjoin{$c} };
890 next unless ($count > 5);
891 &DEBUG("joinflood: count => $count");
894 foreach (keys %{ $floodjoin{$c} }) {
895 my $t = $floodjoin{$c}{$_}{Time};
896 next unless (defined $t);
900 &DEBUG("joinflood: time => $time");
903 &DEBUG("joinflood: new time => $time");
909 foreach $chan (keys %floodjoin) {
910 foreach $who (keys %{ $floodjoin{$chan} }) {
911 my $t = $floodjoin{$chan}{$who}{Time};
912 next unless (defined $t);
914 my $delta = $time - $t;
915 next unless ($delta > 10);
917 delete $floodjoin{$chan}{$who};
922 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
928 if (exists $nuh{$n}) {
929 return &makeHostMask($nuh{$n});
931 $cache{on_who_Hack} = 1;