6 # * This file is free software; you can redistribute it and/or modify it
7 # * under the terms of the GNU General Public License as published by
8 # * the Free Software Foundation; either version 3 of the License, or
9 # * (at your option) any later version.
11 # * This program is distributed in the hope that it will be useful, but
12 # * WITHOUT ANY WARRANTY; without even the implied warranty of
13 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # * General Public License for more details.
16 # * You should have received a copy of the GNU General Public License
17 # * along with this program; if not, write to the Free Software
18 # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # * Copyright 2006 Paul Mangan <paul@claws-mail.org>
24 # Convert new style Sylpheed filter rules (Sylpheed >= 0.9.99) to
25 # Claws Mail filtering rules
29 # TABLE OF EQUIVALENTS
31 # SYLPHEED : Claws Mail
32 #------------------------------------------------------
43 # match-header (name From) : from
44 # match-header (name To) : to
45 # match-header (name Cc) : cc
46 # match-header (name Subject) : subject
48 # match-header : header
50 # match-header (type contains) : [nothing]
51 # match-header (type not-contain) : [append with ~]
52 # match-header (type is) : [no equivalent] (use type contains)
53 # match-header (type is-not) : [no equivalent] (use type not-contain)
54 # match-header (type regex) : regexpcase
55 # match-header (type not-regex) : regexpcase [append with ~]
57 # matcher-any-header ; headers-part
58 # match-to-or-cc : to_or_cc
59 # match-body-text : body_part
61 # size (type gt) : size_greater
62 # size (type lt) : size_smaller
63 # age (type gt) : age_greater
64 # age (type lt) : age_lower
70 # not-receive : [no equivalent] (use type delete)
74 # mark-as-read : mark_as_read
79 my $old_config = "$ENV{HOME}/.sylpheed-2.0/filter.xml";
80 my $older_config = "$ENV{HOME}/.sylpheed/filter.xml";
83 my $config_dir = `claws-mail --config-dir` or die("ERROR:
84 You don't appear to have Claws Mail installed\n");
87 chdir($ENV{HOME
} . "/$config_dir") or die("ERROR:
88 Claws Mail config directory not found [~/$config_dir]
89 You need to run Claws Mail once, quit it, and then rerun this script\n");
92 $old_filters = $old_config;
93 } elsif (-e
$older_config) {
94 $old_filters = $older_config;
96 print "ERROR:\n\tSylpheed filter not found\n\t[$old_config]\n\t[$older_config]\n";
100 my $claws_version = `claws-mail --version`;
101 $claws_version =~ s/^Claws Mail version //;
103 my ($major, $minor) = split(/\./, $claws_version);
105 my $version_test = 0;
106 if ($major > 2 || ($major == 2 && $minor >= 3)) {
110 my $parser = XML
::Parser
->new(ErrorContext
=> 2, Style
=> "Tree");
111 my $xmlobj = XML
::SimpleObject
->new($parser->parsefile($old_filters));
113 my @conditions = ('match-header','match-to-or-cc','match-any-header',
114 'match-body-text','command-test','size','age');
116 my @actions = ('copy','not-receive','mark','color-label','mark-as-read',
117 'exec','stop-eval','move','delete');
119 my $standard_headers = qr/^(?:Subject|From|To|Cc)$/;
120 my $negative_matches = qr/^(?:not-contain|is-not|not-regex)$/;
121 my $numeric_matches = qr/^(?:size|age)$/;
122 my $exact_matches = qr/^(?:move|copy|delete|mark)$/;
124 my @new_filters = ("[filtering]");
130 foreach my $element ($xmlobj->child("filter")->children("rule")) {
131 my $new_filter = "\n";
132 if ($element->attribute("enabled")) {
133 if ($element->attribute("enabled") eq "false") {
135 $new_filter .= "disabled ";
138 next; # skip disabled rules
140 } elsif ($version_test) {
141 $new_filter .= "enabled ";
144 if ($element->attribute("name")) {
145 my $name = $element->attribute("name");
146 $name = clean_me
($name);
147 $new_filter .= "rulename \"$name\" ";
150 foreach my $parent ($element->children("condition-list")) {
151 if ($parent->attribute("bool")) {
152 $bool = $parent->attribute("bool");
156 foreach my $condition (@conditions) {
157 my $new_condition = 0;
159 if ($parent->children("$condition")) {
160 foreach my $sibling ($parent->children("$condition")) {
161 if ($new_condition) {
162 $new_filter .= " $bool ";
164 if ($sibling->attribute("type")) {
165 $type = $sibling->attribute("type");
166 if ($type =~ m/$negative_matches/) {
170 if ($sibling->attribute("name")) {
171 my $name = $sibling->attribute("name");
172 if ($condition eq "match-header") {
173 if ($name =~ m/$standard_headers/) {
174 $new_filter .= lc($name) . " ";
176 $new_filter .= "header \"$name\" ";
180 if ($condition eq "match-any-header") {
181 $new_filter .= "headers_part ";
182 } elsif ($condition eq "match-header-content") {
183 $new_filter .= "headers_cont ";
184 } elsif ($condition eq "match-to-or-cc") {
185 $new_filter .= "to_or_cc ";
186 } elsif ($condition eq "match-body-text") {
187 $new_filter .= "body_part ";
188 } elsif ($condition eq "command-test") {
189 $new_filter .= "test ";
190 } elsif ($condition eq "size") {
192 $new_filter .= "size_greater ";
194 $new_filter .= "size_smaller ";
196 } elsif ($condition eq "age") {
198 $new_filter .= "age_greater ";
200 $new_filter .= "age_lower ";
203 if ($condition !~ m/$numeric_matches/ &&
204 $condition ne "command-test") {
205 if ($type =~ m/regex/) {
206 $new_filter .= "regexpcase ";
208 $new_filter .= "matchcase ";
211 my $value = clean_me
($sibling->value);
212 if ($condition =~ m/$numeric_matches/) {
213 $new_filter .= "$value";
215 $new_filter .= "\"$value\"";
222 ## end of condition list
224 foreach my $parent ($element->children("action-list")) {
225 foreach my $action (@actions) {
226 if ($parent->children("$action")) {
227 foreach my $sibling ($parent->children("$action")) {
228 if ($action =~ m/$exact_matches/) {
229 $new_filter .= " $action";
230 } elsif ($action eq "not-receive") {
231 $new_filter .= " delete";
232 } elsif ($action eq "color-label") {
233 $new_filter .= " color";
234 } elsif ($action eq "mark-as-read") {
235 $new_filter .= " mark_as_read";
236 } elsif ($action eq "exec") {
237 $new_filter .= " execute";
238 } elsif ($action eq "stop-eval") {
239 $new_filter .= " stop";
241 if ($sibling->value) {
242 my $value = clean_me
($sibling->value);
243 if ($action eq "color-label") {
244 $new_filter .= " $value";
246 $new_filter .= " \"$value\"";
253 ## end of action list
254 push(@new_filters, $new_filter) if (defined($new_filter));
257 push(@new_filters, "\n");
260 open(MATCHERRC
, ">>matcherrc");
261 print MATCHERRC
@new_filters;
264 print "Converted ". ($#new_filters-1) . " filters\n";
266 print "[$disabled disabled filter(s) not converted]\n";
274 $dirty =~ s/\"/\\\"/g;