filter_conv.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/perl -w
  2. use strict;
  3. # * Copyright 2002 Paul Mangan <claws@thewildbeast.co.uk>
  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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. # *
  21. chdir($ENV{ HOME } . "/.sylpheed") or die("You don't appear to have Sylpheed installed\n");
  22. ###############################################################################
  23. my $normal_headers = qr/^(?:Subject|From|To|Cc)$/;
  24. my @new_filters = ("[global]\n");
  25. ###############################################################################
  26. my $mailbox;
  27. open(FOLDERLIST, "<folderlist.xml") or die("Can't find 'folderlist.xml'\n");
  28. while (<FOLDERLIST>) {
  29. if (m/<folder type="mh" name="([^"]+)" path="[^"]+"/) {
  30. $mailbox = $1;
  31. last;
  32. }
  33. }
  34. close FOLDERLIST;
  35. ###############################################################################
  36. open(FILTERRC, "<filterrc") or die("Can't find your old filter rules ('filterrc')\n");
  37. while (<FILTERRC>) {
  38. chomp();
  39. my ($header_one,
  40. $value_one,
  41. $op,
  42. $header_two,
  43. $value_two,
  44. $destination,
  45. $mode_one,
  46. $mode_two,
  47. $action) = split(/\t/);
  48. $value_one =~ s/\"/\\\"/g ;
  49. $value_two =~ s/\"/\\\"/g ;
  50. $action = $action eq "m" ? "move" : "delete";
  51. $destination = $destination =~ m!^\#mh/! ?
  52. $destination :
  53. "#mh/$mailbox/$destination";
  54. my ($predicate_one,
  55. $predicate_two,
  56. $match_type_one,
  57. $match_type_two,
  58. $new_filter);
  59. ###########################################################################
  60. if ($mode_one % 2 == 0) {
  61. $predicate_one = "~";
  62. }
  63. else {
  64. $predicate_one = "";
  65. }
  66. if ($mode_one <= 1) {
  67. $match_type_one = "matchcase";
  68. }
  69. else {
  70. $match_type_one = "regexpcase";
  71. }
  72. ###########################################################################
  73. if ($mode_two % 2 == 0) {
  74. $predicate_two = "~";
  75. }
  76. else {
  77. $predicate_two = "";
  78. }
  79. if ($mode_two <= 1) {
  80. $match_type_two = "matchcase";
  81. }
  82. else {
  83. $match_type_two = "regexpcase";
  84. }
  85. ###########################################################################
  86. if ($header_one eq "To" && $header_two eq "Cc" ||
  87. $header_one eq "Cc" && $header_two eq "To" and
  88. $value_one eq $value_two and
  89. $mode_one eq $mode_two and
  90. $op eq "|") {
  91. if ($action eq "move") {
  92. $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" move "$destination"\n);
  93. }
  94. else {
  95. $new_filter = $predicate_one . qq(to_or_cc $match_type_one "$value_one" delete\n);
  96. }
  97. }
  98. else {
  99. if ($header_one =~ m/$normal_headers/) {
  100. $new_filter .= $predicate_one . lc($header_one) . qq( $match_type_one "$value_one");
  101. }
  102. else {
  103. $new_filter .= $predicate_one . qq(header "$header_one" $match_type_one "$value_one");
  104. }
  105. if ($op ne " ") {
  106. if ($header_two =~ m/$normal_headers/) {
  107. $new_filter .= qq( $op ) . $predicate_two . lc($header_two) . qq( $match_type_two "$value_two");
  108. }
  109. else {
  110. $new_filter .= qq( $op ) . $predicate_two . qq(header "$header_two" $match_type_two "$value_two");
  111. }
  112. }
  113. if (defined($new_filter)) {
  114. if ($action eq "move") {
  115. $new_filter .= qq( move "$destination"\n);
  116. }
  117. else {
  118. $new_filter .= qq(delete\n);
  119. }
  120. }
  121. }
  122. ###########################################################################
  123. push(@new_filters, $new_filter) if (defined($new_filter));
  124. }
  125. close(FILTERRC);
  126. ###############################################################################
  127. open(MATCHERRC, ">>matcherrc");
  128. print MATCHERRC @new_filters;
  129. close(MATCHERRC);
  130. rename("filterrc", "filterrc.old");
  131. ###############################################################################
  132. print "Converted $#new_filters filters\n";
  133. print "Renamed your old filter rules ('filterrc' to 'filterrc.old')\n";