2006-08-01 [wwp] 2.4.0cvs10
[claws.git] / tools / filter_conv_new.pl
index 8bab8eb..93c9536 100644 (file)
@@ -1,6 +1,7 @@
 #!/usr/bin/perl -w
 
 use strict;
+use XML::SimpleObject;
 
 #  * This file is free software; you can redistribute it and/or modify it
 #  * under the terms of the GNU General Public License as published by
@@ -75,19 +76,39 @@ use strict;
 # stop-eval                    :       stop
 #
 
-use XML::SimpleObject;
-
 my $old_config = "$ENV{HOME}/.sylpheed-2.0/filter.xml";
-my $config_dir = `sylpheed-claws --config-dir`;
+my $older_config = "$ENV{HOME}/.sylpheed/filter.xml";
+my $old_filters;
+
+my $config_dir = `sylpheed-claws --config-dir` or die("ERROR:
+       You don't appear to have Sylpheed-Claws installed\n");
 chomp $config_dir;
 
-chdir($ENV{ HOME } . "/$config_dir")
-       or die("You don't appear to have Sylpheed-Claws installed\n");
+chdir($ENV{HOME} . "/$config_dir") or die("ERROR:
+       Sylpheed-Claws config directory not found [~/$config_dir]
+       You need to run Sylpheed-Claws once, quit it, and then rerun this script\n");
+
+if (-e $old_config) {
+       $old_filters = $old_config;
+} elsif (-e $older_config) {
+       $old_filters = $older_config;
+} else {
+       print "ERROR:\n\tSylpheed filter not found\n\t[$old_config]\n\t[$older_config]\n";
+       exit;
+}
+
+my $claws_version = `sylpheed-claws --version`;
+$claws_version =~ s/^Sylpheed-Claws version //;
 
--e $old_config or die("Can't find old filters [$old_config]\n");
+my ($major, $minor) = split(/\./, $claws_version);
+
+my $version_test = 0;
+if ($major > 2 || ($major == 2 && $minor >= 3)) {
+       $version_test = 1;
+}
 
 my $parser = XML::Parser->new(ErrorContext => 2, Style => "Tree");
-my $xmlobj = XML::SimpleObject->new($parser->parsefile($old_config));
+my $xmlobj = XML::SimpleObject->new($parser->parsefile($old_filters));
 
 my @conditions = ('match-header','match-to-or-cc','match-any-header',
                  'match-body-text','command-test','size','age');
@@ -102,20 +123,28 @@ my $exact_matches = qr/^(?:move|copy|delete|mark)$/;
 
 my @new_filters = ("[filtering]");
 
+my $disabled = 0;
 my $bool;
 
 ## rules list
 foreach my $element ($xmlobj->child("filter")->children("rule")) {
-       my $new_filter;
+       my $new_filter = "\n";
+       if ($element->attribute("enabled")) {
+               if ($element->attribute("enabled") eq "false") {
+                       if ($version_test) {
+                               $new_filter .= "disabled ";
+                       } else {
+                               $disabled++;
+                               next;   # skip disabled rules
+                       }
+               } elsif ($version_test) {
+                               $new_filter .= "enabled ";
+               }
+       }
        if ($element->attribute("name")) {
                my $name = $element->attribute("name");
                $name = clean_me($name);
-               $new_filter = "\nrulename \"$name\" ";
-       }
-       if ($element->attribute("enabled")) {
-               if ($element->attribute("enabled") eq "false") {
-                       next;   # skip disabled rules
-               }
+               $new_filter .= "rulename \"$name\" ";
        }
 ## condition list
        foreach my $parent ($element->children("condition-list")) {
@@ -177,8 +206,7 @@ foreach my $element ($xmlobj->child("filter")->children("rule")) {
                                                        $new_filter .= "matchcase ";
                                                }
                                        }
-                                       my $value = $sibling->value;
-                                       $value = clean_me($value);
+                                       my $value = clean_me($sibling->value);
                                        if ($condition =~ m/$numeric_matches/) {
                                                $new_filter .= "$value";
                                        } else {
@@ -209,8 +237,7 @@ foreach my $element ($xmlobj->child("filter")->children("rule")) {
                                                $new_filter .= " stop";
                                        }
                                        if ($sibling->value) {
-                                               my $value = $sibling->value;
-                                               $value = clean_me($value);
+                                               my $value = clean_me($sibling->value);
                                                if ($action eq "color-label") {
                                                        $new_filter .= " $value";
                                                } else {
@@ -233,6 +260,9 @@ open(MATCHERRC, ">>matcherrc");
 close(MATCHERRC);
 
 print "Converted ". ($#new_filters-1) . " filters\n";
+if ($disabled) {
+       print "[$disabled disabled filter(s) not converted]\n";
+}
 
 exit;