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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
package hotline
import (
"cmp"
"encoding/binary"
"github.com/stretchr/testify/mock"
"io"
"slices"
)
var (
NewsBundle = [2]byte{0, 2}
NewsCategory = [2]byte{0, 3}
)
type ThreadedNewsMgr interface {
ListArticles(newsPath []string) (NewsArtListData, error)
GetArticle(newsPath []string, articleID uint32) *NewsArtData
DeleteArticle(newsPath []string, articleID uint32, recursive bool) error
PostArticle(newsPath []string, parentArticleID uint32, article NewsArtData) error
CreateGrouping(newsPath []string, name string, t [2]byte) error
GetCategories(paths []string) []NewsCategoryListData15
NewsItem(newsPath []string) NewsCategoryListData15
DeleteNewsItem(newsPath []string) error
}
// ThreadedNews contains the top level of threaded news categories, bundles, and articles.
type ThreadedNews struct {
Categories map[string]NewsCategoryListData15 `yaml:"Categories"`
}
type NewsCategoryListData15 struct {
Type [2]byte `yaml:"Type,flow"` // Bundle (2) or category (3)
Name string `yaml:"Name"`
Articles map[uint32]*NewsArtData `yaml:"Articles"` // Optional, if Type is Category
SubCats map[string]NewsCategoryListData15 `yaml:"SubCats"`
GUID [16]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
AddSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
DeleteSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
readOffset int // Internal offset to track read progress
}
func (newscat *NewsCategoryListData15) GetNewsArtListData() (NewsArtListData, error) {
var newsArts []NewsArtList
var newsArtsPayload []byte
for i, art := range newscat.Articles {
id := make([]byte, 4)
binary.BigEndian.PutUint32(id, i) // The article's map key in the Articles map is its ID.
newsArts = append(newsArts, NewsArtList{
ID: [4]byte(id),
TimeStamp: art.Date,
ParentID: art.ParentArt,
Title: []byte(art.Title),
Poster: []byte(art.Poster),
ArticleSize: art.DataSize(),
})
}
// Sort the articles by ID. This is important for displaying the message threading correctly on the client side.
slices.SortFunc(newsArts, func(a, b NewsArtList) int {
return cmp.Compare(
binary.BigEndian.Uint32(a.ID[:]),
binary.BigEndian.Uint32(b.ID[:]),
)
})
for _, v := range newsArts {
b, err := io.ReadAll(&v)
if err != nil {
return NewsArtListData{}, err
}
newsArtsPayload = append(newsArtsPayload, b...)
}
return NewsArtListData{
Count: len(newsArts),
Name: []byte{},
Description: []byte{},
NewsArtList: newsArtsPayload,
}, nil
}
// NewsArtData represents an individual news article.
type NewsArtData struct {
Title string `yaml:"Title"`
Poster string `yaml:"Poster"`
Date [8]byte `yaml:"Date,flow"`
PrevArt [4]byte `yaml:"PrevArt,flow"`
NextArt [4]byte `yaml:"NextArt,flow"`
ParentArt [4]byte `yaml:"ParentArt,flow"`
FirstChildArt [4]byte `yaml:"FirstChildArtArt,flow"`
DataFlav []byte `yaml:"-"` // MIME type string. Always "text/plain".
Data string `yaml:"Data"`
}
func (art *NewsArtData) DataSize() [2]byte {
dataLen := make([]byte, 2)
binary.BigEndian.PutUint16(dataLen, uint16(len(art.Data)))
return [2]byte(dataLen)
}
type NewsArtListData struct {
ID [4]byte `yaml:"Type"`
Name []byte `yaml:"Name"`
Description []byte `yaml:"Description"` // not used?
NewsArtList []byte // List of articles Optional (if article count > 0)
Count int
readOffset int // Internal offset to track read progress
}
func (nald *NewsArtListData) Read(p []byte) (int, error) {
count := make([]byte, 4)
binary.BigEndian.PutUint32(count, uint32(nald.Count))
buf := slices.Concat(
nald.ID[:],
count,
[]byte{uint8(len(nald.Name))},
nald.Name,
[]byte{uint8(len(nald.Description))},
nald.Description,
nald.NewsArtList,
)
if nald.readOffset >= len(buf) {
return 0, io.EOF // All bytes have been read
}
n := copy(p, buf[nald.readOffset:])
nald.readOffset += n
return n, nil
}
// NewsArtList is a summarized version of a NewArtData record for display in list view
type NewsArtList struct {
ID [4]byte
TimeStamp [8]byte // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
ParentID [4]byte
Flags [4]byte
FlavorCount [2]byte
// Title size 1
Title []byte // string
// Poster size 1
// Poster Poster string
Poster []byte
FlavorList []NewsFlavorList
// Flavor list… Optional (if flavor count > 0)
ArticleSize [2]byte // Size 2
readOffset int // Internal offset to track read progress
}
var (
NewsFlavor = []byte("text/plain") // NewsFlavor is always "text/plain"
NewsFlavorCount = []byte{0, 1} // NewsFlavorCount is always 1
)
func (nal *NewsArtList) Read(p []byte) (int, error) {
out := slices.Concat(
nal.ID[:],
nal.TimeStamp[:],
nal.ParentID[:],
nal.Flags[:],
NewsFlavorCount,
[]byte{uint8(len(nal.Title))},
nal.Title,
[]byte{uint8(len(nal.Poster))},
nal.Poster,
[]byte{uint8(len(NewsFlavor))},
NewsFlavor,
nal.ArticleSize[:],
)
if nal.readOffset >= len(out) {
return 0, io.EOF // All bytes have been read
}
n := copy(p, out[nal.readOffset:])
nal.readOffset += n
return n, nil
}
type NewsFlavorList struct {
// Flavor size 1
// Flavor text size MIME type string
// Article size 2
}
func (newscat *NewsCategoryListData15) Read(p []byte) (int, error) {
count := make([]byte, 2)
binary.BigEndian.PutUint16(count, uint16(len(newscat.Articles)+len(newscat.SubCats)))
out := slices.Concat(
newscat.Type[:],
count,
)
if newscat.Type == NewsCategory {
out = slices.Concat(out,
newscat.GUID[:],
newscat.AddSN[:],
newscat.DeleteSN[:],
)
}
out = slices.Concat(out,
newscat.nameLen(),
[]byte(newscat.Name),
)
if newscat.readOffset >= len(out) {
return 0, io.EOF // All bytes have been read
}
n := copy(p, out)
newscat.readOffset = n
return n, nil
}
func (newscat *NewsCategoryListData15) nameLen() []byte {
return []byte{uint8(len(newscat.Name))}
}
// newsPathScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens
func newsPathScanner(data []byte, _ bool) (advance int, token []byte, err error) {
if len(data) < 3 {
return 0, nil, nil
}
advance = 3 + int(data[2])
return advance, data[3:advance], nil
}
type MockThreadNewsMgr struct {
mock.Mock
}
func (m *MockThreadNewsMgr) ListArticles(newsPath []string) (NewsArtListData, error) {
args := m.Called(newsPath)
return args.Get(0).(NewsArtListData), args.Error(1)
}
func (m *MockThreadNewsMgr) GetArticle(newsPath []string, articleID uint32) *NewsArtData {
args := m.Called(newsPath, articleID)
return args.Get(0).(*NewsArtData)
}
func (m *MockThreadNewsMgr) DeleteArticle(newsPath []string, articleID uint32, recursive bool) error {
args := m.Called(newsPath, articleID, recursive)
return args.Error(0)
}
func (m *MockThreadNewsMgr) PostArticle(newsPath []string, parentArticleID uint32, article NewsArtData) error {
args := m.Called(newsPath, parentArticleID, article)
return args.Error(0)
}
func (m *MockThreadNewsMgr) CreateGrouping(newsPath []string, name string, itemType [2]byte) error {
args := m.Called(newsPath, name, itemType)
return args.Error(0)
}
func (m *MockThreadNewsMgr) GetCategories(paths []string) []NewsCategoryListData15 {
args := m.Called(paths)
return args.Get(0).([]NewsCategoryListData15)
}
func (m *MockThreadNewsMgr) NewsItem(newsPath []string) NewsCategoryListData15 {
args := m.Called(newsPath)
return args.Get(0).(NewsCategoryListData15)
}
func (m *MockThreadNewsMgr) DeleteNewsItem(newsPath []string) error {
args := m.Called(newsPath)
return args.Error(0)
}
|