コード例 #1
0
ファイル: spamproc.py プロジェクト: baldurmen/isbg
def test_mail(mail, spamc=False, cmd=False):
    """Test a email with spamassassin."""
    score = "0/0\n"
    orig_code = None
    spamassassin_result = None
    returncode = None

    if cmd:
        satest = cmd
    elif spamc:
        # let spamc process mails larger than 500 KB if ISBG forwards them
        satest = ["spamc", "-E", "--max-size=268435450"]
    else:
        satest = ["spamassassin", "--exit-code"]

    proc = utils.popen(satest)

    try:
        spamassassin_result = proc.communicate(imaputils.mail_content(mail))[0]
        returncode = proc.returncode
        proc.stdin.close()
        score = utils.score_from_mail(spamassassin_result.decode(errors='ignore'))

    except Exception:  # pylint: disable=broad-except
        score = "-9999"

    return score, returncode, spamassassin_result
コード例 #2
0
def test_mail(mail, spamc=False, cmd=False):
    """Test a email with spamassassin."""
    score = "0/0\n"
    orig_code = None
    spamassassin_result = None
    returncode = None

    if cmd:
        satest = cmd
    elif spamc:
        satest = ["spamc", "-E"]
    else:
        satest = ["spamassassin", "--exit-code"]

    proc = utils.popen(satest)

    try:
        spamassassin_result = proc.communicate(imaputils.mail_content(mail))[0]
        returncode = proc.returncode
        proc.stdin.close()
        score = utils.score_from_mail(
            spamassassin_result.decode(errors='ignore'))

    except Exception:  # pylint: disable=broad-except
        score = "-9999"

    return score, returncode, spamassassin_result
コード例 #3
0
ファイル: spamproc.py プロジェクト: lrnthbrt/isbg
def learn_mail(mail, learn_type):
    """Process a email and try to learn or unlearn it.

    Args:
        mail (email.message.Message): email to learn.
        learn_type (str): ```spam``` to learn spam, ```ham``` to learn
            nonspam or ```forget```.
    Returns:
        int, int: It returns a pair of `int`

        The first integer:
            A return code of ``6`` means it was already learned or forgotten,
            a return code of ``5`` means it has been learned or forgotten,
            a ``-9999`` means an error communicating with ``spamc``. If
            ``spamc`` returns an exit code, it returns it.

        The second integer:
            It's the original exit code from ``spamc``

    Notes:
        See `Exit Codes` section of the man page of ``spamc`` for more
        information about other exit codes.

    """
    out = ""
    orig_code = None
    size = len(mail.as_bytes())
    proc = utils.popen(["spamc", "--learntype=" + learn_type, "--max-size="+str(size+1)])
    try:
        out = proc.communicate(imaputils.mail_content(mail))
        code = int(proc.returncode)
        orig_code = code
    except Exception:  # pylint: disable=broad-except
        code = -9999

    proc.stdin.close()

    if code == 0:
        out = out[0].decode(errors='ignore').strip()
        if out == __spamc_msg__['already']:
            code = 6
        elif out == __spamc_msg__['success']:
            code = 5

    return code, orig_code
コード例 #4
0
ファイル: spamproc.py プロジェクト: baldurmen/isbg
def learn_mail(mail, learn_type):
    """Process a email and try to learn or unlearn it.

    Args:
        mail (email.message.Message): email to learn.
        learn_type (str): ```spam``` to learn spam, ```ham``` to learn
            nonspam or ```forget```.
    Returns:
        int, int: It returns a pair of `int`

        The first integer:
            A return code of ``6`` means it was already learned or forgotten,
            a return code of ``5`` means it has been learned or forgotten,
            a ``-9999`` means an error communicating with ``spamc``. If
            ``spamc`` returns an exit code, it returns it.

        The second integer:
            It's the original exit code from ``spamc``

    Notes:
        See `Exit Codes` section of the man page of ``spamc`` for more
        information about other exit codes.

    """
    out = ""
    orig_code = None
    proc = utils.popen(["spamc", "--learntype=" + learn_type])
    try:
        out = proc.communicate(imaputils.mail_content(mail))
        code = int(proc.returncode)
        orig_code = code
    except Exception:  # pylint: disable=broad-except
        code = -9999

    proc.stdin.close()

    if code == 0:
        out = out[0].decode(errors='ignore').strip()
        if out == __spamc_msg__['already']:
            code = 6
        elif out == __spamc_msg__['success']:
            code = 5

    return code, orig_code
コード例 #5
0
ファイル: spamproc.py プロジェクト: lrnthbrt/isbg
def feed_mail(mail, spamc=False, cmd=False):
    """Feed a email with spamassassin report."""
    new_mail = ""
    orig_code = None

    if cmd:
        sasave = cmd
    elif spamc:
        sasave = ["spamc"]
    else:
        sasave = ["spamassassin"]

    proc = utils.popen(sasave)
    try:
        new_mail = proc.communicate(imaputils.mail_content(mail))[0]
        orig_code = proc.returncode
    except Exception:  # pylint: disable=broad-except
        new_mail = u"-9999"

    proc.stdin.close()

    return new_mail, orig_code