]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
woops, use while instead of foreach for ircloop
[infobot.git] / src / IRC / Irc.pl
1 #
2 #    Irc.pl: IRC core stuff.
3 #    Author: dms
4 #   Version: 20000126
5 #      NOTE: Based on code by Kevin Lenzo & Patrick Cole  (c) 1997
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 use vars qw($nickserv);
11 $nickserv       = 0;
12
13 # static scalar variables.
14 $mask{ip}       = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
15 $mask{host}     = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
16 $mask{chan}     = '[\#\&]\S*|_default';
17 my $isnick1     = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
18 my $isnick2     = '0-9\-';
19 $mask{nick}     = "[$isnick1]{1}[$isnick1$isnick2]*";
20 $mask{nuh}      = '\S*!\S*\@\S*';
21
22 sub ircloop {
23     my $error   = 0;
24     my $lastrun = 0;
25
26 loop:;
27     while (my $host = shift @ircServers) {
28         # JUST IN CASE. irq was complaining about this.
29         if ($lastrun == time()) {
30             &DEBUG("hrm... lastrun == time()");
31             $error++;
32             sleep 10;
33             next;
34         }
35
36         if (!defined $host) {
37             &DEBUG("ircloop: ircServers[x] = NULL.");
38             $lastrun = time();
39             next;
40         }
41         next unless (exists $ircPort{$host});
42
43         my $retval = &irc($host, $ircPort{$host});
44         &DEBUG("ircloop: after irc()");
45
46         next unless (defined $retval and $retval == 0);
47
48         $error++;
49
50         if ($error % 3 == 0 and $error != 0) {
51             &ERROR("CANNOT connect to this server; next!");
52             next;
53         }
54
55         if ($error >= 3*3) {
56             &ERROR("CANNOT connect to any irc server; stopping.");
57             exit 1;
58         }
59     }
60
61     &DEBUG("ircloop: end... going back.");
62     &loadIRCServers();
63     goto loop;
64 }
65
66 sub irc {
67     my ($server,$port) = @_;
68
69     my $iaddr = inet_aton($server);
70     my $paddr = sockaddr_in($port, $iaddr);
71     my $proto = getprotobyname('tcp');
72
73     select STDOUT;
74     &status("Connecting to port $port of server $server ...");
75
76     # host->ip.
77     if ($server =~ /\D$/) {
78         my $packed = scalar(gethostbyname($server));
79
80         if (!defined $packed) {
81             &status("  cannot resolve $server.");
82             return 0;
83         }
84
85         my $resolve = inet_ntoa($packed);
86         &status("  resolved to $resolve.");
87         ### warning in Sys/Hostname line 78???
88         ### caused inside Net::IRC?
89     }
90
91     $irc = new Net::IRC;
92
93     $conn = $irc->newconn(
94                 Nick    => $param{'ircNick'},
95                 Server  => $server,
96                 Port    => $port,
97                 Ircname => $param{'ircName'},
98                 LocalAddr => $param{'ircHost'},
99     );
100
101     if (!defined $conn) {
102         &ERROR("irc: conn was not created!defined!!!");
103         return 1;
104     }
105
106     &clearIRCVars();
107
108     # change internal timeout value for scheduler.
109     $irc->{_timeout}    = 10;   # how about 60?
110
111     $ircstats{'Server'} = "$server:$port";
112
113     # handler stuff.
114         $conn->add_handler('caction',   \&on_action);
115         $conn->add_handler('cdcc',      \&on_dcc);
116         $conn->add_handler('cping',     \&on_ping);
117         $conn->add_handler('crping',    \&on_ping_reply);
118         $conn->add_handler('cversion',  \&on_version);
119         $conn->add_handler('crversion', \&on_crversion);
120         $conn->add_handler('dcc_open',  \&on_dcc_open);
121         $conn->add_handler('dcc_close', \&on_dcc_close);
122         $conn->add_handler('chat',      \&on_chat);
123         $conn->add_handler('msg',       \&on_msg);
124         $conn->add_handler('public',    \&on_public);
125         $conn->add_handler('join',      \&on_join);
126         $conn->add_handler('part',      \&on_part);
127         $conn->add_handler('topic',     \&on_topic);
128         $conn->add_handler('invite',    \&on_invite);
129         $conn->add_handler('kick',      \&on_kick);
130         $conn->add_handler('mode',      \&on_mode);
131         $conn->add_handler('nick',      \&on_nick);
132         $conn->add_handler('quit',      \&on_quit);
133         $conn->add_handler('notice',    \&on_notice);
134         $conn->add_handler('whoisuser', \&on_whoisuser);
135         $conn->add_handler('other',     \&on_other);
136         $conn->add_global_handler('disconnect', \&on_disconnect);
137         $conn->add_global_handler([251,252,253,254,255], \&on_init);
138 ###     $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
139         $conn->add_global_handler(324, \&on_modeis);
140         $conn->add_global_handler(333, \&on_topicinfo);
141         $conn->add_global_handler(352, \&on_who);
142         $conn->add_global_handler(353, \&on_names);
143         $conn->add_global_handler(366, \&on_endofnames);
144         $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
145         $conn->add_global_handler(433, \&on_nick_taken);
146         $conn->add_global_handler(439, \&on_targettoofast);
147
148     # end of handler stuff.
149
150     $irc->start;
151 }
152
153 ######################################################################
154 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
155 ######################################################################
156
157 sub rawout {
158     my ($buf) = @_;
159     $buf =~ s/\n//gi;
160
161     # slow down a bit if traffic is "high".
162     # need to take into account time of last message sent.
163     if ($last{buflen} > 256 and length($buf) > 256) {
164         sleep 1;
165     }
166
167     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
168
169     $last{buflen} = length($buf);
170 }
171
172 sub say {
173     my ($msg) = @_;
174     if (!defined $msg) {
175         $msg ||= "NULL";
176         &DEBUG("say: msg == $msg.");
177         return;
178     }
179
180     if ($msg eq $last{say} and length($msg) > 256) {
181         &status("say: detected repeated message; skipping.");
182         return;
183     }
184     $last{say} = $msg;
185
186     &status("</$talkchannel> $msg");
187     if (&whatInterface() =~ /IRC/) {
188         $msg = "zero" if ($msg =~ /^0+$/);
189
190         $conn->privmsg($talkchannel, $msg);
191     }
192 }
193
194 sub msg {
195     my ($nick, $msg) = @_;
196     if (!defined $nick) {
197         &ERROR("msg: nick == NULL.");
198         return;
199     }
200
201     if (!defined $msg) {
202         $msg ||= "NULL";
203         &DEBUG("msg: msg == $msg.");
204         return;
205     }
206
207     if ($msg eq $last{msg} and length($msg) > 256) {
208         &status("msg: detected repeated message; skipping.");
209         return;
210     }
211     $last{msg} = $msg;
212
213     &status(">$nick< $msg");
214     $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
215 }
216
217 # Usage: &action(nick || chan, txt);
218 sub action {
219     my ($target, $txt) = @_;
220     if (!defined $txt) {
221         &DEBUG("action: txt == NULL.");
222         return;
223     }
224
225     my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
226     if (length $rawout > 510) {
227         &status("action: txt too long; truncating.");
228
229         chop($rawout) while (length($rawout) > 510);
230         $rawout .= "\001";
231     }
232
233     &status("* $ident/$target $txt");
234     rawout($rawout);
235 }
236
237 # Usage: &action(nick || chan, txt);
238 sub notice{
239     my ($target, $txt) = @_;
240     if (!defined $txt) {
241         &DEBUG("action: txt == NULL.");
242         return;
243     }
244
245     &status("-$target- $txt");
246
247     $conn->notice($target, $txt);
248 }
249
250
251 sub DCCBroadcast {
252     my ($txt,$flag) = @_;
253
254     ### FIXME: flag not supported yet.
255
256     foreach (keys %{$dcc{'CHAT'}}) {
257         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
258     }
259 }
260
261 ##########
262 ### perform commands.
263 ###
264
265 # Usage: &performReply($reply);
266 sub performReply {
267     my ($reply) = @_;
268     $reply =~ /([\.\?\s]+)$/;
269
270     &checkMsgType($reply);
271
272     if ($msgType eq 'public') {
273         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
274             $reply = "$orig{who}: ".$reply;
275         } else {
276             $reply = "$reply, ".$orig{who};
277         }
278         &say($reply);
279     } elsif ($msgType eq 'private') {
280         if (rand() < 0.5) {
281             $reply = $reply;
282         } else {
283             $reply = "$reply, ".$orig{who};
284         }
285         &msg($who, $reply);
286     } elsif ($msgType eq 'chat') {
287         if (!exists $dcc{'CHAT'}{$who}) {
288             &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
289             return;
290         }
291         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
292     } else {
293         &ERROR("PR: msgType invalid? ($msgType).");
294     }
295 }
296
297 # ...
298 sub performAddressedReply {
299     return unless ($addressed);
300     &performReply(@_);
301 }
302
303 sub pSReply {
304     &performStrictReply(@_);
305 }
306
307 # Usage: &performStrictReply($reply);
308 sub performStrictReply {
309     my ($reply) = @_;
310
311     &checkMsgType($reply);
312
313     if ($msgType eq 'private') {
314         &msg($who, $reply);
315     } elsif ($msgType eq 'public') {
316         &say($reply);
317     } elsif ($msgType eq 'chat') {
318         &dccsay($who,$reply);
319     } else {
320         &ERROR("pSR: msgType invalid? ($msgType).");
321     }
322 }
323
324 sub dccsay {
325     my ($who, $reply) = @_;
326     if (!exists $dcc{'CHAT'}{$who}) {
327         &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
328         return '';
329     }
330
331     &status("=>$who<= $reply");         # dcc chat.
332     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
333 }
334
335 sub dcc_close {
336     my($who) = @_;
337     my $type;
338
339     foreach $type (keys %dcc) {
340         &FIXME("dcc_close: $who");
341         my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
342         next unless (scalar @who);
343         $who = $who[0];
344     }
345 }
346
347 sub joinchan {
348     my ($chankey) = @_;
349     my $chan = lc $chankey;
350
351     if ($chankey =~ s/^($mask{chan}),\S+/ /) {
352         $chan = lc $1;
353     }
354
355     &status("joining $b_blue$chan$ob");
356
357     if (&validChan($chan)) {
358         &status("join: already on $chan");
359     } else {
360         if (!$conn->join($chan)) {
361             &DEBUG("join failed. trying connect!");
362             $conn->connect();
363         }
364     }
365 }
366
367 sub part {
368     my $chan;
369
370     foreach $chan (@_) {
371         next if ($chan eq "");
372         $chan =~ tr/A-Z/a-z/;   # lowercase.
373
374         &status("parting $chan");
375         if (!&validChan($chan)) {
376             &status("part: not on $chan");
377             next;
378         }
379
380         rawout("PART $chan");
381         # deletion of $channels{chan} is done in &entryEvt().
382     }
383 }
384
385 sub mode {
386     my ($chan, @modes) = @_;
387     my $modes = join(" ", @modes);
388
389     if (&validChan($chan) == 0) {
390         &ERROR("mode: invalid chan => '$chan'.");
391         return;
392     }
393
394     &DEBUG("MODE $chan $modes");
395
396     rawout("MODE $chan $modes");
397 }
398
399 sub op {
400     my ($chan, @who) = @_;
401     my $os      = "o" x scalar(@who);
402
403     &mode($chan, "+$os ".@who);
404 }
405
406 sub deop {
407     my ($chan, @who) = @_;
408     my $os = "o" x scalar(@who);
409
410     &mode($chan, "-$os ".@who);
411 }
412
413 sub kick {
414     my ($nick,$chan,$msg) = @_;
415     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
416
417     if ($chan ne "" and &validChan($chan) == 0) {
418         &ERROR("kick: invalid channel $chan.");
419         return;
420     }
421
422     $nick =~ tr/A-Z/a-z/;
423
424     foreach $chan (@chans) {
425         if (!&IsNickInChan($nick,$chan)) {
426             &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
427             next;
428         }
429
430         if (!exists $channels{$chan}{o}{$ident}) {
431             &status("Kick: do not have ops on $chan :(");
432             next;
433         }
434
435         &status("Kicking $nick from $chan.");
436         if ($msg eq "") {
437             &rawout("KICK $chan $nick");
438         } else {
439             &rawout("KICK $chan $nick :$msg");
440         }
441     }
442 }
443
444 sub ban {
445     my ($mask,$chan) = @_;
446     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
447     my $ban     = 0;
448
449     if ($chan ne "" and &validChan($chan) == 0) {
450         &ERROR("ban: invalid channel $chan.");
451         return;
452     }
453
454     $nick =~ tr/A-Z/a-z/;
455
456     foreach $chan (@chans) {
457         if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
458             &status("Ban: $nick is not on $chan.");
459             next;
460         }
461
462         if (!exists $channels{$chan}{o}{$ident}) {
463             &status("Ban: do not have ops on $chan :(");
464             next;
465         }
466
467         &status("Banning $mask from $chan.");
468         &rawout("MODE $chan +b $mask");
469         $ban++;
470     }
471
472     return $ban;
473 }
474
475 sub quit {
476     my ($quitmsg) = @_;
477     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
478     $conn->quit($quitmsg);
479 }
480
481 sub nick {
482     my ($nick) = @_;
483
484     if ($nick =~ /^$mask{nick}$/) {
485         rawout("NICK ".$nick);
486         return 1;
487     }
488
489     return 0;
490 }
491
492 sub invite {
493     my($who, $chan) = @_;
494     rawout("INVITE $who $chan");
495 }
496
497
498 ##########
499 # Channel related functions...
500 #
501
502 # Usage: &joinNextChan();
503 sub joinNextChan {
504     if (scalar @joinchan) {
505         my $chan = shift @joinchan;
506         &joinchan($chan);
507
508         if (my $i = scalar @joinchan) {
509             &status("joinNextChan: $i chans to join.");
510         }
511         return;
512     }
513
514     if (&IsParam("nickServ_pass") and $nickserv < 1) {
515         &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
516         $nickserv--;
517     }
518
519     my %chan = &getChanConfList("chanServ");
520     foreach $chan (keys %chan) {
521         next unless ($chan{$chan} > 0);
522             
523         if (!exists $channels{$chan}{'o'}{$ident}) {
524             &status("ChanServ ==> Requesting ops for $chan.");
525             &rawout("PRIVMSG ChanServ :OP $chan $ident");
526         }
527     }
528 }
529
530 # Usage: &GetNickInChans($nick,$chan);
531 sub GetNickInChans {
532     my ($nick) = @_;
533     my @array;
534
535     foreach (keys %channels) {
536         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
537         push(@array, $_);
538     }
539
540     return @array;
541 }
542
543 # Usage: &GetNicksInChan($chan);
544 sub GetNicksInChan {
545     my ($chan) = @_;
546     my @array;
547
548     return keys %{ $channels{$chan}{''} };
549 }
550
551 sub IsNickInChan {
552     my ($nick,$chan) = @_;
553
554     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
555
556     if (&validChan($chan) == 0) {
557         &ERROR("INIC: invalid channel $chan.");
558         return 0;
559     }
560
561     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
562         return 1;
563     } else {
564         foreach (keys %channels) {
565             next unless (/[A-Z]/);
566             &DEBUG("hash channels contains mixed cased chan!!!");
567         }
568         return 0;
569     }
570 }
571
572 sub IsNickInAnyChan {
573     my ($nick) = @_;
574
575     foreach $chan (keys %channels) {
576         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
577         return 1;
578     }
579     return 0;
580 }
581
582 # Usage: &validChan($chan);
583 sub validChan {
584     my ($chan) = @_;
585
586     if (lc $chan ne $chan) {
587         &WARN("validChan: lc chan != chan. ($chan); fixing.");
588         $chan =~ tr/A-Z/a-z/;
589     }
590
591     if (exists $channels{$chan}) {
592         return 1;
593     } else {
594         return 0;
595     }
596 }
597
598 ###
599 # Usage: &DeleteUserInfo($nick,@chans);
600 sub DeleteUserInfo {
601     my ($nick,@chans) = @_;
602     my ($mode,$chan);
603
604     foreach $chan (@chans) {
605         foreach $mode (keys %{$channels{$chan}}) {
606             # use grep here?
607             next unless (exists $channels{$chan}{$mode}{$nick});
608
609             delete $channels{$chan}{$mode}{$nick};
610         }
611     }
612 }
613
614 sub clearChanVars {
615     my ($chan) = @_;
616
617     delete $channels{$chan};
618 }
619
620 sub clearIRCVars {
621     &DEBUG("clearIRCVars() called!");
622     undef %channels;
623     undef %floodjoin;
624
625     @joinchan   = &getJoinChans();
626 }
627
628 sub getJoinChans {
629     my @chans;
630     my @skip;
631
632     foreach (keys %chanconf) {
633         next if ($_ eq "_default");
634
635         my $val = $chanconf{$_}{autojoin};
636         my $skip = 0;
637
638         if (defined $val) {
639             $skip++ if ($val eq "0");
640         } else {
641             $skip++;
642         }
643
644         if ($skip) {
645             push(@skip, $_);
646             next;
647         }
648
649         push(@chans, $_);
650     }
651
652     if (scalar @skip) {
653         &status("gJC: channels not auto-joining: @skip");
654     } else {
655         &status("gJC: auto-joining all chans.");
656     }
657
658     return @chans;
659 }
660
661 sub closeDCC {
662     &DEBUG("closeDCC called.");
663
664     foreach $type (keys %dcc) {
665         next if ($type ne uc($type));
666  
667         foreach $nick (keys %{$dcc{$type}}) {
668             next unless (defined $nick);
669             &DEBUG("closing DCC $type to $nick (FIXME).");
670             next unless (defined $dcc{$type}{$nick});
671
672             $dcc{$type}{$nick}->close();
673         }
674     }
675 }
676
677 sub joinfloodCheck {
678     my($who,$chan,$userhost) = @_;
679
680     return unless (&IsChanConf("joinfloodCheck"));
681
682     if (exists $netsplit{lc $who}) {    # netsplit join.
683         &DEBUG("jfC: $who was in netsnipe; not checking.");
684     }
685
686     if (exists $floodjoin{$chan}{$who}{Time}) {
687         &WARN("floodjoin{$chan}{$who} already exists?");
688     }
689
690     $floodjoin{$chan}{$who}{Time} = time();
691     $floodjoin{$chan}{$who}{Host} = $userhost;
692
693     ### Check...
694     foreach (keys %floodjoin) {
695         my $c = $_;
696         my $count = scalar keys %{ $floodjoin{$c} };
697         next unless ($count > 5);
698         &DEBUG("count => $count");
699
700         my $time;
701         foreach (keys %{ $floodjoin{$c} }) {
702             $time += $floodjoin{$c}{$_}{Time};
703         }
704         &DEBUG("time => $time");
705         $time /= $count;
706
707         &DEBUG("new time => $time");
708     }
709
710     ### Clean it up.
711     my $delete = 0;
712     foreach $chan (keys %floodjoin) {
713         foreach $who (keys %{ $floodjoin{$chan} }) {
714             my $time = time() - $floodjoin{$chan}{$who}{Time};
715             next unless ($time > 10);
716             delete $floodjoin{$chan}{$who};
717             $delete++;
718         }
719     }
720
721     &DEBUG("jfC: $delete deleted.") if ($delete);
722 }
723
724 sub getHostMask {
725     my($n) = @_;
726
727     &FIXME("getHostMask...");
728 }
729
730 1;