forked from mautrix/imessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custompuppet.go
248 lines (227 loc) · 7.38 KB
/
custompuppet.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
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2022 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 (
"crypto/hmac"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/bridge"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var (
ErrMismatchingMXID = errors.New("whoami result does not match custom mxid")
)
var _ bridge.DoublePuppet = (*User)(nil)
func (user *User) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
if mxid != user.MXID {
return errors.New("mismatching mxid")
}
user.AccessToken = accessToken
return user.startCustomMXID()
}
func (user *User) CustomIntent() *appservice.IntentAPI {
return user.DoublePuppetIntent
}
func (user *User) initDoublePuppet() {
var err error
if len(user.AccessToken) > 0 {
err = user.startCustomMXID()
if errors.Is(err, mautrix.MUnknownToken) && len(user.bridge.Config.Bridge.LoginSharedSecret) > 0 {
user.log.Debugln("Unknown token while starting custom puppet, trying to relogin with shared secret")
err = user.loginWithSharedSecret()
if err == nil {
err = user.startCustomMXID()
}
}
} else {
err = user.loginWithSharedSecret()
if err == nil {
err = user.startCustomMXID()
}
}
if err != nil {
user.log.Warnln("Failed to switch to auto-logined custom puppet:", err)
} else {
user.log.Infoln("Successfully automatically enabled custom puppet")
}
}
func (user *User) loginWithSharedSecret() error {
user.log.Debugfln("Logging in with shared secret")
loginSecret := user.bridge.Config.Bridge.LoginSharedSecret
client, err := user.bridge.AS.NewExternalMautrixClient(user.MXID, "", user.bridge.Config.Bridge.DoublePuppetServerURL)
if err != nil {
return err
}
req := mautrix.ReqLogin{
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(user.MXID)},
DeviceID: id.DeviceID(user.bridge.Config.IMessage.BridgeName()),
InitialDeviceDisplayName: user.bridge.Config.IMessage.BridgeName(),
}
if loginSecret == "appservice" {
client.AccessToken = user.bridge.AS.Registration.AppToken
req.Type = mautrix.AuthTypeAppservice
} else {
mac := hmac.New(sha512.New, []byte(loginSecret))
mac.Write([]byte(user.MXID))
req.Password = hex.EncodeToString(mac.Sum(nil))
req.Type = mautrix.AuthTypePassword
}
resp, err := client.Login(&req)
if err != nil {
return fmt.Errorf("failed to log in with shared secret: %w", err)
}
user.AccessToken = resp.AccessToken
user.Update()
return nil
}
func (user *User) newDoublePuppetIntent() (*appservice.IntentAPI, error) {
client, err := user.bridge.AS.NewExternalMautrixClient(user.MXID, user.AccessToken, user.bridge.Config.Bridge.DoublePuppetServerURL)
if err != nil {
return nil, err
}
client.Syncer = user
ia := user.bridge.AS.NewIntentAPI("custom")
ia.Client = client
ia.Localpart, _, _ = user.MXID.Parse()
ia.UserID = user.MXID
ia.IsCustomPuppet = true
return ia, nil
}
func (user *User) clearCustomMXID() {
user.AccessToken = ""
user.NextBatch = ""
user.DoublePuppetIntent = nil
}
func (user *User) startCustomMXID() error {
if len(user.AccessToken) == 0 {
user.clearCustomMXID()
return nil
}
intent, err := user.newDoublePuppetIntent()
if err != nil {
user.clearCustomMXID()
return fmt.Errorf("failed to create double puppet intent: %w", err)
}
resp, err := intent.Whoami()
if err != nil {
user.clearCustomMXID()
return fmt.Errorf("failed to ensure double puppet token is valid: %w", err)
}
if resp.UserID != user.MXID {
user.clearCustomMXID()
return ErrMismatchingMXID
}
user.DoublePuppetIntent = intent
user.startSyncing()
return nil
}
func (user *User) startSyncing() {
if !user.bridge.Config.Bridge.SyncWithCustomPuppets {
return
}
if !user.bridge.IM.Capabilities().SendTypingNotifications && !user.bridge.IM.Capabilities().SendReadReceipts {
user.log.Warnln("Syncing with double puppet is enabled in config, but configured platform doesn't support sending typing notifications nor read receipts")
}
go func() {
err := user.DoublePuppetIntent.Sync()
if err != nil {
user.log.Errorln("Fatal error syncing:", err)
}
}()
}
func (user *User) stopSyncing() {
if !user.bridge.Config.Bridge.SyncWithCustomPuppets {
return
}
user.DoublePuppetIntent.StopSync()
}
func (user *User) tryRelogin(cause error, action string) bool {
user.log.Debugfln("Trying to relogin after '%v' while %s", cause, action)
err := user.loginWithSharedSecret()
if err != nil {
user.log.Errorfln("Failed to relogin after '%v' while %s: %v", cause, action, err)
return false
}
user.log.Infofln("Successfully relogined after '%v' while %s", cause, action)
return true
}
func (user *User) handleReceiptEvent(portal *Portal, evt *event.Event) {
for eventID, receipts := range *evt.Content.AsReceipt() {
if receipt, ok := receipts.GetOrCreate(event.ReceiptTypeRead)[user.MXID]; !ok {
// Ignore receipt events where this user isn't present.
} else if val, ok := receipt.Extra[appservice.DoublePuppetKey].(string); ok && user.DoublePuppetIntent != nil && val == portal.bridge.Name {
// Ignore double puppeted read receipts.
} else {
portal.HandleMatrixReadReceipt(user, eventID, receipt)
}
}
}
func (user *User) ProcessResponse(resp *mautrix.RespSync, _ string) error {
for roomID, events := range resp.Rooms.Join {
portal := user.bridge.GetPortalByMXID(roomID)
if portal == nil {
continue
}
for _, evt := range events.Ephemeral.Events {
err := evt.Content.ParseRaw(evt.Type)
if err != nil {
return err
}
switch evt.Type {
case event.EphemeralEventReceipt:
go user.handleReceiptEvent(portal, evt)
case event.EphemeralEventTyping:
if portal.IsPrivateChat() {
go portal.HandleMatrixTyping(evt.Content.AsTyping().UserIDs)
}
}
}
}
return nil
}
func (user *User) OnFailedSync(_ *mautrix.RespSync, err error) (time.Duration, error) {
user.log.Warnln("Failed to sync:", err)
if errors.Is(err, mautrix.MUnknownToken) {
if !user.tryRelogin(err, "syncing") {
return 0, err
}
return 0, nil
}
return 10 * time.Second, nil
}
func (user *User) GetFilterJSON(_ id.UserID) *mautrix.Filter {
everything := []event.Type{{Type: "*"}}
return &mautrix.Filter{
Presence: mautrix.FilterPart{
Senders: []id.UserID{user.MXID},
Types: []event.Type{event.EphemeralEventPresence},
},
AccountData: mautrix.FilterPart{NotTypes: everything},
Room: mautrix.RoomFilter{
Ephemeral: mautrix.FilterPart{Types: []event.Type{event.EphemeralEventTyping, event.EphemeralEventReceipt}},
IncludeLeave: false,
AccountData: mautrix.FilterPart{NotTypes: everything},
State: mautrix.FilterPart{NotTypes: everything},
Timeline: mautrix.FilterPart{NotTypes: everything},
},
}
}