3 # Script name : tbird2claws.py
4 # Script purpose : Integrate a Thunderbird folder tree to Claws Mail
5 # Author : Aleksandar Urosevic aka Urke MMI <urke@gmx.net>
7 # Author: Rodrigo Dias Arruda Senra
9 #The script receives two parameters from command-line:
10 #<Thunderbird folder path> <Claws Mail folder path>
12 #Best way to use it is to go to inside yout Thunderbird
13 #root mailfolder directory and invoke it as:
15 #<path>\python3 <path>\tbird2claws.py . <path to claws-mail>\Mail
21 __author__
= 'Rodrigo Senra <rsenra@acm.org>'
22 __date__
= '2005-03-23'
26 This module integrates your Mozilla Thunderbird 1.0 tree to
27 your Claws Mail MH mailbox tree.
29 The script receives two parameters from command-line:
30 <Thunderbird folder path> <Claws Mail folder path>
32 Best way to use it is to go to inside your Thunderbird
33 root mailfolder directory and invoke it as:
35 <path>\python3 <path>\tbird2claws.py . <path to claws mail>\Mail
37 This idiom will avoid the creation of the folder Thunderbird inside
38 your Claws Mail folder tree.
40 If the names of your directories match in both trees, files should
41 be placed in the correct folder.
43 This is an alpha release, so it may be a little rough around the edges.
44 Nevertheless, I used it with great success to convert a very large and
47 Please, do backup your claws-mail (destination) folder tree before trying
48 this out. Live safe and die old!
50 This code is released in the public domain.
53 def harvest_offsets(filepath
):
54 """Given the filepath, this runs through the file finding
55 the number of the line where a message begins.
57 The function returns a list of integers corresponding to
58 the beginning of messages.
63 for i
,line
in enumerate(open(filepath
)):
64 if line
.startswith('From - ') and state
!='found_head':
67 # elif line.startswith('Return-Path') and state=='found_head':
68 # state = 'found_offset'
74 def make_messages(outputdir
, filepath
, offsets
, start
):
75 """Given a filepath holding several messages in Thunderbird format,
76 extract the messages and create individual files for them, inside
77 outputdir with appropriate the appropriate naming scheme.
79 if not os
.path
.exists(outputdir
):
80 os
.makedirs(outputdir
)
81 if not os
.path
.exists(filepath
):
82 raise Exception('Cannot find message file %s'%(filepath))
83 lines
= open(filepath
).readlines()
85 msgoffs
= zip(offsets
[:-1], aux
[1:])
87 fd
= open(os
.path
.join(outputdir
,"%d"%start
),"w")
88 fd
.write(''.join(lines
[i
:j
-1])) #-1 to remove first from line
92 def process_file(filepath
, outputdir
):
93 """Integrates a Thunderbird message file into a claws-mail message directory.
95 offs
= harvest_offsets(filepath
)
96 make_messages(outputdir
, filepath
, offs
, 1)
99 """Rename all directories and subdirectories <X>.sbd to <X>
102 f
= os
.path
.basename(path
)
104 if f
.endswith('.sbd'):
107 path
= os
.path
.dirname(path
)
108 f
= os
.path
.basename(path
)
115 def convert_tree(in_treepath
, out_treepath
):
116 """Traverse your thunderbird tree, converting each message file found into
117 a claws-mail message directory.
119 for path
,subs
,files
in os
.walk(in_treepath
):
120 outpath
= clean_path(path
)
122 for f
in [x
for x
in files
if not x
.endswith('.msf')]:
123 process_file(os
.path
.join(path
,f
),
124 os
.path
.join(out_treepath
,outpath
,f
))
126 if __name__
=='__main__':
130 if sys
.version
[0] == '2':
131 importlib
.reload(sys
)
132 sys
.setdefaultencoding('utf8')
133 convert_tree(sys
.argv
[1], sys
.argv
[2])