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 Cmdstats => 'Tell') );
35 &addCmdHook("main", 'news', ('CODEREF' => 'News::Parse',
36 Module => 'news', Identifier => 'news') );
38 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
41 ### Start of commands for hooks.
45 my $chan = lc shift(@_);
48 if ($chan eq "") { # all channels.
49 my $i = keys %channels;
50 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
51 my $tucount = 0; # total user count.
52 my $uucount = 0; # unique user count.
56 foreach (sort keys %channels) {
58 &status("chanstats: fe channels: chan == NULL.");
62 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
64 &pSReply($reply.": ".join(' ', @array));
67 foreach $chan (keys %channels) {
68 $tucount += scalar(keys %{$channels{$chan}{''}});
71 ### unique user count.
73 foreach $chan (keys %channels) {
74 foreach (keys %{ $channels{$chan}{''} }) {
75 next if (grep /^\Q$_\E$/, @nicks);
80 &DEBUG("nicks => '".scalar(@nicks)."'...");
81 if (scalar @nicks != $uucount) {
82 &DEBUG("nicks != uucount...");
85 my $chans = scalar(keys %channels);
87 "i've cached \002$tucount\002 ". &fixPlural("user",$tucount).
88 ", \002$uucount\002 unique ". &fixPlural("user",$uucount).
89 ", distributed over \002$chans\002 ".
90 &fixPlural("channel", $chans)."."
98 if (&validChan($chan) == 0) {
99 &msg($who,"error: invalid channel \002$chan\002");
105 foreach (sort keys %{$chanstats{$chan}}) {
106 my $int = $chanstats{$chan}{$_};
109 push(@array, "\002$int\002 ". &fixPlural($_,$int));
111 my $reply = "On \002$chan\002, there ".
112 &fixPlural("has",scalar(@array)). " been ".
115 # Step 1b: check channel inconstencies.
116 $chanstats{$chan}{'Join'} ||= 0;
117 $chanstats{$chan}{'SignOff'} ||= 0;
118 $chanstats{$chan}{'Part'} ||= 0;
120 my $delta_stats = $chanstats{$chan}{'Join'}
121 - $chanstats{$chan}{'SignOff'}
122 - $chanstats{$chan}{'Part'};
125 my $total = scalar(keys %{$channels{$chan}{''}});
126 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
128 if ($delta_stats > $total) {
129 &ERROR("chaninfo: delta_stats exceeds total users.");
136 foreach ("v","o","") {
137 my $int = scalar(keys %{$channels{$chan}{$_}});
140 $type = "Voice" if ($_ eq "v");
141 $type = "Opped" if ($_ eq "o");
142 $type = "Total" if ($_ eq "");
144 push(@array,"\002$int\002 $type");
146 $reply .= ". At the moment, ". &IJoin(@array);
149 ### TODO: what's wrong with the following?
150 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
151 my($count) = (sort { $b <=> $a } keys %new)[0];
153 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
158 # Command statistics.
162 if (!scalar(keys %cmdstats)) {
163 &performReply("no-one has run any commands yet");
168 foreach (keys %cmdstats) {
169 $countstats{$cmdstats{$_}}{$_} = 1;
172 foreach (sort {$b <=> $a} keys %countstats) {
176 foreach (keys %{$countstats{$int}}) {
177 push(@array, "\002$int\002 of $_");
180 &pSReply("command usage include ". &IJoin(@array).".");
183 # Factoid extension info. xk++
185 my $faqtoid = lc shift(@_);
188 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
189 &msg($who,"error: individual factoid info queries not supported as yet.");
190 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
197 &CmdFactInfo($faqtoid, $query);
201 my $type = shift(@_);
203 &Forker("factoids", sub {
204 &pSReply( &CmdFactStats($type) );
209 my $target = lc( shift || $who );
210 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
213 &pSReply("$target has karma of $karma");
215 &pSReply("$target has neutral karma");
222 if (! -x "/usr/bin/spell") {
223 &msg($who, "no binary found.");
227 if (!&validExec($query)) {
228 &msg($who,"argument appears to be fuzzy.");
232 my $reply = "I can't find alternate spellings for '$query'";
234 foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
236 last if !length; # end of query.
238 if (/^\@/) { # intro line.
240 } elsif (/^\*/) { # possibly correct.
241 $reply = "'$query' may be spelled correctly";
243 } elsif (/^\&/) { # possible correction(s).
244 s/^\& (\S+) \d+ \d+: //;
245 my @array = split(/,? /);
247 $reply = "possible spellings for $query: @array";
250 &DEBUG("spell: '+' found => '$_'.");
253 &DEBUG("spell: unknown: '$_'.");
262 &status("DNS Lookup: $query");
268 my ($target, $tell_obj) = ('','');
269 my $dont_tell_me = 0;
272 ### is this fixed elsewhere?
273 $args =~ s/\s+/ /g; # fix up spaces.
274 $args =~ s/^\s+|\s+$//g; # again.
276 # this one catches most of them
277 if ($args =~ /^(\S+) (-?)about (.*)$/i) {
280 $dont_tell_me = ($2) ? 1 : 0;
282 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
284 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
285 # i'm sure this could all be nicely collapsed
290 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
295 $query = "$qWord $verb $tell_obj";
297 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
303 # check target type. Deny channel targets.
304 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
305 &msg($who,"No, $who, I won't. (target invalid?)");
309 $target = $talkchannel if ($target =~ /^us$/i);
310 $target = $who if ($target =~ /^(me|myself)$/i);
312 &status("tell: target = $target, query = $query");
315 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
316 &msg($who, "No, $target is not in any of my chans.");
320 ### TODO: don't "tell" if sender is not in target's channel.
323 if ($target eq $ident) { # lc?
324 &msg($who, "Isn't that a bit silly?");
329 my $oldmtype = $msgType;
331 my $result = &doQuestion($tell_obj);
332 # ^ returns '0' if nothing was found.
336 if ($result =~ /^0?$/) {
338 $msgType = "private";
340 # support command redirection.
341 # recursive cmdHooks aswell :)
343 $done++ if &parseCmdHook("main", $tell_obj);
344 $done++ if &parseCmdHook("extra", $tell_obj);
345 $message = $tell_obj;
346 $done++ unless (&Modules());
348 &DEBUG("setting old values of who and msgType.");
350 $msgType = $oldmtype;
353 &msg($who, "told $target about CMD '$tell_obj'");
355 &msg($who, "i dunno what is '$tell_obj'.");
362 &status("tell: <$who> telling $target about $tell_obj.");
363 if ($who ne $target) {
365 &msg($who, "told $target about $tell_obj.");
367 &msg($who, "told $target about $tell_obj ($result)");
370 $reply = "$who wants you to know: $result";
372 $reply = "telling yourself: $result";
375 &msg($target, $reply);
380 my($match, $x, $y, $result);
383 if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) {
384 &status("DNS query by IP address: $in");
386 $y = pack('C4', split(/\./, $match));
387 $x = (gethostbyaddr($y, &AF_INET));
390 $result = $match." is ".$x unless ($x =~ /^\s*$/);
392 $result = "I can't seem to find that address in DNS";
395 &status("DNS query by name: $in");
396 $x = join('.',unpack('C4',(gethostbyname($in))[4]));
399 $result = $in." is ".$x;
401 $result = "I can\'t find that machine name";
405 &performReply($result);
410 ### amalgamated commands.
415 if ($message =~ /^(asci*|chr) (\d+)$/) {
416 return unless (&IsParam("allowConv"));
420 $result = "NULL" if ($arg == 0);
422 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
428 if ($message =~ /^ord (.)$/) {
429 return unless (&IsParam("allowConv"));
432 if (ord($arg) < 32) {
433 $arg = chr(ord($arg) + 64);
434 if ($arg eq chr(64)) {
441 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
446 if ($message =~ /^hex(\s+(.*))?$/i) {
447 return unless (&IsParam("allowConv"));
455 if (length $arg > 80) {
456 &msg($who, "Too long.");
461 foreach (split //, $arg) {
462 $retval .= sprintf(" %X", ord($_));
465 &pSReply("$arg is$retval");
471 if ($message =~ /^crypt(\s+(.*))?$/i) {
472 my @args = split /\s+/, $2;
474 if (!scalar @args or scalar @args > 2) {
479 if (scalar @args == 2) {
480 if (length $args[0] != 2) {
481 &msg($who, "invalid format...");
485 &pSReply( crypt($args[1], $args[0]) );
487 &pSReply( &mkcrypt($args[0]) );
494 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
495 return unless (&hasFlag("o"));
499 if ($msgType =~ /public/) {
500 $chan = $talkchannel;
501 &DEBUG("cycle: setting chan to '$chan'.");
508 if (&validChan($chan) == 0) {
509 &msg($who,"error: invalid channel \002$chan\002");
513 &msg($chan, "I'm coming back. (courtesy of $who)");
515 ### &ScheduleThis(5, "getNickInUse") if (@_);
516 &status("Schedule rejoin in 5secs to $chan by $who.");
517 $conn->schedule(5, sub { &joinchan($chan); });
523 if ($message =~ /^redir(\s+(.*))?/i) {
524 return unless (&hasFlag("o"));
527 if (!defined $factoid) {
532 my $val = &getFactInfo($factoid, "factoid_value");
533 if (!defined $val or $val eq "") {
534 &msg($who, "error: '$factoid' does not exist.");
537 &DEBUG("val => '$val'.");
538 my @list = &searchTable("factoids", "factoid_key",
539 "factoid_value", "^$val\$");
541 if (scalar @list == 1) {
542 &msg($who, "hrm... '$factoid' is unique.");
545 if (scalar @list > 5) {
546 &msg($who, "A bit too many factoids to be redirected, hey?");
551 &status("Redirect '$factoid' (". ($#list) .")...");
554 next if (/^\Q$factoid\E$/i);
556 &status(" Redirecting '$_'.");
557 my $was = &getFactoid($_);
558 if ($was =~ /<REPLY> see/i) {
559 &status("warn: not redirecting a redirection.");
563 &DEBUG(" was '$was'.");
565 &setFactInfo($x, "factoid_value", "<REPLY> see $factoid");
569 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
575 if ($message =~ /^rot13(\s+(.*))?/i) {
578 if (!defined $reply) {
583 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
590 if ($message =~ /^cpustats$/i) {
591 if ($^O !~ /linux/) {
592 &ERROR("cpustats: your OS is not supported yet.");
596 ### poor method to get info out of file, please fix.
597 open(STAT,"/proc/$$/stat");
600 my @data = split(/ /, $line);
603 # utime(13) + stime(14).
604 my $cpu_usage = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
605 my $time = time() - $^T;
606 my $raw_perc = $cpu_usage*100/$time;
610 $perc = sprintf("%.01f", $raw_perc);
611 } elsif ($raw_perc > 0.1) {
612 $perc = sprintf("%.02f", $raw_perc);
614 $perc = sprintf("%.03f", $raw_perc);
617 &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
618 &DEBUG("15 => $data[15] (cutime)");
619 &DEBUG("16 => $data[16] (cstime)");
625 if ($message =~ /^ircstats?$/i) {
626 $ircstats{'TotalTime'} ||= 0;
627 $ircstats{'OffTime'} ||= 0;
629 my $count = $ircstats{'ConnectCount'};
630 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
631 my $total_time = time() - $ircstats{'ConnectTime'} +
632 $ircstats{'TotalTime'};
635 my $connectivity = 100 * ($total_time - $ircstats{'OffTime'}) /
637 my $p = sprintf("%.02f", $connectivity);
638 $p =~ s/(\.\d*)0+$/$1/;
639 if ($p =~ s/\.0$//) {
640 &DEBUG("p sar not working properly :(");
645 &DEBUG("connectivity => $p %");
647 if ($total_time != (time() - $ircstats{'ConnectTime'}) ) {
648 my $tt_format = &Time2String($total_time);
649 &DEBUG("tt_format => $tt_format");
653 if ($count == 1) { # good.
654 $reply = "I'm connected to $ircstats{'Server'} and have been so".
657 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
658 " for $format_time. ".
659 "I had to reconnect \002$count\002 times.";
663 my $reason = $ircstats{'DisconnectReason'};
664 if (defined $reason) {
665 $reply .= " I was last disconnected for '$reason'.";
674 if ($message =~ /^statu?s$/i) {
675 my $startString = scalar(localtime $^T);
676 my $upString = &Time2String(time() - $^T);
677 my $count = &countKeys("factoids");
680 "Since $startString, there have been".
681 " \002$count{'Update'}\002 ".
682 &fixPlural("modification", $count{'Update'}).
683 " and \002$count{'Question'}\002 ".
684 &fixPlural("question",$count{'Question'}).
685 " and \002$count{'Dunno'}\002 ".
686 &fixPlural("dunno",$count{'Dunno'}).
687 " and \002$count{'Moron'}\002 ".
688 &fixPlural("moron",$count{'Moron'}).
689 ". I have been awake for $upString this session, and ".
690 "currently reference \002$count\002 factoids. ".
691 "I'm using about \002$memusage\002 ".
699 if ($message =~ /^wantNick$/i) {
700 if ($param{'ircNick'} eq $ident) {
701 &msg($who, "I hope you're right. I'll try anyway.");
704 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
705 my $str = "attempting to change nick to $param{'ircNick'}";
708 &nick($param{'ircNick'});
710 &msg($who, "hrm... can't do it");
711 &DEBUG("wN: nick is somewhere... should try later.");