kmail-mailbox2claws-mail.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/perl -w
  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 3 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-2007 Paul Mangan <paul@claws-mail.org>
  17. # *
  18. # * 2007-02-25: several fixes for kmail 1.9.6
  19. # --kmaildir now expects the full path
  20. # renamed from maildir2claws-mail.pl to kmail-mailbox2claws-mail.pl
  21. # * 2003-10-01: add --debug and --dry-run options
  22. # * 2003-09-30: updated/improved by Matthias Förste <itsjustme@users.sourceforge.net>
  23. # * 2003-05-27: version one
  24. ## script name : kmail-mailbox2claws-mail.pl
  25. ## script purpose : convert a Kmail mailbox into a Claws Mail mailbox
  26. ## USAGE: kmail-mailbox2claws-mail.pl --kmaildir=/full/path/to/kmail/mailbox
  27. ## tested with Kmail version 1.9.6
  28. use strict;
  29. use Getopt::Long;
  30. use File::Find;
  31. my $kmaildir = '';
  32. my $iNeedHelp = '';
  33. # dont actually change anything if set(useful in conjunction with debug)
  34. my $PRETEND = '';
  35. # print debug info if set
  36. my $DEBUG = '';
  37. my $claws_tmpdir = "$ENV{HOME}/claws_tmp";
  38. GetOptions("kmaildir=s" => \$kmaildir,
  39. "help" => \$iNeedHelp,
  40. "dry-run" => \$PRETEND,
  41. "debug" => \$DEBUG);
  42. if ($kmaildir eq "" || $iNeedHelp) {
  43. if (!$iNeedHelp) {
  44. print "No directory name given\n";
  45. }
  46. print "Use the following format:\n";
  47. print "\tkmail-mailbox2claws-mail.pl --kmaildir=full-path-to-kmail-dir\n\n";
  48. exit;
  49. }
  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("$claws_tmpdir", 0755);
  61. system("mv $claws_tmpdir $ENV{HOME}/Mail");
  62. print "\n\nSucessfully converted mailbox \"$MAIL_dir\"\n";
  63. print "Start claws-mail and right-click \"Mailbox (MH)\" and ";
  64. print "select \"Rebuild folder tree\"\n";
  65. print "You may also need to run \"/File/Folder/Check for ";
  66. print "new messages in all folders\"\n\n";
  67. }
  68. print "\n";
  69. exit;
  70. sub process() {
  71. if (-d) {
  72. process_dir($File::Find::dir);
  73. } else {
  74. process_file($File::Find::name);
  75. }
  76. }
  77. sub process_dir() {
  78. my $direc = shift();
  79. $DEBUG && print "\nDIR $direc";
  80. if ($direc !~ m/^drafts$/ &&
  81. $direc !~ m/^outbox$/ &&
  82. $direc !~ m/^trash$/ &&
  83. $direc !~ m/^inbox$/) {
  84. my $tmpdir = $direc;
  85. $tmpdir =~ s/^$MAIL_dir//;
  86. $tmpdir =~ s/\/sent-mail$/sent/;
  87. $tmpdir =~ s/\/cur$//;
  88. $tmpdir =~ s/\/new$//;
  89. $tmpdir =~ s/^\///;
  90. $tmpdir =~ s/\.directory//g;
  91. $tmpdir =~ s/\.//g;
  92. my $newdir = "$claws_tmpdir/$tmpdir";
  93. $DEBUG && print qq{\n>>> -e "$newdir" || mkdir("$newdir")};
  94. $PRETEND || -e "$newdir" || mkdir("$newdir");
  95. }
  96. }
  97. sub process_file {
  98. my $file = shift;
  99. $DEBUG && print "\nFILE $file";
  100. my $nfile;
  101. my $tmpfile = $file;
  102. $tmpfile =~ s|^$kmaildir||;
  103. if ($tmpfile =~ m/\/cur\// ||
  104. $tmpfile =~ m/\/new\//) {
  105. $tmpfile =~ s/\/new//;
  106. $tmpfile =~ s/\/cur//;
  107. my @spl_str = split("/", $tmpfile);
  108. pop(@spl_str);
  109. push(@spl_str, "$count");
  110. foreach my $spl_str (@spl_str) {
  111. $spl_str =~ s/\.directory$//;
  112. $spl_str =~ s/^\.//;
  113. $spl_str =~ s/^sent-mail$/sent/;
  114. }
  115. $nfile = join("/", @spl_str);
  116. $nfile = $claws_tmpdir.$nfile;
  117. }
  118. if (-e "$file" && $nfile ne "") {
  119. $DEBUG && print qq{\n+++ cp "$file" "$nfile"};
  120. $PRETEND || system("cp \"$file\" \"$nfile\"");
  121. $count++;
  122. }
  123. }