Skip to content

fix issue with report dealing newlines, also turned lib to go module #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/teamwork/spamc

go 1.16

require (
github.com/pkg/errors v0.9.1
github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad
github.com/teamwork/utils v0.0.0-20210422143242-99315371ead6
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/Strum355/go-difflib v1.1.0 h1:+rR2X3UuvIbe1Jmhx8WA7gkgjMNRscFWbHchk2RB8I4=
github.com/Strum355/go-difflib v1.1.0/go.mod h1:r1cVg1JkGsTWkaR7At56v7hfuMgiUL8meTLwxFzOmvE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/teamwork/test v0.0.0-20190410143529-8897d82f8d46/go.mod h1:TIbx7tx6WHBjQeLRM4eWQZBL7kmBZ7/KI4x4v7Y5YmA=
github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad h1:25sEr0awm0ZPancg5W5H5VvN7PWsJloUBpii10a9isw=
github.com/teamwork/test v0.0.0-20200108114543-02621bae84ad/go.mod h1:TIbx7tx6WHBjQeLRM4eWQZBL7kmBZ7/KI4x4v7Y5YmA=
github.com/teamwork/utils v0.0.0-20210422143242-99315371ead6 h1:l7CEV+qJRWPFhPu7ZVI6uantfRGFrBE47lEhgn+wtrs=
github.com/teamwork/utils v0.0.0-20210422143242-99315371ead6/go.mod h1:3Fn0qxFeRNpvsg/9T1+btOOOKkd1qG2nPYKKcOmNpcs=
39 changes: 20 additions & 19 deletions spamc.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,27 @@ func parseReport(tp *textproto.Reader) (Report, error) {

case table:
s := reTableLine.FindAllStringSubmatch(line, -1)
if len(s) != 1 {
continue
if len(s) != 0 {
points, err := strconv.ParseFloat(s[0][1], 64)
if err != nil {
continue
}

report.Table = append(report.Table, struct {
Points float64
Rule string
Description string
}{
points, s[0][2], s[0][3],
})
} else {

last := len(report.Table) - 1
if last >= 0 {
line = strings.TrimSpace(line)
report.Table[last].Description += "\n" + strings.Repeat(" ", 28) + strings.TrimSpace(line)
}
}

if len(s[0]) != 4 {
continue
}

points, err := strconv.ParseFloat(s[0][1], 64)
if err != nil {
continue
}

report.Table = append(report.Table, struct {
Points float64
Rule string
Description string
}{
points, s[0][2], s[0][3],
})
}
}

Expand Down
92 changes: 92 additions & 0 deletions spamc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,98 @@ func TestParseSpamHeader(t *testing.T) {
}
}

func TestParseReportWithNewlines(t *testing.T) {
cases := []struct {
in string
want Report
}{
{
normalizeSpace(`
Spam detection software, running on the system "d311d8df23f8",
has NOT identified this incoming email as spam.

Content preview: the body [...]

Content analysis details: (4.4 points, 5.0 required)

pts rule name description
---- ---------------------- --------------------------------------------------
-0.0 NO_RELAYS Informational: message was not relayed via SMTP
0.0 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail are different
1.9 URIBL_ABUSE_SURBL Contains an URL listed in the ABUSE SURBL blocklist
[URIs: blacklistedurl.com]
2.5 URIBL_DBL_SPAM Contains a spam URL listed in the Spamhaus DBL
blocklist
[URIs: blablablabla.com]
`),
Report{
Intro: normalizeSpace(`
Spam detection software, running on the system "d311d8df23f8",
has NOT identified this incoming email as spam.

Content preview: the body [...]

Content analysis details: (4.4 points, 5.0 required)
`),
Table: []struct {
Points float64
Rule string
Description string
}{
{
Points: -0.0,
Rule: "NO_RELAYS",
Description: "Informational: message was not relayed via SMTP",
},
{
Points: 0.0,
Rule: "HEADER_FROM_DIFFERENT_DOMAINS",
Description: "From and EnvelopeFrom 2nd level mail are different",
},
{
Points: 1.9,
Rule: "URIBL_ABUSE_SURBL",
Description: "Contains an URL listed in the ABUSE SURBL blocklist\n [URIs: blacklistedurl.com]",
},
{
Points: 2.5,
Rule: "URIBL_DBL_SPAM",
Description: "Contains a spam URL listed in the Spamhaus DBL\n blocklist\n [URIs: blablablabla.com]",
},
},
},
},
}

for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
tp := textproto.NewReader(bufio.NewReader(strings.NewReader(tc.in)))

out, err := parseReport(tp)
if err != nil {
t.Fatal(err)
}

if d := diff.TextDiff(tc.want.Intro, out.Intro); d != "" {
t.Errorf("intro wrong\n%v", d)
}

if !reflect.DeepEqual(out.Table, tc.want.Table) {
t.Errorf("wrong table\nout: %#v\nwant: %#v\n",
out.Table, tc.want.Table)
}

if !t.Failed() {
tc.in += "\n"
s := out.String()
if d := diff.TextDiff(s, tc.in); d != "" {
t.Errorf("String() not the same\n%v", d)
}
}
})
}
}

func TestParseReport(t *testing.T) {
cases := []struct {
in string
Expand Down