1#!/usr/bin/perl -w
2
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
25
26## script name : kmail-mailbox2claws-mail.pl
27
28## script purpose : convert a Kmail mailbox into a Claws Mail mailbox
29
30## USAGE: kmail-mailbox2claws-mail.pl --kmaildir=/full/path/to/kmail/mailbox
31
32## tested with Kmail version 1.9.6
33
34use strict;
35
36use Getopt::Long;
37use File::Find;
38
39my $kmaildir  = '';
40my $iNeedHelp = '';
41# dont actually change anything if set(useful in conjunction with debug)
42my $PRETEND = '';
43# print debug info if set
44my $DEBUG = '';
45
46my $claws_tmpdir = "$ENV{HOME}/claws_tmp";
47
48GetOptions("kmaildir=s" => \$kmaildir,
49	   "help"	=> \$iNeedHelp,
50	   "dry-run"	=> \$PRETEND,
51	   "debug"	=> \$DEBUG);
52
53if ($kmaildir eq "" || $iNeedHelp) {
54	if (!$iNeedHelp) {
55		print "No directory name given\n";
56	}
57	print "Use the following format:\n";
58	print "\tkmail-mailbox2claws-mail.pl --kmaildir=full-path-to-kmail-dir\n\n";
59	exit;
60}
61
62
63my $count = 1;
64my $MAIL_dir = "$kmaildir";
65
66my $find_opts = { wanted => \&process };
67
68if (-d $MAIL_dir) {
69	find($find_opts , ($MAIL_dir));
70} else {
71	print "\n$MAIL_dir is not a directory !\n";
72	exit;
73}
74
75unless ($PRETEND) {
76	mkdir("$claws_tmpdir", 0755);
77	system("mv $claws_tmpdir $ENV{HOME}/Mail");
78
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";
84}
85
86print "\n";
87exit;
88
89sub process() {
90  	if (-d) {
91		process_dir($File::Find::dir);
92	} else {
93		process_file($File::Find::name);
94	}
95}
96
97sub process_dir() {
98	my $direc = shift();
99  	$DEBUG && print "\nDIR $direc";
100
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;
113
114		my $newdir = "$claws_tmpdir/$tmpdir";
115		$DEBUG && print qq{\n>>> -e "$newdir" || mkdir("$newdir")};
116		$PRETEND || -e "$newdir" || mkdir("$newdir");
117	}
118
119}
120
121sub process_file {
122	my $file = shift;
123  	$DEBUG && print "\nFILE $file";
124
125  	my $nfile;
126  	my $tmpfile = $file;
127
128    	$tmpfile =~ s|^$kmaildir||;
129
130  	if ($tmpfile =~ m/\/cur\// ||
131	    $tmpfile =~ m/\/new\//) {
132
133    		$tmpfile =~ s/\/new//;
134    		$tmpfile =~ s/\/cur//;
135
136    		my @spl_str = split("/", $tmpfile);
137    		pop(@spl_str);
138    		push(@spl_str, "$count");
139
140    		foreach my $spl_str (@spl_str) {
141			$spl_str =~ s/\.directory$//;
142			$spl_str =~ s/^\.//;
143			$spl_str =~ s/^sent-mail$/sent/;
144		}
145    		$nfile = join("/", @spl_str);
146		$nfile = $claws_tmpdir.$nfile;
147	}
148
149	if (-e "$file" && $nfile ne "") {
150    		$DEBUG && print qq{\n+++ cp "$file" "$nfile"};
151    		$PRETEND || system("cp \"$file\" \"$nfile\"");
152    		$count++;
153  	}
154
155}
156