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);
33 while (my $host = shift @ircServers) {
34 # JUST IN CASE. irq was complaining about this.
35 if ($lastrun == time()) {
36 &DEBUG("ircloop: hrm... lastrun == time()");
43 &DEBUG("ircloop: ircServers[x] = NULL.");
47 next unless (exists $ircPort{$host});
49 my $retval = &irc($host, $ircPort{$host});
50 next unless (defined $retval and $retval == 0);
53 if ($error % 3 == 0 and $error != 0) {
54 &status("IRC: Could not connect.");
60 &status("IRC: cannot connect to any IRC servers; stopping.");
66 &status("IRC: ok, done one cycle of IRC servers; trying again.");
73 my ($server,$port) = @_;
77 # TODO: move all this to an sql table
78 my $iaddr = inet_aton($server);
79 my $paddr = sockaddr_in($port, $iaddr);
80 my $proto = getprotobyname('tcp');
87 if ($server =~ /\D$/) {
88 my $packed = scalar(gethostbyname($server));
90 if (!defined $packed) {
91 &status(" cannot resolve $server.");
95 $resolve = inet_ntoa($packed);
96 ### warning in Sys/Hostname line 78???
97 ### caused inside Net::IRC?
101 Nick => $param{'ircNick'},
104 Ircname => $param{'ircName'},
106 $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
107 $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
109 foreach my $mynick (split ',', $param{'ircNick'}) {
110 &status("Connecting to port $port of server $server ($resolve) as $mynick ...");
111 $args{'Nick'} = $mynick;
112 $conns{$mynick} = $irc->newconn(%args);
113 if (!defined $conns{$mynick}) {
114 &ERROR("IRC: connection failed.");
115 &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
116 &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
117 &ERROR("127.0.0.1 localhost localhost");
118 &ERROR("If this is still a problem, please contact the maintainer.");
120 $conns{$mynick}->maxlinelen($maxlinelen);
122 $conns{$mynick}->add_global_handler('caction', \&on_action);
123 $conns{$mynick}->add_global_handler('cdcc', \&on_dcc);
124 $conns{$mynick}->add_global_handler('cping', \&on_ping);
125 $conns{$mynick}->add_global_handler('crping', \&on_ping_reply);
126 $conns{$mynick}->add_global_handler('cversion', \&on_version);
127 $conns{$mynick}->add_global_handler('crversion', \&on_crversion);
128 $conns{$mynick}->add_global_handler('dcc_open', \&on_dcc_open);
129 $conns{$mynick}->add_global_handler('dcc_close', \&on_dcc_close);
130 $conns{$mynick}->add_global_handler('chat', \&on_chat);
131 $conns{$mynick}->add_global_handler('msg', \&on_msg);
132 $conns{$mynick}->add_global_handler('public', \&on_public);
133 $conns{$mynick}->add_global_handler('join', \&on_join);
134 $conns{$mynick}->add_global_handler('part', \&on_part);
135 $conns{$mynick}->add_global_handler('topic', \&on_topic);
136 $conns{$mynick}->add_global_handler('invite', \&on_invite);
137 $conns{$mynick}->add_global_handler('kick', \&on_kick);
138 $conns{$mynick}->add_global_handler('mode', \&on_mode);
139 $conns{$mynick}->add_global_handler('nick', \&on_nick);
140 $conns{$mynick}->add_global_handler('quit', \&on_quit);
141 $conns{$mynick}->add_global_handler('notice', \&on_notice);
142 $conns{$mynick}->add_global_handler('whoischannels', \&on_whoischannels);
143 $conns{$mynick}->add_global_handler('useronchannel', \&on_useronchannel);
144 $conns{$mynick}->add_global_handler('whois', \&on_whois);
145 $conns{$mynick}->add_global_handler('other', \&on_other);
146 $conns{$mynick}->add_global_handler('disconnect', \&on_disconnect);
147 $conns{$mynick}->add_global_handler([251,252,253,254,255], \&on_init);
148 # $conns{$mynick}->add_global_handler(302, \&on_init); # userhost
149 $conns{$mynick}->add_global_handler(303, \&on_ison); # notify.
150 $conns{$mynick}->add_global_handler(315, \&on_endofwho);
151 $conns{$mynick}->add_global_handler(422, \&on_endofwho); # nomotd.
152 $conns{$mynick}->add_global_handler(324, \&on_modeis);
153 $conns{$mynick}->add_global_handler(333, \&on_topicinfo);
154 $conns{$mynick}->add_global_handler(352, \&on_who);
155 $conns{$mynick}->add_global_handler(353, \&on_names);
156 $conns{$mynick}->add_global_handler(366, \&on_endofnames);
157 $conns{$mynick}->add_global_handler(376, \&on_endofmotd); # on_connect.
158 $conns{$mynick}->add_global_handler(433, \&on_nick_taken);
159 $conns{$mynick}->add_global_handler(439, \&on_targettoofast);
160 # for proper joinnextChan behaviour
161 $conns{$mynick}->add_global_handler(471, \&on_chanfull);
162 $conns{$mynick}->add_global_handler(473, \&on_inviteonly);
163 $conns{$mynick}->add_global_handler(474, \&on_banned);
164 $conns{$mynick}->add_global_handler(475, \&on_badchankey);
165 $conns{$mynick}->add_global_handler(443, \&on_useronchan);
166 # end of handler stuff.
171 # change internal timeout value for scheduler.
172 $irc->{_timeout} = 10; # how about 60?
173 # Net::IRC debugging.
176 $ircstats{'Server'} = "$server:$port";
178 # works? needs to actually do something
179 # should likely listen on a tcp port instead
180 #$irc->addfh(STDIN, \&on_stdin, "r");
182 &status("starting main loop");
187 ######################################################################
188 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
189 ######################################################################
195 # slow down a bit if traffic is "high".
196 # need to take into account time of last message sent.
197 if ($last{buflen} > 256 and length($buf) > 256) {
201 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
203 $last{buflen} = length($buf);
208 my $mynick = $conn->nick();
211 &WARN("say: msg == $msg.");
215 if (&getChanConf('silent', $talkchannel)) {
216 &DEBUG("say: silent in $talkchannel, not saying $msg");
220 if ( $postprocess ) {
222 } elsif ($postprocess = &getChanConf('postprocess', $talkchannel)) {
223 &DEBUG("say: $postprocess $msg");
224 &parseCmdHook($postprocess . ' ' . $msg);
229 &status("<$mynick/$talkchannel> $msg");
231 return unless (&whatInterface() =~ /IRC/);
233 $msg = "zero" if ($msg =~ /^0+$/);
237 if ($t == $pubtime) {
239 $pubsize += length $msg;
241 my $i = &getChanConfDefault("sendPublicLimitLines", 3, $chan);
242 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000, $chan);
244 if ( ($pubcount % $i) == 0 and $pubcount) {
246 } elsif ($pubsize > $j) {
254 $pubsize = length $msg;
257 $conn->privmsg($talkchannel, $msg);
261 my ($nick, $msg) = @_;
262 if (!defined $nick) {
263 &ERROR("msg: nick == NULL.");
269 &WARN("msg: msg == $msg.");
273 # some say() end up here (eg +help)
274 if (&getChanConf('silent', $nick)) {
275 &DEBUG("msg: silent in $nick, not saying $msg");
279 &status(">$nick< $msg");
281 return unless (&whatInterface() =~ /IRC/);
284 if ($t == $msgtime) {
286 $msgsize += length $msg;
288 my $i = &getChanConfDefault("sendPrivateLimitLines", 3, $chan);
289 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000, $chan);
290 if ( ($msgcount % $i) == 0 and $msgcount) {
292 } elsif ($msgsize > $j) {
300 $msgsize = length $msg;
303 $conn->privmsg($nick, $msg);
306 # Usage: &action(nick || chan, txt);
308 my $mynick = $conn->nick();
309 my ($target, $txt) = @_;
311 &WARN("action: txt == NULL.");
315 if (&getChanConf('silent', $target)) {
316 &DEBUG("action: silent in $target, not doing $txt");
320 if (length $txt > 480) {
321 &status("action: txt too long; truncating.");
322 chop($txt) while (length $txt > 480);
325 &status("* $mynick/$target $txt");
326 $conn->me($target, $txt);
329 # Usage: ¬ice(nick || chan, txt);
331 my ($target, $txt) = @_;
333 &WARN("notice: txt == NULL.");
337 &status("-$target- $txt");
341 if ($t == $nottime) {
343 $notsize += length $txt;
345 my $i = &getChanConfDefault("sendNoticeLimitLines", 3, $chan);
346 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000, $chan);
348 if ( ($notcount % $i) == 0 and $notcount) {
350 } elsif ($notsize > $j) {
358 $notsize = length $txt;
361 $conn->notice($target, $txt);
365 my ($txt,$flag) = @_;
367 ### FIXME: flag not supported yet.
369 foreach (keys %{ $dcc{'CHAT'} }) {
370 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
375 ### perform commands.
378 # Usage: &performReply($reply);
382 if (!defined $reply or $reply =~ /^\s*$/) {
383 &DEBUG("performReply: reply == NULL.");
387 $reply =~ /([\.\?\s]+)$/;
389 # FIXME need real throttling....
390 if (length($reply) > $maxlinelen - 30) {
391 $reply = substr($reply, 0, $maxlinelen - 33);
392 $reply =~ s/ [^ ]*?$/ .../;
394 &checkMsgType($reply);
396 if ($msgType eq 'public') {
397 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
398 $reply = "$orig{who}: ".$reply;
400 $reply = "$reply, ".$orig{who};
404 } elsif ($msgType eq 'private') {
406 $reply = "$reply, ".$orig{who};
410 } elsif ($msgType eq 'chat') {
411 if (!exists $dcc{'CHAT'}{$who}) {
412 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
415 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
418 &ERROR("PR: msgType invalid? ($msgType).");
423 sub performAddressedReply {
424 return unless ($addressed);
428 # Usage: &performStrictReply($reply);
429 sub performStrictReply {
432 # FIXME need real throttling....
433 if (length($reply) > $maxlinelen - 30) {
434 $reply = substr($reply, 0, $maxlinelen - 33);
435 $reply =~ s/ [^ ]*?$/ .../;
437 &checkMsgType($reply);
439 if ($msgType eq 'private') {
441 } elsif ($msgType eq 'public') {
443 } elsif ($msgType eq 'chat') {
444 &dccsay(lc $who, $reply);
446 &ERROR("pSR: msgType invalid? ($msgType).");
451 my($who, $reply) = @_;
453 if (!defined $reply or $reply =~ /^\s*$/) {
454 &WARN("dccsay: reply == NULL.");
458 if (!exists $dcc{'CHAT'}{$who}) {
459 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
463 &status("=>$who<= $reply"); # dcc chat.
464 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
471 foreach $type (keys %dcc) {
472 &FIXME("dcc_close: $who");
473 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
474 next unless (scalar @who);
476 &DEBUG("dcc_close... close $who!");
481 my ($chan, $key) = @_;
482 $key ||= &getChanConf("chankey", $chan);
485 # forgot for about 2 years to implement channel keys when moving
486 # over to Net::IRC...
488 # hopefully validChan is right.
489 if (&validChan($chan)) {
490 &status("join: already on $chan?");
493 &status("joining $b_blue$chan $key$ob");
495 return if ($conn->join($chan, $key));
496 return if (&validChan($chan));
498 &DEBUG("joinchan: join failed. trying connect!");
508 next if ($chan eq "");
509 $chan =~ tr/A-Z/a-z/; # lowercase.
511 if ($chan !~ /^$mask{chan}$/) {
512 &WARN("part: chan is invalid ($chan)");
516 &status("parting $chan");
517 if (!&validChan($chan)) {
518 &WARN("part: not on $chan; doing anyway");
523 # deletion of $channels{chan} is done in &entryEvt().
528 my ($chan, @modes) = @_;
529 my $modes = join(" ", @modes);
531 if (&validChan($chan) == 0) {
532 &ERROR("mode: invalid chan => '$chan'.");
536 &DEBUG("mode: MODE $chan $modes");
538 # should move to use Net::IRC's $conn->mode()... but too lazy.
539 rawout("MODE $chan $modes");
543 my ($chan, @who) = @_;
544 my $os = "o" x scalar(@who);
546 &mode($chan, "+$os @who");
550 my ($chan, @who) = @_;
551 my $os = "o" x scalar(@who);
553 &mode($chan, "-$os ".@who);
557 my ($nick,$chan,$msg) = @_;
558 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
559 my $mynick = $conn->nick();
561 if ($chan ne "" and &validChan($chan) == 0) {
562 &ERROR("kick: invalid channel $chan.");
566 $nick =~ tr/A-Z/a-z/;
568 foreach $chan (@chans) {
569 if (!&IsNickInChan($nick,$chan)) {
570 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
574 if (!exists $channels{$chan}{o}{$mynick}) {
575 &status("kick: do not have ops on $chan :(");
579 &status("Kicking $nick from $chan.");
580 $conn->kick($chan, $nick, $msg);
585 my ($mask,$chan) = @_;
586 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
587 my $mynick = $conn->nick();
590 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
591 &ERROR("ban: invalid channel $chan.");
595 foreach $chan (@chans) {
596 if (!exists $channels{$chan}{o}{$mynick}) {
597 &status("ban: do not have ops on $chan :(");
601 &status("Banning $mask from $chan.");
602 &rawout("MODE $chan +b $mask");
610 my ($mask,$chan) = @_;
611 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
612 my $mynick = $conn->nick();
615 &DEBUG("unban: mask = $mask, chan = @chans");
617 foreach $chan (@chans) {
618 if (!exists $channels{$chan}{o}{$mynick}) {
619 &status("unBan: do not have ops on $chan :(");
623 &status("Removed ban $mask from $chan.");
624 &rawout("MODE $chan -b $mask");
634 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
635 $conn->quit($quitmsg);
637 &WARN("quit: could not quit!");
643 my $mynick = $conn->nick();
645 if (!defined $newnick) {
646 &ERROR("nick: nick == NULL.");
650 if (!defined $mynick) {
651 &WARN("nick: mynick == NULL.");
656 $bad++ if (exists $nuh{$newnick});
657 $bad++ if (&IsNickInAnyChan($newnick));
660 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
661 # hrm... over time we lose track of our own nick.
665 if ($newnick =~ /^$mask{nick}$/) {
666 &status("nick: Changing nick from $mynick to $newnick");
667 # ->nick() will NOT change cause we are using rawout?
668 &rawout("NICK $newnick");
671 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
676 my($who, $chan) = @_;
677 # TODO: check if $who or $chan are invalid.
679 $conn->invite($who, $chan);
683 # Channel related functions...
686 # Usage: &joinNextChan();
689 foreach (sort keys %conns) {
691 my $mynick = $conn->nick();
692 my @join = getJoinChans(1);
695 my $chan = shift @join;
698 if (my $i = scalar @join) {
699 &status("joinNextChan: $mynick $i chans to join.");
706 if (exists $cache{joinTime}) {
707 my $delta = time() - $cache{joinTime} - 5;
708 my $timestr = &Time2String($delta);
709 # FIXME: @join should be @in instead (hacked to 10)
710 #my $rate = sprintf("%.1f", $delta / @in);
711 my $rate = sprintf("%.1f", $delta / 10);
712 delete $cache{joinTime};
714 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
717 # chanserv check: global channels, in case we missed one.
718 foreach ( &ChanConfList("chanServ_ops") ) {
723 # Usage: &getNickInChans($nick);
728 foreach (keys %channels) {
729 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
736 # Usage: &getNicksInChan($chan);
741 return keys %{ $channels{$chan}{''} };
745 my ($nick,$chan) = @_;
747 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
750 &DEBUG("INIC: chan == NULL.");
754 if (&validChan($chan) == 0) {
755 &ERROR("INIC: invalid channel $chan.");
759 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
762 foreach (keys %channels) {
763 next unless (/[A-Z]/);
764 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
770 sub IsNickInAnyChan {
774 foreach $chan (keys %channels) {
775 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
781 # Usage: &validChan($chan);
783 # TODO: use $c instead?
786 if (!defined $chan or $chan =~ /^\s*$/) {
790 if (lc $chan ne $chan) {
791 &WARN("validChan: lc chan != chan. ($chan); fixing.");
792 $chan =~ tr/A-Z/a-z/;
795 # it's possible that this check creates the hash if empty.
796 if (defined $channels{$chan} or exists $channels{$chan}) {
797 if ($chan =~ /^_?default$/) {
798 # &WARN("validC: chan cannot be _default! returning 0!");
809 # Usage: &delUserInfo($nick,@chans);
811 my ($nick,@chans) = @_;
814 foreach $chan (@chans) {
815 foreach $mode (keys %{ $channels{$chan} }) {
817 next unless (exists $channels{$chan}{$mode}{$nick});
819 delete $channels{$chan}{$mode}{$nick};
827 delete $channels{$chan};
834 $cache{joinTime} = time();
844 # can't join any if not connected
845 return @join if (!$conn);
847 my $nick = $conn->nick();
849 foreach (keys %chanconf) {
850 next if ($_ eq "_default");
853 my $val = $chanconf{$_}{autojoin};
856 $skip++ if ($val eq "0");
858 # convert old +autojoin to autojoin <nick>
860 $chanconf{$_}{autojoin} = $val;
862 $skip++ if (lc $val ne lc $nick);
870 if (defined $channels{$_} or exists $channels{$_}) {
879 #$str .= ' in:' . join(',', sort @in) if scalar @in;
880 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
881 $str .= ' join:' . join(',', sort @join) if scalar @join;
883 &status("Chans: ($nick)$str") if ($show);
889 # &DEBUG("closeDCC called.");
892 foreach $type (keys %dcc) {
893 next if ($type ne uc($type));
896 foreach $nick (keys %{ $dcc{$type} }) {
897 next unless (defined $nick);
898 &status("DCC CHAT: closing DCC $type to $nick.");
899 next unless (defined $dcc{$type}{$nick});
901 my $ref = $dcc{$type}{$nick};
902 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
903 $dcc{$type}{$nick}->close();
904 delete $dcc{$type}{$nick};
905 &DEBUG("after close for $nick");
912 my($who,$chan,$userhost) = @_;
914 return unless (&IsChanConf("joinfloodCheck") > 0);
916 if (exists $netsplit{lc $who}) { # netsplit join.
917 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
920 if (exists $floodjoin{$chan}{$who}{Time}) {
921 &WARN("floodjoin{$chan}{$who} already exists?");
924 $floodjoin{$chan}{$who}{Time} = time();
925 $floodjoin{$chan}{$who}{Host} = $userhost;
928 foreach (keys %floodjoin) {
930 my $count = scalar keys %{ $floodjoin{$c} };
931 next unless ($count > 5);
932 &DEBUG("joinflood: count => $count");
935 foreach (keys %{ $floodjoin{$c} }) {
936 my $t = $floodjoin{$c}{$_}{Time};
937 next unless (defined $t);
941 &DEBUG("joinflood: time => $time");
944 &DEBUG("joinflood: new time => $time");
950 foreach $chan (keys %floodjoin) {
951 foreach $who (keys %{ $floodjoin{$chan} }) {
952 my $t = $floodjoin{$chan}{$who}{Time};
953 next unless (defined $t);
955 my $delta = $time - $t;
956 next unless ($delta > 10);
958 delete $floodjoin{$chan}{$who};
963 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
969 if (exists $nuh{$n}) {
970 return &makeHostMask($nuh{$n});
972 $cache{on_who_Hack} = 1;