]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/news.go
Refactoring, cleanup, test backfilling
[rbdr/mobius] / hotline / news.go
index e6b756779886bf8e6c2fabe4bc4cba31b1d03e45..6e89388028a8d034ce1ed36102d2bb674bd25071 100644 (file)
@@ -1,8 +1,6 @@
 package hotline
 
 import (
 package hotline
 
 import (
-       "bytes"
-       "crypto/rand"
        "encoding/binary"
        "io"
        "slices"
        "encoding/binary"
        "io"
        "slices"
@@ -17,20 +15,21 @@ const defaultNewsTemplate = `From %s (%s):
 
 __________________________________________________________`
 
 
 __________________________________________________________`
 
+// ThreadedNews is the top level struct containing all threaded news categories, bundles, and articles
 type ThreadedNews struct {
        Categories map[string]NewsCategoryListData15 `yaml:"Categories"`
 }
 
 type NewsCategoryListData15 struct {
 type ThreadedNews struct {
        Categories map[string]NewsCategoryListData15 `yaml:"Categories"`
 }
 
 type NewsCategoryListData15 struct {
-       Type     [2]byte `yaml:"Type"` // Size 2 ; Bundle (2) or category (3)
-       Count    []byte  // Article or SubCategory count Size 2
-       NameSize byte
-       Name     string                            `yaml:"Name"`     //
+       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"`
        Articles map[uint32]*NewsArtData           `yaml:"Articles"` // Optional, if Type is Category
        SubCats  map[string]NewsCategoryListData15 `yaml:"SubCats"`
-       GUID     []byte                            // Size 16
-       AddSN    []byte                            // Size 4
-       DeleteSN []byte                            // Size 4
+       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 {
 }
 
 func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData {
@@ -42,15 +41,14 @@ func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData {
                binary.BigEndian.PutUint32(id, i)
 
                newArt := NewsArtList{
                binary.BigEndian.PutUint32(id, i)
 
                newArt := NewsArtList{
-                       ID:          id,
-                       TimeStamp:   art.Date[:],
-                       ParentID:    art.ParentArt[:],
-                       Flags:       []byte{0, 0, 0, 0},
-                       FlavorCount: []byte{0, 0},
+                       ID:          [4]byte(id),
+                       TimeStamp:   art.Date,
+                       ParentID:    art.ParentArt,
                        Title:       []byte(art.Title),
                        Poster:      []byte(art.Poster),
                        ArticleSize: art.DataSize(),
                }
                        Title:       []byte(art.Title),
                        Poster:      []byte(art.Poster),
                        ArticleSize: art.DataSize(),
                }
+
                newsArts = append(newsArts, newArt)
        }
 
                newsArts = append(newsArts, newArt)
        }
 
@@ -60,31 +58,29 @@ func (newscat *NewsCategoryListData15) GetNewsArtListData() NewsArtListData {
                b, err := io.ReadAll(&v)
                if err != nil {
                        // TODO
                b, err := io.ReadAll(&v)
                if err != nil {
                        // TODO
+                       panic(err)
                }
                newsArtsPayload = append(newsArtsPayload, b...)
        }
 
                }
                newsArtsPayload = append(newsArtsPayload, b...)
        }
 
-       nald := NewsArtListData{
-               ID:          [4]byte{0, 0, 0, 0},
+       return NewsArtListData{
                Count:       len(newsArts),
                Name:        []byte{},
                Description: []byte{},
                NewsArtList: newsArtsPayload,
        }
                Count:       len(newsArts),
                Name:        []byte{},
                Description: []byte{},
                NewsArtList: newsArtsPayload,
        }
-
-       return nald
 }
 
 // NewsArtData represents single news article
 type NewsArtData struct {
        Title         string  `yaml:"Title"`
        Poster        string  `yaml:"Poster"`
 }
 
 // NewsArtData represents single news article
 type NewsArtData struct {
        Title         string  `yaml:"Title"`
        Poster        string  `yaml:"Poster"`
-       Date          [8]byte `yaml:"Date"`             // size 8
-       PrevArt       [4]byte `yaml:"PrevArt"`          // size 4
-       NextArt       [4]byte `yaml:"NextArt"`          // size 4
-       ParentArt     [4]byte `yaml:"ParentArt"`        // size 4
-       FirstChildArt [4]byte `yaml:"FirstChildArtArt"` // size 4
-       DataFlav      []byte  `yaml:"DataFlav"`         // "text/plain"
+       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:"-"` // "text/plain"
        Data          string  `yaml:"Data"`
 }
 
        Data          string  `yaml:"Data"`
 }
 
@@ -96,39 +92,45 @@ func (art *NewsArtData) DataSize() []byte {
 }
 
 type NewsArtListData struct {
 }
 
 type NewsArtListData struct {
-       ID          [4]byte `yaml:"ID"` // Size 4
+       ID          [4]byte `yaml:"ID"`
        Name        []byte  `yaml:"Name"`
        Description []byte  `yaml:"Description"` // not used?
        NewsArtList []byte  // List of articles                 Optional (if article count > 0)
        Count       int
        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))
 
 }
 
 func (nald *NewsArtListData) Read(p []byte) (int, error) {
        count := make([]byte, 4)
        binary.BigEndian.PutUint32(count, uint32(nald.Count))
 
-       return copy(
-                       p,
-                       slices.Concat(
-                               nald.ID[:],
-                               count,
-                               []byte{uint8(len(nald.Name))},
-                               nald.Name,
-                               []byte{uint8(len(nald.Description))},
-                               nald.Description,
-                               nald.NewsArtList,
-                       ),
-               ),
-               io.EOF
+       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 {
 }
 
 // NewsArtList is a summarized version of a NewArtData record for display in list view
 type NewsArtList struct {
-       ID          []byte // Size 4
-       TimeStamp   []byte // Year (2 bytes), milliseconds (2 bytes) and seconds (4 bytes)
-       ParentID    []byte // Size 4
-       Flags       []byte // Size 4
-       FlavorCount []byte // Size 2
+       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
        // Title size   1
        Title []byte // string
        // Poster size  1
@@ -150,21 +152,27 @@ func (s byID) Swap(i, j int) {
        s[i], s[j] = s[j], s[i]
 }
 func (s byID) Less(i, j int) bool {
        s[i], s[j] = s[j], s[i]
 }
 func (s byID) Less(i, j int) bool {
-       return binary.BigEndian.Uint32(s[i].ID) < binary.BigEndian.Uint32(s[j].ID)
+       return binary.BigEndian.Uint32(s[i].ID[:]) < binary.BigEndian.Uint32(s[j].ID[:])
 }
 
 }
 
+var (
+       NewsFlavorLen = []byte{0x0a}
+       NewsFlavor    = []byte("text/plain")
+)
+
 func (nal *NewsArtList) Read(p []byte) (int, error) {
        out := slices.Concat(
 func (nal *NewsArtList) Read(p []byte) (int, error) {
        out := slices.Concat(
-               nal.ID,
-               nal.TimeStamp,
-               nal.ParentID,
-               nal.Flags,
-               []byte{0, 1},
+               nal.ID[:],
+               nal.TimeStamp[:],
+               nal.ParentID[:],
+               nal.Flags[:],
+               []byte{0, 1}, // Flavor Count TODO: make this not hardcoded
                []byte{uint8(len(nal.Title))},
                nal.Title,
                []byte{uint8(len(nal.Poster))},
                nal.Poster,
                []byte{uint8(len(nal.Title))},
                nal.Title,
                []byte{uint8(len(nal.Poster))},
                nal.Poster,
-               []byte{0x0a, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c, 0x61, 0x69, 0x6e},
+               NewsFlavorLen,
+               NewsFlavor,
                nal.ArticleSize,
        )
 
                nal.ArticleSize,
        )
 
@@ -184,29 +192,33 @@ type NewsFlavorList struct {
        // Article size 2
 }
 
        // Article size 2
 }
 
-func (newscat *NewsCategoryListData15) MarshalBinary() (data []byte, err error) {
+func (newscat *NewsCategoryListData15) Read(p []byte) (int, error) {
        count := make([]byte, 2)
        binary.BigEndian.PutUint16(count, uint16(len(newscat.Articles)+len(newscat.SubCats)))
 
        count := make([]byte, 2)
        binary.BigEndian.PutUint16(count, uint16(len(newscat.Articles)+len(newscat.SubCats)))
 
-       out := append(newscat.Type[:], count...)
-
-       if bytes.Equal(newscat.Type[:], []byte{0, 3}) {
-               // Generate a random GUID // TODO: does this need to be random?
-               b := make([]byte, 16)
-               _, err := rand.Read(b)
-               if err != nil {
-                       return data, err
-               }
+       out := slices.Concat(
+               newscat.Type[:],
+               count,
+       )
 
 
-               out = append(out, b...)                  // GUID
-               out = append(out, []byte{0, 0, 0, 1}...) // Add SN (TODO: not sure what this is)
-               out = append(out, []byte{0, 0, 0, 2}...) // Delete SN (TODO: not sure what this is)
+       // If type is category
+       if newscat.Type == [2]byte{0, 3} {
+               out = append(out, newscat.GUID[:]...)
+               out = append(out, newscat.AddSN[:]...)
+               out = append(out, newscat.DeleteSN[:]...)
        }
 
        out = append(out, newscat.nameLen()...)
        out = append(out, []byte(newscat.Name)...)
 
        }
 
        out = append(out, newscat.nameLen()...)
        out = append(out, []byte(newscat.Name)...)
 
-       return out, err
+       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 {
 }
 
 func (newscat *NewsCategoryListData15) nameLen() []byte {