spam

package
v0.167.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spam is Caprock Mail's local junk filter: no AI, no network, no account anywhere. It reads what the receiving server already wrote into a message's header (SPF, DKIM, DMARC and its own spam verdict), a handful of cheap rules about the sender and the body, and a small naive-Bayes classifier that learns from the user's own Junk folder and from every Spam / Not spam the user presses. The verdict only hides a message from the inbox; nothing is moved or deleted on the server by it.

The package is pure: it parses, scores and tokenizes. The token counts and the verdicts live in mail.db (internal/mail/mailstore), and the engine (internal/mail/engine) runs it at sync time.

Index

Constants

View Source
const (
	// MinTrained is how many messages of each kind the classifier must have
	// seen before its opinion counts at all.
	MinTrained = 10
	// MaxTokens per message: the first ones (subject, sender, then the body)
	// are kept.
	MaxTokens = 400
)

Learning limits.

View Source
const (
	BayesScale  = 11.0
	MaxBayes    = 5.0
	MaxBayesHam = 4.0
)

Bayes contributes (p - 0.5) * BayesScale points, clamped to [-MaxBayesHam, MaxBayes]: a confident classifier (p ≥ 0.96) is enough on its own, an unsure one only tips a close call.

View Source
const Threshold = 5.0

Threshold is the number of points at which a message is junk. Every rule below adds or takes points; the classifier adds at most MaxBayes.

Variables

This section is empty.

Functions

func Probability

func Probability(tokens []string, counts map[string]Counts, nSpam, nHam int) (p float64, ok bool)

Probability is the classifier's spam probability for a message's tokens, given each token's counts and the number of spam and ham messages learned. ok is false while either side has fewer than MinTrained messages.

func Tokens

func Tokens(h Header, b Body) []string

Tokens turns a message into the set of features the classifier counts: words of the subject and body, the sender's name words and domain, link hosts, and the header facts (an auth failure is a feature too). Each appears once: the classifier counts messages, not repetitions.

Types

type Body

type Body struct {
	Text string
	HTML string
}

Body is what the rules read from a message's decoded text and HTML.

type Context

type Context struct {
	// Known: the user wrote to this address, said "Not spam" for it, the
	// conversation includes the user's own reply, or it is the user's own
	// address. A known sender is never junk.
	Known bool
	// Answered or Flagged by the user: the user dealt with it as real mail.
	Answered, Flagged bool
}

Context is what the engine knows about the sender beyond the message.

type Counts

type Counts struct{ Spam, Ham int }

Counts is how many spam and ham messages a token appeared in.

type Header struct {
	// Auth results from the receiving server's Authentication-Results:
	// "pass", "fail", "softfail", "neutral", "none", "temperror",
	// "permerror", or "" when the header does not say.
	SPF   string `json:"spf,omitempty"`
	DKIM  string `json:"dkim,omitempty"`
	DMARC string `json:"dmarc,omitempty"`
	// AuthSeen: the server wrote an Authentication-Results at all. Without
	// one, missing results say nothing.
	AuthSeen bool `json:"auth,omitempty"`

	// ProviderSpam: the receiving server's own filter said spam
	// (X-Spam-Flag: YES, X-Spam-Status: Yes, an rspamd action, SCL ≥ 5,
	// a [SPAM] subject tag).
	ProviderSpam bool `json:"pspam,omitempty"`
	// ProviderScore is its numeric score when it gave one, and
	// ProviderLimit the threshold it uses (0 when unknown).
	ProviderScore float64 `json:"pscore,omitempty"`
	ProviderLimit float64 `json:"plimit,omitempty"`
	HasScore      bool    `json:"hscore,omitempty"`

	Bulk            bool   `json:"bulk,omitempty"`  // Precedence: bulk/list/junk
	ListUnsubscribe bool   `json:"unsub,omitempty"` // List-Unsubscribe present
	InReplyTo       bool   `json:"irt,omitempty"`   // In-Reply-To or References present
	NoMessageID     bool   `json:"nomid,omitempty"`
	FromName        string `json:"fname,omitempty"`
	FromAddr        string `json:"faddr,omitempty"`
	ReplyTo         string `json:"rto,omitempty"`
	Subject         string `json:"subj,omitempty"`
}

Header is what the filter keeps from a message's header. It is stored as JSON beside the message, so a verdict can be computed again (when the classifier has learned more) without asking the server for the header.

func ParseHeader

func ParseHeader(raw []byte) Header

ParseHeader reads the filter's facts from a raw RFC 5322 header.

type Hit

type Hit struct {
	Code   string  `json:"code"`
	Points float64 `json:"points"`
}

Hit is one rule that fired: a short code (stable, for the UI and tests) and its points.

func Rules

func Rules(h Header, b Body) (float64, []Hit)

Rules scores a message from its header and body. Positive is spammy.

type Verdict

type Verdict struct {
	Junk  bool    `json:"junk"`
	Score int     `json:"score"` // 0–100; 50 is the threshold
	Hits  []Hit   `json:"hits"`
	Total float64 `json:"total"`
}

Verdict is the filter's answer for one message.

func Decide

func Decide(rulePts float64, hits []Hit, bayes float64, ready bool, c Context) Verdict

Decide combines the rules with the classifier. bayes is the classifier's spam probability and ready says whether it has learned enough to be asked.

func Judge

func Judge(h Header, b Body, tokens []string, counts map[string]Counts, nSpam, nHam int, c Context) Verdict

Judge is the whole verdict: the rules, the classifier on tokens (as returned by Tokens, with their counts), and what is known of the sender.

func (Verdict) Why

func (v Verdict) Why() string

Why is the comma-separated codes of the hits that pushed towards junk, strongest first, for the junk_why column.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL