This repository has been archived by the owner on Jan 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck-size.go
97 lines (82 loc) · 1.83 KB
/
check-size.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Check for min/max size of comment.
//
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
//
// Register ourself as a blogspam-plugin.
//
func init() {
registerPlugin(BlogspamPlugin{Name: "40-size.js",
Description: "Look at the size of the body",
Author: "Steve Kemp <steve@steve.org.uk>",
Test: validateSize})
}
//
// If there are options which specify the min/max-size of the body, then
// test them.
//
func validateSize(x Submission) (PluginResult, string) {
//
// Map to store any options we find.
//
tmp := make(map[string]string)
//
// Do we have options?
//
if len(x.Options) > 0 {
//
// Split the string into an array, based on commas
//
options := strings.Split(x.Options, ",")
//
// Now look for key=val
//
for _, option := range options {
re := regexp.MustCompile("^(.*)=([^=]+)$")
match := re.FindStringSubmatch(option)
if len(match) > 0 {
tmp[match[1]] = match[2]
}
}
}
//
// Do we have a min-size?
//
if len(tmp["min-size"]) > 0 {
i, err := strconv.Atoi(tmp["min-size"])
if err != nil {
return Error, "Failed to parse max-size as a number"
}
if i <= 0 {
return Error, "Failed to parse max-size as a positive number"
}
if len(x.Comment) < i {
return Spam, fmt.Sprintf("Comment size is %d which is less than the minimum size %s", len(x.Comment), tmp["min-size"])
}
}
//
// Do we have a max-size?
//
if len(tmp["max-size"]) > 0 {
i, err := strconv.Atoi(tmp["max-size"])
if err != nil {
return Error, "Failed to parse max-size as a number"
}
if i <= 0 {
return Error, "Failed to parse max-size as a positive number"
}
if len(x.Comment) > i {
return Spam, fmt.Sprintf("Comment size is %d which is more than the maximum size %s", len(x.Comment), tmp["min-size"])
}
}
//
// All OK
//
return Undecided, ""
}