]> git.donarmstrong.com Git - infobot.git/blob - src/UserExtra.pl
- sql function changeover stuff.
[infobot.git] / src / UserExtra.pl
1 #
2 # UserExtra.pl: User Commands, Public.
3 #       Author: dms
4 #      Version: v0.2b (20000707)
5 #      Created: 20000107
6 #
7
8 use strict;
9 use vars qw($message $arg $qWord $verb $lobotomized $who $result $chan
10         $conn $msgType $query $talkchannel $ident $memusage);
11 use vars qw(%channels %chanstats %cmdstats %count %ircstats %param
12         %cache %mask %userstats %hooks_main);
13
14 ###
15 ### Start of command hooks for UserExtra.
16 ###
17
18 &addCmdHook("main", 'chan(stats|info)', ('CODEREF' => 'chaninfo', ) );
19 &addCmdHook("main", 'cmd(stats|info)', ('CODEREF' => 'cmdstats', ) );
20 &addCmdHook("main", 'factinfo', ('CODEREF' => 'factinfo', 
21         'Cmdstats' => 'Factoid Info', Module => 'factoids', ) );
22 &addCmdHook("main", 'factstats?', ('CODEREF' => 'factstats', 
23         'Cmdstats' => 'Factoid Stats', Help => "factstats", 
24         Forker => 1, 'Identifier' => 'factoids', ) );
25 &addCmdHook("main", 'help', ('CODEREF' => 'help', 
26         'Cmdstats' => 'Help', ) );
27 &addCmdHook("main", 'karma', ('CODEREF' => 'karma', ) );
28 &addCmdHook("main", 'i?spell', ('CODEREF' => 'ispell', 
29         Help => 'spell', Identifier => 'spell', ) );
30 &addCmdHook("main", 'd?nslookup', ('CODEREF' => 'DNS', 
31         Help => 'nslookup', Identifier => 'allowDNS',
32         Forker => "NULL", ) );
33 &addCmdHook("main", 'tell|explain', ('CODEREF' => 'tell', 
34         Help => 'tell', Identifier => 'allowTelling',
35         Cmdstats => 'Tell') );
36 &addCmdHook("main", 'news', ('CODEREF' => 'News::Parse', 
37         Module => 'news', 'Cmdstats' => 'News' ) );
38 &addCmdHook("main", 'countrystats', ('CODEREF' => 'countryStats', 
39 #       Forker => "NULL",
40  ) );
41
42 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
43
44 ###
45 ### Start of commands for hooks.
46 ###
47
48 sub chaninfo {
49     my $chan = lc shift(@_);
50     my $mode;
51
52     if ($chan eq "") {          # all channels.
53         my $i           = keys %channels;
54         my $reply       = "i am on \002$i\002 ".&fixPlural("channel",$i);
55         my $tucount     = 0;    # total user count.
56         my $uucount     = 0;    # unique user count.
57         my @array;
58
59         ### line 1.
60         foreach (sort keys %channels) {
61             if (/^\s*$/ or / /) {
62                 &status("chanstats: fe channels: chan == NULL.");
63                 &ircCheck();
64                 next;
65             }
66             push(@array, "$_ (".scalar(keys %{ $channels{$_}{''} }).")");
67         }
68         &pSReply($reply.": ".join(' ', @array));
69
70         ### total user count.
71         foreach $chan (keys %channels) {
72             $tucount += scalar(keys %{ $channels{$chan}{''} });
73         }
74
75         ### unique user count.
76         my @nicks;
77         foreach $chan (keys %channels) {
78             foreach (keys %{ $channels{$chan}{''} }) {
79                 next if (grep /^\Q$_\E$/, @nicks);
80                 $uucount++;
81                 push(@nicks, $_);
82             }
83         }
84         &DEBUG("nicks => '".scalar(@nicks)."'...");
85         if (scalar @nicks != $uucount) {
86             &DEBUG("nicks != uucount...");
87         }
88
89         my $chans = scalar(keys %channels);
90         &pSReply(
91             "i've cached \002$tucount\002 ". &fixPlural("user",$tucount).
92             ", \002$uucount\002 unique ". &fixPlural("user",$uucount).
93             ", distributed over \002$chans\002 ".
94             &fixPlural("channel", $chans)."."
95         );
96
97         return;
98     }
99
100     # channel specific.
101
102     if (&validChan($chan) == 0) {
103         &msg($who,"error: invalid channel \002$chan\002");
104         return;
105     }
106
107     # Step 1:
108     my @array;
109     foreach (sort keys %{ $chanstats{$chan} }) {
110         my $int = $chanstats{$chan}{$_};
111         next unless ($int);
112
113         push(@array, "\002$int\002 ". &fixPlural($_,$int));
114     }
115     my $reply = "On \002$chan\002, there ".
116                 &fixPlural("has",scalar(@array)). " been ".
117                 &IJoin(@array);
118
119     # Step 1b: check channel inconstencies.
120     $chanstats{$chan}{'Join'}           ||= 0;
121     $chanstats{$chan}{'SignOff'}        ||= 0;
122     $chanstats{$chan}{'Part'}           ||= 0;
123
124     my $delta_stats = $chanstats{$chan}{'Join'}
125                 - $chanstats{$chan}{'SignOff'}
126                 - $chanstats{$chan}{'Part'};
127
128     if ($delta_stats) {
129         my $total = scalar(keys %{ $channels{$chan}{''} });
130         &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
131
132         if ($delta_stats > $total) {
133             &ERROR("chaninfo: delta_stats exceeds total users.");
134         }
135     }
136
137     # Step 2:
138     undef @array;
139     my $type;
140     foreach ("v","o","") {
141         my $int = scalar(keys %{ $channels{$chan}{$_} });
142         next unless ($int);
143
144         $type = "Voice" if ($_ eq "v");
145         $type = "Opped" if ($_ eq "o");
146         $type = "Total" if ($_ eq "");
147
148         push(@array,"\002$int\002 $type");
149     }
150     $reply .= ".  At the moment, ". &IJoin(@array);
151
152     # Step 3:
153     my %new;
154     foreach (keys %userstats) {
155         next unless (exists $userstats{$_}{'Count'});
156         if ($userstats{$_}{'Count'} =~ /^\D+$/) {
157             &WARN("userstats{$_}{Count} is non-digit.");
158             next;
159         }
160
161         $new{$_} = $userstats{$_}{'Count'};
162     }
163
164     # todo: show top 3 with percentages?
165     my($count) = (sort { $new{$a} <=> $new{$b} } keys %new)[0];
166     if ($count) {
167         $reply .= ".  \002$count\002 has said the most with a total of \002$new{$count}\002 messages";
168     }
169     &pSReply("$reply.");
170 }
171
172 # Command statistics.
173 sub cmdstats {
174     my @array;
175
176     if (!scalar(keys %cmdstats)) {
177         &performReply("no-one has run any commands yet");
178         return;
179     }
180
181     my %countstats;
182     foreach (keys %cmdstats) {
183         $countstats{ $cmdstats{$_} }{$_} = 1;
184     }
185
186     foreach (sort {$b <=> $a} keys %countstats) {
187         my $int = $_;
188         next unless ($int);
189
190         foreach (keys %{ $countstats{$int} }) {
191             push(@array, "\002$int\002 of $_");
192         }
193     }
194     &pSReply("command usage include ". &IJoin(@array).".");
195 }
196
197 # Factoid extension info. xk++
198 sub factinfo {
199     my $faqtoid = lc shift(@_);
200     my $query   = "";
201
202     if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
203         &msg($who,"error: individual factoid info queries not supported as yet.");
204         &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
205         return;
206
207         $query   = lc $1;
208         $faqtoid = lc $3;
209     }
210
211     &CmdFactInfo($faqtoid, $query);
212 }
213
214 sub factstats {
215     my $type = shift(@_);
216
217     &Forker("factoids", sub {
218         &pSReply( &CmdFactStats($type) );
219     } );
220 }
221
222 sub karma {
223     my $target  = lc( shift || $who );
224     my $karma   = &dbGet("stats", "counter", "nick=".
225                         &dbQuote($target)." AND type='karma'") || 0; 
226
227     if ($karma != 0) {
228         &pSReply("$target has karma of $karma");
229     } else {
230         &pSReply("$target has neutral karma");
231     }
232 }
233
234 sub ispell {
235     my $query = shift;
236
237     if (! -x "/usr/bin/ispell") {
238         &msg($who, "no binary found.");
239         return;
240     }
241
242     if (!&validExec($query)) {
243         &msg($who,"argument appears to be fuzzy.");
244         return;
245     }
246
247     my $reply = "I can't find alternate spellings for '$query'";
248
249     foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
250         chop;
251         last if !length;                # end of query.
252
253         if (/^\@/) {            # intro line.
254             next;
255         } elsif (/^\*/) {               # possibly correct.
256             $reply = "'$query' may be spelled correctly";
257             last;
258         } elsif (/^\&/) {               # possible correction(s).
259             s/^\& (\S+) \d+ \d+: //;
260             my @array = split(/,? /);
261
262             $reply = "possible spellings for $query: @array";
263             last;
264         } elsif (/^\+/) {
265             &DEBUG("spell: '+' found => '$_'.");
266             last;
267         } elsif (/^# (.*?) 0$/) {
268             # none found.
269             last;
270         } else {
271             &DEBUG("spell: unknown: '$_'.");
272         }
273     }
274
275     &pSReply($reply);
276 }
277
278 sub nslookup {
279     my $query = shift;
280     &status("DNS Lookup: $query");
281     &DNS($query);
282 }
283
284 sub tell {
285     my $args = shift;
286     my ($target, $tell_obj) = ('','');
287     my $dont_tell_me    = 0;
288     my $reply;
289
290     ### is this fixed elsewhere?
291     $args =~ s/\s+/ /g;         # fix up spaces.
292     $args =~ s/^\s+|\s+$//g;    # again.
293
294     # this one catches most of them
295     if ($args =~ /^(\S+) (-?)about (.*)$/i) {
296         $target         = $1;
297         $tell_obj       = $3;
298         $dont_tell_me   = ($2) ? 1 : 0;
299
300         $tell_obj       = $who  if ($tell_obj =~ /^(me|myself)$/i);
301         $query          = $tell_obj;
302     } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
303         # i'm sure this could all be nicely collapsed
304         $target         = $1;
305         $tell_obj       = $4;
306         $query          = $tell_obj;
307
308     } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
309         $target         = $1;
310         $qWord          = $2;
311         $tell_obj       = $3;
312         $verb           = $4;
313         $query          = "$qWord $verb $tell_obj";
314
315     } elsif ($args =~ /^(.*?) to (\S+)$/i) {
316         $target         = $3;
317         $tell_obj       = $2;
318         $query          = $tell_obj;
319     }
320
321     # check target type. Deny channel targets.
322     if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
323         &msg($who,"No, $who, I won't. (target invalid?)");
324         return;
325     }
326
327     $target     = $talkchannel  if ($target =~ /^us$/i);
328     $target     = $who          if ($target =~ /^(me|myself)$/i);
329
330     &status("tell: target = $target, query = $query");  
331
332     # "intrusive".
333 #    if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
334 #       &msg($who, "No, $target is not in any of my chans.");
335 #       return;
336 #    }
337
338     # self.
339     if ($target =~  /^\Q$ident\E$/i) {
340         &msg($who, "Isn't that a bit silly?");
341         return;
342     }
343
344     my $oldwho          = $who;
345     my $oldmtype        = $msgType;
346     $who                = $target;
347     my $result = &doQuestion($tell_obj);
348         # ^ returns '0' if nothing was found.
349     $who                = $oldwho;
350
351     # no such factoid.
352     if (!defined $result || $result =~ /^0?$/) {
353         $who            = $target;
354         $msgType        = "private";
355
356         # support command redirection.
357         # recursive cmdHooks aswell :)
358         my $done = 0;
359         $done++ if &parseCmdHook("main", $tell_obj);
360         $done++ if &parseCmdHook("extra", $tell_obj);
361         $message        = $tell_obj;
362         $done++ unless (&Modules());
363
364         &VERB("tell: setting old values of who and msgType.",2);
365         $who            = $oldwho;
366         $msgType        = $oldmtype;
367
368         if ($done) {
369             &msg($who, "told $target about CMD '$tell_obj'");
370         } else {
371             &msg($who, "i dunno what is '$tell_obj'.");
372         }
373
374         return;
375     }
376
377     # success.
378     &status("tell: <$who> telling $target about $tell_obj.");
379     if ($who ne $target) {
380         if ($dont_tell_me) {
381             &msg($who, "told $target about $tell_obj.");
382         } else {
383             &msg($who, "told $target about $tell_obj ($result)");
384         }
385
386         $reply = "$who wants you to know: $result";
387     } else {
388         $reply = "telling yourself: $result";
389     }
390
391     &msg($target, $reply);
392 }
393
394 sub DNS {
395     my $dns = shift;
396     my($match, $x, $y, $result);
397     my $pid;
398     $dns =~ s/^\s+|\s+$//g;
399
400     if (!defined $dns or $dns =~ /^\s*$/ or $dns =~ / /) {
401         &help("dns");
402         return;
403     }
404
405     if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) {
406         $match = $1;
407         &status("DNS query by IP address: $match");
408
409         $y = pack('C4', split(/\./, $match));
410         $x = (gethostbyaddr($y, &AF_INET));
411
412         if ($x !~ /^\s*$/) {
413             $result = $match." is ".$x unless ($x =~ /^\s*$/);
414         } else {
415             $result = "I can't seem to find that address in DNS";
416         }
417
418     } else {
419
420         &status("DNS query by name: $dns");
421         $x = join('.',unpack('C4',(gethostbyname($dns))[4]));
422
423         if ($x !~ /^\s*$/) {
424             $result = $dns." is ".$x;
425         } else {
426             $result = "I can\'t find that machine name";
427         }
428     }
429
430     &performReply($result);
431 }
432
433 sub countryStats {
434     if (exists $cache{countryStats}) {
435         &msg($who,"countrystats is already running!");
436         return;
437     }
438
439     if ($chan eq "") {
440         $chan = $_[0];
441     }
442
443     if ($chan eq "") {
444         &help("countrystats");
445         return;
446     }
447
448     $conn->who($chan);
449     $cache{countryStats}{chan}  = $chan;
450     $cache{countryStats}{mtype} = $msgType;
451     $cache{countryStats}{who}   = $who;
452     $cache{on_who_Hack}         = 1;
453 }
454
455 sub do_countrystats {
456     $chan       = $cache{countryStats}{chan};
457     $msgType    = $cache{countryStats}{mtype};
458     $who        = $cache{countryStats}{who};
459
460     my $total   = 0;
461     my %cstats;
462     foreach (keys %{ $cache{nuhInfo} }) {
463         my $h = $cache{nuhInfo}{$_}{Host};
464
465         if ($h =~ /^.*\.(\D+)$/) {      # host
466             $cstats{$1}++;
467         } else {                        # ip
468             $cstats{unresolve}++;
469         }
470         $total++;
471     }
472     my %count;
473     foreach (keys %cstats) {
474         $count{ $cstats{$_} }{$_} = 1;
475     }
476
477     my @list;
478     foreach (sort {$b <=> $a} keys %count) {
479         my $str = join(", ", sort keys %{ $count{$_} });
480 #       push(@list, "$str ($_)");
481         my $perc        = sprintf("%.01f", 100 * $_ / $total);
482         $perc           =~ s/\.0+$//;
483         push(@list, "$str ($_, $perc %)");
484     }
485
486     # todo: move this into a scheduler like nickometer
487     $msgType    = "private";
488     &pSReply( &formListReply(0, "Country Stats ", @list) );
489
490     delete $cache{countryStats};
491     delete $cache{on_who_Hack};
492 }
493
494 ###
495 ### amalgamated commands.
496 ###
497
498 sub userCommands {
499     # conversion: ascii.
500     if ($message =~ /^(asci*|chr) (\d+)$/) {
501         &DEBUG("ascii/chr called ...");
502         return unless (&hasParam("allowConv"));
503
504         &DEBUG("ascii/chr called");
505
506         $arg    = $2;
507         $result = chr($arg);
508         $result = "NULL"        if ($arg == 0);
509
510         &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
511
512         return;
513     }
514
515     # conversion: ord.
516     if ($message =~ /^ord(\s+(.*))$/) {
517         return unless (&hasParam("allowConv"));
518
519         $arg = $2;
520
521         if (!defined $arg or length $arg != 1) {
522             &help("ord");
523             return;
524         }
525
526         if (ord($arg) < 32) {
527             $arg = chr(ord($arg) + 64);
528             if ($arg eq chr(64)) {
529                 $arg = 'NULL';
530             } else {
531                 $arg = '^'.$arg;
532             }
533         }
534
535         &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
536         return;
537     }
538
539     # hex.
540     if ($message =~ /^hex(\s+(.*))?$/i) {
541         return unless (&hasParam("allowConv"));
542         my $arg = $2;
543
544         if (!defined $arg) {
545             &help("hex");
546             return;
547         }
548
549         if (length $arg > 80) {
550             &msg($who, "Too long.");
551             return;
552         }
553
554         my $retval;
555         foreach (split //, $arg) {
556             $retval .= sprintf(" %X", ord($_));
557         }
558
559         &pSReply("$arg is$retval");
560
561         return;
562     }
563
564     # crypt.
565     if ($message =~ /^crypt(\s+(.*))?$/i) {
566         my @args        = split /\s+/, $2;
567
568         if (!scalar @args or scalar @args > 2) {
569             &help("crypt");
570             return;
571         }
572
573         if (scalar @args == 2) {
574             if (length $args[0] != 2) {
575                 &msg($who, "invalid format...");
576                 return;
577             }
578
579             &pSReply( crypt($args[1], $args[0]) );
580         } else {
581             &pSReply( &mkcrypt($args[0]) );
582         }
583
584         return;
585     }
586
587     # cycle.
588     if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
589         return unless (&hasFlag("o"));
590         my $chan = lc $3;
591
592         if ($chan eq "") {
593             if ($msgType =~ /public/) {
594                 $chan = $talkchannel;
595                 &DEBUG("cycle: setting chan to '$chan'.");
596             } else {
597                 &help("cycle");
598                 return;
599             }
600         }
601
602         if (&validChan($chan) == 0) {
603             &msg($who,"error: invalid channel \002$chan\002");
604             return;
605         }
606
607         &msg($chan, "I'm coming back. (courtesy of $who)");
608         &part($chan);
609 ###     &ScheduleThis(5, "getNickInUse") if (@_);
610         &status("Schedule rejoin in 5secs to $chan by $who.");
611         $conn->schedule(5, sub { &joinchan($chan); });
612
613         return;
614     }
615
616     # reload.
617     if ($message =~ /^reload$/i) {
618         return unless (&hasFlag("n"));
619
620         &status("USER reload $who");
621         &pSReply("reloading...");
622         &reloadAllModules();
623         &pSReply("reloaded.");
624         return;
625     }
626
627     # redir.
628     if ($message =~ /^redir(\s+(.*))?/i) {
629         return unless (&hasFlag("o"));
630         my $factoid = $2;
631
632         if (!defined $factoid) {
633             &help("redir");
634             return;
635         }
636
637         my $val  = &getFactInfo($factoid, "factoid_value");
638         if (!defined $val or $val eq "") {
639             &msg($who, "error: '$factoid' does not exist.");
640             return;
641         }
642         &DEBUG("val => '$val'.");
643         my @list = &searchTable("factoids", "factoid_key",
644                                         "factoid_value", "^$val\$");
645
646         if (scalar @list == 1) {
647             &msg($who, "hrm... '$factoid' is unique.");
648             return;
649         }
650         if (scalar @list > 5) {
651             &msg($who, "A bit too many factoids to be redirected, hey?");
652             return;
653         }
654
655         my @redir;
656         &status("Redirect '$factoid' (". ($#list) .")...");
657         for (@list) {
658             my $x = $_;
659             next if (/^\Q$factoid\E$/i);
660
661             &status("  Redirecting '$_'.");
662             my $was = &getFactoid($_);
663             if ($was =~ /<REPLY> see/i) {
664                 &status("warn: not redirecting a redirection.");
665                 next;
666             }
667
668             &DEBUG("  was '$was'.");
669             push(@redir,$x);
670             &setFactInfo($x, "factoid_value", "<REPLY> see $factoid");
671         }
672         &status("Done.");
673
674         &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
675
676         return;
677     }
678
679     # rot13 it.
680     if ($message =~ /^rot13(\s+(.*))?/i) {
681         my $reply = $2;
682
683         if (!defined $reply) {
684             &help("rot13");
685             return;
686         }
687
688         $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
689         &pSReply($reply);
690
691         return;
692     }
693
694     # cpustats.
695     if ($message =~ /^cpustats$/i) {
696         if ($^O !~ /linux/) {
697             &ERROR("cpustats: your OS is not supported yet.");
698             return;
699         }
700
701         ### poor method to get info out of file, please fix.
702         open(STAT,"/proc/$$/stat");
703         my $line = <STAT>;
704         chop $line;
705         my @data = split(/ /, $line);
706         close STAT;
707
708         # utime(13) + stime(14).
709         my $cpu_usage   = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
710         # cutime(15) + cstime (16).
711         my $cpu_usage2  = sprintf("%.01f", ($data[15]+$data[16]) / 100 );
712         my $time        = time() - $^T;
713         my $raw_perc    = $cpu_usage*100/$time;
714         my $raw_perc2   = $cpu_usage2*100/$time;
715         my $perc;
716         my $perc2;
717         my $total;
718         my $ratio;
719
720         if ($raw_perc > 1) {
721             $perc       = sprintf("%.01f", $raw_perc);
722             $perc2      = sprintf("%.01f", $raw_perc2);
723             $total      = sprintf("%.01f", $raw_perc+$raw_perc2);
724         } elsif ($raw_perc > 0.1) {
725             $perc       = sprintf("%.02f", $raw_perc);
726             $perc2      = sprintf("%.02f", $raw_perc2);
727             $total      = sprintf("%.02f", $raw_perc+$raw_perc2);
728         } else {                        # <=0.1
729             $perc       = sprintf("%.03f", $raw_perc);
730             $perc2      = sprintf("%.03f", $raw_perc2);
731             $total      = sprintf("%.03f", $raw_perc+$raw_perc2);
732         }
733         $ratio  = sprintf("%.01f", 100*$perc/($perc+$perc2) );
734
735         &pSReply("Total CPU usage: \002$cpu_usage\002 s ... ".
736                 "Total used: \002$total\002 % ".
737                 "(parent/child ratio: $ratio %)"
738         );
739
740         return;
741     }
742
743     # ircstats.
744     if ($message =~ /^ircstats?$/i) {
745         $ircstats{'TotalTime'}  ||= 0;
746         $ircstats{'OffTime'}    ||= 0;
747
748         my $count       = $ircstats{'ConnectCount'};
749         my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
750         my $total_time  = time() - $ircstats{'ConnectTime'} +
751                                 $ircstats{'TotalTime'};
752         my $reply;
753
754         my $connectivity = 100 * ($total_time - $ircstats{'OffTime'}) /
755                                 $total_time;
756         my $p = sprintf("%.03f", $connectivity);
757         $p =~ s/(\.\d*)0+$/$1/;
758         if ($p =~ s/\.0$//) {
759             # this should not happen... but why...
760         } else {
761             $p =~ s/\.$//
762         }
763
764         if ($total_time != (time() - $ircstats{'ConnectTime'}) ) {
765             my $tt_format = &Time2String($total_time);
766             &DEBUG("tt_format => $tt_format");
767         }
768
769         ### RECONNECT COUNT.
770         if ($count == 1) {      # good.
771             $reply = "I'm connected to $ircstats{'Server'} and have been so".
772                 " for $format_time";
773         } else {
774             $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
775                 " for $format_time.  ".
776                 "I had to reconnect \002$count\002 times.".
777                 "   Connectivity: $p %";
778         }
779
780         ### REASON.
781         my $reason = $ircstats{'DisconnectReason'};
782         if (defined $reason) {
783             $reply .= ".  I was last disconnected for '$reason'.";
784         }
785
786         &pSReply($reply);
787                 
788         return;
789     }
790
791     # status.
792     if ($message =~ /^statu?s$/i) {
793         my $startString = scalar(gmtime $^T);
794         my $upString    = &Time2String(time() - $^T);
795         my $factoids    = &countKeys("factoids");
796
797         $count{'Commands'}      = 0;
798         foreach (keys %cmdstats) {
799             $count{'Commands'} += $cmdstats{$_};
800         }
801
802         &pSReply(
803         "Since $startString, there have been".
804           " \002$count{'Update'}\002 ".
805                 &fixPlural("modification", $count{'Update'}).
806           " and \002$count{'Question'}\002 ".
807                 &fixPlural("question",$count{'Question'}).
808           " and \002$count{'Dunno'}\002 ".
809                 &fixPlural("dunno",$count{'Dunno'}).
810           " and \002$count{'Moron'}\002 ".
811                 &fixPlural("moron",$count{'Moron'}).
812           " and \002$count{'Commands'}\002 ".
813                 &fixPlural("command",$count{'Commands'}).
814           ".  I have been awake for $upString this session, and ".
815           "currently reference \002$factoids\002 factoids.  ".
816           "I'm using about \002$memusage\002 ".
817           "kB of memory."
818         );
819
820         return;
821
822         # todo: use dbGetColNiceHash().
823         my %hash = &sqlSelectColHash("stats", "nick,counter",
824                 { type => "cmdstats" }, 1);
825 # does ORDER matter when used with a hash?
826 #                       " ORDER BY counter DESC", 1);
827
828 if (0) {
829         foreach (keys %hash) {
830             my $i = $_;
831             foreach (keys %{ $hash{$i} }) {
832                 &DEBUG("cmdstats: $hash{$i}{$_} = $_");
833             }
834         }
835         &DEBUG("end of cmdstats.");
836 }
837
838         return;
839     }
840
841     # wantNick. xk++
842     if ($message =~ /^wantNick$/i) {
843         if ($param{'ircNick'} eq $ident) {
844             &msg($who, "I hope you're right. I'll try anyway.");
845         }
846         &DEBUG("ircNick => $param{'ircNick'}");
847         &DEBUG("ident => $ident");
848
849         if (! &IsNickInAnyChan( $param{ircNick} ) ) {
850             my $str = "attempting to change nick to $param{'ircNick'}";
851             &status($str);
852             &msg($who, $str);
853             &nick($param{'ircNick'});
854         } else {
855             &msg($who, "hrm.. I shouldn't do it (BUG?) but doing it anyway!");
856             &DEBUG("wN: nick is somewhere... should try later.");
857             &nick($param{'ircNick'});
858         }
859
860         return;
861     }
862
863     return "CONTINUE";
864 }
865
866 1;