]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
Many changes, basically added and integrated News, and bug fixes.
[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
184         $conn->privmsg($talkchannel, $msg);
185     }
186 }
187
188 sub msg {
189     my ($nick, $msg) = @_;
190     if (!defined $nick) {
191         &ERROR("msg: nick == NULL.");
192         return;
193     }
194
195     if (!defined $msg) {
196         $msg ||= "NULL";
197         &DEBUG("msg: msg == $msg.");
198         return;
199     }
200
201     &status(">$nick< $msg");
202     $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
203 }
204
205 # Usage: &action(nick || chan, txt);
206 sub action {
207     my ($target, $txt) = @_;
208     if (!defined $txt) {
209         &DEBUG("action: txt == NULL.");
210         return;
211     }
212
213     my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
214     if (length $rawout > 510) {
215         &status("action: txt too long; truncating.");
216
217         chop($rawout) while (length($rawout) > 510);
218         $rawout .= "\001";
219     }
220
221     &status("* $ident/$target $txt");
222     rawout($rawout);
223 }
224
225 # Usage: &action(nick || chan, txt);
226 sub notice{
227     my ($target, $txt) = @_;
228     if (!defined $txt) {
229         &DEBUG("action: txt == NULL.");
230         return;
231     }
232
233     &status("-$target- $txt");
234
235     $conn->notice($target, $txt);
236 }
237
238
239 sub DCCBroadcast {
240     my ($txt,$flag) = @_;
241
242     ### FIXME: flag not supported yet.
243
244     foreach (keys %{$dcc{'CHAT'}}) {
245         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
246     }
247 }
248
249 ##########
250 ### perform commands.
251 ###
252
253 # Usage: &performReply($reply);
254 sub performReply {
255     my ($reply) = @_;
256     $reply =~ /([\.\?\s]+)$/;
257
258     &checkMsgType($reply);
259
260     if ($msgType eq 'public') {
261         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
262             $reply = "$orig{who}: ".$reply;
263         } else {
264             $reply = "$reply, ".$orig{who};
265         }
266         &say($reply);
267     } elsif ($msgType eq 'private') {
268         if (rand() < 0.5) {
269             $reply = $reply;
270         } else {
271             $reply = "$reply, ".$orig{who};
272         }
273         &msg($who, $reply);
274     } elsif ($msgType eq 'chat') {
275         if (!exists $dcc{'CHAT'}{$who}) {
276             &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
277             return;
278         }
279         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
280     } else {
281         &ERROR("PR: msgType invalid? ($msgType).");
282     }
283 }
284
285 # ...
286 sub performAddressedReply {
287     return unless ($addressed);
288     &performReply(@_);
289 }
290
291 sub pSReply {
292     &performStrictReply(@_);
293 }
294
295 # Usage: &performStrictReply($reply);
296 sub performStrictReply {
297     my ($reply) = @_;
298
299     &checkMsgType($reply);
300
301     if ($msgType eq 'private') {
302         &msg($who, $reply);
303     } elsif ($msgType eq 'public') {
304         &say($reply);
305     } elsif ($msgType eq 'chat') {
306         &dccsay(lc $who,$reply);
307     } else {
308         &ERROR("pSR: msgType invalid? ($msgType).");
309     }
310 }
311
312 sub dccsay {
313     my ($who, $reply) = @_;
314     if (!exists $dcc{'CHAT'}{$who}) {
315         &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
316         return '';
317     }
318
319     &status("=>$who<= $reply");         # dcc chat.
320     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
321 }
322
323 sub dcc_close {
324     my($who) = @_;
325     my $type;
326
327     foreach $type (keys %dcc) {
328         &FIXME("dcc_close: $who");
329         my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
330         next unless (scalar @who);
331         $who = $who[0];
332     }
333 }
334
335 sub joinchan {
336     my ($chankey) = @_;
337     my $chan = lc $chankey;
338
339     if ($chankey =~ s/^($mask{chan}),\S+/ /) {
340         $chan = lc $1;
341     }
342
343     &status("joining $b_blue$chan$ob");
344
345     if (&validChan($chan)) {
346         &status("join: already on $chan");
347     } else {
348         if (!$conn->join($chan)) {
349             &DEBUG("join failed. trying connect!");
350             $conn->connect();
351         }
352     }
353 }
354
355 sub part {
356     my $chan;
357
358     foreach $chan (@_) {
359         next if ($chan eq "");
360         $chan =~ tr/A-Z/a-z/;   # lowercase.
361
362         &status("parting $chan");
363         if (!&validChan($chan)) {
364             &status("part: not on $chan");
365             next;
366         }
367
368         rawout("PART $chan");
369         # deletion of $channels{chan} is done in &entryEvt().
370     }
371 }
372
373 sub mode {
374     my ($chan, @modes) = @_;
375     my $modes = join(" ", @modes);
376
377     if (&validChan($chan) == 0) {
378         &ERROR("mode: invalid chan => '$chan'.");
379         return;
380     }
381
382     &DEBUG("MODE $chan $modes");
383
384     rawout("MODE $chan $modes");
385 }
386
387 sub op {
388     my ($chan, @who) = @_;
389     my $os      = "o" x scalar(@who);
390
391     &mode($chan, "+$os @who");
392 }
393
394 sub deop {
395     my ($chan, @who) = @_;
396     my $os = "o" x scalar(@who);
397
398     &mode($chan, "-$os ".@who);
399 }
400
401 sub kick {
402     my ($nick,$chan,$msg) = @_;
403     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
404
405     if ($chan ne "" and &validChan($chan) == 0) {
406         &ERROR("kick: invalid channel $chan.");
407         return;
408     }
409
410     $nick =~ tr/A-Z/a-z/;
411
412     foreach $chan (@chans) {
413         if (!&IsNickInChan($nick,$chan)) {
414             &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
415             next;
416         }
417
418         if (!exists $channels{$chan}{o}{$ident}) {
419             &status("Kick: do not have ops on $chan :(");
420             next;
421         }
422
423         &status("Kicking $nick from $chan.");
424         if ($msg eq "") {
425             &rawout("KICK $chan $nick");
426         } else {
427             &rawout("KICK $chan $nick :$msg");
428         }
429     }
430 }
431
432 sub ban {
433     my ($mask,$chan) = @_;
434     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
435     my $ban     = 0;
436
437     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
438         &ERROR("ban: invalid channel $chan.");
439         return;
440     }
441
442     foreach $chan (@chans) {
443         if (!exists $channels{$chan}{o}{$ident}) {
444             &status("Ban: do not have ops on $chan :(");
445             next;
446         }
447
448         &status("Banning $mask from $chan.");
449         &rawout("MODE $chan +b $mask");
450         $ban++;
451     }
452
453     return $ban;
454 }
455
456 sub unban {
457     my ($mask,$chan) = @_;
458     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
459     my $ban     = 0;
460
461     &DEBUG("unban: mask = $mask, chan = @chans");
462
463     foreach $chan (@chans) {
464         if (!exists $channels{$chan}{o}{$ident}) {
465             &status("unBan: do not have ops on $chan :(");
466             next;
467         }
468
469         &status("Removed ban $mask from $chan.");
470         &rawout("MODE $chan -b $mask");
471         $ban++;
472     }
473
474     return $ban;
475 }
476
477 sub quit {
478     my ($quitmsg) = @_;
479     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
480     $conn->quit($quitmsg);
481 }
482
483 sub nick {
484     my ($nick) = @_;
485
486     if ($nick =~ /^$mask{nick}$/) {
487         rawout("NICK ".$nick);
488         return 1;
489     }
490
491     return 0;
492 }
493
494 sub invite {
495     my($who, $chan) = @_;
496     rawout("INVITE $who $chan");
497 }
498
499
500 ##########
501 # Channel related functions...
502 #
503
504 # Usage: &joinNextChan();
505 sub joinNextChan {
506     if (scalar @joinchan) {
507         my $chan = shift @joinchan;
508         &joinchan($chan);
509
510         if (my $i = scalar @joinchan) {
511             &status("joinNextChan: $i chans to join.");
512         }
513         return;
514     }
515
516     if (&IsParam("nickServ_pass") and $nickserv < 1) {
517         &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
518         $nickserv--;
519     }
520
521     my %chan = &getChanConfList("chanServ");
522     foreach $chan (keys %chan) {
523         next unless ($chan{$chan} > 0);
524             
525         if (!exists $channels{$chan}{'o'}{$ident}) {
526             &status("ChanServ ==> Requesting ops for $chan.");
527             &rawout("PRIVMSG ChanServ :OP $chan $ident");
528         }
529     }
530 }
531
532 # Usage: &GetNickInChans($nick,$chan);
533 sub GetNickInChans {
534     my ($nick) = @_;
535     my @array;
536
537     foreach (keys %channels) {
538         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
539         push(@array, $_);
540     }
541
542     return @array;
543 }
544
545 # Usage: &GetNicksInChan($chan);
546 sub GetNicksInChan {
547     my ($chan) = @_;
548     my @array;
549
550     return keys %{ $channels{$chan}{''} };
551 }
552
553 sub IsNickInChan {
554     my ($nick,$chan) = @_;
555
556     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
557
558     if (&validChan($chan) == 0) {
559         &ERROR("INIC: invalid channel $chan.");
560         return 0;
561     }
562
563     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
564         return 1;
565     } else {
566         foreach (keys %channels) {
567             next unless (/[A-Z]/);
568             &DEBUG("hash channels contains mixed cased chan!!!");
569         }
570         return 0;
571     }
572 }
573
574 sub IsNickInAnyChan {
575     my ($nick) = @_;
576
577     foreach $chan (keys %channels) {
578         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
579         return 1;
580     }
581     return 0;
582 }
583
584 # Usage: &validChan($chan);
585 sub validChan {
586     my ($chan) = @_;
587
588     if (lc $chan ne $chan) {
589         &WARN("validChan: lc chan != chan. ($chan); fixing.");
590         $chan =~ tr/A-Z/a-z/;
591     }
592
593     if (exists $channels{$chan}) {
594         return 1;
595     } else {
596         return 0;
597     }
598 }
599
600 ###
601 # Usage: &DeleteUserInfo($nick,@chans);
602 sub DeleteUserInfo {
603     my ($nick,@chans) = @_;
604     my ($mode,$chan);
605
606     foreach $chan (@chans) {
607         foreach $mode (keys %{$channels{$chan}}) {
608             # use grep here?
609             next unless (exists $channels{$chan}{$mode}{$nick});
610
611             delete $channels{$chan}{$mode}{$nick};
612         }
613     }
614 }
615
616 sub clearChanVars {
617     my ($chan) = @_;
618
619     delete $channels{$chan};
620 }
621
622 sub clearIRCVars {
623     &DEBUG("clearIRCVars() called!");
624     undef %channels;
625     undef %floodjoin;
626
627     @joinchan   = &getJoinChans();
628 }
629
630 sub getJoinChans {
631     my @chans;
632     my @skip;
633
634     foreach (keys %chanconf) {
635         next if ($_ eq "_default");
636
637         my $val = $chanconf{$_}{autojoin};
638         my $skip = 0;
639
640         if (defined $val) {
641             $skip++ if ($val eq "0");
642         } else {
643             $skip++;
644         }
645
646         if ($skip) {
647             push(@skip, $_);
648             next;
649         }
650
651         push(@chans, $_);
652     }
653
654     if (scalar @skip) {
655         &status("gJC: channels not auto-joining: @skip");
656     } else {
657         &status("gJC: auto-joining all chans.");
658     }
659
660     return @chans;
661 }
662
663 sub closeDCC {
664     &DEBUG("closeDCC called.");
665
666     foreach $type (keys %dcc) {
667         next if ($type ne uc($type));
668  
669         foreach $nick (keys %{$dcc{$type}}) {
670             next unless (defined $nick);
671             &DEBUG("closing DCC $type to $nick (FIXME).");
672             next unless (defined $dcc{$type}{$nick});
673
674             my $ref = $dcc{$type}{$nick};
675             &DEBUG("ref => $ref");
676
677 #           $dcc{$type}{$nick}->close();
678         }
679     }
680 }
681
682 sub joinfloodCheck {
683     my($who,$chan,$userhost) = @_;
684
685     return unless (&IsChanConf("joinfloodCheck"));
686
687     if (exists $netsplit{lc $who}) {    # netsplit join.
688         &DEBUG("jfC: $who was in netsplit; not checking.");
689     }
690
691     if (exists $floodjoin{$chan}{$who}{Time}) {
692         &WARN("floodjoin{$chan}{$who} already exists?");
693     }
694
695     $floodjoin{$chan}{$who}{Time} = time();
696     $floodjoin{$chan}{$who}{Host} = $userhost;
697
698     ### Check...
699     foreach (keys %floodjoin) {
700         my $c = $_;
701         my $count = scalar keys %{ $floodjoin{$c} };
702         next unless ($count > 5);
703         &DEBUG("count => $count");
704
705         my $time;
706         foreach (keys %{ $floodjoin{$c} }) {
707             $time += $floodjoin{$c}{$_}{Time};
708         }
709         &DEBUG("time => $time");
710         $time /= $count;
711
712         &DEBUG("new time => $time");
713     }
714
715     ### Clean it up.
716     my $delete = 0;
717     foreach $chan (keys %floodjoin) {
718         foreach $who (keys %{ $floodjoin{$chan} }) {
719             my $time = time() - $floodjoin{$chan}{$who}{Time};
720             next unless ($time > 10);
721             delete $floodjoin{$chan}{$who};
722             $delete++;
723         }
724     }
725
726     &DEBUG("jfC: $delete deleted.") if ($delete);
727 }
728
729 sub getHostMask {
730     my($n) = @_;
731
732     &FIXME("getHostMask...");
733 }
734
735 1;