tbird2syl.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/python
  2. # Script name : tbird2syl.py
  3. # Script purpose : Integrate a Thunderbird folder tree to S-Claws
  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> <Sylpheed 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>\tbird2syl.py . <path to
  12. #sylpheed>\Mail
  13. import os
  14. import sys
  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 sylpheed MH mailbox tree.
  21. The script receives two parameters from command-line:
  22. <Thunderbird folder path> <Sylpheed folder path>
  23. Best way to use it is to go to inside yout Thunderbird
  24. root mailfolder directory and invoke it as:
  25. <path>\python2.4 <path>\tbird2syl.py . <path to sylpheed>\Mail
  26. This idiom will avoid the creation of the folder Thunderbird inside
  27. your sylpheed 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 sylpheed (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 functions returns a list of integers corresponding to
  41. the begining 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. extarct 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 sylpheed message diretory.
  75. """
  76. offs = harvest_offsets(filepath)
  77. make_messages(outputdir, filepath, offs, 1)
  78. def convert_tree(in_treepath, out_treepath):
  79. """Traverse your thunderbird tree, converting each message file found into
  80. a sylpheed message directory.
  81. """
  82. for path,subs,files in os.walk(in_treepath):
  83. if files:
  84. for f in [x for x in files if not x.endswith('.msf')]:
  85. if path.endswith('.sbd'):
  86. outpath = path[:-4]
  87. else:
  88. outpath = path
  89. print path,f
  90. process_file(os.path.join(path,f),
  91. os.path.join(out_treepath,outpath,f))
  92. if __name__=='__main__':
  93. if len(sys.argv)<3:
  94. print __doc__
  95. else:
  96. convert_tree(sys.argv[1], sys.argv[2])