Home

Script: curiousignore.pl

Remove messages from a nick and only print nick. (for WeeChat ≥ 0.3.2)
Author: nils_2, version 0.3, GPL3 — added: 2010-06-08, updated: 2013-10-22
  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
#
# Copyright (c) 2010-2013 by Nils Görs <weechatter@arcor.de>
#
# ignore message and only print nick-name from specified users
#
# 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/>.
#
# idea by darrob
#
# settings:
# /set plugins.var.perl.curiousignore.blacklist server.channel.nick
# /set plugins.var.perl.curiousignore.cloaked_text "text cloaked"
#
# this scripts needs weechat 0.3.2 or higher
#
# v0.3  : add: option cloaked_text_reply (suggested by dAnjou)
#       : add: option rapid_fire
#       : add: description for options
# v0.2  : error with uninitialized channel removed
#       : /me text will be cloaked, too
#
# TODO saving the cloaked text to log-file.

use strict;
use POSIX qw(strftime);

my $SCRIPT_NAME         = "curiousignore";
my $SCRIPT_VERSION      = "0.3";
my $SCRIPT_DESC         = "suppresses messages from specified nick and only prints his nickname in channel";

my $save_to_log = "on";
my $nick_mode = "";
my %nick_structure = ();
#$VAR1 = {
#		'server.channel' => nick
#	}

my %options_default = ('blacklist'              => ['','comma separated list of nicks to be cloaked. format: server.#channel.nick'],
                       'cloaked_text'           => ['text cloaked','text that will be displayed in buffer. if no text is given, the message will be discard'],
                       'cloaked_text_reply'     => ['reply to cloaked text','text that will be displayed in buffer for an reply of a cloaked nick. if no text is given, the message will be discard. Own written messages will be displayed'],
                       'rapid_fire'             => ['on','displays cloaked text only once, even nick sends several messages'],
);

my %options = ();

# program starts here
sub colorize_cb {
    my ( $data, $modifier, $modifier_data, $string ) = @_;

    $string =~ m/^(.*)\t(.*)/;                                                                          # get the nick & message: nick[tab]message
    my $nick = $1;
    my $message = $2;

    if (not defined $nick) {return $string;}                                                            # no nickname
    #irc;freenode.#weechat;
    $modifier_data =~ (m/irc;(.+?)\.(.+?)\;/);
    my $server = $1;
    my $channel = $2;
    if (not defined $channel) {return $string;}                                                         # no channel
    my $server_chan = $server . "." . $channel;

    $nick = weechat::string_remove_color($nick,"");                                                     # remove colour-codes from nick
    my $nick_color = weechat::info_get('irc_nick_color', $nick);                                        # get nick-colour

    my $last_nick = "";
    if ($nick =~ m/^\@|^\%|^\+|^\~|^\*|^\&|^\!|^\-/)                                                    # check for nick modes (@%+~*&!-)
    {
        $nick_mode = substr($nick,0,1);
        $last_nick = substr($nick,1,length($nick)-1);
    }
    else
    {
        $last_nick = $nick;
    }

    my $blacklist2 = $options{blacklist};
    $blacklist2 =~ tr/,/ /;                                                                             # replace "," with space
    if ( index( $blacklist2, $server_chan.".".$last_nick ) >= 0 )                                           # check blacklist
    {
        return "" if ( lc($options{rapid_fire}) eq "on"  && defined $nick_structure{$server_chan} && ($nick_structure{$server_chan} eq $last_nick) );
        #get_logfile_name( weechat::buffer_search("",$server_chan),$string );                       # find the buffer pointer

        return $string if (get_current_nick() eq $nick);
        $string = $last_nick . "\t" . $options{cloaked_text};
        $nick_structure{$server_chan} = $last_nick;                                                         # store last nick
        return "" if ( $options{cloaked_text} eq "" );
        return $string;
    }

# curious nick made a /me ?
    if (weechat::config_string(weechat::config_get("weechat.look.prefix_action")) eq $last_nick)            # get prefix_action
    {
        my @array=split(/,/,$options{blacklist});
        foreach (@array)
        {
            $_ =~ (/(.+?)\.(.+?)\.(.*)/);
            my $string_w_color = weechat::string_remove_color($string,"");
            $nick = $3;
            my $nick_w_prefix = weechat::config_string(weechat::config_get("weechat.look.prefix_action")) . "\t" . $nick;
            if (  $string_w_color =~ m/^$nick_w_prefix/ )
            {
                return "" if ( lc($options{rapid_fire}) eq "on"  && defined $nick_structure{$server_chan} && ($nick_structure{$server_chan} eq $nick) );
                return "" if ( defined $nick_structure{$server_chan} && ($nick_structure{$server_chan} eq $nick) && $options{cloaked_text} eq "" );
                $string = $nick . "\t" . $options{cloaked_text};
                $nick_structure{$server_chan} = $nick;                                                  # store last nick
                return "" if ( $options{cloaked_text} eq "" );
                return $string;
            }
        }

    }

    if ( search_nick_in_message($server,$channel,$message) eq 1 )
    {
        return "" if ( lc($options{rapid_fire}) eq "on"  && defined $nick_structure{$server_chan} && ($nick_structure{$server_chan} eq $last_nick) );

        return $string if (get_current_nick() eq $nick);
        $string = $last_nick . "\t" . $options{cloaked_text_reply};
        $nick_structure{$server_chan} = $last_nick;                                                         # store last nick
        return "" if ( $options{cloaked_text} eq "" );
        return $string;
    }

    $nick_structure{$server_chan} = $last_nick;                                                             # store last nick
    return $string;
}

sub get_current_nick
{
    return weechat::buffer_get_string(weechat::current_buffer(),'localvar_nick');
}

sub search_nick_in_message
{
    my ($server,$channel,$message) = @_;
    my @array=split(/,/,$options{blacklist});
    foreach (@array)
    {
        my ($server_cloaked,$channel_cloaked,$nick_cloaked) = split(/\./,$_);
        next if ($server ne $server_cloaked or $channel ne $channel_cloaked);
        return 1 if ( $message =~ m/$nick_cloaked/i );
    }
return 0;
}

# using this "hack" weechat will crash on start
sub get_logfile_name
{                                                                                                       # get name of log-file
    my ($buffer, $string) = @_;                                                                         # buffer pointer
    my $logfilename = "";
    my $log_enabled = "";
    my $log_level = "";
    my $bpointer = "";

    if ($save_to_log eq "on")                                                                           # save to log on?
    {
        my $linfolist = weechat::infolist_get("logger_buffer", "", "");
        while(weechat::infolist_next($linfolist))
        {
            $bpointer = weechat::infolist_pointer($linfolist, "buffer");
            if($bpointer eq $buffer)
            {
                $logfilename = weechat::infolist_string($linfolist, "log_filename");
                $log_enabled = weechat::infolist_integer($linfolist, "log_enabled");
                $log_level = weechat::infolist_integer($linfolist, "log_level");
                last if ($log_level == 0 );                                                             # logging disabled
                # get time format and convert it
                my $time_format = weechat::config_string( weechat::config_get("logger.file.time_format") );
                $time_format = strftime $time_format, localtime;
                # remove color-codes from string and create output for log
                $string = $time_format . "\t" . weechat::string_remove_color($string,"");
                weechat::command($buffer, "/mute logger disable");                                      # disable file logging
                system("echo \'" . $string . "\' >>".$logfilename);                                     # write output to logfile
                weechat::command($buffer,"/mute logger set " . $log_level);                             # start file logging again
                last;
            }
        }
        weechat::infolist_free($linfolist);
    }
    return weechat::WEECHAT_RC_OK;
}

sub toggle_config_by_set
{
    my ($pointer, $name, $value) = @_;
    $name = substr($name, length("plugins.var.perl.".$SCRIPT_NAME."."), length($name));
    $options{$name} = $value;
    return weechat::WEECHAT_RC_OK;
}

sub init_config
{
    my $version = weechat::info_get("version_number", "") || 0;
    foreach my $option (keys %options_default)
    {
        if (!weechat::config_is_set_plugin($option))
        {
            weechat::config_set_plugin($option, $options_default{$option}[0]);
            $options{$option} = $options_default{$option}[0];
        }
        else
        {
            $options{$option} = weechat::config_get_plugin($option);
        }
        if ($version >= 0x00030500)
        {
            weechat::config_set_desc_plugin($option, $options_default{$option}[1]." (default: \"".$options_default{$option}[0]."\")");
        }
    }
}

# first function called by a WeeChat-script.
weechat::register($SCRIPT_NAME, "Nils Görs <weechatter\@arcor.de>", $SCRIPT_VERSION, "GPL3", $SCRIPT_DESC, "", "");

init_config();

#  if (!weechat::config_is_set_plugin("save_to_log")){
#    weechat::config_set_plugin("save_to_log", $save_to_log);
#  }else{
#    $save_to_log = weechat::config_get_plugin("save_to_log");
#  }

weechat::hook_modifier("weechat_print","colorize_cb", "");

weechat::hook_config( "plugins.var.perl.$SCRIPT_NAME.*", "toggle_config_by_set", "" );