]> git.donarmstrong.com Git - infobot.git/blob - src/UserExtra.pl
multiple spell's supported
[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             if (length $args[0] != 2) {
593                 &msg($who, "invalid format...");
594                 return;
595             }
596
597             &pSReply( crypt($args[1], $args[0]) );
598         } else {
599             &pSReply( &mkcrypt($args[0]) );
600         }
601
602         return;
603     }
604
605     # cycle.
606     if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
607         return unless (&hasFlag("o"));
608         my $chan = lc $3;
609
610         if ($chan eq "") {
611             if ($msgType =~ /public/) {
612                 $chan = $talkchannel;
613                 &DEBUG("cycle: setting chan to '$chan'.");
614             } else {
615                 &help("cycle");
616                 return;
617             }
618         }
619
620         if (&validChan($chan) == 0) {
621             &msg($who,"error: invalid channel \002$chan\002");
622             return;
623         }
624
625         &msg($chan, "I'm coming back. (courtesy of $who)");
626         &part($chan);
627 ###     &ScheduleThis(5, "getNickInUse") if (@_);
628         &status("Schedule rejoin in 5secs to $chan by $who.");
629         $conn->schedule(5, sub { &joinchan($chan); });
630
631         return;
632     }
633
634     # reload.
635     if ($message =~ /^reload$/i) {
636         return unless (&hasFlag("n"));
637
638         &status("USER reload $who");
639         &pSReply("reloading...");
640         my $modules = &reloadAllModules();
641         &pSReply("reloaded:$modules");
642         return;
643     }
644
645     # redir.
646     if ($message =~ /^redir(\s+(.*))?/i) {
647         return unless (&hasFlag("o"));
648         my $factoid = $2;
649
650         if (!defined $factoid) {
651             &help("redir");
652             return;
653         }
654
655         my $val  = &getFactInfo($factoid, "factoid_value");
656         if (!defined $val or $val eq "") {
657             &msg($who, "error: '$factoid' does not exist.");
658             return;
659         }
660         &DEBUG("val => '$val'.");
661         my @list = &searchTable("factoids", "factoid_key",
662                                         "factoid_value", "^$val\$");
663
664         if (scalar @list == 1) {
665             &msg($who, "hrm... '$factoid' is unique.");
666             return;
667         }
668         if (scalar @list > 5) {
669             &msg($who, "A bit too many factoids to be redirected, hey?");
670             return;
671         }
672
673         my @redir;
674         &status("Redirect '$factoid' (". ($#list) .")...");
675         for (@list) {
676             my $x = $_;
677             next if (/^\Q$factoid\E$/i);
678
679             &status("  Redirecting '$_'.");
680             my $was = &getFactoid($_);
681             if ($was =~ /<REPLY> see/i) {
682                 &status("warn: not redirecting a redirection.");
683                 next;
684             }
685
686             &DEBUG("  was '$was'.");
687             push(@redir,$x);
688             &setFactInfo($x, "factoid_value", "<REPLY> see $factoid");
689         }
690         &status("Done.");
691
692         &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
693
694         return;
695     }
696
697     # rot13 it.
698     if ($message =~ /^rot13(\s+(.*))?/i) {
699         my $reply = $2;
700
701         if (!defined $reply) {
702             &help("rot13");
703             return;
704         }
705
706         $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
707         &pSReply($reply);
708
709         return;
710     }
711
712     # cpustats.
713     if ($message =~ /^cpustats$/i) {
714         if ($^O !~ /linux/) {
715             &ERROR("cpustats: your OS is not supported yet.");
716             return;
717         }
718
719         ### poor method to get info out of file, please fix.
720         open(STAT,"/proc/$$/stat");
721         my $line = <STAT>;
722         chop $line;
723         my @data = split(/ /, $line);
724         close STAT;
725
726         # utime(13) + stime(14).
727         my $cpu_usage   = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
728         # cutime(15) + cstime (16).
729         my $cpu_usage2  = sprintf("%.01f", ($data[15]+$data[16]) / 100 );
730         my $time        = time() - $^T;
731         my $raw_perc    = $cpu_usage*100/$time;
732         my $raw_perc2   = $cpu_usage2*100/$time;
733         my $perc;
734         my $perc2;
735         my $total;
736         my $ratio;
737
738         if ($raw_perc > 1) {
739             $perc       = sprintf("%.01f", $raw_perc);
740             $perc2      = sprintf("%.01f", $raw_perc2);
741             $total      = sprintf("%.01f", $raw_perc+$raw_perc2);
742         } elsif ($raw_perc > 0.1) {
743             $perc       = sprintf("%.02f", $raw_perc);
744             $perc2      = sprintf("%.02f", $raw_perc2);
745             $total      = sprintf("%.02f", $raw_perc+$raw_perc2);
746         } else {                        # <=0.1
747             $perc       = sprintf("%.03f", $raw_perc);
748             $perc2      = sprintf("%.03f", $raw_perc2);
749             $total      = sprintf("%.03f", $raw_perc+$raw_perc2);
750         }
751         $ratio  = sprintf("%.01f", 100*$perc/($perc+$perc2) );
752
753         &pSReply("Total CPU usage: \002$cpu_usage\002 s ... ".
754                 "Total used: \002$total\002 % ".
755                 "(parent/child ratio: $ratio %)"
756         );
757
758         return;
759     }
760
761     # ircstats.
762     if ($message =~ /^ircstats?$/i) {
763         $ircstats{'TotalTime'}  ||= 0;
764         $ircstats{'OffTime'}    ||= 0;
765
766         my $count       = $ircstats{'ConnectCount'};
767         my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
768         my $total_time  = time() - $ircstats{'ConnectTime'} +
769                                 $ircstats{'TotalTime'};
770         my $reply;
771
772         my $connectivity = 100 * ($total_time - $ircstats{'OffTime'}) /
773                                 $total_time;
774         my $p = sprintf("%.03f", $connectivity);
775         $p =~ s/(\.\d*)0+$/$1/;
776         if ($p =~ s/\.0$//) {
777             # this should not happen... but why...
778         } else {
779             $p =~ s/\.$//
780         }
781
782         if ($total_time != (time() - $ircstats{'ConnectTime'}) ) {
783             my $tt_format = &Time2String($total_time);
784             &DEBUG("tt_format => $tt_format");
785         }
786
787         ### RECONNECT COUNT.
788         if ($count == 1) {      # good.
789             $reply = "I'm connected to $ircstats{'Server'} and have been so".
790                 " for $format_time";
791         } else {
792             $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
793                 " for $format_time.  ".
794                 "I had to reconnect \002$count\002 times.".
795                 "   Connectivity: $p %";
796         }
797
798         ### REASON.
799         my $reason = $ircstats{'DisconnectReason'};
800         if (defined $reason) {
801             $reply .= ".  I was last disconnected for '$reason'.";
802         }
803
804         &pSReply($reply);
805
806         return;
807     }
808
809     # status.
810     if ($message =~ /^statu?s$/i) {
811         my $startString = scalar(gmtime $^T);
812         my $upString    = &Time2String(time() - $^T);
813         my ($puser,$psystem,$cuser,$csystem) = times;
814         my $factoids    = &countKeys("factoids");
815         my $forks = 0;
816         foreach (keys %forked) {
817             $forks += scalar keys %{ $forked{$_} };
818         }
819         $forks /= 2;
820         $count{'Commands'}      = 0;
821         foreach (keys %cmdstats) {
822             $count{'Commands'} += $cmdstats{$_};
823         }
824
825         &pSReply(
826         "Since $startString, there have been".
827           " \002$count{'Update'}\002 ".
828                 &fixPlural("modification", $count{'Update'}).
829           ", \002$count{'Question'}\002 ".
830                 &fixPlural("question",$count{'Question'}).
831           ", \002$count{'Dunno'}\002 ".
832                 &fixPlural("dunno",$count{'Dunno'}).
833           ", \002$count{'Moron'}\002 ".
834                 &fixPlural("moron",$count{'Moron'}).
835           " and \002$count{'Commands'}\002 ".
836                 &fixPlural("command",$count{'Commands'}).
837           ".  I have been awake for $upString this session, and ".
838           "currently reference \002$factoids\002 factoids.  ".
839           "I'm using about \002$memusage\002 ".
840           "kB of memory. With \002$forks\002 active ".
841                 &fixPlural("fork",$forks).
842           ". Process time user/system $puser/$psystem child $cuser/$csystem"
843         );
844
845         return;
846
847         my %hash = &sqlSelectColHash("stats", "nick,counter",
848                 { type => "cmdstats" }, 1);
849 # ORDER won't be retained in a hash
850 #                       " ORDER BY counter DESC", 1);
851
852 if (0) {
853         foreach (keys %hash) {
854             my $i = $_;
855             foreach (keys %{ $hash{$i} }) {
856                 &DEBUG("cmdstats: $hash{$i}{$_} = $_");
857             }
858         }
859         &DEBUG("end of cmdstats.");
860 }
861
862         return;
863     }
864
865     # wantNick. xk++
866     # FIXME does not try to get nick "back", just switches nicks
867     if ($message =~ /^wantNick\s(.*)?$/i) {
868         return unless (&hasFlag("o"));
869         my $wantnick = lc $1;
870         my $mynick = $conn->nick();
871
872         if ($mynick eq $wantnick) {
873             &msg($who, "I hope you're right. I'll try anyway (mynick=$mynick, wantnick=$wantnick).");
874         }
875
876         # fallback check, I guess.  needed?
877         if (! &IsNickInAnyChan( $wantnick ) ) {
878             my $str = "attempting to change nick from $mynick to $wantnick";
879             &status($str);
880             &msg($who, $str);
881             &nick($wantnick);
882             return;
883         }
884
885         # idea from dondelecarlo :)
886         # TODO: use cache{nickserv}
887         if ($param{'nickServ_pass'}) {
888             my $str = "someone is using nick $wantnick; GHOSTing";
889             &status($str);
890             &msg($who, $str);
891             &msg("NickServ", "GHOST $wantnick $param{'nickServ_pass'}");
892
893             $conn->schedule(5, sub {
894                 &status("going to change nick from $mynick to $wantnick after GHOST.");
895                 &nick($wantnick);
896             } );
897
898             return;
899         }
900
901         return;
902     }
903
904     return "CONTINUE";
905 }
906
907 1;