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