2 # UserExtra.pl: User Commands, Public.
4 # Version: v0.2b (20000707)
8 if (&IsParam("useStrict")) { use strict; }
10 use vars qw($message $arg $qWord $verb $lobotomized);
11 use vars qw(%channels %chanstats %cmdstats);
14 return '' unless ($addressed);
17 if ($message =~ /^chan(stats|info)(\s+(\S+))?$/i) {
21 if ($chan eq "") { # all channels.
23 my $i = keys %channels;
24 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
28 foreach (sort keys %channels) {
30 &status("chanstats: fe channels: chan == NULL.");
34 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
36 &performStrictReply($reply.": ".join(' ', @array));
39 foreach $chan (keys %channels) {
41 # TODO: use $mask{chan} instead?
43 &ERROR("bad channel: chan => '$chan'.");
45 $count += scalar(keys %{$channels{$chan}{''}});
48 "i've cached \002$count\002 ".&fixPlural("user",$count).
49 " distributed over \002".scalar(keys %channels)."\002 ".
50 &fixPlural("channel",scalar(keys %channels))."."
58 if (&validChan($chan) == 0) {
59 &msg($who,"error: invalid channel \002$chan\002");
65 foreach (sort keys %{$chanstats{$chan}}) {
66 my $int = $chanstats{$chan}{$_};
69 push(@array, "\002$int\002 ". &fixPlural($_,$int));
71 my $reply = "On \002$chan\002, there ".
72 &fixPlural("has",scalar(@array)). " been ".
75 # Step 1b: check channel inconstencies.
76 $chanstats{$chan}{'Join'} ||= 0;
77 $chanstats{$chan}{'SignOff'} ||= 0;
78 $chanstats{$chan}{'Part'} ||= 0;
80 my $delta_stats = $chanstats{$chan}{'Join'}
81 - $chanstats{$chan}{'SignOff'}
82 - $chanstats{$chan}{'Part'};
85 my $total = scalar(keys %{$channels{$chan}{''}});
86 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
88 if ($delta_stats > $total) {
89 &ERROR("chaninfo: delta_stats exceeds total users.");
96 foreach ("v","o","") {
97 my $int = scalar(keys %{$channels{$chan}{$_}});
100 $type = "Voice" if ($_ eq "v");
101 $type = "Opped" if ($_ eq "o");
102 $type = "Total" if ($_ eq "");
104 push(@array,"\002$int\002 $type");
106 $reply .= ". At the moment, ". &IJoin(@array);
109 ### TODO: what's wrong with the following?
110 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
111 my($count) = (sort { $b <=> $a } keys %new)[0];
113 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
115 &performStrictReply("$reply.");
120 # Command statistics.
121 if ($message =~ /^cmdstats$/i) {
124 if (!scalar(keys %cmdstats)) {
125 &performReply("no-one has run any commands yet");
130 foreach (keys %cmdstats) {
131 $countstats{$cmdstats{$_}}{$_} = 1;
134 foreach (sort {$b <=> $a} keys %countstats) {
138 foreach (keys %{$countstats{$int}}) {
139 push(@array, "\002$int\002 of $_");
142 &performStrictReply("command usage include ". &IJoin(@array).".");
148 if ($message =~ /^(asci*|chr) (\d+)$/) {
149 return '' unless (&IsParam("allowConv"));
154 $result = "^".chr($arg);
158 $result = "NULL" if ($arg == 0);
160 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
165 if ($message =~ /^ord (.)$/) {
166 return '' unless (&IsParam("allowConv"));
169 if (ord($arg) < 32) {
170 $arg = chr(ord($arg) + 64);
171 if ($arg eq chr(64)) {
178 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
183 if ($message =~ /^hex(\s+(.*))?$/i) {
191 if (length $arg > 80) {
192 &msg($who, "Too long.");
197 foreach (split //, $arg) {
198 $retval .= sprintf(" %X", ord($_));
201 &performStrictReply("$arg is$retval");
207 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
209 &performStrictReply(crypt($1, $2));
213 # Factoid extension info. xk++
214 if ($message =~ /^(factinfo)(\s+(.*))?$/i) {
218 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
219 &msg($who,"error: individual factoid info queries not supported as yet.");
220 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
227 &loadMyModule($myModules{'factoids'});
228 &CmdFactInfo($faqtoid, $query);
230 $cmdstats{'Factoid Info'}++;
234 # Factoid extension statistics. xk++
235 if ($message =~ /^(factstats?)(\s+(\S+))?$/i) {
238 if (!defined $type) {
243 &Forker("factoids", sub {
244 &performStrictReply( &CmdFactStats($type) );
246 $cmdstats{'Factoid Statistics'}++;
251 if ($message =~ /^help(\s+(.*))?$/i) {
260 if ($message =~ /^karma(\s+(\S+))?\??$/i) {
261 return '' unless (&IsParam("karma"));
263 my $target = lc $2 || lc $who;
265 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
267 &performStrictReply("$target has karma of $karma");
269 &performStrictReply("$target has neutral karma");
276 if ($message =~ /^ignorelist$/i) {
277 &status("$who asked for the ignore list");
280 my $count = scalar(keys %ignoreList);
285 &performStrictReply("no one in the ignore list!!!");
289 foreach (sort keys %ignoreList) {
292 if ($ignoreList{$_} != 1) { # temporary ignore.
293 my $expire = $ignoreList{$_} - $time;
294 if (defined $expire and $expire < 0) {
295 &status("ignorelist: deleting $_.");
296 delete $ignoreList{$_};
298 $str = "$_ (". &Time2String($expire) .")";
306 if (scalar @array >= 8 or $counter == $count) {
307 &msg($who, &formListReply(0, "Ignore list ", @array) );
316 if ($message =~ /^spell(\s+(.*))?$/) {
317 return '' unless (&IsParam("spell"));
325 if (! -x "/usr/bin/spell") {
326 &msg($who, "no binary found.");
330 if (!&validExec($query)) {
331 &msg($who,"argument appears to be fuzzy.");
335 my $reply = "I can't find alternate spellings for '$query'";
337 foreach (`echo '$query' | ispell -a -S`) {
339 last if !length; # end of query.
341 if (/^\@/) { # intro line.
343 } elsif (/^\*/) { # possibly correct.
344 $reply = "'$query' may be spelled correctly";
346 } elsif (/^\&/) { # possible correction(s).
347 s/^\& (\S+) \d+ \d+: //;
348 my @array = split(/,? /);
350 $reply = "possible spellings for $query: @array";
353 &DEBUG("spell: '+' found => '$_'.");
356 &DEBUG("spell: unknown: '$_'.");
360 &performStrictReply($reply);
366 if ($message =~ /^(dns|nslookup)(\s+(\S+))?$/i) {
367 return '' unless (&IsParam("allowDNS"));
374 &status("DNS Lookup: $3");
375 &loadMyModule($myModules{'allowDNS'});
381 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
382 return $noreply unless (&hasFlag("o"));
386 if ($msgType =~ /public/) {
387 $chan = $talkchannel;
388 &DEBUG("cycle: setting chan to '$chan'.");
395 if (&validChan($chan) == 0) {
396 &msg($who,"error: invalid channel \002$chan\002");
400 &msg($chan, "I'm coming back. (courtesy of $who)");
402 ### &ScheduleThis(5, "getNickInUse") if (@_);
403 &status("Schedule rejoin in 5secs to $chan by $who.");
404 $conn->schedule(5, sub { &joinchan($chan); });
410 if ($message =~ /^redir(\s+(.*))?/i) {
411 return $noreply unless (&hasFlag("o"));
414 if (!defined $factoid) {
419 my $val = &getFactInfo($factoid, "factoid_value");
420 if (!defined $val or $val eq "") {
421 &msg($who, "error: '$factoid' does not exist.");
424 &DEBUG("val => '$val'.");
425 my @list = &searchTable("factoids", "factoid_key",
426 "factoid_value", "^$val\$");
428 if (scalar @list == 1) {
429 &msg($who, "hrm... '$factoid' is unique.");
432 if (scalar @list > 5) {
433 &msg($who, "A bit too many factoids to be redirected, hey?");
438 &status("Redirect '$factoid' (". ($#list) .")...");
440 next if (/^\Q$factoid\E$/i);
442 &status(" Redirecting '$_'.");
443 my $was = &getFactoid($_);
444 &DEBUG(" was '$was'.");
446 &setFactInfo($_, "factoid_value", "<REPLY> see $factoid");
450 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
456 if ($message =~ /^rot13(\s+(.*))?/i) {
464 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
465 &performStrictReply($reply);
471 if ($message =~ /^ircstats$/i) {
472 my $count = $ircstats{'ConnectCount'};
473 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
476 foreach (keys %ircstats) {
477 &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
481 if ($count == 1) { # good.
482 $reply = "I'm connected to $ircstats{'Server'} and have been so".
485 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
486 " for $format_time. ".
487 "I had to reconnect \002$count\002 times.";
491 my $reason = $ircstats{'DisconnectReason'};
492 if (defined $reason) {
493 $reply .= " I was last disconnected for '$reason'.";
496 &performStrictReply($reply);
502 if ($message =~ /^statu?s$/i) {
503 my $startString = scalar(localtime $^T);
504 my $upString = &Time2String(time() - $^T);
505 my $count = &countKeys("factoids");
508 "Since $startString, there have been".
509 " \002$count{'Update'}\002 ".
510 &fixPlural("modification", $count{'Update'}).
511 " and \002$count{'Question'}\002 ".
512 &fixPlural("question",$count{'Question'}).
513 " and \002$count{'Dunno'}\002 ".
514 &fixPlural("dunno",$count{'Dunno'}).
515 ". I have been awake for $upString this session, and ".
516 "currently reference \002$count\002 factoids. ".
517 "I'm using about \002$memusage\002 ".
525 if ($message =~ /^(tell|explain)(\s+(.*))?$/) {
526 return '' unless (&IsParam("allowTelling"));
529 if (!defined $args) {
534 my ($target, $tell_obj) = ('','');
536 ### is this fixed elsewhere?
537 $args =~ s/\s+/ /g; # fix up spaces.
538 $args =~ s/^\s+|\s+$//g; # again.
540 # this one catches most of them
541 if ($args =~ /^(\S+) about (.*)$/i) {
545 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
547 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
548 # i'm sure this could all be nicely collapsed
553 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
558 $query = "$qWord $verb $tell_obj";
560 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
566 # check target type. Deny channel targets.
567 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
568 &msg($who,"No, $who, I won't. (target invalid?)");
572 $target = $talkchannel if ($target =~ /^us$/i);
573 $target = $who if ($target =~ /^(me|myself)$/i);
575 &status("target: $target query: $query");
578 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
579 &msg($who, "No, $target is not in any of my chans.");
583 ### TODO: don't "tell" if sender is not in target's channel.
586 if ($target eq $ident) { # lc?
587 &msg($who, "Isn't that a bit silly?");
592 my $result = &doQuestion($tell_obj);
593 return $noreply if ($result eq $noreply);
597 &msg($who, "i dunno what is '$tell_obj'.");
602 &status("tell: <$who> telling $target about $tell_obj.");
603 if ($who ne $target) {
604 &msg($who, "told $target about $tell_obj ($result)");
605 $reply = "$who wants you to know: $result";
607 $reply = "telling yourself: $result";
610 &msg($target, $reply);
616 if ($message =~ /^wantNick$/i) {
617 if ($param{'ircNick'} eq $ident) {
618 &msg($who, "I hope you're right. I'll try anyway.");
621 my $str = "attempting to change nick to $param{'ircNick'}";
625 &nick($param{'ircNick'});