fix check for mail at start-up
[claws.git] / tools / kmail-mailbox2claws-mail.pl
blobbc147e7dffac60bdd1a2476aeb95a1e1b75faed8
1 #!/usr/bin/perl -w
3 # * This file is free software; you can redistribute it and/or modify it
4 # * under the terms of the GNU General Public License as published by
5 # * the Free Software Foundation; either version 3 of the License, or
6 # * (at your option) any later version.
7 # *
8 # * This program is distributed in the hope that it will be useful, but
9 # * WITHOUT ANY WARRANTY; without even the implied warranty of
10 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # * General Public License for more details.
12 # *
13 # * You should have received a copy of the GNU General Public License
14 # * along with this program; if not, write to the Free Software
15 # * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # *
17 # * Copyright 2003-2007 Paul Mangan <paul@claws-mail.org>
18 # *
19 # * 2007-02-25: several fixes for kmail 1.9.6
20 # --kmaildir now expects the full path
21 # renamed from maildir2claws-mail.pl to kmail-mailbox2claws-mail.pl
22 # * 2003-10-01: add --debug and --dry-run options
23 # * 2003-09-30: updated/improved by Matthias Förste <itsjustme@users.sourceforge.net>
24 # * 2003-05-27: version one
26 ## script name : kmail-mailbox2claws-mail.pl
28 ## script purpose : convert a Kmail mailbox into a Claws Mail mailbox
30 ## USAGE: kmail-mailbox2claws-mail.pl --kmaildir=/full/path/to/kmail/mailbox
32 ## tested with Kmail version 1.9.6
34 use strict;
36 use Getopt::Long;
37 use File::Find;
39 my $kmaildir = '';
40 my $iNeedHelp = '';
41 # dont actually change anything if set(useful in conjunction with debug)
42 my $PRETEND = '';
43 # print debug info if set
44 my $DEBUG = '';
46 my $claws_tmpdir = "$ENV{HOME}/claws_tmp";
48 GetOptions("kmaildir=s" => \$kmaildir,
49 "help" => \$iNeedHelp,
50 "dry-run" => \$PRETEND,
51 "debug" => \$DEBUG);
53 if ($kmaildir eq "" || $iNeedHelp) {
54 if (!$iNeedHelp) {
55 print "No directory name given\n";
57 print "Use the following format:\n";
58 print "\tkmail-mailbox2claws-mail.pl --kmaildir=full-path-to-kmail-dir\n\n";
59 exit;
63 my $count = 1;
64 my $MAIL_dir = "$kmaildir";
66 my $find_opts = { wanted => \&process };
68 if (-d $MAIL_dir) {
69 find($find_opts , ($MAIL_dir));
70 } else {
71 print "\n$MAIL_dir is not a directory !\n";
72 exit;
75 unless ($PRETEND) {
76 mkdir("$claws_tmpdir", 0755);
77 system("mv $claws_tmpdir $ENV{HOME}/Mail");
79 print "\n\nSucessfully converted mailbox \"$MAIL_dir\"\n";
80 print "Start claws-mail and right-click \"Mailbox (MH)\" and ";
81 print "select \"Rebuild folder tree\"\n";
82 print "You may also need to run \"/File/Folder/Check for ";
83 print "new messages in all folders\"\n\n";
86 print "\n";
87 exit;
89 sub process() {
90 if (-d) {
91 process_dir($File::Find::dir);
92 } else {
93 process_file($File::Find::name);
97 sub process_dir() {
98 my $direc = shift();
99 $DEBUG && print "\nDIR $direc";
101 if ($direc !~ m/^drafts$/ &&
102 $direc !~ m/^outbox$/ &&
103 $direc !~ m/^trash$/ &&
104 $direc !~ m/^inbox$/) {
105 my $tmpdir = $direc;
106 $tmpdir =~ s/^$MAIL_dir//;
107 $tmpdir =~ s/\/sent-mail$/sent/;
108 $tmpdir =~ s/\/cur$//;
109 $tmpdir =~ s/\/new$//;
110 $tmpdir =~ s/^\///;
111 $tmpdir =~ s/\.directory//g;
112 $tmpdir =~ s/\.//g;
114 my $newdir = "$claws_tmpdir/$tmpdir";
115 $DEBUG && print qq{\n>>> -e "$newdir" || mkdir("$newdir")};
116 $PRETEND || -e "$newdir" || mkdir("$newdir");
121 sub process_file {
122 my $file = shift;
123 $DEBUG && print "\nFILE $file";
125 my $nfile;
126 my $tmpfile = $file;
128 $tmpfile =~ s|^$kmaildir||;
130 if ($tmpfile =~ m/\/cur\// ||
131 $tmpfile =~ m/\/new\//) {
133 $tmpfile =~ s/\/new//;
134 $tmpfile =~ s/\/cur//;
136 my @spl_str = split("/", $tmpfile);
137 pop(@spl_str);
138 push(@spl_str, "$count");
140 foreach my $spl_str (@spl_str) {
141 $spl_str =~ s/\.directory$//;
142 $spl_str =~ s/^\.//;
143 $spl_str =~ s/^sent-mail$/sent/;
145 $nfile = join("/", @spl_str);
146 $nfile = $claws_tmpdir.$nfile;
149 if (-e "$file" && $nfile ne "") {
150 $DEBUG && print qq{\n+++ cp "$file" "$nfile"};
151 $PRETEND || system("cp \"$file\" \"$nfile\"");
152 $count++;