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)) {
217 &DEBUG("say: silent in $talkchannel, not saying $msg");
221 if ( $postprocess ) {
223 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
224 &DEBUG("say: $postprocess $msg");
225 &parseCmdHook($postprocess . ' ' . $msg);
230 &status("<$mynick/$talkchannel> $msg");
232 return unless (&whatInterface() =~ /IRC/);
234 $msg = 'zero' if ($msg =~ /^0+$/);
238 if ($t == $pubtime) {
240 $pubsize += length $msg;
242 my $i = &getChanConfDefault('sendPublicLimitLines', 3, $chan);
243 my $j = &getChanConfDefault('sendPublicLimitBytes', 1000, $chan);
245 if ( ($pubcount % $i) == 0 and $pubcount) {
247 } elsif ($pubsize > $j) {
255 $pubsize = length $msg;
258 $conn->privmsg($talkchannel, $msg);
262 my ($nick, $msg) = @_;
263 if (!defined $nick) {
264 &ERROR("msg: nick == NULL.");
270 &WARN("msg: msg == $msg.");
274 # some say() end up here (eg +help)
275 if (&getChanConf('silent', $nick)) {
276 &DEBUG("msg: silent in $nick, not saying $msg");
280 &status(">$nick< $msg");
282 return unless (&whatInterface() =~ /IRC/);
285 if ($t == $msgtime) {
287 $msgsize += length $msg;
289 my $i = &getChanConfDefault('sendPrivateLimitLines', 3, $chan);
290 my $j = &getChanConfDefault('sendPrivateLimitBytes', 1000, $chan);
291 if ( ($msgcount % $i) == 0 and $msgcount) {
293 } elsif ($msgsize > $j) {
301 $msgsize = length $msg;
304 $conn->privmsg($nick, $msg);
307 # Usage: &action(nick || chan, txt);
309 my $mynick = $conn->nick();
310 my ($target, $txt) = @_;
312 &WARN("action: txt == NULL.");
316 if (&getChanConf('silent', $target)) {
317 &DEBUG("action: silent in $target, not doing $txt");
321 if (length $txt > 480) {
322 &status("action: txt too long; truncating.");
323 chop($txt) while (length $txt > 480);
326 &status("* $mynick/$target $txt");
327 $conn->me($target, $txt);
330 # Usage: ¬ice(nick || chan, txt);
332 my ($target, $txt) = @_;
334 &WARN("notice: txt == NULL.");
338 &status("-$target- $txt");
342 if ($t == $nottime) {
344 $notsize += length $txt;
346 my $i = &getChanConfDefault('sendNoticeLimitLines', 3, $chan);
347 my $j = &getChanConfDefault('sendNoticeLimitBytes', 1000, $chan);
349 if ( ($notcount % $i) == 0 and $notcount) {
351 } elsif ($notsize > $j) {
359 $notsize = length $txt;
362 $conn->notice($target, $txt);
366 my ($txt,$flag) = @_;
368 ### FIXME: flag not supported yet.
370 foreach (keys %{ $dcc{'CHAT'} }) {
371 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
376 ### perform commands.
379 # Usage: &performReply($reply);
383 if (!defined $reply or $reply =~ /^\s*$/) {
384 &DEBUG("performReply: reply == NULL.");
388 $reply =~ /([\.\?\s]+)$/;
390 # FIXME need real throttling....
391 if (length($reply) > $maxlinelen - 30) {
392 $reply = substr($reply, 0, $maxlinelen - 33);
393 $reply =~ s/ [^ ]*?$/ .../;
395 &checkMsgType($reply);
397 if ($msgType eq 'public') {
398 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
399 $reply = "$orig{who}: ".$reply;
401 $reply = "$reply, ".$orig{who};
405 } elsif ($msgType eq 'private') {
407 $reply = "$reply, ".$orig{who};
411 } elsif ($msgType eq 'chat') {
412 if (!exists $dcc{'CHAT'}{$who}) {
413 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
416 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
419 &ERROR("PR: msgType invalid? ($msgType).");
424 sub performAddressedReply {
425 return unless ($addressed);
429 # Usage: &performStrictReply($reply);
430 sub performStrictReply {
433 # FIXME need real throttling....
434 if (length($reply) > $maxlinelen - 30) {
435 $reply = substr($reply, 0, $maxlinelen - 33);
436 $reply =~ s/ [^ ]*?$/ .../;
438 &checkMsgType($reply);
440 if ($msgType eq 'private') {
442 } elsif ($msgType eq 'public') {
444 } elsif ($msgType eq 'chat') {
445 &dccsay(lc $who, $reply);
447 &ERROR("pSR: msgType invalid? ($msgType).");
452 my($who, $reply) = @_;
454 if (!defined $reply or $reply =~ /^\s*$/) {
455 &WARN("dccsay: reply == NULL.");
459 if (!exists $dcc{'CHAT'}{$who}) {
460 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
464 &status("=>$who<= $reply"); # dcc chat.
465 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
472 foreach $type (keys %dcc) {
473 &FIXME("dcc_close: $who");
474 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
475 next unless (scalar @who);
477 &DEBUG("dcc_close... close $who!");
482 my ($chan, $key) = @_;
483 $key ||= &getChanConf('chankey', $chan);
486 # forgot for about 2 years to implement channel keys when moving
487 # over to Net::IRC...
489 # hopefully validChan is right.
490 if (&validChan($chan)) {
491 &status("join: already on $chan?");
494 &status("joining $b_blue$chan $key$ob");
496 return if ($conn->join($chan, $key));
497 return if (&validChan($chan));
499 &DEBUG("joinchan: join failed. trying connect!");
509 next if ($chan eq '');
510 $chan =~ tr/A-Z/a-z/; # lowercase.
512 if ($chan !~ /^$mask{chan}$/) {
513 &WARN("part: chan is invalid ($chan)");
517 &status("parting $chan");
518 if (!&validChan($chan)) {
519 &WARN("part: not on $chan; doing anyway");
524 # deletion of $channels{chan} is done in &entryEvt().
529 my ($chan, @modes) = @_;
530 my $modes = join(" ", @modes);
532 if (&validChan($chan) == 0) {
533 &ERROR("mode: invalid chan => '$chan'.");
537 &DEBUG("mode: MODE $chan $modes");
539 # should move to use Net::IRC's $conn->mode()... but too lazy.
540 rawout("MODE $chan $modes");
544 my ($chan, @who) = @_;
545 my $os = 'o' x scalar(@who);
547 &mode($chan, "+$os @who");
551 my ($chan, @who) = @_;
552 my $os = 'o' x scalar(@who);
554 &mode($chan, "-$os ".@who);
558 my ($nick,$chan,$msg) = @_;
559 my (@chans) = ($chan eq '') ? (keys %channels) : lc($chan);
560 my $mynick = $conn->nick();
562 if ($chan ne '' and &validChan($chan) == 0) {
563 &ERROR("kick: invalid channel $chan.");
567 $nick =~ tr/A-Z/a-z/;
569 foreach $chan (@chans) {
570 if (!&IsNickInChan($nick,$chan)) {
571 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
575 if (!exists $channels{$chan}{o}{$mynick}) {
576 &status("kick: do not have ops on $chan :(");
580 &status("Kicking $nick from $chan.");
581 $conn->kick($chan, $nick, $msg);
586 my ($mask,$chan) = @_;
587 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
588 my $mynick = $conn->nick();
591 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
592 &ERROR("ban: invalid channel $chan.");
596 foreach $chan (@chans) {
597 if (!exists $channels{$chan}{o}{$mynick}) {
598 &status("ban: do not have ops on $chan :(");
602 &status("Banning $mask from $chan.");
603 &rawout("MODE $chan +b $mask");
611 my ($mask,$chan) = @_;
612 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
613 my $mynick = $conn->nick();
616 &DEBUG("unban: mask = $mask, chan = @chans");
618 foreach $chan (@chans) {
619 if (!exists $channels{$chan}{o}{$mynick}) {
620 &status("unBan: do not have ops on $chan :(");
624 &status("Removed ban $mask from $chan.");
625 &rawout("MODE $chan -b $mask");
635 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
636 $conn->quit($quitmsg);
638 &WARN("quit: could not quit!");
644 my $mynick = $conn->nick();
646 if (!defined $newnick) {
647 &ERROR("nick: nick == NULL.");
651 if (!defined $mynick) {
652 &WARN("nick: mynick == NULL.");
657 $bad++ if (exists $nuh{$newnick});
658 $bad++ if (&IsNickInAnyChan($newnick));
661 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
662 # hrm... over time we lose track of our own nick.
666 if ($newnick =~ /^$mask{nick}$/) {
667 &status("nick: Changing nick from $mynick to $newnick");
668 # ->nick() will NOT change cause we are using rawout?
669 &rawout("NICK $newnick");
672 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
677 my($who, $chan) = @_;
678 # TODO: check if $who or $chan are invalid.
680 $conn->invite($who, $chan);
684 # Channel related functions...
687 # Usage: &joinNextChan();
690 foreach (sort keys %conns) {
692 my $mynick = $conn->nick();
693 my @join = getJoinChans(1);
696 my $chan = shift @join;
699 if (my $i = scalar @join) {
700 &status("joinNextChan: $mynick $i chans to join.");
707 if (exists $cache{joinTime}) {
708 my $delta = time() - $cache{joinTime} - 5;
709 my $timestr = &Time2String($delta);
710 # FIXME: @join should be @in instead (hacked to 10)
711 #my $rate = sprintf("%.1f", $delta / @in);
712 my $rate = sprintf("%.1f", $delta / 10);
713 delete $cache{joinTime};
715 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
718 # chanserv check: global channels, in case we missed one.
719 foreach ( &ChanConfList('chanServ_ops') ) {
724 # Usage: &getNickInChans($nick);
729 foreach (keys %channels) {
730 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
737 # Usage: &getNicksInChan($chan);
742 return keys %{ $channels{$chan}{''} };
746 my ($nick,$chan) = @_;
748 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
751 &DEBUG("INIC: chan == NULL.");
755 if (&validChan($chan) == 0) {
756 &ERROR("INIC: invalid channel $chan.");
760 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
763 foreach (keys %channels) {
764 next unless (/[A-Z]/);
765 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
771 sub IsNickInAnyChan {
775 foreach $chan (keys %channels) {
776 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
782 # Usage: &validChan($chan);
784 # TODO: use $c instead?
787 if (!defined $chan or $chan =~ /^\s*$/) {
791 if (lc $chan ne $chan) {
792 &WARN("validChan: lc chan != chan. ($chan); fixing.");
793 $chan =~ tr/A-Z/a-z/;
796 # it's possible that this check creates the hash if empty.
797 if (defined $channels{$chan} or exists $channels{$chan}) {
798 if ($chan =~ /^_?default$/) {
799 # &WARN("validC: chan cannot be _default! returning 0!");
810 # Usage: &delUserInfo($nick,@chans);
812 my ($nick,@chans) = @_;
815 foreach $chan (@chans) {
816 foreach $mode (keys %{ $channels{$chan} }) {
818 next unless (exists $channels{$chan}{$mode}{$nick});
820 delete $channels{$chan}{$mode}{$nick};
828 delete $channels{$chan};
835 $cache{joinTime} = time();
845 # can't join any if not connected
846 return @join if (!$conn);
848 my $nick = $conn->nick();
850 foreach (keys %chanconf) {
851 next if ($_ eq '_default');
854 my $val = $chanconf{$_}{autojoin};
857 $skip++ if ($val eq '0');
859 # convert old +autojoin to autojoin <nick>
861 $chanconf{$_}{autojoin} = $val;
863 $skip++ if (lc $val ne lc $nick);
871 if (defined $channels{$_} or exists $channels{$_}) {
880 #$str .= ' in:' . join(',', sort @in) if scalar @in;
881 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
882 $str .= ' join:' . join(',', sort @join) if scalar @join;
884 &status("Chans: ($nick)$str") if ($show);
890 # &DEBUG("closeDCC called.");
893 foreach $type (keys %dcc) {
894 next if ($type ne uc($type));
897 foreach $nick (keys %{ $dcc{$type} }) {
898 next unless (defined $nick);
899 &status("DCC CHAT: closing DCC $type to $nick.");
900 next unless (defined $dcc{$type}{$nick});
902 my $ref = $dcc{$type}{$nick};
903 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
904 $dcc{$type}{$nick}->close();
905 delete $dcc{$type}{$nick};
906 &DEBUG("after close for $nick");
913 my($who,$chan,$userhost) = @_;
915 return unless (&IsChanConf('joinfloodCheck') > 0);
917 if (exists $netsplit{lc $who}) { # netsplit join.
918 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
921 if (exists $floodjoin{$chan}{$who}{Time}) {
922 &WARN("floodjoin{$chan}{$who} already exists?");
925 $floodjoin{$chan}{$who}{Time} = time();
926 $floodjoin{$chan}{$who}{Host} = $userhost;
929 foreach (keys %floodjoin) {
931 my $count = scalar keys %{ $floodjoin{$c} };
932 next unless ($count > 5);
933 &DEBUG("joinflood: count => $count");
936 foreach (keys %{ $floodjoin{$c} }) {
937 my $t = $floodjoin{$c}{$_}{Time};
938 next unless (defined $t);
942 &DEBUG("joinflood: time => $time");
945 &DEBUG("joinflood: new time => $time");
951 foreach $chan (keys %floodjoin) {
952 foreach $who (keys %{ $floodjoin{$chan} }) {
953 my $t = $floodjoin{$chan}{$who}{Time};
954 next unless (defined $t);
956 my $delta = $time - $t;
957 next unless ($delta > 10);
959 delete $floodjoin{$chan}{$who};
964 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
970 if (exists $nuh{$n}) {
971 return &makeHostMask($nuh{$n});
973 $cache{on_who_Hack} = 1;