-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (113 loc) · 4.05 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/ricoschulte/go-myapps/connection"
)
var host = flag.String("host", "", "the IP address or Hostname of the Pbx. Needs to be the HTTP/TLS port")
var username = flag.String("username", "", "the Username of the myApps account")
var password = flag.String("password", "", "the Password of the myApps account")
var sessionfile = flag.String("sessionfile", "myapps_session_%s.json", "the filename used to store the session. can contain '%s' as placeholder for the accountname.")
var useragent = flag.String("useragent", "Clockbot (go-myapps)", "the UserAgent showe in the list of sessions of the myApps account")
var insecureskipverify = flag.Bool("insecureskipverify", false, "skip verify TLS/SSL certificates")
var debug = flag.Bool("debug", false, "show debug output")
var interval = flag.String("interval", "1s", "interval in seconds to update the user presence")
var timezone = flag.String("timezone", "", "the timezone to use to show the date & time")
var format = flag.String("format", "2006-01-02 15:04:05", "the format to show the current data & time")
func main() {
flag.Usage = func() {
fmt.Println("Usage: go-myapps-clockbot [options]")
fmt.Println("Options:")
flag.PrintDefaults()
}
flag.Parse()
options := ". use -h to show commandline options"
if *host == "" {
fmt.Println("host cant be empty", options)
ExitWithDelay(1)
}
if *username == "" {
fmt.Println("username cant be empty", options)
ExitWithDelay(1)
}
if *password == "" {
fmt.Println("password cant be empty", options)
ExitWithDelay(1)
}
if *useragent == "" {
fmt.Println("useragent cant be empty", options)
ExitWithDelay(1)
}
_, err_location := time.LoadLocation(*timezone)
if err_location != nil {
fmt.Printf("unknown timezone '%s': %v\n", *timezone, err_location)
ExitWithDelay(1)
}
_, err_duration := time.ParseDuration(*interval)
if err_duration != nil {
fmt.Printf("unknown duration '%s': %v\n", *interval, err_duration)
ExitWithDelay(1)
}
var wg sync.WaitGroup
accountConfig := connection.Config{
Host: *host,
Username: *username,
Password: *password,
InsecureSkipVerify: *insecureskipverify,
UserAgent: *useragent,
SessionFilePath: fmt.Sprintf(*sessionfile, *username),
Debug: *debug,
}
// add a handler to the account
// this handler will wait until after the login the pbx send a 'UpdateAppsComplete' message
accountConfig.Handler.AddHandler(&ClockbotHandler{})
go accountConfig.StartSession(&wg)
wg.Wait()
select {}
}
func ExitWithDelay(exitcode int) {
time.Sleep(3 * time.Second)
os.Exit(exitcode)
}
type ClockbotHandler struct{}
func (m *ClockbotHandler) GetMt() string {
return "UpdateAppsComplete"
}
func (m *ClockbotHandler) HandleMessage(myAppsConnection *connection.MyAppsConnection, message []byte) error {
// we received the UpdateAppsComplete message and we start the loop that sends the presence updates
go m.RunLoop(myAppsConnection, message)
return nil
}
func (m *ClockbotHandler) RunLoop(myAppsConnection *connection.MyAppsConnection, message []byte) error {
for {
tz, _ := time.LoadLocation(*timezone)
// Get the current time in the specified timezone
now := time.Now().In(tz)
var message_out struct {
Mt string `json:"mt"`
Activity string `json:"activity"`
Note string `json:"note"`
}
message_out.Mt = "SetOwnPresence"
message_out.Activity = ""
message_out.Note = GetFormatedTimeString(now, *format)
updateStr, _ := json.Marshal(message_out)
err_write := myAppsConnection.Conn.WriteMessage(websocket.TextMessage, []byte(updateStr))
if err_write != nil {
// if sending the update fails we quit our loop
myAppsConnection.Config.Printf("error while sending message: %v", err_write)
return err_write
}
// wait the interval and do it again
duration, _ := time.ParseDuration(*interval)
time.Sleep(duration)
}
}
func GetFormatedTimeString(current time.Time, format string) string {
return current.Format(format)
}