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 ¶
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.
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.
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 ¶
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.
Types ¶
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 ¶
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 ¶
ParseHeader reads the filter's facts from a raw RFC 5322 header.
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 ¶
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.