コード例 #1
0
def process_rules(cfg, debug, conn):
    num_messages = 0
    num_processed = 0

    for mailbox in cfg['mailboxes']:  # multiple mailboxes allowed
        mailbox_name = mailbox['name']
        conn.select_folder(mailbox_name)

        mailbox_rules = [  # convert data to instances
            rules.factory(r, cfg) for r in mailbox['rules']
        ]

        msg_ids = conn.search(['ALL'])

        for msg_id in msg_ids:
            num_messages += 1
            message = get_message(conn, msg_id)

            for rule in mailbox_rules:
                if rule.check(message):
                    action = actions.factory(rule.get_action(), cfg)
                    action.invoke(conn, msg_id, message)
                    num_processed += 1
                    break

        # Remove messages that we just moved.
        conn.expunge()
コード例 #2
0
def process_rules(cfg, debug, conn, dry_run=False):
    """Run the rules from the configuration file.

    :param cfg: full configuration
    :type cfg: dict
    :param debug: flag to control debug output
    :type debug: bool
    :param conn: IMAP server onnection
    :type conn: imapautofiler.client.Client

    """
    num_messages = 0
    num_processed = 0
    num_errors = 0

    for mailbox in cfg['mailboxes']:
        mailbox_name = mailbox['name']

        mailbox_rules = [rules.factory(r, cfg) for r in mailbox['rules']]

        for (msg_id, message) in conn.mailbox_iterate(mailbox_name):
            num_messages += 1
            if debug:
                print(message.as_string().rstrip())
            else:
                LOG.debug('message %s: %s', msg_id, message['subject'])

            for rule in mailbox_rules:
                if rule.check(message):
                    action = actions.factory(rule.get_action(), cfg)
                    try:
                        action.report(conn, mailbox_name, msg_id, message)
                        if not dry_run:
                            action.invoke(conn, mailbox_name, msg_id, message)
                    except Exception as err:
                        LOG.error('failed to %s "%s": %s', action.NAME,
                                  message['subject'], err)
                        num_errors += 1
                        if debug:
                            raise
                    else:
                        num_processed += 1
                    # At this point we've processed the message
                    # based on one rule, so there is no need to
                    # look at the other rules.
                    break
                else:
                    LOG.debug('no rules match')

            # break

        # Remove messages that we just moved.
        conn.expunge()
    LOG.info('encountered %s messages, processed %s', num_messages,
             num_processed)
    if num_errors:
        LOG.info('encountered %d errors', num_errors)
    return
コード例 #3
0
ファイル: app.py プロジェクト: dhellmann/imapautofiler
def process_rules(cfg, debug, conn):
    num_messages = 0
    num_processed = 0

    for mailbox in cfg['mailboxes']:
        mailbox_name = mailbox['name']
        conn.select_folder(mailbox_name)

        mailbox_rules = [
            rules.factory(r, cfg)
            for r in mailbox['rules']
        ]

        msg_ids = conn.search(['ALL'])

        for msg_id in msg_ids:
            num_messages += 1
            message = get_message(conn, msg_id)
            if debug:
                print(message.as_string().rstrip())
            else:
                LOG.debug('message %s: %s', msg_id, message['subject'])

            for rule in mailbox_rules:
                if rule.check(message):
                    action = actions.factory(rule.get_action(), cfg)
                    action.invoke(conn, msg_id, message)
                    # At this point we've processed the message
                    # based on one rule, so there is no need to
                    # look at the other rules.
                    num_processed += 1
                    break
                else:
                    LOG.debug('no rules match')

            # break

        # Remove messages that we just moved.
        conn.expunge()
    LOG.info('encountered %s messages, processed %s',
             num_messages, num_processed)
    return
コード例 #4
0
 def test_lookup(self):
     with mock.patch.object(rules, '_lookup_table', {}) as lt:
         lt['or'] = mock.Mock()
         rules.factory({'or': {}}, {})
         lt['or'].assert_called_with({'or': {}}, {})
コード例 #5
0
ファイル: test_rules.py プロジェクト: dhellmann/imapautofiler
 def test_headers(self, headers):
     rules.factory({'headers': {}}, {})
     headers.assert_called_with({'headers': {}}, {})
コード例 #6
0
ファイル: test_rules.py プロジェクト: dhellmann/imapautofiler
 def test_or(self, or_):
     rules.factory({'or': {}}, {})
     or_.assert_called_with({'or': {}}, {})
 def test_headers(self, headers):
     rules.factory({'headers': {}}, {})
     headers.assert_called_with({'headers': {}}, {})
 def test_or(self, or_):
     rules.factory({'or': {}}, {})
     or_.assert_called_with({'or': {}}, {})