-
Notifications
You must be signed in to change notification settings - Fork 1
/
pushover_client.go
98 lines (79 loc) · 2.6 KB
/
pushover_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"log"
"net"
"reflect"
"time"
"github.com/jangxx/go-poclient"
)
func initPOClient(config Config) *poclient.Client {
po := poclient.New()
po.SetAppInfo(APP_NAME, APP_VERSION)
if config.Userid != "" && config.Usersecret != "" {
po.RestoreLogin(config.Usersecret, config.Userid)
}
if config.Deviceid != "" {
po.RestoreDevice(config.Deviceid)
}
if loggedIn, registered := po.GetStatus(); loggedIn && registered {
_, err := po.GetMessages() // get messages to test login
if err != nil {
log.Printf("Error while restoring Pushover login: %s\n", err.Error())
po = poclient.New() // start from scratch
} else {
log.Printf("Successfully restored Pushover login & device registration")
}
}
return po
}
func resetPOClient() {
pushover.CloseWebsocket()
pushover = poclient.New()
}
func listenForNotifications(po *poclient.Client) {
messages, err := po.GetMessages()
if err == nil {
for _, msg := range messages {
po.Messages <- msg // shove the old messages through the channel
}
err := po.DeleteOldMessages(messages)
if err != nil {
log.Printf("Error while deleting old messages: %s\n", err.Error())
}
} else {
log.Printf("Error while fetching old messages: %s\n", err.Error())
}
for {
err := po.ListenForNotifications()
log.Println(err.Error())
if _, iserrorframe := err.(*poclient.ErrorFrameError); iserrorframe {
// reset user config
config.Userid = ""
config.Usersecret = ""
config.Deviceid = ""
config.Display_Devicename = ""
config.Display_Username = ""
err = storeConfig(config, "settings.json")
if err != nil {
log.Printf("Error while resetting user config: %s\n", err.Error())
}
sendStatusNotification("A permanent error occured. You need to re-login after the application is started again.")
log.Println("The pushover server sent an error frame; qutting the application now")
quit_channel <- true
return
}
if _, isneterror := err.(net.Error); isneterror {
log.Println("Error was network related, retry in 15 seconds")
// don't wait for pushover_retry if the error was network related, instead wait for 15 seconds
time.Sleep(15 * time.Second)
} else if _, registered := po.GetStatus(); registered {
log.Println("Error was not network related, but we are registered; retry in 15 seconds")
time.Sleep(15 * time.Second)
} else {
log.Println("Error was not network related, retry when we get the instruction. Error type: " + reflect.TypeOf(err).String())
// wait until a retry makes sense if the error wasn't network related
<-pushover_retry
}
log.Println("Retrying connection")
}
}