html/hide.pl


   1 use strict;
   2 use vars  qw ($VERSION %IRSSI);
   3 use Irssi qw (settings_add_str settings_get_str settings_set_str command_bind command_runsub signal_emit );
   4 
   5 $VERSION = '0.0.7';
   6 %IRSSI = (
   7     authors     => 'Marcus Rueckert',
   8     contact     => 'darix@irssi.de',
   9     name        => 'hide tools',
  10     description => 'a little interface to irssi\'s activity_hide_* settings',
  11     license     => 'Public Domain',
  12     url         => 'http://scripts.irssi.de/',
  13     changed     => '2002-07-21 06:53:21+0200'
  14 );
  15 
  16 
  17 #
  18 # functions
  19 #
  20 
  21 sub add_item {
  22     my ($target_type, $data) = @_;
  23     my $target = target_check ($target_type);
  24     return 0 unless $target;
  25     if ($data =~ /^\s*$/ ) {
  26         print (CRAP "\cBNo target specified!\cB");
  27         print (CRAP "\cBUsage:\cB hide $target_type add [$target_type]+");
  28     }
  29     else {
  30         my $set = settings_get_str($target);
  31         for my $item ( split (/\s+/, $data) ) {
  32             if ($set =~ m/^\Q$item\E$/i) {
  33                 print (CRAP "\cBWarning:\cB $item is already in in $target_type hide list.")
  34             }
  35             else {
  36                 print (CRAP "$item added to $target_type hide list.");
  37                 $set = join (' ', $set, $item);
  38             }
  39         };
  40         settings_set_str ($target, $set);
  41         signal_emit('setup changed');
  42     }
  43     return 1;
  44 }
  45 
  46 sub remove_item {
  47     my ($target_type, $data) = @_;
  48     my $target = target_check ($target_type);
  49     if ( not ( $target )) { return 0 };
  50     if ($data =~ /^\s*$/ ) {
  51         print (CRAP "\cBNo target specified!\cB");
  52         print (CRAP "\cBUsage:\cB hide $target_type remove [$target_type]+");
  53     }
  54     else {
  55         my $set = settings_get_str($target);
  56         for my $item ( split (/\s+/, $data) ) {
  57             if ($set =~ s/$item//i) {
  58                 print (CRAP "$item removed from $target_type hide list.")
  59             }
  60             else {
  61                 print (CRAP "\cBWarning:\cB $item was not in $target_type hide list.")
  62             }
  63         };
  64         settings_set_str ($target, $set);
  65         signal_emit('setup changed');
  66     }
  67     return 1;
  68 }
  69 
  70 sub target_check {
  71     my ($target_type) = @_;
  72     my $target = '';
  73     if ($target_type eq 'level') {
  74         $target = 'activity_hide_level';
  75     }
  76     elsif ($target_type eq 'target') {
  77         $target = 'activity_hide_targets';
  78     }
  79     else {
  80         print (CLIENTERROR "\cBadd_item: no such target_type $target_type\cB");
  81     }
  82     return $target;
  83 }
  84 
  85 sub print_usage {
  86     print (CRAP "\cBUsage:\cB");
  87     print (CRAP "  hide target [add|remove] [targets]+");
  88     print (CRAP "  hide level [add|remove] [levels]+");
  89     print (CRAP "  hide usage");
  90     print (CRAP "  hide print");
  91     print (CRAP "See also: levels");
  92 };
  93 
  94 sub print_items {
  95     my ($target_type) = @_;
  96     my $delimiter = settings_get_str('hide_print_delimiter');
  97     my $target = target_check ($target_type);
  98     if ( not ( $target )) { return 0 };
  99     print ( CRAP "\cB$target_type hide list:\cB$delimiter", join ( $delimiter, sort ( split ( " ", settings_get_str($target) ) ) ) );
 100     return 1;
 101 }
 102 
 103 #
 104 # targets
 105 #
 106 
 107 command_bind 'hide target' => sub {
 108     my ($data, $server, $item) = @_;
 109     if ($data =~ m/^[(add)|(remove)]/i ) {
 110         command_runsub ('hide target', $data, $server, $item);
 111     }
 112     else {
 113         print (CRAP "\cBUsage:\cB hide target [add|remove] [targets]+");
 114     }
 115 };
 116 
 117 command_bind 'hide target add' => sub {
 118     my ($data, $server, $item) = @_;
 119     add_item ('target', $data);
 120 };
 121 
 122 command_bind 'hide target remove' => sub {
 123     my ($data, $server, $item) = @_;
 124     remove_item ('target', $data);
 125 };
 126 
 127 #
 128 # levels
 129 #
 130 command_bind 'hide level' => sub {
 131     my ($data, $server, $item) = @_;
 132     if ($data =~ m/^[(add)|(remove)]/i ) {
 133         command_runsub ('hide level', $data, $server, $item);
 134     }
 135     else {
 136         print (CRAP "\cBUsage:\cB hide level [add|remove] [levels]+");
 137         print (CRAP "See also: levels");
 138     }
 139 };
 140 
 141 command_bind 'hide level add' => sub {
 142     my ($data, $server, $item) = @_;
 143     add_item ('level', $data);
 144 };
 145 
 146 command_bind 'hide level remove' => sub {
 147     my ($data, $server, $item) = @_;
 148     remove_item ('level', $data);
 149 };
 150 
 151 #
 152 # general
 153 #
 154 
 155 command_bind 'hide' => sub {
 156     my ($data, $server, $item) = @_;
 157     if ($data =~ m/^[(target)|(level)|(help)|(usage)|(print)]/i ) {
 158         command_runsub ('hide', $data, $server, $item);
 159     }
 160     else {
 161         print_usage();
 162     }
 163 };
 164 
 165 command_bind 'hide print' => sub {
 166     print_items ('level');
 167     print_items ('target');
 168 };
 169 
 170 command_bind  'hide usage' => sub {  print_usage (); };
 171 command_bind  'hide help' => sub {  print_usage (); };
 172 
 173 #
 174 # settings
 175 #
 176 
 177 settings_add_str ( 'script', 'hide_print_delimiter',  "\n - ");