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