clean up some warnings
[claws.git] / tools / filter_conv.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 #  * Copyright 2002 Paul Mangan <claws@thewildbeast.co.uk>
5 #  *
6 #  * Reimplemented by Torsten Schoenfeld <kaffeetisch@web.de>
7 #  *
8 #  * This file is free software; you can redistribute it and/or modify it
9 #  * under the terms of the GNU General Public License as published by
10 #  * the Free Software Foundation; either version 2 of the License, or
11 #  * (at your option) any later version.
12 #  *
13 #  * This program is distributed in the hope that it will be useful, but
14 #  * WITHOUT ANY WARRANTY; without even the implied warranty of
15 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 #  * General Public License for more details.
17 #  *
18 #  * You should have received a copy of the GNU General Public License
19 #  * along with this program; if not, write to the Free Software
20 #  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #  *
22
23 chdir($ENV{ HOME } . "/.sylpheed") or die("You don't appear to have Sylpheed installed\n");
24
25 ###############################################################################
26
27 my $normal_headers = qr/^(?:Subject|From|To|Cc)$/;
28 my $extra_headers = qr/^(?:Reply-To|Sender|List-Id|X-ML-Name|X-List|X-Sequence|X-Mailer)$/;
29
30 my @new_filters = ("[global]\n");
31
32 ###############################################################################
33
34 my $mailbox;
35
36 open(FOLDERLIST, "<folderlist.xml") or die("Can't find 'folderlist.xml'\n");
37   while (<FOLDERLIST>) {
38     if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
39       $mailbox = $1;
40       last;
41     }
42   }
43 close FOLDERLIST;
44
45 ###############################################################################
46
47 open(FILTERRC, "<filterrc") or die("Can't find your old filter rules ('filterrc')\n");
48   while (<FILTERRC>) {
49     chomp();
50
51     my ($header_one,
52         $value_one,
53         $op,
54         $header_two,
55         $value_two,
56         $destination,
57         $mode_one,
58         $mode_two,
59         $action) = split(/\t/);
60
61     $action = $action eq "m" ? "move" : "delete";
62     $destination = $destination =~ m!^\#mh/! ?
63                      $destination :
64                      "#mh/$mailbox/$destination";
65
66     my ($predicate_one,
67         $predicate_two,
68         $match_type_one,
69         $match_type_two,
70         $new_filter);
71
72     ###########################################################################
73
74     if ($mode_one % 2 == 0) {
75       $predicate_one = "~";
76     }
77     else {
78       $predicate_one = "";
79     }
80
81     if ($mode_one <= 1) {
82       $match_type_one = "matchcase";
83     }
84     else {
85       $match_type_one = "regexpcase";
86     }
87
88     ###########################################################################
89
90     if ($mode_two % 2 == 0) {
91       $predicate_two = "~";
92     }
93     else {
94       $predicate_two = "";
95     }
96
97     if ($mode_two <= 1) {
98       $match_type_two = "matchcase";
99     }
100     else {
101       $match_type_two = "regexpcase";
102     }
103
104     ###########################################################################
105
106     if ($header_one eq "To" && $header_two eq "Cc" ||
107         $header_one eq "Cc" && $header_two eq "To" and
108         $value_one eq $value_two and
109         $mode_one eq $mode_two and
110         $op eq "|") {
111       if ($action eq "move") {
112         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
113       }
114       else {
115         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
116       }
117     }
118     else {
119       if ($header_one =~ m/$normal_headers/) {
120         $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
121       }
122       elsif ($header_one =~ m/$extra_headers/) {
123         $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
124       }
125
126       if ($op ne " ") {
127         if ($header_two =~ m/$normal_headers/) {
128           $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
129         }
130         elsif ($header_two =~ m/$extra_headers/) {
131           $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
132         }
133       }
134
135       if (defined($new_filter)) {
136         if ($action eq "move") {
137           $new_filter .= qq( move "$destination"\n);
138         }
139         else {
140           $new_filter .= qq(delete\n);
141         }
142       }
143     }
144
145     ###########################################################################
146
147     push(@new_filters, $new_filter) if (defined($new_filter));
148   }
149 close(FILTERRC);
150
151 ###############################################################################
152
153 open(MATCHERRC, ">>matcherrc");
154   print MATCHERRC @new_filters;
155 close(MATCHERRC);
156
157 rename("filterrc", "filterrc.old");
158
159 ###############################################################################
160
161 print "Converted $#new_filters filters\n";
162 print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";