3 ############################################################################
5 # url-pattern-translator
7 # Filters Privoxy action files and changes old-school URL patterns to
8 # use extended regular expressions for the host as well.
10 # While it works good enough to satisfy the regression tests in
11 # default.action.master, it isn't perfect and you should double-check
12 # the output and keep backups of your old action files.
16 # url-pattern-translator.pl old.action > new.action
18 # Copyright (c) 2008 Fabian Keil <fk@fabiankeil.de>
20 # Permission to use, copy, modify, and distribute this software for any
21 # purpose with or without fee is hereby granted, provided that the above
22 # copyright notice and this permission notice appear in all copies.
24 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
25 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
26 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
27 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
29 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 ############################################################################
39 print $message . "\n";
42 sub convert_host_pattern ($) {
43 my $host_pattern = shift;
44 my $hp = $host_pattern;
51 # XXX: This is somewhat ugly and while it's
52 # the equivalent pattern in most cases
53 # \. should be good enough.
60 # Match-all syntax has changed ...
63 # Extended host patterns are right-anchored by default
64 $hp =~ s@\.$@(\..*)?@;
66 # Literal dots have to be escaped
67 $hp =~ s@((?<!\\)\.[^*])@\\$1@g;
69 # Match single character with a dot.
70 $hp =~ s@(?<!\))\?@.@g;
73 $hp = "PCRE-HOST-PATTERN:" . $hp;
78 sub looks_interesting($) {
80 my $type_to_skip = undef;
84 $type_to_skip = "comment";
86 } elsif (/[{}]/ or /\\$/) {
88 $type_to_skip = "action settings";
92 $type_to_skip = "whitespace";
94 } elsif (m@^\s*CLIENT-TAG:@i) {
96 $type_to_skip = "client tag patttern";
98 } elsif (m@^\s*TAG:@i) {
100 $type_to_skip = "tag patttern";
102 } elsif (m@^[^/]*=@) {
104 $type_to_skip = "macro or version definition";
106 } elsif (m@^\s*standard\.@) {
108 $type_to_skip = "predefined settings";
110 } elsif (m@^\s*PCRE-HOST-PATTERN:@i) {
112 $type_to_skip = "already converted pcre host patttern";
116 #p("Skipping " . $type_to_skip . ": " . $_) if defined $type_to_skip;
118 return not defined $type_to_skip;
128 if (looks_interesting($_)) {
129 if (m@^([^/]+)(/.*)$@) {
132 $host = convert_host_pattern($host);
135 elsif (m@^([^/]*)$@) {
137 $host = convert_host_pattern($host);