cleaner messages, no popup if required
[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
29 my @new_filters = ("[global]\n");
30
31 ###############################################################################
32
33 my $mailbox;
34
35 open(FOLDERLIST, "<folderlist.xml") or die("Can't find 'folderlist.xml'\n");
36   while (<FOLDERLIST>) {
37     if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
38       $mailbox = $1;
39       last;
40     }
41   }
42 close FOLDERLIST;
43
44 ###############################################################################
45
46 open(FILTERRC, "<filterrc") or die("Can't find your old filter rules ('filterrc')\n");
47   while (<FILTERRC>) {
48     chomp();
49
50     my ($header_one,
51         $value_one,
52         $op,
53         $header_two,
54         $value_two,
55         $destination,
56         $mode_one,
57         $mode_two,
58         $action) = split(/\t/);
59
60     $action = $action eq "m" ? "move" : "delete";
61     $destination = $destination =~ m!^\#mh/! ?
62                      $destination :
63                      "#mh/$mailbox/$destination";
64
65     my ($predicate_one,
66         $predicate_two,
67         $match_type_one,
68         $match_type_two,
69         $new_filter);
70
71     ###########################################################################
72
73     if ($mode_one % 2 == 0) {
74       $predicate_one = "~";
75     }
76     else {
77       $predicate_one = "";
78     }
79
80     if ($mode_one <= 1) {
81       $match_type_one = "matchcase";
82     }
83     else {
84       $match_type_one = "regexpcase";
85     }
86
87     ###########################################################################
88
89     if ($mode_two % 2 == 0) {
90       $predicate_two = "~";
91     }
92     else {
93       $predicate_two = "";
94     }
95
96     if ($mode_two <= 1) {
97       $match_type_two = "matchcase";
98     }
99     else {
100       $match_type_two = "regexpcase";
101     }
102
103     ###########################################################################
104
105     if ($header_one eq "To" && $header_two eq "Cc" ||
106         $header_one eq "Cc" && $header_two eq "To" and
107         $value_one eq $value_two and
108         $mode_one eq $mode_two and
109         $op eq "|") {
110       if ($action eq "move") {
111         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
112       }
113       else {
114         $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
115       }
116     }
117     else {
118       if ($header_one =~ m/$normal_headers/) {
119         $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
120       }
121       else {
122         $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
123       }
124
125       if ($op ne " ") {
126         if ($header_two =~ m/$normal_headers/) {
127           $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
128         }
129         else {
130           $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
131         }
132       }
133
134       if (defined($new_filter)) {
135         if ($action eq "move") {
136           $new_filter .= qq( move "$destination"\n);
137         }
138         else {
139           $new_filter .= qq(delete\n);
140         }
141       }
142     }
143
144     ###########################################################################
145
146     push(@new_filters, $new_filter) if (defined($new_filter));
147   }
148 close(FILTERRC);
149
150 ###############################################################################
151
152 open(MATCHERRC, ">>matcherrc");
153   print MATCHERRC @new_filters;
154 close(MATCHERRC);
155
156 rename("filterrc", "filterrc.old");
157
158 ###############################################################################
159
160 print "Converted $#new_filters filters\n";
161 print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";