Ejemplo n.º 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()
Ejemplo n.º 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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
 def test_lookup(self):
     with mock.patch.object(actions, '_lookup_table', {}) as lt:
         lt['move'] = mock.Mock()
         actions.factory({'name': 'move'}, {})
         lt['move'].assert_called_with({'name': 'move'}, {})
 def test_trash(self, trash):
     actions.factory({'name': 'trash'}, {})
     trash.assert_called_with({'name': 'trash'}, {})
 def test_delete(self, delete):
     actions.factory({'name': 'delete'}, {})
     delete.assert_called_with({'name': 'delete'}, {})
 def test_move(self, move):
     actions.factory({'name': 'move'}, {})
     move.assert_called_with({'name': 'move'}, {})