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