]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
e3cae5683437a27076cf952329c8656bd7779970
[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     return '';
304 }
305
306 sub dccsay {
307     my ($who, $reply) = @_;
308     if (!exists $dcc{'CHAT'}{$who}) {
309         &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
310         return '';
311     }
312
313     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
314 }
315
316 sub dcc_close {
317     my($who) = @_;
318     my $type;
319
320     foreach $type (keys %dcc) {
321         &FIXME("dcc_close: $who");
322         my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
323         next unless (scalar @who);
324         $who = $who[0];
325     }
326 }
327
328 sub joinchan {
329     my ($chankey) = @_;
330     my $chan = lc $chankey;
331
332     if ($chankey =~ s/^($mask{chan}),\S+/ /) {
333         $chan = lc $1;
334     }
335
336     &status("joining $b_blue$chan$ob");
337
338     if (&validChan($chan)) {
339         &status("join: already on $chan");
340     } else {
341         $conn->join($chan);
342     }
343 }
344
345 sub part {
346     my $chan;
347
348     foreach $chan (@_) {
349         next if ($chan eq "");
350         $chan =~ tr/A-Z/a-z/;   # lowercase.
351
352         &status("parting $chan");
353         if (!&validChan($chan)) {
354             &status("part: not on $chan");
355             next;
356         }
357
358         rawout("PART $chan");
359         # deletion of $channels{chan} is done in &entryEvt().
360     }
361 }
362
363 sub mode {
364     my ($chan, @modes) = @_;
365     my $modes = join(" ", @modes);
366
367     if (&validChan($chan) == 0) {
368         &ERROR("mode: invalid chan => '$chan'.");
369         return;
370     }
371
372     &DEBUG("MODE $chan $modes");
373
374     rawout("MODE $chan $modes");
375 }
376
377 sub op {
378     my ($chan, @who) = @_;
379     my $os      = "o" x scalar(@who);
380
381     &mode($chan, "+$os ".@who);
382 }
383
384 sub deop {
385     my ($chan, @who) = @_;
386     my $os = "o" x scalar(@who);
387
388     &mode($chan, "-$os ".@who);
389 }
390
391 sub kick {
392     my ($nick,$chan,$msg) = @_;
393     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
394
395     if ($chan ne "" and &validChan($chan) == 0) {
396         &ERROR("kick: invalid channel $chan.");
397         return;
398     }
399
400     $nick =~ tr/A-Z/a-z/;
401
402     foreach $chan (@chans) {
403         if (!&IsNickInChan($nick,$chan)) {
404             &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
405             next;
406         }
407
408         if (!exists $channels{$chan}{o}{$ident}) {
409             &status("Kick: do not have ops on $chan :(");
410             next;
411         }
412
413         &status("Kicking $nick from $chan.");
414         if ($msg eq "") {
415             &rawout("KICK $chan $nick");
416         } else {
417             &rawout("KICK $chan $nick :$msg");
418         }
419     }
420 }
421
422 sub ban {
423     my ($mask,$chan) = @_;
424     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
425
426     if ($chan ne "" and &validChan($chan) == 0) {
427         &ERROR("ban: invalid channel $chan.");
428         return;
429     }
430
431     $nick =~ tr/A-Z/a-z/;
432
433     foreach $chan (@chans) {
434         if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
435             &status("Ban: $nick is not on $chan.");
436             next;
437         }
438
439         if (!exists $channels{$chan}{o}{$ident}) {
440             &status("Ban: do not have ops on $chan :(");
441             next;
442         }
443
444         &status("Banning $mask from $chan.");
445         &rawout("MODE $chan +b $mask");
446     }
447 }
448
449 sub quit {
450     my ($quitmsg) = @_;
451     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
452     $conn->quit($quitmsg);
453 }
454
455 sub nick {
456     my ($nick) = @_;
457
458     if ($nick =~ /^$mask{nick}$/) {
459         rawout("NICK ".$nick);
460         return 1;
461     }
462
463     return 0;
464 }
465
466 sub invite {
467     my($who, $chan) = @_;
468     rawout("INVITE $who $chan");
469 }
470
471
472 ##########
473 # Channel related functions...
474 #
475
476 # Usage: &joinNextChan();
477 sub joinNextChan {
478     if (scalar @joinchan) {
479         my $chan = shift @joinchan;
480         &joinchan($chan);
481
482         if (my $i = scalar @joinchan) {
483             &status("joinNextChan: $i chans to join.");
484         }
485     } else {
486         return unless (&IsParam("chanServ_ops"));
487         if (!$nickserv) {
488             &DEBUG("jNC: nickserv/chanserv not up?");
489         }
490
491         my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
492         foreach $chan (keys %channels) {
493             next unless (grep /^$chan$/i, @chans);
494
495             if (!exists $channels{$chan}{'o'}{$ident}) {
496                 &status("ChanServ ==> Requesting ops for $chan.");
497                 &rawout("PRIVMSG ChanServ :OP $chan $ident");
498             }
499         }
500     }
501 }
502
503 # Usage: &GetNickInChans($nick,$chan);
504 sub GetNickInChans {
505     my ($nick) = @_;
506     my @array;
507
508     foreach (keys %channels) {
509         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
510         push(@array, $_);
511     }
512
513     return @array;
514 }
515
516 sub IsNickInChan {
517     my ($nick,$chan) = @_;
518
519     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
520
521     if (&validChan($chan) == 0) {
522         &ERROR("INIC: invalid channel $chan.");
523         return 0;
524     }
525
526     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
527         return 1;
528     } else {
529         foreach (keys %channels) {
530             next unless (/[A-Z]/);
531             &DEBUG("hash channels contains mixed cased chan!!!");
532         }
533         return 0;
534     }
535 }
536
537 sub IsNickInAnyChan {
538     my ($nick) = @_;
539
540     foreach $chan (keys %channels) {
541         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
542         return 1;
543     }
544     return 0;
545 }
546
547 # Usage: &validChan($chan);
548 sub validChan {
549     my ($chan) = @_;
550
551     if (lc $chan ne $chan) {
552         &WARN("validChan: lc chan != chan. ($chan); fixing.");
553         $chan =~ tr/A-Z/a-z/;
554     }
555
556     if (exists $channels{$chan}) {
557         return 1;
558     } else {
559         return 0;
560     }
561 }
562
563 ###
564 # Usage: &DeleteUserInfo($nick,@chans);
565 sub DeleteUserInfo {
566     my ($nick,@chans) = @_;
567     my ($mode,$chan);
568
569     foreach $chan (@chans) {
570         foreach $mode (keys %{$channels{$chan}}) {
571             # use grep here?
572             next unless (exists $channels{$chan}{$mode}{$nick});
573
574             delete $channels{$chan}{$mode}{$nick};
575         }
576     }
577 }
578
579 sub clearChanVars {
580     my ($chan) = @_;
581
582     delete $channels{$chan};
583 }
584
585 sub clearIRCVars {
586     &DEBUG("clearIRCVars() called!");
587     undef %channels;
588     undef %floodjoin;
589     @joinchan = split /[\t\s]+/, $param{'join_channels'};
590 }
591
592 sub makeChanList {
593     my ($str)   = @_;
594     my $inverse = 0;
595     my @chans;
596
597     if ($str eq "ALL") {
598         return(keys %channels);
599     } elsif ($str =~ s/^ALL but //i) {
600         @chans = keys %channels;
601         foreach (split /[\s\t\,]+/, lc $str) {
602             @chans = grep !/^$_$/, @chans;
603         }
604     } else {
605         foreach (split /[\s\t\,]+/, lc $str) {
606             next unless (&validChan($_));
607             push(@chans, $_);
608         }
609     }
610
611     @chans;
612 }
613
614 sub closeDCC {
615     &DEBUG("closeDCC called.");
616
617     foreach $type (keys %dcc) {
618         next if ($type ne uc($type));
619  
620         foreach (keys %{$dcc{$type}}) {
621             &DEBUG("closing DCC $type to $_ (FIXME).");
622             $dcc{$type}{$_}->close();
623         }
624     }
625 }
626
627 sub joinfloodCheck {
628     my($who,$chan,$userhost) = @_;
629
630     return unless (&IsParam("joinfloodCheck"));
631
632     if (exists $netsplit{lc $who}) {    # netsplit join.
633         &DEBUG("jfC: $who was in netsnipe; not checking.");
634     }
635
636     if (exists $floodjoin{$chan}{$who}{Time}) {
637         &WARN("floodjoin{$chan}{$who} already exists?");
638     }
639
640     $floodjoin{$chan}{$who}{Time} = time();
641     $floodjoin{$chan}{$who}{Host} = $userhost;
642
643     ### Check...
644     foreach (keys %floodjoin) {
645         my $c = $_;
646         my $count = scalar keys %{ $floodjoin{$c} };
647         next unless ($count > 5);
648         &DEBUG("count => $count");
649
650         my $time;
651         foreach (keys %{ $floodjoin{$c} }) {
652             $time += $floodjoin{$c}{$_}{Time};
653         }
654         &DEBUG("time => $time");
655         $time /= $count;
656
657         &DEBUG("new time => $time");
658     }
659
660     ### Clean it up.
661     my $delete = 0;
662     foreach $chan (keys %floodjoin) {
663         foreach $who (keys %{ $floodjoin{$chan} }) {
664             my $time = time() - $floodjoin{$chan}{$who}{Time};
665             next unless ($time > 10);
666             delete $floodjoin{$chan}{$who};
667             $delete++;
668         }
669     }
670
671     &DEBUG("jfC: $delete deleted.") if ($delete);
672 }
673
674 1;