forked from adtac/commento
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spam_filter.go
46 lines (38 loc) · 968 Bytes
/
spam_filter.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
package main
import (
"github.com/adtac/go-akismet/akismet"
"net/http"
"os"
"regexp"
)
func checkSpam(r *http.Request, url string, name string, comment string) bool {
akismetKey := os.Getenv("AKISMET_KEY")
if akismetKey == "" {
return false
}
IP := r.RemoteAddr
if r.Header.Get("X-Forwarded-For") != "" {
IP = r.Header.Get("X-Forwarded-For")
}
exp := regexp.MustCompile(`^(?P<Blog>(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?(?:[^:\/\n]+))`)
match := exp.FindStringSubmatch(url)
var blog string
for i, name := range exp.SubexpNames() {
if name == "Blog" {
blog = match[i]
}
}
isSpam, err := akismet.Check(&akismet.Comment{
Blog: blog,
UserIP: IP,
UserAgent: r.Header.Get("User-Agent"),
CommentType: "comment",
CommentAuthor: name,
CommentContent: comment,
}, akismetKey)
if err != nil {
Logger.Errorf("error: cannot validate commenet using Akismet: %v", err)
return true
}
return isSpam
}