forked from ABPindo/indonesianadblockrules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_generate_subscriptions.pl
316 lines (268 loc) · 7.8 KB
/
2_generate_subscriptions.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
#!C:\strawberry\perl\bin -w
# Script for the generation of the EasyList subscriptions
# Copyright 2011 Wladimir Palant and Michael
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use File::Basename;
use Digest::MD5 qw(md5_base64);
use Compress::Zlib;
use POSIX qw(strftime);
require Encode;
my $rootdir = dirname($0);
my $sourcedir = "$rootdir";
my $subsdir = "$rootdir/subscriptions";
my %verbatim = map {$_ => 1} qw(LICENSE);
unless (-d "$subsdir")
{
mkdir ($subsdir) || die "$subsdir does not exist and cannot be created.";
}
if (-f "$subsdir/.lock")
{
my $pid = readFile("$subsdir/.lock");
exit if kill(0, $pid);
}
writeFile("$subsdir/.lock", $$);
#system("hg", "pull", "--update", $sourcedir);
my %exists = ();
opendir(local *SOURCE, $sourcedir) || die "Could not open directory $sourcedir.";
foreach my $file (readdir(SOURCE))
{
next unless ($file =~ /\.txt$/ && $file ne "robots.txt") || exists($verbatim{$file});
my $data = readFile("$sourcedir/$file");
if (exists($verbatim{$file}))
{
$exists{$file} = 1;
if (-f "$subsdir/$file")
{
my $cmp = readFile("$subsdir/$file");
next if $cmp eq $data;
}
}
else
{
my ($header) = ($data =~ /^(.*)/);
next unless $header =~ /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i;
$data = resolveIncludes($data);
unless ($data)
{
warn "Failed to resolve includes in $file.";
$exists{$file} = -f "$subsdir/$file";
next;
}
$exists{$file} = 1;
$data =~ s/\r//g;
$data =~ s/\n+/\n/g;
$data =~ s/^.*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//gmi;
if (-f "$subsdir/$file")
{
my $cmp1 = readFile("$subsdir/$file");
$cmp1 =~ s/^.*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n//gmi;
$cmp1 =~ s/\s*\d+ \w+ \d+ \d+:\d+ UTC//g;
my $cmp2 = $data;
$cmp2 =~ s/\s*\d+ \w+ \d+ \d+:\d+ UTC//g;
next if $cmp1 eq $cmp2;
}
my $checksum = md5_base64($data);
$data =~ s/\n/\n! Checksum: $checksum\n/;
}
writeFile("$subsdir/$file", $data);
# unlink("$subsdir/$file.gz");
# system("7za", "a", "-tgzip", "-mx=9", "-mpass=15", "$subsdir/$file.gz", "$subsdir/$file") && warn "Failed to compress file $subsdir/$file. Please ensure that p7zip is installed on the system.";
}
closedir(SOURCE);
=pot
opendir(local *TARGET, $subsdir) || die "Could not open directory $subsdir.";
foreach my $file (readdir(TARGET))
{
next if $file =~ /^\./ || $file !~ /\.txt$/;
my $data = readFile("$subsdir/$file");
$file =~ s/\.txt$/.tpl/;
$exists{$file} = 1;
my $converted = convertToTPL($data);
if (-f "$subsdir/$file")
{
my $cmp = readFile("$subsdir/$file");
next if $cmp eq $converted;
}
writeFile("$subsdir/$file", $converted);
unlink("$subsdir/$file.gz");
system("7za", "a", "-tgzip", "-mx=9", "-mpass=15", "$subsdir/$file.gz", "$subsdir/$file") && warn "Failed to compress file $subsdir/$file. Please ensure that p7zip is installed on the system.";
}
=cut
opendir(local *TARGET, $subsdir) || die "Could not open directory $subsdir.";
foreach my $file (readdir(TARGET))
{
next if -d "$subsdir/$file" || $file =~ /\.gz$/ || exists($exists{$file});
unlink("$subsdir/$file");
unlink("$subsdir/$file.gz");
}
closedir(TARGET);
unlink("$subsdir/.lock");
sub resolveIncludes
{
my ($data, $level) = @_;
$level = $level || 0;
if ($level > 5)
{
warn "There are too many instances of \%include\% in the data. It is possible that circular reference is present.";
return undef;
}
my $failures = 0;
$data =~ s/^\s*%include\s+(.*)%\s*$/my $include = resolveInclude($1, $level);$include ? $include : $failures++/gem;
my $timestamp = strftime("%d %B %Y, %H:%M:%S WIB", localtime);
$data =~ s/%timestamp%/$timestamp/g;
return ($failures ? undef : $data);
}
sub resolveInclude
{
my ($file, $level) = @_;
if ($file =~ /^[\w\-\+][\w\.\-\+]*(?:\/[\w\-\+][\w\.\-\+]*)*$/ && -f "$sourcedir/$file")
{
my $data = readFile("$sourcedir/$file");
$data =~ s/\r//g;
$data =~ s/^.*?%timestamp%.*?$//gm;
$data =~ s/\n+/\n/g;
my ($header) = ($data =~ /^(.*)/);
$data =~ s/^.*\n// if $header =~ /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i;
$data = "! *** $file ***\n" . $data;
$data = resolveIncludes($data, $level + 1);
return $data;
}
elsif ($file =~ /^https?:\/\//)
{
my $param = $file;
$param =~ s/\W/\\$&/g;
my $data = `wget -q -O - $param`;
return undef unless $data;
$data = Compress::Zlib::memGunzip($data) || $data;
$data =~ s/\r//g;
$data =~ s/\n+/\n/g;
my ($header) = ($data =~ /^(.*)/);
return undef unless $header =~ /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i;
$data =~ s/^.*\n//;
$data = "! *** Fetched from: $file ***\n" . $data;
return $data;
}
return undef;
}
=pot
sub convertToTPL
{
my $data = shift;
my @result;
#Add necessary definitions
push @result, "msFilterList";
push @result, ": Expires=5";
foreach my $line (split(/\n/, $data))
{
my $original = $line;
#Translate comments, not including Adblock Plus specific values
if ($line =~m/\[.*?\]/i)
{
}
elsif ($line =~ m/^!/)
{
if ($line =~m/Expires/i)
{
#Eventually use this for the : Expires= value
}
elsif ($line !~ m/\!\s*checksum/i)
{
$line =~ tr/\!/#/;
push @result, $line;
}
}
else
{
#Remove lone third party options
$line =~ s/\$third-party$//;
#Translate domain blocks
if ($line =~ /^(|@@)\|\|.*?\^/)
{
$line =~ s/\^/ /;
}
elsif ($line =~ /^(|@@)\|\|.*?\//)
{
$line =~ s/\// \//;
}
#Remove unnecessary asterisks
$line =~ s/\*$//;
$line =~ s/ \*/ /;
#Remove unnecessary slashes and spaces
$line =~ s/ \/$//;
$line =~ s/ $//;
#Remove beginning and end anchors
unless ($line =~ m/^\|\|/)
{
$line =~ s/^\|//;
}
$line =~ s/\|($|\$)//;
#Translate the script option to "*.js"
$line =~ s/\$script$/\*\.js/;
#Translate whitelists, making them wider if necessary
if ($line =~ m/^@@\|\|.*?(^|\/)/)
{
$line =~ s/@@\|\|/\+d /;
#Remove all options
$line =~ s/\$.*?$//;
}
#Comment out all other whitelists, as a domain must be specified in Internet Explorer
elsif ($line =~ m/^@@/)
{
$line = "# " . $original;
}
#Comment out all filters with options or element hiding rules, as this functionality is not available in Internet Explorer
elsif ($line =~ m/(\$|##)/)
{
$line = "# " . $original;
}
#Translate all domain filters
elsif ($line =~ m/^\|\|.*?(^|\/)/)
{
$line =~ s/^\|\|/\-d /;
}
#Translate all other double anchored filters
elsif ($line =~ m/^\|\|/)
{
$line =~ s/^\|\|/\- http:\/\//;
}
#Translate all remaining filters as general
else
{
$line = "- " . $line;
}
push @result, $line;
}
}
return join("\n", @result) . "\n";
}
=cut
sub readFile
{
my $file = shift;
open(local *FILE, "<", $file) || die "Could not read file '$file'.";
binmode(FILE);
local $/;
my $result = <FILE>;
close(FILE);
return $result;
}
sub writeFile
{
my ($file, $contents) = @_;
open(local *FILE, ">", $file) || die "Could not write file '$file'.";
binmode(FILE);
print FILE $contents;
close(FILE);
}