-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.go
435 lines (357 loc) · 10.4 KB
/
commands.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
package main
import (
"fmt"
"strconv"
"strings"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
type cmdReply struct {
msg string
msgF string
}
func handleCommand(cli *mautrix.Client, data *store, ev *event.Event) (replies []cmdReply) {
start := time.Now()
defer func() {
fmt.Println(time.Since(start))
}()
var err error
str := strings.TrimSpace(ev.Content.AsMessage().Body)
str = strings.ToLower(str)
args := strings.Split(str, " ")
if len(args) < 0 {
return
}
ud, err := data.user(ev.Sender)
if err != nil {
fmt.Println(err)
replies = append(replies, cmdReply{
"Oops, something went wrong", ""})
return
}
if !ud.ExistsInDB() {
fmt.Println("Storing room")
err = ud.store(ev.RoomID)
if err != nil {
fmt.Println(err)
replies = append(replies, cmdReply{
"Oops, something went wrong", ""})
}
}
if ev.RoomID != ud.roomID {
replies = append(replies, cmdReply{
"This is not the room we normally use. Please go to: " + string(ud.roomID), ""})
}
var reply cmdReply
switch args[0] {
case "events", "week":
yearNum := 0
weekNum := 0
var yearErr error
var weekErr error
if len(args) >= 2 {
if len(args) >= 3 {
yearNum, yearErr = strconv.Atoi(args[1])
weekNum, weekErr = strconv.Atoi(args[2])
} else {
weekNum, weekErr = strconv.Atoi(args[1])
}
}
if yearErr != nil || weekErr != nil {
if yearErr != nil {
replies = append(replies, cmdReply{"Invalid year specified", ""})
}
if weekErr != nil {
replies = append(replies, cmdReply{"Invalid week specified", ""})
}
break
}
reply, err = cmdListEvents(ud, "week", yearNum, weekNum)
case "today":
reply, err = cmdListEvents(ud, "today", 0, 0)
case "next":
if len(args) < 2 {
reply = cmdReply{"Next what? 'next week'?", ""}
break
}
switch args[1] {
case "week":
reply, err = cmdListEvents(ud, "nextweek", 0, 0)
}
case "last", "prev", "previous":
if len(args) < 2 {
reply = cmdReply{"Last what? 'last week'?", ""}
break
}
switch args[1] {
case "week":
reply, err = cmdListEvents(ud, "lastweek", 0, 0)
}
case "cal", "calendar":
if len(args) < 2 {
reply = cmdCalendarList(ud)
break
}
switch args[1] {
case "add":
reply, err = cmdCalendarAdd(ud, args)
case "list":
reply = cmdCalendarList(ud)
case "remove":
reply, err = cmdCalendarRemove(ud, args)
default:
replies = append(replies, cmdReply{
"Unknown option", ""})
reply = formatHelp(helpCal)
}
case "help", "?":
reply = formatAllHelp()
default:
replies = append(replies, cmdReply{"Unknown command", ""})
reply = formatAllHelp()
}
if reply.msg != "" {
replies = append(replies, reply)
}
if err != nil {
replies = append(replies, cmdReply{
"Oops, something went wrong", ""})
fmt.Println(err)
}
return
}
func cmdListEvents(u *user, period string, year int, week int) (cmdReply, error) {
cal, err := u.combinedCalendar()
if err != nil {
return cmdReply{}, err
}
now := time.Now()
from := time.Time{}
to := time.Time{}
loc := now.Location() // TODO: loc should be depending on user.
daysFromToTo := 7
switch period {
case "today":
from = timeStartOfToday(now, loc)
fmt.Println(from)
daysFromToTo = 1
case "nextweek":
from = timeStartOfWeek(now, loc).AddDate(0, 0, 7)
case "lastweek":
from = timeStartOfWeek(now, loc).AddDate(0, 0, -7)
default:
if week == 0 {
from = timeStartOfWeek(now, loc)
} else {
if year == 0 {
year = now.Year()
}
from = timeStartOfYearPlusWeeks(year, loc, week)
}
}
to = time.Date(from.Year(), from.Month(), from.Day()+daysFromToTo, 0, 0, 0, 0, loc)
fmt.Println(from, to)
events, err := cal.eventsBetween(from, to)
if err != nil {
if err == errNoCalendars {
return cmdReply{"You haven't configured any calendars. Use the 'cal add' command to start.", ""}, nil
}
return cmdReply{}, err
}
// TODO: Properly handle multi-day events.
lines := []string{}
linesF := []string{}
if strings.Contains(period, "week") {
_, week := from.ISOWeek()
wk := strconv.Itoa(week)
lines = append(lines, "Week "+wk, "")
linesF = append(linesF, "<b>Week "+wk+"</b>", "")
}
days := events.formatToDays()
for i, day := range days {
if to.Before(day.day) {
continue
}
if i > 0 {
lines = append(lines, "")
linesF = append(linesF, "")
}
header := day.day.Format("Monday 2 January")
lines = append(lines, fmt.Sprintf("%s", header))
linesF = append(linesF, fmt.Sprintf("<b>%s</b>", header))
for _, ev := range day.events {
lines = append(lines, fmt.Sprintf("%s - %s: %s", ev.from.Format("15:04"), ev.to.Format("15:04"), ev.text))
linesF = append(linesF, fmt.Sprintf("<code>%s - %s</code>: %s", ev.from.Format("15:04"), ev.to.Format("15:04"), ev.text))
}
}
return cmdReply{strings.Join(lines, "\n"), strings.Join(linesF, "<br />")}, nil
}
func timeStartOfToday(base time.Time, loc *time.Location) time.Time {
return time.Date(base.Year(), base.Month(), base.Day(), 0, 0, 0, 0, loc)
}
func timeStartOfWeek(base time.Time, loc *time.Location) time.Time {
return time.Date(base.Year(), base.Month(), base.Day()-int(base.Weekday())+1, 0, 0, 0, 0, loc)
}
func timeStartOfYearPlusWeeks(year int, loc *time.Location, weekNumber int) time.Time {
tm := time.Date(year, 1, 1, 0, 0, 0, 0, loc)
addDays := (weekNumber - 1) * 7
addDays += int(tm.Weekday()-time.Monday) * -1
tm = tm.AddDate(0, 0, addDays)
return tm
}
func cmdCalendarRemove(u *user, args []string) (cmdReply, error) {
if len(args) < 3 {
return formatUsage(usageCalRemove), nil
}
name := strings.ToLower(args[2])
err := u.removeCalendar(name)
if err != nil {
if err == errCalendarNotExists {
return cmdReply{
"There is no calendar named " + name,
"There is no calendar named <b>" + name + "</b>"}, nil
}
return cmdReply{}, err
}
return cmdReply{"Calendar " + name + " removed",
"Calendar <b>" + name + "</b> removed"}, nil
}
var replyNoCalendars = cmdReply{"You haven't configured any calendars. Use the 'cal add' command to start.", ""}
func cmdCalendarList(u *user) cmdReply {
lines := []string{}
linesF := []string{}
if len(u.calendars) == 0 {
return replyNoCalendars
}
u.calendarsMutex.RLock()
amount := len(u.calendars)
u.calendarsMutex.RUnlock()
if amount == 1 {
lines = append(lines, "You have one calendar")
linesF = append(linesF, "You have <b>one</b> calendar")
} else {
lines = append(lines, fmt.Sprintf("You have %d calendars", amount))
linesF = append(linesF, fmt.Sprintf("You have <b>%d</b> calendars", amount))
}
lines = append(lines, "")
linesF = append(linesF, "")
u.calendarsMutex.RLock()
for i, uc := range u.calendars {
if i > 0 {
lines = append(lines, "")
linesF = append(linesF, "")
}
lines = append(lines, uc.Name)
lines = append(lines, "type: "+string(uc.CalType))
linesF = append(linesF, fmt.Sprintf("<b>%s</b>", uc.Name))
linesF = append(linesF, "type: "+string(uc.CalType))
}
u.calendarsMutex.RUnlock()
return cmdReply{strings.Join(lines, "\n"), strings.Join(linesF, "<br />")}
}
func cmdCalendarAdd(u *user, args []string) (cmdReply, error) {
if len(args) < 5 {
return formatUsage(usageCalAdd), nil
}
name := strings.ToLower(args[2])
if u.hasCalendar(name) {
return cmdReply{
"You already have a calendar named " + name + ". Please choose a different name",
"You already have a calendar named <b>" + name + "</b>. Please choose a different name."}, nil
}
calTypeStr := strings.ToLower(args[3])
uri := args[4]
var calType calendarType
if calTypeStr == "caldav" {
calType = calendarTypeCalDav
_, err := newCalDavCalendar(uri)
if err != nil {
fmt.Println(err)
return cmdReply{"Specified address is not a supported CalDAV calendar", ""}, nil
}
} else if calTypeStr == "ical" {
calType = calendarTypeICal
_, err := newICalCalendar(uri)
if err != nil {
fmt.Println(err)
return cmdReply{"Specified address is not a supported ical calendar", ""}, nil
}
} else {
return cmdReply{"Invalid calendar type specified. Supported types are 'caldav' and 'ical'.", ""}, nil
}
return cmdReply{"Calendar added", ""}, u.addCalendar(name, calType, uri)
}
type helpSection struct {
title string
cmds []helpCommand
}
type helpCommand struct {
cmd string
info string
example string
}
var helpCal = helpSection{
"Managing your calendars",
[]helpCommand{
{"cal", "List your calendars", ""},
usageCalAdd,
usageCalRemove,
},
}
var helpView = helpSection{
"Viewing events in your calendars",
[]helpCommand{
{"week", "View your schedule for this week", ""},
{"week {number}", "View your schedule for the specified week", ""},
{"week {year} {number}", "View your schedule for the specified week", ""},
{"last week", "View your schedule for last week", ""},
{"next week", "View your schedule for next week", ""},
},
}
func formatAllHelp() cmdReply {
lines := []string{"Use these commands to interact with the bot", ""}
linesF := []string{"<b>Use these commands to interact with the bot</b>", ""}
for i, s := range []helpSection{helpCal, helpView} {
if i > 0 {
lines = append(lines, "")
linesF = append(linesF, "")
}
reply := formatHelp(s)
lines = append(lines, reply.msg)
linesF = append(linesF, reply.msgF)
}
return cmdReply{strings.Join(lines, "\n"), strings.Join(linesF, "<br />\n")}
}
func formatHelp(help helpSection) cmdReply {
lines := []string{}
linesF := []string{}
lines = append(lines, help.title)
linesF = append(linesF, "<b>"+help.title+"</b>")
for _, c := range help.cmds {
msg := fmt.Sprintf("* %s - %s", c.cmd, c.info)
msgF := fmt.Sprintf(" ◦ <code>%s</code> - %s", c.cmd, c.info)
lines = append(lines, msg)
linesF = append(linesF, msgF)
}
return cmdReply{strings.Join(lines, "\n"), strings.Join(linesF, "<br />\n")}
}
var usageCalAdd = helpCommand{
"cal add {name} {type} {address}",
"Add a calendar by choosing a name, and specifying the type (caldav or ical) and webaddress",
"cal add personal caldav https://mysite.nl/calendar/3owevfu1d0rb3psw",
}
var usageCalRemove = helpCommand{
"cal remove {name}",
"Remove the specified calendar from the bot",
"",
}
func formatUsage(usage helpCommand) cmdReply {
msg := fmt.Sprintf("Usage: %s\n%s", usage.cmd, usage.info)
msgF := fmt.Sprintf("<b>Usage</b>: %s<br />\n%s", usage.cmd, usage.info)
if usage.example != "" {
msg += "\n\nExample: " + usage.example
msgF += "<br />\n<br />\n<b>Example</b>: " + usage.example
}
return cmdReply{msg, msgF}
}