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