-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add optional SpamAssassin integration to display scores (#233)
- Loading branch information
Showing
15 changed files
with
1,013 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Package postmark uses the free https://spamcheck.postmarkapp.com/ | ||
// See https://spamcheck.postmarkapp.com/doc/ for more details. | ||
package postmark | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Response struct | ||
type Response struct { | ||
Success bool `json:"success"` | ||
Message string `json:"message"` // for errors only | ||
Score string `json:"score"` | ||
Rules []Rule `json:"rules"` | ||
Report string `json:"report"` // ignored | ||
} | ||
|
||
// Rule struct | ||
type Rule struct { | ||
Score string `json:"score"` | ||
// Name not returned by postmark but rather extracted from description | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
} | ||
|
||
// Check will post the email data to Postmark | ||
func Check(email []byte, timeout int) (Response, error) { | ||
r := Response{} | ||
// '{"email":"raw dump of email", "options":"short"}' | ||
var d struct { | ||
// The raw dump of the email to be filtered, including all headers. | ||
Email string `json:"email"` | ||
// Default "long". Must either be "long" for a full report of processing rules, or "short" for a score request. | ||
Options string `json:"options"` | ||
} | ||
|
||
d.Email = string(email) | ||
d.Options = "long" | ||
|
||
data, err := json.Marshal(d) | ||
if err != nil { | ||
return r, err | ||
} | ||
|
||
client := http.Client{ | ||
Timeout: time.Duration(timeout) * time.Second, | ||
} | ||
|
||
resp, err := client.Post("https://spamcheck.postmarkapp.com/filter", "application/json", | ||
bytes.NewBuffer(data)) | ||
|
||
if err != nil { | ||
return r, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&r) | ||
|
||
// remove trailing line spaces for all lines in report | ||
re := regexp.MustCompile("\r?\n") | ||
lines := re.Split(r.Report, -1) | ||
reportLines := []string{} | ||
for _, l := range lines { | ||
line := strings.TrimRight(l, " ") | ||
reportLines = append(reportLines, line) | ||
} | ||
reportRaw := strings.Join(reportLines, "\n") | ||
|
||
// join description lines to make a single line per rule | ||
re2 := regexp.MustCompile("\n ") | ||
report := re2.ReplaceAllString(reportRaw, "") | ||
for i, rule := range r.Rules { | ||
// populate rule name | ||
r.Rules[i].Name = nameFromReport(rule.Score, rule.Description, report) | ||
} | ||
|
||
return r, err | ||
} | ||
|
||
// Extract the name of the test from the report as Postmark does not include this in the JSON reports | ||
func nameFromReport(score, description, report string) string { | ||
score = regexp.QuoteMeta(score) | ||
description = regexp.QuoteMeta(description) | ||
str := fmt.Sprintf("%s\\s+([A-Z0-9\\_]+)\\s+%s", score, description) | ||
re := regexp.MustCompile(str) | ||
|
||
matches := re.FindAllStringSubmatch(report, 1) | ||
if len(matches) > 0 && len(matches[0]) == 2 { | ||
return strings.TrimSpace(matches[0][1]) | ||
} | ||
|
||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Package spamassassin will return results from either a SpamAssassin server or | ||
// Postmark's public API depending on configuration | ||
package spamassassin | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/axllent/mailpit/internal/spamassassin/postmark" | ||
"github.com/axllent/mailpit/internal/spamassassin/spamc" | ||
) | ||
|
||
var ( | ||
// Service to use, either "<host>:<ip>" for self-hosted SpamAssassin or "postmark" | ||
service string | ||
|
||
// SpamScore is the score at which a message is determined to be spam | ||
spamScore = 5.0 | ||
|
||
// Timeout in seconds | ||
timeout = 8 | ||
) | ||
|
||
// Result is a SpamAssassin result | ||
// | ||
// swagger:model SpamAssassinResponse | ||
type Result struct { | ||
// Whether the message is spam or not | ||
IsSpam bool | ||
// If populated will return an error string | ||
Error string | ||
// Total spam score based on triggered rules | ||
Score float64 | ||
// Spam rules triggered | ||
Rules []Rule | ||
} | ||
|
||
// Rule struct | ||
type Rule struct { | ||
// Spam rule score | ||
Score float64 | ||
// SpamAssassin rule name | ||
Name string | ||
// SpamAssassin rule description | ||
Description string | ||
} | ||
|
||
// SetService defines which service should be used. | ||
func SetService(s string) { | ||
switch s { | ||
case "postmark": | ||
service = "postmark" | ||
default: | ||
service = s | ||
} | ||
} | ||
|
||
// SetTimeout defines the timeout | ||
func SetTimeout(t int) { | ||
if t > 0 { | ||
timeout = t | ||
} | ||
} | ||
|
||
// Ping returns whether a service is active or not | ||
func Ping() error { | ||
if service == "postmark" { | ||
return nil | ||
} | ||
|
||
var client *spamc.Client | ||
if strings.HasPrefix("unix:", service) { | ||
client = spamc.NewUnix(strings.TrimLeft(service, "unix:")) | ||
} else { | ||
client = spamc.NewTCP(service, timeout) | ||
} | ||
|
||
return client.Ping() | ||
} | ||
|
||
// Check will return a Result | ||
func Check(msg []byte) (Result, error) { | ||
r := Result{Score: 0} | ||
|
||
if service == "" { | ||
return r, errors.New("no SpamAssassin service defined") | ||
} | ||
|
||
if service == "postmark" { | ||
res, err := postmark.Check(msg, timeout) | ||
if err != nil { | ||
r.Error = err.Error() | ||
return r, nil | ||
} | ||
resFloat, err := strconv.ParseFloat(res.Score, 32) | ||
if err == nil { | ||
r.Score = round1dm(resFloat) | ||
r.IsSpam = resFloat >= spamScore | ||
} | ||
r.Error = res.Message | ||
for _, pr := range res.Rules { | ||
rule := Rule{} | ||
value, err := strconv.ParseFloat(pr.Score, 32) | ||
if err == nil { | ||
rule.Score = round1dm(value) | ||
} | ||
rule.Name = pr.Name | ||
rule.Description = pr.Description | ||
r.Rules = append(r.Rules, rule) | ||
} | ||
} else { | ||
var client *spamc.Client | ||
if strings.HasPrefix("unix:", service) { | ||
client = spamc.NewUnix(strings.TrimLeft(service, "unix:")) | ||
} else { | ||
client = spamc.NewTCP(service, timeout) | ||
} | ||
|
||
res, err := client.Report(msg) | ||
if err != nil { | ||
r.Error = err.Error() | ||
return r, nil | ||
} | ||
r.IsSpam = res.Score >= spamScore | ||
r.Score = round1dm(res.Score) | ||
r.Rules = []Rule{} | ||
for _, sr := range res.Rules { | ||
rule := Rule{} | ||
value, err := strconv.ParseFloat(sr.Points, 32) | ||
if err == nil { | ||
rule.Score = round1dm(value) | ||
} | ||
rule.Name = sr.Name | ||
rule.Description = sr.Description | ||
r.Rules = append(r.Rules, rule) | ||
} | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
// Round to one decimal place | ||
func round1dm(n float64) float64 { | ||
return math.Floor(n*10) / 10 | ||
} |
Oops, something went wrong.