maildir2sylpheed.pl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/perl
  2. # * This file is free software; you can redistribute it and/or modify it
  3. # * under the terms of the GNU General Public License as published by
  4. # * the Free Software Foundation; either version 2 of the License, or
  5. # * (at your option) any later version.
  6. # *
  7. # * This program is distributed in the hope that it will be useful, but
  8. # * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. # * General Public License for more details.
  11. # *
  12. # * You should have received a copy of the GNU General Public License
  13. # * along with this program; if not, write to the Free Software
  14. # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. # *
  16. # * Copyright © 2003 Paul Mangan <claws@thewildbeast.co.uk>
  17. # *
  18. # * 2003-10-01: add --debug and --dry-run options
  19. # * 2003-09-30: updated/improved by Matthias Förste <itsjustme@users.sourceforge.net>
  20. # * 2003-05-27: version one
  21. ## script name : maildir2sylpheed.pl
  22. ## script purpose : convert a Kmail mailbox into a Sylpheed mailbox
  23. ## USAGE: maildir2sylpheed.pl --kmaildir=Mail
  24. ## tested with Kmail version 1.5.2
  25. use strict;
  26. use Getopt::Long;
  27. use File::Find;
  28. my $kmaildir = '';
  29. my $iNeedHelp = '';
  30. # dont actually change anything if set(useful in conjunction with debug)
  31. my $PRETEND = '';
  32. # print debug info if set
  33. my $DEBUG = '';
  34. my $sylpheed_tmpdir = "$ENV{HOME}/sylpheed_tmp";
  35. my $kmail_olddir = "$ENV{HOME}/kmail_junk";
  36. GetOptions("kmaildir=s" => \$kmaildir,
  37. "help" => \$iNeedHelp,
  38. "dry-run" => \$PRETEND,
  39. "debug" => \$DEBUG);
  40. if ($kmaildir eq "" || $iNeedHelp) {
  41. if (!$iNeedHelp) {
  42. print "No directory name given\n";
  43. }
  44. print "Use the following format:\n";
  45. print "\tmaildir2sylpheed.pl --kmaildir=mail_folder_name\n\n";
  46. print "For example: 'Mail'\n";
  47. exit;
  48. }
  49. $kmaildir = "$ENV{PWD}/$kmaildir" unless '/' eq substr($kmaildir,0,1);
  50. my $count = 1;
  51. my $MAIL_dir = "$kmaildir";
  52. my $find_opts = { wanted => \&process };
  53. if (-d $MAIL_dir) {
  54. find($find_opts , ($MAIL_dir));
  55. } else {
  56. print "\n$MAIL_dir is not a directory !\n";
  57. exit;
  58. }
  59. unless ($PRETEND) {
  60. mkdir("$sylpheed_tmpdir", 0755);
  61. system("mv $kmaildir $kmail_olddir");
  62. system("mv $sylpheed_tmpdir $ENV{HOME}/Mail");
  63. print "\n\nSucessfully converted mailbox \"$MAIL_dir\"\n";
  64. print "Start Sylpheed and right-click \"Mailbox (MH)\" and ";
  65. print "select \"Rebuild folder tree\"\n";
  66. print "You may also need to run \"/File/Folder/Check for ";
  67. print "new messages in all folders\"\n\n";
  68. print "Your kmail directories have been backed-up to\n";
  69. print "$kmail_olddir\n\n";
  70. }
  71. print "\n";
  72. exit;
  73. sub process() {
  74. if (-d) {
  75. process_dir($File::Find::dir);
  76. } else {
  77. process_file($File::Find::name);
  78. }
  79. }
  80. sub process_dir() {
  81. my $direc = shift();
  82. $DEBUG && print "\nDIR $direc";
  83. if ($direc !~ m/^drafts$/ &&
  84. $direc !~ m/^outbox$/ &&
  85. $direc !~ m/^trash$/ &&
  86. $direc !~ m/^inbox$/) {
  87. my $tmpdir = $direc;
  88. $tmpdir =~ s/^$MAIL_dir//;
  89. $tmpdir =~ s/sent-mail/sent/;
  90. $tmpdir =~ s/\/cur$//;
  91. $tmpdir =~ s/\/new$//;
  92. $tmpdir =~ s/^\///;
  93. $tmpdir =~ s/\.directory//g;
  94. $tmpdir =~ s/\.//g;
  95. my $newdir = "$sylpheed_tmpdir/$tmpdir";
  96. $DEBUG && print qq{\n>>> -e "$newdir" || mkdir("$newdir")};
  97. $PRETEND || -e "$newdir" || mkdir("$newdir");
  98. }
  99. }
  100. sub process_file {
  101. my $file = shift;
  102. $DEBUG && print "\nFILE $file";
  103. my $nfile;
  104. my $tmpfile = $file;
  105. if ($tmpfile =~ m/\/cur\// ||
  106. $tmpfile =~ m/\/new\//) {
  107. $tmpfile =~ s/\/new//;
  108. $tmpfile =~ s/\/cur//;
  109. my @spl_str = split("/", $tmpfile);
  110. pop(@spl_str);
  111. push(@spl_str, "$count");
  112. foreach my $spl_str (@spl_str) {
  113. $spl_str =~ s/^\.//;
  114. $spl_str =~ s/\.directory$//;
  115. $spl_str =~ s/sent-mail/sent/;
  116. }
  117. $nfile = join("/", @spl_str);
  118. $nfile =~ s|$kmaildir|$sylpheed_tmpdir/|;
  119. }
  120. if (-e "$file" && $nfile ne "") {
  121. $DEBUG && print qq{\n+++ cp "$file" "$nfile"};
  122. $PRETEND || system("cp \"$file\" \"$nfile\"");
  123. $count++;
  124. }
  125. }