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