Example #1
0
def gen_recipients_from_cache(options, progress=False):
    if not os.path.exists(cache_messages_path):
        return {}
    max_age = options['max_age']
    dstore = shelve.open(cache_messages_path, flag='r', protocol=2)
    recipients = {}
    n_messages = len(dstore)
    log.info('cache only, %d messages', n_messages)
    if progress:
        pstatus = log.PercentStatus(n_messages, prefix='      ')
    for identifier in dstore:
        if progress:
            pstatus.inc()
            pstatus.output()
        msg = scan.Message()
        msg.from_dict_only(dstore[identifier])
        if options['skip_multiple_recipients'] and len(msg.to_emails) > 1:
            continue
        if options['exclude_mails_to_me'] and filter_any(config.is_this_me, msg.to_emails):
            continue
        if options['only_include_mails_from_me'] and not config.is_this_me(msg.from_email):
            continue
        if max_age >= 0 and msg.age > max_age:
            continue
        if msg.to_emails_str not in recipients:
            r = scan.Recipient(msg.to_emails, msg)
            recipients[msg.to_emails_str] = r
        else:
            recipients[msg.to_emails_str].add(msg)
    if progress:
        pstatus.finish()
    dstore.close()
    return recipients
Example #2
0
def gen_recipients(mailboxes, options, use_cache=True, clean_cache=False, progress=False):
    base = os.path.dirname(cache_messages_path)
    if not os.path.exists(base):
        os.makedirs(base)
    if not os.path.exists(cache_messages_path):
        # if there is no cache, it doesn't need to be cleaned
        clean_cache = False
    max_age = options['max_age']
    flag = 'r' if clean_cache else 'c'
    dstore = shelve.open(cache_messages_path, flag=flag, protocol=2)
    dstore_write = shelve.open(cache_messages_tmp_path, flag='n', protocol=2) if clean_cache else dstore
    recipients = {}
    n_mailboxes = len(mailboxes)
    for i, mb in enumerate(mailboxes):
        if progress:
            n_messages = len(mb)
            log.info('[%d/%d] %s: %d messages', i+1, n_mailboxes, mb.path, n_messages)
            pstatus = log.PercentStatus(n_messages, prefix='      ')
        for msg in mb.messages():
            if progress:
                pstatus.inc()
                pstatus.output()
            d = dstore.get(msg.identifier)
            if use_cache and d and not msg.has_changed(d):
                msg.from_dict(d)
                if clean_cache:
                    dstore_write[msg.identifier] = d
            else:
                if not msg.parse_header():
                    continue
                d = {}
                msg.parse_body()
                if msg.identifier:
                    msg.to_dict(d)
                    dstore_write[msg.identifier] = d

            if options['skip_multiple_recipients'] and len(msg.to_emails) > 1:
                continue
            if options['exclude_mails_to_me'] and filter_any(config.is_this_me, msg.to_emails):
                continue
            if options['only_include_mails_from_me'] and not config.is_this_me(msg.from_email):
                continue
            if max_age >= 0 and msg.age > max_age:
                continue

            if msg.to_emails_str not in recipients:
                r = scan.Recipient(msg.to_emails, msg)
                recipients[msg.to_emails_str] = r
            else:
                recipients[msg.to_emails_str].add(msg)
        if progress:
            pstatus.finish()
    dstore.close()
    if clean_cache:
        dstore_write.close()
        os.rename(cache_messages_tmp_path, cache_messages_path)
    return recipients
Example #3
0
        body = self.body
        if self.greeting:
            body = self._re_greeting.sub(u'', body, 1).lstrip('\n')

        mb = MessageBody(body, self._re_quote, self._re_smileys)

        if len(mb.interleaved) + len(mb.bottom) > len(mb.top):
            self.posting_style = u'inline'

        init_guess_language()
        words_to_guess = u' '.join(re.split(r'\W+', u'%s %s' % (mb.unquoted, mb.quoted))[:20])
        guessed_language = guessLanguage(words_to_guess)
        self.language = guessed_language if guessed_language != 'UNKNOWN' else ''

        if not config.get('personalize_mailinglists') and filter_any(config.is_mailinglist, self.to_emails):
            self.greeting = u''
            return True

        if not mb.unquoted:
            self.greeting = u''

        match = self._re_goodbye.search(mb.top.rstrip('\n'))
        if not match:
            match = self._re_goodbye.search(mb.bottom.rstrip('\n'))
        if match:
            body = self._re_goodbye.sub(u'', body, 1).strip('\n')
            if body:
                self.goodbye = match.group(1)

        return True