+

Last commit for thunderbirdFilters.py: 4b2682d820b2fecb33e662d7706490b001967e0f

Initial commit

Benjamin Renard [2013-12-25 20:10:39]
Initial commit
  1. #!/usr/bin/python
  2.  
  3. # This library is inspired by tbprocmailconv.py script
  4. # writed by Michael Scheper :
  5. # http://tbprocmailconv.sourceforge.net/
  6. # Some part of this script is simply copy from original
  7. # script with some corrections and adjustments.
  8. #
  9. # It's part of thunderbird2sogo scripts.
  10. #
  11. # Author : Benjamin Renard <brenard@zionetrix.net>
  12. # Date : Wed, 25 Dec 2013 20:41:39 +0100
  13. # Source : http://git.zionetrix.net/thunderbird2sogo
  14.  
  15. import os
  16. import re
  17. import sys
  18. import urllib
  19. import logging
  20. from replace_accents import replace_accents
  21.  
  22. def read_from_file(file):
  23. "Read the Thunderbird filters file into a list of maps."
  24.  
  25. filters = []
  26. current_filter = None
  27. current_actions = []
  28. warnings = []
  29.  
  30. # Each value gets run through each of these regex replacements.
  31. value_replacements = (
  32. (re.compile(r'\\\"'), '"'),
  33. (re.compile(r'^ *\"(.*)\"\r*\n?$'), r'\1'),
  34. )
  35.  
  36. for line in open(file):
  37. (key, value) = line.split('=',1)
  38. for (regex, repl) in value_replacements:
  39. value = regex.sub(repl, value)
  40.  
  41. logging.debug('Key %s => %s' % (key,value))
  42. # Each filter starts with a name.
  43. if key == 'name':
  44. if current_filter is not None:
  45. if any(current_actions):
  46. current_filter['actions'] = current_actions
  47. logging.debug("Add filter : %s" % current_filter)
  48. filters.append(current_filter)
  49. logging.debug("Initialize new filter")
  50. current_filter = {}
  51. current_actions = []
  52.  
  53. # If we're still not up to the first filter, ignore this line.
  54. if current_filter is None:
  55. continue
  56.  
  57. # Each filter can have multiple actions. Each action has an optional
  58. # actionValue.
  59. if key == 'action':
  60. logging.debug('New action : %s' % value)
  61. # We'll add the value later if there is one.
  62. current_actions.append((value, None))
  63. elif key == 'actionValue':
  64. if not any(current_actions):
  65. warnings.append('Ignored unexpected action value "%s" in '
  66. '"%s"' % (current_actions[-1][0],
  67. current_filter.get('name', '(unnamed filter)')))
  68. elif current_actions[-1][1] is not None:
  69. warnings.append('Ignored extra value "%s" for action "%s" '
  70. 'in "%s"' % (value, current_actions[-1][0],
  71. current_filter.get('name', '(unnamed filter)')))
  72. else:
  73. current_action = current_actions.pop()
  74. current_actions.append((current_action[0], value))
  75. else:
  76. if key in current_filter:
  77. warnings.append('Ignored extra %s ("%s") in %s' % (key, value,
  78. current_filter.get('name', '(unnamed filter)')))
  79. else:
  80. current_filter[key] = value
  81. if any(current_actions):
  82. current_filter['actions'] = current_actions
  83. logging.debug("Add filter : %s" % current_filter)
  84. filters.append(current_filter)
  85. return (filters, warnings)
  86.  
  87. condition_regex = re.compile('(AND|OR) \(([^,]+),([^,]+),([^)]+)\) *')
  88. def convert_conditions(tb_condition):
  89. "Converts a Thunderbird condition string into procmail condition strings."
  90. conditions = []
  91.  
  92. for match in condition_regex.finditer(tb_condition):
  93.  
  94. (bool_operator, cri_operand, cri_operator, value) = match.groups()
  95.  
  96. conditions.append({
  97. 'bool_operator': bool_operator,
  98. 'cri_operand': cri_operand,
  99. 'cri_operator': cri_operator,
  100. 'value': value
  101. })
  102.  
  103. return conditions
  104.  
  105. _convert_uri_path_to_sogo=re.compile('^[a-z]*\:\/\/[^\/]*\/(.*)$')
  106. def convert_uri_path_to_maildir(path,separator='.',replaceaccents=False):
  107. # URL decode
  108. path=urllib.unquote(path)
  109. # Remove URI prefix
  110. path=_convert_uri_path_to_sogo.sub(r'\1',path)
  111. # Replace accents
  112. if replaceaccents:
  113. path=replace_accents(path)
  114. # UTF7 encode
  115. path=path.decode('UTF-8').encode('UTF-7')
  116. # Replace '+' by '&'
  117. path=re.sub('\+','&',path)
  118. # Replace ^Inbox by INBOX
  119. path=re.sub('^Inbox\/','INBOX/',path)
  120. # Replace '/' by separator
  121. if separator!='/':
  122. path=re.sub('\/',separator,path)
  123. return path
  124.