]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
IRC/IrcHooks.pl
[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         &DEBUG("Changing nick to $nick (from $ident)");
488         rawout("NICK ".$nick);
489         return 1;
490     }
491     &DEBUG("nick failed... why oh why (nick => $nick)");
492
493     return 0;
494 }
495
496 sub invite {
497     my($who, $chan) = @_;
498     rawout("INVITE $who $chan");
499 }
500
501
502 ##########
503 # Channel related functions...
504 #
505
506 # Usage: &joinNextChan();
507 sub joinNextChan {
508     if (scalar @joinchan) {
509         my $chan = shift @joinchan;
510         &joinchan($chan);
511
512         if (my $i = scalar @joinchan) {
513             &status("joinNextChan: $i chans to join.");
514         }
515         return;
516     }
517
518     if (&IsParam("nickServ_pass") and $nickserv < 1) {
519         &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
520         $nickserv--;
521     }
522
523     my %chan = &getChanConfList("chanServ");
524     foreach $chan (keys %chan) {
525         next unless ($chan{$chan} > 0);
526             
527         if (!exists $channels{$chan}{'o'}{$ident}) {
528             &status("ChanServ ==> Requesting ops for $chan.");
529             &rawout("PRIVMSG ChanServ :OP $chan $ident");
530         }
531     }
532 }
533
534 # Usage: &GetNickInChans($nick,$chan);
535 sub GetNickInChans {
536     my ($nick) = @_;
537     my @array;
538
539     foreach (keys %channels) {
540         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
541         push(@array, $_);
542     }
543
544     return @array;
545 }
546
547 # Usage: &GetNicksInChan($chan);
548 sub GetNicksInChan {
549     my ($chan) = @_;
550     my @array;
551
552     return keys %{ $channels{$chan}{''} };
553 }
554
555 sub IsNickInChan {
556     my ($nick,$chan) = @_;
557
558     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
559
560     if (&validChan($chan) == 0) {
561         &ERROR("INIC: invalid channel $chan.");
562         return 0;
563     }
564
565     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
566         return 1;
567     } else {
568         foreach (keys %channels) {
569             next unless (/[A-Z]/);
570             &DEBUG("hash channels contains mixed cased chan!!!");
571         }
572         return 0;
573     }
574 }
575
576 sub IsNickInAnyChan {
577     my ($nick) = @_;
578
579     foreach $chan (keys %channels) {
580         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
581         return 1;
582     }
583     return 0;
584 }
585
586 # Usage: &validChan($chan);
587 sub validChan {
588     my ($chan) = @_;
589
590     if (lc $chan ne $chan) {
591         &WARN("validChan: lc chan != chan. ($chan); fixing.");
592         $chan =~ tr/A-Z/a-z/;
593     }
594
595     if (exists $channels{$chan}) {
596         return 1;
597     } else {
598         return 0;
599     }
600 }
601
602 ###
603 # Usage: &DeleteUserInfo($nick,@chans);
604 sub DeleteUserInfo {
605     my ($nick,@chans) = @_;
606     my ($mode,$chan);
607
608     foreach $chan (@chans) {
609         foreach $mode (keys %{$channels{$chan}}) {
610             # use grep here?
611             next unless (exists $channels{$chan}{$mode}{$nick});
612
613             delete $channels{$chan}{$mode}{$nick};
614         }
615     }
616 }
617
618 sub clearChanVars {
619     my ($chan) = @_;
620
621     delete $channels{$chan};
622 }
623
624 sub clearIRCVars {
625     &DEBUG("clearIRCVars() called!");
626     undef %channels;
627     undef %floodjoin;
628
629     @joinchan   = &getJoinChans();
630 }
631
632 sub getJoinChans {
633     my @chans;
634     my @skip;
635
636     foreach (keys %chanconf) {
637         next if ($_ eq "_default");
638
639         my $val = $chanconf{$_}{autojoin};
640         my $skip = 0;
641
642         if (defined $val) {
643             $skip++ if ($val eq "0");
644         } else {
645             $skip++;
646         }
647
648         if ($skip) {
649             push(@skip, $_);
650             next;
651         }
652
653         push(@chans, $_);
654     }
655
656     if (scalar @skip) {
657         &status("gJC: channels not auto-joining: @skip");
658     } else {
659         &status("gJC: auto-joining all chans.");
660     }
661
662     return @chans;
663 }
664
665 sub closeDCC {
666     &DEBUG("closeDCC called.");
667
668     foreach $type (keys %dcc) {
669         next if ($type ne uc($type));
670  
671         foreach $nick (keys %{$dcc{$type}}) {
672             next unless (defined $nick);
673             &DEBUG("closing DCC $type to $nick (FIXME).");
674             next unless (defined $dcc{$type}{$nick});
675
676             my $ref = $dcc{$type}{$nick};
677             &DEBUG("ref => $ref");
678
679 #           $dcc{$type}{$nick}->close();
680         }
681     }
682 }
683
684 sub joinfloodCheck {
685     my($who,$chan,$userhost) = @_;
686
687     return unless (&IsChanConf("joinfloodCheck"));
688
689     if (exists $netsplit{lc $who}) {    # netsplit join.
690         &DEBUG("jfC: $who was in netsplit; not checking.");
691     }
692
693     if (exists $floodjoin{$chan}{$who}{Time}) {
694         &WARN("floodjoin{$chan}{$who} already exists?");
695     }
696
697     $floodjoin{$chan}{$who}{Time} = time();
698     $floodjoin{$chan}{$who}{Host} = $userhost;
699
700     ### Check...
701     foreach (keys %floodjoin) {
702         my $c = $_;
703         my $count = scalar keys %{ $floodjoin{$c} };
704         next unless ($count > 5);
705         &DEBUG("count => $count");
706
707         my $time;
708         foreach (keys %{ $floodjoin{$c} }) {
709             $time += $floodjoin{$c}{$_}{Time};
710         }
711         &DEBUG("time => $time");
712         $time /= $count;
713
714         &DEBUG("new time => $time");
715     }
716
717     ### Clean it up.
718     my $delete = 0;
719     foreach $chan (keys %floodjoin) {
720         foreach $who (keys %{ $floodjoin{$chan} }) {
721             my $time = time() - $floodjoin{$chan}{$who}{Time};
722             next unless ($time > 10);
723             delete $floodjoin{$chan}{$who};
724             $delete++;
725         }
726     }
727
728     &DEBUG("jfC: $delete deleted.") if ($delete);
729 }
730
731 sub getHostMask {
732     my($n) = @_;
733
734     &FIXME("getHostMask...");
735 }
736
737 1;