X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/72dd37f1abb2b550aaaac48eac677403d5664797..958108952eec4cef92bcd26cd0c845aaed5a4982:/hotline/client.go diff --git a/hotline/client.go b/hotline/client.go index dd113c9..83bcfab 100644 --- a/hotline/client.go +++ b/hotline/client.go @@ -10,7 +10,7 @@ import ( "github.com/rivo/tview" "github.com/stretchr/testify/mock" "go.uber.org/zap" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "math/big" "math/rand" "net" @@ -61,7 +61,6 @@ func readConfig(cfgPath string) (*ClientPrefs, error) { prefs := ClientPrefs{} decoder := yaml.NewDecoder(fh) - decoder.SetStrict(true) if err := decoder.Decode(&prefs); err != nil { return nil, err } @@ -104,8 +103,7 @@ func NewClient(cfgPath string, logger *zap.SugaredLogger) *Client { prefs, err := readConfig(cfgPath) if err != nil { - fmt.Printf("unable to read config file %s", cfgPath) - os.Exit(1) + logger.Fatal(fmt.Sprintf("unable to read config file %s\n", cfgPath)) } c.pref = prefs @@ -121,7 +119,7 @@ func (db *DebugBuffer) Write(p []byte) (int, error) { return db.TextView.Write(p) } -// Sync is a noop function that exists to satisfy the zapcore.WriteSyncer interface +// Sync is a noop function that dataFile to satisfy the zapcore.WriteSyncer interface func (db *DebugBuffer) Sync() error { return nil } @@ -200,7 +198,7 @@ var clientHandlers = map[uint16]clientTHandler{ Handler: handleTranServerMsg, }, tranKeepAlive: clientTransaction{ - Name: "tranKeepAlive", + Name: "tranKeepAlive", Handler: func(client *Client, transaction *Transaction) (t []Transaction, err error) { return t, err }, @@ -239,7 +237,41 @@ func handleTranServerMsg(c *Client, t *Transaction) (res []Transaction, err erro return res, err } +func (c *Client) showErrMsg(msg string) { + time := time.Now().Format(time.RFC850) + + title := "| Error |" + + msgBox := tview.NewTextView().SetScrollable(true) + msgBox.SetText(msg).SetBackgroundColor(tcell.ColorDarkRed) + msgBox.SetTitle(title).SetBorder(true) + msgBox.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { + switch event.Key() { + case tcell.KeyEscape: + c.UI.Pages.RemovePage("serverMsgModal" + time) + } + return event + }) + + centeredFlex := tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(msgBox, 0, 2, true). + AddItem(nil, 0, 1, false), 0, 2, true). + AddItem(nil, 0, 1, false) + + c.UI.Pages.AddPage("serverMsgModal"+time, centeredFlex, true, true) + c.UI.App.Draw() // TODO: errModal doesn't render without this. wtf? +} + func handleGetFileNameList(c *Client, t *Transaction) (res []Transaction, err error) { + if t.IsError() { + c.showErrMsg(string(t.GetField(fieldError).Data)) + c.Logger.Infof("Error: %s", t.GetField(fieldError).Data) + return res, err + } + fTree := tview.NewTreeView().SetTopLevel(1) root := tview.NewTreeNode("Root") fTree.SetRoot(root).SetCurrentNode(root) @@ -336,8 +368,8 @@ func handleGetMsgs(c *Client, t *Transaction) (res []Transaction, err error) { newsTextView.SetBorder(true).SetTitle("News") c.UI.Pages.AddPage("news", newsTextView, true, true) - //c.UI.Pages.SwitchToPage("news") - //c.UI.App.SetFocus(newsTextView) + // c.UI.Pages.SwitchToPage("news") + // c.UI.App.SetFocus(newsTextView) c.UI.App.Draw() return res, err @@ -399,54 +431,6 @@ func handleNotifyDeleteUser(c *Client, t *Transaction) (res []Transaction, err e return res, err } -const readBuffSize = 1024000 // 1KB - TODO: what should this be? - -func (c *Client) ReadLoop() error { - tranBuff := make([]byte, 0) - tReadlen := 0 - // Infinite loop where take action on incoming client requests until the connection is closed - for { - buf := make([]byte, readBuffSize) - tranBuff = tranBuff[tReadlen:] - - readLen, err := c.Connection.Read(buf) - if err != nil { - return err - } - tranBuff = append(tranBuff, buf[:readLen]...) - - // We may have read multiple requests worth of bytes from Connection.Read. readTransactions splits them - // into a slice of transactions - var transactions []Transaction - if transactions, tReadlen, err = readTransactions(tranBuff); err != nil { - c.Logger.Errorw("Error handling transaction", "err", err) - } - - // iterate over all of the transactions that were parsed from the byte slice and handle them - for _, t := range transactions { - if err := c.HandleTransaction(&t); err != nil { - c.Logger.Errorw("Error handling transaction", "err", err) - } - } - } -} - -func (c *Client) GetTransactions() error { - tranBuff := make([]byte, 0) - tReadlen := 0 - - buf := make([]byte, readBuffSize) - tranBuff = tranBuff[tReadlen:] - - readLen, err := c.Connection.Read(buf) - if err != nil { - return err - } - tranBuff = append(tranBuff, buf[:readLen]...) - - return nil -} - func handleClientGetUserNameList(c *Client, t *Transaction) (res []Transaction, err error) { var users []User for _, field := range t.Fields { @@ -605,10 +589,10 @@ var ServerHandshake = []byte{ } func (c *Client) Handshake() error { - //Protocol ID 4 ‘TRTP’ 0x54 52 54 50 - //Sub-protocol ID 4 User defined - //Version 2 1 Currently 1 - //Sub-version 2 User defined + // Protocol ID 4 ‘TRTP’ 0x54 52 54 50 + // Sub-protocol ID 4 User defined + // Version 2 1 Currently 1 + // Sub-version 2 User defined if _, err := c.Connection.Write(ClientHandshake); err != nil { return fmt.Errorf("handshake write err: %s", err) } @@ -635,7 +619,6 @@ func (c *Client) LogIn(login string, password string) error { NewField(fieldUserIconID, c.pref.IconBytes()), NewField(fieldUserLogin, negateString([]byte(login))), NewField(fieldUserPassword, negateString([]byte(password))), - NewField(fieldVersion, []byte{0, 2}), ), ) } @@ -644,7 +627,7 @@ func (c *Client) Send(t Transaction) error { requestNum := binary.BigEndian.Uint16(t.Type) tID := binary.BigEndian.Uint32(t.ID) - //handler := TransactionHandlers[requestNum] + // 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 { @@ -677,10 +660,7 @@ func (c *Client) HandleTransaction(t *Transaction) error { } requestNum := binary.BigEndian.Uint16(t.Type) - c.Logger.Infow( - "Received Transaction", - "RequestType", requestNum, - ) + c.Logger.Debugw("Received Transaction", "RequestType", requestNum) if handler, ok := c.Handlers[requestNum]; ok { outT, _ := handler.Handle(c, t) @@ -698,18 +678,6 @@ func (c *Client) HandleTransaction(t *Transaction) error { return nil } -func (c *Client) Connected() bool { - // c.Agreed == true && - if c.UserAccess != nil { - return true - } - return false -} - func (c *Client) Disconnect() error { - err := c.Connection.Close() - if err != nil { - return err - } - return nil + return c.Connection.Close() }