]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
ban fixes.
[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(lc $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     &DEBUG("ban: mask = $mask, chan = $chan");
450     if ($chan ne "" and &validChan($chan) == 0) {
451         &ERROR("ban: invalid channel $chan.");
452         return;
453     }
454
455     foreach $chan (@chans) {
456         if (!exists $channels{$chan}{o}{$ident}) {
457             &status("Ban: do not have ops on $chan :(");
458             next;
459         }
460
461         &status("Banning $mask from $chan.");
462         &rawout("MODE $chan +b $mask");
463         $ban++;
464     }
465
466     return $ban;
467 }
468
469 sub quit {
470     my ($quitmsg) = @_;
471     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
472     $conn->quit($quitmsg);
473 }
474
475 sub nick {
476     my ($nick) = @_;
477
478     if ($nick =~ /^$mask{nick}$/) {
479         rawout("NICK ".$nick);
480         return 1;
481     }
482
483     return 0;
484 }
485
486 sub invite {
487     my($who, $chan) = @_;
488     rawout("INVITE $who $chan");
489 }
490
491
492 ##########
493 # Channel related functions...
494 #
495
496 # Usage: &joinNextChan();
497 sub joinNextChan {
498     if (scalar @joinchan) {
499         my $chan = shift @joinchan;
500         &joinchan($chan);
501
502         if (my $i = scalar @joinchan) {
503             &status("joinNextChan: $i chans to join.");
504         }
505         return;
506     }
507
508     if (&IsParam("nickServ_pass") and $nickserv < 1) {
509         &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
510         $nickserv--;
511     }
512
513     my %chan = &getChanConfList("chanServ");
514     foreach $chan (keys %chan) {
515         next unless ($chan{$chan} > 0);
516             
517         if (!exists $channels{$chan}{'o'}{$ident}) {
518             &status("ChanServ ==> Requesting ops for $chan.");
519             &rawout("PRIVMSG ChanServ :OP $chan $ident");
520         }
521     }
522 }
523
524 # Usage: &GetNickInChans($nick,$chan);
525 sub GetNickInChans {
526     my ($nick) = @_;
527     my @array;
528
529     foreach (keys %channels) {
530         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
531         push(@array, $_);
532     }
533
534     return @array;
535 }
536
537 # Usage: &GetNicksInChan($chan);
538 sub GetNicksInChan {
539     my ($chan) = @_;
540     my @array;
541
542     return keys %{ $channels{$chan}{''} };
543 }
544
545 sub IsNickInChan {
546     my ($nick,$chan) = @_;
547
548     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
549
550     if (&validChan($chan) == 0) {
551         &ERROR("INIC: invalid channel $chan.");
552         return 0;
553     }
554
555     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
556         return 1;
557     } else {
558         foreach (keys %channels) {
559             next unless (/[A-Z]/);
560             &DEBUG("hash channels contains mixed cased chan!!!");
561         }
562         return 0;
563     }
564 }
565
566 sub IsNickInAnyChan {
567     my ($nick) = @_;
568
569     foreach $chan (keys %channels) {
570         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
571         return 1;
572     }
573     return 0;
574 }
575
576 # Usage: &validChan($chan);
577 sub validChan {
578     my ($chan) = @_;
579
580     if (lc $chan ne $chan) {
581         &WARN("validChan: lc chan != chan. ($chan); fixing.");
582         $chan =~ tr/A-Z/a-z/;
583     }
584
585     if (exists $channels{$chan}) {
586         return 1;
587     } else {
588         return 0;
589     }
590 }
591
592 ###
593 # Usage: &DeleteUserInfo($nick,@chans);
594 sub DeleteUserInfo {
595     my ($nick,@chans) = @_;
596     my ($mode,$chan);
597
598     foreach $chan (@chans) {
599         foreach $mode (keys %{$channels{$chan}}) {
600             # use grep here?
601             next unless (exists $channels{$chan}{$mode}{$nick});
602
603             delete $channels{$chan}{$mode}{$nick};
604         }
605     }
606 }
607
608 sub clearChanVars {
609     my ($chan) = @_;
610
611     delete $channels{$chan};
612 }
613
614 sub clearIRCVars {
615     &DEBUG("clearIRCVars() called!");
616     undef %channels;
617     undef %floodjoin;
618
619     @joinchan   = &getJoinChans();
620 }
621
622 sub getJoinChans {
623     my @chans;
624     my @skip;
625
626     foreach (keys %chanconf) {
627         next if ($_ eq "_default");
628
629         my $val = $chanconf{$_}{autojoin};
630         my $skip = 0;
631
632         if (defined $val) {
633             $skip++ if ($val eq "0");
634         } else {
635             $skip++;
636         }
637
638         if ($skip) {
639             push(@skip, $_);
640             next;
641         }
642
643         push(@chans, $_);
644     }
645
646     if (scalar @skip) {
647         &status("gJC: channels not auto-joining: @skip");
648     } else {
649         &status("gJC: auto-joining all chans.");
650     }
651
652     return @chans;
653 }
654
655 sub closeDCC {
656     &DEBUG("closeDCC called.");
657
658     foreach $type (keys %dcc) {
659         next if ($type ne uc($type));
660  
661         foreach $nick (keys %{$dcc{$type}}) {
662             next unless (defined $nick);
663             &DEBUG("closing DCC $type to $nick (FIXME).");
664             next unless (defined $dcc{$type}{$nick});
665
666             $dcc{$type}{$nick}->close();
667         }
668     }
669 }
670
671 sub joinfloodCheck {
672     my($who,$chan,$userhost) = @_;
673
674     return unless (&IsChanConf("joinfloodCheck"));
675
676     if (exists $netsplit{lc $who}) {    # netsplit join.
677         &DEBUG("jfC: $who was in netsnipe; not checking.");
678     }
679
680     if (exists $floodjoin{$chan}{$who}{Time}) {
681         &WARN("floodjoin{$chan}{$who} already exists?");
682     }
683
684     $floodjoin{$chan}{$who}{Time} = time();
685     $floodjoin{$chan}{$who}{Host} = $userhost;
686
687     ### Check...
688     foreach (keys %floodjoin) {
689         my $c = $_;
690         my $count = scalar keys %{ $floodjoin{$c} };
691         next unless ($count > 5);
692         &DEBUG("count => $count");
693
694         my $time;
695         foreach (keys %{ $floodjoin{$c} }) {
696             $time += $floodjoin{$c}{$_}{Time};
697         }
698         &DEBUG("time => $time");
699         $time /= $count;
700
701         &DEBUG("new time => $time");
702     }
703
704     ### Clean it up.
705     my $delete = 0;
706     foreach $chan (keys %floodjoin) {
707         foreach $who (keys %{ $floodjoin{$chan} }) {
708             my $time = time() - $floodjoin{$chan}{$who}{Time};
709             next unless ($time > 10);
710             delete $floodjoin{$chan}{$who};
711             $delete++;
712         }
713     }
714
715     &DEBUG("jfC: $delete deleted.") if ($delete);
716 }
717
718 sub getHostMask {
719     my($n) = @_;
720
721     &FIXME("getHostMask...");
722 }
723
724 1;