From 92fc9cce29c508ae42aa2dff669abd8906ba65e1 Mon Sep 17 00:00:00 2001 From: Daniel085 Date: Thu, 10 Jun 2021 12:54:27 -0300 Subject: [PATCH] some error handling, readme update and refactoring --- README.md | 6 ++++++ client/client.go | 23 ++++++++++++++++++----- ipc/ipc.go | 31 +++++++------------------------ ipc/ipc_notwin.go | 18 +++++++++++++++++- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f98a554..fc6a041 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,12 @@ err = client.SetActivity(client.Activity{ Timestamps: &client.Timestamps{ Start: time.Now(), }, + Buttons: []*client.Button{ + &client.Button{ + Label: "GitHub", + Url: "https://github.com/hugolgst/rich-go", + }, + }, }) if err != nil { diff --git a/client/client.go b/client/client.go index 546e9e7..d964d97 100644 --- a/client/client.go +++ b/client/client.go @@ -24,26 +24,33 @@ func Login(clientid string) error { return err } + err = ipc.Send(0, string(payload)) + if err != nil { + return err + } + // TODO: Response should be parsed - ipc.Send(0, string(payload)) + //fmt.Println(ipc.Read()) } logged = true return nil } -func Logout() { +func Logout() error { logged = false err := ipc.CloseSocket() if err != nil { - panic(err) + return err } + + return nil } func SetActivity(activity Activity) error { if !logged { - return nil + return fmt.Errorf("not logged") } payload, err := json.Marshal(Frame{ @@ -59,8 +66,14 @@ func SetActivity(activity Activity) error { return err } + err = ipc.Send(1, string(payload)) + if err != nil { + return err + } + // TODO: Response should be parsed - ipc.Send(1, string(payload)) + //fmt.Println(ipc.Read()) + return nil } diff --git a/ipc/ipc.go b/ipc/ipc.go index c66579d..209da51 100644 --- a/ipc/ipc.go +++ b/ipc/ipc.go @@ -5,26 +5,10 @@ import ( "encoding/binary" "fmt" "net" - "os" ) var socket net.Conn -// Choose the right directory to the ipc socket and return it -func GetIpcPath() string { - variablesnames := []string{"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"} - - for _, variablename := range variablesnames { - path, exists := os.LookupEnv(variablename) - - if exists { - return path - } - } - - return "/tmp" -} - func CloseSocket() error { if socket != nil { socket.Close() @@ -34,23 +18,22 @@ func CloseSocket() error { } // Read the socket response -func Read() string { +func Read() (string, error) { buf := make([]byte, 512) payloadlength, err := socket.Read(buf) if err != nil { //fmt.Println("Nothing to read") + return "", err } buffer := new(bytes.Buffer) - for i := 8; i < payloadlength; i++ { - buffer.WriteByte(buf[i]) - } + buffer.Write(buf[8:payloadlength]) - return buffer.String() + return buffer.String(), nil } // Send opcode and payload to the unix socket -func Send(opcode int, payload string) string { +func Send(opcode int, payload string) error { buf := new(bytes.Buffer) err := binary.Write(buf, binary.LittleEndian, int32(opcode)) @@ -66,8 +49,8 @@ func Send(opcode int, payload string) string { buf.Write([]byte(payload)) _, err = socket.Write(buf.Bytes()) if err != nil { - fmt.Println(err) + return err } - return Read() + return nil } diff --git a/ipc/ipc_notwin.go b/ipc/ipc_notwin.go index 2e96da3..3f860ff 100644 --- a/ipc/ipc_notwin.go +++ b/ipc/ipc_notwin.go @@ -4,12 +4,28 @@ package ipc import ( "net" + "os" "time" ) +// Choose the right directory to the ipc socket and return it +func getIpcPath() string { + variablesnames := []string{"XDG_RUNTIME_DIR", "TMPDIR", "TMP", "TEMP"} + + for _, variablename := range variablesnames { + path, exists := os.LookupEnv(variablename) + + if exists { + return path + } + } + + return "/tmp" +} + // OpenSocket opens the discord-ipc-0 unix socket func OpenSocket() error { - sock, err := net.DialTimeout("unix", GetIpcPath()+"/discord-ipc-0", time.Second*2) + sock, err := net.DialTimeout("unix", getIpcPath()+"/discord-ipc-0", time.Second*2) if err != nil { return err }