2 # UserExtra.pl: User Commands, Public.
4 # Version: v0.2b (20000707)
8 if (&IsParam("useStrict")) { use strict; }
10 use vars qw($message $arg $qWord $verb $lobotomized);
11 use vars qw(%channels %chanstats %cmdstats);
14 ### Start of command hooks for UserExtra.
17 &addCmdHook("main", 'chan(stats|info)', ('CODEREF' => 'chaninfo', ) );
18 &addCmdHook("main", 'cmd(stats|info)', ('CODEREF' => 'cmdstats', ) );
19 &addCmdHook("main", 'factinfo', ('CODEREF' => 'factinfo',
20 'Cmdstats' => 'Factoid Info', Module => 'factoids', ) );
21 &addCmdHook("main", 'factstats?', ('CODEREF' => 'factstats',
22 'Cmdstats' => 'Factoid Statistics', Help => "factstats",
23 Forker => 1, 'Identifier' => 'factoids', ) );
24 &addCmdHook("main", 'help', ('CODEREF' => 'help',
25 'Cmdstats' => 'Help', ) );
26 &addCmdHook("main", 'karma', ('CODEREF' => 'karma', ) );
27 &addCmdHook("main", 'i?spell', ('CODEREF' => 'ispell',
28 Help => 'spell', Identifier => 'spell', ) );
29 &addCmdHook("main", 'd?nslookup', ('CODEREF' => 'DNS',
30 Help => 'nslookup', Identifier => 'allowDNS',
31 Forker => "NULL", ) );
32 &addCmdHook("main", 'tell|explain', ('CODEREF' => 'tell',
33 Help => 'tell', Identifier => 'allowTelling', ) );
34 &addCmdHook("main", 'news', ('CODEREF' => 'News::Parse', ) );
36 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
39 ### Start of commands for hooks.
43 my $chan = lc shift(@_);
46 if ($chan eq "") { # all channels.
47 my $i = keys %channels;
48 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
49 my $tucount = 0; # total user count.
50 my $uucount = 0; # unique user count.
54 foreach (sort keys %channels) {
56 &status("chanstats: fe channels: chan == NULL.");
60 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
62 &pSReply($reply.": ".join(' ', @array));
65 foreach $chan (keys %channels) {
66 $tucount += scalar(keys %{$channels{$chan}{''}});
69 ### unique user count.
71 foreach $chan (keys %channels) {
72 foreach (keys %{ $channels{$chan}{''} }) {
73 next if (grep /^\Q$_\E$/, @nicks);
78 &DEBUG("nicks => '".scalar(@nicks)."'...");
79 if (scalar @nicks != $uucount) {
80 &DEBUG("nicks != uucount...");
83 my $chans = scalar(keys %channels);
85 "i've cached \002$tucount\002 ". &fixPlural("user",$tucount).
86 ", \002$uucount\002 unique ". &fixPlural("user",$uucount).
87 ", distributed over \002$chans\002 ".
88 &fixPlural("channel", $chans)."."
96 if (&validChan($chan) == 0) {
97 &msg($who,"error: invalid channel \002$chan\002");
103 foreach (sort keys %{$chanstats{$chan}}) {
104 my $int = $chanstats{$chan}{$_};
107 push(@array, "\002$int\002 ". &fixPlural($_,$int));
109 my $reply = "On \002$chan\002, there ".
110 &fixPlural("has",scalar(@array)). " been ".
113 # Step 1b: check channel inconstencies.
114 $chanstats{$chan}{'Join'} ||= 0;
115 $chanstats{$chan}{'SignOff'} ||= 0;
116 $chanstats{$chan}{'Part'} ||= 0;
118 my $delta_stats = $chanstats{$chan}{'Join'}
119 - $chanstats{$chan}{'SignOff'}
120 - $chanstats{$chan}{'Part'};
123 my $total = scalar(keys %{$channels{$chan}{''}});
124 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
126 if ($delta_stats > $total) {
127 &ERROR("chaninfo: delta_stats exceeds total users.");
134 foreach ("v","o","") {
135 my $int = scalar(keys %{$channels{$chan}{$_}});
138 $type = "Voice" if ($_ eq "v");
139 $type = "Opped" if ($_ eq "o");
140 $type = "Total" if ($_ eq "");
142 push(@array,"\002$int\002 $type");
144 $reply .= ". At the moment, ". &IJoin(@array);
147 ### TODO: what's wrong with the following?
148 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
149 my($count) = (sort { $b <=> $a } keys %new)[0];
151 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
156 # Command statistics.
160 if (!scalar(keys %cmdstats)) {
161 &performReply("no-one has run any commands yet");
166 foreach (keys %cmdstats) {
167 $countstats{$cmdstats{$_}}{$_} = 1;
170 foreach (sort {$b <=> $a} keys %countstats) {
174 foreach (keys %{$countstats{$int}}) {
175 push(@array, "\002$int\002 of $_");
178 &pSReply("command usage include ". &IJoin(@array).".");
181 # Factoid extension info. xk++
183 my $faqtoid = lc shift(@_);
186 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
187 &msg($who,"error: individual factoid info queries not supported as yet.");
188 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
195 &CmdFactInfo($faqtoid, $query);
199 my $type = shift(@_);
201 &Forker("factoids", sub {
202 &pSReply( &CmdFactStats($type) );
207 my $target = lc( shift || $who );
208 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
211 &pSReply("$target has karma of $karma");
213 &pSReply("$target has neutral karma");
220 if (! -x "/usr/bin/spell") {
221 &msg($who, "no binary found.");
225 if (!&validExec($query)) {
226 &msg($who,"argument appears to be fuzzy.");
230 my $reply = "I can't find alternate spellings for '$query'";
232 foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
234 last if !length; # end of query.
236 if (/^\@/) { # intro line.
238 } elsif (/^\*/) { # possibly correct.
239 $reply = "'$query' may be spelled correctly";
241 } elsif (/^\&/) { # possible correction(s).
242 s/^\& (\S+) \d+ \d+: //;
243 my @array = split(/,? /);
245 $reply = "possible spellings for $query: @array";
248 &DEBUG("spell: '+' found => '$_'.");
251 &DEBUG("spell: unknown: '$_'.");
260 &status("DNS Lookup: $query");
266 my ($target, $tell_obj) = ('','');
267 my $dont_tell_me = 0;
270 ### is this fixed elsewhere?
271 $args =~ s/\s+/ /g; # fix up spaces.
272 $args =~ s/^\s+|\s+$//g; # again.
274 # this one catches most of them
275 if ($args =~ /^(\S+) (-?)about (.*)$/i) {
278 $dont_tell_me = ($2) ? 1 : 0;
280 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
282 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
283 # i'm sure this could all be nicely collapsed
288 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
293 $query = "$qWord $verb $tell_obj";
295 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
301 # check target type. Deny channel targets.
302 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
303 &msg($who,"No, $who, I won't. (target invalid?)");
307 $target = $talkchannel if ($target =~ /^us$/i);
308 $target = $who if ($target =~ /^(me|myself)$/i);
310 &status("tell: target = $target, query = $query");
313 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
314 &msg($who, "No, $target is not in any of my chans.");
318 ### TODO: don't "tell" if sender is not in target's channel.
321 if ($target eq $ident) { # lc?
322 &msg($who, "Isn't that a bit silly?");
327 my $oldmtype = $msgType;
329 my $result = &doQuestion($tell_obj);
330 # ^ returns '0' if nothing was found.
334 if ($result =~ /^0?$/) {
336 $msgType = "private";
338 # support command redirection.
339 # recursive cmdHooks aswell :)
341 $done++ if &parseCmdHook("main", $tell_obj);
342 $done++ if &parseCmdHook("extra", $tell_obj);
343 $message = $tell_obj;
344 $done++ unless (&Modules());
346 &DEBUG("setting old values of who and msgType.");
348 $msgType = $oldmtype;
351 &msg($who, "told $target about CMD '$tell_obj'");
353 &msg($who, "i dunno what is '$tell_obj'.");
360 &status("tell: <$who> telling $target about $tell_obj.");
361 if ($who ne $target) {
363 &msg($who, "told $target about $tell_obj.");
365 &msg($who, "told $target about $tell_obj ($result)");
368 $reply = "$who wants you to know: $result";
370 $reply = "telling yourself: $result";
373 &msg($target, $reply);
378 my($match, $x, $y, $result);
381 if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) {
382 &status("DNS query by IP address: $in");
384 $y = pack('C4', split(/\./, $match));
385 $x = (gethostbyaddr($y, &AF_INET));
388 $result = $match." is ".$x unless ($x =~ /^\s*$/);
390 $result = "I can't seem to find that address in DNS";
393 &status("DNS query by name: $in");
394 $x = join('.',unpack('C4',(gethostbyname($in))[4]));
397 $result = $in." is ".$x;
399 $result = "I can\'t find that machine name";
403 &performReply($result);
408 ### amalgamated commands.
413 if ($message =~ /^(asci*|chr) (\d+)$/) {
414 return unless (&IsParam("allowConv"));
418 $result = "NULL" if ($arg == 0);
420 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
426 if ($message =~ /^ord (.)$/) {
427 return unless (&IsParam("allowConv"));
430 if (ord($arg) < 32) {
431 $arg = chr(ord($arg) + 64);
432 if ($arg eq chr(64)) {
439 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
444 if ($message =~ /^hex(\s+(.*))?$/i) {
445 return unless (&IsParam("allowConv"));
453 if (length $arg > 80) {
454 &msg($who, "Too long.");
459 foreach (split //, $arg) {
460 $retval .= sprintf(" %X", ord($_));
463 &pSReply("$arg is$retval");
469 if ($message =~ /^crypt(\s+(.*))?$/i) {
470 my @args = split /\s+/, $2;
472 if (!scalar @args or scalar @args > 2) {
477 if (scalar @args == 2) {
478 if (length $args[0] != 2) {
479 &msg($who, "invalid format...");
483 &pSReply( crypt($args[1], $args[0]) );
485 &pSReply( &mkcrypt($args[0]) );
492 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
493 return unless (&hasFlag("o"));
497 if ($msgType =~ /public/) {
498 $chan = $talkchannel;
499 &DEBUG("cycle: setting chan to '$chan'.");
506 if (&validChan($chan) == 0) {
507 &msg($who,"error: invalid channel \002$chan\002");
511 &msg($chan, "I'm coming back. (courtesy of $who)");
513 ### &ScheduleThis(5, "getNickInUse") if (@_);
514 &status("Schedule rejoin in 5secs to $chan by $who.");
515 $conn->schedule(5, sub { &joinchan($chan); });
521 if ($message =~ /^redir(\s+(.*))?/i) {
522 return unless (&hasFlag("o"));
525 if (!defined $factoid) {
530 my $val = &getFactInfo($factoid, "factoid_value");
531 if (!defined $val or $val eq "") {
532 &msg($who, "error: '$factoid' does not exist.");
535 &DEBUG("val => '$val'.");
536 my @list = &searchTable("factoids", "factoid_key",
537 "factoid_value", "^$val\$");
539 if (scalar @list == 1) {
540 &msg($who, "hrm... '$factoid' is unique.");
543 if (scalar @list > 5) {
544 &msg($who, "A bit too many factoids to be redirected, hey?");
549 &status("Redirect '$factoid' (". ($#list) .")...");
552 next if (/^\Q$factoid\E$/i);
554 &status(" Redirecting '$_'.");
555 my $was = &getFactoid($_);
556 if ($was =~ /<REPLY> see/i) {
557 &status("warn: not redirecting a redirection.");
561 &DEBUG(" was '$was'.");
563 &setFactInfo($x, "factoid_value", "<REPLY> see $factoid");
567 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
573 if ($message =~ /^rot13(\s+(.*))?/i) {
576 if (!defined $reply) {
581 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
588 if ($message =~ /^cpustats$/i) {
589 if ($^O !~ /linux/) {
590 &ERROR("cpustats: your OS is not supported yet.");
594 ### poor method to get info out of file, please fix.
595 open(STAT,"/proc/$$/stat");
598 my @data = split(/ /, $line);
601 # utime(13) + stime(14).
602 my $cpu_usage = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
603 my $time = time() - $^T;
604 my $raw_perc = $cpu_usage*100/$time;
608 $perc = sprintf("%.01f", $raw_perc);
609 } elsif ($raw_perc > 0.1) {
610 $perc = sprintf("%.02f", $raw_perc);
612 $perc = sprintf("%.03f", $raw_perc);
615 &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
616 &DEBUG("15 => $data[15] (cutime)");
617 &DEBUG("16 => $data[16] (cstime)");
623 if ($message =~ /^ircstats?$/i) {
624 $ircstats{'TotalTime'} ||= 0;
625 $ircstats{'OffTime'} ||= 0;
627 my $count = $ircstats{'ConnectCount'};
628 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
629 my $total_time = time() - $ircstats{'ConnectTime'} +
630 $ircstats{'TotalTime'};
633 my $connectivity = 100 * ($total_time - $ircstats{'OffTime'}) /
635 my $p = sprintf("%.02f", $connectivity);
636 $p =~ s/(\.\d*)0+$/$1/;
637 if ($p =~ s/\.0$//) {
638 &DEBUG("p sar not working properly :(");
643 &DEBUG("connectivity => $p %");
645 if ($total_time != (time() - $ircstats{'ConnectTime'}) ) {
646 my $tt_format = &Time2String($total_time);
647 &DEBUG("tt_format => $tt_format");
651 if ($count == 1) { # good.
652 $reply = "I'm connected to $ircstats{'Server'} and have been so".
655 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
656 " for $format_time. ".
657 "I had to reconnect \002$count\002 times.";
661 my $reason = $ircstats{'DisconnectReason'};
662 if (defined $reason) {
663 $reply .= " I was last disconnected for '$reason'.";
672 if ($message =~ /^statu?s$/i) {
673 my $startString = scalar(localtime $^T);
674 my $upString = &Time2String(time() - $^T);
675 my $count = &countKeys("factoids");
678 "Since $startString, there have been".
679 " \002$count{'Update'}\002 ".
680 &fixPlural("modification", $count{'Update'}).
681 " and \002$count{'Question'}\002 ".
682 &fixPlural("question",$count{'Question'}).
683 " and \002$count{'Dunno'}\002 ".
684 &fixPlural("dunno",$count{'Dunno'}).
685 " and \002$count{'Moron'}\002 ".
686 &fixPlural("moron",$count{'Moron'}).
687 ". I have been awake for $upString this session, and ".
688 "currently reference \002$count\002 factoids. ".
689 "I'm using about \002$memusage\002 ".
697 if ($message =~ /^wantNick$/i) {
698 if ($param{'ircNick'} eq $ident) {
699 &msg($who, "I hope you're right. I'll try anyway.");
702 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
703 my $str = "attempting to change nick to $param{'ircNick'}";
706 &nick($param{'ircNick'});
708 &msg($who, "hrm... can't do it");
709 &DEBUG("wN: nick is somewhere... should try later.");