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;
26 "i am on \002$i\002 ". &fixPlural("channel",$i).
27 ": ". join(' ', sort keys %channels)
30 foreach $chan (keys %channels) {
33 &ERROR("bad channel: chan => '$chan'.");
35 $count += scalar(keys %{$channels{$chan}{''}});
38 "i've cached \002$count\002 ".&fixPlural("user",$count).
39 " distributed over \002".scalar(keys %channels)."\002 ".
40 &fixPlural("channel",scalar(keys %channels))."."
48 if (&validChan($chan) == 0) {
49 &msg($who,"error: invalid channel \002$chan\002");
55 foreach (sort keys %{$chanstats{$chan}}) {
56 my $int = $chanstats{$chan}{$_};
59 push(@array, "\002$int\002 ". &fixPlural($_,$int));
61 my $reply = "On \002$chan\002, there ".
62 &fixPlural("has",scalar(@array)). " been ".
65 # Step 1b: check channel inconstencies.
66 $chanstats{$chan}{'Join'} ||= 0;
67 $chanstats{$chan}{'SignOff'} ||= 0;
68 $chanstats{$chan}{'Part'} ||= 0;
70 my $delta_stats = $chanstats{$chan}{'Join'}
71 - $chanstats{$chan}{'SignOff'}
72 - $chanstats{$chan}{'Part'};
75 my $total = scalar(keys %{$channels{$chan}{''}});
76 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
78 if ($delta_stats > $total) {
79 &ERROR("chaninfo: delta_stats exceeds total users.");
86 foreach ("v","o","") {
87 my $int = scalar(keys %{$channels{$chan}{$_}});
90 $type = "Voice" if ($_ eq "v");
91 $type = "Opped" if ($_ eq "o");
92 $type = "Total" if ($_ eq "");
94 push(@array,"\002$int\002 $type");
96 $reply .= ". At the moment, ". &IJoin(@array);
99 ### TODO: what's wrong with the following?
100 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
101 my($count) = (sort { $b <=> $a } keys %new)[0];
103 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
105 &performStrictReply("$reply.");
110 # Command statistics.
111 if ($message =~ /^cmdstats$/i) {
114 if (!scalar(keys %cmdstats)) {
115 &performReply("no-one has run any commands yet");
120 foreach (keys %cmdstats) {
121 $countstats{$cmdstats{$_}}{$_} = 1;
124 foreach (sort {$b <=> $a} keys %countstats) {
128 foreach (keys %{$countstats{$int}}) {
129 push(@array, "\002$int\002 of $_");
132 &performStrictReply("command usage include ". &IJoin(@array).".");
138 if ($message =~ /^(asci*|chr) (\d+)$/) {
139 return '' unless (&IsParam("allowConv"));
144 $result = "^".chr($arg);
148 $result = "NULL" if ($arg == 0);
150 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
155 if ($message =~ /^ord (.)$/) {
156 return '' unless (&IsParam("allowConv"));
159 if (ord($arg) < 32) {
160 $arg = chr(ord($arg) + 64);
161 if ($arg eq chr(64)) {
168 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
173 if ($message =~ /^hex(\s+(.*))?$/i) {
181 if (length $arg > 80) {
182 &msg($who, "Too long.");
187 foreach (split //, $arg) {
188 $retval .= sprintf(" %X", ord($_));
191 &performStrictReply("$arg is$retval");
197 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
199 &performStrictReply(crypt($1, $2));
203 # Factoid extension info. xk++
204 if ($message =~ /^(factinfo)(\s+(.*))?$/i) {
208 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
209 &msg($who,"error: individual factoid info queries not supported as yet.");
210 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
217 &loadMyModule($myModules{'factoids'});
218 &CmdFactInfo($faqtoid, $query);
220 $cmdstats{'Factoid Info'}++;
224 # Factoid extension statistics. xk++
225 if ($message =~ /^(factstats?)(\s+(\S+))?$/i) {
228 if (!defined $type) {
233 &Forker("factoids", sub {
234 &performStrictReply( &CmdFactStats($type) );
236 $cmdstats{'Factoid Statistics'}++;
241 if ($message =~ /^help(\s+(.*))?$/i) {
250 if ($message =~ /^karma(\s+(\S+))?\??$/i) {
251 return '' unless (&IsParam("karma"));
253 my $target = lc $2 || lc $who;
255 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
257 &performStrictReply("$target has karma of $karma");
259 &performStrictReply("$target has neutral karma");
266 if ($message =~ /^ignorelist$/i) {
267 &status("$who asked for the ignore list");
270 my $count = scalar(keys %ignoreList);
275 &performStrictReply("no one in the ignore list!!!");
279 foreach (sort keys %ignoreList) {
282 if ($ignoreList{$_} != 1) { # temporary ignore.
283 my $expire = $ignoreList{$_} - $time;
284 if (defined $expire and $expire < 0) {
285 &status("ignorelist: deleting $_.");
286 delete $ignoreList{$_};
288 $str = "$_ (". &Time2String($expire) .")";
296 if (scalar @array >= 8 or $counter == $count) {
297 &msg($who, &formListReply(0, "Ignore list ", @array) );
306 if ($message =~ /^spell(\s+(.*))?$/) {
307 return '' unless (&IsParam("spell"));
315 if (! -x "/usr/bin/spell") {
316 &msg($who, "no binary found.");
320 if (!&validExec($query)) {
321 &msg($who,"argument appears to be fuzzy.");
325 my $reply = "I can't find alternate spellings for '$query'";
327 foreach (`echo '$query' | ispell -a -S`) {
329 last if !length; # end of query.
331 if (/^\@/) { # intro line.
333 } elsif (/^\*/) { # possibly correct.
334 $reply = "'$query' may be spelled correctly";
336 } elsif (/^\&/) { # possible correction(s).
337 s/^\& (\S+) \d+ \d+: //;
338 my @array = split(/,? /);
340 $reply = "possible spellings for $query: @array";
343 &DEBUG("spell: '+' found => '$_'.");
346 &DEBUG("spell: unknown: '$_'.");
350 &performStrictReply($reply);
356 if ($message =~ /^(dns|nslookup)(\s+(\S+))?$/i) {
357 return '' unless (&IsParam("allowDNS"));
364 &status("DNS Lookup: $3");
365 &loadMyModule($myModules{'allowDNS'});
371 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
372 return $noreply unless (&hasFlag("o"));
376 if ($msgType =~ /public/) {
377 $chan = $talkchannel;
378 &DEBUG("cycle: setting chan to '$chan'.");
385 if (&validChan($chan) == 0) {
386 &msg($who,"error: invalid channel \002$chan\002");
390 &msg($chan, "I'm coming back. (courtesy of $who)");
399 if ($message =~ /^redir(\s+(.*))?/i) {
400 return $noreply unless (&hasFlag("o"));
403 if (!defined $factoid) {
408 my $val = &getFactInfo($factoid, "factoid_value");
409 if (!defined $val or $val eq "") {
410 &msg($who, "error: '$factoid' does not exist.");
413 &DEBUG("val => '$val'.");
414 my @list = &searchTable("factoids", "factoid_key",
415 "factoid_value", "^$val\$");
417 if (scalar @list == 1) {
418 &msg($who, "hrm... '$factoid' is unique.");
421 if (scalar @list > 5) {
422 &msg($who, "A bit too many factoids to be redirected, hey?");
427 &status("Redirect '$factoid' (". ($#list) .")...");
429 next if (/^\Q$factoid\E$/i);
431 &status(" Redirecting '$_'.");
432 my $was = &getFactoid($_);
433 &DEBUG(" was '$was'.");
435 &setFactInfo($_, "factoid_value", "<REPLY> see $factoid");
439 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
445 if ($message =~ /^rot13(\s+(.*))?/i) {
453 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
454 &performStrictReply($reply);
460 if ($message =~ /^ircstats$/i) {
461 my $count = $ircstats{'ConnectCount'};
462 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
466 if ($count == 1) { # good.
467 $reply = "I'm connected to $ircstats{'Server'} and have been so".
470 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
471 " for $format_time. ".
472 "I had to reconnect \002$count\002 times.";
476 my $reason = $ircstats{'DisconnectReason'};
477 if (defined $reason) {
478 $reply .= " I was last disconnected for '$reason'.";
481 &performStrictReply($reply);
487 if ($message =~ /^statu?s$/i) {
488 my $startString = scalar(localtime $^T);
489 my $upString = &Time2String(time() - $^T);
490 my $count = &countKeys("factoids");
493 "Since $startString, there have been".
494 " \002$count{'Update'}\002 ".
495 &fixPlural("modification", $count{'Update'}).
496 " and \002$count{'Question'}\002 ".
497 &fixPlural("question",$count{'Question'}).
498 " and \002$count{'Dunno'}\002 ".
499 &fixPlural("dunno",$count{'Dunno'}).
500 ". I have been awake for $upString this session, and ".
501 "currently reference \002$count\002 factoids. ".
502 "I'm using about \002$memusage\002 ".
510 if ($message =~ /^(tell|explain)(\s+(.*))?$/) {
511 return '' unless (&IsParam("allowTelling"));
514 if (!defined $args) {
519 my ($target, $tell_obj) = ('','');
522 # this one catches most of them
523 if ($message =~ /^tell\s+(\S+)\s+about\s+(.*)/i) {
527 # required for privmsg if excessive size.(??)
528 if ($target =~ /^us$/i) {
529 $target = $talkchannel;
530 } elsif ($target =~ /^(me|myself)$/i) {
534 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
536 } elsif ($message =~ /tell\s+(\S+)\s+where\s+(\S+)\s+can\s+(\S+)\s+(.*)/i) {
537 # i'm sure this could all be nicely collapsed
542 $target = "" if ($target =~ /^us$/i);
543 } elsif ($message =~ /tell\s+(\S+)\s+(what|where)\s+(.*?)\s+(is|are)[.?!]*$/i) {
548 $query = "$qWord $verb $tell_obj";
550 $target = "" if ($target =~ /^us$/i);
551 } elsif ($message =~ /^(explain|tell)\s+(\S+)\s+to\s+(.*)$/i) {
555 $target = "" if ($target =~ /^us$/i);
557 &status("target: $target query: $query");
559 # check target type. Deny channel targets.
560 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
561 &msg($who,"No, $who, I won't.");
566 if (!&IsNickInAnyChan($target)) {
567 &msg($who, "No, $target is not in any of my chans.");
571 ### TODO: don't "tell" if sender is not in target's channel.
574 if ($target eq $ident) {
575 &msg($who, "Isn't that a bit silly?");
580 my $result = &doQuestion($tell_obj);
581 return $noreply if ($result eq $noreply);
585 &msg($who, "i dunno what is '$tell_obj'.");
590 &status("tell: <$who> telling $target about $tell_obj.");
591 if ($who ne $target) {
592 &msg($who, "told $target about $tell_obj ($result)");
593 $reply = "$who wants you to know: $result";
595 $reply = "telling yourself: $result";
598 &msg($target, $reply);
604 if ($message =~ /^wantNick$/i) {
605 if ($param{'ircNick'} eq $ident) {
606 &msg($who, "I hope you're right. I'll try anyway.");
609 my $str = "attempting to change nick to $param{'ircNick'}";
613 &nick($param{'ircNick'});