forked from bakape/shamichan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
body.go
73 lines (65 loc) · 1.27 KB
/
body.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
// Package parser parses and verifies user-sent post data
package parser
import (
"meguca/common"
"meguca/util"
"regexp"
)
var linkRegexp = regexp.MustCompile(`^>{2,}(\d+)$`)
// Needed to avoid cyclic imports for the 'db' package
func init() {
common.ParseBody = ParseBody
}
// ParseBody parses the entire post text body for commands and links
func ParseBody(body []byte, board string) (
links [][2]uint64, com []common.Command, err error,
) {
start := 0
for i, b := range body {
switch b {
case '\n', ' ', '\t':
default:
if i == len(body)-1 {
i++
} else {
continue
}
}
_, word, _ := util.SplitPunctuation(body[start:i])
start = i + 1
if len(word) == 0 {
continue
}
switch word[0] {
case '>':
m := linkRegexp.FindSubmatch(word)
if m == nil {
continue
}
var l [2]uint64
l, err = parseLink(m)
switch {
case err != nil:
return
case l[0] != 0:
links = append(links, l)
}
case '#':
m := common.CommandRegexp.FindSubmatch(word)
if m == nil {
continue
}
var c common.Command
c, err = parseCommand(m[1], board)
switch err {
case nil:
com = append(com, c)
case errTooManyRolls, errDieTooBig: // Consider command invalid
err = nil
default:
return
}
}
}
return
}