Script: filter_ext.pl

Extends /filter, help against spam.
Author: rettub — Version: 0.04 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: filter, spam
Added: 2010-01-17 — Updated: 2011-09-17

Download GitHub Repository

  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
# -----------------------------------------------------------------------------
# Copyright (c) 2010-2011 by rettub <rettub@gmx.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------------
# filter_ext.pl for weechat version 0.3.0 or later
#
# Quick help against spam, extend /filter with prefixes for following args: del, enable, disable, list
# Listens for highlights and sends them to a bar.
#
# Usage:
#   install srcipt and do /help filter_ext
#
# Configuration:
#   none
#
# -----------------------------------------------------------------------------
# Changelog:
#
# Version 0.04 2011-09-17
#
#   FIX: infolists not freed
#
# Version 0.03 2010-01-18
#
#   FIX: use weechat::print_date_tags() to list filters
#        otherwise if filter was enabled for all plugins even output of this script was filtered
#
# Version 0.02 2010-01-18
#   added hints to filter more aggressive
#   usefull for SPAM like this:
#   -- | CTCP requested by ntba: VERSION To find out more visit http://wiredside.com/page.ext
#
# Version 0.01 2010-01-17
# initial version

use 5.006;

#use Carp;	    # don't use die in modules
#use Carp::Clan;    # or better use this

use strict;
use warnings;

my $Version = 0.04;

sub version {
    $Version;
}

my $SCRIPT      = "filter_ext";
my $AUTHOR      = "rettub";
my $LICENCE     = "GPL3";
my $DESCRIPTION = "Quick help against spam, extend /filter with prefixes for following args: del, enable, disable, list";
my $COMMAND     = "filter_ext";             # new command name
my $COMPLETITION  = "del|enable|disable|list";
my $CALLBACK      = $COMMAND;
my $ARGS_HELP   = "list regex | del regex | enable regex | disable regex";
my $CMD_HELP    = <<EO_HELP;

Quick access to filters on spam invasion via prefixes for filter-names.

First you should define an alias like
  /alias /f /filter add SPAM\$1 irc. * \$1

Sometimes the filter above will not work, eg for ctcp requests, then a more aggressive filter can help:
  /alias /fa /filter add SPAMAGR\$1 * * \$1

  Note y'll never see lines with regex (\$1) again!

With this alias you can simply add filters for spam with copy and paste. 
  /f copy_and_pasted_string

With the alias /f, defined above, filters like

  [SPAMcopy_and_pasted_string] buffer: irc. / tags: * / regex: copy_and_pasted_string

will be created.

Now you can simply list/disable/enable or delete them at once like

  /filter_ext del SPAM

Keep in mind 'SPAM' is handled as a perl regex, so all filternames containing 'SPAM' will be deleted. If you want del filter-names beginning with 'SPAM' use

  /filter_ext del ^SPAM

instead. (for perl regular expressions, look at: perldoc perlre)

To be more quick define aliases like:
  /alias /fddel  /filter_ext del ^SPAM
  /alias /fl     /filter_ext list SPAM
  /alias /fd     /filter_ext disable SPAM
  /alias /fe     /filter_ext enable SPAM
for aggressive filters:
  /alias /faddel  /filter_ext del ^SPAMAGR
  /alias /fal     /filter_ext list SPAMAGR
  /alias /fad     /filter_ext disable SPAMAGR
  /alias /fae     /filter_ext enable SPAMAGR

See /help filter for details. If y're using weechat > 0.3.0 take a look at irc.look.display_ctcp*
EO_HELP

my $DEBUG=0;
sub DEBUG {weechat::print('', "***\t" . $SCRIPT . ": $_[0]") if $DEBUG;}
sub wprint { weechat::print_date_tags("", 0, "no_filter", , "\t $_[0]");}
sub werror {weechat::print('', weechat::prefix("error")."$COMMAND: $_[0]");}

my $Input_buffer;

use constant {
    ENABLED => 1,
    DISABLED =>0,
};

sub list {
    my $regex = shift;

    my $infolist = weechat::infolist_get( "filter", "", "" );

    my $i = 0;
    while ( weechat::infolist_next($infolist) ) {

        my $fname = weechat::infolist_string( $infolist, "name" );


        if ( $fname =~ /$regex/ ) {
        wprint("Message filters with regex '$regex':") if $i == 0;
        $i++;
            my $regex  = weechat::infolist_string( $infolist, "regex" );
            my $plugin = weechat::infolist_string( $infolist, "plugin_name" );
            my $buffer = weechat::infolist_string( $infolist, "buffer_name" );
            my $tags   = weechat::infolist_string( $infolist, "tags" );
            DEBUG("filer_regex: $regex");
            my $enabled = weechat::infolist_integer( $infolist, "enabled" );
            DEBUG("filer_enaled: $enabled");
            wprint( " "
                  . weechat::color("green") . "["
                  . weechat::color("reset")
                  . $fname
                  . weechat::color("green") . "]"
                  . weechat::color("reset")
                  . "   plugin.(buffer): "
                  . weechat::color("red")
                  . "$plugin"
                  . weechat::color("reset") . "."
                  . ( $buffer ? $buffer : "all_buffers" )
                  . " / tags: $tags"
                  . " /  regex: $regex" . " /  "
                  . ( $enabled ? 'enabled  ' : 'disabled' ) );
        }
    }
    weechat::infolist_free($infolist);
}

sub toggle {
    my ( $regex, $cmd, $flag ) = @_;

    my $infolist = weechat::infolist_get( "filter", "", "" );

    while ( weechat::infolist_next($infolist) ) {

        my $fname = weechat::infolist_string( $infolist, "name" );
        if ( $fname =~ /$regex/ ) {
            next
              unless weechat::infolist_integer( $infolist, "enabled" ) == $flag;
            weechat::command( "", "/filter $cmd $fname" );
        }
    }
    weechat::infolist_free($infolist);
}

sub del {
    my ($regex) = @_;

    my $infolist = weechat::infolist_get( "filter", "", "" );

    while ( weechat::infolist_next($infolist) ) {

        my $fname = weechat::infolist_string( $infolist, "name" );
        if ( $fname =~ /$regex/ ) {
            weechat::command( "", "/filter del $fname" );
        }
    }
    weechat::infolist_free($infolist);
}

sub filter_ext {
    #my ( undef, undef, $_cmd ) = @_;
    my ($_cmd, $args) = split(/ /, $_[2], 2);

        DEBUG($_cmd);
        DEBUG($args);

        if ( not defined $args ) {
            werror("no regex defined see /help $SCRIPT" );
    return weechat::WEECHAT_RC_OK;
        }
    if ( $_cmd eq 'list' ) {
        list($args);
    } elsif ($_cmd eq 'disable' ) {
        toggle($args, $_cmd, ENABLED);
    } elsif ($_cmd eq 'enable' ) {
        toggle($args, $_cmd, DISABLED);
    } elsif ($_cmd eq 'del' ) {
        del($args);
    }


    return weechat::WEECHAT_RC_OK;
}

# init script
if ( weechat::register(  $SCRIPT,  $AUTHOR, $Version, $LICENCE, $DESCRIPTION, "", "" ) ) {

    weechat::hook_command( $COMMAND,  $DESCRIPTION,  $ARGS_HELP, $CMD_HELP, $COMPLETITION, $CALLBACK, "" );
}

# vim: ai ts=4 sts=4 et sw=4 foldmethod=marker :