-
Notifications
You must be signed in to change notification settings - Fork 11
/
process.go
99 lines (78 loc) · 1.68 KB
/
process.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
98
99
package main
import (
"encoding/json"
"log"
"strings"
)
type SaveItem struct {
Location string
Content string
}
func (s *SaveItem) Json() []byte {
data, err := json.Marshal(s)
if err != nil {
log.Printf("[-] Could not create JSON for %s", s.Location)
return nil
}
return data
}
type ProcessItem struct {
Source string
Key string
Location string
Content string
Save bool
}
func (p *ProcessItem) Write() {
if (conf.Save == false) || (p.Save == false) {
return
}
if len(p.Content) > conf.MaxSize {
return
}
s := SaveItem{Location: p.Location, Content: p.Content}
db.Write("pastes", p.Key, s.Json())
}
func (p *ProcessItem) Regexes() {
for i := range conf.Regexes {
r := conf.Regexes[i]
switch r.Match {
case "all":
items := r.compiled.FindAllString(p.Content, -1)
if items != nil {
p.Save = true
data := strings.Join(items, "\n")
s := SaveItem{Location: p.Location, Content: data}
db.Write(r.Bucket, p.Key, s.Json())
}
// for k := range items {
// rKey := fmt.Sprintf("%s-%d", p.Source, k)
// db.Write(r.Bucket, rKey, []byte(items[k]))
// }
case "one":
match := r.compiled.FindString(p.Content)
if match != "" {
p.Save = true
s := SaveItem{Location: p.Location, Content: match}
db.Write(r.Bucket, p.Key, s.Json())
}
default:
}
}
}
func (p *ProcessItem) Keywords() {
for i, _ := range conf.Keywords {
kwd := conf.Keywords[i]
if strings.Contains(strings.ToLower(p.Content), strings.ToLower(kwd.Keyword)) {
p.Save = true
db.Write(kwd.Bucket, p.Location, nil)
}
}
}
func process(sem chan struct{}, pi *ProcessItem) {
sem <- struct{}{}
pi.Regexes()
pi.Keywords()
pi.Write()
<-sem
}