This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmaildir++2kmail.py
executable file
·94 lines (78 loc) · 3.31 KB
/
maildir++2kmail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
##############################################################################
#
# Copyright (C) 2007 Kevin Deldycke <[email protected]>
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
"""
Description:
This script import a Maildir++ folder and its subfolders to Kmail local folder.
"""
#== User config ===============================================================
MAILDIR_SOURCE = "/home/kevin/Maildir"
KMAILDIR_DEST = "/home/kevin/.kde/share/apps/kmail/mail"
#== Script core ===============================================================
from os import listdir
from shutil import copytree
from os.path import abspath, isdir
# Clean-up source and destination path
maildir_path = abspath(MAILDIR_SOURCE)
kmaildir_path = abspath(KMAILDIR_DEST)
# Define folders that are part of maildir strandard structure
STD_MAILDIR = ['cur', 'new', 'tmp']
# Create the top folder where everything will be imported
IMPORT_FOLDER_NAME = "Maildir++ import"
import_folder = kmaildir_path + "/" + IMPORT_FOLDER_NAME
import_subfolder = kmaildir_path + "/." + IMPORT_FOLDER_NAME + '.directory'
# Scan the maildir++ source folder
folders = listdir(maildir_path)
folders.sort()
folders.reverse()
for folder in folders:
# Fix exotic filename encoding
folder = folder.replace("&-", "&")
# Calculate some folder properties
folder_path = maildir_path + '/' + folder
is_dir = isdir(folder_path)
# The directory is part of the standard maildir structure, import it to kmail
if folder in STD_MAILDIR and is_dir:
f_dest = import_folder + "/" + folder
print "IMPORT: %s -> %s" % (folder_path, f_dest)
copytree(folder_path, f_dest)
# The item is a valid maildir subfolder
elif folder.startswith('.') and is_dir:
# TODO: Should we check the existence of the "maildirfolder" file ?
# Calculte the new kmail location of the subfolder
new_path = []
sub_folders = folder.split(".")
for sub_folder in sub_folders[1:-1]:
new_path.append("." + sub_folder + ".directory")
new_path.append(sub_folders[-1])
new_path = "/".join(new_path)
# From each maildir++ subfolder, only import standard maildir folders, ignore everything else
for f in listdir(folder_path):
f_src = folder_path + "/" + f
if f in STD_MAILDIR:
f_dest = import_subfolder + '/' + new_path + '/' + f
print "IMPORT: %s -> %s" % (f_src, f_dest)
copytree(f_src, f_dest)
else:
print "SKIP: %s" % f_src
# The item is not a mail subfolder, ignore it
# This will catch files like "maildirfolder", "courierimap*"...
else:
print "SKIP: %s" % folder_path