allow for collapsed folder trees
[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]+"//ig;
31                 $folderlist =~ s/ collapsed="1"//;
32                 $folderlist =~ s/>\n//ig;
33                 $folderlist =~ s/^ +//;
34                 $mailbox = $folderlist;
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[2] eq "&") {
66                 $operator = "&";
67                 &sort_data;
68         }
69         elsif ($split_lines[2] eq "|") {
70                 $operator = "|";
71                 &sort_data;
72         } elsif ($split_lines[0] eq "To") {
73                 $WRITE_THIS .= "to matchcase \"$split_lines[1]\"";
74         } elsif ($split_lines[0] eq "Reply-To") {
75                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\"";
76         } elsif ($split_lines[0] eq "Subject") {
77                 $WRITE_THIS .= "subject matchcase \"$split_lines[1]\"";
78         } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")){
79                 $WRITE_THIS .= "from matchcase \"$split_lines[1]\"";
80         }
81         if (!$split_lines[5]) {
82                 $WRITE_THIS .= " delete";
83         } elsif ($split_lines[8] eq "m\n"){
84                 $WRITE_THIS .= " move \"\#mh/$mailbox/$split_lines[5]\"";
85         }
86         $WRITE_THIS .= "\n";
87         @split_lines = "";
88 }
89
90 open (MATCHERRC, ">matcherrc");
91 print MATCHERRC $WRITE_THIS;
92 close MATCHERRC;
93
94 rename ("filterrc","filterrc.old");
95
96 print "\nYou have sucessfully converted $COUNT filtering rules\n\n";
97 print "'filterrc' has been renamed 'filterrc.old'\n\n";
98 exit;
99
100 sub sort_data {
101         if ($split_lines[0] eq "To" && $split_lines[3] eq "Cc" && 
102         $split_lines[1] eq $split_lines[4]) {
103                 $WRITE_THIS .= "to_or_cc matchcase \"$split_lines[1]\"";                
104         }
105         elsif ($split_lines[0] eq "To") {
106                 $WRITE_THIS .= "to matchcase \"$split_lines[1]\" $operator ";
107         } elsif ($split_lines[0] eq "Reply-To") {
108                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\" $operator ";
109         } elsif ($split_lines[0] eq "Subject") {
110                 $WRITE_THIS .= "subject matchcase \"$split_lines[1]\" $operator ";
111         } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")) {
112                 $WRITE_THIS .= "from matchcase \"$split_lines[1]\" $operator ";
113         }
114         if ($split_lines[3] eq "To") {
115                 $WRITE_THIS .= "to matchcase \"$split_lines[4]\"";
116         } elsif ($split_lines[3] eq "Reply-To") {
117                 $WRITE_THIS .= "inreplyto matchcase \"$split_lines[4]\"";
118         } elsif ($split_lines[3] eq "Subject") {
119                 $WRITE_THIS .= "subject matchcase \"$split_lines[4]\"";
120         } elsif (($split_lines[3] eq "From") || ($split_lines[3] eq "Sender")) {
121                 $WRITE_THIS .= "from matchcase \"$split_lines[4]\"";
122         }
123 }