#!/usr/bin/python # This library is inspired by tbprocmailconv.py script # writed by Michael Scheper : # http://tbprocmailconv.sourceforge.net/ # Some part of this script is simply copy from original # script with some corrections and adjustments. # # It's part of thunderbird2sogo scripts. # # Author : Benjamin Renard # Date : Wed, 25 Dec 2013 20:41:39 +0100 # Source : http://git.zionetrix.net/thunderbird2sogo import os import re import sys import urllib import logging from replace_accents import replace_accents def read_from_file(file): "Read the Thunderbird filters file into a list of maps." filters = [] current_filter = None current_actions = [] warnings = [] # Each value gets run through each of these regex replacements. value_replacements = ( (re.compile(r'\\\"'), '"'), (re.compile(r'^ *\"(.*)\"\r*\n?$'), r'\1'), ) for line in open(file): (key, value) = line.split('=',1) for (regex, repl) in value_replacements: value = regex.sub(repl, value) logging.debug('Key %s => %s' % (key,value)) # Each filter starts with a name. if key == 'name': if current_filter is not None: if any(current_actions): current_filter['actions'] = current_actions logging.debug("Add filter : %s" % current_filter) filters.append(current_filter) logging.debug("Initialize new filter") current_filter = {} current_actions = [] # If we're still not up to the first filter, ignore this line. if current_filter is None: continue # Each filter can have multiple actions. Each action has an optional # actionValue. if key == 'action': logging.debug('New action : %s' % value) # We'll add the value later if there is one. current_actions.append((value, None)) elif key == 'actionValue': if not any(current_actions): warnings.append('Ignored unexpected action value "%s" in ' '"%s"' % (current_actions[-1][0], current_filter.get('name', '(unnamed filter)'))) elif current_actions[-1][1] is not None: warnings.append('Ignored extra value "%s" for action "%s" ' 'in "%s"' % (value, current_actions[-1][0], current_filter.get('name', '(unnamed filter)'))) else: current_action = current_actions.pop() current_actions.append((current_action[0], value)) else: if key in current_filter: warnings.append('Ignored extra %s ("%s") in %s' % (key, value, current_filter.get('name', '(unnamed filter)'))) else: current_filter[key] = value if any(current_actions): current_filter['actions'] = current_actions logging.debug("Add filter : %s" % current_filter) filters.append(current_filter) return (filters, warnings) condition_regex = re.compile('(AND|OR) \(([^,]+),([^,]+),([^)]+)\) *') def convert_conditions(tb_condition): "Converts a Thunderbird condition string into procmail condition strings." conditions = [] for match in condition_regex.finditer(tb_condition): (bool_operator, cri_operand, cri_operator, value) = match.groups() conditions.append({ 'bool_operator': bool_operator, 'cri_operand': cri_operand, 'cri_operator': cri_operator, 'value': value }) return conditions _convert_uri_path_to_sogo=re.compile('^[a-z]*\:\/\/[^\/]*\/(.*)$') def convert_uri_path_to_maildir(path,separator='.',replaceaccents=False): # URL decode path=urllib.unquote(path) # Remove URI prefix path=_convert_uri_path_to_sogo.sub(r'\1',path) # Replace accents if replaceaccents: path=replace_accents(path) # UTF7 encode path=path.decode('UTF-8').encode('UTF-7') # Replace '+' by '&' path=re.sub('\+','&',path) # Replace ^Inbox by INBOX path=re.sub('^Inbox\/','INBOX/',path) # Replace '/' by separator if separator!='/': path=re.sub('\/',separator,path) return path