3 ## script purpose : convert matcherrc filtering rules into
6 # This conversion-tool doesn't produce nice Perl code and is just
7 # intended to get you started. If you choose to use the Perl plugin,
8 # consider rewriting your rules.
10 # Copyright (C) 2004-2014 Holger Berndt
13 # This file is free software; you can redistribute it and/or modify it
14 # under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32 my $home_dir = $ENV{"HOME"}; $home_dir ||= ".";
33 my $sylph_dir = `claws-mail --config-dir`;
34 my $matcherrc = "matcherrc";
35 my $perlfilter = "perl_filter";
38 chomp($sylph_dir); $sylph_dir =~ s/.*\n(.*)$/$1/;
39 my $inpath = $home_dir.$dirsep.$sylph_dir.$dirsep.$matcherrc;
40 my $outpath = $home_dir.$dirsep.$sylph_dir.$dirsep.$perlfilter;
41 open IN
, $inpath or die "Cannot open $inpath: $!";
42 open OUT
,">>",$outpath or die "Cannot open $outpath: $!";
44 print "Filtering rules are read from `$inpath', converted to Perl\n";
45 print "syntax and appended to `$outpath'\n";
46 print "`$inpath' is not changed, so you might want to make a backup\n";
47 print "copy of it and then remove your former filtering rules\n";
51 print OUT
"### Begin: Rules converted by matcherrc2perlfilter.pl $date ###\n";
52 while(my $line = <IN
>) {
53 $line =~ s/^\s*(.*)\s*$/$1/;
54 if($line =~ /^\[filtering\]$/i) {
56 $line =~ s/^\s*(.*)\s*$/$1/;
57 next if $line =~ /^$/;
58 if($line =~ /^\[(.+)\]$/) {
59 last unless ($1 =~ /filtering/i);
61 my @fields = splitline
($line);
67 print "---\n" if $warnings;
68 print "Finished conversion of $lines rules with $warnings warnings.\n";
69 print OUT
"### End: Rules converted by matcherrc2perlfilter.pl $date ###\n";
75 while(my $token = shift) {
80 elsif($token eq "|") {
81 $output =~ s/&& $/\|\| /;
84 elsif($tokens != 1 and $act == 0) {
86 if($output =~ / (&&|\|\|) $/) {
87 $output =~ s/ (&&|\|\|) $/\) $1 /;
100 $token eq "marked" or
101 $token eq "deleted" or
102 $token eq "replied" or
103 $token eq "forwarded" or
104 $token eq "locked" or
105 $token eq "unread" or
107 $token eq "partial" or
108 $token eq "ignore_thread" or
110 $token eq "unmark" or
112 $token eq "unlock" or
115 $token eq "mark_as_read" or
116 $token eq "mark_as_unread") {
117 $output .= qq|($token) && |;
119 elsif($token eq "delete") {
120 $output .= qq|(dele
) && |;
122 elsif($token eq "subject" or
126 $token eq "to_or_cc" or
127 $token eq "newsgroups" or
128 $token eq "inreplyto" or
129 $token eq "references" or
130 $token eq "headers_part" or
131 $token eq "headers_cont" or
132 $token eq "body_part" or
133 $token eq "message") {
136 $what =~ s/\\"/"/g;$what =~ s/'/\\'/g;
137 $what =~ s/^"(.*)"$/'$1'/;
138 $output .= qq|($match($token,$what)) && |;
140 elsif($token eq "age_greater" or
141 $token eq "age_lower" or
142 $token eq "colorlabel" or
143 $token eq "score_greater" or
144 $token eq "score_lower" or
145 $token eq "score_equal" or
146 $token eq "size_greater" or
147 $token eq "size_smaller" or
148 $token eq "size_equal" or
151 $token eq "execute" or
154 $token eq "change_score" or
155 $token eq "set_score") {
157 $arg =~ s/\\"/"/g;$arg =~ s/'/\\'/g;
158 $arg =~ s/^"(.*)"$/'$1'/;
159 $output .= qq|($token($arg)) && |;
161 elsif($token eq "header") {
162 my $headername = shift;
163 $headername =~ s/\\"/"/g;$headername =~ s/'/\\'/g;
164 $headername =~ s/^"(.*)"$/'$1'/;
167 $what =~ s/\\"/"/g;$what =~ s/'/\\'/g;
168 $what =~ s/^"(.*)"$/'$1'/;
169 $output .= qq|($match($headername,$what)) && |;
171 elsif($token eq "stop") {
172 $output .= qq|(return) && |;
175 print STDERR
"WARNING: unknown token in $inpath ignored: $token\n";
179 $output =~ s
| && $|;\n|;
184 # split the input line
191 $line =~ s
#^(".*?[^\\]")##;
194 elsif($line =~ /^~/) {