2 # Irc.pl: IRC core stuff.
5 # NOTE: Based on code by Kevin Lenzo & Patrick Cole (c) 1997
8 if (&IsParam("useStrict")) { use strict; }
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]*";
23 # JUST IN CASE. irq was complaining about this.
24 if ($lastrun == time()) {
30 foreach (@ircServers) {
32 &DEBUG("ircloop: ircServers[x] = NULL.");
36 &DEBUG("ircloop: _ => '$_'.");
37 next unless (exists $ircPort{$_});
39 my $retval = &irc($_, $ircPort{$_});
40 next unless (defined $retval and $retval == 0);
42 if ($error % 3 == 0 and $error != 0) {
43 &ERROR("CANNOT connect to this server; next!");
48 &ERROR("CANNOT connect to any irc server; stopping.");
56 my ($server,$port) = @_;
58 my $iaddr = inet_aton($server);
59 my $paddr = sockaddr_in($port, $iaddr);
60 my $proto = getprotobyname('tcp');
63 &status("Connecting to port $port of server $server ...");
67 if ($server =~ /\D$/) {
68 my $packed = scalar(gethostbyname($server));
70 if (!defined $packed) {
71 &status(" cannot resolve $server.");
75 my $resolve = inet_ntoa($packed);
76 &status(" resolved to $resolve.");
81 $conn = $irc->newconn(
82 Nick => $param{'ircNick'},
85 Ircname => $param{'ircName'},
86 LocalAddr => $param{'ircHost'},
90 &ERROR("irc: conn was not created!defined!!!");
94 # change internal timeout value for scheduler.
95 $irc->{_timeout} = 10; # how about 60?
97 # clear out hashes before connecting...
100 $ident = $param{'ircNick'};
102 $ircstats{'ConnectTime'} = time();
103 $ircstats{'ConnectCount'}++;
104 $ircstats{'Server'} = "$server:$port";
107 $conn->add_handler('caction', \&on_action);
108 $conn->add_handler('cdcc', \&on_dcc);
109 $conn->add_handler('cping', \&on_ping);
110 $conn->add_handler('crping', \&on_ping_reply);
111 $conn->add_handler('cversion', \&on_version);
113 $conn->add_handler('dcc_open', \&on_dcc_open);
114 $conn->add_handler('dcc_close', \&on_dcc_close);
115 $conn->add_handler('chat', \&on_chat);
116 $conn->add_handler('msg', \&on_msg);
117 $conn->add_handler('public', \&on_public);
118 $conn->add_handler('join', \&on_join);
119 $conn->add_handler('part', \&on_part);
120 $conn->add_handler('topic', \&on_topic);
121 $conn->add_handler('invite', \&on_invite);
122 $conn->add_handler('kick', \&on_kick);
123 $conn->add_handler('mode', \&on_mode);
124 $conn->add_handler('nick', \&on_nick);
125 $conn->add_handler('quit', \&on_quit);
126 $conn->add_handler('notice', \&on_notice);
127 $conn->add_handler('whoisuser', \&on_whoisuser);
128 $conn->add_handler('other', \&on_other);
129 $conn->add_global_handler('disconnect', \&on_disconnect);
130 $conn->add_global_handler([251,252,253,254,255], \&on_init);
131 ### $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
132 $conn->add_global_handler(324, \&on_modeis);
133 $conn->add_global_handler(333, \&on_topicinfo);
134 $conn->add_global_handler(352, \&on_who);
135 $conn->add_global_handler(353, \&on_names);
136 $conn->add_global_handler(366, \&on_endofnames);
137 $conn->add_global_handler(376, \&on_endofmotd);
138 $conn->add_global_handler(433, \&on_nick_taken);
139 $conn->add_global_handler(439, \&on_targettoofast);
140 # end of handler stuff.
145 ######################################################################
146 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
147 ######################################################################
153 # slow down a bit if traffic is "high".
154 # need to take into account time of last message sent.
155 if ($last{buflen} > 256 and length($buf) > 256) {
159 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
161 $last{buflen} = length($buf);
166 if (!defined $msg or $msg eq $noreply) {
168 &DEBUG("say: msg == $msg.");
172 if ($msg eq $last{say} and length($msg) > 256) {
173 &status("say: detected repeated message; skipping.");
178 &status("</$talkchannel> $msg");
179 if (&whatInterface() =~ /IRC/) {
180 $msg = "zero" if ($msg =~ /^0+$/);
182 $conn->privmsg($talkchannel, $msg);
187 my ($nick, $msg) = @_;
188 if (!defined $nick) {
189 &ERROR("msg: nick == NULL.");
193 if (!defined $msg or $msg eq $noreply) {
195 &DEBUG("msg: msg == $msg.");
199 if ($msg eq $last{msg} and length($msg) > 256) {
200 &status("msg: detected repeated message; skipping.");
205 &status(">$nick< $msg");
206 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
209 # Usage: &action(nick || chan, txt);
211 my ($target, $txt) = @_;
213 &DEBUG("action: txt == NULL.");
217 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
218 if (length $rawout > 510) {
219 &status("action: txt too long; truncating.");
221 chop($rawout) while (length($rawout) > 510);
225 &status("* $ident/$target $txt");
229 # Usage: &action(nick || chan, txt);
231 my ($target, $txt) = @_;
233 &DEBUG("action: txt == NULL.");
237 &status("-$target- $txt");
239 $conn->notice($target, $txt);
246 foreach (keys %{$dcc{'CHAT'}}) {
247 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
252 ### perform commands.
255 # Usage: &performReply($reply);
258 $reply =~ /([\.\?\s]+)$/;
260 &checkMsgType($reply);
262 if ($msgType eq 'public') {
263 if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
264 $reply = "$orig{who}: ".$reply;
266 $reply = "$reply, ".$orig{who};
269 } elsif ($msgType eq 'private') {
273 $reply = "$reply, ".$orig{who};
276 } elsif ($msgType eq 'chat') {
277 if (!exists $dcc{'CHAT'}{$who}) {
278 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
281 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
283 &ERROR("PR: msgType invalid? ($msgType).");
288 sub performAddressedReply {
289 return unless ($addressed);
293 # Usage: &performStrictReply($reply);
294 sub performStrictReply {
297 &checkMsgType($reply);
299 if ($msgType eq 'private') {
301 } elsif ($msgType eq 'public') {
303 } elsif ($msgType eq 'chat') {
304 if (!exists $dcc{'CHAT'}{$who}) {
305 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
308 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
310 &ERROR("pSR: msgType invalid? ($msgType).");
318 my $chan = lc $chankey;
320 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
324 &status("joining $b_blue$chan$ob");
326 if (&validChan($chan)) {
327 &status("join: already on $chan");
337 next if ($chan eq "");
338 $chan =~ tr/A-Z/a-z/; # lowercase.
340 &status("parting $chan");
341 if (!&validChan($chan)) {
342 &status("part: not on $chan");
346 rawout("PART $chan");
347 # deletion of $channels{chan} is done in &entryEvt().
352 my ($chan, @modes) = @_;
353 my $modes = join(" ", @modes);
355 if (&validChan($chan) == 0) {
356 &ERROR("mode: invalid chan => '$chan'.");
360 &DEBUG("MODE $chan $modes");
362 rawout("MODE $chan $modes");
366 my ($chan, @who) = @_;
367 my $os = "o" x scalar(@who);
369 &mode($chan, "+$os ".@who);
373 my ($chan, @who) = @_;
374 my $os = "o" x scalar(@who);
376 &mode($chan, "-$os ".@who);
380 my ($nick,$chan,$msg) = @_;
381 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
383 if ($chan ne "" and &validChan($chan) == 0) {
384 &ERROR("kick: invalid channel $chan.");
388 $nick =~ tr/A-Z/a-z/;
390 foreach $chan (@chans) {
391 if (!&IsNickInChan($nick,$chan)) {
392 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
396 if (!exists $channels{$chan}{o}{$ident}) {
397 &status("Kick: do not have ops on $chan :(");
401 &status("Kicking $nick from $chan.");
403 &rawout("KICK $chan $nick");
405 &rawout("KICK $chan $nick :$msg");
411 my ($mask,$chan) = @_;
412 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
414 if ($chan ne "" and &validChan($chan) == 0) {
415 &ERROR("ban: invalid channel $chan.");
419 $nick =~ tr/A-Z/a-z/;
421 foreach $chan (@chans) {
422 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
423 &status("Ban: $nick is not on $chan.");
427 if (!exists $channels{$chan}{o}{$ident}) {
428 &status("Ban: do not have ops on $chan :(");
432 &status("Banning $mask from $chan.");
433 &rawout("MODE $chan +b $mask");
439 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
440 $conn->quit($quitmsg);
446 if ($nick =~ /^$mask{nick}$/) {
447 rawout("NICK ".$nick);
455 my($who, $chan) = @_;
456 rawout("INVITE $who $chan");
461 # Channel related functions...
464 # Usage: &joinNextChan();
466 if (scalar @joinchan) {
467 my $chan = shift @joinchan;
470 if (my $i = scalar @joinchan) {
471 &status("joinNextChan: $i chans to join.");
474 return unless (&IsParam("chanServ_ops"));
476 &DEBUG("jNC: nickserv/chanserv not up?");
479 my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
480 foreach $chan (keys %channels) {
481 next unless (grep /^$chan$/i, @chans);
483 if (!exists $channels{$chan}{'o'}{$ident}) {
484 &status("ChanServ ==> Requesting ops for $chan.");
485 &rawout("PRIVMSG ChanServ :OP $chan $ident");
491 # Usage: &GetNickInChans($nick,$chan);
496 foreach (keys %channels) {
497 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
505 my ($nick,$chan) = @_;
507 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
509 if (&validChan($chan) == 0) {
510 &ERROR("INIC: invalid channel $chan.");
514 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
521 sub IsNickInAnyChan {
524 foreach $chan (keys %channels) {
525 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
531 # Usage: &validChan($chan);
535 if (lc $chan ne $chan) {
536 &WARN("validChan: lc chan != chan. ($chan); fixing.");
537 $chan =~ tr/A-Z/a-z/;
540 if (exists $channels{$chan}) {
548 # Usage: &DeleteUserInfo($nick,@chans);
550 my ($nick,@chans) = @_;
553 foreach $chan (@chans) {
554 foreach $mode (keys %{$channels{$chan}}) {
556 next unless (exists $channels{$chan}{$mode}{$nick});
558 delete $channels{$chan}{$mode}{$nick};
566 delete $channels{$chan};
570 &DEBUG("clearIRCVars() called!");
572 @joinchan = split /[\t\s]+/, $param{'join_channels'};
581 return(keys %channels);
582 } elsif ($str =~ s/^ALL but //i) {
583 @chans = keys %channels;
584 foreach (split /[\s\t\,]+/, lc $str) {
585 @chans = grep !/^$_$/, @chans;
588 foreach (split /[\s\t\,]+/, lc $str) {
589 next unless (&validChan($_));
598 foreach $type (keys %dcc) {
599 foreach (keys %{$dcc{$type}}) {
600 &DEBUG("closing DCC $type to $_ (FIXME).");
601 ### $irc->removeconn($dcc{$type}{$_});