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-lotsaurls.go
89 lines (74 loc) · 1.53 KB
/
check-lotsaurls.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
//
// Check that there are not "too many" hyperlinks in a body.
//
package main
import (
"index/suffixarray"
"regexp"
"strconv"
"strings"
)
//
// Register ourself as a blogspam-plugin.
//
func init() {
registerPlugin(BlogspamPlugin{Name: "50-lotsaurls.js",
Description: "Look for excessive numbers of HTTP links.",
Author: "Steve Kemp <[email protected]>",
Test: checkHyperlinkCounts})
}
func checkHyperlinkCounts(x Submission) (PluginResult, string) {
//
// Map to store any options we find.
//
tmp := make(map[string]string)
//
// Default failure threshold.
//
tmp["max-links"] = "10"
//
// 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]
}
}
}
//
// Now convert our (possibly updated) max value
//
max, err := strconv.Atoi(tmp["max-links"])
if err != nil {
return Error, "Failed to parse max-links as a number"
}
if max <= 0 {
return Error, "Failed to parse max-links as a positive number"
}
//
// Look for hyperlinks
//
r := regexp.MustCompile("https?://")
//
// Get the count
//
index := suffixarray.New([]byte(x.Comment))
count := index.FindAllIndex(r, -1)
if len(count) > max {
return Spam, "Too many hyperlinks"
}
//
// All OK
//
return Undecided, ""
}