]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/client.go
Merge pull request #96 from jhalter/disconnect_ban_users_earlier_in_login_flow
[rbdr/mobius] / hotline / client.go
index e5b396b75192f367239b99541edb225618b7a7b9..c7c1463b118e4d5aa2a68fcb6cd8b456e137833e 100644 (file)
@@ -35,10 +35,11 @@ type Bookmark struct {
 }
 
 type ClientPrefs struct {
-       Username  string     `yaml:"Username"`
-       IconID    int        `yaml:"IconID"`
-       Bookmarks []Bookmark `yaml:"Bookmarks"`
-       Tracker   string     `yaml:"Tracker"`
+       Username   string     `yaml:"Username"`
+       IconID     int        `yaml:"IconID"`
+       Bookmarks  []Bookmark `yaml:"Bookmarks"`
+       Tracker    string     `yaml:"Tracker"`
+       EnableBell bool       `yaml:"EnableBell"`
 }
 
 func (cp *ClientPrefs) IconBytes() []byte {
@@ -237,7 +238,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)
@@ -397,54 +432,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 {
@@ -480,6 +467,10 @@ func (c *Client) renderUserList() {
 }
 
 func handleClientChatMsg(c *Client, t *Transaction) (res []Transaction, err error) {
+       if c.pref.EnableBell {
+               fmt.Println("\a")
+       }
+
        _, _ = fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data)
 
        return res, err