Remove support for the obsolete AWN
[claws.git] / tools / tbird2claws.py
blob8cf2bb25fa6289341e194560e62710662e6c0909
1 #!/usr/bin/env python3
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
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
17 import os
18 import sys
19 import importlib
21 __author__ = 'Rodrigo Senra <rsenra@acm.org>'
22 __date__ = '2005-03-23'
23 __version__ = '0.3'
25 __doc__ = r"""
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
45 deep folder tree.
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.
51 """
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.
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
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.
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
92 def process_file(filepath, outputdir):
93 """Integrates a Thunderbird message file into a claws-mail message directory.
94 """
95 offs = harvest_offsets(filepath)
96 make_messages(outputdir, filepath, offs, 1)
98 def clean_path(path):
99 """Rename all directories and subdirectories <X>.sbd to <X>
101 l = []
102 f = os.path.basename(path)
103 while f and f != "":
104 if f.endswith('.sbd'):
105 f = f[:-4]
106 l.append(f)
107 path = os.path.dirname(path)
108 f = os.path.basename(path)
109 l.reverse()
110 r = os.path.join(*l)
111 return r
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)
121 if files:
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__':
127 if len(sys.argv)<3:
128 print (__doc__)
129 else:
130 if sys.version[0] == '2':
131 importlib.reload(sys)
132 sys.setdefaultencoding('utf8')
133 convert_tree(sys.argv[1], sys.argv[2])