tbird2claws.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/python
  2. # Script name : tbird2claws.py
  3. # Script purpose : Integrate a Thunderbird folder tree to Claws Mail
  4. # Author : Aleksandar Urosevic aka Urke MMI <urke@gmx.net>
  5. # Licence : GPL
  6. # Author: Rodrigo Dias Arruda Senra
  7. #The script receives two parameters from command-line:
  8. #<Thunderbird folder path> <Claws Mail folder path>
  9. #Best way to use it is to go to inside yout Thunderbird
  10. #root mailfolder directory and invoke it as:
  11. #<path>\python2.4 <path>\tbird2claws.py . <path to
  12. #claws-mail>\Mail
  13. import os
  14. import sys
  15. from imp import reload
  16. __author__ = 'Rodrigo Senra <rsenra@acm.org>'
  17. __date__ = '2005-03-23'
  18. __version__ = '0.3'
  19. __doc__ = r"""
  20. This module integrates your Mozilla Thunderbird 1.0 tree to
  21. your Claws Mail MH mailbox tree.
  22. The script receives two parameters from command-line:
  23. <Thunderbird folder path> <Claws Mail folder path>
  24. Best way to use it is to go to inside your Thunderbird
  25. root mailfolder directory and invoke it as:
  26. <path>\python2.4 <path>\tbird2syl.py . <path to claws mail>\Mail
  27. This idiom will avoid the creation of the folder Thunderbird inside
  28. your Claws Mail folder tree.
  29. If the names of your directories match in both trees, files should
  30. be placed in the correct folder.
  31. This is an alpha release, so it may be a little rough around the edges.
  32. Nevertheless, I used it with great success to convert a very large and
  33. deep folder tree.
  34. Please, do backup your claws-mail (destination) folder tree before trying
  35. this out. Live safe and die old!
  36. This code is released in the public domain.
  37. """
  38. def harvest_offsets(filepath):
  39. """Given the filepath, this runs through the file finding
  40. the number of the line where a message begins.
  41. The function returns a list of integers corresponding to
  42. the beginning of messages.
  43. """
  44. offsets = []
  45. i = 0
  46. state = 'begin'
  47. for i,line in enumerate(open(filepath)):
  48. if line.startswith('From - ') and state!='found_head':
  49. offsets.append(i)
  50. continue
  51. # elif line.startswith('Return-Path') and state=='found_head':
  52. # state = 'found_offset'
  53. # offsets.append(i)
  54. # continue
  55. offsets.append(i)
  56. return offsets
  57. def make_messages(outputdir, filepath, offsets, start):
  58. """Given a filepath holding several messages in Thunderbird format,
  59. extract the messages and create individual files for them, inside
  60. outputdir with appropriate the appropriate naming scheme.
  61. """
  62. if not os.path.exists(outputdir):
  63. os.makedirs(outputdir)
  64. if not os.path.exists(filepath):
  65. raise Exception('Cannot find message file %s'%(filepath))
  66. lines = open(filepath).readlines()
  67. aux = offsets[:]
  68. msgoffs = zip(offsets[:-1], aux[1:])
  69. for i,j in msgoffs:
  70. fd = open(os.path.join(outputdir,"%d"%start),"w")
  71. fd.write(''.join(lines[i:j-1])) #-1 to remove first from line
  72. fd.close()
  73. start +=1
  74. def process_file(filepath, outputdir):
  75. """Integrates a Thunderbird message file into a claws-mail message directory.
  76. """
  77. offs = harvest_offsets(filepath)
  78. make_messages(outputdir, filepath, offs, 1)
  79. def clean_path(path):
  80. """Rename all directories and subdirectories <X>.sbd to <X>
  81. """
  82. l = []
  83. f = os.path.basename(path)
  84. while f and f != "":
  85. if f.endswith('.sbd'):
  86. f = f[:-4]
  87. l.append(f)
  88. path = os.path.dirname(path)
  89. f = os.path.basename(path)
  90. l.reverse()
  91. r = os.path.join(*l)
  92. return r
  93. def convert_tree(in_treepath, out_treepath):
  94. """Traverse your thunderbird tree, converting each message file found into
  95. a claws-mail message directory.
  96. """
  97. for path,subs,files in os.walk(in_treepath):
  98. outpath = clean_path(path)
  99. if files:
  100. for f in [x for x in files if not x.endswith('.msf')]:
  101. process_file(os.path.join(path,f),
  102. os.path.join(out_treepath,outpath,f))
  103. if __name__=='__main__':
  104. if len(sys.argv)<3:
  105. print (__doc__)
  106. else:
  107. if sys.version[0] == '2':
  108. reload(sys)
  109. sys.setdefaultencoding('utf8')
  110. convert_tree(sys.argv[1], sys.argv[2])