tbird2claws.py 4.1 KB

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