Exemple #1
0
def test_process_empty():
    tmp = maketemp()
    incoming_path = os.path.join(tmp, 'incoming')
    maildir.create(incoming_path)
    cfg = ConfigParser.RawConfigParser()
    cfg.add_section('depot %s' % tmp)
    incoming.process(cfg=cfg)
Exemple #2
0
def test_process_newline():
    # line wrapping in the config file can cause extra whitespace in
    # the value; this affects all rules but testing with this one
    tmp = maketemp()
    depot = os.path.join(tmp, 'depot')
    os.mkdir(depot)
    incoming_path = os.path.join(depot, 'incoming')
    maildir.create(incoming_path)
    MAILBODY = """\
From: bar
To: foo
List-ID: Users of FOO <FOO-users.lists.example.COM>
Subject: foo

foo
"""
    writeFile(
        os.path.join(
            incoming_path,
            'new',
            '1202936960.V801I880d5M358047.foo',
            ),
        MAILBODY,
        )
    cfg = ConfigParser.RawConfigParser()
    cfg.add_section('depot %s' % depot)
    cfg.add_section('rules incoming')
    cfg.set(
        'rules incoming',
        'angle-bracket-header liST-id foo-USERS.lists.EXAMPLE.com',
        '\nlist.foo\n',
        )
    incoming.process(cfg)
    folder = 'list.foo'
    eq(
        sorted(os.listdir(depot)),
        sorted(['incoming', folder]),
        )
    eq(
        list(maildir.walk(os.path.join(depot, 'incoming'))),
        [],
        )
    path = os.path.join(depot, folder)
    eq(
        list(maildir.walk(path)),
        [
            'new/1202936960.V801I880d5M358047.foo',
            ],
        )
    eq(
        readFile(os.path.join(
                path,
                'new',
                '1202936960.V801I880d5M358047.foo',
                )),
        MAILBODY,
        )
Exemple #3
0
def test_process_post_colon():
    # bah ConfigParser won't let the config keys contain either ":" or "="
    tmp = maketemp()
    depot = os.path.join(tmp, 'depot')
    os.mkdir(depot)
    incoming_path = os.path.join(depot, 'incoming')
    maildir.create(incoming_path)
    MAILBODY = """\
From: bar
To: foo
List-Post: <mailto:[email protected]>
Subject: foo

foo
"""
    writeFile(
        os.path.join(
            incoming_path,
            'new',
            '1202936960.V801I880d5M358047.foo',
            ),
        MAILBODY,
        )
    cfg = ConfigParser.RawConfigParser()
    cfg.add_section('depot %s' % depot)
    cfg.add_section('rules incoming')
    cfg.set(
        'rules incoming',
        'angle-bracket-header liST-post mailto%(colon)[email protected]',
        'list.foo',
        )
    incoming.process(cfg)
    folder = 'list.foo'
    eq(
        sorted(os.listdir(depot)),
        sorted(['incoming', folder]),
        )
    eq(
        list(maildir.walk(os.path.join(depot, 'incoming'))),
        [],
        )
    path = os.path.join(depot, folder)
    eq(
        list(maildir.walk(path)),
        [
            'new/1202936960.V801I880d5M358047.foo',
            ],
        )
    eq(
        readFile(os.path.join(
                path,
                'new',
                '1202936960.V801I880d5M358047.foo',
                )),
        MAILBODY,
        )
Exemple #4
0
def test_create_simple():
    tmp = maketemp()
    maildir.create(os.path.join(tmp, 'mdir'))
    eq(os.listdir(tmp), ['mdir'])
    eq(
        sorted(os.listdir(os.path.join(tmp, 'mdir'))),
        sorted([
                'cur',
                'new',
                'tmp',
                ]),
        )
Exemple #5
0
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
Exemple #6
0
def test_process_spam():
    tmp = maketemp()
    depot = os.path.join(tmp, 'depot')
    os.mkdir(depot)
    incoming_path = os.path.join(depot, 'incoming')
    maildir.create(incoming_path)
    writeFile(
        os.path.join(
            incoming_path,
            'new',
            '1202936960.V801I880d5M358047.foo',
            ),
        'fake email',
        )
    mockqsf = os.path.join(tmp, 'mock-qsf')
    writeFile(
        mockqsf,
        """\
#!/bin/sh
test "$#" = "2"
test "$1" = "--test"
test "$2" = "--rating"
test "$(cat)" = "fake email"
echo 97
exit 1
""")
    os.chmod(mockqsf, 0755)
    cfg = ConfigParser.RawConfigParser()
    cfg.add_section('qsf')
    cfg.set('qsf', 'path',  mockqsf)
    cfg.add_section('depot %s' % depot)
    cfg.add_section('rules incoming')
    cfg.set(
        'rules incoming',
        'spam all',
        'INBOX.spam.%(spamminess_percentage_rounded_5)d',
        )
    incoming.process(cfg)
    folder = 'INBOX.spam.95'
    eq(
        sorted(os.listdir(depot)),
        sorted(['incoming', folder]),
        )
    eq(
        list(maildir.walk(os.path.join(depot, 'incoming'))),
        [],
        )
    path = os.path.join(depot, folder)
    eq(
        list(maildir.walk(path)),
        [
            'new/1202936960.V801I880d5M358047.foo',
            ],
        )
    eq(
        readFile(os.path.join(
                path,
                'new',
                '1202936960.V801I880d5M358047.foo',
                )),
        'fake email',
        )