]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/client.go
Merge pull request #103 from jhalter/handle_tracker_list_errors
[rbdr/mobius] / hotline / client.go
index 3598a002d999161f0321f93becac3e8ba4f25a0f..ffd0fb3c1cb2c1607848c52278dcfcdf9c9ecd6a 100644 (file)
@@ -9,7 +9,6 @@ import (
        "fmt"
        "github.com/gdamore/tcell/v2"
        "github.com/rivo/tview"
-       "github.com/stretchr/testify/mock"
        "go.uber.org/zap"
        "gopkg.in/yaml.v3"
        "math/big"
@@ -49,10 +48,8 @@ func (cp *ClientPrefs) IconBytes() []byte {
        return iconBytes
 }
 
-func (cp *ClientPrefs) AddBookmark(name, addr, login, pass string) error {
+func (cp *ClientPrefs) AddBookmark(name, addr, login, pass string) {
        cp.Bookmarks = append(cp.Bookmarks, Bookmark{Addr: addr, Login: login, Password: pass})
-
-       return nil
 }
 
 func readConfig(cfgPath string) (*ClientPrefs, error) {
@@ -160,15 +157,6 @@ type ClientTHandler interface {
        Handle(*Client, *Transaction) ([]Transaction, error)
 }
 
-type mockClientHandler struct {
-       mock.Mock
-}
-
-func (mh *mockClientHandler) Handle(cc *Client, t *Transaction) ([]Transaction, error) {
-       args := mh.Called(cc, t)
-       return args.Get(0).([]Transaction), args.Error(1)
-}
-
 var clientHandlers = map[uint16]ClientHandler{
        TranChatMsg:          handleClientChatMsg,
        TranLogin:            handleClientTranLogin,
@@ -186,10 +174,10 @@ var clientHandlers = map[uint16]ClientHandler{
 }
 
 func handleTranServerMsg(c *Client, t *Transaction) (res []Transaction, err error) {
-       time := time.Now().Format(time.RFC850)
+       now := time.Now().Format(time.RFC850)
 
        msg := strings.ReplaceAll(string(t.GetField(FieldData).Data), "\r", "\n")
-       msg += "\n\nAt " + time
+       msg += "\n\nAt " + now
        title := fmt.Sprintf("| Private Message From:   %s |", t.GetField(FieldUserName).Data)
 
        msgBox := tview.NewTextView().SetScrollable(true)
@@ -198,7 +186,7 @@ func handleTranServerMsg(c *Client, t *Transaction) (res []Transaction, err erro
        msgBox.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
                switch event.Key() {
                case tcell.KeyEscape:
-                       c.UI.Pages.RemovePage("serverMsgModal" + time)
+                       c.UI.Pages.RemovePage("serverMsgModal" + now)
                }
                return event
        })
@@ -211,14 +199,14 @@ func handleTranServerMsg(c *Client, t *Transaction) (res []Transaction, err erro
                        AddItem(nil, 0, 1, false), 0, 2, true).
                AddItem(nil, 0, 1, false)
 
-       c.UI.Pages.AddPage("serverMsgModal"+time, centeredFlex, true, true)
+       c.UI.Pages.AddPage("serverMsgModal"+now, centeredFlex, true, true)
        c.UI.App.Draw() // TODO: errModal doesn't render without this.  wtf?
 
        return res, err
 }
 
 func (c *Client) showErrMsg(msg string) {
-       time := time.Now().Format(time.RFC850)
+       t := time.Now().Format(time.RFC850)
 
        title := "| Error |"
 
@@ -228,7 +216,7 @@ func (c *Client) showErrMsg(msg string) {
        msgBox.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
                switch event.Key() {
                case tcell.KeyEscape:
-                       c.UI.Pages.RemovePage("serverMsgModal" + time)
+                       c.UI.Pages.RemovePage("serverMsgModal" + t)
                }
                return event
        })
@@ -241,7 +229,7 @@ func (c *Client) showErrMsg(msg string) {
                        AddItem(nil, 0, 1, false), 0, 2, true).
                AddItem(nil, 0, 1, false)
 
-       c.UI.Pages.AddPage("serverMsgModal"+time, centeredFlex, true, true)
+       c.UI.Pages.AddPage("serverMsgModal"+t, centeredFlex, true, true)
        c.UI.App.Draw() // TODO: errModal doesn't render without this.  wtf?
 }
 
@@ -435,7 +423,7 @@ func (c *Client) renderUserList() {
        c.UI.userList.Clear()
        for _, u := range c.UserList {
                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(u.Flags)))
-               if flagBitmap.Bit(userFlagAdmin) == 1 {
+               if flagBitmap.Bit(UserFlagAdmin) == 1 {
                        _, _ = fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name)
                } else {
                        _, _ = fmt.Fprintf(c.UI.userList, "%s\n", u.Name)
@@ -601,21 +589,18 @@ func (c *Client) LogIn(login string, password string) error {
 
 func (c *Client) Send(t Transaction) error {
        requestNum := binary.BigEndian.Uint16(t.Type)
-       tID := binary.BigEndian.Uint32(t.ID)
-
-       // handler := TransactionHandlers[requestNum]
 
        // if transaction is NOT reply, add it to the list to transactions we're expecting a response for
        if t.IsReply == 0 {
-               c.activeTasks[tID] = &t
+               c.activeTasks[binary.BigEndian.Uint32(t.ID)] = &t
        }
 
-       var n int
-       var err error
        b, err := t.MarshalBinary()
        if err != nil {
                return err
        }
+
+       var n int
        if n, err = c.Connection.Write(b); err != nil {
                return err
        }
@@ -635,18 +620,17 @@ func (c *Client) HandleTransaction(t *Transaction) error {
                t.Type = origT.Type
        }
 
-       requestNum := binary.BigEndian.Uint16(t.Type)
-       c.Logger.Debugw("Received Transaction", "RequestType", requestNum)
-
-       if handler, ok := c.Handlers[requestNum]; ok {
+       if handler, ok := c.Handlers[binary.BigEndian.Uint16(t.Type)]; ok {
                outT, _ := handler(c, t)
                for _, t := range outT {
-                       c.Send(t)
+                       if err := c.Send(t); err != nil {
+                               return err
+                       }
                }
        } else {
                c.Logger.Debugw(
                        "Unimplemented transaction type received",
-                       "RequestID", requestNum,
+                       "RequestID", t.Type,
                        "TransactionID", t.ID,
                )
        }