-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.go
444 lines (375 loc) · 13.3 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
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
443
444
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"errors"
"os"
"strings"
"sync"
"github.com/TheDiscordian/onebot/onelib"
"github.com/matrix-org/gomatrix"
)
const (
// NAME is same as filename, minus extension
NAME = "matrix"
// LONGNAME is what's presented to the user
LONGNAME = "Matrix"
// VERSION of the script
VERSION = "v0.0.0"
)
var (
// matrixHomeServer
matrixHomeServer string
// matrixAuthUser
matrixAuthUser string
// matrixAuthToken if blank, falls back onto pass
matrixAuthToken string
// matrixAuthPass
matrixAuthPass string
)
func loadConfig() {
matrixHomeServer = onelib.GetTextConfig(NAME, "home_server")
matrixAuthUser = onelib.GetTextConfig(NAME, "auth_user")
matrixAuthToken = onelib.GetTextConfig(NAME, "auth_token")
matrixAuthPass = onelib.GetTextConfig(NAME, "auth_pass")
}
// Matrix protocol structs. These should maybe be in their own library.
// RespUserStatus is the JSON response for https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
// https://github.com/matrix-org/gomatrix/pull/80
type RespUserStatus struct {
Presence string `json:"presence"`
StatusMsg string `json:"status_msg"`
lastActiveAgo int `json:"last_active_ago"`
currentlyActive bool `json:"currently_active"`
}
// GetStatus returns the status of the user from the specified MXID. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
// https://github.com/matrix-org/gomatrix/pull/80
func (cli *matrixClient) GetStatus(mxid string) (resp *RespUserStatus, err error) {
urlPath := cli.BuildURL("presence", mxid, "status")
err = cli.MakeRequest("GET", urlPath, nil, &resp)
return
}
// GetOwnStatus returns the user's status. See https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-presence-userid-status
// https://github.com/matrix-org/gomatrix/pull/80
func (cli *matrixClient) GetOwnStatus() (resp *RespUserStatus, err error) {
return cli.GetStatus(cli.UserID)
}
// SetStatus sets the user's status. See https://matrix.org/docs/spec/client_server/r0.6.0#put-matrix-client-r0-presence-userid-status
// https://github.com/matrix-org/gomatrix/pull/80
func (cli *matrixClient) SetStatus(presence, status string) (err error) {
urlPath := cli.BuildURL("presence", cli.UserID, "status")
s := struct {
Presence string `json:"presence"`
StatusMsg string `json:"status_msg"`
}{presence, status}
err = cli.MakeRequest("PUT", urlPath, &s, nil)
return
}
// MarkRead marks eventID in roomID as read, signifying the event, and all before it have been read. See https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-rooms-roomid-receipt-receipttype-eventid
// https://github.com/matrix-org/gomatrix/pull/81
func (cli *matrixClient) MarkRead(roomID, eventID string) error {
urlPath := cli.BuildURL("rooms", roomID, "receipt", "m.read", eventID)
return cli.MakeRequest("POST", urlPath, nil, nil)
}
type matrixProtocolMessage struct {
Format string `json:"format"`
Msgtype string `json:"msgtype"`
Body string `json:"body"`
FormattedBody string `json:"formatted_body"`
}
func (client *matrixClient) setAvatarToFile(fPath string) error {
if onelib.DefaultAvatar == "" {
return errors.New("no default avatar set")
}
f, err := os.Open(fPath)
if err != nil {
return err
}
fInfo, err := f.Stat()
if err != nil {
f.Close()
return err
}
var resp *gomatrix.RespMediaUpload
resp, err = client.UploadToContentRepo(f, "image/png", fInfo.Size()) // TODO don't assume png
f.Close()
if err != nil {
return err
}
onelib.Info.Println("Avatar set! ContentURI:", resp.ContentURI)
client.SetAvatarURL(resp.ContentURI)
return nil
}
// Load connects to Matrix, and sets up listeners. It's required for OneBot.
// TODO store rooms as a map of locations mapped by UID
func Load() onelib.Protocol {
loadConfig()
client, err := gomatrix.NewClient(matrixHomeServer, matrixAuthUser, matrixAuthToken)
if err != nil {
onelib.Error.Panicln(err)
}
if matrixAuthToken == "" {
if matrixAuthUser == "" {
panic("both matrixAuthPass and matrixAuthToken can't be blank.")
}
resp, err := client.Login(&gomatrix.ReqLogin{
Type: "m.login.password",
User: matrixAuthUser,
Password: matrixAuthPass,
})
if err != nil {
onelib.Error.Panicln(err)
}
onelib.SetTextConfig(NAME, "auth_token", resp.AccessToken)
onelib.Info.Println("Access token (saved):", resp.AccessToken)
client.SetCredentials(resp.UserID, resp.AccessToken)
}
syncer := client.Syncer.(*gomatrix.DefaultSyncer)
matrix := &Matrix{client: &matrixClient{Client: client}, prefix: onelib.DefaultPrefix, nickname: onelib.DefaultNickname, knownMembers: new(memberMap)}
matrix.knownMembers.mMap = make(map[onelib.UUID]*member, 1)
matrix.knownMembers.lock = new(sync.RWMutex)
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
if ev.Content["msgtype"] != nil && ev.Content["msgtype"].(string) == "m.text" {
msg := &matrixMessage{text: ev.Content["body"].(string)} // FIXME just because msgtype is m.text, doesn't mean body *absolutely* exists
if ev.Content["format"] != nil && ev.Content["format"].(string) == "org.matrix.custom.html" {
msg.formattedText = ev.Content["formatted_body"].(string) // FIXME just because format is org.matrix.custom.html, doesn't mean formatted_body *absolutely* exists
}
mc := &matrixClient{Client: client}
ml := &matrixLocation{Client: mc, uuid: onelib.UUID(ev.RoomID)}
var displayName string
tuser := matrix.knownMembers.Get(onelib.UUID(ev.Sender))
if tuser == nil {
resp, err := client.GetDisplayName(ev.Sender)
if err != nil {
onelib.Debug.Println("Error getting display name:", err)
displayName = ev.Sender
} else {
displayName = resp.DisplayName
}
matrix.knownMembers.Set(onelib.UUID(ev.Sender), &member{displayName: displayName})
} else {
displayName = tuser.displayName
}
sender := &matrixSender{uuid: onelib.UUID(ev.Sender), username: ev.Sender, displayName: displayName, location: ml}
matrix.recv(onelib.Message(msg), onelib.Sender(sender))
onelib.Debug.Printf("%s: %s\n", displayName, msg.Text())
} else {
onelib.Debug.Println("Message: ", ev)
}
err = matrix.client.MarkRead(ev.RoomID, ev.ID)
if err != nil {
onelib.Error.Println(err)
}
})
syncer.OnEventType("m.room.third_party_invite", func(ev *gomatrix.Event) {
onelib.Debug.Println("Third Party Invite: ", ev)
})
syncer.OnEventType("m.room.member", func(ev *gomatrix.Event) {
if ev.Content["membership"] != nil && ev.Content["membership"].(string) == "invite" {
_, err = client.JoinRoom(ev.RoomID, "", nil)
if err != nil {
onelib.Error.Println(err)
}
} else {
onelib.Debug.Println("Member: ", ev)
}
})
client.SetDisplayName(onelib.DefaultNickname)
// client.setAvatarToFile(onelib.DefaultAvatar) // TODO only do this if avatar hasn't been set yet
err = matrix.client.SetStatus("online", "Test status.")
/*_, err = matrix.client.SendStateEvent("<DM room ID>", "im.vector.user_status", matrixAuthUser, struct {
Status string `json:"status"`
}{"Test status"})*/
if err != nil {
onelib.Error.Println("Error setting presence:", err)
}
go matrix.handleconnections()
return onelib.Protocol(matrix)
}
type matrixMessage struct {
formattedText, text string
}
func (mm *matrixMessage) Mentioned() bool {
return strings.Contains(mm.formattedText, matrixAuthUser)
}
func (mm *matrixMessage) UUID() onelib.UUID {
onelib.Debug.Printf("[%s] Message UUIDs not yet supported.\n", NAME)
return onelib.UUID("")
}
func (mm *matrixMessage) Reaction() *onelib.Emoji {
onelib.Debug.Printf("[%s] Reactions not yet supported.\n", NAME)
return nil
}
func (mm *matrixMessage) Text() string {
return mm.text
}
func (mm *matrixMessage) FormattedText() string {
return mm.formattedText
}
func (mm *matrixMessage) StripPrefix(prefix string) onelib.Message {
if len(mm.text) > len(prefix) {
prefix = prefix + " "
}
return onelib.Message(&matrixMessage{text: strings.Replace(mm.text, prefix, "", 1), formattedText: strings.Replace(mm.formattedText, prefix, "", 1)})
}
func (mm *matrixMessage) Raw() []byte {
return []byte(mm.text)
}
type matrixSender struct {
displayName, username string
location *matrixLocation
uuid onelib.UUID
}
func (ms *matrixSender) Self() bool {
return ms.uuid == onelib.UUID(matrixAuthUser)
}
func (ms *matrixSender) DisplayName() string {
return ms.displayName
}
func (ms *matrixSender) Username() string {
return ms.username
}
func (ms *matrixSender) UUID() onelib.UUID {
return ms.uuid
}
func (ms *matrixSender) Location() onelib.Location {
return ms.location
}
func (ms *matrixSender) Protocol() string {
return NAME
}
func (ms *matrixSender) Send(msg onelib.Message) {
ms.location.Client.Send(ms.uuid, msg)
}
func (ms *matrixSender) SendText(text string) {
ms.location.Client.SendText(ms.uuid, text)
}
func (ms *matrixSender) SendFormattedText(text, formattedText string) {
ms.location.Client.SendFormattedText(ms.uuid, text, formattedText)
}
type matrixLocation struct {
Client *matrixClient // pointer to originating client
displayName, nickname, topic string
uuid onelib.UUID
}
func (ml *matrixLocation) DisplayName() string {
return ml.displayName
}
func (ml *matrixLocation) Nickname() string {
return ml.nickname
}
func (ml *matrixLocation) Topic() string {
return ml.topic
}
func (ml *matrixLocation) UUID() onelib.UUID {
return ml.uuid
}
func (ml *matrixLocation) Send(msg onelib.Message) {
ml.Client.Send(ml.uuid, msg)
}
func (ml *matrixLocation) SendText(text string) {
ml.Client.SendText(ml.uuid, text)
}
func (ml *matrixLocation) SendFormattedText(text, formattedText string) {
ml.Client.SendFormattedText(ml.uuid, text, formattedText)
}
func (ml *matrixLocation) Protocol() string {
return NAME
}
type matrixClient struct {
*gomatrix.Client
}
// Send sends a Message object to a location specified by to (usually a location or sender UUID).
func (mc *matrixClient) Send(to onelib.UUID, msg onelib.Message) {
// code here
}
// SendText sends text to a location specified by to (usually a location or sender UUID).
func (mc *matrixClient) SendText(to onelib.UUID, text string) {
_, err := mc.Client.SendText(string(to), text)
if err != nil {
onelib.Error.Println(err)
}
}
// SendFormattedText sends formatted text to a location specified by to (usually a location or sender UUID).
func (mc *matrixClient) SendFormattedText(to onelib.UUID, text, formattedText string) {
_, err := mc.SendMessageEvent(string(to), "m.room.message", &matrixProtocolMessage{Body: text, FormattedBody: formattedText, Format: "org.matrix.custom.html", Msgtype: "m.text"})
if err != nil {
onelib.Error.Println(err)
}
}
// member contains useful data about a possible sender that's not typically sent in a message
type member struct {
displayName string
}
type memberMap struct {
mMap map[onelib.UUID]*member
lock *sync.RWMutex
}
func (mm *memberMap) Get(uuid onelib.UUID) *member {
mm.lock.RLock()
mem := mm.mMap[uuid]
mm.lock.RUnlock()
return mem
}
func (mm *memberMap) Set(uuid onelib.UUID, mem *member) {
mm.lock.Lock()
mm.mMap[uuid] = mem
mm.lock.Unlock()
}
// Matrix is the Protocol object used for handling anything Matrix related.
type Matrix struct {
/*
Store useful data here such as connected rooms, admins, nickname, accepted prefixes, etc
*/
prefix string
nickname string
client *matrixClient
knownMembers *memberMap
}
// TODO finish this, only proof of concept right now
func (matrix *Matrix) handleconnections() {
for {
if err := matrix.client.Sync(); err != nil {
onelib.Debug.Println("Sync() returned ", err)
}
}
}
// Name returns the name of the plugin, usually the filename.
func (matrix *Matrix) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (matrix *Matrix) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (matrix *Matrix) Version() string {
return VERSION
}
// NewMessage should generate a message object from something
func (matrix *Matrix) NewMessage(raw []byte) onelib.Message {
return nil
}
// Send sends a Message object to a location specified by to (usually a location or sender UUID).
func (matrix *Matrix) Send(to onelib.UUID, msg onelib.Message) {
matrix.client.Send(to, msg)
}
// SendText sends text to a location specified by to (usually a location or sender UUID).
func (matrix *Matrix) SendText(to onelib.UUID, text string) {
matrix.client.SendText(to, text)
}
// SendFormattedText sends formatted text to a location specified by to (usually a location or sender UUID).
func (matrix *Matrix) SendFormattedText(to onelib.UUID, text, formattedText string) {
matrix.client.SendFormattedText(to, text, formattedText)
}
// GetUserDisplayName returns a user's display name from a UUID
//func (matrix *Matrix) GetUserDisplayName(uuid onelib.UUID) string
// recv should be called after you've recieved data and built a Message object
func (matrix *Matrix) recv(msg onelib.Message, sender onelib.Sender) {
if string(sender.UUID()) != matrixAuthUser {
onelib.ProcessMessage([]string{matrix.prefix}, msg, sender)
}
}
// Remove currently doesn't do anything.
func (matrix *Matrix) Remove() {
matrix.client.SetStatus("offline", "")
}