Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some error handling, readme update and refactoring #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 {
23 changes: 18 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -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
}

31 changes: 7 additions & 24 deletions ipc/ipc.go
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 17 additions & 1 deletion ipc/ipc_notwin.go
Original file line number Diff line number Diff line change
@@ -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
}