filter_conv_new.pl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use XML::SimpleObject;
  4. # * This file is free software; you can redistribute it and/or modify it
  5. # * under the terms of the GNU General Public License as published by
  6. # * the Free Software Foundation; either version 2 of the License, or
  7. # * (at your option) any later version.
  8. # *
  9. # * This program is distributed in the hope that it will be useful, but
  10. # * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # * General Public License for more details.
  13. # *
  14. # * You should have received a copy of the GNU General Public License
  15. # * along with this program; if not, write to the Free Software
  16. # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. # *
  18. # * Copyright 2006 Paul Mangan <claws@thewildbeast.co.uk>
  19. # *
  20. #
  21. # Convert new style Sylpheed filter rules (Sylpheed >= 0.9.99) to
  22. # Sylpheed-Claws filtering rules
  23. #
  24. #
  25. # TABLE OF EQUIVALENTS
  26. #
  27. # SYLPHEED : SYLPHEED-CLAWS
  28. #------------------------------------------------------
  29. #
  30. # NAME
  31. #
  32. # name : rulename
  33. #
  34. # CONDITION LIST
  35. #
  36. # bool or : |
  37. # bool and : &
  38. #
  39. # match-header (name From) : from
  40. # match-header (name To) : to
  41. # match-header (name Cc) : cc
  42. # match-header (name Subject) : subject
  43. # else...
  44. # match-header : header
  45. #
  46. # match-header (type contains) : [nothing]
  47. # match-header (type not-contain) : [append with ~]
  48. # match-header (type is) : [no equivalent] (use type contains)
  49. # match-header (type is-not) : [no equivalent] (use type not-contain)
  50. # match-header (type regex) : regexpcase
  51. # match-header (type not-regex) : regexpcase [append with ~]
  52. #
  53. # matcher-any-header ; headers-part
  54. # match-to-or-cc : to_or_cc
  55. # match-body-text : body_part
  56. # command-test : test
  57. # size (type gt) : size_greater
  58. # size (type lt) : size_smaller
  59. # age (type gt) : age_greater
  60. # age (type lt) : age_lower
  61. #
  62. # ACTION LIST
  63. #
  64. # move : move
  65. # copy : copy
  66. # not-receive : [no equivalent] (use type delete)
  67. # delete : delete
  68. # mark : mark
  69. # color-label : color
  70. # mark-as-read : mark_as_read
  71. # exec : execute
  72. # stop-eval : stop
  73. #
  74. my $old_config = "$ENV{HOME}/.sylpheed-2.0/filter.xml";
  75. my $older_config = "$ENV{HOME}/.sylpheed/filter.xml";
  76. my $old_filters;
  77. my $config_dir = `sylpheed-claws --config-dir` or die("ERROR:
  78. You don't appear to have Sylpheed-Claws installed\n");
  79. chomp $config_dir;
  80. chdir($ENV{HOME} . "/$config_dir") or die("ERROR:
  81. Sylpheed-Claws config directory not found [~/$config_dir]
  82. You need to run Sylpheed-Claws once, quit it, and then rerun this script\n");
  83. if (-e $old_config) {
  84. $old_filters = $old_config;
  85. } elsif (-e $older_config) {
  86. $old_filters = $older_config;
  87. } else {
  88. print "ERROR:\n\tSylpheed filter not found\n\t[$old_config]\n\t[$older_config]\n";
  89. exit;
  90. }
  91. my $claws_version = `sylpheed-claws --version`;
  92. $claws_version =~ s/^Sylpheed-Claws version //;
  93. my ($major, $minor) = split(/\./, $claws_version);
  94. my $version_test = 0;
  95. if ($major > 2 || ($major == 2 && $minor >= 3)) {
  96. $version_test = 1;
  97. }
  98. my $parser = XML::Parser->new(ErrorContext => 2, Style => "Tree");
  99. my $xmlobj = XML::SimpleObject->new($parser->parsefile($old_filters));
  100. my @conditions = ('match-header','match-to-or-cc','match-any-header',
  101. 'match-body-text','command-test','size','age');
  102. my @actions = ('move','copy','not-receive','delete','mark','color-label',
  103. 'mark-as-read','exec','stop-eval');
  104. my $standard_headers = qr/^(?:Subject|From|To|Cc)$/;
  105. my $negative_matches = qr/^(?:not-contain|is-not|not-regex)$/;
  106. my $numeric_matches = qr/^(?:size|age)$/;
  107. my $exact_matches = qr/^(?:move|copy|delete|mark)$/;
  108. my @new_filters = ("[filtering]");
  109. my $disabled = 0;
  110. my $bool;
  111. ## rules list
  112. foreach my $element ($xmlobj->child("filter")->children("rule")) {
  113. my $new_filter = "\n";
  114. if ($element->attribute("enabled")) {
  115. if ($element->attribute("enabled") eq "false") {
  116. if ($version_test) {
  117. $new_filter .= "disabled ";
  118. } else {
  119. $disabled++;
  120. next; # skip disabled rules
  121. }
  122. } elsif ($version_test) {
  123. $new_filter .= "enabled ";
  124. }
  125. }
  126. if ($element->attribute("name")) {
  127. my $name = $element->attribute("name");
  128. $name = clean_me($name);
  129. $new_filter .= "rulename \"$name\" ";
  130. }
  131. ## condition list
  132. foreach my $parent ($element->children("condition-list")) {
  133. if ($parent->attribute("bool")) {
  134. $bool = $parent->attribute("bool");
  135. $bool =~ s/or/|/;
  136. $bool =~ s/and/&/;
  137. }
  138. foreach my $condition (@conditions) {
  139. my $new_condition = 0;
  140. my $type;
  141. if ($parent->children("$condition")) {
  142. foreach my $sibling ($parent->children("$condition")) {
  143. if ($new_condition) {
  144. $new_filter .= " $bool ";
  145. }
  146. if ($sibling->attribute("type")) {
  147. $type = $sibling->attribute("type");
  148. if ($type =~ m/$negative_matches/) {
  149. $new_filter .= '~';
  150. }
  151. }
  152. if ($sibling->attribute("name")) {
  153. my $name = $sibling->attribute("name");
  154. if ($condition eq "match-header") {
  155. if ($name =~ m/$standard_headers/) {
  156. $new_filter .= lc($name) . " ";
  157. } else {
  158. $new_filter .= "header \"$name\" ";
  159. }
  160. }
  161. }
  162. if ($condition eq "match-any-header") {
  163. $new_filter .= "headers_part ";
  164. } elsif ($condition eq "match-to-or-cc") {
  165. $new_filter .= "to_or_cc ";
  166. } elsif ($condition eq "match-body-text") {
  167. $new_filter .= "body_part ";
  168. } elsif ($condition eq "command-test") {
  169. $new_filter .= "test ";
  170. } elsif ($condition eq "size") {
  171. if ($type eq "gt") {
  172. $new_filter .= "size_greater ";
  173. } else {
  174. $new_filter .= "size_smaller ";
  175. }
  176. } elsif ($condition eq "age") {
  177. if ($type eq "gt") {
  178. $new_filter .= "age_greater ";
  179. } else {
  180. $new_filter .= "age_lower ";
  181. }
  182. }
  183. if ($condition !~ m/$numeric_matches/ &&
  184. $condition ne "command-test") {
  185. if ($type =~ m/regex/) {
  186. $new_filter .= "regexpcase ";
  187. } else {
  188. $new_filter .= "matchcase ";
  189. }
  190. }
  191. my $value = clean_me($sibling->value);
  192. if ($condition =~ m/$numeric_matches/) {
  193. $new_filter .= "$value";
  194. } else {
  195. $new_filter .= "\"$value\"";
  196. }
  197. $new_condition++;
  198. }
  199. }
  200. }
  201. }
  202. ## end of condition list
  203. ## action list
  204. foreach my $parent ($element->children("action-list")) {
  205. foreach my $action (@actions) {
  206. if ($parent->children("$action")) {
  207. foreach my $sibling ($parent->children("$action")) {
  208. if ($action =~ m/$exact_matches/) {
  209. $new_filter .= " $action";
  210. } elsif ($action eq "not-receive") {
  211. $new_filter .= " delete";
  212. } elsif ($action eq "color-label") {
  213. $new_filter .= " color";
  214. } elsif ($action eq "mark-as-read") {
  215. $new_filter .= " mark_as_read";
  216. } elsif ($action eq "exec") {
  217. $new_filter .= " execute";
  218. } elsif ($action eq "stop-eval") {
  219. $new_filter .= " stop";
  220. }
  221. if ($sibling->value) {
  222. my $value = clean_me($sibling->value);
  223. if ($action eq "color-label") {
  224. $new_filter .= " $value";
  225. } else {
  226. $new_filter .= " \"$value\"";
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. ## end of action list
  234. push(@new_filters, $new_filter) if (defined($new_filter));
  235. }
  236. ## end of rules list
  237. push(@new_filters, "\n");
  238. # write new config
  239. open(MATCHERRC, ">>matcherrc");
  240. print MATCHERRC @new_filters;
  241. close(MATCHERRC);
  242. print "Converted ". ($#new_filters-1) . " filters\n";
  243. if ($disabled) {
  244. print "[$disabled disabled filter(s) not converted]\n";
  245. }
  246. exit;
  247. sub clean_me {
  248. my ($dirty) = @_;
  249. $dirty =~ s/\"/\\\"/g;
  250. $dirty =~ s/\n/ /g;
  251. return $dirty;
  252. }