2 # UserExtra.pl: User Commands, Public.
7 use vars qw($message $arg $qWord $verb $lobotomized $who $result $chan
8 $conn $msgType $query $talkchannel $ident $memusage);
9 use vars qw(%channels %chanstats %cmdstats %count %forked %ircstats %param
10 %cache %mask %userstats);
12 ### hooks get added in CommandHooks.pl.
15 ### Start of commands for hooks.
19 my $chan = lc shift(@_);
22 if ( $chan eq '' ) { # all channels.
23 my $i = keys %channels;
24 my $tucount = 0; # total user count.
25 my $uucount = 0; # unique user count.
30 foreach ( keys %channels ) {
31 if ( /^\s*$/ or / / ) {
32 &status('chanstats: fe channels: chan == NULL.');
37 next if (/^_default$/);
39 $chans{$_} = scalar( keys %{ $channels{$_}{''} } );
41 $tucount += scalar( keys %{ $channels{$_}{''} } );
43 foreach $chan ( sort { $chans{$b} <=> $chans{$a} } keys %chans ) {
44 push( @array, "$chan/" . $chans{$chan} );
47 ### unique user count.
49 foreach $chan ( keys %channels ) {
51 foreach $nick ( keys %{ $channels{$chan}{''} } ) {
55 $uucount = scalar( keys %nicks );
57 my $chans = scalar( keys %chans );
58 my $join = &ircCheck();
60 "\002$chans/".($chans+$join)."\002 " . &fixPlural('channel', $chans)
61 . ", \002$tucount\002 " . &fixPlural('user', $tucount)
62 . ", \002$uucount\002 unique: "
63 . join(', ', @array));
70 if ( &validChan($chan) == 0 ) {
71 &msg( $who, "error: invalid channel \002$chan\002" );
77 foreach ( sort keys %{ $chanstats{$chan} } ) {
78 my $int = $chanstats{$chan}{$_};
81 push( @array, "\002$int\002 " . &fixPlural( $_, $int ) );
84 "On \002$chan\002, there "
85 . &fixPlural( 'has', scalar(@array) )
89 # Step 1b: check channel inconstencies.
90 $chanstats{$chan}{'Join'} ||= 0;
91 $chanstats{$chan}{'SignOff'} ||= 0;
92 $chanstats{$chan}{'Part'} ||= 0;
94 my $delta_stats = $chanstats{$chan}{'Join'} - $chanstats{$chan}{'SignOff'} -
95 $chanstats{$chan}{'Part'};
98 my $total = scalar( keys %{ $channels{$chan}{''} } );
100 "chaninfo: join ~= signoff + part (drift of $delta_stats < $total)."
103 if ( $delta_stats > $total ) {
104 &ERROR('chaninfo: delta_stats exceeds total users.');
111 foreach ( 'v', 'o', '' ) {
112 my $int = scalar( keys %{ $channels{$chan}{$_} } );
115 $type = 'Voice' if ( $_ eq 'v' );
116 $type = 'Opped' if ( $_ eq 'o' );
117 $type = 'Total' if ( $_ eq '' );
119 push( @array, "\002$int\002 $type" );
121 $reply .= '. At the moment, ' . &IJoin(@array);
125 foreach ( keys %userstats ) {
126 next unless ( exists $userstats{$_}{'Count'} );
127 if ( $userstats{$_}{'Count'} =~ /^\D+$/ ) {
128 &WARN("userstats{$_}{Count} is non-digit.");
132 $new{$_} = $userstats{$_}{'Count'};
135 # TODO: show top 3 with percentages?
136 my ($count) = ( sort { $new{$b} <=> $new{$a} } keys %new )[0];
139 ". \002$count\002 has said the most with a total of \002$new{$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 includes ' . &IJoin(@array) . '.' );
169 # Command statistics.
171 my $reply = 'conninfo:';
173 foreach $key ( sort keys %::conns ) {
174 my $myconn = $::conns{$key};
176 next if (!defined $myconn);
177 my $mynick = $myconn->nick();
180 &performStrictReply( "conninfo: $reply.");
183 # Factoid extension info. xk++
185 my $faqtoid = lc shift(@_);
188 if ( $faqtoid =~ /^\-(\S+)(\s+(.*))$/ ) {
190 'error: individual factoid info queries not supported as yet.' );
192 "it's possible that the factoid mistakenly begins with '-'." );
199 &CmdFactInfo( $faqtoid, $query );
203 my $type = shift(@_);
208 &performStrictReply( &CmdFactStats($type) );
214 my $target = lc( shift || $who );
216 &sqlSelect( 'stats', 'counter', { nick => $target, type => 'karma' } )
220 &performStrictReply("$target has karma of $karma");
223 &performStrictReply("$target has neutral karma");
229 my ( $target, $tell_obj ) = ( '', '' );
230 my $dont_tell_me = 0;
233 ### is this fixed elsewhere?
234 $args =~ s/\s+/ /g; # fix up spaces.
235 $args =~ s/^\s+|\s+$//g; # again.
237 # this one catches most of them
238 if ( $args =~ /^(\S+) (-?)about (.*)$/i ) {
241 $dont_tell_me = ($2) ? 1 : 0;
243 $tell_obj = $who if ( $tell_obj =~ /^(me|myself)$/i );
246 elsif ( $args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i ) {
248 # i'm sure this could all be nicely collapsed
254 elsif ( $args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i ) {
259 $query = "$qWord $verb $tell_obj";
262 elsif ( $args =~ /^(.*?) to (\S+)$/i ) {
268 # check target type. Deny channel targets.
269 if ( $target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/ ) {
270 &msg( $who, "No, $who, I won't. (target invalid?)" );
274 $target = $talkchannel if ( $target =~ /^us$/i );
275 $target = $who if ( $target =~ /^(me|myself)$/i );
277 &status("tell: target = $target, query = $query");
280 # if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
281 # &msg($who, "No, $target is not in any of my chans.");
286 if ( $target =~ /^\Q$ident\E$/i ) {
287 &msg( $who, "Isn't that a bit silly?" );
292 my $oldmtype = $msgType;
294 my $result = &doQuestion($tell_obj);
296 # ^ returns '0' if nothing was found.
300 if ( !defined $result || $result =~ /^0?$/ ) {
302 $msgType = 'private';
304 # support command redirection.
305 # recursive cmdHooks aswell :)
307 $done++ if &parseCmdHook($tell_obj);
308 $message = $tell_obj;
309 $done++ unless ( &Modules() );
311 &VERB( 'tell: setting old values of who and msgType.', 2 );
313 $msgType = $oldmtype;
316 &msg( $who, "told $target about CMD '$tell_obj'" );
319 &msg( $who, "i dunno what is '$tell_obj'." );
326 &status("tell: <$who> telling $target about $tell_obj.");
327 if ( $who ne $target ) {
329 &msg( $who, "told $target about $tell_obj." );
332 &msg( $who, "told $target about $tell_obj ($result)" );
335 $reply = "$who wants you to know: $result";
338 $reply = "telling yourself: $result";
341 &msg( $target, $reply );
345 if ( exists $cache{countryStats} ) {
346 &msg( $who, 'countrystats is already running!' );
355 &help('countrystats');
360 $cache{countryStats}{chan} = $chan;
361 $cache{countryStats}{mtype} = $msgType;
362 $cache{countryStats}{who} = $who;
363 $cache{on_who_Hack} = 1;
366 sub do_countrystats {
367 $chan = $cache{countryStats}{chan};
368 $msgType = $cache{countryStats}{mtype};
369 $who = $cache{countryStats}{who};
373 foreach ( keys %{ $cache{nuhInfo} } ) {
374 my $h = $cache{nuhInfo}{$_}{Host};
376 if ( $h =~ /^.*\.(\D+)$/ ) { # host
380 $cstats{unresolve}++;
385 foreach ( keys %cstats ) {
386 $count{ $cstats{$_} }{$_} = 1;
390 foreach ( sort { $b <=> $a } keys %count ) {
391 my $str = join( ', ', sort keys %{ $count{$_} } );
393 #push(@list, "$str ($_)");
394 my $perc = sprintf( '%.01f', 100 * $_ / $total );
396 push( @list, "$str ($_, $perc %)" );
399 # TODO: move this into a scheduler
400 $msgType = 'private';
401 &performStrictReply( &formListReply( 0, 'Country Stats ', @list ) );
403 delete $cache{countryStats};
404 delete $cache{on_who_Hack};
408 ### amalgamated commands.
414 if ( $message =~ /^(asci*|chr) (\d+)$/ ) {
415 &DEBUG('ascii/chr called ...');
416 return unless ( &IsChanConfOrWarn('allowConv') );
418 &DEBUG('ascii/chr called');
422 $result = 'NULL' if ( $arg == 0 );
424 &performReply( sprintf( "ascii %s is '%s'", $arg, $result ) );
430 if ( $message =~ /^ord(\s+(.*))$/ ) {
431 return unless ( &IsChanConfOrWarn('allowConv') );
435 if ( !defined $arg or length $arg != 1 ) {
440 if ( ord($arg) < 32 ) {
441 $arg = chr( ord($arg) + 64 );
442 if ( $arg eq chr(64) ) {
450 &performReply( sprintf( "'%s' is ascii %s", $arg, ord $arg ) );
455 if ( $message =~ /^hex(\s+(.*))?$/i ) {
456 return unless ( &IsChanConfOrWarn('allowConv') );
459 if ( !defined $arg ) {
464 if ( length $arg > 80 ) {
465 &msg( $who, 'Too long.' );
470 foreach ( split //, $arg ) {
471 $retval .= sprintf( ' %X', ord($_) );
474 &performStrictReply("$arg is$retval");
480 if ( $message =~ /^crypt\s+(\S*)?\s*(.*)?$/i ) {
481 &status("crypt: $1:$2:$3");
483 &performStrictReply( crypt( $2, $1 ) );
486 &performStrictReply( &mkcrypt($1) );
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'.");
507 if ( &validChan($chan) == 0 ) {
508 &msg( $who, "error: invalid channel \002$chan\002" );
512 &msg( $chan, "I'm coming back. (courtesy of $who)" );
514 #&ScheduleThis(5, 'getNickInUse') if (@_);
515 &status("Schedule rejoin in 5secs to $chan by $who.");
516 $conn->schedule( 5, sub { &joinchan($chan); } );
522 if ( $message =~ /^reload$/i ) {
523 return unless ( &hasFlag('n') );
525 &status("USER reload $who");
526 &performStrictReply('reloading...');
529 my $modules = &reloadAllModules();
530 &performStrictReply("reloaded:$modules");
535 if ( $message =~ /^redir(\s+(.*))?/i ) {
536 return unless ( &hasFlag('o') );
539 if ( !defined $factoid ) {
544 my $val = &getFactInfo( $factoid, 'factoid_value' );
545 if ( !defined $val or $val eq '' ) {
546 &msg( $who, "error: '$factoid' does not exist." );
549 &DEBUG("val => '$val'.");
551 &searchTable( 'factoids', 'factoid_key', 'factoid_value', "^$val\$" );
553 if ( scalar @list == 1 ) {
554 &msg( $who, "hrm... '$factoid' is unique." );
557 if ( scalar @list > 5 ) {
558 &msg( $who, 'A bit too many factoids to be redirected, hey?' );
563 &status( "Redirect '$factoid' (" . ($#list) . ')...' );
566 next if (/^\Q$factoid\E$/i);
568 &status(" Redirecting '$_'.");
569 my $was = &getFactoid($_);
570 if ( $was =~ /<REPLY> see/i ) {
571 &status('warn: not redirecting a redirection.');
575 &DEBUG(" was '$was'.");
577 &setFactInfo( $x, 'factoid_value', "<REPLY> see $factoid" );
582 &formListReply( 0, "'$factoid' is redirected to by '", @redir ) );
588 if ( $message =~ /^rot([0-9]*)(\s+(.*))?/i ) {
591 if ( !defined $reply ) {
596 my $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
597 my $lower = 'abcdefghijklmnopqrstuvwxyz';
599 substr( $upper, $num )
600 . substr( $upper, 0, $num )
601 . substr( $lower, $num )
602 . substr( $lower, 0, $num );
603 eval "\$reply =~ tr/$upper$lower/$to/;";
605 #$reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
606 &performStrictReply($reply);
612 if ( $message =~ /^cpustats$/i ) {
613 if ( $^O !~ /linux/ ) {
614 &ERROR('cpustats: your OS is not supported yet.');
618 ### poor method to get info out of file, please fix.
619 open( STAT, "/proc/$$/stat" );
622 my @data = split( / /, $line );
625 # utime(13) + stime(14).
626 my $cpu_usage = sprintf( '%.01f', ( $data[13] + $data[14] ) / 100 );
628 # cutime(15) + cstime (16).
629 my $cpu_usage2 = sprintf( '%.01f', ( $data[15] + $data[16] ) / 100 );
630 my $time = time() - $^T;
631 my $raw_perc = $cpu_usage * 100 / $time;
632 my $raw_perc2 = $cpu_usage2 * 100 / $time;
638 if ( $raw_perc > 1 ) {
639 $perc = sprintf( '%.01f', $raw_perc );
640 $perc2 = sprintf( '%.01f', $raw_perc2 );
641 $total = sprintf( '%.01f', $raw_perc + $raw_perc2 );
643 elsif ( $raw_perc > 0.1 ) {
644 $perc = sprintf( '%.02f', $raw_perc );
645 $perc2 = sprintf( '%.02f', $raw_perc2 );
646 $total = sprintf( '%.02f', $raw_perc + $raw_perc2 );
649 $perc = sprintf( '%.03f', $raw_perc );
650 $perc2 = sprintf( '%.03f', $raw_perc2 );
651 $total = sprintf( '%.03f', $raw_perc + $raw_perc2 );
653 $ratio = sprintf( '%.01f', 100 * $perc / ( $perc + $perc2 ) );
655 &performStrictReply( "Total CPU usage: \002$cpu_usage\002 s ... "
656 . "Total used: \002$total\002 % "
657 . "(parent/child ratio: $ratio %)" );
663 if ( $message =~ /^ircstats?$/i ) {
664 $ircstats{'TotalTime'} ||= 0;
665 $ircstats{'OffTime'} ||= 0;
667 my $count = $ircstats{'ConnectCount'};
668 my $format_time = &Time2String( time() - $ircstats{'ConnectTime'} );
670 time() - $ircstats{'ConnectTime'} + $ircstats{'TotalTime'};
674 100 * ( $total_time - $ircstats{'OffTime'} ) / $total_time;
675 my $p = sprintf( '%.03f', $connectivity );
676 $p =~ s/(\.\d*)0+$/$1/;
677 if ( $p =~ s/\.0$// ) {
679 # this should not happen... but why...
685 if ( $total_time != ( time() - $ircstats{'ConnectTime'} ) ) {
686 my $tt_format = &Time2String($total_time);
687 &DEBUG("tt_format => $tt_format");
691 if ( $count == 1 ) { # good.
693 "I'm connected to $ircstats{'Server'} and have been so"
694 . " for $format_time";
698 "Currently I'm hooked up to $ircstats{'Server'} but only"
699 . " for $format_time. "
700 . "I had to reconnect \002$count\002 times."
701 . " Connectivity: $p %";
705 my $reason = $ircstats{'DisconnectReason'};
706 if ( defined $reason ) {
707 $reply .= ". I was last disconnected for '$reason'.";
710 &performStrictReply($reply);
716 if ( $message =~ /^statu?s$/i ) {
717 my $startString = scalar( gmtime $^T );
718 my $upString = &Time2String( time() - $^T );
719 my ( $puser, $psystem, $cuser, $csystem ) = times;
720 my $factoids = &countKeys('factoids');
722 foreach ( keys %forked ) {
723 $forks += scalar keys %{ $forked{$_} };
726 $count{'Commands'} = 0;
727 foreach ( keys %cmdstats ) {
728 $count{'Commands'} += $cmdstats{$_};
731 &performStrictReply( "Since $startString, there have been"
732 . " \002$count{'Update'}\002 "
733 . &fixPlural( 'modification', $count{'Update'} )
734 . ", \002$count{'Question'}\002 "
735 . &fixPlural( 'question', $count{'Question'} )
736 . ", \002$count{'Dunno'}\002 "
737 . &fixPlural( 'dunno', $count{'Dunno'} )
738 . ", \002$count{'Moron'}\002 "
739 . &fixPlural( 'moron', $count{'Moron'} )
740 . " and \002$count{'Commands'}\002 "
741 . &fixPlural( 'command', $count{'Commands'} )
742 . ". I have been awake for $upString this session, and "
743 . "currently reference \002$factoids\002 factoids. "
744 . "I'm using about \002$memusage\002 "
745 . "kB of memory. With \002$forks\002 active "
746 . &fixPlural( 'fork', $forks )
747 . ". Process time user/system $puser/$psystem child $cuser/$csystem"
754 # FIXME does not try to get nick 'back', just switches nicks
755 if ( $message =~ /^wantNick\s(.*)?$/i ) {
756 return unless ( &hasFlag('o') );
757 my $wantnick = lc $1;
758 my $mynick = $conn->nick();
760 if ( $mynick eq $wantnick ) {
762 "I hope you're right. I'll try anyway (mynick=$mynick, wantnick=$wantnick)."
766 # fallback check, I guess. needed?
767 if ( !&IsNickInAnyChan($wantnick) ) {
768 my $str = "attempting to change nick from $mynick to $wantnick";
775 # idea from dondelecarlo :)
776 # TODO: use cache{nickserv}
777 if ( $param{'nickServ_pass'} ) {
778 my $str = "someone is using nick $wantnick; GHOSTing";
781 &msg( 'NickServ', "GHOST $wantnick $param{'nickServ_pass'}" );
787 "going to change nick from $mynick to $wantnick after GHOST."
804 # vim:ts=4:sw=4:expandtab:tw=80