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