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 foreach $chan ( sort { $chans{$b} <=> $chans{$a} } keys %chans ) {
42 push( @array, "$chan/" . $chans{$chan} );
46 foreach $chan ( keys %channels ) {
47 $tucount += scalar( keys %{ $channels{$chan}{''} } );
50 ### unique user count.
52 foreach $chan ( keys %channels ) {
54 foreach $nick ( keys %{ $channels{$chan}{''} } ) {
58 $uucount = scalar( keys %nicks );
60 my $chans = scalar( keys %channels );
61 my $join = &ircCheck();
63 "\002$chans/".($chans+$join)."\002 " . &fixPlural('channel', $chans)
64 . ", \002$tucount\002 " . &fixPlural('user', $tucount)
65 . ", \002$uucount\002 unique: "
66 . join(', ', @array));
73 if ( &validChan($chan) == 0 ) {
74 &msg( $who, "error: invalid channel \002$chan\002" );
80 foreach ( sort keys %{ $chanstats{$chan} } ) {
81 my $int = $chanstats{$chan}{$_};
84 push( @array, "\002$int\002 " . &fixPlural( $_, $int ) );
87 "On \002$chan\002, there "
88 . &fixPlural( 'has', scalar(@array) )
92 # Step 1b: check channel inconstencies.
93 $chanstats{$chan}{'Join'} ||= 0;
94 $chanstats{$chan}{'SignOff'} ||= 0;
95 $chanstats{$chan}{'Part'} ||= 0;
97 my $delta_stats = $chanstats{$chan}{'Join'} - $chanstats{$chan}{'SignOff'} -
98 $chanstats{$chan}{'Part'};
101 my $total = scalar( keys %{ $channels{$chan}{''} } );
103 "chaninfo: join ~= signoff + part (drift of $delta_stats < $total)."
106 if ( $delta_stats > $total ) {
107 &ERROR('chaninfo: delta_stats exceeds total users.');
114 foreach ( 'v', 'o', '' ) {
115 my $int = scalar( keys %{ $channels{$chan}{$_} } );
118 $type = 'Voice' if ( $_ eq 'v' );
119 $type = 'Opped' if ( $_ eq 'o' );
120 $type = 'Total' if ( $_ eq '' );
122 push( @array, "\002$int\002 $type" );
124 $reply .= '. At the moment, ' . &IJoin(@array);
128 foreach ( keys %userstats ) {
129 next unless ( exists $userstats{$_}{'Count'} );
130 if ( $userstats{$_}{'Count'} =~ /^\D+$/ ) {
131 &WARN("userstats{$_}{Count} is non-digit.");
135 $new{$_} = $userstats{$_}{'Count'};
138 # TODO: show top 3 with percentages?
139 my ($count) = ( sort { $new{$b} <=> $new{$a} } keys %new )[0];
142 ". \002$count\002 has said the most with a total of \002$new{$count}\002 messages";
144 &performStrictReply("$reply.");
147 # Command statistics.
151 if ( !scalar( keys %cmdstats ) ) {
152 &performReply('no-one has run any commands yet');
157 foreach ( keys %cmdstats ) {
158 $countstats{ $cmdstats{$_} }{$_} = 1;
161 foreach ( sort { $b <=> $a } keys %countstats ) {
165 foreach ( keys %{ $countstats{$int} } ) {
166 push( @array, "\002$int\002 of $_" );
169 &performStrictReply( 'command usage includes ' . &IJoin(@array) . '.' );
172 # Command statistics.
174 my $reply = 'conninfo:';
176 foreach $key ( sort keys %::conns ) {
177 my $myconn = $::conns{$key};
179 next if (!defined $myconn);
180 my $mynick = $myconn->nick();
183 &performStrictReply( "conninfo: $reply.");
186 # Factoid extension info. xk++
188 my $faqtoid = lc shift(@_);
191 if ( $faqtoid =~ /^\-(\S+)(\s+(.*))$/ ) {
193 'error: individual factoid info queries not supported as yet.' );
195 "it's possible that the factoid mistakenly begins with '-'." );
202 &CmdFactInfo( $faqtoid, $query );
206 my $type = shift(@_);
211 &performStrictReply( &CmdFactStats($type) );
217 my $target = lc( shift || $who );
219 &sqlSelect( 'stats', 'counter', { nick => $target, type => 'karma' } )
223 &performStrictReply("$target has karma of $karma");
226 &performStrictReply("$target has neutral karma");
232 my ( $target, $tell_obj ) = ( '', '' );
233 my $dont_tell_me = 0;
236 ### is this fixed elsewhere?
237 $args =~ s/\s+/ /g; # fix up spaces.
238 $args =~ s/^\s+|\s+$//g; # again.
240 # this one catches most of them
241 if ( $args =~ /^(\S+) (-?)about (.*)$/i ) {
244 $dont_tell_me = ($2) ? 1 : 0;
246 $tell_obj = $who if ( $tell_obj =~ /^(me|myself)$/i );
249 elsif ( $args =~ /^(\S+) where (\S+) can (\S+) (.*)$/i ) {
251 # i'm sure this could all be nicely collapsed
257 elsif ( $args =~ /^(\S+) (what|where) (.*?) (is|are)[.?!]*$/i ) {
262 $query = "$qWord $verb $tell_obj";
265 elsif ( $args =~ /^(.*?) to (\S+)$/i ) {
271 # check target type. Deny channel targets.
272 if ( $target !~ /^$mask{nick}$/ or $target =~ /^$mask{chan}$/ ) {
273 &msg( $who, "No, $who, I won't. (target invalid?)" );
277 $target = $talkchannel if ( $target =~ /^us$/i );
278 $target = $who if ( $target =~ /^(me|myself)$/i );
280 &status("tell: target = $target, query = $query");
283 # if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) {
284 # &msg($who, "No, $target is not in any of my chans.");
289 if ( $target =~ /^\Q$ident\E$/i ) {
290 &msg( $who, "Isn't that a bit silly?" );
295 my $oldmtype = $msgType;
297 my $result = &doQuestion($tell_obj);
299 # ^ returns '0' if nothing was found.
303 if ( !defined $result || $result =~ /^0?$/ ) {
305 $msgType = 'private';
307 # support command redirection.
308 # recursive cmdHooks aswell :)
310 $done++ if &parseCmdHook($tell_obj);
311 $message = $tell_obj;
312 $done++ unless ( &Modules() );
314 &VERB( 'tell: setting old values of who and msgType.', 2 );
316 $msgType = $oldmtype;
319 &msg( $who, "told $target about CMD '$tell_obj'" );
322 &msg( $who, "i dunno what is '$tell_obj'." );
329 &status("tell: <$who> telling $target about $tell_obj.");
330 if ( $who ne $target ) {
332 &msg( $who, "told $target about $tell_obj." );
335 &msg( $who, "told $target about $tell_obj ($result)" );
338 $reply = "$who wants you to know: $result";
341 $reply = "telling yourself: $result";
344 &msg( $target, $reply );
348 if ( exists $cache{countryStats} ) {
349 &msg( $who, 'countrystats is already running!' );
358 &help('countrystats');
363 $cache{countryStats}{chan} = $chan;
364 $cache{countryStats}{mtype} = $msgType;
365 $cache{countryStats}{who} = $who;
366 $cache{on_who_Hack} = 1;
369 sub do_countrystats {
370 $chan = $cache{countryStats}{chan};
371 $msgType = $cache{countryStats}{mtype};
372 $who = $cache{countryStats}{who};
376 foreach ( keys %{ $cache{nuhInfo} } ) {
377 my $h = $cache{nuhInfo}{$_}{Host};
379 if ( $h =~ /^.*\.(\D+)$/ ) { # host
383 $cstats{unresolve}++;
388 foreach ( keys %cstats ) {
389 $count{ $cstats{$_} }{$_} = 1;
393 foreach ( sort { $b <=> $a } keys %count ) {
394 my $str = join( ', ', sort keys %{ $count{$_} } );
396 # push(@list, "$str ($_)");
397 my $perc = sprintf( '%.01f', 100 * $_ / $total );
399 push( @list, "$str ($_, $perc %)" );
402 # TODO: move this into a scheduler
403 $msgType = 'private';
404 &performStrictReply( &formListReply( 0, 'Country Stats ', @list ) );
406 delete $cache{countryStats};
407 delete $cache{on_who_Hack};
411 ### amalgamated commands.
417 if ( $message =~ /^(asci*|chr) (\d+)$/ ) {
418 &DEBUG('ascii/chr called ...');
419 return unless ( &IsChanConfOrWarn('allowConv') );
421 &DEBUG('ascii/chr called');
425 $result = 'NULL' if ( $arg == 0 );
427 &performReply( sprintf( "ascii %s is '%s'", $arg, $result ) );
433 if ( $message =~ /^ord(\s+(.*))$/ ) {
434 return unless ( &IsChanConfOrWarn('allowConv') );
438 if ( !defined $arg or length $arg != 1 ) {
443 if ( ord($arg) < 32 ) {
444 $arg = chr( ord($arg) + 64 );
445 if ( $arg eq chr(64) ) {
453 &performReply( sprintf( "'%s' is ascii %s", $arg, ord $arg ) );
458 if ( $message =~ /^hex(\s+(.*))?$/i ) {
459 return unless ( &IsChanConfOrWarn('allowConv') );
462 if ( !defined $arg ) {
467 if ( length $arg > 80 ) {
468 &msg( $who, 'Too long.' );
473 foreach ( split //, $arg ) {
474 $retval .= sprintf( ' %X', ord($_) );
477 &performStrictReply("$arg is$retval");
483 if ( $message =~ /^crypt\s+(\S*)?\s*(.*)?$/i ) {
484 &status("crypt: $1:$2:$3");
486 &performStrictReply( crypt( $2, $1 ) );
489 &performStrictReply( &mkcrypt($1) );
495 if ( $message =~ /^(cycle)(\s+(\S+))?$/i ) {
496 return unless ( &hasFlag('o') );
500 if ( $msgType =~ /public/ ) {
501 $chan = $talkchannel;
502 &DEBUG("cycle: setting chan to '$chan'.");
510 if ( &validChan($chan) == 0 ) {
511 &msg( $who, "error: invalid channel \002$chan\002" );
515 &msg( $chan, "I'm coming back. (courtesy of $who)" );
517 ### &ScheduleThis(5, 'getNickInUse') if (@_);
518 &status("Schedule rejoin in 5secs to $chan by $who.");
519 $conn->schedule( 5, sub { &joinchan($chan); } );
525 if ( $message =~ /^reload$/i ) {
526 return unless ( &hasFlag('n') );
528 &status("USER reload $who");
529 &performStrictReply('reloading...');
532 my $modules = &reloadAllModules();
533 &performStrictReply("reloaded:$modules");
538 if ( $message =~ /^redir(\s+(.*))?/i ) {
539 return unless ( &hasFlag('o') );
542 if ( !defined $factoid ) {
547 my $val = &getFactInfo( $factoid, 'factoid_value' );
548 if ( !defined $val or $val eq '' ) {
549 &msg( $who, "error: '$factoid' does not exist." );
552 &DEBUG("val => '$val'.");
554 &searchTable( 'factoids', 'factoid_key', 'factoid_value', "^$val\$" );
556 if ( scalar @list == 1 ) {
557 &msg( $who, "hrm... '$factoid' is unique." );
560 if ( scalar @list > 5 ) {
561 &msg( $who, 'A bit too many factoids to be redirected, hey?' );
566 &status( "Redirect '$factoid' (" . ($#list) . ')...' );
569 next if (/^\Q$factoid\E$/i);
571 &status(" Redirecting '$_'.");
572 my $was = &getFactoid($_);
573 if ( $was =~ /<REPLY> see/i ) {
574 &status('warn: not redirecting a redirection.');
578 &DEBUG(" was '$was'.");
580 &setFactInfo( $x, 'factoid_value', "<REPLY> see $factoid" );
585 &formListReply( 0, "'$factoid' is redirected to by '", @redir ) );
591 if ( $message =~ /^rot([0-9]*)(\s+(.*))?/i ) {
594 if ( !defined $reply ) {
599 my $upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
600 my $lower = 'abcdefghijklmnopqrstuvwxyz';
602 substr( $upper, $num )
603 . substr( $upper, 0, $num )
604 . substr( $lower, $num )
605 . substr( $lower, 0, $num );
606 eval "\$reply =~ tr/$upper$lower/$to/;";
608 #$reply =~ y/A-Za-z/N-ZA-Mn-za-m/;
609 &performStrictReply($reply);
615 if ( $message =~ /^cpustats$/i ) {
616 if ( $^O !~ /linux/ ) {
617 &ERROR('cpustats: your OS is not supported yet.');
621 ### poor method to get info out of file, please fix.
622 open( STAT, "/proc/$$/stat" );
625 my @data = split( / /, $line );
628 # utime(13) + stime(14).
629 my $cpu_usage = sprintf( '%.01f', ( $data[13] + $data[14] ) / 100 );
631 # cutime(15) + cstime (16).
632 my $cpu_usage2 = sprintf( '%.01f', ( $data[15] + $data[16] ) / 100 );
633 my $time = time() - $^T;
634 my $raw_perc = $cpu_usage * 100 / $time;
635 my $raw_perc2 = $cpu_usage2 * 100 / $time;
641 if ( $raw_perc > 1 ) {
642 $perc = sprintf( '%.01f', $raw_perc );
643 $perc2 = sprintf( '%.01f', $raw_perc2 );
644 $total = sprintf( '%.01f', $raw_perc + $raw_perc2 );
646 elsif ( $raw_perc > 0.1 ) {
647 $perc = sprintf( '%.02f', $raw_perc );
648 $perc2 = sprintf( '%.02f', $raw_perc2 );
649 $total = sprintf( '%.02f', $raw_perc + $raw_perc2 );
652 $perc = sprintf( '%.03f', $raw_perc );
653 $perc2 = sprintf( '%.03f', $raw_perc2 );
654 $total = sprintf( '%.03f', $raw_perc + $raw_perc2 );
656 $ratio = sprintf( '%.01f', 100 * $perc / ( $perc + $perc2 ) );
658 &performStrictReply( "Total CPU usage: \002$cpu_usage\002 s ... "
659 . "Total used: \002$total\002 % "
660 . "(parent/child ratio: $ratio %)" );
666 if ( $message =~ /^ircstats?$/i ) {
667 $ircstats{'TotalTime'} ||= 0;
668 $ircstats{'OffTime'} ||= 0;
670 my $count = $ircstats{'ConnectCount'};
671 my $format_time = &Time2String( time() - $ircstats{'ConnectTime'} );
673 time() - $ircstats{'ConnectTime'} + $ircstats{'TotalTime'};
677 100 * ( $total_time - $ircstats{'OffTime'} ) / $total_time;
678 my $p = sprintf( '%.03f', $connectivity );
679 $p =~ s/(\.\d*)0+$/$1/;
680 if ( $p =~ s/\.0$// ) {
682 # this should not happen... but why...
688 if ( $total_time != ( time() - $ircstats{'ConnectTime'} ) ) {
689 my $tt_format = &Time2String($total_time);
690 &DEBUG("tt_format => $tt_format");
694 if ( $count == 1 ) { # good.
696 "I'm connected to $ircstats{'Server'} and have been so"
697 . " for $format_time";
701 "Currently I'm hooked up to $ircstats{'Server'} but only"
702 . " for $format_time. "
703 . "I had to reconnect \002$count\002 times."
704 . " Connectivity: $p %";
708 my $reason = $ircstats{'DisconnectReason'};
709 if ( defined $reason ) {
710 $reply .= ". I was last disconnected for '$reason'.";
713 &performStrictReply($reply);
719 if ( $message =~ /^statu?s$/i ) {
720 my $startString = scalar( gmtime $^T );
721 my $upString = &Time2String( time() - $^T );
722 my ( $puser, $psystem, $cuser, $csystem ) = times;
723 my $factoids = &countKeys('factoids');
725 foreach ( keys %forked ) {
726 $forks += scalar keys %{ $forked{$_} };
729 $count{'Commands'} = 0;
730 foreach ( keys %cmdstats ) {
731 $count{'Commands'} += $cmdstats{$_};
734 &performStrictReply( "Since $startString, there have been"
735 . " \002$count{'Update'}\002 "
736 . &fixPlural( 'modification', $count{'Update'} )
737 . ", \002$count{'Question'}\002 "
738 . &fixPlural( 'question', $count{'Question'} )
739 . ", \002$count{'Dunno'}\002 "
740 . &fixPlural( 'dunno', $count{'Dunno'} )
741 . ", \002$count{'Moron'}\002 "
742 . &fixPlural( 'moron', $count{'Moron'} )
743 . " and \002$count{'Commands'}\002 "
744 . &fixPlural( 'command', $count{'Commands'} )
745 . ". I have been awake for $upString this session, and "
746 . "currently reference \002$factoids\002 factoids. "
747 . "I'm using about \002$memusage\002 "
748 . "kB of memory. With \002$forks\002 active "
749 . &fixPlural( 'fork', $forks )
750 . ". Process time user/system $puser/$psystem child $cuser/$csystem"
757 # FIXME does not try to get nick 'back', just switches nicks
758 if ( $message =~ /^wantNick\s(.*)?$/i ) {
759 return unless ( &hasFlag('o') );
760 my $wantnick = lc $1;
761 my $mynick = $conn->nick();
763 if ( $mynick eq $wantnick ) {
765 "I hope you're right. I'll try anyway (mynick=$mynick, wantnick=$wantnick)."
769 # fallback check, I guess. needed?
770 if ( !&IsNickInAnyChan($wantnick) ) {
771 my $str = "attempting to change nick from $mynick to $wantnick";
778 # idea from dondelecarlo :)
779 # TODO: use cache{nickserv}
780 if ( $param{'nickServ_pass'} ) {
781 my $str = "someone is using nick $wantnick; GHOSTing";
784 &msg( 'NickServ', "GHOST $wantnick $param{'nickServ_pass'}" );
790 "going to change nick from $mynick to $wantnick after GHOST."
807 # vim:ts=4:sw=4:expandtab:tw=80