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