]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
changed email address
[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             $error++;
26             sleep 1;
27             next;
28         }
29
30         foreach (@ircServers) {
31             if (!defined $_) {
32                 &DEBUG("ircloop: ircServers[x] = NULL.");
33                 $lastrun = time();
34                 next;
35             }
36             &DEBUG("ircloop: _ => '$_'.");
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     sleep 3;            # lame hack.
65
66     # host->ip.
67     if ($server =~ /\D$/) {
68         my $packed = scalar(gethostbyname($server));
69
70         if (!defined $packed) {
71             &status("  cannot resolve $server.");
72             return 0;
73         }
74
75         my $resolve = inet_ntoa($packed);
76         &status("  resolved to $resolve.");
77     }
78
79     $irc = new Net::IRC;
80
81     $conn = $irc->newconn(
82                 Nick    => $param{'ircNick'},
83                 Server  => $server,
84                 Port    => $port,
85                 Ircname => $param{'ircName'},
86                 LocalAddr => $param{'ircHost'},
87     );
88
89     if (!defined $conn) {
90         &ERROR("irc: conn was not created!defined!!!");
91         return 1;
92     }
93
94     # clear out hashes before connecting...
95     &clearIRCVars();
96
97     $ident                      = $param{'ircNick'};
98     ### IRCSTATS.
99     $ircstats{'ConnectTime'}    = time();
100     $ircstats{'ConnectCount'}++;
101     $ircstats{'Server'}         = "$server:$port";
102
103     # handler stuff.
104         $conn->add_handler('caction',   \&on_action);
105         $conn->add_handler('cdcc',      \&on_dcc);
106         $conn->add_handler('cping',     \&on_ping);
107         $conn->add_handler('crping',    \&on_ping_reply);
108         $conn->add_handler('cversion',  \&on_version);
109
110         $conn->add_handler('dcc_open',  \&on_dcc_open);
111         $conn->add_handler('dcc_close', \&on_dcc_close);
112         $conn->add_handler('chat',      \&on_chat);
113         $conn->add_handler('msg',       \&on_msg);
114         $conn->add_handler('public',    \&on_public);
115         $conn->add_handler('join',      \&on_join);
116         $conn->add_handler('part',      \&on_part);
117         $conn->add_handler('topic',     \&on_topic);
118         $conn->add_handler('invite',    \&on_invite);
119         $conn->add_handler('kick',      \&on_kick);
120         $conn->add_handler('mode',      \&on_mode);
121         $conn->add_handler('nick',      \&on_nick);
122         $conn->add_handler('quit',      \&on_quit);
123         $conn->add_handler('notice',    \&on_notice);
124         $conn->add_handler('whoisuser', \&on_whoisuser);
125         $conn->add_handler('other',     \&on_other);
126         $conn->add_global_handler('disconnect', \&on_disconnect);
127         $conn->add_global_handler([251,252,253,254,255], \&on_init);
128 ###     $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
129         $conn->add_global_handler(324, \&on_modeis);
130         $conn->add_global_handler(333, \&on_topicinfo);
131         $conn->add_global_handler(352, \&on_who);
132         $conn->add_global_handler(353, \&on_names);
133         $conn->add_global_handler(366, \&on_endofnames);
134         $conn->add_global_handler(376, \&on_endofmotd);
135         $conn->add_global_handler(433, \&on_nick_taken);
136         $conn->add_global_handler(439, \&on_targettoofast);
137     # end of handler stuff.
138
139     $irc->start;
140 }
141
142 ######################################################################
143 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
144 ######################################################################
145
146 sub rawout {
147     my ($buf) = @_;
148     $buf =~ s/\n//gi;
149
150     # slow down a bit if traffic is "high".
151     # need to take into account time of last message sent.
152     if ($last{buflen} > 256 and length($buf) > 256) {
153         sleep 1;
154     }
155
156     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
157
158     $last{buflen} = length($buf);
159 }
160
161 sub say {
162     my ($msg) = @_;
163     if (!defined $msg or $msg eq "NOREPLY") {
164         $msg ||= "NULL";
165         &DEBUG("say: msg == $msg.");
166         return;
167     }
168
169     if ($msg eq $last{say} and length($msg) > 256) {
170         &status("say: detected repeated message; skipping.");
171         return;
172     }
173     $last{say} = $msg;
174
175     &status("</$talkchannel> $msg");
176     if (&whatInterface() =~ /IRC/) {
177         $msg = "zero" if ($msg =~ /^0+$/);
178
179         $conn->privmsg($talkchannel, $msg);
180     }
181 }
182
183 sub msg {
184     my ($nick, $msg) = @_;
185     if (!defined $nick) {
186         &ERROR("msg: nick == NULL.");
187         return;
188     }
189
190     if (!defined $msg or $msg eq "NOREPLY") {
191         $msg ||= "NULL";
192         &DEBUG("msg: msg == $msg.");
193         return;
194     }
195
196     if ($msg eq $last{msg} and length($msg) > 256) {
197         &status("msg: detected repeated message; skipping.");
198         return;
199     }
200     $last{msg} = $msg;
201
202     &status(">$nick< $msg");
203     $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
204 }
205
206 # Usage: &action(nick || chan, txt);
207 sub action {
208     my ($target, $txt) = @_;
209     if (!defined $txt) {
210         &DEBUG("action: txt == NULL.");
211         return;
212     }
213
214     my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
215     if (length $rawout > 510) {
216         &status("action: txt too long; truncating.");
217
218         chop($rawout) while (length($rawout) > 510);
219         $rawout .= "\001";
220     }
221
222     &status("* $ident/$target $txt");
223     rawout($rawout);
224 }
225
226 # Usage: &action(nick || chan, txt);
227 sub notice{
228     my ($target, $txt) = @_;
229     if (!defined $txt) {
230         &DEBUG("action: txt == NULL.");
231         return;
232     }
233
234     &status("-$target- $txt");
235
236     $conn->notice($target, $txt);
237 }
238
239
240 sub DCCBroadcast {
241     my ($txt) = @_;
242
243     foreach (keys %{$dcc{'CHAT'}}) {
244         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
245     }
246 }
247
248 ##########
249 ### perform commands.
250 ###
251
252 # Usage: &performReply($reply);
253 sub performReply {
254     my ($reply) = @_;
255     $reply =~ /([\.\?\s]+)$/;
256
257     &checkMsgType($reply);
258
259     if ($msgType eq 'public') {
260         if (rand() < 0.5) {
261             $reply = "$orig{who}: ".$reply;
262         } else {
263             $reply = "$reply, ".$orig{who};
264         }
265         &say($reply);
266     } elsif ($msgType eq 'private') {
267         if (rand() < 0.5) {
268             $reply = $reply;
269         } else {
270             $reply = "$reply, ".$orig{who};
271         }
272         &msg($who, $reply);
273     } elsif ($msgType eq 'chat') {
274         &DEBUG("pR: chat: reply => '$reply'.");
275         &DEBUG("pR: chat: sock => '$dcc{'CHAT'}{$nick}'.");
276         &DEBUG("pR: chat: sock => '$dcc{'CHAT'}{$who}'.");
277     } else {
278         &ERROR("PR: msgType invalid? ($msgType).");
279     }
280 }
281
282 # ...
283 sub performAddressedReply {
284     return unless ($addressed);
285     &performReply(@_);
286 }
287
288 # Usage: &performStrictReply($reply);
289 sub performStrictReply {
290     my ($reply) = @_;
291
292     &checkMsgType($reply);
293
294     if ($msgType eq 'private') {
295         &msg($who, $reply);
296     } elsif ($msgType eq 'public') {
297         &say($reply);
298     } elsif ($msgType eq 'chat') {
299         if (!exists $dcc{'CHAT'}{$who}) {
300             &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
301             return;
302         }
303         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
304     } else {
305         &ERROR("pSR: msgType invalid? ($msgType).");
306     }
307
308     return '';
309 }
310
311 sub joinchan {
312     my ($chankey) = @_;
313     my $chan = lc $chankey;
314
315     if ($chankey =~ s/^($mask{chan}),\S+/ /) {
316         $chan = lc $1;
317     }
318
319     &status("joining $b_blue$chan$ob");
320
321     if (&validChan($chan)) {
322         &status("join: already on $chan");
323     } else {
324         $conn->join($chan);
325     }
326 }
327
328 sub part {
329     my $chan;
330
331     foreach $chan (@_) {
332         next if ($chan eq "");
333         $chan =~ tr/A-Z/a-z/;   # lowercase.
334
335         &status("parting $chan");
336         if (!&validChan($chan)) {
337             &status("part: not on $chan");
338             next;
339         }
340
341         rawout("PART $chan");
342         # deletion of $channels{chan} is done in &entryEvt().
343     }
344 }
345
346 sub mode {
347     my ($chan, @modes) = @_;
348     my $modes = join(" ", @modes);
349
350     if (&validChan($chan) == 0) {
351         &ERROR("mode: invalid chan => '$chan'.");
352         return;
353     }
354
355     rawout("MODE $chan $modes");
356 }
357
358 sub op {
359     my ($chan, @who) = @_;
360     my $os      = "o" x scalar(@who);
361
362     &mode($chan, "+$os ".@who);
363 }
364
365 sub deop {
366     my ($chan, @who) = @_;
367     my $os = "o" x scalar(@who);
368
369     &mode($chan, "-$os ".@who);
370 }
371
372 sub kick {
373     my ($nick,$chan,$msg) = @_;
374     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
375
376     if ($chan ne "" and &validChan($chan) == 0) {
377         &ERROR("kick: invalid channel $chan.");
378         return;
379     }
380
381     $nick =~ tr/A-Z/a-z/;
382
383     foreach $chan (@chans) {
384         if (!&IsNickInChan($nick,$chan)) {
385             &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
386             next;
387         }
388
389         if (!exists $channels{$chan}{o}{$ident}) {
390             &status("Kick: do not have ops on $chan :(");
391             next;
392         }
393
394         &status("Kicking $nick from $chan.");
395         if ($msg eq "") {
396             &rawout("KICK $chan $nick");
397         } else {
398             &rawout("KICK $chan $nick :$msg");
399         }
400     }
401 }
402
403 sub ban {
404     my ($mask,$chan) = @_;
405     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
406
407     if ($chan ne "" and &validChan($chan) == 0) {
408         &ERROR("ban: invalid channel $chan.");
409         return;
410     }
411
412     $nick =~ tr/A-Z/a-z/;
413
414     foreach $chan (@chans) {
415         if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
416             &status("Ban: $nick is not on $chan.");
417             next;
418         }
419
420         if (!exists $channels{$chan}{o}{$ident}) {
421             &status("Ban: do not have ops on $chan :(");
422             next;
423         }
424
425         &status("Banning $mask from $chan.");
426         &rawout("MODE $chan +b $mask");
427     }
428 }
429
430 sub quit {
431     my ($quitmsg) = @_;
432     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
433     $conn->quit($quitmsg);
434 }
435
436 sub nick {
437     my ($nick) = @_;
438
439     if ($nick =~ /^$mask{nick}$/) {
440         rawout("NICK ".$nick);
441         return 1;
442     }
443
444     return 0;
445 }
446
447 sub invite {
448     my($who, $chan) = @_;
449     rawout("INVITE $who $chan");
450 }
451
452
453 ##########
454 # Channel related functions...
455 #
456
457 # Usage: &joinNextChan();
458 sub joinNextChan {
459     if (scalar @joinchan) {
460         my $chan = shift @joinchan;
461         &joinchan($chan);
462
463         if (my $i = scalar @joinchan) {
464             &status("joinNextChan: $i chans to join.");
465         }
466     }
467 }
468
469 # Usage: &GetNickInChans($nick,$chan);
470 sub GetNickInChans {
471     my ($nick) = @_;
472     my @array;
473
474     foreach (keys %channels) {
475         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
476         push(@array, $_);
477     }
478
479     return @array;
480 }
481
482 sub IsNickInChan {
483     my ($nick,$chan) = @_;
484
485     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
486
487     if (&validChan($chan) == 0) {
488         &ERROR("INIC: invalid channel $chan.");
489         return 0;
490     }
491
492     if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
493         return 1;
494     } else {
495         return 0;
496     }
497 }
498
499 sub IsNickInAnyChan {
500     my ($nick) = @_;
501
502     foreach $chan (keys %channels) {
503         next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
504         return 1;
505     }
506     return 0;
507 }
508
509 # Usage: &validChan($chan);
510 sub validChan {
511     my ($chan) = @_;
512
513     if (lc $chan ne $chan) {
514         &WARN("validChan: lc chan != chan. ($chan); fixing.");
515         $chan =~ tr/A-Z/a-z/;
516     }
517
518     if (exists $channels{$chan}) {
519         return 1;
520     } else {
521         return 0;
522     }
523 }
524
525 ###
526 # Usage: &DeleteUserInfo($nick,@chans);
527 sub DeleteUserInfo {
528     my ($nick,@chans) = @_;
529     my ($mode,$chan);
530
531     foreach $chan (@chans) {
532         foreach $mode (keys %{$channels{$chan}}) {
533             # use grep here?
534             next unless (exists $channels{$chan}{$mode}{$nick});
535
536             delete $channels{$chan}{$mode}{$nick};
537         }
538     }
539 }
540
541 sub clearChanVars {
542     my ($chan) = @_;
543
544     delete $channels{$chan};
545 }
546
547 sub clearIRCVars {
548     &DEBUG("clearIRCVars() called!");
549     %channels = ();
550     @joinchan = split /[\t\s]+/, $param{'join_channels'};
551 }
552
553 sub makeChanList {
554     my ($str)   = @_;
555     my $inverse = 0;
556     my @chans;
557
558     if ($str eq "ALL") {
559         return(keys %channels);
560     } elsif ($str =~ s/^ALL but //i) {
561         @chans = keys %channels;
562         foreach (split /[\s\t\,]+/, lc $str) {
563             @chans = grep !/^$_$/, @chans;
564         }
565     } else {
566         foreach (split /[\s\t\,]+/, lc $str) {
567             next unless (&validChan($_));
568             push(@chans, $_);
569         }
570     }
571
572     @chans;
573 }
574
575 sub closeDCC {
576     foreach $type (keys %dcc) {
577         foreach (keys %{$dcc{$type}}) {
578             &DEBUG("closing DCC $type to $_ (FIXME).");
579 ###         $irc->removeconn($dcc{$type}{$_});
580         }
581     }
582 }
583
584 1;