forked from Difrex/go-idec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
209 lines (183 loc) · 4.46 KB
/
parser.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package idec
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"net/url"
"errors"
)
// ParseMessage ...
func ParseMessage(message string) (Message, error) {
var m Message
plainMessage, err := base64.StdEncoding.DecodeString(message)
if err != nil {
return m, err
}
txtMessage := strings.Split(string(plainMessage), "\n")
var body string
for i := 8; i < len(txtMessage); i++ {
body = strings.Join([]string{body, txtMessage[i]}, "\n")
}
ts, err := strconv.Atoi(txtMessage[2])
if err != nil {
return m, err
}
tags, err := ParseTags(txtMessage[0])
m.Tags = tags
m.Echo = txtMessage[1]
m.Timestamp = ts
m.From = txtMessage[3]
m.Address = txtMessage[4]
m.To = txtMessage[5]
m.Subg = txtMessage[6]
m.Body = body
return m, err
}
// ParsePointMessage ...
func ParsePointMessage(message string) (*PointMessage, error) {
var pointMessage *PointMessage
// Unescape message
unsafe, err := url.QueryUnescape(message)
if err != nil {
return pointMessage, err
}
plainMessage, err := base64.StdEncoding.DecodeString(unsafe)
if err != nil {
return pointMessage, err
}
txtMessage := strings.Split(string(plainMessage), "\n")
if len(txtMessage) < 6 {
e := errors.New("Bad message")
return pointMessage, e
}
var body string
for i := 5; i < len(txtMessage); i++ {
body = strings.Join([]string{body, txtMessage[i]}, "\n")
}
pointMessage = &PointMessage{
Echo: strings.Trim(txtMessage[0], " "),
To: txtMessage[1],
Subg: txtMessage[2],
EmptyLine: txtMessage[3],
Body: body,
}
if !strings.Contains(txtMessage[4], "@repto:") {
pointMessage.Body = txtMessage[4] + "\n" + pointMessage.Body
pointMessage.Repto = ""
} else {
pointMessage.Repto = ParseReptoField(txtMessage[4])
}
return pointMessage, nil
}
// Validate point message
// Returns error if one of the message fields is invalid
// Also, attach repto into body is it does not
func (p *PointMessage) Validate() error {
var err error
if p.Echo == "" {
err = errors.New("`Echo' field is empty")
}
if !strings.Contains(p.Echo, ".") {
err = errors.New("Wrong Echo name")
}
if p.To == "" {
err = errors.New("`To' field is empty")
}
if p.Subg == "" {
err = errors.New("`Subg' field is empty")
}
if p.EmptyLine != "" {
err = errors.New("EmptyLine is not empty")
}
if p.Body == "" {
err = errors.New("`Body' field is empty")
}
if p.Repto != "" && len(p.Repto) != 20 {
err = errors.New("Wrong @repto field length")
}
return err
}
// ParseReptoField @repto:MSGID, drops @repto prefix
// and return raw MSGID
func ParseReptoField(repto string) string {
r := strings.Trim(strings.Split(repto, "@repto:")[1], " ")
return r
}
// MakeBundledMessage from point message.
// Returns Message with empty From and Address fields
// you must set this somewhere outside
func MakeBundledMessage(pointMessage *PointMessage) (Message, error) {
var msg Message
t := "ii/ok"
if pointMessage.Repto != "" {
t = fmt.Sprintf("%s/repto/%s", t, pointMessage.Repto)
}
tags, err := ParseTags(t)
if err != nil {
return msg, err
}
msg = Message{
Tags: tags,
Echo: pointMessage.Echo,
Timestamp: int(time.Now().Unix()),
To: pointMessage.To,
Subg: pointMessage.Subg,
Repto: pointMessage.Repto,
Body: pointMessage.Body,
}
return msg, nil
}
// parseTags parse message tags and return Tags struct
func ParseTags(tags string) (Tags, error) {
var t Tags
if !strings.Contains(tags, "ii/") {
e := errors.New("Bad tagstring")
return t, e
}
tagsSlice := strings.Split(tags, "/")
if len(tagsSlice) < 4 {
t.II = tagsSlice[1]
return t, nil
}
t.II = tagsSlice[1]
t.Repto = tagsSlice[3]
return t, nil
}
// ParseEchoList parse /list.txt
func ParseEchoList(list string) ([]Echo, error) {
var echoes []Echo
for _, e := range strings.Split(list, "\n") {
desc := strings.Split(e, ":")
if len(desc) <= 1 {
break
}
count, err := strconv.Atoi(desc[1])
if err != nil {
return echoes, err
}
echoes = append(echoes, Echo{desc[0], count, desc[2]})
}
return echoes, nil
}
// MakeMsgID from provided plain bundled message
func MakeMsgID(msg string) string {
sum := sha256.Sum256([]byte(msg))
id := base64.StdEncoding.EncodeToString(sum[:])
id = strings.Replace(id, "+", "A", -1)
id = strings.Replace(id, "/", "Z", -1)
return id[:20]
}
// String from PointMessage
func (p *PointMessage) String() string {
return strings.Join([]string{
p.Echo,
p.To,
p.Subg,
"",
"@repto:" + p.Repto,
p.Body,
}, "\n")
}