11 const defaultNewsDateFormat = "Jan02 15:04" // Jun23 20:49
13 const defaultNewsTemplate = `From %s (%s):
17 __________________________________________________________`
19 // ThreadedNews is the top level struct containing all threaded news categories, bundles, and articles
20 type ThreadedNews struct {
21 Categories map[string]NewsCategoryListData15 `yaml:"Categories"`
24 type NewsCategoryListData15 struct {
25 Type [2]byte `yaml:"Type,flow"` // Bundle (2) or category (3)
26 Name string `yaml:"Name"`
27 Articles map[uint32]*NewsArtData `yaml:"Articles"` // Optional, if Type is Category
28 SubCats map[string]NewsCategoryListData15 `yaml:"SubCats"`
29 GUID [16]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
30 AddSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
31 DeleteSN [4]byte `yaml:"-"` // What does this do? Undocumented and seeming unused.
34 func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData {
35 var newsArts []NewsArtList
36 var newsArtsPayload []byte
38 for i, art := range newscat.Articles {
40 binary.BigEndian.PutUint32(id, i)
42 newArt := NewsArtList{
45 ParentID: art.ParentArt,
46 Title: []byte(art.Title),
47 Poster: []byte(art.Poster),
48 ArticleSize: art.DataSize(),
51 newsArts = append(newsArts, newArt)
54 sort.Sort(byID(newsArts))
56 for _, v := range newsArts {
57 b, err := io.ReadAll(&v)
62 newsArtsPayload = append(newsArtsPayload, b...)
65 return NewsArtListData{
68 Description: []byte{},
69 NewsArtList: newsArtsPayload,
73 // NewsArtData represents single news article
74 type NewsArtData struct {
75 Title string `yaml:"Title"`
76 Poster string `yaml:"Poster"`
77 Date [8]byte `yaml:"Date,flow"`
78 PrevArt [4]byte `yaml:"PrevArt,flow"`
79 NextArt [4]byte `yaml:"NextArt,flow"`
80 ParentArt [4]byte `yaml:"ParentArt,flow"`
81 FirstChildArt [4]byte `yaml:"FirstChildArtArt,flow"`
82 DataFlav []byte `yaml:"-"` // "text/plain"
83 Data string `yaml:"Data"`
86 func (art *NewsArtData) DataSize() []byte {
87 dataLen := make([]byte, 2)
88 binary.BigEndian.PutUint16(dataLen, uint16(len(art.Data)))
93 type NewsArtListData struct {
94 ID [4]byte `yaml:"ID"`
95 Name []byte `yaml:"Name"`
96 Description []byte `yaml:"Description"` // not used?
97 NewsArtList []byte // List of articles Optional (if article count > 0)
100 readOffset int // Internal offset to track read progress
103 func (nald *NewsArtListData) Read(p []byte) (int, error) {
104 count := make([]byte, 4)
105 binary.BigEndian.PutUint32(count, uint32(nald.Count))
107 buf := slices.Concat(
110 []byte{uint8(len(nald.Name))},
112 []byte{uint8(len(nald.Description))},
117 if nald.readOffset >= len(buf) {
118 return 0, io.EOF // All bytes have been read
120 n := copy(p, buf[nald.readOffset:])
126 // NewsArtList is a summarized version of a NewArtData record for display in list view
127 type NewsArtList struct {
129 TimeStamp [8]byte // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
134 Title []byte // string
136 // Poster Poster string
138 FlavorList []NewsFlavorList
139 // Flavor list… Optional (if flavor count > 0)
140 ArticleSize []byte // Size 2
142 readOffset int // Internal offset to track read progress
145 type byID []NewsArtList
147 func (s byID) Len() int {
150 func (s byID) Swap(i, j int) {
151 s[i], s[j] = s[j], s[i]
153 func (s byID) Less(i, j int) bool {
154 return binary.BigEndian.Uint32(s[i].ID[:]) < binary.BigEndian.Uint32(s[j].ID[:])
158 NewsFlavorLen = []byte{0x0a}
159 NewsFlavor = []byte("text/plain")
162 func (nal *NewsArtList) Read(p []byte) (int, error) {
163 out := slices.Concat(
168 []byte{0, 1}, // Flavor Count
169 []byte{uint8(len(nal.Title))},
171 []byte{uint8(len(nal.Poster))},
178 if nal.readOffset >= len(out) {
179 return 0, io.EOF // All bytes have been read
182 n := copy(p, out[nal.readOffset:])
188 type NewsFlavorList struct {
190 // Flavor text size MIME type string
194 func (newscat *NewsCategoryListData15) MarshalBinary() (data []byte, err error) {
195 count := make([]byte, 2)
196 binary.BigEndian.PutUint16(count, uint16(len(newscat.Articles)+len(newscat.SubCats)))
198 out := append(newscat.Type[:], count...)
200 // If type is category
201 if bytes.Equal(newscat.Type[:], []byte{0, 3}) {
202 out = append(out, newscat.GUID[:]...) // GUID
203 out = append(out, newscat.AddSN[:]...) // Add SN
204 out = append(out, newscat.DeleteSN[:]...) // Delete SN
207 out = append(out, newscat.nameLen()...)
208 out = append(out, []byte(newscat.Name)...)
213 func (newscat *NewsCategoryListData15) nameLen() []byte {
214 return []byte{uint8(len(newscat.Name))}
217 // TODO: re-implement as bufio.Scanner interface
218 func ReadNewsPath(newsPath []byte) []string {
219 if len(newsPath) == 0 {
222 pathCount := binary.BigEndian.Uint16(newsPath[0:2])
224 pathData := newsPath[2:]
227 for i := uint16(0); i < pathCount; i++ {
228 pathLen := pathData[2]
229 paths = append(paths, string(pathData[3:3+pathLen]))
231 pathData = pathData[pathLen+3:]
237 func (s *Server) GetNewsCatByPath(paths []string) map[string]NewsCategoryListData15 {
238 cats := s.ThreadedNews.Categories
239 for _, path := range paths {
240 cats = cats[path].SubCats