forked from beeper/standupbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standupbot.go
343 lines (305 loc) · 11.2 KB
/
standupbot.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
package main
import (
"database/sql"
"encoding/json"
"flag"
"io"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/kyoh86/xdg"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"maunium.net/go/mautrix"
mcrypto "maunium.net/go/mautrix/crypto"
mevent "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
mid "maunium.net/go/mautrix/id"
"github.com/beeper/standupbot/store"
"github.com/beeper/standupbot/types"
)
var client *mautrix.Client
var configuration Configuration
var olmMachine *mcrypto.OlmMachine
var stateStore *store.StateStore
var VERSION = "0.4.1"
func main() {
// Arg parsing
configPath := flag.String("config", "./config.json", "config file location")
logLevelStr := flag.String("loglevel", "debug", "the log level")
logFilename := flag.String("logfile", "", "the log file to use (defaults to '' meaning no log file)")
flag.Parse()
// Configure logging
if *logFilename != "" {
logFile, err := os.OpenFile(*logFilename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err == nil {
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
} else {
log.Errorf("Failed to open logging file; using default stderr: %s", err)
}
}
log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.DebugLevel)
logLevel, err := log.ParseLevel(*logLevelStr)
if err == nil {
log.SetLevel(logLevel)
} else {
log.Errorf("Invalid loglevel '%s'. Using default 'debug'.", logLevel)
}
log.Info("standupbot starting...")
// Load configuration
log.Infof("reading config from %s...", *configPath)
configJson, err := os.ReadFile(*configPath)
if err != nil {
log.Fatalf("Could not read config from %s: %s", *configPath, err)
}
err = json.Unmarshal(configJson, &configuration)
username := mid.UserID(configuration.Username)
// Create directory /test
dataDir := xdg.DataHome() + "/standupbot"
if err = os.MkdirAll(dataDir, os.ModePerm); err != nil {
log.Fatalf("Could not create data directory %s: %s", dataDir, err)
}
// Open the config database
db, err := sql.Open("sqlite3", dataDir+"/standupbot.db")
if err != nil {
log.Fatal("Could not open standupbot database.")
}
currentStandupFlowsJson, err := os.ReadFile(dataDir + "/current-flows.json")
if err != nil {
log.Warn("Couldn't open the current-flows JSON.")
} else {
err = json.Unmarshal(currentStandupFlowsJson, ¤tStandupFlows)
if err != nil {
log.Warnf("Failed to unmarshal the current flows JSON: %+v", err)
} else {
log.Info("Loaded current flows from disk.")
}
}
// Make sure to exit cleanly
c := make(chan os.Signal, 1)
signal.Notify(c,
os.Interrupt,
os.Kill,
syscall.SIGABRT,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
go func() {
for range c { // when the process is killed
log.Info("Cleaning up")
db.Close()
bytes, err := json.Marshal(currentStandupFlows)
if err != nil {
log.Error("Failed to serialize current standup flows!")
} else {
currentStandupFlowsFile, err := os.OpenFile(dataDir+"/current-flows.json", os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Error("Failed to open current standup flows JSON file!")
} else {
_, err = currentStandupFlowsFile.Write(bytes)
if err != nil {
log.Error("Failed to write current standup flows JSON to file!")
} else {
log.Info("Saved current flows to disk.")
}
}
currentStandupFlowsFile.Close()
}
os.Exit(0)
}
}()
stateStore = store.NewStateStore(db)
if err := stateStore.CreateTables(); err != nil {
log.Fatalf("Failed to create the tables for standupbot: %v", err)
}
// login to homeserver
log.Info("Logging in")
password, err := configuration.GetPassword()
if err != nil {
log.Fatalf("Could not read password from %s", configuration.PasswordFile)
}
deviceID := FindDeviceID(db, username.String())
if len(deviceID) > 0 {
log.Info("Found existing device ID in database:", deviceID)
}
client, err = mautrix.NewClient(configuration.Homeserver, "", "")
if err != nil {
panic(err)
}
_, err = DoRetry("login", func() (interface{}, error) {
return client.Login(&mautrix.ReqLogin{
Type: mautrix.AuthTypePassword,
Identifier: mautrix.UserIdentifier{
Type: mautrix.IdentifierTypeUser,
User: username.String(),
},
Password: password,
InitialDeviceDisplayName: "standupbot",
DeviceID: deviceID,
StoreCredentials: true,
})
})
if err != nil {
log.Fatalf("Couldn't login to the homeserver.")
}
log.Infof("Logged in as %s/%s", client.UserID, client.DeviceID)
// set the client store on the client.
client.Store = stateStore
stateStore.Client = client
// Load state from all of the rooms that we are joined to in case the
// database died.
log.Info("Loading state from joined rooms...")
joinedRooms, err := client.JoinedRooms()
if err == nil {
for _, roomID := range joinedRooms.JoinedRooms {
members, err := client.Members(roomID)
potentialUsers := make([]mid.UserID, 0)
if err != nil {
continue
}
for _, membershipEvent := range members.Chunk {
potentialUsers = append(potentialUsers, membershipEvent.Sender)
}
for _, userID := range potentialUsers {
stateKey := strings.TrimPrefix(userID.String(), "@")
var tzSettingEventContent types.TzSettingEventContent
if err := client.StateEvent(roomID, types.StateTzSetting, stateKey, &tzSettingEventContent); err == nil {
if location, err := time.LoadLocation(tzSettingEventContent.TzString); err == nil {
log.Infof("Loaded timezone (%s) for %s from state", location, userID)
stateStore.SetConfigRoom(userID, roomID)
stateStore.SetTimezone(userID, location.String())
}
}
var notifyEventContent types.NotifyEventContent
if err := client.StateEvent(roomID, types.StateNotify, stateKey, ¬ifyEventContent); err == nil {
if notifyEventContent.MinutesAfterMidnight != nil {
log.Infof("Loaded notification minutes after midnight (%d) for %s from state", *notifyEventContent.MinutesAfterMidnight, userID)
stateStore.SetConfigRoom(userID, roomID)
stateStore.SetNotify(userID, *notifyEventContent.MinutesAfterMidnight)
}
}
var sendRoomEventContent types.SendRoomEventContent
if err := client.StateEvent(roomID, types.StateSendRoom, stateKey, &sendRoomEventContent); err == nil {
log.Infof("Loaded send room (%s) for %s from state", sendRoomEventContent.SendRoomID, userID)
stateStore.SetConfigRoom(userID, roomID)
stateStore.SetSendRoomId(userID, sendRoomEventContent.SendRoomID)
}
var useThreadsEventContent types.UseThreadsEventContent
if err := client.StateEvent(roomID, types.StateUseThreads, stateKey, &useThreadsEventContent); err == nil {
log.Infof("Loaded thread usage setting (%t) for %s from state", useThreadsEventContent.UseThreads, userID)
stateStore.SetConfigRoom(userID, roomID)
stateStore.SetUseThreads(userID, useThreadsEventContent.UseThreads)
}
}
}
}
log.Info("Finished loading state from joined rooms")
// Setup the crypto store
sqlCryptoStore := mcrypto.NewSQLCryptoStore(
db,
"sqlite3",
username.String(),
client.DeviceID,
[]byte("standupbot_cryptostore_key"),
CryptoLogger{},
)
err = sqlCryptoStore.CreateTables()
if err != nil {
log.Fatal("Could not create tables for the SQL crypto store.")
}
olmMachine = mcrypto.NewOlmMachine(client, &CryptoLogger{}, sqlCryptoStore, stateStore)
err = olmMachine.Load()
if err != nil {
log.Errorf("Could not initialize encryption support. Encrypted rooms will not work.")
}
syncer := client.Syncer.(*mautrix.DefaultSyncer)
// Hook up the OlmMachine into the Matrix client so it receives e2ee
// keys and other such things.
syncer.OnSync(func(resp *mautrix.RespSync, since string) bool {
olmMachine.ProcessSyncResponse(resp, since)
return true
})
syncer.OnEventType(mevent.StateMember, func(_ mautrix.EventSource, event *mevent.Event) {
olmMachine.HandleMemberEvent(event)
stateStore.SetMembership(event)
if event.GetStateKey() == username.String() && event.Content.AsMember().Membership == mevent.MembershipInvite {
log.Info("Joining ", event.RoomID)
_, err := DoRetry("join room", func() (interface{}, error) {
return client.JoinRoomByID(event.RoomID)
})
if err != nil {
log.Errorf("Could not join channel %s. Error %+v", event.RoomID.String(), err)
} else {
log.Infof("Joined %s sucessfully", event.RoomID.String())
}
} else if event.GetStateKey() == username.String() && event.Content.AsMember().Membership.IsLeaveOrBan() {
log.Infof("Left or banned from %s", event.RoomID)
}
})
syncer.OnEventType(mevent.StateEncryption, func(_ mautrix.EventSource, event *mevent.Event) {
stateStore.SetEncryptionEvent(event)
})
syncer.OnEventType(mevent.EventReaction, func(source mautrix.EventSource, event *mevent.Event) { go HandleReaction(source, event) })
syncer.OnEventType(mevent.EventMessage, func(source mautrix.EventSource, event *mevent.Event) { go HandleMessage(source, event) })
syncer.OnEventType(mevent.EventRedaction, func(source mautrix.EventSource, event *mevent.Event) { go HandleRedaction(source, event) })
syncer.OnEventType(mevent.EventEncrypted, func(source mautrix.EventSource, event *mevent.Event) {
decryptedEvent, err := olmMachine.DecryptMegolmEvent(event)
if err != nil {
log.Errorf("Failed to decrypt message from %s in %s: %+v", event.Sender, event.RoomID, err)
} else {
log.Debugf("Received encrypted event from %s in %s", event.Sender, event.RoomID)
if decryptedEvent.Type == mevent.EventMessage {
go HandleMessage(source, decryptedEvent)
} else if decryptedEvent.Type == mevent.EventReaction {
go HandleReaction(source, decryptedEvent)
} else if decryptedEvent.Type == mevent.EventRedaction {
go HandleRedaction(source, decryptedEvent)
}
}
})
// Notification loop
go func() {
log.Debugf("Starting notification loop")
for {
h, m, _ := time.Now().UTC().Clock()
currentMinutesAfterMidnight := h*60 + m
usersForCurrentMinute := stateStore.GetNotifyUsersForMinutesAfterUtcForToday()[currentMinutesAfterMidnight]
for userID, roomID := range usersForCurrentMinute {
log.Infof("Notifying %s", userID)
if currentFlow, found := currentStandupFlows[userID]; !found || currentFlow.State == FlowNotStarted || currentFlow.State == Sent {
SendMessage(roomID, &mevent.MessageEventContent{
MsgType: mevent.MsgText,
Body: "Time to write your standup post!",
})
currentStandupFlows[userID] = BlankStandupFlow()
go CreatePost(roomID, userID)
} else {
content := format.RenderMarkdown("Looks like you are already writing a standup post! If you want to start over, type `!standupbot new`", true, false)
SendMessage(roomID, &content)
}
}
// Sleep until the next minute comes around
time.Sleep(time.Duration(60-time.Now().Second()) * time.Second)
}
}()
for {
log.Debugf("Running sync...")
err = client.Sync()
if err != nil {
log.Errorf("Sync failed. %+v", err)
}
}
}
func FindDeviceID(db *sql.DB, accountID string) (deviceID mid.DeviceID) {
err := db.QueryRow("SELECT device_id FROM crypto_account WHERE account_id=$1", accountID).Scan(&deviceID)
if err != nil && err != sql.ErrNoRows {
log.Warnf("Failed to scan device ID: %v", err)
}
return
}