Fix use of uninitialized value in sgpgme_has_secret_key().
[claws.git] / tools / filter_conv.pl
old mode 100644 (file)
new mode 100755 (executable)
index 63dc5a5..fec94e8
@@ -1,10 +1,13 @@
-#!/usr/bin/perl
+#!/usr/bin/perl -w
+use strict;
 
-#  * Copyright 2002 Paul Mangan <claws@thewildbeast.co.uk>
+#  * Copyright 2002 Paul Mangan <paul@claws-mail.org>
+#  *
+#  * Reimplemented by Torsten Schoenfeld <kaffeetisch@web.de>
 #  *
 #  * 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
-#  * the Free Software Foundation; either version 2 of the License, or
+#  * the Free Software Foundation; either version 3 of the License, or
 #  * (at your option) any later version.
 #  *
 #  * This program is distributed in the hope that it will be useful, but
 #  *
 #  * You should have received a copy of the GNU General Public License
 #  * along with this program; if not, write to the Free Software
-#  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 #  *
 
-chdir;
-chdir '.sylpheed' || die("You don't appear to have Sylpheed installed\n");
+my $old_config_dir = "$ENV{HOME}/.sylpheed";
+my $config_dir = `claws-mail --config-dir`;
+chomp $config_dir;
+
+chdir($ENV{ HOME } . "/$config_dir")
+       or die("You don't appear to have Claws Mail installed\n");
+
+###############################################################################
+
+my $normal_headers = qr/^(?:Subject|From|To|Cc)$/;
 
-open(FOLDERLIST, "<folderlist.xml") || die("Can't find folderlist.xml\n");
-@folderlist = <FOLDERLIST>;
+my @new_filters = ("[global]\n");
+
+###############################################################################
+
+my $mailbox;
+
+open(FOLDERLIST, "<$old_config_dir/folderlist.xml")
+       or die("Can't find '$old_config_dir/folderlist.xml'\n");
+  while (<FOLDERLIST>) {
+    if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
+      $mailbox = $1;
+      last;
+    }
+  }
 close FOLDERLIST;
 
-foreach $folderlist (@folderlist) {
-       if ($folderlist =~ m/<folder type="mh"/) {
-               $folderlist =~ s/<folder type="mh" name="//;
-                $folderlist =~ s/" path="[A-Z0-9]+"//ig;
-               $folderlist =~ s/ collapsed="1"//;
-               $folderlist =~ s/>\n//ig;
-                $folderlist =~ s/^ +//;
-                $mailbox = $folderlist;
+###############################################################################
+
+open(FILTERRC, "<$old_config_dir/filterrc")
+       or die("Can't find your old filter rules ('$old_config_dir/filterrc')\n");
+  while (<FILTERRC>) {
+    chomp();
+
+    my ($header_one,
+        $value_one,
+        $op,
+        $header_two,
+        $value_two,
+        $destination,
+        $mode_one,
+        $mode_two,
+        $action) = split(/\t/);
+
+    $value_one =~ s/\"/\\\"/g ;
+    $value_two =~ s/\"/\\\"/g ;
+    $action = $action eq "m" ? "move" : "delete";
+    $destination = $destination =~ m!^\#mh/! ?
+                     $destination :
+                     "#mh/$mailbox/$destination";
+
+    my ($predicate_one,
+        $predicate_two,
+        $match_type_one,
+        $match_type_two,
+        $new_filter);
+
+    ###########################################################################
+
+    if ($mode_one % 2 == 0) {
+      $predicate_one = "~";
+    }
+    else {
+      $predicate_one = "";
+    }
+
+    if ($mode_one <= 1) {
+      $match_type_one = "matchcase";
+    }
+    else {
+      $match_type_one = "regexpcase";
+    }
+
+    ###########################################################################
+
+    if ($mode_two % 2 == 0) {
+      $predicate_two = "~";
+    }
+    else {
+      $predicate_two = "";
+    }
+
+    if ($mode_two <= 1) {
+      $match_type_two = "matchcase";
+    }
+    else {
+      $match_type_two = "regexpcase";
+    }
+
+    ###########################################################################
+
+    if ($header_one eq "To" && $header_two eq "Cc" ||
+        $header_one eq "Cc" && $header_two eq "To" and
+        $value_one eq $value_two and
+        $mode_one eq $mode_two and
+        $op eq "|") {
+      if ($action eq "move") {
+        $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
+      }
+      else {
+        $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
+      }
+    }
+    else {
+      if ($header_one =~ m/$normal_headers/) {
+        $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
+      }
+      else {
+        $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
+      }
+
+      if ($op ne " ") {
+        if ($header_two =~ m/$normal_headers/) {
+          $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
         }
-}
+        else {
+          $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
+        }
+      }
 
-open (FILTERRC, "<filterrc") || die("Can't find your old filter rules\n");
-@filterrc = <FILTERRC>;
-close FILTERRC;
+      if (defined($new_filter)) {
+        if ($action eq "move") {
+          $new_filter .= qq( move "$destination"\n);
+        }
+        else {
+          $new_filter .= qq(delete\n);
+        }
+      }
+    }
 
-if (!@filterrc) {
-       print "\nYou don't have any filter rules\n\n";
-       exit;
-}
+    ###########################################################################
 
-$WRITE_THIS = "";
-$COUNT      = "0";
-
-if (-e "matcherrc") {
-       open (MATCHER, "<matcherrc") || die("Can't open matcherrc\n");
-       @matcherrc = <MATCHER>;
-       close MATCHER;
-               foreach $matcherrc (@matcherrc) {
-                       $WRITE_THIS .= $matcherrc;
-               }
-       $WRITE_THIS .= "\n";
-}
+    push(@new_filters, $new_filter) if (defined($new_filter));
+  }
+close(FILTERRC);
 
-$WRITE_THIS .= "[global]\n";
-
-foreach $filterrc (@filterrc) {
-       $COUNT++;
-       @split_lines = split("\t", $filterrc);
-       if ($split_lines[2] eq "&") {
-               $operator = "&";
-               &sort_data;
-       }
-       elsif ($split_lines[2] eq "|") {
-               $operator = "|";
-               &sort_data;
-       } elsif ($split_lines[0] eq "To") {
-               $WRITE_THIS .= "to matchcase \"$split_lines[1]\"";
-       } elsif ($split_lines[0] eq "Reply-To") {
-               $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\"";
-       } elsif ($split_lines[0] eq "Subject") {
-               $WRITE_THIS .= "subject matchcase \"$split_lines[1]\"";
-       } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")){
-               $WRITE_THIS .= "from matchcase \"$split_lines[1]\"";
-       }
-       if (!$split_lines[5]) {
-               $WRITE_THIS .= " delete";
-       } elsif ($split_lines[8] eq "m\n"){
-               $WRITE_THIS .= " move \"\#mh/$mailbox/$split_lines[5]\"";
-       }
-       $WRITE_THIS .= "\n";
-       @split_lines = "";
-}
+###############################################################################
 
-open (MATCHERRC, ">matcherrc");
-print MATCHERRC $WRITE_THIS;
-close MATCHERRC;
-
-rename ("filterrc","filterrc.old");
-
-print "\nYou have sucessfully converted $COUNT filtering rules\n\n";
-print "'filterrc' has been renamed 'filterrc.old'\n\n";
-exit;
-
-sub sort_data {
-       if ($split_lines[0] eq "To" && $split_lines[3] eq "Cc" && 
-       $split_lines[1] eq $split_lines[4]) {
-               $WRITE_THIS .= "to_or_cc matchcase \"$split_lines[1]\"";                
-       }
-       elsif ($split_lines[0] eq "To") {
-               $WRITE_THIS .= "to matchcase \"$split_lines[1]\" $operator ";
-       } elsif ($split_lines[0] eq "Reply-To") {
-               $WRITE_THIS .= "inreplyto matchcase \"$split_lines[1]\" $operator ";
-       } elsif ($split_lines[0] eq "Subject") {
-               $WRITE_THIS .= "subject matchcase \"$split_lines[1]\" $operator ";
-       } elsif (($split_lines[0] eq "From") || ($split_lines[0] eq "Sender")) {
-               $WRITE_THIS .= "from matchcase \"$split_lines[1]\" $operator ";
-       }
-       if ($split_lines[3] eq "To") {
-               $WRITE_THIS .= "to matchcase \"$split_lines[4]\"";
-       } elsif ($split_lines[3] eq "Reply-To") {
-               $WRITE_THIS .= "inreplyto matchcase \"$split_lines[4]\"";
-       } elsif ($split_lines[3] eq "Subject") {
-               $WRITE_THIS .= "subject matchcase \"$split_lines[4]\"";
-       } elsif (($split_lines[3] eq "From") || ($split_lines[3] eq "Sender")) {
-               $WRITE_THIS .= "from matchcase \"$split_lines[4]\"";
-       }
+open(MATCHERRC, ">>matcherrc");
+  print MATCHERRC @new_filters;
+close(MATCHERRC);
+
+print "Converted $#new_filters filters\n";
+
+if ($old_config_dir eq $config_dir) {
+       rename("filterrc", "filterrc.old");
+       print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";
 }
+###############################################################################
+