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