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 $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 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
106 $args{'Nick'} = $mynick;
107 $conns{$mynick} = $irc->newconn(%args);
108 if (!defined $conns{$mynick}) {
109 &ERROR("IRC: connection failed.");
110 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
111 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
112 &ERROR("127.0.0.1 localhost localhost");
113 &ERROR("If this is still a problem, please contact the maintainer.");
115 $conns{$mynick}->maxlinelen($maxlinelen);
117 $conns{$mynick}->add_global_handler('caction', \&on_action);
118 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
119 $conns{$mynick}->add_global_handler('cping', \&on_ping);
120 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
121 $conns{$mynick}->add_global_handler('cversion', \&on_version);
122 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
123 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
124 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
125 $conns{$mynick}->add_global_handler('chat', \&on_chat);
126 $conns{$mynick}->add_global_handler('msg', \&on_msg);
127 $conns{$mynick}->add_global_handler('public', \&on_public);
128 $conns{$mynick}->add_global_handler('join', \&on_join);
129 $conns{$mynick}->add_global_handler('part', \&on_part);
130 $conns{$mynick}->add_global_handler('topic', \&on_topic);
131 $conns{$mynick}->add_global_handler('invite', \&on_invite);
132 $conns{$mynick}->add_global_handler('kick', \&on_kick);
133 $conns{$mynick}->add_global_handler('mode', \&on_mode);
134 $conns{$mynick}->add_global_handler('nick', \&on_nick);
135 $conns{$mynick}->add_global_handler('quit', \&on_quit);
136 $conns{$mynick}->add_global_handler('notice', \&on_notice);
137 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
138 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
139 $conns{$mynick}->add_global_handler('whois', \&on_whois);
140 $conns{$mynick}->add_global_handler('other', \&on_other);
141 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
142 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
143 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
144 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
145 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
146 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
147 $conns{$mynick}->add_global_handler(324, \&on_modeis);
148 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
149 $conns{$mynick}->add_global_handler(352, \&on_who);
150 $conns{$mynick}->add_global_handler(353, \&on_names);
151 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
152 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
153 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
154 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
155 # for proper joinnextChan behaviour
156 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
157 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
158 $conns{$mynick}->add_global_handler(474, \&on_banned);
159 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
160 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
161 # end of handler stuff.
166 # change internal timeout value for scheduler.
167 $irc->{_timeout} = 10; # how about 60?
168 # Net::IRC debugging.
171 $ircstats{'Server'} = "$server:$port";
176 ######################################################################
177 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
178 ######################################################################
184 # slow down a bit if traffic is "high".
185 # need to take into account time of last message sent.
186 if ($last{buflen} > 256 and length($buf) > 256) {
190 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
192 $last{buflen} = length($buf);
197 my $mynick = $conn->nick();
200 &WARN("say: msg == $msg.");
205 &status("<$mynick/$talkchannel> $msg");
206 if (&whatInterface() =~ /IRC/) {
207 $msg = "zero" if ($msg =~ /^0+$/);
210 if ($t == $pubtime) {
212 $pubsize += length $msg;
214 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
215 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
217 if ( ($pubcount % $i) == 0 and $pubcount) {
219 } elsif ($pubsize > $j) {
227 $pubsize = length $msg;
230 $conn->privmsg($talkchannel, $msg);
235 my ($nick, $msg) = @_;
236 if (!defined $nick) {
237 &ERROR("msg: nick == NULL.");
243 &WARN("msg: msg == $msg.");
247 &status(">$nick< $msg");
249 return unless (&whatInterface() =~ /IRC/);
252 if ($t == $msgtime) {
254 $msgsize += length $msg;
256 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
257 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
258 if ( ($msgcount % $i) == 0 and $msgcount) {
260 } elsif ($msgsize > $j) {
268 $msgsize = length $msg;
271 $conn->privmsg($nick, $msg);
274 # Usage: &action(nick || chan, txt);
276 my $mynick = $conn->nick();
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("* $mynick/$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 # FIXME need real throttling....
353 if (length($reply) > $maxlinelen - 30) {
354 $reply = substr($reply, 0, $maxlinelen - 33);
355 $reply =~ s/ [^ ]*?$/ .../;
357 &checkMsgType($reply);
359 if ($msgType eq 'public') {
360 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
361 $reply = "$orig{who}: ".$reply;
363 $reply = "$reply, ".$orig{who};
367 } elsif ($msgType eq 'private') {
369 $reply = "$reply, ".$orig{who};
373 } elsif ($msgType eq 'chat') {
374 if (!exists $dcc{'CHAT'}{$who}) {
375 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
378 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
381 &ERROR("PR: msgType invalid? ($msgType).");
386 sub performAddressedReply {
387 return unless ($addressed);
392 &performStrictReply(@_);
395 # Usage: &performStrictReply($reply);
396 sub performStrictReply {
399 # FIXME need real throttling....
400 if (length($reply) > $maxlinelen - 30) {
401 $reply = substr($reply, 0, $maxlinelen - 33);
402 $reply =~ s/ [^ ]*?$/ .../;
404 &checkMsgType($reply);
406 if ($msgType eq 'private') {
408 } elsif ($msgType eq 'public') {
410 } elsif ($msgType eq 'chat') {
411 &dccsay(lc $who, $reply);
413 &ERROR("pSR: msgType invalid? ($msgType).");
418 my($who, $reply) = @_;
420 if (!defined $reply or $reply =~ /^\s*$/) {
421 &WARN("dccsay: reply == NULL.");
425 if (!exists $dcc{'CHAT'}{$who}) {
426 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
430 &status("=>$who<= $reply"); # dcc chat.
431 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
438 foreach $type (keys %dcc) {
439 &FIXME("dcc_close: $who");
440 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
441 next unless (scalar @who);
443 &DEBUG("dcc_close... close $who!");
449 my $key = &getChanConf("chankey", $chan) || "";
451 # forgot for about 2 years to implement channel keys when moving
452 # over to Net::IRC...
454 # hopefully validChan is right.
455 if (&validChan($chan)) {
456 &status("join: already on $chan?");
459 &status("joining $b_blue$chan$ob");
461 return if ($conn->join($chan, $key));
462 return if (&validChan($chan));
464 &DEBUG("joinchan: join failed. trying connect!");
474 next if ($chan eq "");
475 $chan =~ tr/A-Z/a-z/; # lowercase.
477 if ($chan !~ /^$mask{chan}$/) {
478 &WARN("part: chan is invalid ($chan)");
482 &status("parting $chan");
483 if (!&validChan($chan)) {
484 &WARN("part: not on $chan; doing anyway");
489 # deletion of $channels{chan} is done in &entryEvt().
494 my ($chan, @modes) = @_;
495 my $modes = join(" ", @modes);
497 if (&validChan($chan) == 0) {
498 &ERROR("mode: invalid chan => '$chan'.");
502 &DEBUG("mode: MODE $chan $modes");
504 # should move to use Net::IRC's $conn->mode()... but too lazy.
505 rawout("MODE $chan $modes");
509 my ($chan, @who) = @_;
510 my $os = "o" x scalar(@who);
512 &mode($chan, "+$os @who");
516 my ($chan, @who) = @_;
517 my $os = "o" x scalar(@who);
519 &mode($chan, "-$os ".@who);
523 my ($nick,$chan,$msg) = @_;
524 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
525 my $mynick = $conn->nick();
527 if ($chan ne "" and &validChan($chan) == 0) {
528 &ERROR("kick: invalid channel $chan.");
532 $nick =~ tr/A-Z/a-z/;
534 foreach $chan (@chans) {
535 if (!&IsNickInChan($nick,$chan)) {
536 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
540 if (!exists $channels{$chan}{o}{$mynick}) {
541 &status("kick: do not have ops on $chan :(");
545 &status("Kicking $nick from $chan.");
546 $conn->kick($chan, $nick, $msg);
551 my ($mask,$chan) = @_;
552 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
553 my $mynick = $conn->nick();
556 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
557 &ERROR("ban: invalid channel $chan.");
561 foreach $chan (@chans) {
562 if (!exists $channels{$chan}{o}{$mynick}) {
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);
578 my $mynick = $conn->nick();
581 &DEBUG("unban: mask = $mask, chan = @chans");
583 foreach $chan (@chans) {
584 if (!exists $channels{$chan}{o}{$mynick}) {
585 &status("unBan: do not have ops on $chan :(");
589 &status("Removed ban $mask from $chan.");
590 &rawout("MODE $chan -b $mask");
600 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
601 $conn->quit($quitmsg);
603 &WARN("quit: could not quit!");
609 my $mynick = $conn->nick();
611 if (!defined $newnick) {
612 &ERROR("nick: nick == NULL.");
616 if (!defined $mynick) {
617 &WARN("nick: mynick == NULL.");
622 $bad++ if (exists $nuh{$newnick});
623 $bad++ if (&IsNickInAnyChan($newnick));
626 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
627 # hrm... over time we lose track of our own nick.
631 if ($newnick =~ /^$mask{nick}$/) {
632 &status("nick: Changing nick from $mynick to $newnick");
633 # ->nick() will NOT change cause we are using rawout?
634 &rawout("NICK $newnick");
637 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
642 my($who, $chan) = @_;
643 # TODO: check if $who or $chan are invalid.
645 $conn->invite($who, $chan);
649 # Channel related functions...
652 # Usage: &joinNextChan();
655 foreach (sort keys %conns) {
657 my $mynick = $conn->nick();
658 my @join = getJoinChans(1);
661 my $chan = shift @join;
664 if (my $i = scalar @join) {
665 &status("joinNextChan: $mynick $i chans to join.");
672 if (exists $cache{joinTime}) {
673 my $delta = time() - $cache{joinTime} - 5;
674 my $timestr = &Time2String($delta);
675 # FIXME: @join should be @in instead (hacked to 10)
676 #my $rate = sprintf("%.1f", $delta / @in);
677 my $rate = sprintf("%.1f", $delta / 10);
678 delete $cache{joinTime};
680 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
683 # chanserv check: global channels, in case we missed one.
684 foreach ( &ChanConfList("chanServ_ops") ) {
689 # Usage: &getNickInChans($nick);
694 foreach (keys %channels) {
695 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
702 # Usage: &getNicksInChan($chan);
707 return keys %{ $channels{$chan}{''} };
711 my ($nick,$chan) = @_;
713 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
716 &DEBUG("INIC: chan == NULL.");
720 if (&validChan($chan) == 0) {
721 &ERROR("INIC: invalid channel $chan.");
725 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
728 foreach (keys %channels) {
729 next unless (/[A-Z]/);
730 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
736 sub IsNickInAnyChan {
740 foreach $chan (keys %channels) {
741 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
747 # Usage: &validChan($chan);
749 # TODO: use $c instead?
752 if (!defined $chan or $chan =~ /^\s*$/) {
756 if (lc $chan ne $chan) {
757 &WARN("validChan: lc chan != chan. ($chan); fixing.");
758 $chan =~ tr/A-Z/a-z/;
761 # it's possible that this check creates the hash if empty.
762 if (defined $channels{$chan} or exists $channels{$chan}) {
763 if ($chan =~ /^_?default$/) {
764 # &WARN("validC: chan cannot be _default! returning 0!");
775 # Usage: &delUserInfo($nick,@chans);
777 my ($nick,@chans) = @_;
780 foreach $chan (@chans) {
781 foreach $mode (keys %{ $channels{$chan} }) {
783 next unless (exists $channels{$chan}{$mode}{$nick});
785 delete $channels{$chan}{$mode}{$nick};
793 delete $channels{$chan};
800 $cache{joinTime} = time();
810 # can't join any if not connected
811 return @join if (!$conn);
813 my $nick = $conn->nick();
815 foreach (keys %chanconf) {
816 next if ($_ eq "_default");
819 my $val = $chanconf{$_}{autojoin};
822 $skip++ if ($val eq "0");
824 # convert old +autojoin to autojoin <nick>
826 $chanconf{$_}{autojoin} = $val;
828 $skip++ if ($val ne $nick);
836 if (defined $channels{$_} or exists $channels{$_}) {
845 $str .= ' in:' . join(',', sort @in) if scalar @in;
846 $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
847 $str .= ' join:' . join(',', sort @join) if scalar @join;
849 &status("Chans: ($nick)$str") if ($show);
855 # &DEBUG("closeDCC called.");
858 foreach $type (keys %dcc) {
859 next if ($type ne uc($type));
862 foreach $nick (keys %{ $dcc{$type} }) {
863 next unless (defined $nick);
864 &status("DCC CHAT: closing DCC $type to $nick.");
865 next unless (defined $dcc{$type}{$nick});
867 my $ref = $dcc{$type}{$nick};
868 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
869 $dcc{$type}{$nick}->close();
870 delete $dcc{$type}{$nick};
871 &DEBUG("after close for $nick");
878 my($who,$chan,$userhost) = @_;
880 return unless (&IsChanConf("joinfloodCheck"));
882 if (exists $netsplit{lc $who}) { # netsplit join.
883 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
886 if (exists $floodjoin{$chan}{$who}{Time}) {
887 &WARN("floodjoin{$chan}{$who} already exists?");
890 $floodjoin{$chan}{$who}{Time} = time();
891 $floodjoin{$chan}{$who}{Host} = $userhost;
894 foreach (keys %floodjoin) {
896 my $count = scalar keys %{ $floodjoin{$c} };
897 next unless ($count > 5);
898 &DEBUG("joinflood: count => $count");
901 foreach (keys %{ $floodjoin{$c} }) {
902 my $t = $floodjoin{$c}{$_}{Time};
903 next unless (defined $t);
907 &DEBUG("joinflood: time => $time");
910 &DEBUG("joinflood: new time => $time");
916 foreach $chan (keys %floodjoin) {
917 foreach $who (keys %{ $floodjoin{$chan} }) {
918 my $t = $floodjoin{$chan}{$who}{Time};
919 next unless (defined $t);
921 my $delta = $time - $t;
922 next unless ($delta > 10);
924 delete $floodjoin{$chan}{$who};
929 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
935 if (exists $nuh{$n}) {
936 return &makeHostMask($nuh{$n});
938 $cache{on_who_Hack} = 1;