/
presence.go
161 lines (129 loc) · 3.23 KB
/
presence.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package presence
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/42wim/sqlittle"
)
var (
ErrToken = errors.New("retrieving token failed")
StatusURL = "https://presence.teams.microsoft.com/v1/me/forceavailability/"
)
type request struct {
Availability string `json:"availability"`
Activity string `json:"activity,omitempty"`
DesiredExpirationTime string `json:"desiredExpirationTime,omitempty"`
}
func readToken() (string, error) {
var token string
configfile, err := os.UserConfigDir()
if err != nil {
return "", err
}
switch runtime.GOOS {
case "windows":
configfile = filepath.Join(configfile, "Microsoft", "Teams", "Cookies")
case "linux":
configfile = filepath.Join(configfile, "Microsoft", "Microsoft Teams", "Cookies")
}
db, err := sqlittle.Open(configfile)
if err != nil {
return "", fmt.Errorf("%w", err)
}
defer db.Close()
// read the TSAUTHCOOKIE from the cookies table
err = db.Select("cookies", func(r sqlittle.Row) {
var (
name string
value string
)
err = r.Scan(&name, &value)
if err != nil {
log.Panicf("db scan failed: %s", err)
}
if name == "TSAUTHCOOKIE" {
token = value
}
}, "name", "value")
if err != nil {
return "", fmt.Errorf("%w: %s", ErrToken, err)
}
return token, nil
}
func setPresence(presence, duration, token string) error {
var activity string
var desiredExpirationTime string
bearer := "Bearer " + token
if duration != "" {
d, err := time.ParseDuration(duration)
if err != nil {
return err
}
desiredExpirationTime = time.Now().Add(d).UTC().Format(time.RFC3339Nano)
}
if strings.ToLower(presence) == "offline" {
activity = "OffWork"
}
bReq, err := json.Marshal(&request{
Availability: presence,
Activity: activity,
DesiredExpirationTime: desiredExpirationTime,
})
if err != nil {
return fmt.Errorf("json marshal failed: %s", err)
}
req, _ := http.NewRequest("PUT", StatusURL, bytes.NewReader(bReq))
req.Header.Add("Authorization", bearer)
req.Header.Add("Content-Type", "application/json")
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
}
client := &http.Client{Transport: transport}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("http request failed: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error while reading the response bytes: %s", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("update failed: %s", string(body))
}
return nil
}
func SetPresenceDuration(presence, duration string) error {
token, err := readToken()
if err != nil {
return err
}
switch strings.ToLower(presence) {
case "dnd":
presence = "DoNotDisturb"
case "brb":
presence = "BeRightBack"
}
return setPresence(presence, duration, token)
}
func SetPresence(presence string) error {
return SetPresenceDuration(presence, "")
}