-
Notifications
You must be signed in to change notification settings - Fork 0
/
automode.pl
321 lines (259 loc) · 8.22 KB
/
automode.pl
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
# automode.pl
#
# Passively learn and actively maintain the ops/voices/halfops in channels.
# This is a no-maintenance auto-op/auto-voice script for irssi.
#
# INSTALL:
# 1) /script load automode.pl
# 2) Be a channel operator
#
# HOW IT WORKS:
# When someone joins a channel and is given ops/voice/halfop, the script
# will record that user's mask, as a combination of their nickname and part
# of their hostname or IP address. When that person leaves and rejoins, the
# script will check against its database and regrant the user the modes
# he or she had before leaving.
#
# If a user is kicked from a channel, all modes for that person are removed.
# They must therefore be regiven by another operator manually. Note this.
#
# Also, this script relies on the "chatnet" attribute being set for a
# particular connection. Use /network (or /ircnet) to set up your networks
# and such. This script will spit out (lots of) warnings if the chatnet is
# not set.
#
# IGNORING CHANNELS:
# If you do not wish to maintain modes on a channel, add it to the setting
# "automode_ignore" in the form <tag>:<channel>, separated by spaces.
#
# For example: /set automode_ignore FreeNode:#perl EFnet:#irssi
# (should) not maintain modes in #perl on FreeNode or #irssi
# on EFnet, provided FreeNode and EFnet are the tags for those
# connections.
#
# NOTES:
# The Perl module Data::Serializer is needed for this script.
# The database file is not written instantaneously; it is on a timer and is
# written to every five minutes or so. If the script is reloaded before it has
# had a chance to save will result in forgotten modes.
#
use strict;
use Irssi;
use Data::Serializer;
use Data::Dumper;
use vars qw($VERSION %IRSSI);
$VERSION = '1.3';
%IRSSI = (
authors => 'Matt "f0rked" Sparks',
contact => '[email protected]',
name => 'automode',
description => 'Mode maintainer',
license => 'BSD',
url => 'http://quadpoint.org',
changed => '2008-06-14',
);
# show debug lines
my $debug = 0;
my $s = new Data::Serializer;
my $file = Irssi::get_irssi_dir."/automode_list";
if (!-e $file) {
print "[automode] creating $file";
system("touch $file");
}
my $listref = $s->retrieve($file);
my %list = $listref ? %{$listref} : ();
#print Dumper %list;
my $save_tag;
my %buffer_tags;
my %buffer;
sub save_list
{
$s->store(\%list,$file);
}
sub clear_list
{
%list = ();
}
sub make_mask
{
my($address) = @_;
return if !$address;
my($ident, $host) = split /@/, $address;
my @split = split /\./, $host;
if (@split <= 2) {
# host is something like "foo.com". We cannot make the mask *.com.
} else {
if ($split[$#split] =~ /^\d+$/) {
# Looks like an IP address.
pop @split;
$host = join(".", @split) . ".\d{1,3}";
} else {
# Mask the first segment.
shift @split;
$host = ".+?." . join(".", @split);
}
}
return ".+?!.*${ident}@" . "${host}";
}
sub show
{
my($net, $channel) = @_;
print Dumper %{$list{$net}->{$channel}};
}
sub show_all
{
my $list;
print Dumper %list;
}
sub clear_channel
{
my($net, $channel) = @_;
delete $list{$net}->{$channel};
}
sub set_modes
{
my($net, $channel) = @{$_[0]};
return if !$buffer{$net}->{$channel};
my($nicks, $modes) = values(%{$buffer{$net}->{$channel}});
print "[automode] modes: $modes, nicks: $nicks" if $debug;
my $c = Irssi::server_find_chatnet($net)->channel_find($channel);
# iterate through the modes and see which ones we don't have to set
my($final_modes, $final_nicks);
my $i = 0;
for (split //,$modes) {
my $m = $_;
my $n = (split / /, $nicks)[$i];
$i++;
next if (!$c->nick_find($n));
next if ($m eq "o" && $c->nick_find($n)->{"op"});
next if ($m eq "v" && $c->nick_find($n)->{"voice"});
next if ($m eq "h" && $c->nick_find($n)->{"halfop"});
# if we made it this far, add this to the final modes
$final_modes .= $m;
$final_nicks .= "$n ";
}
print "[automode] final modes: +$final_modes $final_nicks" if $debug;
$c->command("MODE $channel +$final_modes $final_nicks")
if ($final_modes && $final_nicks);
delete $buffer{$net}->{$channel};
}
sub mode2letter
{
my($mode) = @_;
if ($mode eq "@") {
return "o";
} elsif ($mode eq "+") {
return "v";
} elsif ($mode eq "%") {
return "h";
}
return -1;
}
sub remove_mode
{
my($net, $channel, $mask, $mode) = @_;
my $letter = mode2letter($mode);
$list{$net}->{$channel}->{$mask} =~ s/$letter//
if user_modes($net, $channel, $mask);
delete $list{$net}->{$channel}->{$mask}
if exists $list{$net}->{$channel}->{$mask}
and !$list{$net}->{$channel}->{$mask};
}
sub remove_all
{
my($net, $channel, $mask) = @_;
delete $list{$net}->{$channel}->{$mask}
if exists $list{$net}->{$channel}->{$mask};
}
sub user_modes
{
my($net, $channel, $mask) = @_;
return $list{$net}->{$channel}->{$mask};
}
sub add_mode
{
my($net, $channel, $mask, $mode) = @_;
return if !$mask or !$net or !$channel or !$mode;
my $letter = mode2letter($mode);
$list{$net}->{$channel}->{$mask} .= $letter
if $list{$net}->{$channel}->{$mask} !~ /$letter/;
Irssi::timeout_remove($save_tag);
$save_tag = Irssi::timeout_add_once(300, "save_list", []);
}
sub event_mode
{
my($channel, $nick, $setby, $mode, $type) = @_;
return if check_ignore($channel->{server}, $channel->{name});
my $w = Irssi::active_win;
return if $mode != '@' and $mode != '%' and $mode != '+';
my $chatnet = $channel->{server}->{chatnet};
my $tag = $channel->{server}->{tag};
print ("[automode] The 'chatnet' attribute is missing for the tag '$tag'. " .
"Use /network (or /ircnet) to properly manage this.") if !$chatnet;
return if !$chatnet;
my $mask = make_mask($nick->{host});
print "[automode] failed to make mask ($mask)" if (!$mask && $debug);
return if !$mask;
if ($type eq "+") {
print ("[automode] adding mode '$mode' for $mask in $channel->{name} on " .
$chatnet) if $debug;
add_mode($chatnet, $channel->{name}, $mask, $mode);
} else {
# don't remove op if they deop themselves.
return if $setby eq $nick->{nick};
print ("[automode] removing mode '$mode' for $mask in $channel->{name} " .
" on $chatnet") if $debug;
remove_mode($chatnet, $channel->{name}, $mask, $mode);
}
#show($chatnet, $channel->{name});
}
sub event_join
{
my($server, $channel, $nick, $address) = @_;
return if check_ignore($server, $channel);
my $mask = make_mask($address);
return if !user_modes($server->{chatnet}, $channel, $mask);
my $c = $server->channel_find($channel);
return if not $c->{chanop};
if (my $modes = user_modes($server->{chatnet}, $channel, $mask)) {
print "[automode] Matched mask ($mask) with modes: $modes" if $debug;
my $nick_list = "$nick " x length($modes);
my %buf = ($buffer{$server->{chatnet}}->{$channel} ?
%{$buffer{$server->{chatnet}}->{$channel}} : ());
$buf{modes} .= $modes;
$buf{nicks} .= $nick_list;
$buffer{$server->{chatnet}}->{$channel} = \%buf;
my $tag = $server->{chatnet} . "_$channel";
Irssi::timeout_remove($buffer_tags{$tag});
$buffer_tags{$tag} = Irssi::timeout_add_once(1000 + int(rand(250) * 3),
"set_modes",
[$server->{chatnet},
$channel]);
#print Dumper %buffer;
}
}
sub event_kick
{
my($server, $channel, $nick, $kicker, $address, $reason) = @_;
my $n = $server->channel_find($channel)->nick_find($nick);
#print Dumper $n;
my $mask = make_mask($n->{host});
remove_all($server->{chatnet}, $channel, $mask) if $mask;
}
sub check_ignore
{
my($server, $channel) = @_;
my $chatnet = $server->{chatnet};
my $ignore = Irssi::settings_get_str("automode_ignore") . " ";
return ($ignore =~ /$chatnet:$channel /i) ? 1 : 0;
}
# I don't think this does what I want it to do.
sub event_exit
{
save_list;
}
Irssi::signal_add("gui exit", "event_exit");
Irssi::signal_add("message kick", "event_kick");
Irssi::signal_add("message join", "event_join");
Irssi::signal_add("nick mode changed", "event_mode");
Irssi::settings_add_str("automode", "automode_ignore", "IM:&bitlbee");