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', ) );
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.
48 my $i = keys %channels;
49 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
53 foreach (sort keys %channels) {
55 &status("chanstats: fe channels: chan == NULL.");
59 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
61 &pSReply($reply.": ".join(' ', @array));
64 foreach $chan (keys %channels) {
66 # TODO: use $mask{chan} instead?
68 &ERROR("bad channel: chan => '$chan'.");
70 $count += scalar(keys %{$channels{$chan}{''}});
73 "i've cached \002$count\002 ".&fixPlural("user",$count).
74 " distributed over \002".scalar(keys %channels)."\002 ".
75 &fixPlural("channel",scalar(keys %channels))."."
83 if (&validChan($chan) == 0) {
84 &msg($who,"error: invalid channel \002$chan\002");
90 foreach (sort keys %{$chanstats{$chan}}) {
91 my $int = $chanstats{$chan}{$_};
94 push(@array, "\002$int\002 ". &fixPlural($_,$int));
96 my $reply = "On \002$chan\002, there ".
97 &fixPlural("has",scalar(@array)). " been ".
100 # Step 1b: check channel inconstencies.
101 $chanstats{$chan}{'Join'} ||= 0;
102 $chanstats{$chan}{'SignOff'} ||= 0;
103 $chanstats{$chan}{'Part'} ||= 0;
105 my $delta_stats = $chanstats{$chan}{'Join'}
106 - $chanstats{$chan}{'SignOff'}
107 - $chanstats{$chan}{'Part'};
110 my $total = scalar(keys %{$channels{$chan}{''}});
111 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
113 if ($delta_stats > $total) {
114 &ERROR("chaninfo: delta_stats exceeds total users.");
121 foreach ("v","o","") {
122 my $int = scalar(keys %{$channels{$chan}{$_}});
125 $type = "Voice" if ($_ eq "v");
126 $type = "Opped" if ($_ eq "o");
127 $type = "Total" if ($_ eq "");
129 push(@array,"\002$int\002 $type");
131 $reply .= ". At the moment, ". &IJoin(@array);
134 ### TODO: what's wrong with the following?
135 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
136 my($count) = (sort { $b <=> $a } keys %new)[0];
138 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
143 # Command statistics.
147 if (!scalar(keys %cmdstats)) {
148 &performReply("no-one has run any commands yet");
153 foreach (keys %cmdstats) {
154 $countstats{$cmdstats{$_}}{$_} = 1;
157 foreach (sort {$b <=> $a} keys %countstats) {
161 foreach (keys %{$countstats{$int}}) {
162 push(@array, "\002$int\002 of $_");
165 &pSReply("command usage include ". &IJoin(@array).".");
168 # Factoid extension info. xk++
170 my $faqtoid = lc shift(@_);
173 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
174 &msg($who,"error: individual factoid info queries not supported as yet.");
175 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
182 &CmdFactInfo($faqtoid, $query);
186 my $type = shift(@_);
188 &Forker("factoids", sub {
189 &pSReply( &CmdFactStats($type) );
194 my $target = lc( shift || $who );
195 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
198 &pSReply("$target has karma of $karma");
200 &pSReply("$target has neutral karma");
207 if (! -x "/usr/bin/spell") {
208 &msg($who, "no binary found.");
212 if (!&validExec($query)) {
213 &msg($who,"argument appears to be fuzzy.");
217 my $reply = "I can't find alternate spellings for '$query'";
219 foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
221 last if !length; # end of query.
223 if (/^\@/) { # intro line.
225 } elsif (/^\*/) { # possibly correct.
226 $reply = "'$query' may be spelled correctly";
228 } elsif (/^\&/) { # possible correction(s).
229 s/^\& (\S+) \d+ \d+: //;
230 my @array = split(/,? /);
232 $reply = "possible spellings for $query: @array";
235 &DEBUG("spell: '+' found => '$_'.");
238 &DEBUG("spell: unknown: '$_'.");
247 &status("DNS Lookup: $query");
253 my ($target, $tell_obj) = ('','');
254 my $dont_tell_me = 0;
257 ### is this fixed elsewhere?
258 $args =~ s/\s+/ /g; # fix up spaces.
259 $args =~ s/^\s+|\s+$//g; # again.
261 # this one catches most of them
262 if ($args =~ /^(\S+) (-?)about (.*)$/i) {
265 $dont_tell_me = ($2) ? 1 : 0;
267 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
269 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
270 # i'm sure this could all be nicely collapsed
275 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
280 $query = "$qWord $verb $tell_obj";
282 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
288 # check target type. Deny channel targets.
289 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
290 &msg($who,"No, $who, I won't. (target invalid?)");
294 $target = $talkchannel if ($target =~ /^us$/i);
295 $target = $who if ($target =~ /^(me|myself)$/i);
297 &status("target: $target query: $query");
300 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
301 &msg($who, "No, $target is not in any of my chans.");
305 ### TODO: don't "tell" if sender is not in target's channel.
308 if ($target eq $ident) { # lc?
309 &msg($who, "Isn't that a bit silly?");
314 my $result = &doQuestion($tell_obj);
315 &DEBUG("result => $result.");
316 # return if ($result eq);
320 &msg($who, "i dunno what is '$tell_obj'.");
325 &status("tell: <$who> telling $target about $tell_obj.");
326 if ($who ne $target) {
327 &msg($who, "told $target about $tell_obj ($result)")
328 unless ($dont_tell_me);
329 $reply = "$who wants you to know: $result";
331 $reply = "telling yourself: $result";
334 &msg($target, $reply);
339 my($match, $x, $y, $result);
342 if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) {
343 &status("DNS query by IP address: $in");
345 $y = pack('C4', split(/\./, $match));
346 $x = (gethostbyaddr($y, &AF_INET));
349 $result = $match." is ".$x unless ($x =~ /^\s*$/);
351 $result = "I can't seem to find that address in DNS";
354 &status("DNS query by name: $in");
355 $x = join('.',unpack('C4',(gethostbyname($in))[4]));
358 $result = $in." is ".$x;
360 $result = "I can\'t find that machine name";
364 &performReply($result);
369 ### amalgamated commands.
374 if ($message =~ /^(asci*|chr) (\d+)$/) {
375 return '' unless (&IsParam("allowConv"));
380 $result = "^".chr($arg);
384 $result = "NULL" if ($arg == 0);
386 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
391 if ($message =~ /^ord (.)$/) {
392 return '' unless (&IsParam("allowConv"));
395 if (ord($arg) < 32) {
396 $arg = chr(ord($arg) + 64);
397 if ($arg eq chr(64)) {
404 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
409 if ($message =~ /^hex(\s+(.*))?$/i) {
417 if (length $arg > 80) {
418 &msg($who, "Too long.");
423 foreach (split //, $arg) {
424 $retval .= sprintf(" %X", ord($_));
427 &pSReply("$arg is$retval");
433 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
435 &pSReply(crypt($1, $2));
442 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
443 return unless (&hasFlag("o"));
447 if ($msgType =~ /public/) {
448 $chan = $talkchannel;
449 &DEBUG("cycle: setting chan to '$chan'.");
456 if (&validChan($chan) == 0) {
457 &msg($who,"error: invalid channel \002$chan\002");
461 &msg($chan, "I'm coming back. (courtesy of $who)");
463 ### &ScheduleThis(5, "getNickInUse") if (@_);
464 &status("Schedule rejoin in 5secs to $chan by $who.");
465 $conn->schedule(5, sub { &joinchan($chan); });
471 if ($message =~ /^redir(\s+(.*))?/i) {
472 return unless (&hasFlag("o"));
475 if (!defined $factoid) {
480 my $val = &getFactInfo($factoid, "factoid_value");
481 if (!defined $val or $val eq "") {
482 &msg($who, "error: '$factoid' does not exist.");
485 &DEBUG("val => '$val'.");
486 my @list = &searchTable("factoids", "factoid_key",
487 "factoid_value", "^$val\$");
489 if (scalar @list == 1) {
490 &msg($who, "hrm... '$factoid' is unique.");
493 if (scalar @list > 5) {
494 &msg($who, "A bit too many factoids to be redirected, hey?");
499 &status("Redirect '$factoid' (". ($#list) .")...");
501 next if (/^\Q$factoid\E$/i);
503 &status(" Redirecting '$_'.");
504 my $was = &getFactoid($_);
505 &DEBUG(" was '$was'.");
507 &setFactInfo($_, "factoid_value", "<REPLY> see $factoid");
511 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
517 if ($message =~ /^rot13(\s+(.*))?/i) {
520 if (!defined $reply) {
525 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
532 if ($message =~ /^cpustats$/i) {
533 if ($^O !~ /linux/) {
534 &ERROR("cpustats: your OS is not supported yet.");
538 ### poor method to get info out of file, please fix.
539 open(STAT,"/proc/$$/stat");
542 my @data = split(/ /, $line);
545 # utime(13) + stime(14).
546 my $cpu_usage = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
547 my $time = time() - $^T;
548 my $raw_perc = $cpu_usage*100/$time;
552 $perc = sprintf("%.01f", $raw_perc);
553 } elsif ($raw_perc > 0.1) {
554 $perc = sprintf("%.02f", $raw_perc);
556 $perc = sprintf("%.03f", $raw_perc);
559 &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
560 &DEBUG("15 => $data[15] (cutime)");
561 &DEBUG("16 => $data[16] (cstime)");
567 if ($message =~ /^ircstats$/i) {
568 my $count = $ircstats{'ConnectCount'};
569 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
572 foreach (keys %ircstats) {
573 &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
577 if ($count == 1) { # good.
578 $reply = "I'm connected to $ircstats{'Server'} and have been so".
581 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
582 " for $format_time. ".
583 "I had to reconnect \002$count\002 times.";
587 my $reason = $ircstats{'DisconnectReason'};
588 if (defined $reason) {
589 $reply .= " I was last disconnected for '$reason'.";
598 if ($message =~ /^statu?s$/i) {
599 my $startString = scalar(localtime $^T);
600 my $upString = &Time2String(time() - $^T);
601 my $count = &countKeys("factoids");
604 "Since $startString, there have been".
605 " \002$count{'Update'}\002 ".
606 &fixPlural("modification", $count{'Update'}).
607 " and \002$count{'Question'}\002 ".
608 &fixPlural("question",$count{'Question'}).
609 " and \002$count{'Dunno'}\002 ".
610 &fixPlural("dunno",$count{'Dunno'}).
611 " and \002$count{'Moron'}\002 ".
612 &fixPlural("moron",$count{'Moron'}).
613 ". I have been awake for $upString this session, and ".
614 "currently reference \002$count\002 factoids. ".
615 "I'm using about \002$memusage\002 ".
623 if ($message =~ /^wantNick$/i) {
624 if ($param{'ircNick'} eq $ident) {
625 &msg($who, "I hope you're right. I'll try anyway.");
628 my $str = "attempting to change nick to $param{'ircNick'}";
632 &nick($param{'ircNick'});