]> git.vpit.fr Git - perl/scripts/xchat.git/blobdiff - tools.pl
Make sure local_context() runs the code only when the context was found
[perl/scripts/xchat.git] / tools.pl
index bd98aa396028bcda91a20ed7c8f9726419cdd7ca..070f096d8da4777e71647ae74db4407d08453e70 100755 (executable)
--- a/tools.pl
+++ b/tools.pl
@@ -4,14 +4,17 @@ use strict;
 use warnings;
 
 use Data::Dumper;
-use Encode qw/encode/;
+use Encode qw<encode>;
 
-use Xchat qw/:all/;
+use Xchat qw<:all>;
 
 use lib get_info 'xchatdir';
 use Xchat::XPI;
+use Xchat::XPI::Utils qw<gui save_context>;
 
-our $VERSION = '0.04';
+use constant MAX_MATCHES => 10;
+
+our $VERSION = '0.05';
 
 my $ph;
 
@@ -21,12 +24,11 @@ BEGIN {
 }
 
 hook_command 'TC', sub {
- my $oldctxt = get_context;
+ my $guard = save_context;
  for (get_list 'channels') {
   set_context $_->{context};
-  command 'GUI COLOR 0';
+  gui 'clear';
  }
- set_context $oldctxt;
  return EAT_ALL;
 }, {
  help_text => 'TC, reset all tab color indicators'
@@ -113,11 +115,38 @@ hook_command 'WIDE', sub {
  help_text => 'WIDE <text>, say text in wide unicode characters'
 };
 
-$ph = new Xchat::XPI name   => 'Misc tools',
-                     tag    => 'Tools',
-                     desc   => 'Perl interpretor, URI escaper',
-                     author => 'Vincent Pit (VPIT)',
-                     email  => 'perl@profvince.com',
-                     url    => 'http://www.profvince.com';
+hook_command 'HGREP', sub {
+ my $txt = $_[1][1];
+ return EAT_ALL unless defined $txt;
+ $txt = qr/$txt/i;
+ my @matches;
+ for (get_list 'users') {
+  my $host = strip_code $_->{host};
+  next unless $host =~ /$txt/;
+  push @matches, [ $_->{nick}, $host ];
+ }
+ my $nbr = @matches;
+ print $ph ($nbr == 0 ? 'No' : $nbr)
+           . ' match' . ('es' x ($nbr > 1))
+           . ' to ' . $txt;
+ if (@matches < MAX_MATCHES) {
+  print $ph +(':' x ($nbr > 0)) . "\n";
+  print $ph "\002$_->[0]\002 ($_->[1])\n" for @matches;
+ } else {
+  print $ph " (too many of them, omitted)\n";
+ }
+ return EAT_ALL;
+}, {
+ help_text => 'HGREP <text>, print users in the current context whose hostname matches text'
+};
+
+$ph = Xchat::XPI->new(
+ name   => 'Misc tools',
+ tag    => 'Tools',
+ desc   => 'Perl interpretor, URI escaper',
+ author => 'Vincent Pit (VPIT)',
+ email  => 'perl@profvince.com',
+ url    => 'http://www.profvince.com',
+);
 
 1;