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