-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
matrix.go
352 lines (317 loc) · 9.29 KB
/
matrix.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
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/gomarkdown/markdown"
lm "github.com/hrfee/jfa-go/logmessages"
"github.com/timshannon/badgerhold/v4"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var (
DEVICE_ID = id.DeviceID("jfa-go")
)
type MatrixDaemon struct {
Stopped bool
bot *mautrix.Client
userID id.UserID
homeserver string
tokens map[string]UnverifiedUser // Map of tokens to users
languages map[id.RoomID]string // Map of roomIDs to language codes
Encryption bool
crypto *Crypto
app *appContext
start int64
cancellation sync.WaitGroup
cancel context.CancelFunc
}
type UnverifiedUser struct {
Verified bool
User *MatrixUser
}
var matrixFilter = mautrix.Filter{
Room: mautrix.RoomFilter{
Timeline: mautrix.FilterPart{
Types: []event.Type{
event.EventMessage,
event.EventEncrypted,
event.StateMember,
},
},
},
EventFields: []string{
"type",
"event_id",
"room_id",
"state_key",
"sender",
"content",
"timestamp",
// "content.body",
// "content.membership",
},
}
func (d *MatrixDaemon) renderUserID(uid id.UserID) id.UserID {
if uid[0] != '@' {
uid = "@" + uid
}
if !strings.ContainsRune(string(uid), ':') {
uid = id.UserID(string(uid) + ":" + d.homeserver)
}
return uid
}
func newMatrixDaemon(app *appContext) (d *MatrixDaemon, err error) {
matrix := app.config.Section("matrix")
token := matrix.Key("token").String()
d = &MatrixDaemon{
userID: id.UserID(matrix.Key("user_id").String()),
homeserver: matrix.Key("homeserver").String(),
tokens: map[string]UnverifiedUser{},
languages: map[id.RoomID]string{},
app: app,
start: time.Now().UnixNano() / 1e6,
}
d.userID = d.renderUserID(d.userID)
d.bot, err = mautrix.NewClient(d.homeserver, d.userID, token)
if err != nil {
return
}
d.bot.DeviceID = DEVICE_ID
// resp, err := d.bot.CreateFilter(&matrixFilter)
// if err != nil {
// return
// }
// d.bot.Store.SaveFilterID(d.userID, resp.FilterID)
for _, user := range app.storage.GetMatrix() {
if user.Lang != "" {
d.languages[id.RoomID(user.RoomID)] = user.Lang
}
}
err = InitMatrixCrypto(d)
return
}
// SetTransport sets the http.Transport to use for requests. Can be used to set a proxy.
func (d *MatrixDaemon) SetTransport(t *http.Transport) {
d.bot.Client.Transport = t
}
func (d *MatrixDaemon) generateAccessToken(homeserver, username, password string) (string, error) {
req := &mautrix.ReqLogin{
Type: mautrix.AuthTypePassword,
Identifier: mautrix.UserIdentifier{
Type: mautrix.IdentifierTypeUser,
User: username,
},
Password: password,
DeviceID: DEVICE_ID,
}
bot, err := mautrix.NewClient(homeserver, id.UserID(username), "")
if err != nil {
return "", err
}
resp, err := bot.Login(context.TODO(), req)
if err != nil {
return "", err
}
return resp.AccessToken, nil
}
func (d *MatrixDaemon) run() {
syncer := d.bot.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, d.handleMessage)
d.app.info.Printf(lm.StartDaemon, lm.Matrix)
var syncCtx context.Context
syncCtx, d.cancel = context.WithCancel(context.Background())
d.cancellation.Add(1)
if err := d.bot.SyncWithContext(syncCtx); err != nil && !errors.Is(err, context.Canceled) {
d.app.err.Printf(lm.FailedSyncMatrix, err)
}
d.cancellation.Done()
}
func (d *MatrixDaemon) Shutdown() {
d.cancel()
d.cancellation.Wait()
d.Stopped = true
}
func (d *MatrixDaemon) handleMessage(ctx context.Context, evt *event.Event) {
if evt.Timestamp < d.start {
return
}
if evt.Sender == d.userID {
return
}
lang := "en-us"
if l, ok := d.languages[evt.RoomID]; ok {
if _, ok := d.app.storage.lang.Telegram[l]; ok {
lang = l
}
}
sects := strings.Split(evt.Content.Raw["body"].(string), " ")
switch sects[0] {
case "!lang":
if len(sects) == 2 {
d.commandLang(evt, sects[1], lang)
} else {
d.commandLang(evt, "", lang)
}
}
}
func (d *MatrixDaemon) commandLang(evt *event.Event, code, lang string) {
if code == "" {
list := "!lang <lang>\n"
for c := range d.app.storage.lang.Telegram {
list += fmt.Sprintf("%s: %s\n", c, d.app.storage.lang.Telegram[c].Meta.Name)
}
_, err := d.bot.SendText(
context.TODO(),
evt.RoomID,
list,
)
if err != nil {
d.app.err.Printf(lm.FailedReply, lm.Matrix, evt.Sender, err)
}
return
}
if _, ok := d.app.storage.lang.Telegram[code]; !ok {
return
}
d.languages[evt.RoomID] = code
if u, ok := d.app.storage.GetMatrixKey(string(evt.RoomID)); ok {
u.Lang = code
d.app.storage.SetMatrixKey(string(evt.RoomID), u)
}
}
func (d *MatrixDaemon) CreateRoom(userID string) (roomID id.RoomID, err error) {
var room *mautrix.RespCreateRoom
room, err = d.bot.CreateRoom(context.TODO(), &mautrix.ReqCreateRoom{
Visibility: "private",
Invite: []id.UserID{id.UserID(userID)},
Topic: d.app.config.Section("matrix").Key("topic").String(),
IsDirect: true,
})
if err != nil {
return
}
// encrypted = EncryptRoom(d, room, id.UserID(userID))
roomID = room.RoomID
err = EncryptRoom(d, roomID)
return
}
func (d *MatrixDaemon) SendStart(userID string) (ok bool) {
roomID, err := d.CreateRoom(userID)
if err != nil {
d.app.err.Printf(lm.FailedCreateMatrixRoom, userID, err)
return
}
lang := "en-us"
pin := genAuthToken()
d.tokens[pin] = UnverifiedUser{
false,
&MatrixUser{
RoomID: string(roomID),
UserID: userID,
Lang: lang,
},
}
err = d.sendToRoom(
&event.MessageEventContent{
MsgType: event.MsgText,
Body: d.app.storage.lang.Telegram[lang].Strings.get("matrixStartMessage") + "\n\n" + pin + "\n\n" +
d.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "!lang"}),
},
roomID,
)
if err != nil {
d.app.err.Printf(lm.FailedMessage, lm.Matrix, userID, err)
return
}
ok = true
return
}
func (d *MatrixDaemon) sendToRoom(content *event.MessageEventContent, roomID id.RoomID) (err error) {
return d.send(content, roomID)
/*if encrypted, ok := d.isEncrypted[roomID]; ok && encrypted {
err = SendEncrypted(d, content, roomID)
} else {
_, err = d.bot.SendMessageEvent(context.TODO(), roomID, event.EventMessage, content, mautrix.ReqSendEvent{})
}
return*/
}
func (d *MatrixDaemon) send(content *event.MessageEventContent, roomID id.RoomID) (err error) {
_, err = d.bot.SendMessageEvent(context.TODO(), roomID, event.EventMessage, content, mautrix.ReqSendEvent{})
return
}
func (d *MatrixDaemon) Send(message *Message, users ...MatrixUser) (err error) {
md := ""
if message.Markdown != "" {
// Convert images to links
md = string(markdown.ToHTML([]byte(strings.ReplaceAll(message.Markdown, "![", "[")), nil, markdownRenderer))
}
content := &event.MessageEventContent{
MsgType: "m.text",
Body: message.Text,
}
if md != "" {
content.FormattedBody = md
content.Format = "org.matrix.custom.html"
}
for _, user := range users {
err = d.sendToRoom(content, id.RoomID(user.RoomID))
if err != nil {
return
}
}
return
}
// UserExists returns whether or not a user with the given User ID exists.
func (d *MatrixDaemon) UserExists(userID string) bool {
c, err := d.app.storage.db.Count(&MatrixUser{}, badgerhold.Where("UserID").Eq(userID))
return err != nil || c > 0
}
// Exists returns whether or not the given user exists.
func (d *MatrixDaemon) Exists(user ContactMethodUser) bool {
return d.UserExists(user.Name())
}
// User enters ID on sign-up, a PIN is sent to them. They enter it on sign-up.
// Message the user first, to avoid E2EE by default
func (d *MatrixDaemon) PIN(req newUserDTO) string { return req.MatrixPIN }
func (d *MatrixDaemon) Name() string { return lm.Matrix }
func (d *MatrixDaemon) Required() bool {
return d.app.config.Section("telegram").Key("required").MustBool(false)
}
func (d *MatrixDaemon) UniqueRequired() bool {
return d.app.config.Section("telegram").Key("require_unique").MustBool(false)
}
// TokenVerified returns whether or not a token with the given PIN has been verified, and the token itself.
func (d *MatrixDaemon) TokenVerified(pin string) (token UnverifiedUser, ok bool) {
token, ok = d.tokens[pin]
// delete(t.verifiedTokens, pin)
return
}
// DeleteVerifiedToken removes the token with the given PIN.
func (d *MatrixDaemon) DeleteVerifiedToken(PIN string) {
delete(d.tokens, PIN)
}
func (d *MatrixDaemon) UserVerified(PIN string) (ContactMethodUser, bool) {
token, ok := d.TokenVerified(PIN)
if !ok {
return &MatrixUser{}, false
}
return token.User, ok
}
func (d *MatrixDaemon) PostVerificationTasks(string, ContactMethodUser) error { return nil }
func (m *MatrixUser) Name() string { return m.UserID }
func (m *MatrixUser) SetMethodID(id any) { m.UserID = id.(string) }
func (m *MatrixUser) MethodID() any { return m.UserID }
func (m *MatrixUser) SetJellyfin(id string) { m.JellyfinID = id }
func (m *MatrixUser) Jellyfin() string { return m.JellyfinID }
func (m *MatrixUser) SetAllowContactFromDTO(req newUserDTO) { m.Contact = req.MatrixContact }
func (m *MatrixUser) SetAllowContact(contact bool) { m.Contact = contact }
func (m *MatrixUser) AllowContact() bool { return m.Contact }
func (m *MatrixUser) Store(st *Storage) {
st.SetMatrixKey(m.Jellyfin(), *m)
}