File: kicks.pl

package info (click to toggle)
irssi-scripts 20201016
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,068 kB
  • sloc: perl: 71,842; sh: 193; makefile: 6
file content (253 lines) | stat: -rw-r--r-- 7,496 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w
# various kick and ban commands
#  by c0ffee 
#    - http://www.penguin-breeder.org/irssi/

#<scriptinfo>
use strict;
use vars qw($VERSION %IRSSI);

use Irssi 20020120;
$VERSION = "0.27";
%IRSSI = (
    authors => "c0ffee",
    contact => "c0ffee\@penguin-breeder.org",
    name    => "Various kick and ban commands",
    description => "Enhances /k /kb and /kn with some nice options.",
    license => "Public Domain",
    url     => "http://www.penguin-breeder.org/irssi/",
    changed => "2017-03-31",
);
#</scriptinfo>

my %kickreasons  = (
    default => ["random kick victim",
            "no",
            "are you stupid?",
            "i don't like you, go away!",
            "oh, fsck off",
            "waste other ppls time, elsewhere",
            "get out and STAY OUT",
            "don't come back",
            "no thanks",
            "on popular demand, you are now leaving the channel",
            "\$N",
            "*void*",
            "/part is the command you're looking for",
            "this is the irssi of borg. your mIRC will be assimilated. resistance is futile.",
            "Autokick! mwahahahah!"
           ],
    none    => [""],
    topic   => ["\$topic"],
);


# fine tune the script for different chatnets
#    cmdline_k      regular expr that matches a correct cmdline for /k
#    req_chan       0/1 whether the channel is always part of the cmdline
#    num_nicks      number of nicks ... (-1 = inf)
#    start_with_dash    0/1 whether the normal cmdline may start with a dash
#    match_chn      matches channels
#    match_n        match nicks
#    match_reason   matches reasons
#    default_reason reason to give as "no reason"
my %cfg = (
    IRC => {
            cmdline_k       => '\s*([!#+&][^\x0\a\n\r ]*)\s+[-\[\]\\\\\`{}\w_|^\'~]+(,[-\[\]\\\\\`{}\w_|^\'~]+)*\s+\S.*',
            req_chan        => 0,
            num_nicks       => 3, # actually, /k takes more, but
                                  # normal irc servers only take
                          # three in a row
            start_with_dash => 1,
            match_chn       => '([!#+&][^\x0\a\n\r ]*)\s',
            match_n         => '(?:^|\s+)([-\[\]\\\\\`{}\w_|^\'~]+(?:,[-\[\]\\\\\`{}\w_|^\']+)*)',
            match_reason    => '^\s*[!#+&][^\x0\a\n\r ]*\s+[-\[\]\\\\\`{}\w_|^\'~]+(?:,[-\[\]\\\\\`{}\w_|^\'~]+)*\s+(\S.*)$',
            default_reason  => '$N'
        },

    SILC    => {
            cmdline_k   => '\s*[^\x0-\x20\*\?,@!]+\s+[^\x0-\x20\*\?,@!]+\s+\S.*',
            req_chan    => 1,
            num_nicks   => 1,
            start_with_dash => 0,
            match_chn   => '\s*([^\x0-\x20\*\?,@!]+)\s+[^\x0-\x20\*\?,@!]+(?:,[^\x0-\x20\*\?,@!]+)*',
            match_n     => '\s*(?:[^\x0-\x20\*\?,@!]+\s+)?([^\x0-\x20\*\?,@!]+(?:,[^\x0-\x20\*\?,@!]+)*)',
            match_reason    => '\s*[^\x0-\x20\*\?,@!]+\s+[^\x0-\x20\*\?,@!]+(?:,[^\x0-\x20\*\?,@!]+)*\s+(\S.*)',
            default_reason  => '$N'
        }
);

sub initialize {

    my $conf_file = Irssi::settings_get_str("kicks_configuration");
    $conf_file =~ s/~/$ENV{HOME}/;
    my ($basedir) = $conf_file =~ /^(.*\/).*?/;

    if (-f $conf_file) {
        open CONF, '<', $conf_file;

        my $line=0;
        
        while (<CONF>) {
            $line++;

            next if /^\s*#/;

            chomp;
            my ($key, $reasons) = /^(\S+)\s+(.+)\s*$/ or next;

            if ($reasons =~ /\`([^\s]+).*?\`/) {
                $kickreasons{$key} = "$reasons";
                Irssi::print("Added executable $1 as $key...");
                next;
            }

            $reasons =~ s/^"(.*)"$/$1/;
            $reasons =~ s/~/$ENV{HOME}/;
            $reasons =~ s/^([^\/])/$basedir$1/;
            
            if (-f $reasons) {

                $kickreasons{$key} = [];

                open REASON, '<',  $reasons;

                while (<REASON>) {
                    chomp;
                    push @{$kickreasons{$key}}, $_;
                }

                close REASON;
                Irssi::print("Loaded $reasons as $key...");
            } else {
                Irssi::print("can't parse config line $line...");
            }
        }
        close CONF;
    } else {
        Irssi::print("Could not find configuration file for kicks...");
        Irssi::print("... use /set kicks_configuration <file>");
    }
}

            
sub get_a_reason {
    my ($topic) = @_;


    return "" if not defined $kickreasons{$topic};

    $_ = eval $kickreasons{$topic}, chomp, s/[\n\t]+/ /mg, return $_
        if ref($kickreasons{$topic}) ne "ARRAY";

    return $kickreasons{$topic}[rand @{$kickreasons{$topic}}];
    
}

sub cmd_realkick {
    my ($data, $server, $channel, $cmd) = @_;
    my $reasons = "default";

    return if not $server
              or not defined $cfg{$server->{chat_type}}
          or not $channel
              or $data =~ /^$cfg{$server->{chat_type}}{cmdline_k}$/;

    Irssi::signal_stop();

    # let's see whether some options where supplied
    my $default = Irssi::settings_get_str("default_kick_options");
    $data = "$default $data" if not $default =~ /^\s*$/;
    my @opts = split /\s+/, $data;
    my $opt;
    my $fail=0;

    while (($opt) = (shift @opts) =~ /^\s*-(\S+)/) {
      
        $data =~ s/^\s*--\s+//, last if $opt eq "-";
      
        $data =~ s/^\s*-$opt\s+//, 
            $reasons = lc $opt,
        next if defined $kickreasons{lc $opt};

        last if $cfg{$server->{chat_type}}{start_with_dash};

        Irssi::print("Unknown option -$opt");
        $fail = 1;

    }

    return if $fail;

    my $chn = "";
    ($chn) = $data =~ /^$cfg{$server->{chat_type}}{match_chn}/;

    if ($cfg{$server->{chat_type}}{req_chan} && ($chn eq "")) {
      Irssi::print "Not joined to any channel";
      return;
    }

    # do we need to add a channel?
    if ($chn eq "") {
    Irssi::print("Not joined to any channel"), return 
        if $channel->{type} ne "CHANNEL";
        $chn = $channel->{name};

    $data = "$chn $data";
    }

    # is a reason already supplied?
    my $reason;
    $reason = get_a_reason($reasons)
        if not (($reason) = $data =~ /$cfg{$server->{chat_type}}{match_reason}/);

    $reason = $cfg{$server->{chat_type}}{default_reason}
        if $reason =~ /^\s*$/;
    
    my @nicks = split /,/, ($data =~ /$cfg{$server->{chat_type}}{match_n}/)[0];
    my $num_nicks = $cfg{$server->{chat_type}}{num_nicks};
    $num_nicks = @nicks if $num_nicks <= 0;

    my @commands;
    undef @commands;

    while (@nicks) {
        my $tmp = ($chn ne "" ? "$chn " : "") .
               join ",", (splice @nicks,0,$num_nicks);
        $tmp =~ s/([;\\\$])/\\$1/g;
        push @commands, "$tmp $reason";
    }
    
    foreach (@commands) {
      if ($_ =~ /^$cfg{$server->{chat_type}}{cmdline_k}$/) {
        s/\s+$//;
        $channel->command("EVAL $cmd $_");
      } else {
        Irssi::print("BUG: generated invalid $cmd command: $_");
      }
    }
}

sub cmd_kick {
    my ($data, $server, $channel) = @_;

    cmd_realkick $data, $server, $channel, "KICK";

}

sub cmd_kickban {
    my ($data, $server, $channel) = @_;

    cmd_realkick $data, $server, $channel, "KICKBAN";

}

Irssi::settings_add_str("misc", "default_kick_options", "");
Irssi::settings_add_str("misc", "kicks_configuration",
                Irssi::get_irssi_dir() . "/kicks.conf");

Irssi::command_bind("kick", "cmd_kick");
Irssi::command_bind("kickban", "cmd_kickban");

initialize();