-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (77 loc) · 1.94 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
package main
import (
"flag"
"fmt"
"os"
"time"
)
func main() {
cfgFileName := flag.String("config", "config.json", "")
flag.Parse()
cfg, isNew, err := loadConfig(*cfgFileName)
if err != nil {
fmt.Printf("Could not read JSON from configuration file with name %s. "+
"Could not create it either.\nError message: %s.\n",
*cfgFileName, err.Error())
os.Exit(1)
return
}
if isNew {
fmt.Printf("Configuration file created. Saved as %s. Please look at it, fill in the values and rerun the application.\n", *cfgFileName)
os.Exit(2)
return
}
db, err := initSQLDB(cfg.SQLiteURI)
if err != nil {
fmt.Println("Error initialising database:", err)
os.Exit(4)
}
data := newDataStore(db)
fmt.Println("Reading database into memory...")
err = data.populateFromDB()
if err != nil {
fmt.Println(err)
}
fmt.Println("Initialising Matrix bot...")
m, err := initMatrixBot(cfg.MatrixBot, data)
if err != nil {
fmt.Println("Error initialising Matrix connection:", err)
os.Exit(3)
}
fmt.Println("Setting up reminder timers...")
if true {
setupReminderTimers(m, data)
}
fmt.Println("Done")
<-make(chan struct{})
}
func setupReminderTimers(m matrixBot, data *store) {
for _, user := range data.users {
send := func(ev *calendarEvent) {
msg := ""
timeUntil := ev.from.Sub(time.Now())
if timeUntil.Minutes() > 0 {
msg = fmt.Sprintf("Reminder: %q starts in %d minutes", ev.text, int(timeUntil.Minutes()))
} else {
msg = fmt.Sprintf("Reminder: %q starts now", ev.text)
}
m.sendMessage(user.roomID, msg, "")
}
go func() {
err := user.initialiseReminderTimer(send, 65*time.Minute)
if err != nil {
fmt.Println(err)
}
for {
<-time.After(60 * time.Minute)
fmt.Println("call setup reminder timers")
err = user.restartReminderTimer()
if err != nil {
fmt.Println(err)
}
fmt.Println("done call setup reminder timers")
}
}()
<-time.After(100 * time.Millisecond)
}
}