2008-04-23 [paul] 3.4.0cvs8
[claws.git] / tools / filter_conv.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 #  * Copyright 2002 Paul Mangan <paul@claws-mail.org>
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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #  *
22
23 my $old_config_dir = "$ENV{HOME}/.sylpheed";
24 my $config_dir = `claws-mail --config-dir`;
25 chomp $config_dir;
26
27 chdir($ENV{ HOME } . "/$config_dir")
28         or die("You don't appear to have Claws Mail installed\n");
29
30 ###############################################################################
31
32 my $normal_headers = qr/^(?:Subject|From|To|Cc)$/;
33
34 my @new_filters = ("[global]\n");
35
36 ###############################################################################
37
38 my $mailbox;
39
40 open(FOLDERLIST, "<$old_config_dir/folderlist.xml")
41         or die("Can't find '$old_config_dir/folderlist.xml'\n");
42   while (<FOLDERLIST>) {
43     if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
44       $mailbox = $1;
45       last;
46     }
47   }
48 close FOLDERLIST;
49
50 ###############################################################################
51
52 open(FILTERRC, "<$old_config_dir/filterrc")
53         or die("Can't find your old filter rules ('$old_config_dir/filterrc')\n");
54   while (<FILTERRC>) {
55     chomp();
56
57     my ($header_one,
58         $value_one,
59         $op,
60         $header_two,
61         $value_two,
62         $destination,
63         $mode_one,
64         $mode_two,
65         $action) = split(/\t/);
66
67     $value_one =~ s/\"/\\\"/g ;
68     $value_two =~ s/\"/\\\"/g ;
69     $action = $action eq "m" ? "move" : "delete";
70     $destination = $destination =~ m!^\#mh/! ?
71                      $destination :
72                      "#mh/$mailbox/$destination";
73
74     my ($predicate_one,
75         $predicate_two,
76         $match_type_one,
77         $match_type_two,
78         $new_filter);
79
80     ###########################################################################
81
82     if ($mode_one % 2 == 0) {
83       $predicate_one = "~";
84     }
85     else {
86       $predicate_one = "";
87     }
88
89     if ($mode_one <= 1) {
90       $match_type_one = "matchcase";
91     }
92     else {
93       $match_type_one = "regexpcase";
94     }
95
96     ###########################################################################
97
98     if ($mode_two % 2 == 0) {
99       $predicate_two = "~";
100     }
101     else {
102       $predicate_two = "";
103     }
104
105     if ($mode_two <= 1) {
106       $match_type_two = "matchcase";
107     }
108     else {
109       $match_type_two = "regexpcase";
110     }
111
112     ###########################################################################
113
114     if ($header_one eq "To" && $header_two eq "Cc" ||
115         $header_one eq "Cc" && $header_two eq "To" and
116         $value_one eq $value_two and
117         $mode_one eq $mode_two and
118         $op eq "|") {
119       if ($action eq "move") {
120         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
121       }
122       else {
123         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
124       }
125     }
126     else {
127       if ($header_one =~ m/$normal_headers/) {
128         $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
129       }
130       else {
131         $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
132       }
133
134       if ($op ne " ") {
135         if ($header_two =~ m/$normal_headers/) {
136           $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
137         }
138         else {
139           $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
140         }
141       }
142
143       if (defined($new_filter)) {
144         if ($action eq "move") {
145           $new_filter .= qq( move "$destination"\n);
146         }
147         else {
148           $new_filter .= qq(delete\n);
149         }
150       }
151     }
152
153     ###########################################################################
154
155     push(@new_filters, $new_filter) if (defined($new_filter));
156   }
157 close(FILTERRC);
158
159 ###############################################################################
160
161 open(MATCHERRC, ">>matcherrc");
162   print MATCHERRC @new_filters;
163 close(MATCHERRC);
164
165 print "Converted $#new_filters filters\n";
166
167 if ($old_config_dir eq $config_dir) {
168         rename("filterrc", "filterrc.old");
169         print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";
170 }
171 ###############################################################################
172