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);
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("main", $postprocess . ' ' . $msg);
221 &parseCmdHook("extra", $postprocess . ' ' . $msg);
226 &status("<$mynick/$talkchannel> $msg");
228 return unless (&whatInterface() =~ /IRC/);
230 $msg = "zero" if ($msg =~ /^0+$/);
234 if ($t == $pubtime) {
236 $pubsize += length $msg;
238 my $i = &getChanConfDefault("sendPublicLimitLines", 3);
239 my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
241 if ( ($pubcount % $i) == 0 and $pubcount) {
243 } elsif ($pubsize > $j) {
251 $pubsize = length $msg;
254 $conn->privmsg($talkchannel, $msg);
258 my ($nick, $msg) = @_;
259 if (!defined $nick) {
260 &ERROR("msg: nick == NULL.");
266 &WARN("msg: msg == $msg.");
270 &status(">$nick< $msg");
272 return unless (&whatInterface() =~ /IRC/);
275 if ($t == $msgtime) {
277 $msgsize += length $msg;
279 my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
280 my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
281 if ( ($msgcount % $i) == 0 and $msgcount) {
283 } elsif ($msgsize > $j) {
291 $msgsize = length $msg;
294 $conn->privmsg($nick, $msg);
297 # Usage: &action(nick || chan, txt);
299 my $mynick = $conn->nick();
300 my ($target, $txt) = @_;
302 &WARN("action: txt == NULL.");
306 if (length $txt > 480) {
307 &status("action: txt too long; truncating.");
308 chop($txt) while (length $txt > 480);
311 &status("* $mynick/$target $txt");
312 $conn->me($target, $txt);
315 # Usage: ¬ice(nick || chan, txt);
317 my ($target, $txt) = @_;
319 &WARN("notice: txt == NULL.");
323 &status("-$target- $txt");
327 if ($t == $nottime) {
329 $notsize += length $txt;
331 my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
332 my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
334 if ( ($notcount % $i) == 0 and $notcount) {
336 } elsif ($notsize > $j) {
344 $notsize = length $txt;
347 $conn->notice($target, $txt);
351 my ($txt,$flag) = @_;
353 ### FIXME: flag not supported yet.
355 foreach (keys %{ $dcc{'CHAT'} }) {
356 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
361 ### perform commands.
364 # Usage: &performReply($reply);
368 if (!defined $reply or $reply =~ /^\s*$/) {
369 &DEBUG("performReply: reply == NULL.");
373 $reply =~ /([\.\?\s]+)$/;
375 # FIXME need real throttling....
376 if (length($reply) > $maxlinelen - 30) {
377 $reply = substr($reply, 0, $maxlinelen - 33);
378 $reply =~ s/ [^ ]*?$/ .../;
380 &checkMsgType($reply);
382 if ($msgType eq 'public') {
383 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
384 $reply = "$orig{who}: ".$reply;
386 $reply = "$reply, ".$orig{who};
390 } elsif ($msgType eq 'private') {
392 $reply = "$reply, ".$orig{who};
396 } elsif ($msgType eq 'chat') {
397 if (!exists $dcc{'CHAT'}{$who}) {
398 &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
401 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
404 &ERROR("PR: msgType invalid? ($msgType).");
409 sub performAddressedReply {
410 return unless ($addressed);
414 # Usage: &performStrictReply($reply);
415 sub performStrictReply {
418 # FIXME need real throttling....
419 if (length($reply) > $maxlinelen - 30) {
420 $reply = substr($reply, 0, $maxlinelen - 33);
421 $reply =~ s/ [^ ]*?$/ .../;
423 &checkMsgType($reply);
425 if ($msgType eq 'private') {
427 } elsif ($msgType eq 'public') {
429 } elsif ($msgType eq 'chat') {
430 &dccsay(lc $who, $reply);
432 &ERROR("pSR: msgType invalid? ($msgType).");
437 my($who, $reply) = @_;
439 if (!defined $reply or $reply =~ /^\s*$/) {
440 &WARN("dccsay: reply == NULL.");
444 if (!exists $dcc{'CHAT'}{$who}) {
445 &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
449 &status("=>$who<= $reply"); # dcc chat.
450 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
457 foreach $type (keys %dcc) {
458 &FIXME("dcc_close: $who");
459 my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
460 next unless (scalar @who);
462 &DEBUG("dcc_close... close $who!");
467 my ($chan, $key) = @_;
468 $key ||= &getChanConf("chankey", $chan);
471 # forgot for about 2 years to implement channel keys when moving
472 # over to Net::IRC...
474 # hopefully validChan is right.
475 if (&validChan($chan)) {
476 &status("join: already on $chan?");
479 &status("joining $b_blue$chan $key$ob");
481 return if ($conn->join($chan, $key));
482 return if (&validChan($chan));
484 &DEBUG("joinchan: join failed. trying connect!");
494 next if ($chan eq "");
495 $chan =~ tr/A-Z/a-z/; # lowercase.
497 if ($chan !~ /^$mask{chan}$/) {
498 &WARN("part: chan is invalid ($chan)");
502 &status("parting $chan");
503 if (!&validChan($chan)) {
504 &WARN("part: not on $chan; doing anyway");
509 # deletion of $channels{chan} is done in &entryEvt().
514 my ($chan, @modes) = @_;
515 my $modes = join(" ", @modes);
517 if (&validChan($chan) == 0) {
518 &ERROR("mode: invalid chan => '$chan'.");
522 &DEBUG("mode: MODE $chan $modes");
524 # should move to use Net::IRC's $conn->mode()... but too lazy.
525 rawout("MODE $chan $modes");
529 my ($chan, @who) = @_;
530 my $os = "o" x scalar(@who);
532 &mode($chan, "+$os @who");
536 my ($chan, @who) = @_;
537 my $os = "o" x scalar(@who);
539 &mode($chan, "-$os ".@who);
543 my ($nick,$chan,$msg) = @_;
544 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
545 my $mynick = $conn->nick();
547 if ($chan ne "" and &validChan($chan) == 0) {
548 &ERROR("kick: invalid channel $chan.");
552 $nick =~ tr/A-Z/a-z/;
554 foreach $chan (@chans) {
555 if (!&IsNickInChan($nick,$chan)) {
556 &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
560 if (!exists $channels{$chan}{o}{$mynick}) {
561 &status("kick: do not have ops on $chan :(");
565 &status("Kicking $nick from $chan.");
566 $conn->kick($chan, $nick, $msg);
571 my ($mask,$chan) = @_;
572 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
573 my $mynick = $conn->nick();
576 if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
577 &ERROR("ban: invalid channel $chan.");
581 foreach $chan (@chans) {
582 if (!exists $channels{$chan}{o}{$mynick}) {
583 &status("ban: do not have ops on $chan :(");
587 &status("Banning $mask from $chan.");
588 &rawout("MODE $chan +b $mask");
596 my ($mask,$chan) = @_;
597 my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
598 my $mynick = $conn->nick();
601 &DEBUG("unban: mask = $mask, chan = @chans");
603 foreach $chan (@chans) {
604 if (!exists $channels{$chan}{o}{$mynick}) {
605 &status("unBan: do not have ops on $chan :(");
609 &status("Removed ban $mask from $chan.");
610 &rawout("MODE $chan -b $mask");
620 &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
621 $conn->quit($quitmsg);
623 &WARN("quit: could not quit!");
629 my $mynick = $conn->nick();
631 if (!defined $newnick) {
632 &ERROR("nick: nick == NULL.");
636 if (!defined $mynick) {
637 &WARN("nick: mynick == NULL.");
642 $bad++ if (exists $nuh{$newnick});
643 $bad++ if (&IsNickInAnyChan($newnick));
646 &WARN("Nick: not going to try to change from $mynick to $newnick. [". scalar(gmtime). "]");
647 # hrm... over time we lose track of our own nick.
651 if ($newnick =~ /^$mask{nick}$/) {
652 &status("nick: Changing nick from $mynick to $newnick");
653 # ->nick() will NOT change cause we are using rawout?
654 &rawout("NICK $newnick");
657 &DEBUG("nick: failed... why oh why (mynick=$mynick, newnick=$newnick)");
662 my($who, $chan) = @_;
663 # TODO: check if $who or $chan are invalid.
665 $conn->invite($who, $chan);
669 # Channel related functions...
672 # Usage: &joinNextChan();
675 foreach (sort keys %conns) {
677 my $mynick = $conn->nick();
678 my @join = getJoinChans(1);
681 my $chan = shift @join;
684 if (my $i = scalar @join) {
685 &status("joinNextChan: $mynick $i chans to join.");
692 if (exists $cache{joinTime}) {
693 my $delta = time() - $cache{joinTime} - 5;
694 my $timestr = &Time2String($delta);
695 # FIXME: @join should be @in instead (hacked to 10)
696 #my $rate = sprintf("%.1f", $delta / @in);
697 my $rate = sprintf("%.1f", $delta / 10);
698 delete $cache{joinTime};
700 &status("time taken to join all chans: $timestr; rate: $rate sec/join");
703 # chanserv check: global channels, in case we missed one.
704 foreach ( &ChanConfList("chanServ_ops") ) {
709 # Usage: &getNickInChans($nick);
714 foreach (keys %channels) {
715 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
722 # Usage: &getNicksInChan($chan);
727 return keys %{ $channels{$chan}{''} };
731 my ($nick,$chan) = @_;
733 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
736 &DEBUG("INIC: chan == NULL.");
740 if (&validChan($chan) == 0) {
741 &ERROR("INIC: invalid channel $chan.");
745 if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
748 foreach (keys %channels) {
749 next unless (/[A-Z]/);
750 &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
756 sub IsNickInAnyChan {
760 foreach $chan (keys %channels) {
761 next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} });
767 # Usage: &validChan($chan);
769 # TODO: use $c instead?
772 if (!defined $chan or $chan =~ /^\s*$/) {
776 if (lc $chan ne $chan) {
777 &WARN("validChan: lc chan != chan. ($chan); fixing.");
778 $chan =~ tr/A-Z/a-z/;
781 # it's possible that this check creates the hash if empty.
782 if (defined $channels{$chan} or exists $channels{$chan}) {
783 if ($chan =~ /^_?default$/) {
784 # &WARN("validC: chan cannot be _default! returning 0!");
795 # Usage: &delUserInfo($nick,@chans);
797 my ($nick,@chans) = @_;
800 foreach $chan (@chans) {
801 foreach $mode (keys %{ $channels{$chan} }) {
803 next unless (exists $channels{$chan}{$mode}{$nick});
805 delete $channels{$chan}{$mode}{$nick};
813 delete $channels{$chan};
820 $cache{joinTime} = time();
830 # can't join any if not connected
831 return @join if (!$conn);
833 my $nick = $conn->nick();
835 foreach (keys %chanconf) {
836 next if ($_ eq "_default");
839 my $val = $chanconf{$_}{autojoin};
842 $skip++ if ($val eq "0");
844 # convert old +autojoin to autojoin <nick>
846 $chanconf{$_}{autojoin} = $val;
848 $skip++ if ($val ne $nick);
856 if (defined $channels{$_} or exists $channels{$_}) {
865 #$str .= ' in:' . join(',', sort @in) if scalar @in;
866 #$str .= ' skip:' . join(',', sort @skip) if scalar @skip;
867 $str .= ' join:' . join(',', sort @join) if scalar @join;
869 &status("Chans: ($nick)$str") if ($show);
875 # &DEBUG("closeDCC called.");
878 foreach $type (keys %dcc) {
879 next if ($type ne uc($type));
882 foreach $nick (keys %{ $dcc{$type} }) {
883 next unless (defined $nick);
884 &status("DCC CHAT: closing DCC $type to $nick.");
885 next unless (defined $dcc{$type}{$nick});
887 my $ref = $dcc{$type}{$nick};
888 &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
889 $dcc{$type}{$nick}->close();
890 delete $dcc{$type}{$nick};
891 &DEBUG("after close for $nick");
898 my($who,$chan,$userhost) = @_;
900 return unless (&IsChanConf("joinfloodCheck"));
902 if (exists $netsplit{lc $who}) { # netsplit join.
903 &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
906 if (exists $floodjoin{$chan}{$who}{Time}) {
907 &WARN("floodjoin{$chan}{$who} already exists?");
910 $floodjoin{$chan}{$who}{Time} = time();
911 $floodjoin{$chan}{$who}{Host} = $userhost;
914 foreach (keys %floodjoin) {
916 my $count = scalar keys %{ $floodjoin{$c} };
917 next unless ($count > 5);
918 &DEBUG("joinflood: count => $count");
921 foreach (keys %{ $floodjoin{$c} }) {
922 my $t = $floodjoin{$c}{$_}{Time};
923 next unless (defined $t);
927 &DEBUG("joinflood: time => $time");
930 &DEBUG("joinflood: new time => $time");
936 foreach $chan (keys %floodjoin) {
937 foreach $who (keys %{ $floodjoin{$chan} }) {
938 my $t = $floodjoin{$chan}{$who}{Time};
939 next unless (defined $t);
941 my $delta = $time - $t;
942 next unless ($delta > 10);
944 delete $floodjoin{$chan}{$who};
949 &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
955 if (exists $nuh{$n}) {
956 return &makeHostMask($nuh{$n});
958 $cache{on_who_Hack} = 1;