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 (&getChanConf('silent', $talkchannel) and not
217 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
218 &DEBUG("say: silent in $talkchannel, not saying $msg");
222 if ( $postprocess ) {
224 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
225 &DEBUG("say: $postprocess $msg");
226 &parseCmdHook($postprocess . ' ' . $msg);
231 &status("<$mynick/$talkchannel> $msg");
233 return unless (&whatInterface() =~ /IRC/);
235 $msg = 'zero' if ($msg =~ /^0+$/);
239 if ($t == $pubtime) {
241 $pubsize += length $msg;
243 my $i = &getChanConfDefault('sendPublicLimitLines', 3, $chan);
244 my $j = &getChanConfDefault('sendPublicLimitBytes', 1000, $chan);
246 if ( ($pubcount % $i) == 0 and $pubcount) {
248 } elsif ($pubsize > $j) {
256 $pubsize = length $msg;
259 $conn->privmsg($talkchannel, $msg);
263 my ($nick, $msg) = @_;
264 if (!defined $nick) {
265 &ERROR("msg: nick == NULL.");
271 &WARN("msg: msg == $msg.");
275 # some say() end up here (eg +help)
276 if (&getChanConf('silent', $nick) and not
277 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
278 &DEBUG("msg: silent in $nick, not saying $msg");
282 &status(">$nick< $msg");
284 return unless (&whatInterface() =~ /IRC/);
287 if ($t == $msgtime) {
289 $msgsize += length $msg;
291 my $i = &getChanConfDefault('sendPrivateLimitLines', 3, $chan);
292 my $j = &getChanConfDefault('sendPrivateLimitBytes', 1000, $chan);
293 if ( ($msgcount % $i) == 0 and $msgcount) {
295 } elsif ($msgsize > $j) {
303 $msgsize = length $msg;
306 $conn->privmsg($nick, $msg);
309 # Usage: &action(nick || chan, txt);
311 my $mynick = $conn->nick();
312 my ($target, $txt) = @_;
314 &WARN("action: txt == NULL.");
318 if (&getChanConf('silent', $target) and not
319 (&IsFlag("s") and &verifyUser($who,$nuh{lc $who}))) {
320 &DEBUG("action: silent in $target, not doing $txt");
324 if (length $txt > 480) {
325 &status("action: txt too long; truncating.");
326 chop($txt) while (length $txt > 480);
329 &status("* $mynick/$target $txt");
330 $conn->me($target, $txt);
333 # Usage: ¬ice(nick || chan, txt);
335 my ($target, $txt) = @_;
337 &WARN("notice: txt == NULL.");
341 &status("-$target- $txt");
345 if ($t == $nottime) {
347 $notsize += length $txt;
349 my $i = &getChanConfDefault('sendNoticeLimitLines', 3, $chan);
350 my $j = &getChanConfDefault('sendNoticeLimitBytes', 1000, $chan);
352 if ( ($notcount % $i) == 0 and $notcount) {
354 } elsif ($notsize > $j) {
362 $notsize = length $txt;
365 $conn->notice($target, $txt);
369 my ($txt,$flag) = @_;
371 ### FIXME: flag not supported yet.
373 foreach (keys %{ $dcc{'CHAT'} }) {
374 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
379 ### perform commands.
382 # Usage: &performReply($reply);
386 if (!defined $reply or $reply =~ /^\s*$/) {
387 &DEBUG("performReply: reply == NULL.");
391 $reply =~ /([\.\?\s]+)$/;
393 # FIXME need real throttling....
394 if (length($reply) > $maxlinelen - 30) {
395 $reply = substr($reply, 0, $maxlinelen - 33);
396 $reply =~ s/ [^ ]*?$/ .../;
398 &checkMsgType($reply);
400 if ($msgType eq 'public') {
401 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
402 $reply = "$orig{who}: ".$reply;
404 $reply = "$reply, ".$orig{who};
408 } elsif ($msgType eq 'private') {
410 $reply = "$reply, ".$orig{who};
414 } elsif ($msgType eq 'chat') {
415 if (!exists $dcc{'CHAT'}{$who}) {
416 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
419 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
422 &ERROR("PR: msgType invalid? ($msgType).");
427 sub performAddressedReply {
428 return unless ($addressed);
432 # Usage: &performStrictReply($reply);
433 sub performStrictReply {
436 # FIXME need real throttling....
437 if (length($reply) > $maxlinelen - 30) {
438 $reply = substr($reply, 0, $maxlinelen - 33);
439 $reply =~ s/ [^ ]*?$/ .../;
441 &checkMsgType($reply);
443 if ($msgType eq 'private') {
445 } elsif ($msgType eq 'public') {
447 } elsif ($msgType eq 'chat') {
448 &dccsay(lc $who, $reply);
450 &ERROR("pSR: msgType invalid? ($msgType).");
455 my($who, $reply) = @_;
457 if (!defined $reply or $reply =~ /^\s*$/) {
458 &WARN("dccsay: reply == NULL.");
462 if (!exists $dcc{'CHAT'}{$who}) {
463 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
467 &status("=>$who<= $reply"); # dcc chat.
468 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
475 foreach $type (keys %dcc) {
476 &FIXME("dcc_close: $who");
477 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
478 next unless (scalar @who);
480 &DEBUG("dcc_close... close $who!");
485 my ($chan, $key) = @_;
486 $key ||= &getChanConf('chankey', $chan);
489 # forgot for about 2 years to implement channel keys when moving
490 # over to Net::IRC...
492 # hopefully validChan is right.
493 if (&validChan($chan)) {
494 &status("join: already on $chan?");
497 &status("joining $b_blue$chan $key$ob");
499 return if ($conn->join($chan, $key));
500 return if (&validChan($chan));
502 &DEBUG("joinchan: join failed. trying connect!");
512 next if ($chan eq '');
513 $chan =~ tr/A-Z/a-z/; # lowercase.
515 if ($chan !~ /^$mask{chan}$/) {
516 &WARN("part: chan is invalid ($chan)");
520 &status("parting $chan");
521 if (!&validChan($chan)) {
522 &WARN("part: not on $chan; doing anyway");
527 # deletion of $channels{chan} is done in &entryEvt().
532 my ($chan, @modes) = @_;
533 my $modes = join(" ", @modes);
535 if (&validChan($chan) == 0) {
536 &ERROR("mode: invalid chan => '$chan'.");
540 &DEBUG("mode: MODE $chan $modes");
542 # should move to use Net::IRC's $conn->mode()... but too lazy.
543 rawout("MODE $chan $modes");
547 my ($chan, @who) = @_;
548 my $os = 'o' x scalar(@who);
550 &mode($chan, "+$os @who");
554 my ($chan, @who) = @_;
555 my $os = 'o' x scalar(@who);
557 &mode($chan, "-$os ".@who);
561 my ($nick,$chan,$msg) = @_;
562 my (@chans) = ($chan eq '') ? (keys %channels) : lc($chan);
563 my $mynick = $conn->nick();
565 if ($chan ne '' and &validChan($chan) == 0) {
566 &ERROR("kick: invalid channel $chan.");
570 $nick =~ tr/A-Z/a-z/;
572 foreach $chan (@chans) {
573 if (!&IsNickInChan($nick,$chan)) {
574 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
578 if (!exists $channels{$chan}{o}{$mynick}) {
579 &status("kick: do not have ops on $chan :(");
583 &status("Kicking $nick from $chan.");
584 $conn->kick($chan, $nick, $msg);
589 my ($mask,$chan) = @_;
590 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
591 my $mynick = $conn->nick();
594 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
595 &ERROR("ban: invalid channel $chan.");
599 foreach $chan (@chans) {
600 if (!exists $channels{$chan}{o}{$mynick}) {
601 &status("ban: do not have ops on $chan :(");
605 &status("Banning $mask from $chan.");
606 &rawout("MODE $chan +b $mask");
614 my ($mask,$chan) = @_;
615 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
616 my $mynick = $conn->nick();
619 &DEBUG("unban: mask = $mask, chan = @chans");
621 foreach $chan (@chans) {
622 if (!exists $channels{$chan}{o}{$mynick}) {
623 &status("unBan: do not have ops on $chan :(");
627 &status("Removed ban $mask from $chan.");
628 &rawout("MODE $chan -b $mask");
638 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
639 $conn->quit($quitmsg);
641 &WARN("quit: could not quit!");
647 my $mynick = $conn->nick();
649 if (!defined $newnick) {
650 &ERROR("nick: nick == NULL.");
654 if (!defined $mynick) {
655 &WARN("nick: mynick == NULL.");
660 $bad++ if (exists $nuh{$newnick});
661 $bad++ if (&IsNickInAnyChan($newnick));
664 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
665 # hrm... over time we lose track of our own nick.
669 if ($newnick =~ /^$mask{nick}$/) {
670 &status("nick: Changing nick from $mynick to $newnick");
671 # ->nick() will NOT change cause we are using rawout?
672 &rawout("NICK $newnick");
675 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
680 my($who, $chan) = @_;
681 # TODO: check if $who or $chan are invalid.
683 $conn->invite($who, $chan);
687 # Channel related functions...
690 # Usage: &joinNextChan();
693 foreach (sort keys %conns) {
695 my $mynick = $conn->nick();
696 my @join = getJoinChans(1);
699 my $chan = shift @join;
702 if (my $i = scalar @join) {
703 &status("joinNextChan: $mynick $i chans to join.");
710 if (exists $cache{joinTime}) {
711 my $delta = time() - $cache{joinTime} - 5;
712 my $timestr = &Time2String($delta);
713 # FIXME: @join should be @in instead (hacked to 10)
714 #my $rate = sprintf("%.1f", $delta / @in);
715 my $rate = sprintf("%.1f", $delta / 10);
716 delete $cache{joinTime};
718 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
721 # chanserv check: global channels, in case we missed one.
722 foreach ( &ChanConfList('chanServ_ops') ) {
727 # Usage: &getNickInChans($nick);
732 foreach (keys %channels) {
733 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
740 # Usage: &getNicksInChan($chan);
745 return keys %{ $channels{$chan}{''} };
749 my ($nick,$chan) = @_;
751 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
754 &DEBUG("INIC: chan == NULL.");
758 if (&validChan($chan) == 0) {
759 &ERROR("INIC: invalid channel $chan.");
763 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
766 foreach (keys %channels) {
767 next unless (/[A-Z]/);
768 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
774 sub IsNickInAnyChan {
778 foreach $chan (keys %channels) {
779 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
785 # Usage: &validChan($chan);
787 # TODO: use $c instead?
790 if (!defined $chan or $chan =~ /^\s*$/) {
794 if (lc $chan ne $chan) {
795 &WARN("validChan: lc chan != chan. ($chan); fixing.");
796 $chan =~ tr/A-Z/a-z/;
799 # it's possible that this check creates the hash if empty.
800 if (defined $channels{$chan} or exists $channels{$chan}) {
801 if ($chan =~ /^_?default$/) {
802 # &WARN("validC: chan cannot be _default! returning 0!");
813 # Usage: &delUserInfo($nick,@chans);
815 my ($nick,@chans) = @_;
818 foreach $chan (@chans) {
819 foreach $mode (keys %{ $channels{$chan} }) {
821 next unless (exists $channels{$chan}{$mode}{$nick});
823 delete $channels{$chan}{$mode}{$nick};
831 delete $channels{$chan};
838 $cache{joinTime} = time();
848 # can't join any if not connected
849 return @join if (!$conn);
851 my $nick = $conn->nick();
853 foreach (keys %chanconf) {
854 next if ($_ eq '_default');
857 my $val = $chanconf{$_}{autojoin};
860 $skip++ if ($val eq '0');
862 # convert old +autojoin to autojoin <nick>
864 $chanconf{$_}{autojoin} = $val;
866 $skip++ if (lc $val ne lc $nick);
874 if (defined $channels{$_} or exists $channels{$_}) {
883 #$str .= ' in:' . join(',', sort @in) if scalar @in;
884 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
885 $str .= ' join:' . join(',', sort @join) if scalar @join;
887 &status("Chans: ($nick)$str") if ($show);
893 # &DEBUG("closeDCC called.");
896 foreach $type (keys %dcc) {
897 next if ($type ne uc($type));
900 foreach $nick (keys %{ $dcc{$type} }) {
901 next unless (defined $nick);
902 &status("DCC CHAT: closing DCC $type to $nick.");
903 next unless (defined $dcc{$type}{$nick});
905 my $ref = $dcc{$type}{$nick};
906 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
907 $dcc{$type}{$nick}->close();
908 delete $dcc{$type}{$nick};
909 &DEBUG("after close for $nick");
916 my($who,$chan,$userhost) = @_;
918 return unless (&IsChanConf('joinfloodCheck') > 0);
920 if (exists $netsplit{lc $who}) { # netsplit join.
921 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
924 if (exists $floodjoin{$chan}{$who}{Time}) {
925 &WARN("floodjoin{$chan}{$who} already exists?");
928 $floodjoin{$chan}{$who}{Time} = time();
929 $floodjoin{$chan}{$who}{Host} = $userhost;
932 foreach (keys %floodjoin) {
934 my $count = scalar keys %{ $floodjoin{$c} };
935 next unless ($count > 5);
936 &DEBUG("joinflood: count => $count");
939 foreach (keys %{ $floodjoin{$c} }) {
940 my $t = $floodjoin{$c}{$_}{Time};
941 next unless (defined $t);
945 &DEBUG("joinflood: time => $time");
948 &DEBUG("joinflood: new time => $time");
954 foreach $chan (keys %floodjoin) {
955 foreach $who (keys %{ $floodjoin{$chan} }) {
956 my $t = $floodjoin{$chan}{$who}{Time};
957 next unless (defined $t);
959 my $delta = $time - $t;
960 next unless ($delta > 10);
962 delete $floodjoin{$chan}{$who};
967 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
973 if (exists $nuh{$n}) {
974 return &makeHostMask($nuh{$n});
976 $cache{on_who_Hack} = 1;