aboutsummaryrefslogtreecommitdiffstats
path: root/act_hide/act_hide.pl
blob: 95c10b96887f8975ab7de9f4fe886d2d68a64be1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
=pod

=head1 NAME

act_hide.pl

=head1 DESCRIPTION

A minimalist template useful for basing actual scripts on.

=head1 INSTALLATION

Copy into your F<~/.irssi/scripts/> directory and load with
C</SCRIPT LOAD F<filename>>.

=head1 USAGE

None, since it doesn't actually do anything.

=head1 AUTHORS

Derived from the L<hide.pl|http://scripts.irssi.org/scripts/hide.pl> script,
Original Copyright E<copy> 2002 Marcus Rueckert C<E<lt>darix@irssi.deE<gt>>

Modifications Copyright E<copy> 2011 Tom Feist C<E<lt>shabble+irssi@metavore.orgE<gt>>

=head1 LICENCE

B<TODO:> Is this Public Domain enough?

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

=head1 BUGS

=head1 TODO

=cut

use strict;
use warnings;

use Irssi;
use Irssi::Irc;
use Irssi::TextUI;

use Data::Dumper;

our $VERSION = '0.1';
our %IRSSI = (
              authors     => 'Marcus Rueckert, shabble',
              contact     => 'shabble+irssi@metavore.org',
              name        => 'act_hide',
              description => 'Improved interface to activity_hide_* settings',
              license     => 'Public Domain',
             );

# Globals.

my $CMD_NAME = "hide";
my $SUB_CMDS = {
                add  => \&cmd_hide_add,
                del  => \&cmd_hide_del,
                list => \&cmd_hide_list,
               };

my $CMD_OPTS = "-target -level";


act_hide_init();

sub act_hide_init {

    Irssi::command_bind($CMD_NAME, \&subcmd_handler);

    foreach my $subcmd (keys %$SUB_CMDS) {
        my $coderef = $SUB_CMDS->{$subcmd};
        Irssi::command_bind("$CMD_NAME $subcmd", $coderef);
        Irssi::command_set_options("$CMD_NAME $subcmd", $CMD_OPTS);
    }
}

sub subcmd_handler {
    my ($data, $server, $w_item) = @_;
    $data =~ s/\s+$//g; # strip trailing whitespace.
    Irssi::command_runsub($CMD_NAME, $data, $server, $w_item);
}

sub parse_args {
    my ($cmd, $data) = @_;
    my $options = {};
    my @opts = Irssi::command_parse_options($cmd, $data);

    if (@opts and ref($opts[0]) eq 'HASH') {
        $options = $opts[0];
        $options->{__remainder} = $opts[1];
        print "Options: " . Dumper($options);
    } else {
        $options->{__remainder} = $data;
    }
    return $options;
}

sub cmd_hide_add {
    my ($args, $server, $w_item) = @_;
    my $opts = parse_args("$CMD_NAME add", $args);

    if (exists $opts->{target}) {
        my $target = $opts->{__remainder};
        $target =~ s/^\s*(\S+)\s*$/$1/;

        if (add_item_to_targets($target)) {
            $w_item->print("added $target");
        } else {
            $w_item->print("failed to add $target");
        }
    } elsif (exists $opts->{level}) {

    } else {
        print "Bah";
    }

}

sub cmd_hide_del {
    my ($args, $server, $w_item) = @_;
    my $opts = parse_args("$CMD_NAME del", $args);
    if (exists $opts->{target}) {
        my $target = $opts->{__remainder};
        $target =~ s/^\s*(\S+)\s*$/$1/;

        if (remove_item_from_targets($target)) {
            $w_item->print("removed $target");
        } else {
            $w_item->print("failed to remove $target");
        }
    } elsif (exists $opts->{level}) {

    } else {
        print "Bah";
    }

}

sub cmd_hide_list {
    my ($args, $server, $w_item) = @_;
    my $opts = parse_args("$CMD_NAME list", $args);
    if (exists $opts->{target}) {
        $w_item->print("Targets: "
                       . Irssi::settings_get_str('activity_hide_targets'));
    } elsif (exists $opts->{level}) {
      $w_item->print("Levels: "
                     . Irssi::settings_get_level('activity_hide_level'));
    } else {
        $w_item->print("Bah");
    }

}

sub add_item_to_targets {
    my ($item) = @_;
    my $current = Irssi::settings_get_str('activity_hide_targets');
    my %map = map { $_ => 1 } split /\s+/, $current;

    if (not exists $map{$item}) {
        Irssi::settings_set_str('activity_hide_targets', $current . ' ' . $item);
        return 1;
    } else {
        print "Cannot add $item, already exists";
        return 0;
    }
}

sub remove_item_from_targets {
    my ($item) = @_;
    my $current = Irssi::settings_get_str('activity_hide_targets');
    my %map = map { $_ => 1 } split /\s+/, $current;

    if (exists $map{$item}) {
        my $new_str = join ' ', grep { $_ ne $item } keys %map;
        Irssi::settings_set_str('activity_hide_targets', $new_str);
        return 1;
    } else {
        print "Cannot remove $item, doesn't exist";
        return 0;
    }
}




# sub add_item {
#     my ($target_type, $data) = @_;
#     my $target = target_check ($target_type);
#     return 0 unless $target;
#     if ($data =~ /^\s*$/ ) {
#         print (CRAP "\cBNo target specified!\cB");
#         print (CRAP "\cBUsage:\cB hide $target_type add [$target_type]+");
#     }
#     else {
#         my $set = settings_get_str($target);
#         for my $item ( split (/\s+/, $data) ) {
#             if ($set =~ m/^\Q$item\E$/i) {
#                 print (CRAP "\cBWarning:\cB $item is already in in $target_type hide list.")
#             }
#             else {
#                 print (CRAP "$item added to $target_type hide list.");
#                 $set = join (' ', $set, $item);
#             }
#         };
#         settings_set_str ($target, $set);
#         signal_emit('setup changed');
#     }
#     return 1;
# }

# sub remove_item {
#     my ($target_type, $data) = @_;
#     my $target = target_check ($target_type);
#     if ( not ( $target )) { return 0 };
#     if ($data =~ /^\s*$/ ) {
#         print (CRAP "\cBNo target specified!\cB");
#         print (CRAP "\cBUsage:\cB hide $target_type remove [$target_type]+");
#     }
#     else {
#         my $set = settings_get_str($target);
#         for my $item ( split (/\s+/, $data) ) {
#             if ($set =~ s/$item//i) {
#                 print (CRAP "$item removed from $target_type hide list.")
#             }
#             else {
#                 print (CRAP "\cBWarning:\cB $item was not in $target_type hide list.")
#             }
#         };
#         settings_set_str ($target, $set);
#         signal_emit('setup changed');
#     }
#     return 1;
# }

# sub target_check {
#     my ($target_type) = @_;
#     my $target = '';
#     if ($target_type eq 'level') {
#         $target = 'activity_hide_level';
#     }
#     elsif ($target_type eq 'target') {
#         $target = 'activity_hide_targets';
#     }
#     else {
#         print (CLIENTERROR "\cBadd_item: no such target_type $target_type\cB");
#     }
#     return $target;
# }

# sub print_usage {
#     print (CRAP "\cBUsage:\cB");
#     print (CRAP "  hide target [add|remove] [targets]+");
#     print (CRAP "  hide level [add|remove] [levels]+");
#     print (CRAP "  hide usage");
#     print (CRAP "  hide print");
#     print (CRAP "See also: levels");
# };

# sub print_items {
#     my ($target_type) = @_;
#     my $delimiter = settings_get_str('hide_print_delimiter');
#     my $target = target_check ($target_type);
#     if ( not ( $target )) { return 0 };
#     print ( CRAP "\cB$target_type hide list:\cB$delimiter", join ( $delimiter, sort ( split ( " ", settings_get_str($target) ) ) ) );
#     return 1;
# }

# #
# # targets
# #

# command_bind 'hide target' => sub {
#     my ($data, $server, $item) = @_;
#     if ($data =~ m/^[(add)|(remove)]/i ) {
#         command_runsub ('hide target', $data, $server, $item);
#     }
#     else {
#         print (CRAP "\cBUsage:\cB hide target [add|remove] [targets]+");
#     }
# };

# command_bind 'hide target add' => sub {
#     my ($data, $server, $item) = @_;
#     add_item ('target', $data);
# };

# command_bind 'hide target remove' => sub {
#     my ($data, $server, $item) = @_;
#     remove_item ('target', $data);
# };

# #
# # levels
# #
# command_bind 'hide level' => sub {
#     my ($data, $server, $item) = @_;
#     if ($data =~ m/^[(add)|(remove)]/i ) {
#         command_runsub ('hide level', $data, $server, $item);
#     }
#     else {
#         print (CRAP "\cBUsage:\cB hide level [add|remove] [levels]+");
#         print (CRAP "See also: levels");
#     }
# };

# command_bind 'hide level add' => sub {
#     my ($data, $server, $item) = @_;
#     add_item ('level', $data);
# };

# command_bind 'hide level remove' => sub {
#     my ($data, $server, $item) = @_;
#     remove_item ('level', $data);
# };

# #
# # general
# #

# command_bind 'hide' => sub {
#     my ($data, $server, $item) = @_;
#     if ($data =~ m/^[(target)|(level)|(help)|(usage)|(print)]/i ) {
#         command_runsub ('hide', $data, $server, $item);
#     }
#     else {
#         print_usage();
#     }
# };

# command_bind 'hide print' => sub {
#     print_items ('level');
#     print_items ('target');
# };

# command_bind  'hide usage' => sub {  print_usage (); };
# command_bind  'hide help' => sub {  print_usage (); };

# #
# # settings
# #

# settings_add_str ( 'script', 'hide_print_delimiter',  "\n - ");