-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
108 lines (90 loc) · 2.18 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
package idec
import (
"encoding/base64"
"errors"
"strconv"
"strings"
)
// 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
plainMessage, err := base64.StdEncoding.DecodeString(message)
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.Echo = txtMessage[0]
pointMessage.To = txtMessage[1]
pointMessage.Subg = txtMessage[2]
pointMessage.EmptyLine = txtMessage[3]
pointMessage.Repto = txtMessage[4]
pointMessage.Body = body
return pointMessage, 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
}