-
Notifications
You must be signed in to change notification settings - Fork 1
/
proto.go
233 lines (190 loc) · 4.59 KB
/
proto.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package idec
// Base IDEC protocol implementation
import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// IDEC Extensions. see: https://ii-net.tk/idec-doc/?p=extensions
const (
listTXT = "list.txt"
blacklistTXT = "blacklist.txt"
features = "x/features"
xcount = "x/c/"
echoSchema = "u/e/"
messageSchema = "u/m/"
)
// Extensions IDEC extensions
type Extensions struct {
ListTXT string `json:"list_txt"`
BlacklistTXT string `json:"backlist_txt"`
Features string `json:"features"`
XCount string `json:"xcount"`
}
// NewExtensions ...
func NewExtensions() Extensions {
e := Extensions{
listTXT,
blacklistTXT,
features,
xcount,
}
return e
}
// FetchConfig node, echo, and other connection settings
type FetchConfig struct {
Node string `json:"node"`
Echoes []string `json:"echo"`
Num int `json:"count"`
Offset int `json:"offset"`
Limit int `json:"limit"`
}
// ID ...
type ID struct {
Echo string `json:"echo"`
MsgID string `json:"msgids"`
}
// GetMessagesIDS get message ids from node
func (f FetchConfig) GetMessagesIDS() ([]ID, error) {
var ids []ID
var getURI string
getEchoes := strings.Join(f.Echoes, "/")
// Make strings
offset := strconv.Itoa(f.Offset)
limit := strconv.Itoa(f.Limit)
getURI = strings.Join([]string{f.Node, echoSchema, getEchoes, "/", offset, ":", limit}, "")
// Get messages ids
response, err := http.Get(getURI)
if err != nil {
return ids, err
}
defer response.Body.Close()
c, err := ioutil.ReadAll(response.Body)
if err != nil {
return ids, err
}
var i ID
var curEcho string
rawIDS := strings.Split(string(c), "\n")
for _, line := range rawIDS {
// Match echoarea
if strings.Contains(line, ".") {
curEcho = line
continue
}
// Match message ID
if !strings.Contains(line, ".") && !strings.Contains(line, ":") && line != "" {
i.Echo = curEcho
i.MsgID = line
ids = append(ids, i)
}
}
return ids, nil
}
// GetAllMessagesIDS get all message ids from node
func (f FetchConfig) GetAllMessagesIDS() ([]ID, error) {
var ids []ID
var getURI string
getEchoes := strings.Join(f.Echoes, "/")
getURI = strings.Join([]string{f.Node, echoSchema, getEchoes}, "")
// Get messages ids
response, err := http.Get(getURI)
if err != nil {
return ids, err
}
defer response.Body.Close()
c, err := ioutil.ReadAll(response.Body)
if err != nil {
return ids, err
}
var i ID
var curEcho string
rawIDS := strings.Split(string(c), "\n")
for _, line := range rawIDS {
// Match echoarea
if strings.Contains(line, ".") {
curEcho = line
continue
}
// Match message ID
if !strings.Contains(line, ".") && !strings.Contains(line, ":") && line != "" {
i.Echo = curEcho
i.MsgID = line
ids = append(ids, i)
}
}
return ids, nil
}
// MSG ...
type MSG struct {
Message string `json:"message"`
ID string `json:"id"`
}
// GetRawMessages get messages from node
func (f FetchConfig) GetRawMessages(ids []ID) ([]MSG, error) {
var messages []MSG
var messagesIDS []string
for _, id := range ids {
messagesIDS = append(messagesIDS, id.MsgID)
}
getMessages := strings.Join(messagesIDS, "/")
getURI := strings.Join([]string{f.Node, messageSchema, getMessages}, "")
// Get messages ids
response, err := http.Get(getURI)
if err != nil {
e := errors.New(strings.Join([]string{"Failed to get", getURI, "."}, " "))
return messages, e
}
defer response.Body.Close()
c, err := ioutil.ReadAll(response.Body)
if err != nil {
return messages, err
}
for _, m := range strings.Split(string(c), "\n") {
if len(m) == 0 {
break
}
message := strings.Split(m, ":")
if len(message) > 1 {
messages = append(messages, MSG{message[1], message[0]})
}
}
return messages, err
}
// Echo echo description
type Echo struct {
Name string `json:"name"`
Size int `json:"size"`
Description string `json:"description"`
}
// GetEchoList ...
func (f FetchConfig) GetEchoList() ([]Echo, error) {
var echoes []Echo
// Check node features support
fres, err := http.Get(strings.Join([]string{f.Node, features}, "/"))
if err != nil {
return echoes, err
}
defer fres.Body.Close()
c, err := ioutil.ReadAll(fres.Body)
if err != nil {
return echoes, err
}
if !strings.Contains(string(c), listTXT) {
err = errors.New("Node does not support echoes list")
return echoes, err
}
lres, err := http.Get(strings.Join([]string{f.Node, listTXT}, "/"))
if err != nil {
return echoes, err
}
defer lres.Body.Close()
l, err := ioutil.ReadAll(lres.Body)
if err != nil {
return echoes, err
}
echoes, err = ParseEchoList(string(l))
return echoes, err
}