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 ### Start of command hooks for UserExtra.
17 &addCmdHook("main", 'chan(stats|info)', ('CODEREF' => 'chaninfo', ) );
18 &addCmdHook("main", 'cmd(stats|info)', ('CODEREF' => 'cmdstats', ) );
19 &addCmdHook("main", 'factinfo', ('CODEREF' => 'factinfo',
20 'Cmdstats' => 'Factoid Info', Module => 'factoids', ) );
21 &addCmdHook("main", 'factstats?', ('CODEREF' => 'factstats',
22 'Cmdstats' => 'Factoid Statistics', Help => "factstats",
23 Forker => 1, 'Identifier' => 'factoids', ) );
24 &addCmdHook("main", 'help', ('CODEREF' => 'help',
25 'Cmdstats' => 'Help', ) );
27 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
30 ### Start of commands for hooks.
34 my $chan = lc shift(@_);
37 if ($chan eq "") { # all channels.
39 my $i = keys %channels;
40 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
44 foreach (sort keys %channels) {
46 &status("chanstats: fe channels: chan == NULL.");
50 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
52 &performStrictReply($reply.": ".join(' ', @array));
55 foreach $chan (keys %channels) {
57 # TODO: use $mask{chan} instead?
59 &ERROR("bad channel: chan => '$chan'.");
61 $count += scalar(keys %{$channels{$chan}{''}});
64 "i've cached \002$count\002 ".&fixPlural("user",$count).
65 " distributed over \002".scalar(keys %channels)."\002 ".
66 &fixPlural("channel",scalar(keys %channels))."."
74 if (&validChan($chan) == 0) {
75 &msg($who,"error: invalid channel \002$chan\002");
81 foreach (sort keys %{$chanstats{$chan}}) {
82 my $int = $chanstats{$chan}{$_};
85 push(@array, "\002$int\002 ". &fixPlural($_,$int));
87 my $reply = "On \002$chan\002, there ".
88 &fixPlural("has",scalar(@array)). " been ".
91 # Step 1b: check channel inconstencies.
92 $chanstats{$chan}{'Join'} ||= 0;
93 $chanstats{$chan}{'SignOff'} ||= 0;
94 $chanstats{$chan}{'Part'} ||= 0;
96 my $delta_stats = $chanstats{$chan}{'Join'}
97 - $chanstats{$chan}{'SignOff'}
98 - $chanstats{$chan}{'Part'};
101 my $total = scalar(keys %{$channels{$chan}{''}});
102 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
104 if ($delta_stats > $total) {
105 &ERROR("chaninfo: delta_stats exceeds total users.");
112 foreach ("v","o","") {
113 my $int = scalar(keys %{$channels{$chan}{$_}});
116 $type = "Voice" if ($_ eq "v");
117 $type = "Opped" if ($_ eq "o");
118 $type = "Total" if ($_ eq "");
120 push(@array,"\002$int\002 $type");
122 $reply .= ". At the moment, ". &IJoin(@array);
125 ### TODO: what's wrong with the following?
126 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
127 my($count) = (sort { $b <=> $a } keys %new)[0];
129 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
131 &performStrictReply("$reply.");
134 # Command statistics.
138 if (!scalar(keys %cmdstats)) {
139 &performReply("no-one has run any commands yet");
144 foreach (keys %cmdstats) {
145 $countstats{$cmdstats{$_}}{$_} = 1;
148 foreach (sort {$b <=> $a} keys %countstats) {
152 foreach (keys %{$countstats{$int}}) {
153 push(@array, "\002$int\002 of $_");
156 &performStrictReply("command usage include ". &IJoin(@array).".");
159 # Factoid extension info. xk++
161 my $faqtoid = lc shift(@_);
164 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
165 &msg($who,"error: individual factoid info queries not supported as yet.");
166 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
173 &CmdFactInfo($faqtoid, $query);
177 my $type = shift(@_);
179 &Forker("factoids", sub {
180 &performStrictReply( &CmdFactStats($type) );
186 if ($message =~ /^(asci*|chr) (\d+)$/) {
187 return '' unless (&IsParam("allowConv"));
192 $result = "^".chr($arg);
196 $result = "NULL" if ($arg == 0);
198 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
203 if ($message =~ /^ord (.)$/) {
204 return '' unless (&IsParam("allowConv"));
207 if (ord($arg) < 32) {
208 $arg = chr(ord($arg) + 64);
209 if ($arg eq chr(64)) {
216 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
221 if ($message =~ /^hex(\s+(.*))?$/i) {
229 if (length $arg > 80) {
230 &msg($who, "Too long.");
235 foreach (split //, $arg) {
236 $retval .= sprintf(" %X", ord($_));
239 &performStrictReply("$arg is$retval");
245 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
247 &performStrictReply(crypt($1, $2));
253 if ($message =~ /^karma(\s+(\S+))?\??$/i) {
254 return '' unless (&IsParam("karma"));
256 my $target = lc $2 || lc $who;
258 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
260 &performStrictReply("$target has karma of $karma");
262 &performStrictReply("$target has neutral karma");
269 if ($message =~ /^ignorelist$/i) {
270 &status("$who asked for the ignore list");
273 my $count = scalar(keys %ignoreList);
278 &performStrictReply("no one in the ignore list!!!");
282 foreach (sort keys %ignoreList) {
285 if ($ignoreList{$_} != 1) { # temporary ignore.
286 my $expire = $ignoreList{$_} - $time;
287 if (defined $expire and $expire < 0) {
288 &status("ignorelist: deleting $_.");
289 delete $ignoreList{$_};
291 $str = "$_ (". &Time2String($expire) .")";
299 if (scalar @array >= 8 or $counter == $count) {
300 &msg($who, &formListReply(0, "Ignore list ", @array) );
309 if ($message =~ /^spell(\s+(.*))?$/) {
310 return '' unless (&IsParam("spell"));
318 if (! -x "/usr/bin/spell") {
319 &msg($who, "no binary found.");
323 if (!&validExec($query)) {
324 &msg($who,"argument appears to be fuzzy.");
328 my $reply = "I can't find alternate spellings for '$query'";
330 foreach (`echo '$query' | ispell -a -S`) {
332 last if !length; # end of query.
334 if (/^\@/) { # intro line.
336 } elsif (/^\*/) { # possibly correct.
337 $reply = "'$query' may be spelled correctly";
339 } elsif (/^\&/) { # possible correction(s).
340 s/^\& (\S+) \d+ \d+: //;
341 my @array = split(/,? /);
343 $reply = "possible spellings for $query: @array";
346 &DEBUG("spell: '+' found => '$_'.");
349 &DEBUG("spell: unknown: '$_'.");
353 &performStrictReply($reply);
359 if ($message =~ /^(dns|nslookup)(\s+(\S+))?$/i) {
360 return '' unless (&IsParam("allowDNS"));
367 &status("DNS Lookup: $3");
368 &loadMyModule($myModules{'allowDNS'});
374 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
375 return $noreply unless (&hasFlag("o"));
379 if ($msgType =~ /public/) {
380 $chan = $talkchannel;
381 &DEBUG("cycle: setting chan to '$chan'.");
388 if (&validChan($chan) == 0) {
389 &msg($who,"error: invalid channel \002$chan\002");
393 &msg($chan, "I'm coming back. (courtesy of $who)");
395 ### &ScheduleThis(5, "getNickInUse") if (@_);
396 &status("Schedule rejoin in 5secs to $chan by $who.");
397 $conn->schedule(5, sub { &joinchan($chan); });
403 if ($message =~ /^redir(\s+(.*))?/i) {
404 return $noreply unless (&hasFlag("o"));
407 if (!defined $factoid) {
412 my $val = &getFactInfo($factoid, "factoid_value");
413 if (!defined $val or $val eq "") {
414 &msg($who, "error: '$factoid' does not exist.");
417 &DEBUG("val => '$val'.");
418 my @list = &searchTable("factoids", "factoid_key",
419 "factoid_value", "^$val\$");
421 if (scalar @list == 1) {
422 &msg($who, "hrm... '$factoid' is unique.");
425 if (scalar @list > 5) {
426 &msg($who, "A bit too many factoids to be redirected, hey?");
431 &status("Redirect '$factoid' (". ($#list) .")...");
433 next if (/^\Q$factoid\E$/i);
435 &status(" Redirecting '$_'.");
436 my $was = &getFactoid($_);
437 &DEBUG(" was '$was'.");
439 &setFactInfo($_, "factoid_value", "<REPLY> see $factoid");
443 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
449 if ($message =~ /^rot13(\s+(.*))?/i) {
457 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
458 &performStrictReply($reply);
464 if ($message =~ /^ircstats$/i) {
465 my $count = $ircstats{'ConnectCount'};
466 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
469 foreach (keys %ircstats) {
470 &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
474 if ($count == 1) { # good.
475 $reply = "I'm connected to $ircstats{'Server'} and have been so".
478 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
479 " for $format_time. ".
480 "I had to reconnect \002$count\002 times.";
484 my $reason = $ircstats{'DisconnectReason'};
485 if (defined $reason) {
486 $reply .= " I was last disconnected for '$reason'.";
489 &performStrictReply($reply);
495 if ($message =~ /^statu?s$/i) {
496 my $startString = scalar(localtime $^T);
497 my $upString = &Time2String(time() - $^T);
498 my $count = &countKeys("factoids");
501 "Since $startString, there have been".
502 " \002$count{'Update'}\002 ".
503 &fixPlural("modification", $count{'Update'}).
504 " and \002$count{'Question'}\002 ".
505 &fixPlural("question",$count{'Question'}).
506 " and \002$count{'Dunno'}\002 ".
507 &fixPlural("dunno",$count{'Dunno'}).
508 " and \002$count{'Moron'}\002 ".
509 &fixPlural("moron",$count{'Moron'}).
510 ". I have been awake for $upString this session, and ".
511 "currently reference \002$count\002 factoids. ".
512 "I'm using about \002$memusage\002 ".
520 if ($message =~ /^(tell|explain)(\s+(.*))?$/) {
521 return '' unless (&IsParam("allowTelling"));
524 if (!defined $args) {
529 my ($target, $tell_obj) = ('','');
531 ### is this fixed elsewhere?
532 $args =~ s/\s+/ /g; # fix up spaces.
533 $args =~ s/^\s+|\s+$//g; # again.
535 # this one catches most of them
536 if ($args =~ /^(\S+) about (.*)$/i) {
540 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
542 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
543 # i'm sure this could all be nicely collapsed
548 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
553 $query = "$qWord $verb $tell_obj";
555 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
561 # check target type. Deny channel targets.
562 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
563 &msg($who,"No, $who, I won't. (target invalid?)");
567 $target = $talkchannel if ($target =~ /^us$/i);
568 $target = $who if ($target =~ /^(me|myself)$/i);
570 &status("target: $target query: $query");
573 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
574 &msg($who, "No, $target is not in any of my chans.");
578 ### TODO: don't "tell" if sender is not in target's channel.
581 if ($target eq $ident) { # lc?
582 &msg($who, "Isn't that a bit silly?");
587 my $result = &doQuestion($tell_obj);
588 return $noreply if ($result eq $noreply);
592 &msg($who, "i dunno what is '$tell_obj'.");
597 &status("tell: <$who> telling $target about $tell_obj.");
598 if ($who ne $target) {
599 &msg($who, "told $target about $tell_obj ($result)");
600 $reply = "$who wants you to know: $result";
602 $reply = "telling yourself: $result";
605 &msg($target, $reply);
611 if ($message =~ /^wantNick$/i) {
612 if ($param{'ircNick'} eq $ident) {
613 &msg($who, "I hope you're right. I'll try anyway.");
616 my $str = "attempting to change nick to $param{'ircNick'}";
620 &nick($param{'ircNick'});