2007-10-18 [mones] 3.0.2cvs86
[claws.git] / tools / tbird2claws.py
1 #!/usr/bin/python
2
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>
6 # Licence : GPL
7 # Author: Rodrigo Dias Arruda Senra
8
9 #The script receives two parameters from command-line:
10 #<Thunderbird folder path> <Claws Mail folder path>
11
12 #Best way to use it is to go to inside yout Thunderbird
13 #root mailfolder directory and invoke it as:
14
15 #<path>\python2.4 <path>\tbird2claws.py . <path to
16 #claws-mail>\Mail
17
18 import os
19 import sys
20
21 __author__ = 'Rodrigo Senra <rsenra@acm.org>'
22 __date__ =  '2005-03-23'
23 __version__ =  '0.3'
24
25 __doc__ = r"""
26 This module integrates your Mozilla Thunderbird 1.0 tree to
27 your Claws Mail MH mailbox tree.
28
29 The script receives two parameters from command-line:
30  <Thunderbird folder path> <Claws Mail folder path>
31
32 Best way to use it is to go to inside yout Thunderbird
33 root mailfolder directory and invoke it as:
34
35   <path>\python2.4 <path>\tbird2syl.py . <path to claws mail>\Mail
36
37 This idiom will avoid the creation of the folder Thunderbird inside
38 your Claws Mail folder tree.
39
40 If the names of your directories match in both trees, files should
41 be placed in the correct folder.
42
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
45 deep folder tree.
46
47 Please, do backup your claws-mail (destination) folder tree before trying
48 this out. Live safe and die old!
49
50 This code is released in the public domain.
51 """
52
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.
56     
57     The functions returns a list of integers corresponding to
58     the begining of messages.
59     """ 
60     offsets = []
61     i = 0
62     state = 'begin'
63     for i,line in enumerate(open(filepath)):
64         if line.startswith('From - ') and state!='found_head':
65             offsets.append(i)
66             continue
67 #        elif line.startswith('Return-Path') and state=='found_head':
68 #           state = 'found_offset'
69 #           offsets.append(i)
70 #           continue
71     offsets.append(i)
72     return offsets
73
74 def make_messages(outputdir, filepath, offsets, start):
75     """Given a filepath holding several messages in Thunderbird format,
76     extarct the messages and create individual files for them, inside
77     outputdir with appropriate the appropriate naming scheme.
78     """ 
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()
84     aux = offsets[:]
85     msgoffs = zip(offsets[:-1], aux[1:])
86     for i,j in msgoffs:
87         fd  = open(os.path.join(outputdir,"%d"%start),"w")
88         fd.write(''.join(lines[i:j-1])) #-1 to remove first from line
89         fd.close()
90         start +=1
91
92 def process_file(filepath, outputdir):
93     """Integrates a Thunderbird message file into a claws-mail message diretory.
94     """  
95     offs = harvest_offsets(filepath)
96     make_messages(outputdir, filepath, offs, 1)
97
98
99 def convert_tree(in_treepath, out_treepath):
100     """Traverse your thunderbird tree, converting each message file found into
101     a claws-mail message directory.
102     """
103     for path,subs,files in os.walk(in_treepath):
104         if files:
105             for f in [x for x in files if not x.endswith('.msf')]:
106                 if path.endswith('.sbd'):
107                     outpath = path[:-4]
108                 else:
109                     outpath = path
110                 print path,f
111                 process_file(os.path.join(path,f),
112                              os.path.join(out_treepath,outpath,f))
113     
114 if __name__=='__main__':
115     if len(sys.argv)<3:
116         print __doc__
117     else:
118         convert_tree(sys.argv[1], sys.argv[2])