privoxy-log-parser.pl: Unbreak the gathering of host statistics with http requests
[privoxy.git] / tools / url-pattern-translator.pl
1 #!/usr/bin/perl
2
3 ############################################################################
4 #
5 # url-pattern-translator
6 #
7 # Filters Privoxy action files and changes old-school URL patterns to
8 # use extended regular expressions for the host as well.
9 #
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.
13 #
14 # Usage:
15 #
16 # url-pattern-translator.pl old.action > new.action 
17 #
18 # Only convert your files once, or, as RoboCop used to say,
19 # there will be... trouble.
20 #
21 # Copyright (c) 2008 Fabian Keil <fk@fabiankeil.de>
22 #
23 # Permission to use, copy, modify, and distribute this software for any
24 # purpose with or without fee is hereby granted, provided that the above
25 # copyright notice and this permission notice appear in all copies.
26 #
27 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
28 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
29 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
30 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
32 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
33 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 #
35 ############################################################################
36
37 use strict;
38 use warnings;
39
40 sub p ($) {
41     my $message = shift;
42     print $message . "\n";
43 }
44
45 sub convert_host_pattern ($) {
46     my $host_pattern = shift;
47     my $hp = $host_pattern;
48
49     $hp =~ s@\s@@g;
50
51     if ($hp =~ m@^\.@) {
52         # Not left-anchored
53         #
54         # XXX: This is somewhat ugly and while it's
55         # the equivalent pattern in most cases
56         # \. should be good enough.
57         $hp =~ s@^\.@(^|.)@;
58     } else {
59         # left-anchored
60         $hp = '^' . $hp;
61     }
62
63     # Match-all syntax has changed ...
64     $hp =~ s@\*@[^.]*@g;
65
66     # Extended host patterns are right-anchored by default
67     $hp =~ s@\.$@(\..*)?@;
68
69     # Literal dots have to be escaped    
70     $hp =~ s@((?<!\\)\.[^*])@\\$1@g;
71
72     # Match single character with a dot.
73     $hp =~ s@(?<!\))\?@.@g;
74
75     return $hp;
76 }
77
78 sub looks_interesting($) {
79     my $line = shift;
80     my $type_to_skip = undef;
81
82     if (/^\s*\#/) {
83
84         $type_to_skip = "comment";
85
86     } elsif (/[{}]/ or /\\$/) {
87
88         $type_to_skip = "action settings";
89
90     } elsif (m@^\s*$@) {
91
92         $type_to_skip = "whitespace";
93
94     } elsif (m@^\s*TAG:@) {
95
96         $type_to_skip = "tag patttern";
97
98     } elsif (m@^[^/]*=@) {
99
100         $type_to_skip = "macro or version definition";
101
102     } elsif (m@^\s*standard\.@) {
103
104         $type_to_skip = "predefined settings";
105
106     }
107
108     #p("Skipping " . $type_to_skip . ": " . $_) if defined $type_to_skip;
109
110     return not defined $type_to_skip;
111 }
112
113 sub main () {
114     my $host = undef;
115     my $path = undef;
116  
117     while (<>) {
118         chomp;
119
120         if (looks_interesting($_)) {
121             if (m@^([^/]+)(/.*)$@) {
122                 $host = $1;
123                 $path = $2;
124                 $host = convert_host_pattern($host);
125                 $_ = $host . $path;
126             }
127             elsif (m@^([^/]*)$@) {
128                 $host = $1;
129                 $host = convert_host_pattern($host);
130                 $_ = $host;
131             }
132         }
133         p($_);
134     }
135 }
136
137 main();