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