filter_conv.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/perl -w
  2. use strict;
  3. # * Copyright 2002 Paul Mangan <paul@claws-mail.org>
  4. # *
  5. # * Reimplemented by Torsten Schoenfeld <kaffeetisch@web.de>
  6. # *
  7. # * This file is free software; you can redistribute it and/or modify it
  8. # * under the terms of the GNU General Public License as published by
  9. # * the Free Software Foundation; either version 3 of the License, or
  10. # * (at your option) any later version.
  11. # *
  12. # * This program is distributed in the hope that it will be useful, but
  13. # * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # * General Public License for more details.
  16. # *
  17. # * You should have received a copy of the GNU General Public License
  18. # * along with this program; if not, write to the Free Software
  19. # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. # *
  21. my $old_config_dir = "$ENV{HOME}/.sylpheed";
  22. my $config_dir = `claws-mail --config-dir`;
  23. chomp $config_dir;
  24. chdir($ENV{ HOME } . "/$config_dir")
  25. or die("You don't appear to have Claws Mail installed\n");
  26. ###############################################################################
  27. my $normal_headers = qr/^(?:Subject|From|To|Cc)$/;
  28. my @new_filters = ("[global]\n");
  29. ###############################################################################
  30. my $mailbox;
  31. open(FOLDERLIST, "<$old_config_dir/folderlist.xml")
  32. or die("Can't find '$old_config_dir/folderlist.xml'\n");
  33. while (<FOLDERLIST>) {
  34. if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
  35. $mailbox = $1;
  36. last;
  37. }
  38. }
  39. close FOLDERLIST;
  40. ###############################################################################
  41. open(FILTERRC, "<$old_config_dir/filterrc")
  42. or die("Can't find your old filter rules ('$old_config_dir/filterrc')\n");
  43. while (<FILTERRC>) {
  44. chomp();
  45. my ($header_one,
  46. $value_one,
  47. $op,
  48. $header_two,
  49. $value_two,
  50. $destination,
  51. $mode_one,
  52. $mode_two,
  53. $action) = split(/\t/);
  54. $value_one =~ s/\"/\\\"/g ;
  55. $value_two =~ s/\"/\\\"/g ;
  56. $action = $action eq "m" ? "move" : "delete";
  57. $destination = $destination =~ m!^\#mh/! ?
  58. $destination :
  59. "#mh/$mailbox/$destination";
  60. my ($predicate_one,
  61. $predicate_two,
  62. $match_type_one,
  63. $match_type_two,
  64. $new_filter);
  65. ###########################################################################
  66. if ($mode_one % 2 == 0) {
  67. $predicate_one = "~";
  68. }
  69. else {
  70. $predicate_one = "";
  71. }
  72. if ($mode_one <= 1) {
  73. $match_type_one = "matchcase";
  74. }
  75. else {
  76. $match_type_one = "regexpcase";
  77. }
  78. ###########################################################################
  79. if ($mode_two % 2 == 0) {
  80. $predicate_two = "~";
  81. }
  82. else {
  83. $predicate_two = "";
  84. }
  85. if ($mode_two <= 1) {
  86. $match_type_two = "matchcase";
  87. }
  88. else {
  89. $match_type_two = "regexpcase";
  90. }
  91. ###########################################################################
  92. if ($header_one eq "To" && $header_two eq "Cc" ||
  93. $header_one eq "Cc" && $header_two eq "To" and
  94. $value_one eq $value_two and
  95. $mode_one eq $mode_two and
  96. $op eq "|") {
  97. if ($action eq "move") {
  98. $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
  99. }
  100. else {
  101. $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
  102. }
  103. }
  104. else {
  105. if ($header_one =~ m/$normal_headers/) {
  106. $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
  107. }
  108. else {
  109. $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
  110. }
  111. if ($op ne " ") {
  112. if ($header_two =~ m/$normal_headers/) {
  113. $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
  114. }
  115. else {
  116. $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
  117. }
  118. }
  119. if (defined($new_filter)) {
  120. if ($action eq "move") {
  121. $new_filter .= qq( move "$destination"\n);
  122. }
  123. else {
  124. $new_filter .= qq(delete\n);
  125. }
  126. }
  127. }
  128. ###########################################################################
  129. push(@new_filters, $new_filter) if (defined($new_filter));
  130. }
  131. close(FILTERRC);
  132. ###############################################################################
  133. open(MATCHERRC, ">>matcherrc");
  134. print MATCHERRC @new_filters;
  135. close(MATCHERRC);
  136. print "Converted $#new_filters filters\n";
  137. if ($old_config_dir eq $config_dir) {
  138. rename("filterrc", "filterrc.old");
  139. print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";
  140. }
  141. ###############################################################################