exit if no rules found, rename filterrc after completion
[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         if ($folderlist =~ m/<folder type="mh"/) {
29                 $folderlist =~ s/<folder type="mh" name="//;
30                 $folderlist =~ s/" path="[A-Z0-9]+">\n//ig;
31                 $folderlist =~ s/^ +//;
32                 $mailbox = $folderlist;
33         }
34 }
35
36 open (FILTERRC, "<filterrc") || die("Can't find your old filter rules\n");
37 @filterrc = <FILTERRC>;
38 close FILTERRC;
39
40 if (!@filterrc) {
41         print "\nYou don't have any filter rules\n\n";
42         exit;
43 }
44
45 $WRITE_THIS = "";
46 $COUNT      = "0";
47
48 if (-e "matcherrc") {
49         open (MATCHER, "<matcherrc") || die("Can't open matcherrc\n");
50         @matcherrc = <MATCHER>;
51         close MATCHER;
52                 foreach $matcherrc (@matcherrc) {
53                         $WRITE_THIS .= $matcherrc;
54                 }
55         $WRITE_THIS .= "\n";
56 }
57
58 $WRITE_THIS .= "[global]\n";
59
60 foreach $filterrc (@filterrc) {
61         $COUNT++;
62         @split_lines = split("\t", $filterrc);
63         if ($split_lines[2] eq "&") {
64                 $operator = "&";
65                 &sort_data;
66         }
67         elsif ($split_lines[2] eq "|") {
68                 $operator = "|";
69                 &sort_data;
70         } elsif ($split_lines[0] eq "To") {
71                 $WRITE_THIS .= "to matchcase \"$split_lines[1]\"";
72         } elsif ($split_lines[0] eq "Reply-To") {
73                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\"";
74         } elsif ($split_lines[0] eq "Subject") {
75                 $WRITE_THIS .= "subject matchcase \"$split_lines[1]\"";
76         } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")){
77                 $WRITE_THIS .= "from matchcase \"$split_lines[1]\"";
78         }
79         if (!$split_lines[5]) {
80                 $WRITE_THIS .= " delete";
81         } elsif ($split_lines[8] eq "m\n"){
82                 $WRITE_THIS .= " move \"\#mh/$mailbox/$split_lines[5]\"";
83         }
84         $WRITE_THIS .= "\n";
85         @split_lines = "";
86 }
87
88 open (MATCHERRC, ">matcherrc");
89 print MATCHERRC $WRITE_THIS;
90 close MATCHERRC;
91
92 rename ("filterrc","filterrc.old");
93
94 print "\nYou have sucessfully converted $COUNT filtering rules\n\n";
95 print "'filterrc' has been renamed 'filterrc.old'\n\n";
96 exit;
97
98 sub sort_data {
99         if ($split_lines[0] eq "To" && $split_lines[3] eq "Cc" && 
100         $split_lines[1] eq $split_lines[4]) {
101                 $WRITE_THIS .= "to_or_cc matchcase \"$split_lines[1]\"";                
102         }
103         elsif ($split_lines[0] eq "To") {
104                 $WRITE_THIS .= "to matchcase \"$split_lines[1]\" $operator ";
105         } elsif ($split_lines[0] eq "Reply-To") {
106                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\" $operator ";
107         } elsif ($split_lines[0] eq "Subject") {
108                 $WRITE_THIS .= "subject matchcase \"$split_lines[1]\" $operator ";
109         } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")) {
110                 $WRITE_THIS .= "from matchcase \"$split_lines[1]\" $operator ";
111         }
112         if ($split_lines[3] eq "To") {
113                 $WRITE_THIS .= "to matchcase \"$split_lines[4]\"";
114         } elsif ($split_lines[3] eq "Reply-To") {
115                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[4]\"";
116         } elsif ($split_lines[3] eq "Subject") {
117                 $WRITE_THIS .= "subject matchcase \"$split_lines[4]\"";
118         } elsif (($split_lines[3] eq "From") || ($split_lines[3] eq "Sender")) {
119                 $WRITE_THIS .= "from matchcase \"$split_lines[4]\"";
120         }
121 }