]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "cmp" | |
5 | "encoding/binary" | |
6 | "io" | |
7 | "slices" | |
8 | ) | |
9 | ||
10 | var ( | |
11 | NewsBundle = [2]byte{0, 2} | |
12 | NewsCategory = [2]byte{0, 3} | |
13 | ) | |
14 | ||
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 | |
24 | } | |
25 | ||
26 | // ThreadedNews contains the top level of threaded news categories, bundles, and articles. | |
27 | type ThreadedNews struct { | |
28 | Categories map[string]NewsCategoryListData15 `yaml:"Categories"` | |
29 | } | |
30 | ||
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. | |
39 | ||
40 | readOffset int // Internal offset to track read progress | |
41 | } | |
42 | ||
43 | func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData { | |
44 | var newsArts []NewsArtList | |
45 | var newsArtsPayload []byte | |
46 | ||
47 | for i, art := range newscat.Articles { | |
48 | id := make([]byte, 4) | |
49 | binary.BigEndian.PutUint32(id, i) | |
50 | ||
51 | newsArts = append(newsArts, NewsArtList{ | |
52 | ID: [4]byte(id), | |
53 | TimeStamp: art.Date, | |
54 | ParentID: art.ParentArt, | |
55 | Title: []byte(art.Title), | |
56 | Poster: []byte(art.Poster), | |
57 | ArticleSize: art.DataSize(), | |
58 | }) | |
59 | } | |
60 | ||
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 { | |
63 | return cmp.Compare( | |
64 | binary.BigEndian.Uint32(a.ID[:]), | |
65 | binary.BigEndian.Uint32(b.ID[:]), | |
66 | ) | |
67 | }) | |
68 | ||
69 | for _, v := range newsArts { | |
70 | b, err := io.ReadAll(&v) | |
71 | if err != nil { | |
72 | // TODO | |
73 | panic(err) | |
74 | } | |
75 | newsArtsPayload = append(newsArtsPayload, b...) | |
76 | } | |
77 | ||
78 | return NewsArtListData{ | |
79 | Count: len(newsArts), | |
80 | Name: []byte{}, | |
81 | Description: []byte{}, | |
82 | NewsArtList: newsArtsPayload, | |
83 | } | |
84 | } | |
85 | ||
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"` | |
97 | } | |
98 | ||
99 | func (art *NewsArtData) DataSize() [2]byte { | |
100 | dataLen := make([]byte, 2) | |
101 | binary.BigEndian.PutUint16(dataLen, uint16(len(art.Data))) | |
102 | ||
103 | return [2]byte(dataLen) | |
104 | } | |
105 | ||
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) | |
111 | Count int | |
112 | ||
113 | readOffset int // Internal offset to track read progress | |
114 | } | |
115 | ||
116 | func (nald *NewsArtListData) Read(p []byte) (int, error) { | |
117 | count := make([]byte, 4) | |
118 | binary.BigEndian.PutUint32(count, uint32(nald.Count)) | |
119 | ||
120 | buf := slices.Concat( | |
121 | nald.ID[:], | |
122 | count, | |
123 | []byte{uint8(len(nald.Name))}, | |
124 | nald.Name, | |
125 | []byte{uint8(len(nald.Description))}, | |
126 | nald.Description, | |
127 | nald.NewsArtList, | |
128 | ) | |
129 | ||
130 | if nald.readOffset >= len(buf) { | |
131 | return 0, io.EOF // All bytes have been read | |
132 | } | |
133 | n := copy(p, buf[nald.readOffset:]) | |
134 | nald.readOffset += n | |
135 | ||
136 | return n, nil | |
137 | } | |
138 | ||
139 | // NewsArtList is a summarized version of a NewArtData record for display in list view | |
140 | type NewsArtList struct { | |
141 | ID [4]byte | |
142 | TimeStamp [8]byte // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes) | |
143 | ParentID [4]byte | |
144 | Flags [4]byte | |
145 | FlavorCount [2]byte | |
146 | // Title size 1 | |
147 | Title []byte // string | |
148 | // Poster size 1 | |
149 | // Poster Poster string | |
150 | Poster []byte | |
151 | FlavorList []NewsFlavorList | |
152 | // Flavor list… Optional (if flavor count > 0) | |
153 | ArticleSize [2]byte // Size 2 | |
154 | ||
155 | readOffset int // Internal offset to track read progress | |
156 | } | |
157 | ||
158 | var ( | |
159 | NewsFlavorLen = []byte{0x0a} | |
160 | NewsFlavor = []byte("text/plain") | |
161 | ) | |
162 | ||
163 | func (nal *NewsArtList) Read(p []byte) (int, error) { | |
164 | out := slices.Concat( | |
165 | nal.ID[:], | |
166 | nal.TimeStamp[:], | |
167 | nal.ParentID[:], | |
168 | nal.Flags[:], | |
169 | []byte{0, 1}, // Flavor Count TODO: make this not hardcoded | |
170 | []byte{uint8(len(nal.Title))}, | |
171 | nal.Title, | |
172 | []byte{uint8(len(nal.Poster))}, | |
173 | nal.Poster, | |
174 | NewsFlavorLen, | |
175 | NewsFlavor, | |
176 | nal.ArticleSize[:], | |
177 | ) | |
178 | ||
179 | if nal.readOffset >= len(out) { | |
180 | return 0, io.EOF // All bytes have been read | |
181 | } | |
182 | ||
183 | n := copy(p, out[nal.readOffset:]) | |
184 | nal.readOffset += n | |
185 | ||
186 | return n, io.EOF | |
187 | } | |
188 | ||
189 | type NewsFlavorList struct { | |
190 | // Flavor size 1 | |
191 | // Flavor text size MIME type string | |
192 | // Article size 2 | |
193 | } | |
194 | ||
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))) | |
198 | ||
199 | out := slices.Concat( | |
200 | newscat.Type[:], | |
201 | count, | |
202 | ) | |
203 | if newscat.Type == NewsCategory { | |
204 | out = slices.Concat(out, | |
205 | newscat.GUID[:], | |
206 | newscat.AddSN[:], | |
207 | newscat.DeleteSN[:], | |
208 | ) | |
209 | } | |
210 | out = slices.Concat(out, | |
211 | newscat.nameLen(), | |
212 | []byte(newscat.Name), | |
213 | ) | |
214 | ||
215 | if newscat.readOffset >= len(out) { | |
216 | return 0, io.EOF // All bytes have been read | |
217 | } | |
218 | ||
219 | n := copy(p, out) | |
220 | ||
221 | newscat.readOffset = n | |
222 | ||
223 | return n, nil | |
224 | } | |
225 | ||
226 | func (newscat *NewsCategoryListData15) nameLen() []byte { | |
227 | return []byte{uint8(len(newscat.Name))} | |
228 | } | |
229 | ||
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) { | |
232 | if len(data) < 3 { | |
233 | return 0, nil, nil | |
234 | } | |
235 | ||
236 | advance = 3 + int(data[2]) | |
237 | return advance, data[3:advance], nil | |
238 | } |