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.
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);
79 my $chans = scalar(keys %channels);
81 "i've cached \002$tucount\002 ". &fixPlural("user",$tucount).
82 ", \002$uucount\002 unique ". &fixPlural("user",$uucount).
83 ", distributed over \002$chans\002 ".
84 &fixPlural("channel", $chans)."."
92 if (&validChan($chan) == 0) {
93 &msg($who,"error: invalid channel \002$chan\002");
99 foreach (sort keys %{$chanstats{$chan}}) {
100 my $int = $chanstats{$chan}{$_};
103 push(@array, "\002$int\002 ". &fixPlural($_,$int));
105 my $reply = "On \002$chan\002, there ".
106 &fixPlural("has",scalar(@array)). " been ".
109 # Step 1b: check channel inconstencies.
110 $chanstats{$chan}{'Join'} ||= 0;
111 $chanstats{$chan}{'SignOff'} ||= 0;
112 $chanstats{$chan}{'Part'} ||= 0;
114 my $delta_stats = $chanstats{$chan}{'Join'}
115 - $chanstats{$chan}{'SignOff'}
116 - $chanstats{$chan}{'Part'};
119 my $total = scalar(keys %{$channels{$chan}{''}});
120 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
122 if ($delta_stats > $total) {
123 &ERROR("chaninfo: delta_stats exceeds total users.");
130 foreach ("v","o","") {
131 my $int = scalar(keys %{$channels{$chan}{$_}});
134 $type = "Voice" if ($_ eq "v");
135 $type = "Opped" if ($_ eq "o");
136 $type = "Total" if ($_ eq "");
138 push(@array,"\002$int\002 $type");
140 $reply .= ". At the moment, ". &IJoin(@array);
143 ### TODO: what's wrong with the following?
144 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
145 my($count) = (sort { $b <=> $a } keys %new)[0];
147 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
152 # Command statistics.
156 if (!scalar(keys %cmdstats)) {
157 &performReply("no-one has run any commands yet");
162 foreach (keys %cmdstats) {
163 $countstats{$cmdstats{$_}}{$_} = 1;
166 foreach (sort {$b <=> $a} keys %countstats) {
170 foreach (keys %{$countstats{$int}}) {
171 push(@array, "\002$int\002 of $_");
174 &pSReply("command usage include ". &IJoin(@array).".");
177 # Factoid extension info. xk++
179 my $faqtoid = lc shift(@_);
182 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
183 &msg($who,"error: individual factoid info queries not supported as yet.");
184 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
191 &CmdFactInfo($faqtoid, $query);
195 my $type = shift(@_);
197 &Forker("factoids", sub {
198 &pSReply( &CmdFactStats($type) );
203 my $target = lc( shift || $who );
204 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
207 &pSReply("$target has karma of $karma");
209 &pSReply("$target has neutral karma");
216 if (! -x "/usr/bin/spell") {
217 &msg($who, "no binary found.");
221 if (!&validExec($query)) {
222 &msg($who,"argument appears to be fuzzy.");
226 my $reply = "I can't find alternate spellings for '$query'";
228 foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
230 last if !length; # end of query.
232 if (/^\@/) { # intro line.
234 } elsif (/^\*/) { # possibly correct.
235 $reply = "'$query' may be spelled correctly";
237 } elsif (/^\&/) { # possible correction(s).
238 s/^\& (\S+) \d+ \d+: //;
239 my @array = split(/,? /);
241 $reply = "possible spellings for $query: @array";
244 &DEBUG("spell: '+' found => '$_'.");
247 &DEBUG("spell: unknown: '$_'.");
256 &status("DNS Lookup: $query");
262 my ($target, $tell_obj) = ('','');
263 my $dont_tell_me = 0;
266 ### is this fixed elsewhere?
267 $args =~ s/\s+/ /g; # fix up spaces.
268 $args =~ s/^\s+|\s+$//g; # again.
270 # this one catches most of them
271 if ($args =~ /^(\S+) (-?)about (.*)$/i) {
274 $dont_tell_me = ($2) ? 1 : 0;
276 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
278 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
279 # i'm sure this could all be nicely collapsed
284 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
289 $query = "$qWord $verb $tell_obj";
291 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
297 # check target type. Deny channel targets.
298 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
299 &msg($who,"No, $who, I won't. (target invalid?)");
303 $target = $talkchannel if ($target =~ /^us$/i);
304 $target = $who if ($target =~ /^(me|myself)$/i);
306 &status("tell: target = $target, query = $query");
309 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
310 &msg($who, "No, $target is not in any of my chans.");
314 ### TODO: don't "tell" if sender is not in target's channel.
317 if ($target eq $ident) { # lc?
318 &msg($who, "Isn't that a bit silly?");
323 my $result = &doQuestion($tell_obj);
324 # ^ returns '0' if nothing was found.
327 if ($result =~ /^0?$/) {
329 my $oldmtype = $msgType;
331 $msgType = "private";
333 # support command redirection.
334 # recursive cmdHooks aswell :)
336 $done++ if &parseCmdHook("main", $tell_obj);
337 $done++ if &parseCmdHook("extra", $tell_obj);
338 $message = $tell_obj;
339 $done++ unless (&Modules());
341 &DEBUG("setting old values of who and msgType.");
343 $msgType = $oldmtype;
346 &msg($who, "told $target about CMD '$tell_obj'");
348 &msg($who, "i dunno what is '$tell_obj'.");
355 &status("tell: <$who> telling $target about $tell_obj.");
356 if ($who ne $target) {
358 &msg($who, "told $target about $tell_obj.");
360 &msg($who, "told $target about $tell_obj ($result)");
363 $reply = "$who wants you to know: $result";
365 $reply = "telling yourself: $result";
368 &msg($target, $reply);
373 my($match, $x, $y, $result);
376 if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) {
377 &status("DNS query by IP address: $in");
379 $y = pack('C4', split(/\./, $match));
380 $x = (gethostbyaddr($y, &AF_INET));
383 $result = $match." is ".$x unless ($x =~ /^\s*$/);
385 $result = "I can't seem to find that address in DNS";
388 &status("DNS query by name: $in");
389 $x = join('.',unpack('C4',(gethostbyname($in))[4]));
392 $result = $in." is ".$x;
394 $result = "I can\'t find that machine name";
398 &performReply($result);
403 ### amalgamated commands.
408 if ($message =~ /^(asci*|chr) (\d+)$/) {
409 return '' unless (&IsParam("allowConv"));
414 $result = "^".chr($arg);
418 $result = "NULL" if ($arg == 0);
420 &performReply( sprintf("ascii %s is '%s'", $arg, $result) );
425 if ($message =~ /^ord (.)$/) {
426 return '' unless (&IsParam("allowConv"));
429 if (ord($arg) < 32) {
430 $arg = chr(ord($arg) + 64);
431 if ($arg eq chr(64)) {
438 &performReply( sprintf("'%s' is ascii %s", $arg, ord $1) );
443 if ($message =~ /^hex(\s+(.*))?$/i) {
451 if (length $arg > 80) {
452 &msg($who, "Too long.");
457 foreach (split //, $arg) {
458 $retval .= sprintf(" %X", ord($_));
461 &pSReply("$arg is$retval");
467 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
469 &pSReply(crypt($1, $2));
476 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
477 return unless (&hasFlag("o"));
481 if ($msgType =~ /public/) {
482 $chan = $talkchannel;
483 &DEBUG("cycle: setting chan to '$chan'.");
490 if (&validChan($chan) == 0) {
491 &msg($who,"error: invalid channel \002$chan\002");
495 &msg($chan, "I'm coming back. (courtesy of $who)");
497 ### &ScheduleThis(5, "getNickInUse") if (@_);
498 &status("Schedule rejoin in 5secs to $chan by $who.");
499 $conn->schedule(5, sub { &joinchan($chan); });
505 if ($message =~ /^redir(\s+(.*))?/i) {
506 return unless (&hasFlag("o"));
509 if (!defined $factoid) {
514 my $val = &getFactInfo($factoid, "factoid_value");
515 if (!defined $val or $val eq "") {
516 &msg($who, "error: '$factoid' does not exist.");
519 &DEBUG("val => '$val'.");
520 my @list = &searchTable("factoids", "factoid_key",
521 "factoid_value", "^$val\$");
523 if (scalar @list == 1) {
524 &msg($who, "hrm... '$factoid' is unique.");
527 if (scalar @list > 5) {
528 &msg($who, "A bit too many factoids to be redirected, hey?");
533 &status("Redirect '$factoid' (". ($#list) .")...");
535 next if (/^\Q$factoid\E$/i);
537 &status(" Redirecting '$_'.");
538 my $was = &getFactoid($_);
539 &DEBUG(" was '$was'.");
541 &setFactInfo($_, "factoid_value", "<REPLY> see $factoid");
545 &msg($who, &formListReply(0, "'$factoid' is redirected to by '", @redir));
551 if ($message =~ /^rot13(\s+(.*))?/i) {
554 if (!defined $reply) {
559 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
566 if ($message =~ /^cpustats$/i) {
567 if ($^O !~ /linux/) {
568 &ERROR("cpustats: your OS is not supported yet.");
572 ### poor method to get info out of file, please fix.
573 open(STAT,"/proc/$$/stat");
576 my @data = split(/ /, $line);
579 # utime(13) + stime(14).
580 my $cpu_usage = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
581 my $time = time() - $^T;
582 my $raw_perc = $cpu_usage*100/$time;
586 $perc = sprintf("%.01f", $raw_perc);
587 } elsif ($raw_perc > 0.1) {
588 $perc = sprintf("%.02f", $raw_perc);
590 $perc = sprintf("%.03f", $raw_perc);
593 &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
594 &DEBUG("15 => $data[15] (cutime)");
595 &DEBUG("16 => $data[16] (cstime)");
601 if ($message =~ /^ircstats$/i) {
602 my $count = $ircstats{'ConnectCount'};
603 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
606 foreach (keys %ircstats) {
607 &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
611 if ($count == 1) { # good.
612 $reply = "I'm connected to $ircstats{'Server'} and have been so".
615 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
616 " for $format_time. ".
617 "I had to reconnect \002$count\002 times.";
621 my $reason = $ircstats{'DisconnectReason'};
622 if (defined $reason) {
623 $reply .= " I was last disconnected for '$reason'.";
632 if ($message =~ /^statu?s$/i) {
633 my $startString = scalar(localtime $^T);
634 my $upString = &Time2String(time() - $^T);
635 my $count = &countKeys("factoids");
638 "Since $startString, there have been".
639 " \002$count{'Update'}\002 ".
640 &fixPlural("modification", $count{'Update'}).
641 " and \002$count{'Question'}\002 ".
642 &fixPlural("question",$count{'Question'}).
643 " and \002$count{'Dunno'}\002 ".
644 &fixPlural("dunno",$count{'Dunno'}).
645 " and \002$count{'Moron'}\002 ".
646 &fixPlural("moron",$count{'Moron'}).
647 ". I have been awake for $upString this session, and ".
648 "currently reference \002$count\002 factoids. ".
649 "I'm using about \002$memusage\002 ".
657 if ($message =~ /^wantNick$/i) {
658 if ($param{'ircNick'} eq $ident) {
659 &msg($who, "I hope you're right. I'll try anyway.");
662 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
663 my $str = "attempting to change nick to $param{'ircNick'}";
666 &nick($param{'ircNick'});
668 &msg($who, "hrm... can't do it");
669 &DEBUG("wN: nick is somewhere... should try later.");