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 $reply = "I'm on \002$i\002 " . &fixPlural( 'channel', $i );
25 my $tucount = 0; # total user count.
26 my $uucount = 0; # unique user count.
31 foreach ( keys %channels ) {
32 if ( /^\s*$/ or / / ) {
33 &status('chanstats: fe channels: chan == NULL.');
38 next if (/^_default$/);
40 $chans{$_} = scalar( keys %{ $channels{$_}{''} } );
42 foreach $chan ( sort { $chans{$b} <=> $chans{$a} } keys %chans ) {
43 push( @array, "$chan/" . $chans{$chan} );
45 &performStrictReply( $reply . ': ' . join( ', ', @array ) );
48 foreach $chan ( keys %channels ) {
49 $tucount += scalar( keys %{ $channels{$chan}{''} } );
52 ### unique user count.
54 foreach $chan ( keys %channels ) {
56 foreach $nick ( keys %{ $channels{$chan}{''} } ) {
60 $uucount = scalar( keys %nicks );
62 my $chans = scalar( keys %channels );
63 &performStrictReply( "i've cached \002$tucount\002 "
64 . &fixPlural( 'user', $tucount )
65 . ", \002$uucount\002 unique "
66 . &fixPlural( 'user', $uucount )
67 . ", distributed over \002$chans\002 "
68 . &fixPlural( 'channel', $chans )
77 if ( &validChan($chan) == 0 ) {
78 &msg( $who, "error: invalid channel \002$chan\002" );
84 foreach ( sort keys %{ $chanstats{$chan} } ) {
85 my $int = $chanstats{$chan}{$_};
88 push( @array, "\002$int\002 " . &fixPlural( $_, $int ) );
91 "On \002$chan\002, there "
92 . &fixPlural( 'has', scalar(@array) )
96 # Step 1b: check channel inconstencies.
97 $chanstats{$chan}{'Join'} ||= 0;
98 $chanstats{$chan}{'SignOff'} ||= 0;
99 $chanstats{$chan}{'Part'} ||= 0;
101 my $delta_stats = $chanstats{$chan}{'Join'} - $chanstats{$chan}{'SignOff'} -
102 $chanstats{$chan}{'Part'};
105 my $total = scalar( keys %{ $channels{$chan}{''} } );
107 "chaninfo: join ~= signoff + part (drift of $delta_stats < $total)."
110 if ( $delta_stats > $total ) {
111 &ERROR('chaninfo: delta_stats exceeds total users.');
118 foreach ( 'v', 'o', '' ) {
119 my $int = scalar( keys %{ $channels{$chan}{$_} } );
122 $type = 'Voice' if ( $_ eq 'v' );
123 $type = 'Opped' if ( $_ eq 'o' );
124 $type = 'Total' if ( $_ eq '' );
126 push( @array, "\002$int\002 $type" );
128 $reply .= '. At the moment, ' . &IJoin(@array);
132 foreach ( keys %userstats ) {
133 next unless ( exists $userstats{$_}{'Count'} );
134 if ( $userstats{$_}{'Count'} =~ /^\D+$/ ) {
135 &WARN("userstats{$_}{Count} is non-digit.");
139 $new{$_} = $userstats{$_}{'Count'};
142 # TODO: show top 3 with percentages?
143 my ($count) = ( sort { $new{$b} <=> $new{$a} } keys %new )[0];
146 ". \002$count\002 has said the most with a total of \002$new{$count}\002 messages";
148 &performStrictReply("$reply.");
151 # Command statistics.
155 if ( !scalar( keys %cmdstats ) ) {
156 &performReply('no-one has run any commands yet');
161 foreach ( keys %cmdstats ) {
162 $countstats{ $cmdstats{$_} }{$_} = 1;
165 foreach ( sort { $b <=> $a } keys %countstats ) {
169 foreach ( keys %{ $countstats{$int} } ) {
170 push( @array, "\002$int\002 of $_" );
173 &performStrictReply( 'command usage include ' . &IJoin(@array) . '.' );
176 # Command statistics.
178 &performStrictReply( 'connection info ' . &IJoin(sort keys %::conns) );
181 # Factoid extension info. xk++
183 my $faqtoid = lc shift(@_);
186 if ( $faqtoid =~ /^\-(\S+)(\s+(.*))$/ ) {
188 'error: individual factoid info queries not supported as yet.' );
190 "it's possible that the factoid mistakenly begins with '-'." );
197 &CmdFactInfo( $faqtoid, $query );
201 my $type = shift(@_);
206 &performStrictReply( &CmdFactStats($type) );
212 my $target = lc( shift || $who );
214 &sqlSelect( 'stats', 'counter', { nick => $target, type => 'karma' } )
218 &performStrictReply("$target has karma of $karma");
221 &performStrictReply("$target has neutral karma");
227 my ( $target, $tell_obj ) = ( '', '' );
228 my $dont_tell_me = 0;
231 ### is this fixed elsewhere?
232 $args =~ s/\s+/ /g; # fix up spaces.
233 $args =~ s/^\s+|\s+$//g; # again.
235 # this one catches most of them
236 if ( $args =~ /^(\S+) (-?)about (.*)$/i ) {
239 $dont_tell_me = ($2) ? 1 : 0;
241 $tell_obj = $who if ( $tell_obj =~ /^(me|myself)$/i );
244 elsif ( $args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i ) {
246 # i'm sure this could all be nicely collapsed
252 elsif ( $args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i ) {
257 $query = "$qWord $verb $tell_obj";
260 elsif ( $args =~ /^(.*?) to (\S+)$/i ) {
266 # check target type. Deny channel targets.
267 if ( $target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/ ) {
268 &msg( $who, "No, $who, I won't. (target invalid?)" );
272 $target = $talkchannel if ( $target =~ /^us$/i );
273 $target = $who if ( $target =~ /^(me|myself)$/i );
275 &status("tell: target = $target, query = $query");
278 # if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
279 # &msg($who, "No, $target is not in any of my chans.");
284 if ( $target =~ /^\Q$ident\E$/i ) {
285 &msg( $who, "Isn't that a bit silly?" );
290 my $oldmtype = $msgType;
292 my $result = &doQuestion($tell_obj);
294 # ^ returns '0' if nothing was found.
298 if ( !defined $result || $result =~ /^0?$/ ) {
300 $msgType = 'private';
302 # support command redirection.
303 # recursive cmdHooks aswell :)
305 $done++ if &parseCmdHook($tell_obj);
306 $message = $tell_obj;
307 $done++ unless ( &Modules() );
309 &VERB( 'tell: setting old values of who and msgType.', 2 );
311 $msgType = $oldmtype;
314 &msg( $who, "told $target about CMD '$tell_obj'" );
317 &msg( $who, "i dunno what is '$tell_obj'." );
324 &status("tell: <$who> telling $target about $tell_obj.");
325 if ( $who ne $target ) {
327 &msg( $who, "told $target about $tell_obj." );
330 &msg( $who, "told $target about $tell_obj ($result)" );
333 $reply = "$who wants you to know: $result";
336 $reply = "telling yourself: $result";
339 &msg( $target, $reply );
343 if ( exists $cache{countryStats} ) {
344 &msg( $who, 'countrystats is already running!' );
353 &help('countrystats');
358 $cache{countryStats}{chan} = $chan;
359 $cache{countryStats}{mtype} = $msgType;
360 $cache{countryStats}{who} = $who;
361 $cache{on_who_Hack} = 1;
364 sub do_countrystats {
365 $chan = $cache{countryStats}{chan};
366 $msgType = $cache{countryStats}{mtype};
367 $who = $cache{countryStats}{who};
371 foreach ( keys %{ $cache{nuhInfo} } ) {
372 my $h = $cache{nuhInfo}{$_}{Host};
374 if ( $h =~ /^.*\.(\D+)$/ ) { # host
378 $cstats{unresolve}++;
383 foreach ( keys %cstats ) {
384 $count{ $cstats{$_} }{$_} = 1;
388 foreach ( sort { $b <=> $a } keys %count ) {
389 my $str = join( ', ', sort keys %{ $count{$_} } );
391 # push(@list, "$str ($_)");
392 my $perc = sprintf( '%.01f', 100 * $_ / $total );
394 push( @list, "$str ($_, $perc %)" );
397 # TODO: move this into a scheduler
398 $msgType = 'private';
399 &performStrictReply( &formListReply( 0, 'Country Stats ', @list ) );
401 delete $cache{countryStats};
402 delete $cache{on_who_Hack};
406 ### amalgamated commands.
412 if ( $message =~ /^(asci*|chr) (\d+)$/ ) {
413 &DEBUG('ascii/chr called ...');
414 return unless ( &IsChanConfOrWarn('allowConv') );
416 &DEBUG('ascii/chr called');
420 $result = 'NULL' if ( $arg == 0 );
422 &performReply( sprintf( "ascii %s is '%s'", $arg, $result ) );
428 if ( $message =~ /^ord(\s+(.*))$/ ) {
429 return unless ( &IsChanConfOrWarn('allowConv') );
433 if ( !defined $arg or length $arg != 1 ) {
438 if ( ord($arg) < 32 ) {
439 $arg = chr( ord($arg) + 64 );
440 if ( $arg eq chr(64) ) {
448 &performReply( sprintf( "'%s' is ascii %s", $arg, ord $arg ) );
453 if ( $message =~ /^hex(\s+(.*))?$/i ) {
454 return unless ( &IsChanConfOrWarn('allowConv') );
457 if ( !defined $arg ) {
462 if ( length $arg > 80 ) {
463 &msg( $who, 'Too long.' );
468 foreach ( split //, $arg ) {
469 $retval .= sprintf( ' %X', ord($_) );
472 &performStrictReply("$arg is$retval");
478 if ( $message =~ /^crypt\s+(\S*)?\s*(.*)?$/i ) {
479 &status("crypt: $1:$2:$3");
481 &performStrictReply( crypt( $2, $1 ) );
484 &performStrictReply( &mkcrypt($1) );
490 if ( $message =~ /^(cycle)(\s+(\S+))?$/i ) {
491 return unless ( &hasFlag('o') );
495 if ( $msgType =~ /public/ ) {
496 $chan = $talkchannel;
497 &DEBUG("cycle: setting chan to '$chan'.");
505 if ( &validChan($chan) == 0 ) {
506 &msg( $who, "error: invalid channel \002$chan\002" );
510 &msg( $chan, "I'm coming back. (courtesy of $who)" );
512 ### &ScheduleThis(5, 'getNickInUse') if (@_);
513 &status("Schedule rejoin in 5secs to $chan by $who.");
514 $conn->schedule( 5, sub { &joinchan($chan); } );
520 if ( $message =~ /^reload$/i ) {
521 return unless ( &hasFlag('n') );
523 &status("USER reload $who");
524 &performStrictReply('reloading...');
525 my $modules = &reloadAllModules();
526 &performStrictReply("reloaded:$modules");
531 if ( $message =~ /^redir(\s+(.*))?/i ) {
532 return unless ( &hasFlag('o') );
535 if ( !defined $factoid ) {
540 my $val = &getFactInfo( $factoid, 'factoid_value' );
541 if ( !defined $val or $val eq '' ) {
542 &msg( $who, "error: '$factoid' does not exist." );
545 &DEBUG("val => '$val'.");
547 &searchTable( 'factoids', 'factoid_key', 'factoid_value', "^$val\$" );
549 if ( scalar @list == 1 ) {
550 &msg( $who, "hrm... '$factoid' is unique." );
553 if ( scalar @list > 5 ) {
554 &msg( $who, 'A bit too many factoids to be redirected, hey?' );
559 &status( "Redirect '$factoid' (" . ($#list) . ')...' );
562 next if (/^\Q$factoid\E$/i);
564 &status(" Redirecting '$_'.");
565 my $was = &getFactoid($_);
566 if ( $was =~ /<REPLY> see/i ) {
567 &status('warn: not redirecting a redirection.');
571 &DEBUG(" was '$was'.");
573 &setFactInfo( $x, 'factoid_value', "<REPLY> see $factoid" );
578 &formListReply( 0, "'$factoid' is redirected to by '", @redir ) );
584 if ( $message =~ /^rot([0-9]*)(\s+(.*))?/i ) {
587 if ( !defined $reply ) {
592 my $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
593 my $lower = 'abcdefghijklmnopqrstuvwxyz';
595 substr( $upper, $num )
596 . substr( $upper, 0, $num )
597 . substr( $lower, $num )
598 . substr( $lower, 0, $num );
599 eval "\$reply =~ tr/$upper$lower/$to/;";
601 #$reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
602 &performStrictReply($reply);
608 if ( $message =~ /^cpustats$/i ) {
609 if ( $^O !~ /linux/ ) {
610 &ERROR('cpustats: your OS is not supported yet.');
614 ### poor method to get info out of file, please fix.
615 open( STAT, "/proc/$$/stat" );
618 my @data = split( / /, $line );
621 # utime(13) + stime(14).
622 my $cpu_usage = sprintf( '%.01f', ( $data[13] + $data[14] ) / 100 );
624 # cutime(15) + cstime (16).
625 my $cpu_usage2 = sprintf( '%.01f', ( $data[15] + $data[16] ) / 100 );
626 my $time = time() - $^T;
627 my $raw_perc = $cpu_usage * 100 / $time;
628 my $raw_perc2 = $cpu_usage2 * 100 / $time;
634 if ( $raw_perc > 1 ) {
635 $perc = sprintf( '%.01f', $raw_perc );
636 $perc2 = sprintf( '%.01f', $raw_perc2 );
637 $total = sprintf( '%.01f', $raw_perc + $raw_perc2 );
639 elsif ( $raw_perc > 0.1 ) {
640 $perc = sprintf( '%.02f', $raw_perc );
641 $perc2 = sprintf( '%.02f', $raw_perc2 );
642 $total = sprintf( '%.02f', $raw_perc + $raw_perc2 );
645 $perc = sprintf( '%.03f', $raw_perc );
646 $perc2 = sprintf( '%.03f', $raw_perc2 );
647 $total = sprintf( '%.03f', $raw_perc + $raw_perc2 );
649 $ratio = sprintf( '%.01f', 100 * $perc / ( $perc + $perc2 ) );
651 &performStrictReply( "Total CPU usage: \002$cpu_usage\002 s ... "
652 . "Total used: \002$total\002 % "
653 . "(parent/child ratio: $ratio %)" );
659 if ( $message =~ /^ircstats?$/i ) {
660 $ircstats{'TotalTime'} ||= 0;
661 $ircstats{'OffTime'} ||= 0;
663 my $count = $ircstats{'ConnectCount'};
664 my $format_time = &Time2String( time() - $ircstats{'ConnectTime'} );
666 time() - $ircstats{'ConnectTime'} + $ircstats{'TotalTime'};
670 100 * ( $total_time - $ircstats{'OffTime'} ) / $total_time;
671 my $p = sprintf( '%.03f', $connectivity );
672 $p =~ s/(\.\d*)0+$/$1/;
673 if ( $p =~ s/\.0$// ) {
675 # this should not happen... but why...
681 if ( $total_time != ( time() - $ircstats{'ConnectTime'} ) ) {
682 my $tt_format = &Time2String($total_time);
683 &DEBUG("tt_format => $tt_format");
687 if ( $count == 1 ) { # good.
689 "I'm connected to $ircstats{'Server'} and have been so"
690 . " for $format_time";
694 "Currently I'm hooked up to $ircstats{'Server'} but only"
695 . " for $format_time. "
696 . "I had to reconnect \002$count\002 times."
697 . " Connectivity: $p %";
701 my $reason = $ircstats{'DisconnectReason'};
702 if ( defined $reason ) {
703 $reply .= ". I was last disconnected for '$reason'.";
706 &performStrictReply($reply);
712 if ( $message =~ /^statu?s$/i ) {
713 my $startString = scalar( gmtime $^T );
714 my $upString = &Time2String( time() - $^T );
715 my ( $puser, $psystem, $cuser, $csystem ) = times;
716 my $factoids = &countKeys('factoids');
718 foreach ( keys %forked ) {
719 $forks += scalar keys %{ $forked{$_} };
722 $count{'Commands'} = 0;
723 foreach ( keys %cmdstats ) {
724 $count{'Commands'} += $cmdstats{$_};
727 &performStrictReply( "Since $startString, there have been"
728 . " \002$count{'Update'}\002 "
729 . &fixPlural( 'modification', $count{'Update'} )
730 . ", \002$count{'Question'}\002 "
731 . &fixPlural( 'question', $count{'Question'} )
732 . ", \002$count{'Dunno'}\002 "
733 . &fixPlural( 'dunno', $count{'Dunno'} )
734 . ", \002$count{'Moron'}\002 "
735 . &fixPlural( 'moron', $count{'Moron'} )
736 . " and \002$count{'Commands'}\002 "
737 . &fixPlural( 'command', $count{'Commands'} )
738 . ". I have been awake for $upString this session, and "
739 . "currently reference \002$factoids\002 factoids. "
740 . "I'm using about \002$memusage\002 "
741 . "kB of memory. With \002$forks\002 active "
742 . &fixPlural( 'fork', $forks )
743 . ". Process time user/system $puser/$psystem child $cuser/$csystem"
750 # FIXME does not try to get nick 'back', just switches nicks
751 if ( $message =~ /^wantNick\s(.*)?$/i ) {
752 return unless ( &hasFlag('o') );
753 my $wantnick = lc $1;
754 my $mynick = $conn->nick();
756 if ( $mynick eq $wantnick ) {
758 "I hope you're right. I'll try anyway (mynick=$mynick, wantnick=$wantnick)."
762 # fallback check, I guess. needed?
763 if ( !&IsNickInAnyChan($wantnick) ) {
764 my $str = "attempting to change nick from $mynick to $wantnick";
771 # idea from dondelecarlo :)
772 # TODO: use cache{nickserv}
773 if ( $param{'nickServ_pass'} ) {
774 my $str = "someone is using nick $wantnick; GHOSTing";
777 &msg( 'NickServ', "GHOST $wantnick $param{'nickServ_pass'}" );
783 "going to change nick from $mynick to $wantnick after GHOST."
800 # vim:ts=4:sw=4:expandtab:tw=80