11 NewsBundle = [2]byte{0, 2}
12 NewsCategory = [2]byte{0, 3}
15 type ThreadedNewsMgr interface {
16 ListArticles(newsPath []string) NewsArtListData
17 GetArticle(newsPath []string, articleID uint32) *NewsArtData
18 DeleteArticle(newsPath []string, articleID uint32, recursive bool) error
19 PostArticle(newsPath []string, parentArticleID uint32, article NewsArtData) error
20 CreateGrouping(newsPath []string, name string, t [2]byte) error
21 GetCategories(paths []string) []NewsCategoryListData15
22 NewsItem(newsPath []string) NewsCategoryListData15
23 DeleteNewsItem(newsPath []string) error
26 // ThreadedNews contains the top level of threaded news categories, bundles, and articles.
27 type ThreadedNews struct {
28 Categories map[string]NewsCategoryListData15 `yaml:"Categories"`
31 type NewsCategoryListData15 struct {
32 Type [2]byte `yaml:"Type,flow"` // Bundle (2) or category (3)
33 Name string `yaml:"Name"`
34 Articles map[uint32]*NewsArtData `yaml:"Articles"` // Optional, if Type is Category
35 SubCats map[string]NewsCategoryListData15 `yaml:"SubCats"`
36 GUID [16]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
37 AddSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
38 DeleteSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
40 readOffset int // Internal offset to track read progress
43 func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData {
44 var newsArts []NewsArtList
45 var newsArtsPayload []byte
47 for i, art := range newscat.Articles {
49 binary.BigEndian.PutUint32(id, i)
51 newsArts = append(newsArts, NewsArtList{
54 ParentID: art.ParentArt,
55 Title: []byte(art.Title),
56 Poster: []byte(art.Poster),
57 ArticleSize: art.DataSize(),
61 // Sort the articles by ID. This is important for displaying the message threading correctly on the client side.
62 slices.SortFunc(newsArts, func(a, b NewsArtList) int {
64 binary.BigEndian.Uint32(a.ID[:]),
65 binary.BigEndian.Uint32(b.ID[:]),
69 for _, v := range newsArts {
70 b, err := io.ReadAll(&v)
75 newsArtsPayload = append(newsArtsPayload, b...)
78 return NewsArtListData{
81 Description: []byte{},
82 NewsArtList: newsArtsPayload,
86 // NewsArtData represents an individual news article.
87 type NewsArtData struct {
88 Title string `yaml:"Title"`
89 Poster string `yaml:"Poster"`
90 Date [8]byte `yaml:"Date,flow"`
91 PrevArt [4]byte `yaml:"PrevArt,flow"`
92 NextArt [4]byte `yaml:"NextArt,flow"`
93 ParentArt [4]byte `yaml:"ParentArt,flow"`
94 FirstChildArt [4]byte `yaml:"FirstChildArtArt,flow"`
95 DataFlav []byte `yaml:"-"` // MIME type string. Always "text/plain".
96 Data string `yaml:"Data"`
99 func (art *NewsArtData) DataSize() [2]byte {
100 dataLen := make([]byte, 2)
101 binary.BigEndian.PutUint16(dataLen, uint16(len(art.Data)))
103 return [2]byte(dataLen)
106 type NewsArtListData struct {
107 ID [4]byte `yaml:"Type"`
108 Name []byte `yaml:"Name"`
109 Description []byte `yaml:"Description"` // not used?
110 NewsArtList []byte // List of articles Optional (if article count > 0)
113 readOffset int // Internal offset to track read progress
116 func (nald *NewsArtListData) Read(p []byte) (int, error) {
117 count := make([]byte, 4)
118 binary.BigEndian.PutUint32(count, uint32(nald.Count))
120 buf := slices.Concat(
123 []byte{uint8(len(nald.Name))},
125 []byte{uint8(len(nald.Description))},
130 if nald.readOffset >= len(buf) {
131 return 0, io.EOF // All bytes have been read
133 n := copy(p, buf[nald.readOffset:])
139 // NewsArtList is a summarized version of a NewArtData record for display in list view
140 type NewsArtList struct {
142 TimeStamp [8]byte // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
147 Title []byte // string
149 // Poster Poster string
151 FlavorList []NewsFlavorList
152 // Flavor list… Optional (if flavor count > 0)
153 ArticleSize [2]byte // Size 2
155 readOffset int // Internal offset to track read progress
159 NewsFlavorLen = []byte{0x0a}
160 NewsFlavor = []byte("text/plain")
163 func (nal *NewsArtList) Read(p []byte) (int, error) {
164 out := slices.Concat(
169 []byte{0, 1}, // Flavor Count TODO: make this not hardcoded
170 []byte{uint8(len(nal.Title))},
172 []byte{uint8(len(nal.Poster))},
179 if nal.readOffset >= len(out) {
180 return 0, io.EOF // All bytes have been read
183 n := copy(p, out[nal.readOffset:])
189 type NewsFlavorList struct {
191 // Flavor text size MIME type string
195 func (newscat *NewsCategoryListData15) Read(p []byte) (int, error) {
196 count := make([]byte, 2)
197 binary.BigEndian.PutUint16(count, uint16(len(newscat.Articles)+len(newscat.SubCats)))
199 out := slices.Concat(
203 if newscat.Type == NewsCategory {
204 out = slices.Concat(out,
210 out = slices.Concat(out,
212 []byte(newscat.Name),
215 if newscat.readOffset >= len(out) {
216 return 0, io.EOF // All bytes have been read
221 newscat.readOffset = n
226 func (newscat *NewsCategoryListData15) nameLen() []byte {
227 return []byte{uint8(len(newscat.Name))}
230 // newsPathScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens
231 func newsPathScanner(data []byte, _ bool) (advance int, token []byte, err error) {
236 advance = 3 + int(data[2])
237 return advance, data[3:advance], nil