]> git.r.bdr.sh - rbdr/mobius/blobdiff - client.go
Cleanup and error handling
[rbdr/mobius] / client.go
index de2844313e2d3b10d81d5b91a0d6046e706eb29c..79f623a9f31d74bcec1138ef1fbc8ecf2d8d78bb 100644 (file)
--- a/client.go
+++ b/client.go
@@ -44,6 +44,12 @@ type ClientPrefs struct {
        Tracker   string     `yaml:"Tracker"`
 }
 
+func (cp *ClientPrefs) IconBytes() []byte {
+       iconBytes := make([]byte, 2)
+       binary.BigEndian.PutUint16(iconBytes, uint16(cp.IconID))
+       return iconBytes
+}
+
 func readConfig(cfgPath string) (*ClientPrefs, error) {
        fh, err := os.Open(cfgPath)
        if err != nil {
@@ -62,10 +68,8 @@ func readConfig(cfgPath string) (*ClientPrefs, error) {
 type Client struct {
        DebugBuf    *DebugBuffer
        Connection  net.Conn
-       UserName    []byte
        Login       *[]byte
        Password    *[]byte
-       Icon        *[]byte
        Flags       *[]byte
        ID          *[]byte
        Version     []byte
@@ -210,7 +214,7 @@ func (ui *UI) renderSettingsForm() *tview.Flex {
        iconStr := strconv.Itoa(ui.HLClient.pref.IconID)
        settingsForm := tview.NewForm()
        settingsForm.AddInputField("Your Name", ui.HLClient.pref.Username, 0, nil, nil)
-       settingsForm.AddInputField("IconID",iconStr, 0, func(idStr string, _ rune) bool {
+       settingsForm.AddInputField("IconID", iconStr, 0, func(idStr string, _ rune) bool {
                _, err := strconv.Atoi(idStr)
                return err == nil
        }, nil)
@@ -249,12 +253,6 @@ func (ui *UI) renderSettingsForm() *tview.Flex {
        return centerFlex
 }
 
-var (
-       srvIP    string
-       srvLogin string
-       srvPass  string
-)
-
 // DebugBuffer wraps a *tview.TextView and adds a Sync() method to make it available as a Zap logger
 type DebugBuffer struct {
        TextView *tview.TextView
@@ -284,19 +282,13 @@ func (ui *UI) joinServer(addr, login, password string) error {
 }
 
 func (ui *UI) renderJoinServerForm(server, login, password, backPage string, save, defaultConnect bool) *tview.Flex {
-       srvIP = server
        joinServerForm := tview.NewForm()
        joinServerForm.
-               AddInputField("Server", server, 20, nil, func(text string) {
-                       srvIP = text
-               }).
-               AddInputField("Login", login, 20, nil, func(text string) {
-                       l := []byte(text)
-                       ui.HLClient.Login = &l
-               }).
-               AddPasswordField("Password", password, 20, '*', nil).
+               AddInputField("Server", server, 0, nil, nil).
+               AddInputField("Login", login, 0, nil, nil).
+               AddPasswordField("Password", password, 0, '*', nil).
                AddCheckbox("Save", save, func(checked bool) {
-                       // TODO
+                       // TODO: Implement bookmark saving
                }).
                AddButton("Cancel", func() {
                        ui.Pages.SwitchToPage(backPage)
@@ -435,8 +427,6 @@ func (ui *UI) Start() {
                        ui.Pages.AddAndSwitchToPage("trackerList", ui.trackerList, true)
                }).
                AddItem("Settings", "", 's', func() {
-                       //ui.Pages.AddPage("settings", ui.renderSettingsForm(), true, false)
-
                        ui.Pages.AddPage("settings", ui.renderSettingsForm(), true, true)
                }).
                AddItem("Quit", "", 'q', func() {
@@ -454,12 +444,10 @@ func (ui *UI) Start() {
                }
                // Show Logs
                if event.Key() == tcell.KeyCtrlL {
-                       //curPage, _ := ui.Pages.GetFrontPage()
                        ui.HLClient.DebugBuf.TextView.ScrollToEnd()
                        ui.HLClient.DebugBuf.TextView.SetBorder(true).SetTitle("Logs")
                        ui.HLClient.DebugBuf.TextView.SetDoneFunc(func(key tcell.Key) {
                                if key == tcell.KeyEscape {
-                                       //ui.Pages.SwitchToPage("serverUI")
                                        ui.Pages.RemovePage("logs")
                                }
                        })
@@ -470,13 +458,13 @@ func (ui *UI) Start() {
        })
 
        if err := ui.App.SetRoot(ui.Pages, true).SetFocus(ui.Pages).Run(); err != nil {
-               panic(err)
+               ui.App.Stop()
+               os.Exit(1)
        }
 }
 
 func NewClient(username string, logger *zap.SugaredLogger) *Client {
        c := &Client{
-               Icon:        &[]byte{0x07, 0xd7},
                Logger:      logger,
                activeTasks: make(map[uint32]*Transaction),
                Handlers:    clientHandlers,
@@ -700,15 +688,15 @@ func (c *Client) renderUserList() {
        for _, u := range c.UserList {
                flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(u.Flags)))
                if flagBitmap.Bit(userFlagAdmin) == 1 {
-                       fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name)
+                       _, _ = fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name)
                } else {
-                       fmt.Fprintf(c.UI.userList, "%s\n", u.Name)
+                       _, _ = fmt.Fprintf(c.UI.userList, "%s\n", u.Name)
                }
        }
 }
 
 func handleClientChatMsg(c *Client, t *Transaction) (res []Transaction, err error) {
-       fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data)
+       _, _ = fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data)
 
        return res, err
 }
@@ -732,7 +720,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction
                                        *NewTransaction(
                                                tranAgreed, nil,
                                                NewField(fieldUserName, []byte(c.pref.Username)),
-                                               NewField(fieldUserIconID, *c.Icon),
+                                               NewField(fieldUserIconID, c.pref.IconBytes()),
                                                NewField(fieldUserFlags, []byte{0x00, 0x00}),
                                                NewField(fieldOptions, []byte{0x00, 0x00}),
                                        ),
@@ -741,7 +729,7 @@ func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction
                                c.UI.Pages.HidePage("agreement")
                                c.UI.App.SetFocus(c.UI.chatInput)
                        } else {
-                               c.Disconnect()
+                               _ = c.Disconnect()
                                c.UI.Pages.SwitchToPage("home")
                        }
                },
@@ -853,7 +841,7 @@ func (c *Client) LogIn(login string, password string) error {
                *NewTransaction(
                        tranLogin, nil,
                        NewField(fieldUserName, []byte(c.pref.Username)),
-                       NewField(fieldUserIconID, []byte{0x07, 0xd1}),
+                       NewField(fieldUserIconID, c.pref.IconBytes()),
                        NewField(fieldUserLogin, []byte(NegatedUserString([]byte(login)))),
                        NewField(fieldUserPassword, []byte(NegatedUserString([]byte(password)))),
                        NewField(fieldVersion, []byte{0, 2}),