sync with 0.7.5
[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]+"//ig;
32                         $folderlist =~ s/ collapsed="1"//;
33                         $folderlist =~ s/>\n//ig;
34                         $folderlist =~ s/^ +//;
35                         $mailbox = $folderlist;
36                 }
37         }
38 }
39
40 open (FILTERRC, "<filterrc") || die("Can't find your old filter rules\n");
41 @filterrc = <FILTERRC>;
42 close FILTERRC;
43
44 if (!@filterrc) {
45         print "\nYou don't have any filter rules\n\n";
46         exit;
47 }
48
49 $WRITE_THIS = "";
50 $COUNT      = "0";
51
52 if (-e "matcherrc") {
53         open (MATCHER, "<matcherrc") || die("Can't open matcherrc\n");
54         @matcherrc = <MATCHER>;
55         close MATCHER;
56                 foreach $matcherrc (@matcherrc) {
57                         $WRITE_THIS .= $matcherrc;
58                 }
59         $WRITE_THIS .= "\n";
60 }
61
62 $WRITE_THIS .= "[global]\n";
63
64 foreach $filterrc (@filterrc) {
65         $COUNT++;
66         @split_lines = split("\t", $filterrc);
67         if ($split_lines[6] eq "0") {
68                 $match_type = "matchcase";
69                 $predicate_one = "~";
70         } elsif ($split_lines[6] eq "1") {
71                 $match_type = "matchcase";
72                 $predicate_one = "";
73         } elsif ($split_lines[6] eq "4") {
74                 $match_type = "regexpcase";
75                 $predicate_one = "~";
76         } elsif ($split_lines[6] eq "5") {
77                 $match_type =  "regexpcase";
78                 $predicate_one = "";
79         }
80         if ($split_lines[7] eq "0") {
81                 $match_type = "matchcase";
82                 $predicate_two = "~";
83         } elsif ($split_lines[7] eq "1") {
84                 $match_type = "matchcase";
85                 $predicate_two = "";
86         } elsif ($split_lines[7] eq "4") {
87                 $match_type = "regexpcase";
88                 $predicate_two = "~";
89         } elsif ($split_lines[7] eq "5") {
90                 $match_type =  "regexpcase";
91                 $predicate_two = "";
92         }
93
94         if ($split_lines[2] eq "&") {
95                 $operator = "&";
96                 &sort_data;
97         }
98         elsif ($split_lines[2] eq "|") {
99                 $operator = "|";
100                 &sort_data;
101         } elsif ($split_lines[0] eq "To") {
102                 $WRITE_THIS .= "$predicate_one"."to $match_type \"$split_lines[1]\"";
103         } elsif ($split_lines[0] eq "Reply-To") {
104                 $WRITE_THIS .= "$predicate_one"."inreplyto $match_type \"$split_lines[1]\"";
105         } elsif ($split_lines[0] eq "Subject") {
106                 $WRITE_THIS .= "$predicate_one"."subject $match_type \"$split_lines[1]\"";
107         } elsif ($split_lines[0] eq "From"){
108                 $WRITE_THIS .= "$predicate_one"."from $match_type \"$split_lines[1]\"";
109         } elsif ($split_lines[0] eq "Sender" || $split_lines[0] eq "List-Id" || 
110                  $split_lines[0] eq "X-ML-Name" || $split_lines[0] eq "X-List" ||
111                  $split_lines[0] eq "X-Sequence" || $split_lines[0] eq "X-Mailer" ||
112                  $split_lines[0] eq "Cc") {
113                 $WRITE_THIS .= "$predicate_one"."header \"$split_lines[0]\" $match_type \"$split_lines[1]\"";
114         }
115         if ($split_lines[8] eq "n\n") {
116                 $WRITE_THIS .= " delete";
117         } elsif ($split_lines[8] eq "m\n"){
118                 $WRITE_THIS .= " move \"\#mh/$mailbox/$split_lines[5]\"";
119         }
120         $WRITE_THIS .= "\n";
121         @split_lines = "";
122 }
123
124 open (MATCHERRC, ">matcherrc");
125 print MATCHERRC $WRITE_THIS;
126 close MATCHERRC;
127
128 rename ("filterrc","filterrc.old");
129
130 print "\nYou have sucessfully converted $COUNT filtering rules\n\n";
131 print "'filterrc' has been renamed 'filterrc.old'\n\n";
132 exit;
133
134 sub sort_data {
135         if ($split_lines[0] eq "To" && $split_lines[3] eq "Cc" && 
136         $split_lines[1] eq $split_lines[4]) {
137                 $WRITE_THIS .= "$predicate_one"."to_or_cc $match_type \"$split_lines[1]\"";             
138         }
139         elsif ($split_lines[0] eq "To") {
140                 $WRITE_THIS .= "$predicate_one"."to $match_type \"$split_lines[1]\" $operator ";
141         } elsif ($split_lines[0] eq "Reply-To") {
142                 $WRITE_THIS .= "$predicate_one"."inreplyto $match_type \"$split_lines[1]\" $operator ";
143         } elsif ($split_lines[0] eq "Subject") {
144                 $WRITE_THIS .= "$predicate_one"."subject $match_type \"$split_lines[1]\" $operator ";
145         } elsif ($split_lines[0] eq "From") {
146                 $WRITE_THIS .= "$predicate_one"."from $match_type \"$split_lines[1]\" $operator ";
147         } elsif ($split_lines[0] eq "Sender" || $split_lines[0] eq "List-Id" || 
148                  $split_lines[0] eq "X-ML-Name" || $split_lines[0] eq "X-List" ||
149                  $split_lines[0] eq "X-Sequence" || $split_lines[0] eq "X-Mailer" ||
150                  $split_lines[0] eq "Cc") {
151                 $WRITE_THIS .= "$predicate_one"."header \"$split_lines[0]\" $match_type \"$split_lines[1]\" $operator ";
152         }
153
154         if ($split_lines[3] eq "To") {
155                 $WRITE_THIS .= "$predicate_two"."to $match_type \"$split_lines[4]\"";
156         } elsif ($split_lines[3] eq "Reply-To") {
157                 $WRITE_THIS .= "$predicate_two"."inreplyto $match_type \"$split_lines[4]\"";
158         } elsif ($split_lines[3] eq "Subject") {
159                 $WRITE_THIS .= "$predicate_two"."subject $match_type \"$split_lines[4]\"";
160         } elsif ($split_lines[3] eq "From") {
161                 $WRITE_THIS .= "$predicate_two"."from $match_type \"$split_lines[4]\"";
162         } elsif ($split_lines[3] eq "Sender" || $split_lines[3] eq "List-Id" || 
163                  $split_lines[3] eq "X-ML-Name" || $split_lines[3] eq "X-List" ||
164                  $split_lines[3] eq "X-Sequence" || $split_lines[3] eq "X-Mailer" ||
165                  $split_lines[3] eq "Cc") {
166                 $WRITE_THIS .= "$predicate_two"."header \"$split_lines[3]\" $match_type \"$split_lines[4]\"";
167         } 
168
169 }