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