Skip to content

About

A very simple implementation of spam filtering, just a for fun project.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spamgopher

A small, SpamAssassin-inspired spam filter written in Go. You define rules in YAML — match against headers, body text, raw body, or URIs — and spamgopher scores an email based on which rules it triggers.

A very simple implementation of spam filtering, just a for-fun project.

Features

  • Rule-based scoring — define named tests with a regex and a score; matching tests add up to a final spam score.
  • Multiple test types
    • header — match against a specific header (e.g. Subject, X-Mailgun-Tag)
    • body — match against the sanitized, human-readable message body (HTML is stripped)
    • rawbody — match against the raw MIME body, before any sanitization
    • uri — match against URIs extracted from the body
    • meta — combine other tests into boolean/arithmetic expressions (&&, ||, !, +, -, *, >, <, >=, <=, =)
  • not_contains flag to score when a pattern is absent instead of present.
  • repeatable flag to score once per match instead of once per test.
  • Hidden tests — prefix a test name with __ to use it in meta expressions without it appearing (or scoring) on its own.
  • HTML/plain-text fuzzy diff — automatically compares the HTML and plain-text parts of a multipart message (via TLSH fuzzy hashing) and scores messages whose two versions diverge significantly, a common phishing/spam signal.
  • Parses real-world MIME messages via emersion/go-message.

Installation

git clone [email protected]:m50/spamgopher.git
cd spamgopher
go build -o spamgopher ./cmd

Requires Go 1.26 or later.

Usage

spamgopher currently runs as a simple example CLI that scores a single .eml file against the rules in conf.yaml, both expected in the working directory:

./spamgopher

It will print the tests that were configured, the final score, which tests matched, and how long the check took:

tests: SELF_SUBJECT=1.2, TUTA=1.2, MAILGUN_BULK=1.2, ...

Score: 3.301
Result: BodyTest=-0.1, META_TEST=2, ...
Took 2ms

To check your own message, drop it in as test.eml and adjust conf.yaml (see below).

Configuring rules

Rules live in a YAML file (conf.yaml) under a top-level tests list. Each test needs a unique name, a type, and either a regex (for header/body/rawbody/uri tests) or a meta expression (for meta tests).

tests:
  - name: TUTA
    header: Subject
    regex: "(?i)tuta"
    type: header
    score: 1.2

  - name: MAILGUN_BULK
    header: X-Mailgun-Tag
    regex: "(?i)bulk"
    type: header
    score: 1.2

  - name: SUSPICIOUS_LINK
    regex: "selfh"
    type: uri
    score: 0.5

  - name: __SUB_HAS_S
    type: header
    header: Subject
    regex: "(?i)s"
    # names starting with __ are hidden from the final results

  - name: LIKELY_SPAM
    type: meta
    meta: "MAILGUN_BULK && __SUB_HAS_S"
    score: 2

Test fields

Field Description
name Unique identifier for the test. Prefix with __ to hide it from results.
type One of header, body, rawbody, uri, meta.
regex Go regex to match against. Required for all types except meta.
header Header name to check. Required (and only used) for header tests.
meta Boolean/arithmetic expression referencing other test names. Required for meta tests.
score Points added to the total when the test matches (default 0).
not_contains If true, the test matches when the pattern is absent instead of present.
repeatable If true, the test scores once per match instead of once overall.
description Optional human-readable description.
hide Force-hide a test from results regardless of its name.

Meta expressions

Meta tests let you combine other tests' results (treated as 1/0, or a match count if repeatable) using:

  • Logical: &&, ||, !
  • Arithmetic: +, -, *
  • Comparison: >, <, >=, <=, =
- name: META_TEST
  type: meta
  meta: "BodyTest && SUB_TEST && !(Nonexistant || Nonexistant2)"
  score: 2

- name: META_TEST_2
  type: meta
  meta: "(META_TEST * 2 + SUB_TEST + !Nonexistant) >= 4"
  score: -2

Meta tests can reference other meta tests; spamgopher resolves the dependency order automatically.

Identifier rules: test names referenced in meta expressions must be valid identifiers: they must start with a letter (A-Z/a-z) or underscore (_), and may contain letters, digits, and underscores.

Using it as a library

The core logic lives in pkg/conf (config parsing/validation) and pkg/processor (running the checks), so you can embed spamgopher in your own tool:

import (
    "context"
    "os"

    "github.com/m50/spamgopher/pkg/conf"
    "github.com/m50/spamgopher/pkg/processor"
)

func scoreEmail(configPath, emailPath string) (float64, error) {
    cfFile, err := os.Open(configPath)
    if err != nil {
        return 0, err
    }
    cf, err := conf.ReadYamlConfig(cfFile)
    if err != nil {
        return 0, err
    }
    if err := cf.Validate(); err != nil {
        return 0, err
    }

    r, err := os.Open(emailPath)
    if err != nil {
        return 0, err
    }

    checks, err := processor.Check(context.Background(), cf, r)
    if err != nil {
        return 0, err
    }

    var score float64
    for _, c := range checks {
        score += c.Score
    }
    return score, nil
}

JSON config is also supported via conf.ReadJsonConfig.

Development

go test ./...

License

MIT

About

A very simple implementation of spam filtering, just a for fun project.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages