def match_spam( cfg, depot, folder, path, args, ): try: qsf = cfg.get('qsf', 'path') except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): qsf = None rating = spamminess( path=os.path.join(depot, folder, path), _qsf=qsf, ) if rating is None: log.debug('Miss %s', path) return None if rating > 90: log.info('Spam %-3d%% %s', rating, path) rounded_rating = int(round(rating/5.0)) * 5 data = matcher.call_matcher( name=args[0], cfg=cfg, depot=depot, folder=folder, path=path, args=args[1:], ) if data is not None: data.update( spamminess_percentage=rating, spamminess_percentage_rounded_5=rounded_rating, ) return data else: log.info('Ham %-3d%% %s', rating, path) return None
def process(cfg): """ Process all new emails as according to C{cfg}. @param cfg: the configuration @type cfg: ConfigParser.SafeConfigParser """ for section in cfg.sections(): l = section.split(None, 1) if l[0] != 'depot': continue depot = l[1] depot = os.path.expanduser(depot) log.info('Depot %s', depot) def g(): found = False for var, rules_name in cfg.items(section): l = var.split(None, 1) if l[0] != 'process': continue found = True incoming_folder = l[1] yield (incoming_folder, rules_name) if not found: yield ('incoming', 'incoming') for incoming_folder, rules_name in g(): log.info( 'Incoming folder %r using rules %r', incoming_folder, rules_name, ) incoming_path = os.path.join(depot, incoming_folder) for path in maildir.walk(incoming_path): # default folder if nothing matches folder = 'INBOX' for k,v in cfg.items('rules %s' % rules_name): k = k % dict( colon=':', equals='=', ) log.debug('Rule %s = %s', k, v) l = k.split(None) name = l[0] data = matcher.call_matcher( name=name, cfg=cfg, depot=depot, folder=incoming_folder, path=path, args=l[1:], ) if data is not None: log.debug('Matcher data: %r', data) folder = v.strip() % data break maildir.create(os.path.join(depot, folder)) old_path = os.path.join( incoming_path, path, ) new_path = os.path.join( depot, folder, path, ) log.debug('Move %s to %s', old_path, new_path) try: os.rename(old_path, new_path) except OSError, e: if e.errno == errno.ENOENT: # lost a race pass else: raise