2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
11 no strict 'subs'; # IN/STDIN
13 use vars qw(%floodjoin %nuh %dcc %cache %conns %channels %param %mask
14 %chanconf %orig %ircPort %ircstats %last %netsplit);
15 use vars qw($irc $nickserv $conn $msgType $who $talkchannel
16 $addressed $postprocess);
17 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
18 $pubcount $pubtime $pubsize);
19 use vars qw($b_blue $ob);
20 use vars qw(@ircServers);
26 # It's probably closer to 510, but let's be cautious until we calculate it extensively.
34 while (my $host = shift @ircServers) {
35 # JUST IN CASE. irq was complaining about this.
36 if ($lastrun == time()) {
37 &DEBUG("ircloop: hrm... lastrun == time()");
44 &DEBUG("ircloop: ircServers[x] = NULL.");
48 next unless (exists $ircPort{$host});
50 my $retval = &irc($host, $ircPort{$host});
51 next unless (defined $retval and $retval == 0);
54 if ($error % 3 == 0 and $error != 0) {
55 &status("IRC: Could not connect.");
61 &status("IRC: cannot connect to any IRC servers; stopping.");
67 &status("IRC: ok, done one cycle of IRC servers; trying again.");
74 my ($server,$port) = @_;
78 # TODO: move all this to an sql table
79 my $iaddr = inet_aton($server);
80 my $paddr = sockaddr_in($port, $iaddr);
81 my $proto = getprotobyname('tcp');
88 if ($server =~ /\D$/) {
89 my $packed = scalar(gethostbyname($server));
91 if (!defined $packed) {
92 &status(" cannot resolve $server.");
96 $resolve = inet_ntoa($packed);
97 ### warning in Sys/Hostname line 78???
98 ### caused inside Net::IRC?
102 Nick => $param{'ircNick'},
105 Ircname => $param{'ircName'},
107 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
108 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
110 foreach my $mynick (split ',', $param{'ircNick'}) {
111 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
112 $args{'Nick'} = $mynick;
113 $conns{$mynick} = $irc->newconn(%args);
114 if (!defined $conns{$mynick}) {
115 &ERROR("IRC: connection failed.");
116 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
117 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
118 &ERROR("127.0.0.1 localhost localhost");
119 &ERROR("If this is still a problem, please contact the maintainer.");
121 $conns{$mynick}->maxlinelen($maxlinelen);
123 $conns{$mynick}->add_global_handler('caction', \&on_action);
124 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
125 $conns{$mynick}->add_global_handler('cping', \&on_ping);
126 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
127 $conns{$mynick}->add_global_handler('cversion', \&on_version);
128 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
129 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
130 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
131 $conns{$mynick}->add_global_handler('chat', \&on_chat);
132 $conns{$mynick}->add_global_handler('msg', \&on_msg);
133 $conns{$mynick}->add_global_handler('public', \&on_public);
134 $conns{$mynick}->add_global_handler('join', \&on_join);
135 $conns{$mynick}->add_global_handler('part', \&on_part);
136 $conns{$mynick}->add_global_handler('topic', \&on_topic);
137 $conns{$mynick}->add_global_handler('invite', \&on_invite);
138 $conns{$mynick}->add_global_handler('kick', \&on_kick);
139 $conns{$mynick}->add_global_handler('mode', \&on_mode);
140 $conns{$mynick}->add_global_handler('nick', \&on_nick);
141 $conns{$mynick}->add_global_handler('quit', \&on_quit);
142 $conns{$mynick}->add_global_handler('notice', \&on_notice);
143 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
144 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
145 $conns{$mynick}->add_global_handler('whois', \&on_whois);
146 $conns{$mynick}->add_global_handler('other', \&on_other);
147 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
148 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
149 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
150 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
151 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
152 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
153 $conns{$mynick}->add_global_handler(324, \&on_modeis);
154 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
155 $conns{$mynick}->add_global_handler(352, \&on_who);
156 $conns{$mynick}->add_global_handler(353, \&on_names);
157 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
158 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
159 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
160 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
161 # for proper joinnextChan behaviour
162 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
163 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
164 $conns{$mynick}->add_global_handler(474, \&on_banned);
165 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
166 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
167 # end of handler stuff.
172 # change internal timeout value for scheduler.
173 $irc->{_timeout} = 10; # how about 60?
174 # Net::IRC debugging.
177 $ircstats{'Server'} = "$server:$port";
179 # works? needs to actually do something
180 # should likely listen on a tcp port instead
181 #$irc->addfh(STDIN, \&on_stdin, "r");
183 &status("starting main loop");
188 ######################################################################
189 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
190 ######################################################################
196 # slow down a bit if traffic is "high".
197 # need to take into account time of last message sent.
198 if ($last{buflen} > 256 and length($buf) > 256) {
202 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
204 $last{buflen} = length($buf);
209 my $mynick = $conn->nick();
212 &WARN("say: msg == $msg.");
216 if ( $postprocess ) {
218 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
219 &DEBUG("say: $postprocess $msg");
220 &parseCmdHook($postprocess . ' ' . $msg);
225 &status("<$mynick/$talkchannel> $msg");
227 return unless (&whatInterface() =~ /IRC/);
229 $msg = "zero" if ($msg =~ /^0+$/);
233 if ($t == $pubtime) {
235 $pubsize += length $msg;
237 my $i = &getChanConfDefault("sendPublicLimitLines", 3, $chan);
238 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000, $chan);
240 if ( ($pubcount % $i) == 0 and $pubcount) {
242 } elsif ($pubsize > $j) {
250 $pubsize = length $msg;
253 $conn->privmsg($talkchannel, $msg);
257 my ($nick, $msg) = @_;
258 if (!defined $nick) {
259 &ERROR("msg: nick == NULL.");
265 &WARN("msg: msg == $msg.");
269 &status(">$nick< $msg");
271 return unless (&whatInterface() =~ /IRC/);
274 if ($t == $msgtime) {
276 $msgsize += length $msg;
278 my $i = &getChanConfDefault("sendPrivateLimitLines", 3, $chan);
279 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000, $chan);
280 if ( ($msgcount % $i) == 0 and $msgcount) {
282 } elsif ($msgsize > $j) {
290 $msgsize = length $msg;
293 $conn->privmsg($nick, $msg);
296 # Usage: &action(nick || chan, txt);
298 my $mynick = $conn->nick();
299 my ($target, $txt) = @_;
301 &WARN("action: txt == NULL.");
305 if (length $txt > 480) {
306 &status("action: txt too long; truncating.");
307 chop($txt) while (length $txt > 480);
310 &status("* $mynick/$target $txt");
311 $conn->me($target, $txt);
314 # Usage: ¬ice(nick || chan, txt);
316 my ($target, $txt) = @_;
318 &WARN("notice: txt == NULL.");
322 &status("-$target- $txt");
326 if ($t == $nottime) {
328 $notsize += length $txt;
330 my $i = &getChanConfDefault("sendNoticeLimitLines", 3, $chan);
331 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000, $chan);
333 if ( ($notcount % $i) == 0 and $notcount) {
335 } elsif ($notsize > $j) {
343 $notsize = length $txt;
346 $conn->notice($target, $txt);
350 my ($txt,$flag) = @_;
352 ### FIXME: flag not supported yet.
354 foreach (keys %{ $dcc{'CHAT'} }) {
355 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
360 ### perform commands.
363 # Usage: &performReply($reply);
367 if (!defined $reply or $reply =~ /^\s*$/) {
368 &DEBUG("performReply: reply == NULL.");
372 $reply =~ /([\.\?\s]+)$/;
374 # FIXME need real throttling....
375 if (length($reply) > $maxlinelen - 30) {
376 $reply = substr($reply, 0, $maxlinelen - 33);
377 $reply =~ s/ [^ ]*?$/ .../;
379 &checkMsgType($reply);
381 if ($msgType eq 'public') {
382 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
383 $reply = "$orig{who}: ".$reply;
385 $reply = "$reply, ".$orig{who};
389 } elsif ($msgType eq 'private') {
391 $reply = "$reply, ".$orig{who};
395 } elsif ($msgType eq 'chat') {
396 if (!exists $dcc{'CHAT'}{$who}) {
397 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
400 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
403 &ERROR("PR: msgType invalid? ($msgType).");
408 sub performAddressedReply {
409 return unless ($addressed);
413 # Usage: &performStrictReply($reply);
414 sub performStrictReply {
417 # FIXME need real throttling....
418 if (length($reply) > $maxlinelen - 30) {
419 $reply = substr($reply, 0, $maxlinelen - 33);
420 $reply =~ s/ [^ ]*?$/ .../;
422 &checkMsgType($reply);
424 if ($msgType eq 'private') {
426 } elsif ($msgType eq 'public') {
428 } elsif ($msgType eq 'chat') {
429 &dccsay(lc $who, $reply);
431 &ERROR("pSR: msgType invalid? ($msgType).");
436 my($who, $reply) = @_;
438 if (!defined $reply or $reply =~ /^\s*$/) {
439 &WARN("dccsay: reply == NULL.");
443 if (!exists $dcc{'CHAT'}{$who}) {
444 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
448 &status("=>$who<= $reply"); # dcc chat.
449 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
456 foreach $type (keys %dcc) {
457 &FIXME("dcc_close: $who");
458 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
459 next unless (scalar @who);
461 &DEBUG("dcc_close... close $who!");
466 my ($chan, $key) = @_;
467 $key ||= &getChanConf("chankey", $chan);
470 # forgot for about 2 years to implement channel keys when moving
471 # over to Net::IRC...
473 # hopefully validChan is right.
474 if (&validChan($chan)) {
475 &status("join: already on $chan?");
478 &status("joining $b_blue$chan $key$ob");
480 return if ($conn->join($chan, $key));
481 return if (&validChan($chan));
483 &DEBUG("joinchan: join failed. trying connect!");
493 next if ($chan eq "");
494 $chan =~ tr/A-Z/a-z/; # lowercase.
496 if ($chan !~ /^$mask{chan}$/) {
497 &WARN("part: chan is invalid ($chan)");
501 &status("parting $chan");
502 if (!&validChan($chan)) {
503 &WARN("part: not on $chan; doing anyway");
508 # deletion of $channels{chan} is done in &entryEvt().
513 my ($chan, @modes) = @_;
514 my $modes = join(" ", @modes);
516 if (&validChan($chan) == 0) {
517 &ERROR("mode: invalid chan => '$chan'.");
521 &DEBUG("mode: MODE $chan $modes");
523 # should move to use Net::IRC's $conn->mode()... but too lazy.
524 rawout("MODE $chan $modes");
528 my ($chan, @who) = @_;
529 my $os = "o" x scalar(@who);
531 &mode($chan, "+$os @who");
535 my ($chan, @who) = @_;
536 my $os = "o" x scalar(@who);
538 &mode($chan, "-$os ".@who);
542 my ($nick,$chan,$msg) = @_;
543 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
544 my $mynick = $conn->nick();
546 if ($chan ne "" and &validChan($chan) == 0) {
547 &ERROR("kick: invalid channel $chan.");
551 $nick =~ tr/A-Z/a-z/;
553 foreach $chan (@chans) {
554 if (!&IsNickInChan($nick,$chan)) {
555 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
559 if (!exists $channels{$chan}{o}{$mynick}) {
560 &status("kick: do not have ops on $chan :(");
564 &status("Kicking $nick from $chan.");
565 $conn->kick($chan, $nick, $msg);
570 my ($mask,$chan) = @_;
571 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
572 my $mynick = $conn->nick();
575 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
576 &ERROR("ban: invalid channel $chan.");
580 foreach $chan (@chans) {
581 if (!exists $channels{$chan}{o}{$mynick}) {
582 &status("ban: do not have ops on $chan :(");
586 &status("Banning $mask from $chan.");
587 &rawout("MODE $chan +b $mask");
595 my ($mask,$chan) = @_;
596 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
597 my $mynick = $conn->nick();
600 &DEBUG("unban: mask = $mask, chan = @chans");
602 foreach $chan (@chans) {
603 if (!exists $channels{$chan}{o}{$mynick}) {
604 &status("unBan: do not have ops on $chan :(");
608 &status("Removed ban $mask from $chan.");
609 &rawout("MODE $chan -b $mask");
619 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
620 $conn->quit($quitmsg);
622 &WARN("quit: could not quit!");
628 my $mynick = $conn->nick();
630 if (!defined $newnick) {
631 &ERROR("nick: nick == NULL.");
635 if (!defined $mynick) {
636 &WARN("nick: mynick == NULL.");
641 $bad++ if (exists $nuh{$newnick});
642 $bad++ if (&IsNickInAnyChan($newnick));
645 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
646 # hrm... over time we lose track of our own nick.
650 if ($newnick =~ /^$mask{nick}$/) {
651 &status("nick: Changing nick from $mynick to $newnick");
652 # ->nick() will NOT change cause we are using rawout?
653 &rawout("NICK $newnick");
656 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
661 my($who, $chan) = @_;
662 # TODO: check if $who or $chan are invalid.
664 $conn->invite($who, $chan);
668 # Channel related functions...
671 # Usage: &joinNextChan();
674 foreach (sort keys %conns) {
676 my $mynick = $conn->nick();
677 my @join = getJoinChans(1);
680 my $chan = shift @join;
683 if (my $i = scalar @join) {
684 &status("joinNextChan: $mynick $i chans to join.");
691 if (exists $cache{joinTime}) {
692 my $delta = time() - $cache{joinTime} - 5;
693 my $timestr = &Time2String($delta);
694 # FIXME: @join should be @in instead (hacked to 10)
695 #my $rate = sprintf("%.1f", $delta / @in);
696 my $rate = sprintf("%.1f", $delta / 10);
697 delete $cache{joinTime};
699 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
702 # chanserv check: global channels, in case we missed one.
703 foreach ( &ChanConfList("chanServ_ops") ) {
708 # Usage: &getNickInChans($nick);
713 foreach (keys %channels) {
714 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
721 # Usage: &getNicksInChan($chan);
726 return keys %{ $channels{$chan}{''} };
730 my ($nick,$chan) = @_;
732 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
735 &DEBUG("INIC: chan == NULL.");
739 if (&validChan($chan) == 0) {
740 &ERROR("INIC: invalid channel $chan.");
744 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
747 foreach (keys %channels) {
748 next unless (/[A-Z]/);
749 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
755 sub IsNickInAnyChan {
759 foreach $chan (keys %channels) {
760 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
766 # Usage: &validChan($chan);
768 # TODO: use $c instead?
771 if (!defined $chan or $chan =~ /^\s*$/) {
775 if (lc $chan ne $chan) {
776 &WARN("validChan: lc chan != chan. ($chan); fixing.");
777 $chan =~ tr/A-Z/a-z/;
780 # it's possible that this check creates the hash if empty.
781 if (defined $channels{$chan} or exists $channels{$chan}) {
782 if ($chan =~ /^_?default$/) {
783 # &WARN("validC: chan cannot be _default! returning 0!");
794 # Usage: &delUserInfo($nick,@chans);
796 my ($nick,@chans) = @_;
799 foreach $chan (@chans) {
800 foreach $mode (keys %{ $channels{$chan} }) {
802 next unless (exists $channels{$chan}{$mode}{$nick});
804 delete $channels{$chan}{$mode}{$nick};
812 delete $channels{$chan};
819 $cache{joinTime} = time();
829 # can't join any if not connected
830 return @join if (!$conn);
832 my $nick = $conn->nick();
834 foreach (keys %chanconf) {
835 next if ($_ eq "_default");
838 my $val = $chanconf{$_}{autojoin};
841 $skip++ if ($val eq "0");
843 # convert old +autojoin to autojoin <nick>
845 $chanconf{$_}{autojoin} = $val;
847 $skip++ if (lc $val ne lc $nick);
855 if (defined $channels{$_} or exists $channels{$_}) {
864 #$str .= ' in:' . join(',', sort @in) if scalar @in;
865 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
866 $str .= ' join:' . join(',', sort @join) if scalar @join;
868 &status("Chans: ($nick)$str") if ($show);
874 # &DEBUG("closeDCC called.");
877 foreach $type (keys %dcc) {
878 next if ($type ne uc($type));
881 foreach $nick (keys %{ $dcc{$type} }) {
882 next unless (defined $nick);
883 &status("DCC CHAT: closing DCC $type to $nick.");
884 next unless (defined $dcc{$type}{$nick});
886 my $ref = $dcc{$type}{$nick};
887 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
888 $dcc{$type}{$nick}->close();
889 delete $dcc{$type}{$nick};
890 &DEBUG("after close for $nick");
897 my($who,$chan,$userhost) = @_;
899 return unless (&IsChanConf("joinfloodCheck") > 0);
901 if (exists $netsplit{lc $who}) { # netsplit join.
902 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
905 if (exists $floodjoin{$chan}{$who}{Time}) {
906 &WARN("floodjoin{$chan}{$who} already exists?");
909 $floodjoin{$chan}{$who}{Time} = time();
910 $floodjoin{$chan}{$who}{Host} = $userhost;
913 foreach (keys %floodjoin) {
915 my $count = scalar keys %{ $floodjoin{$c} };
916 next unless ($count > 5);
917 &DEBUG("joinflood: count => $count");
920 foreach (keys %{ $floodjoin{$c} }) {
921 my $t = $floodjoin{$c}{$_}{Time};
922 next unless (defined $t);
926 &DEBUG("joinflood: time => $time");
929 &DEBUG("joinflood: new time => $time");
935 foreach $chan (keys %floodjoin) {
936 foreach $who (keys %{ $floodjoin{$chan} }) {
937 my $t = $floodjoin{$chan}{$who}{Time};
938 next unless (defined $t);
940 my $delta = $time - $t;
941 next unless ($delta > 10);
943 delete $floodjoin{$chan}{$who};
948 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
954 if (exists $nuh{$n}) {
955 return &makeHostMask($nuh{$n});
957 $cache{on_who_Hack} = 1;