-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
commands.go
529 lines (491 loc) · 16.2 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// mautrix-gmessages - A Matrix-Google Messages puppeting bridge.
// Copyright (C) 2023 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/skip2/go-qrcode"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/bridge/commands"
"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-gmessages/libgm"
"go.mau.fi/mautrix-gmessages/libgm/gmproto"
)
type WrappedCommandEvent struct {
*commands.Event
Bridge *GMBridge
User *User
Portal *Portal
}
func (br *GMBridge) RegisterCommands() {
proc := br.CommandProcessor.(*commands.Processor)
proc.AddHandlers(
cmdLoginQR,
cmdLoginGoogle,
cmdDeleteSession,
cmdLogout,
cmdReconnect,
cmdDisconnect,
cmdSetActive,
cmdPing,
cmdPM,
cmdDeletePortal,
cmdDeleteAllPortals,
)
}
func wrapCommand(handler func(*WrappedCommandEvent)) func(*commands.Event) {
return func(ce *commands.Event) {
user := ce.User.(*User)
var portal *Portal
if ce.Portal != nil {
portal = ce.Portal.(*Portal)
}
br := ce.Bridge.Child.(*GMBridge)
handler(&WrappedCommandEvent{ce, br, user, portal})
}
}
var (
HelpSectionConnectionManagement = commands.HelpSection{Name: "Connection management", Order: 11}
HelpSectionPortalManagement = commands.HelpSection{Name: "Portal management", Order: 20}
)
var cmdLoginQR = &commands.FullHandler{
Func: wrapCommand(fnLoginQR),
Name: "login-qr",
Aliases: []string{"login"},
Help: commands.HelpMeta{
Section: commands.HelpSectionAuth,
Description: "Link the bridge to Google Messages on your Android phone by scanning a QR code.",
},
}
func fnLoginQR(ce *WrappedCommandEvent) {
if ce.User.Session != nil {
if ce.User.IsConnected() {
ce.Reply("You're already logged in")
} else {
ce.Reply("You're already logged in. Perhaps you wanted to `reconnect`?")
}
return
} else if ce.User.pairSuccessChan != nil {
ce.Reply("You already have a login in progress")
return
}
ch, err := ce.User.Login(6)
if err != nil {
ce.ZLog.Err(err).Msg("Failed to start login")
ce.Reply("Failed to start login: %v", err)
return
}
var prevEvent id.EventID
for item := range ch {
switch {
case item.qr != "":
ce.ZLog.Debug().Msg("Got code in QR channel")
prevEvent = ce.User.sendQR(ce, item.qr, prevEvent)
case item.err != nil:
ce.ZLog.Err(err).Msg("Error in QR channel")
prevEvent = ce.User.sendQREdit(ce, &event.MessageEventContent{
MsgType: event.MsgNotice,
Body: fmt.Sprintf("Failed to log in: %v", err),
}, prevEvent)
case item.success:
ce.ZLog.Debug().Msg("Got pair success in QR channel")
prevEvent = ce.User.sendQREdit(ce, &event.MessageEventContent{
MsgType: event.MsgNotice,
Body: "Successfully logged in",
}, prevEvent)
default:
ce.ZLog.Error().Any("item_data", item).Msg("Unknown item in QR channel")
}
}
ce.ZLog.Trace().Msg("Login command finished")
}
var cmdLoginGoogle = &commands.FullHandler{
Func: wrapCommand(fnLoginGoogle),
Name: "login-google",
Aliases: []string{"login"},
Help: commands.HelpMeta{
Section: commands.HelpSectionAuth,
Description: "Link the bridge to Google Messages on your Android phone by logging in with your Google account.",
},
}
func fnLoginGoogle(ce *WrappedCommandEvent) {
if ce.User.Session != nil {
if ce.User.IsConnected() {
ce.Reply("You're already logged in")
} else {
ce.Reply("You're already logged in. Perhaps you wanted to `reconnect`?")
}
return
} else if ce.User.pairSuccessChan != nil {
ce.Reply("You already have a login in progress")
return
}
ce.User.CommandState = &commands.CommandState{
Next: commands.MinimalHandlerFunc(wrapCommand(fnLoginGoogleCookies)),
Action: "Login",
}
ce.Reply("Send your Google cookies here, formatted as a key-value JSON object (see <https://docs.mau.fi/bridges/go/gmessages/authentication.html> for details)")
}
const (
pairingErrMsgNoDevices = "No devices found. Make sure you've enabled account pairing in the Google Messages app on your phone."
pairingErrPhoneNotResponding = "Phone not responding. Make sure your phone is connected to the internet and that account pairing is enabled in the Google Messages app. You may need to keep the app open and/or disable battery optimizations. Alternatively, try QR pairing"
pairingErrMsgIncorrectEmoji = "Incorrect emoji chosen on phone, please try again"
pairingErrMsgCancelled = "Pairing cancelled on phone"
pairingErrMsgTimeout = "Pairing timed out, please try again"
)
func fnLoginGoogleCookies(ce *WrappedCommandEvent) {
ce.User.CommandState = nil
if ce.User.Session != nil {
if ce.User.IsConnected() {
ce.Reply("You're already logged in")
} else {
ce.Reply("You're already logged in. Perhaps you wanted to `reconnect`?")
}
return
} else if ce.User.pairSuccessChan != nil {
ce.Reply("You already have a login in progress")
return
}
var cookies map[string]string
err := json.Unmarshal([]byte(ce.RawArgs), &cookies)
if err != nil {
ce.Reply("Failed to parse cookies: %v", err)
return
} else if missingCookie := findMissingCookies(cookies); missingCookie != "" {
ce.Reply("Missing %s cookie", missingCookie)
return
}
ce.Redact()
err = ce.User.LoginGoogle(ce.Ctx, cookies, func(emoji string) {
ce.Reply(emoji)
})
if err != nil {
if errors.Is(err, libgm.ErrNoDevicesFound) {
ce.Reply(pairingErrMsgNoDevices)
} else if errors.Is(err, libgm.ErrPairingInitTimeout) {
ce.Reply(pairingErrPhoneNotResponding)
} else if errors.Is(err, libgm.ErrIncorrectEmoji) {
ce.Reply(pairingErrMsgIncorrectEmoji)
} else if errors.Is(err, libgm.ErrPairingCancelled) {
ce.Reply(pairingErrMsgCancelled)
} else if errors.Is(err, libgm.ErrPairingTimeout) {
ce.Reply(pairingErrMsgTimeout)
} else {
ce.Reply("Login failed: %v", err)
}
} else {
ce.Reply("Login successful")
}
}
func (user *User) sendQREdit(ce *WrappedCommandEvent, content *event.MessageEventContent, prevEvent id.EventID) id.EventID {
if len(prevEvent) != 0 {
content.SetEdit(prevEvent)
}
resp, err := ce.Bot.SendMessageEvent(ce.Ctx, ce.RoomID, event.EventMessage, &content)
if err != nil {
ce.ZLog.Err(err).Msg("Failed to send edited QR code")
} else if len(prevEvent) == 0 {
prevEvent = resp.EventID
}
return prevEvent
}
func (user *User) sendQR(ce *WrappedCommandEvent, code string, prevEvent id.EventID) id.EventID {
var content event.MessageEventContent
url, err := user.uploadQR(ce.Ctx, code)
if err != nil {
ce.ZLog.Err(err).Msg("Failed to upload QR code")
content = event.MessageEventContent{
MsgType: event.MsgNotice,
Body: fmt.Sprintf("Failed to upload QR code: %v", err),
}
} else {
content = event.MessageEventContent{
MsgType: event.MsgImage,
Body: code,
URL: url.CUString(),
}
}
return user.sendQREdit(ce, &content, prevEvent)
}
func (user *User) uploadQR(ctx context.Context, code string) (id.ContentURI, error) {
qrCode, err := qrcode.Encode(code, qrcode.Low, 256)
if err != nil {
return id.ContentURI{}, err
}
resp, err := user.bridge.Bot.UploadBytes(ctx, qrCode, "image/png")
if err != nil {
return id.ContentURI{}, err
}
return resp.ContentURI, nil
}
var cmdLogout = &commands.FullHandler{
Func: wrapCommand(fnLogout),
Name: "logout",
Help: commands.HelpMeta{
Section: commands.HelpSectionAuth,
Description: "Unpair the bridge and delete session information.",
},
}
func fnLogout(ce *WrappedCommandEvent) {
if ce.User.Session == nil && ce.User.Client == nil {
ce.Reply("You're not logged in")
return
}
logoutOK := ce.User.Logout(status.BridgeState{StateEvent: status.StateLoggedOut}, true)
if logoutOK {
ce.Reply("Successfully logged out")
} else {
ce.Reply("Session information removed, but unpairing may not have succeeded")
}
}
var cmdDeleteSession = &commands.FullHandler{
Func: wrapCommand(fnDeleteSession),
Name: "delete-session",
Help: commands.HelpMeta{
Section: commands.HelpSectionAuth,
Description: "Delete session information and disconnect from Google Messages without sending a logout request.",
},
}
func fnDeleteSession(ce *WrappedCommandEvent) {
if ce.User.Session == nil && ce.User.Client == nil {
ce.Reply("Nothing to purge: no session information stored and no active connection.")
return
}
ce.User.Logout(status.BridgeState{StateEvent: status.StateLoggedOut}, false)
ce.Reply("Session information purged")
}
var cmdReconnect = &commands.FullHandler{
Func: wrapCommand(fnReconnect),
Name: "reconnect",
Help: commands.HelpMeta{
Section: HelpSectionConnectionManagement,
Description: "Reconnect to Google Messages.",
},
}
func fnReconnect(ce *WrappedCommandEvent) {
if ce.User.Client == nil {
if ce.User.Session == nil {
ce.Reply("You're not logged into Google Messages. Please log in first.")
} else {
ce.User.Connect()
ce.Reply("Started connecting to Google Messages")
}
} else {
ce.User.DeleteConnection()
ce.User.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: GMNotConnected})
ce.User.Connect()
ce.Reply("Restarted connection to Google Messages")
}
}
var cmdDisconnect = &commands.FullHandler{
Func: wrapCommand(fnDisconnect),
Name: "disconnect",
Help: commands.HelpMeta{
Section: HelpSectionConnectionManagement,
Description: "Disconnect from Google Messages (without logging out).",
},
}
func fnDisconnect(ce *WrappedCommandEvent) {
if ce.User.Client == nil {
ce.Reply("You don't have a Google Messages connection.")
return
}
ce.User.DeleteConnection()
ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.")
ce.User.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Error: GMNotConnected})
}
var cmdSetActive = &commands.FullHandler{
Func: wrapCommand(fnSetActive),
Name: "set-active",
Help: commands.HelpMeta{
Section: HelpSectionConnectionManagement,
Description: "Set the bridge as the active browser (if you opened Google Messages in a real browser)",
},
}
func fnSetActive(ce *WrappedCommandEvent) {
if ce.User.Client == nil {
ce.Reply("You don't have a Google Messages connection.")
return
}
err := ce.User.Client.SetActiveSession()
if err != nil {
ce.Reply("Failed to set active session: %v", err)
} else {
ce.Reply("Set bridge as active session")
}
}
var cmdPing = &commands.FullHandler{
Func: wrapCommand(fnPing),
Name: "ping",
Help: commands.HelpMeta{
Section: HelpSectionConnectionManagement,
Description: "Check your connection to Google Messages.",
},
}
func fnPing(ce *WrappedCommandEvent) {
if ce.User.Session == nil {
if ce.User.Client != nil {
ce.Reply("Connected to Google Messages, but not logged in.")
} else {
ce.Reply("You're not logged into Google Messages.")
}
} else if ce.User.Client == nil || !ce.User.Client.IsConnected() {
ce.Reply("Linked to %s, but not connected to Google Messages.", ce.User.PhoneID)
} else if ce.User.longPollingError != nil {
ce.Reply("Linked to %s, but long polling is erroring (%v)", ce.User.PhoneID, ce.User.longPollingError)
} else if ce.User.browserInactiveType != "" {
ce.Reply("Linked to %s, but not active, use `set-active` to reconnect", ce.User.PhoneID)
} else {
modifiers := make([]string, 0, 3)
if ce.User.batteryLow {
modifiers = append(modifiers, "battery low")
}
if ce.User.mobileData {
modifiers = append(modifiers, "using mobile data")
}
if !ce.User.phoneResponding {
modifiers = append(modifiers, "phone not responding")
}
var modifierStr string
if len(modifiers) > 0 {
modifierStr = fmt.Sprintf(" (warnings: %s)", strings.Join(modifiers, ", "))
}
ce.Reply("Linked to %s and active as primary browser%s", ce.User.PhoneID, modifierStr)
}
}
var cmdPM = &commands.FullHandler{
Func: wrapCommand(fnPM),
Name: "pm",
Help: commands.HelpMeta{
Section: HelpSectionPortalManagement,
Description: "Create a chat on Google Messages",
Args: "<phone numbers...>",
},
RequiresLogin: true,
}
func fnPM(ce *WrappedCommandEvent) {
var reqData gmproto.GetOrCreateConversationRequest
reqData.Numbers = make([]*gmproto.ContactNumber, 0, len(ce.Args))
for _, number := range ce.Args {
number = strings.TrimSpace(number)
if number == "" {
continue
}
reqData.Numbers = append(reqData.Numbers, &gmproto.ContactNumber{
// This should maybe sometimes be 7
MysteriousInt: 2,
Number: number,
Number2: number,
})
}
resp, err := ce.User.Client.GetOrCreateConversation(&reqData)
if err != nil {
ce.ZLog.Err(err).Msg("Failed to start chat")
ce.Reply("Failed to start chat: request failed")
} else if len(reqData.Numbers) > 1 && resp.GetStatus() == gmproto.GetOrCreateConversationResponse_CREATE_RCS {
ce.Reply("All recipients are on RCS, but creating RCS groups via this command is not yet supported.")
} else if resp.GetConversation() == nil {
ce.ZLog.Warn().
Int("req_number_count", len(reqData.Numbers)).
Str("status", resp.GetStatus().String()).
Msg("No conversation in chat create response")
ce.Reply("Failed to start chat: no conversation in response")
} else if portal := ce.User.GetPortalByID(resp.Conversation.ConversationID); portal.MXID != "" {
ce.Reply("Chat already exists at [%s](https://matrix.to/#/%s)", portal.MXID, portal.MXID)
} else if err = portal.CreateMatrixRoom(ce.Ctx, ce.User, resp.Conversation, false); err != nil {
ce.ZLog.Err(err).Msg("Failed to create matrix room")
ce.Reply("Failed to create portal room for conversation")
} else {
ce.Reply("Chat created: [%s](https://matrix.to/#/%s)", portal.MXID, portal.MXID)
}
}
var cmdDeletePortal = &commands.FullHandler{
Func: wrapCommand(fnDeletePortal),
Name: "delete-portal",
Help: commands.HelpMeta{
Section: HelpSectionPortalManagement,
Description: "Delete the current portal. If the portal is used by other people, this is limited to bridge admins.",
},
RequiresPortal: true,
}
func fnDeletePortal(ce *WrappedCommandEvent) {
if !ce.User.Admin && ce.Portal.Receiver != ce.User.RowID {
ce.Reply("Only bridge admins can delete other users' portals")
return
}
ce.ZLog.Info().Str("conversation_id", ce.Portal.ID).Msg("Deleting portal from command")
ce.Portal.Delete(ce.Ctx)
ce.Portal.Cleanup(ce.Ctx)
}
var cmdDeleteAllPortals = &commands.FullHandler{
Func: wrapCommand(fnDeleteAllPortals),
Name: "delete-all-portals",
Help: commands.HelpMeta{
Section: HelpSectionPortalManagement,
Description: "Delete all portals.",
},
}
func fnDeleteAllPortals(ce *WrappedCommandEvent) {
portals := ce.Bridge.GetAllPortalsForUser(ce.User.RowID)
if len(portals) == 0 {
ce.Reply("Didn't find any portals to delete")
return
}
leave := func(portal *Portal) {
if len(portal.MXID) > 0 {
_, _ = portal.MainIntent().KickUser(ce.Ctx, portal.MXID, &mautrix.ReqKickUser{
Reason: "Deleting portal",
UserID: ce.User.MXID,
})
}
}
intent := ce.User.DoublePuppetIntent
if intent != nil {
leave = func(portal *Portal) {
if len(portal.MXID) > 0 {
_, _ = intent.LeaveRoom(ce.Ctx, portal.MXID)
_, _ = intent.ForgetRoom(ce.Ctx, portal.MXID)
}
}
}
roomYeeting := ce.Bridge.SpecVersions.Supports(mautrix.BeeperFeatureRoomYeeting)
if roomYeeting {
leave = func(portal *Portal) {
portal.Cleanup(ce.Ctx)
}
}
ce.Reply("Found %d portals, deleting...", len(portals))
for _, portal := range portals {
portal.Delete(ce.Ctx)
leave(portal)
}
if !roomYeeting {
ce.Reply("Finished deleting portal info. Now cleaning up rooms in background.")
go func() {
for _, portal := range portals {
portal.Cleanup(ce.Ctx)
}
ce.Reply("Finished background cleanup of deleted portal rooms.")
}()
} else {
ce.Reply("Finished deleting portals.")
}
}