2 # Irc.pl: IRC core stuff.
3 # Author: xk <xk@leguin.openprojects.net>
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 # clear out hashes before connecting...
97 $ident = $param{'ircNick'};
99 $ircstats{'ConnectTime'} = time();
100 $ircstats{'ConnectCount'}++;
101 $ircstats{'Server'} = "$server:$port";
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);
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.
142 ######################################################################
143 ######## IRC ALIASES IRC ALIASES IRC ALIASES IRC ALIASES #######
144 ######################################################################
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) {
156 $conn->sl($buf) if (&whatInterface() =~ /IRC/);
158 $last{buflen} = length($buf);
163 if (!defined $msg or $msg eq "NOREPLY") {
165 &DEBUG("say: msg == $msg.");
169 if ($msg eq $last{say} and length($msg) > 256) {
170 &status("say: detected repeated message; skipping.");
175 &status("</$talkchannel> $msg");
176 if (&whatInterface() =~ /IRC/) {
177 $msg = "zero" if ($msg =~ /^0+$/);
179 $conn->privmsg($talkchannel, $msg);
184 my ($nick, $msg) = @_;
185 if (!defined $nick) {
186 &ERROR("msg: nick == NULL.");
190 if (!defined $msg or $msg eq "NOREPLY") {
192 &DEBUG("msg: msg == $msg.");
196 if ($msg eq $last{msg} and length($msg) > 256) {
197 &status("msg: detected repeated message; skipping.");
202 &status(">$nick< $msg");
203 $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
206 # Usage: &action(nick || chan, txt);
208 my ($target, $txt) = @_;
210 &DEBUG("action: txt == NULL.");
214 my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
215 if (length $rawout > 510) {
216 &status("action: txt too long; truncating.");
218 chop($rawout) while (length($rawout) > 510);
222 &status("* $ident/$target $txt");
226 # Usage: &action(nick || chan, txt);
228 my ($target, $txt) = @_;
230 &DEBUG("action: txt == NULL.");
234 &status("-$target- $txt");
236 $conn->notice($target, $txt);
243 foreach (keys %{$dcc{'CHAT'}}) {
244 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
249 ### perform commands.
252 # Usage: &performReply($reply);
255 $reply =~ /([\.\?\s]+)$/;
257 &checkMsgType($reply);
259 if ($msgType eq 'public') {
261 $reply = "$orig{who}: ".$reply;
263 $reply = "$reply, ".$orig{who};
266 } elsif ($msgType eq 'private') {
270 $reply = "$reply, ".$orig{who};
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}'.");
278 &ERROR("PR: msgType invalid? ($msgType).");
283 sub performAddressedReply {
284 return unless ($addressed);
288 # Usage: &performStrictReply($reply);
289 sub performStrictReply {
292 &checkMsgType($reply);
294 if ($msgType eq 'private') {
296 } elsif ($msgType eq 'public') {
298 } elsif ($msgType eq 'chat') {
299 if (!exists $dcc{'CHAT'}{$who}) {
300 &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
303 $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
305 &ERROR("pSR: msgType invalid? ($msgType).");
313 my $chan = lc $chankey;
315 if ($chankey =~ s/^($mask{chan}),\S+/ /) {
319 &status("joining $b_blue$chan$ob");
321 if (&validChan($chan)) {
322 &status("join: already on $chan");
332 next if ($chan eq "");
333 $chan =~ tr/A-Z/a-z/; # lowercase.
335 &status("parting $chan");
336 if (!&validChan($chan)) {
337 &status("part: not on $chan");
341 rawout("PART $chan");
342 # deletion of $channels{chan} is done in &entryEvt().
347 my ($chan, @modes) = @_;
348 my $modes = join(" ", @modes);
350 if (&validChan($chan) == 0) {
351 &ERROR("mode: invalid chan => '$chan'.");
355 rawout("MODE $chan $modes");
359 my ($chan, @who) = @_;
360 my $os = "o" x scalar(@who);
362 &mode($chan, "+$os ".@who);
366 my ($chan, @who) = @_;
367 my $os = "o" x scalar(@who);
369 &mode($chan, "-$os ".@who);
373 my ($nick,$chan,$msg) = @_;
374 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
376 if ($chan ne "" and &validChan($chan) == 0) {
377 &ERROR("kick: invalid channel $chan.");
381 $nick =~ tr/A-Z/a-z/;
383 foreach $chan (@chans) {
384 if (!&IsNickInChan($nick,$chan)) {
385 &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
389 if (!exists $channels{$chan}{o}{$ident}) {
390 &status("Kick: do not have ops on $chan :(");
394 &status("Kicking $nick from $chan.");
396 &rawout("KICK $chan $nick");
398 &rawout("KICK $chan $nick :$msg");
404 my ($mask,$chan) = @_;
405 my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
407 if ($chan ne "" and &validChan($chan) == 0) {
408 &ERROR("ban: invalid channel $chan.");
412 $nick =~ tr/A-Z/a-z/;
414 foreach $chan (@chans) {
415 if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
416 &status("Ban: $nick is not on $chan.");
420 if (!exists $channels{$chan}{o}{$ident}) {
421 &status("Ban: do not have ops on $chan :(");
425 &status("Banning $mask from $chan.");
426 &rawout("MODE $chan +b $mask");
432 &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
433 $conn->quit($quitmsg);
439 if ($nick =~ /^$mask{nick}$/) {
440 rawout("NICK ".$nick);
448 my($who, $chan) = @_;
449 rawout("INVITE $who $chan");
454 # Channel related functions...
457 # Usage: &joinNextChan();
459 if (scalar @joinchan) {
460 my $chan = shift @joinchan;
463 if (my $i = scalar @joinchan) {
464 &status("joinNextChan: $i chans to join.");
469 # Usage: &GetNickInChans($nick,$chan);
474 foreach (keys %channels) {
475 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
483 my ($nick,$chan) = @_;
485 $chan =~ tr/A-Z/a-z/; # not lowercase unfortunately.
487 if (&validChan($chan) == 0) {
488 &ERROR("INIC: invalid channel $chan.");
492 if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
499 sub IsNickInAnyChan {
502 foreach $chan (keys %channels) {
503 next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
509 # Usage: &validChan($chan);
513 if (lc $chan ne $chan) {
514 &WARN("validChan: lc chan != chan. ($chan); fixing.");
515 $chan =~ tr/A-Z/a-z/;
518 if (exists $channels{$chan}) {
526 # Usage: &DeleteUserInfo($nick,@chans);
528 my ($nick,@chans) = @_;
531 foreach $chan (@chans) {
532 foreach $mode (keys %{$channels{$chan}}) {
534 next unless (exists $channels{$chan}{$mode}{$nick});
536 delete $channels{$chan}{$mode}{$nick};
544 delete $channels{$chan};
548 &DEBUG("clearIRCVars() called!");
550 @joinchan = split /[\t\s]+/, $param{'join_channels'};
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;
566 foreach (split /[\s\t\,]+/, lc $str) {
567 next unless (&validChan($_));
576 foreach $type (keys %dcc) {
577 foreach (keys %{$dcc{$type}}) {
578 &DEBUG("closing DCC $type to $_ (FIXME).");
579 ### $irc->removeconn($dcc{$type}{$_});