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