-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.go
442 lines (356 loc) · 10.9 KB
/
calendar.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
ics "github.com/arran4/golang-ical"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"golang.org/x/oauth2"
"golang.org/x/oauth2/microsoft"
)
const (
RFC3339Short = "2006-01-02T15:04:05"
StartEndTimeParse = "2006-01-02T15:04:05.0000000"
)
type Calendar struct {
ctx context.Context
conf *oauth2.Config
client *http.Client
displayName string
userName string
userMail string
valid bool
lastUpdated time.Time
}
type Attachment struct {
url string
mimeType string
}
func newCalendarHandler() *Calendar {
ctx := context.Background()
conf := &oauth2.Config{
ClientID: viper.GetString("client_id"),
ClientSecret: viper.GetString("secret"),
Scopes: []string{"offline_access", "user.read", "calendars.read"},
RedirectURL: viper.GetString("redirect_url"),
Endpoint: microsoft.AzureADEndpoint(viper.GetString("tenant")),
}
return &Calendar{
ctx: ctx,
conf: conf,
valid: false,
lastUpdated: time.Now(),
}
}
func (c *Calendar) getURL() string {
return c.conf.AuthCodeURL("state", oauth2.AccessTypeOffline)
}
func (c *Calendar) getRemoteData(url string) ([]byte, error) {
resp, err := c.client.Get(url)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return body, nil
}
func (c *Calendar) saveURLToFile(url string, attId string, fname string) error {
baseDir := viper.GetString("attachments_dir") + "/" + attId
os.MkdirAll(baseDir, os.ModePerm)
file, err := os.Create(baseDir + "/" + fname)
if err != nil {
return err
}
resp, err := c.client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
defer file.Close()
return nil
}
func (c *Calendar) handleToken(code string, cookieToken string) (string, error) {
var user map[string]interface{}
// Use the authorization code that is pushed to the redirect
// URL. Exchange will do the handshake to retrieve the
// initial access token. The HTTP Client returned by
// conf.Client will refresh the token as necessary.
tok, err := c.conf.Exchange(c.ctx, code)
if err != nil {
return "", err
}
c.client = c.conf.Client(c.ctx, tok)
body, err := c.getRemoteData("https://graph.microsoft.com/v1.0/me")
if err != nil {
return "", err
}
if err := json.Unmarshal(body, &user); err != nil {
return "", err
}
c.displayName = user["displayName"].(string)
c.userMail = user["userPrincipalName"].(string)
userName := c.userMail[0:strings.Index(c.userMail, "@")]
c.userName = userName
for k, v := range loggedUsers {
if v.userName == userName && k != cookieToken {
cookieToken = k
loggedUsers[k] = c
break
}
}
// TODO Should optimize this and avoid O(2n)
for k, v := range cachedUsers {
if userName == k {
delete(loggedUsers, cookieToken)
cookieToken = v
loggedUsers[v] = c
delete(cachedUsers, k)
break
}
}
c.valid = true
return cookieToken, nil
}
func (c *Calendar) shouldSkip(data map[string]interface{}) bool {
rspStatus := data["responseStatus"].(map[string]interface{})
if rspStatus["response"].(string) != "accepted" && rspStatus["response"].(string) != "organizer" && rspStatus["response"].(string) != "none" {
return true
}
if data["isAllDay"].(bool) {
return true
}
if data["showAs"].(string) != "busy" {
return true
}
return false
}
func (c *Calendar) handleAttachments(baseHost, id string, hasAttachments bool) ([]*Attachment, error) {
if !hasAttachments {
return nil, nil
}
var attData map[string]interface{}
var attachments []*Attachment
baseUrl := "https://graph.microsoft.com/v1.0/me/events/" + id + "/attachments"
body, err := c.getRemoteData(baseUrl)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &attData); err != nil {
return nil, err
}
if _, ok := attData["value"]; !ok {
return attachments, nil
}
values := attData["value"].([]interface{})
for _, v := range values {
data := v.(map[string]interface{})
attId := data["id"].(string)
name := data["name"].(string)
var contentType string
if val, ok := data["contentType"]; ok && val != nil {
contentType = val.(string)
} else {
contentType = "application/octet-stream"
}
if attCache := cachedData.attachmentExists(attId); attCache != nil {
attachments = append(attachments, &Attachment{
url: "https://" + baseHost + "/attachment/" + attId + "/" + url.PathEscape(attCache[0]),
mimeType: attCache[1],
})
continue
}
go func() {
baseUrl := "https://graph.microsoft.com/v1.0/me/events/" + id + "/attachments/" + attId + "/$value"
if err := c.saveURLToFile(baseUrl, attId, name); err != nil {
log.Error().
Err(err).
Str("Attachment ID", attId).
Str("File name", name).
Msg("Error saving file to disk")
}
}()
if err = cachedData.saveAttachment(attId, name, contentType); err != nil {
return nil, err
}
attachments = append(attachments, &Attachment{
url: "https://" + baseHost + "/attachment/" + attId + "/" + url.PathEscape(name),
mimeType: contentType,
})
}
return attachments, nil
}
func (c *Calendar) handleBasicEventData(cal *ics.Calendar, data map[string]interface{}) *ics.VEvent {
event := cal.AddEvent(data["id"].(string))
event.SetDtStampTime(time.Now())
t, _ := time.Parse(time.RFC3339, data["createdDateTime"].(string))
event.SetCreatedTime(t)
t, _ = time.Parse(time.RFC3339, data["lastModifiedDateTime"].(string))
event.SetModifiedAt(t)
ts, _ := time.Parse(StartEndTimeParse, data["start"].(map[string]interface{})["dateTime"].(string))
te, _ := time.Parse(StartEndTimeParse, data["end"].(map[string]interface{})["dateTime"].(string))
if data["isAllDay"].(bool) {
event.SetAllDayStartAt(ts)
event.SetAllDayEndAt(te)
} else {
event.SetStartAt(ts)
event.SetEndAt(te)
}
event.SetSummary(data["subject"].(string))
event.SetLocation(data["location"].(map[string]interface{})["displayName"].(string))
if rsp, ok := data["responseStatus"]; ok {
rspValue := rsp.(map[string]interface{})["response"].(string)
if rspValue != "accepted" && rspValue != "organizer" {
event.SetStatus(ics.ObjectStatusTentative)
}
} else {
event.SetStatus(ics.ObjectStatusTentative)
}
return event
}
func (c *Calendar) handleDescription(event *ics.VEvent, data map[string]interface{}, atts []*Attachment) {
link := strings.TrimSpace(parseTeamsLink(data["body"].(map[string]interface{})["content"].(string), data["onlineMeeting"]))
if link != "" {
event.SetURL(link)
}
var attString strings.Builder
for i, v := range atts {
if i > 0 {
attString.WriteString("\n\n")
}
attString.WriteString("Attachment ")
attString.WriteString("(")
attString.WriteString(strconv.Itoa(i + 1))
attString.WriteString("): ")
attString.WriteString(v.url)
}
description, err := html2text(data["body"].(map[string]interface{})["content"].(string))
if err == nil && description != "" {
var dscString strings.Builder
dscString.WriteString(description)
if attString.Len() > 0 {
dscString.WriteString("\n\n")
dscString.WriteString(attString.String())
}
if link != "" {
dscString.WriteString("\n\n")
dscString.WriteString(link)
}
event.SetDescription(dscString.String())
}
}
func (c *Calendar) handleAttendees(event *ics.VEvent, data map[string]interface{}, google bool) {
organizer := data["organizer"].(map[string]interface{})
organizerMail := organizer["emailAddress"].(map[string]interface{})["address"].(string)
organizerName := organizer["emailAddress"].(map[string]interface{})["name"].(string)
event.SetOrganizer(organizerMail, ics.WithCN(organizerName))
event.AddAttendee(organizerMail, ics.ParticipationRoleChair, ics.ParticipationStatusAccepted, ics.WithCN(organizerName))
// Google can't handle big lists of invitees (>5 I guess), and don't display them either way
if !google {
attendees := data["attendees"].([]interface{})
for _, att := range attendees {
var props []ics.PropertyParameter
castAtt := att.(map[string]interface{})
typ := castAtt["type"].(string)
resp := castAtt["status"].(map[string]interface{})["response"].(string)
name := castAtt["emailAddress"].(map[string]interface{})["name"].(string)
email := castAtt["emailAddress"].(map[string]interface{})["address"].(string)
if typ == "required" {
props = append(props, ics.ParticipationRoleReqParticipant)
} else {
props = append(props, ics.ParticipationRoleOptParticipant)
}
switch resp {
case "accepted":
props = append(props, ics.ParticipationStatusAccepted)
case "tentative":
props = append(props, ics.ParticipationStatusTentative)
case "declined":
props = append(props, ics.ParticipationStatusDeclined)
default:
props = append(props, ics.ParticipationStatusNeedsAction)
}
props = append(props, ics.WithCN(name))
event.AddAttendee(email, props...)
}
}
}
func (c *Calendar) getCalendar(baseHost string, full bool, google bool) (string, error) {
var calData map[string]interface{}
var cacheRetrieved bool
start, end := getStartEndWeekDays()
url := "https://graph.microsoft.com/v1.0/me/calendarview?startdatetime=" + start.Format(RFC3339Short) + "&enddatetime=" + end.Format(RFC3339Short) + "&top=10&skip=0"
body, err := c.getRemoteData(url)
if err != nil {
return "", err
}
if err := json.Unmarshal(body, &calData); err != nil {
return "", err
}
cal := ics.NewCalendar()
cal.SetMethod(ics.MethodRequest)
cal.SetCalscale("GREGORIAN")
cal.SetXWRTimezone("UTC")
for {
values := calData["value"].([]interface{})
for _, v := range values {
data := v.(map[string]interface{})
if !full && c.shouldSkip(data) {
continue
}
event := c.handleBasicEventData(cal, data)
// Google only supports attachments that are hosted on Drive
var atts []*Attachment
if !google {
atts, err = c.handleAttachments(baseHost, data["id"].(string), data["hasAttachments"].(bool))
if err != nil {
return "", err
}
for _, v := range atts {
event.AddAttachmentURL(v.url, v.mimeType)
}
}
c.handleDescription(event, data, atts)
c.handleAttendees(event, data, google)
}
if nextPage, ok := calData["@odata.nextLink"].(string); ok {
calData = make(map[string]interface{})
body, err := c.getRemoteData(nextPage)
if err != nil {
return "", err
}
if err := json.Unmarshal(body, &calData); err != nil {
return "", err
}
} else if !cacheRetrieved {
cacheRetrieved = true
startMonth, endMonth := getMonthAfterStartEndWeekDays()
userCache, err := cachedData.getUserCache(c.userName, startMonth, endMonth)
if err != nil {
log.Warn().
Err(err).
Str("user", c.userName).
Str("method", "getUserCache").
Send()
break
}
calData["value"] = userCache
} else {
break
}
}
return string(cal.Serialize()), nil
}