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", 'ignorelist', ('CODEREF' => 'ignorelist', ) );
28 &addCmdHook("main", 'i?spell', ('CODEREF' => 'ispell',
29 Help => 'spell', Identifier => 'spell', ) );
30 &addCmdHook("main", 'd?nslookup', ('CODEREF' => 'DNS',
31 Help => 'nslookup', Identifier => 'allowDNS',
32 Forker => "NULL", ) );
33 &addCmdHook("main", 'tell|explain', ('CODEREF' => 'tell',
34 Help => 'tell', Identifier => 'allowTelling', ) );
37 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
40 ### Start of commands for hooks.
44 my $chan = lc shift(@_);
47 if ($chan eq "") { # all channels.
49 my $i = keys %channels;
50 my $reply = "i am on \002$i\002 ".&fixPlural("channel",$i);
54 foreach (sort keys %channels) {
56 &status("chanstats: fe channels: chan == NULL.");
60 push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
62 &performStrictReply($reply.": ".join(' ', @array));
65 foreach $chan (keys %channels) {
67 # TODO: use $mask{chan} instead?
69 &ERROR("bad channel: chan => '$chan'.");
71 $count += scalar(keys %{$channels{$chan}{''}});
74 "i've cached \002$count\002 ".&fixPlural("user",$count).
75 " distributed over \002".scalar(keys %channels)."\002 ".
76 &fixPlural("channel",scalar(keys %channels))."."
84 if (&validChan($chan) == 0) {
85 &msg($who,"error: invalid channel \002$chan\002");
91 foreach (sort keys %{$chanstats{$chan}}) {
92 my $int = $chanstats{$chan}{$_};
95 push(@array, "\002$int\002 ". &fixPlural($_,$int));
97 my $reply = "On \002$chan\002, there ".
98 &fixPlural("has",scalar(@array)). " been ".
101 # Step 1b: check channel inconstencies.
102 $chanstats{$chan}{'Join'} ||= 0;
103 $chanstats{$chan}{'SignOff'} ||= 0;
104 $chanstats{$chan}{'Part'} ||= 0;
106 my $delta_stats = $chanstats{$chan}{'Join'}
107 - $chanstats{$chan}{'SignOff'}
108 - $chanstats{$chan}{'Part'};
111 my $total = scalar(keys %{$channels{$chan}{''}});
112 &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
114 if ($delta_stats > $total) {
115 &ERROR("chaninfo: delta_stats exceeds total users.");
122 foreach ("v","o","") {
123 my $int = scalar(keys %{$channels{$chan}{$_}});
126 $type = "Voice" if ($_ eq "v");
127 $type = "Opped" if ($_ eq "o");
128 $type = "Total" if ($_ eq "");
130 push(@array,"\002$int\002 $type");
132 $reply .= ". At the moment, ". &IJoin(@array);
135 ### TODO: what's wrong with the following?
136 my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
137 my($count) = (sort { $b <=> $a } keys %new)[0];
139 $reply .= ". \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
141 &performStrictReply("$reply.");
144 # Command statistics.
148 if (!scalar(keys %cmdstats)) {
149 &performReply("no-one has run any commands yet");
154 foreach (keys %cmdstats) {
155 $countstats{$cmdstats{$_}}{$_} = 1;
158 foreach (sort {$b <=> $a} keys %countstats) {
162 foreach (keys %{$countstats{$int}}) {
163 push(@array, "\002$int\002 of $_");
166 &performStrictReply("command usage include ". &IJoin(@array).".");
169 # Factoid extension info. xk++
171 my $faqtoid = lc shift(@_);
174 if ($faqtoid =~ /^\-(\S+)(\s+(.*))$/) {
175 &msg($who,"error: individual factoid info queries not supported as yet.");
176 &msg($who,"it's possible that the factoid mistakenly begins with '-'.");
183 &CmdFactInfo($faqtoid, $query);
187 my $type = shift(@_);
189 &Forker("factoids", sub {
190 &performStrictReply( &CmdFactStats($type) );
195 my $target = lc( shift || $who );
196 my $karma = &dbGet("karma", "nick",$target,"karma") || 0;
199 &performStrictReply("$target has karma of $karma");
201 &performStrictReply("$target has neutral karma");
206 &status("$who asked for the ignore list");
209 my $count = scalar(keys %ignoreList);
214 &performStrictReply("no one in the ignore list!!!");
218 foreach (sort keys %ignoreList) {
221 if ($ignoreList{$_} != 1) { # temporary ignore.
222 my $expire = $ignoreList{$_} - $time;
223 if (defined $expire and $expire < 0) {
224 &status("ignorelist: deleting $_.");
225 delete $ignoreList{$_};
227 $str = "$_ (". &Time2String($expire) .")";
235 if (scalar @array >= 8 or $counter == $count) {
236 &msg($who, &formListReply(0, "Ignore list ", @array) );
245 if (! -x "/usr/bin/spell") {
246 &msg($who, "no binary found.");
250 if (!&validExec($query)) {
251 &msg($who,"argument appears to be fuzzy.");
255 my $reply = "I can't find alternate spellings for '$query'";
257 foreach (`/bin/echo '$query' | /usr/bin/ispell -a -S`) {
259 last if !length; # end of query.
261 if (/^\@/) { # intro line.
263 } elsif (/^\*/) { # possibly correct.
264 $reply = "'$query' may be spelled correctly";
266 } elsif (/^\&/) { # possible correction(s).
267 s/^\& (\S+) \d+ \d+: //;
268 my @array = split(/,? /);
270 $reply = "possible spellings for $query: @array";
273 &DEBUG("spell: '+' found => '$_'.");
276 &DEBUG("spell: unknown: '$_'.");
280 &performStrictReply($reply);
285 &status("DNS Lookup: $query");
291 my ($target, $tell_obj) = ('','');
294 ### is this fixed elsewhere?
295 $args =~ s/\s+/ /g; # fix up spaces.
296 $args =~ s/^\s+|\s+$//g; # again.
298 # this one catches most of them
299 if ($args =~ /^(\S+) about (.*)$/i) {
303 $tell_obj = $who if ($tell_obj =~ /^(me|myself)$/i);
305 } elsif ($args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i) {
306 # i'm sure this could all be nicely collapsed
311 } elsif ($args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i) {
316 $query = "$qWord $verb $tell_obj";
318 } elsif ($args =~ /^(.*?) to (\S+)$/i) {
324 # check target type. Deny channel targets.
325 if ($target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/) {
326 &msg($who,"No, $who, I won't. (target invalid?)");
330 $target = $talkchannel if ($target =~ /^us$/i);
331 $target = $who if ($target =~ /^(me|myself)$/i);
333 &status("target: $target query: $query");
336 if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
337 &msg($who, "No, $target is not in any of my chans.");
341 ### TODO: don't "tell" if sender is not in target's channel.
344 if ($target eq $ident) { # lc?
345 &msg($who, "Isn't that a bit silly?");
350 my $result = &doQuestion($tell_obj);
351 return if ($result eq $noreply);
355 &msg($who, "i dunno what is '$tell_obj'.");
360 &status("tell: <$who> telling $target about $tell_obj.");
361 if ($who ne $target) {
362 &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 &performStrictReply("$arg is$retval");
467 if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
469 &performStrictReply(crypt($1, $2));
476 if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
477 return $noreply 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 $noreply 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) {
559 $reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
560 &performStrictReply($reply);
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 $perc = sprintf("%.01f", $cpu_usage*100/$time );
584 &performStrictReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
585 &DEBUG("15 => $data[15] (cutime)");
586 &DEBUG("16 => $data[16] (cstime)");
592 if ($message =~ /^ircstats$/i) {
593 my $count = $ircstats{'ConnectCount'};
594 my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
597 foreach (keys %ircstats) {
598 &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
602 if ($count == 1) { # good.
603 $reply = "I'm connected to $ircstats{'Server'} and have been so".
606 $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
607 " for $format_time. ".
608 "I had to reconnect \002$count\002 times.";
612 my $reason = $ircstats{'DisconnectReason'};
613 if (defined $reason) {
614 $reply .= " I was last disconnected for '$reason'.";
617 &performStrictReply($reply);
623 if ($message =~ /^statu?s$/i) {
624 my $startString = scalar(localtime $^T);
625 my $upString = &Time2String(time() - $^T);
626 my $count = &countKeys("factoids");
629 "Since $startString, there have been".
630 " \002$count{'Update'}\002 ".
631 &fixPlural("modification", $count{'Update'}).
632 " and \002$count{'Question'}\002 ".
633 &fixPlural("question",$count{'Question'}).
634 " and \002$count{'Dunno'}\002 ".
635 &fixPlural("dunno",$count{'Dunno'}).
636 " and \002$count{'Moron'}\002 ".
637 &fixPlural("moron",$count{'Moron'}).
638 ". I have been awake for $upString this session, and ".
639 "currently reference \002$count\002 factoids. ".
640 "I'm using about \002$memusage\002 ".
648 if ($message =~ /^wantNick$/i) {
649 if ($param{'ircNick'} eq $ident) {
650 &msg($who, "I hope you're right. I'll try anyway.");
653 my $str = "attempting to change nick to $param{'ircNick'}";
657 &nick($param{'ircNick'});