This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
forked from mautrix/signal
-
Notifications
You must be signed in to change notification settings - Fork 8
/
puppet.go
414 lines (369 loc) · 11.2 KB
/
puppet.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
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Scott Weber
//
// 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"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"regexp"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/element-hq/mautrix-go"
"github.com/element-hq/mautrix-go/appservice"
"github.com/element-hq/mautrix-go/bridge"
"github.com/element-hq/mautrix-go/id"
"github.com/element-hq/mautrix-signal/database"
"github.com/element-hq/mautrix-signal/pkg/signalmeow/types"
)
func (br *SignalBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
signalID, ok := br.ParsePuppetMXID(mxid)
if !ok {
return nil
}
return br.GetPuppetBySignalID(signalID)
}
func (br *SignalBridge) GetPuppetBySignalIDString(id string) *Puppet {
parsed, err := uuid.Parse(id)
if err != nil {
return nil
}
return br.GetPuppetBySignalID(parsed)
}
func (br *SignalBridge) GetPuppetBySignalID(id uuid.UUID) *Puppet {
if id == uuid.Nil {
br.ZLog.Warn().Msg("Trying to get puppet with empty signal_user_id")
return nil
}
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
puppet, ok := br.puppets[id]
if !ok {
dbPuppet, err := br.DB.Puppet.GetBySignalID(context.TODO(), id)
if err != nil {
br.ZLog.Err(err).Msg("Failed to get puppet from database")
return nil
}
return br.loadPuppet(context.TODO(), dbPuppet, &id)
}
return puppet
}
func (br *SignalBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
puppet, ok := br.puppetsByCustomMXID[mxid]
if !ok {
dbPuppet, err := br.DB.Puppet.GetByCustomMXID(context.TODO(), mxid)
if err != nil {
br.ZLog.Err(err).Msg("Failed to get puppet from database")
return nil
}
return br.loadPuppet(context.TODO(), dbPuppet, nil)
}
return puppet
}
func (br *SignalBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
puppets, err := br.DB.Puppet.GetAllWithCustomMXID(context.TODO())
if err != nil {
br.ZLog.Error().Err(err).Msg("Failed to get all puppets with custom MXID")
return nil
}
return br.dbPuppetsToPuppets(puppets)
}
func (br *SignalBridge) FormatPuppetMXID(u uuid.UUID) id.UserID {
return id.NewUserID(
br.Config.Bridge.FormatUsername(u.String()),
br.Config.Homeserver.Domain,
)
}
func (br *SignalBridge) loadPuppet(ctx context.Context, dbPuppet *database.Puppet, u *uuid.UUID) *Puppet {
if dbPuppet == nil {
if u == nil {
return nil
}
dbPuppet = br.DB.Puppet.New()
dbPuppet.SignalID = *u
err := dbPuppet.Insert(ctx)
if err != nil {
br.ZLog.Error().Err(err).Stringer("signal_user_id", *u).Msg("Failed to insert new puppet")
return nil
}
}
puppet := br.NewPuppet(dbPuppet)
br.puppets[puppet.SignalID] = puppet
if puppet.CustomMXID != "" {
br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
}
return puppet
}
func (br *SignalBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
output := make([]*Puppet, len(dbPuppets))
for index, dbPuppet := range dbPuppets {
if dbPuppet == nil {
continue
}
puppet, ok := br.puppets[dbPuppet.SignalID]
if !ok {
puppet = br.loadPuppet(context.TODO(), dbPuppet, nil)
}
output[index] = puppet
}
return output
}
func (br *SignalBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
return &Puppet{
Puppet: dbPuppet,
bridge: br,
log: br.ZLog.With().Stringer("signal_user_id", dbPuppet.SignalID).Logger(),
MXID: br.FormatPuppetMXID(dbPuppet.SignalID),
}
}
func (br *SignalBridge) ParsePuppetMXID(mxid id.UserID) (uuid.UUID, bool) {
if userIDRegex == nil {
pattern := fmt.Sprintf(
"^@%s:%s$",
// The "SignalID" portion of the MXID is a (lowercase) UUID
br.Config.Bridge.FormatUsername("([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})"),
br.Config.Homeserver.Domain,
)
br.ZLog.Debug().Str("pattern", pattern).Msg("Compiling userIDRegex")
userIDRegex = regexp.MustCompile(pattern)
}
match := userIDRegex.FindStringSubmatch(string(mxid))
if len(match) == 2 {
parsed, err := uuid.Parse(match[1])
if err != nil {
return uuid.Nil, false
}
return parsed, true
}
return uuid.Nil, false
}
type PuppetActivity struct {
currentUserCount uint
isBlocked bool
}
type Puppet struct {
*database.Puppet
bridge *SignalBridge
log zerolog.Logger
MXID id.UserID
customIntent *appservice.IntentAPI
customUser *User
}
var userIDRegex *regexp.Regexp
var (
_ bridge.Ghost = (*Puppet)(nil)
_ bridge.GhostWithProfile = (*Puppet)(nil)
)
func (puppet *Puppet) GetMXID() id.UserID {
return puppet.MXID
}
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
return puppet.bridge.AS.Intent(puppet.MXID)
}
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
if puppet == nil {
return nil
}
return puppet.customIntent
}
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
if puppet != nil {
if puppet.customIntent == nil || portal.UserID().UUID == puppet.SignalID {
return puppet.DefaultIntent()
}
return puppet.customIntent
}
return nil
}
func (puppet *Puppet) GetDisplayname() string {
return puppet.Name
}
func (puppet *Puppet) GetAvatarURL() id.ContentURI {
return puppet.AvatarURL
}
func (puppet *Puppet) UpdateInfo(ctx context.Context, source *User) {
log := zerolog.Ctx(ctx).With().
Str("function", "Puppet.UpdateInfo").
Stringer("signal_user_id", puppet.SignalID).
Logger()
ctx = log.WithContext(ctx)
var err error
log.Debug().Msg("Fetching contact info to update puppet")
info, err := source.Client.ContactByACI(ctx, puppet.SignalID)
if err != nil {
log.Err(err).Msg("Failed to fetch contact info")
return
}
if !puppet.bridge.Config.Bridge.UseOutdatedProfiles && puppet.ProfileFetchedAt.After(info.Profile.FetchedAt) {
log.Debug().
Time("contact_profile_fetched_at", info.Profile.FetchedAt).
Time("puppet_profile_fetched_at", puppet.ProfileFetchedAt).
Msg("Ignoring outdated contact info")
return
}
log.Trace().Msg("Updating puppet info")
update := false
if puppet.ProfileFetchedAt.IsZero() && !info.Profile.FetchedAt.IsZero() {
update = true
}
puppet.ProfileFetchedAt = info.Profile.FetchedAt
if info.E164 != "" && puppet.Number != info.E164 {
puppet.Number = info.E164
update = true
}
update = puppet.updateName(ctx, info) || update
update = puppet.updateAvatar(ctx, source, info) || update
if update {
puppet.ContactInfoSet = false
puppet.UpdateContactInfo(ctx)
err = puppet.Update(ctx)
if err != nil {
log.Err(err).Msg("Failed to save puppet to database after updating")
}
go puppet.updatePortalMeta(ctx)
log.Debug().Msg("Puppet info updated")
}
}
func (puppet *Puppet) UpdateContactInfo(ctx context.Context) {
if !puppet.bridge.SpecVersions.Supports(mautrix.BeeperFeatureArbitraryProfileMeta) || puppet.ContactInfoSet {
return
}
identifiers := []string{
fmt.Sprintf("signal:%s", puppet.SignalID),
}
if puppet.Number != "" {
identifiers = append(identifiers, fmt.Sprintf("tel:%s", puppet.Number))
}
contactInfo := map[string]any{
"com.beeper.bridge.identifiers": identifiers,
"com.beeper.bridge.remote_id": puppet.SignalID.String(),
"com.beeper.bridge.service": "signal",
"com.beeper.bridge.network": "signal",
}
err := puppet.DefaultIntent().BeeperUpdateProfile(ctx, contactInfo)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to store custom contact info in profile")
} else {
puppet.ContactInfoSet = true
}
}
func (puppet *Puppet) updatePortalMeta(ctx context.Context) {
for _, portal := range puppet.bridge.FindPrivateChatPortalsWith(puppet.SignalID) {
// Get room create lock to prevent races between receiving contact info and room creation.
portal.roomCreateLock.Lock()
portal.UpdateDMInfo(ctx, false)
portal.roomCreateLock.Unlock()
}
}
func (puppet *Puppet) updateAvatar(ctx context.Context, source *User, info *types.Recipient) bool {
var avatarData []byte
var avatarContentType string
log := zerolog.Ctx(ctx)
if puppet.bridge.Config.Bridge.UseContactAvatars && info.ContactAvatar.Hash != "" {
if puppet.AvatarHash == info.ContactAvatar.Hash && puppet.AvatarSet {
return false
}
avatarData = info.ContactAvatar.Image
avatarContentType = info.ContactAvatar.ContentType
if avatarData == nil {
// TODO what to do? 🤔
return false
}
puppet.AvatarPath = ""
puppet.AvatarHash = info.ContactAvatar.Hash
} else {
if puppet.AvatarPath == info.Profile.AvatarPath && puppet.AvatarSet {
return false
}
if info.Profile.AvatarPath == "" {
puppet.AvatarURL = id.ContentURI{}
puppet.AvatarPath = ""
puppet.AvatarHash = ""
puppet.AvatarSet = false
err := puppet.DefaultIntent().SetAvatarURL(ctx, puppet.AvatarURL)
if err != nil {
log.Err(err).Msg("Failed to remove user avatar")
return true
}
log.Debug().Msg("Avatar removed")
puppet.AvatarSet = true
return true
}
var err error
avatarData, err = source.Client.DownloadUserAvatar(ctx, info.Profile.AvatarPath, info.Profile.Key)
if err != nil {
log.Err(err).
Str("profile_avatar_path", info.Profile.AvatarPath).
Msg("Failed to download new user avatar")
return true
}
avatarContentType = http.DetectContentType(avatarData)
puppet.AvatarPath = info.Profile.AvatarPath
hash := sha256.Sum256(avatarData)
newHash := hex.EncodeToString(hash[:])
if puppet.AvatarHash == newHash && puppet.AvatarSet {
log.Debug().
Str("avatar_hash", newHash).
Str("new_avatar_path", puppet.AvatarPath).
Msg("Avatar path changed, but hash didn't")
// Path changed, but actual avatar didn't
return true
}
puppet.AvatarHash = newHash
}
puppet.AvatarSet = false
puppet.AvatarURL = id.ContentURI{}
resp, err := puppet.DefaultIntent().UploadBytes(ctx, avatarData, avatarContentType)
if err != nil {
log.Err(err).
Str("avatar_hash", puppet.AvatarHash).
Msg("Failed to upload new user avatar")
return true
}
puppet.AvatarURL = resp.ContentURI
err = puppet.DefaultIntent().SetAvatarURL(ctx, puppet.AvatarURL)
if err != nil {
log.Err(err).Msg("Failed to update user avatar")
return true
}
log.Debug().
Str("avatar_hash", puppet.AvatarHash).
Stringer("avatar_mxc", resp.ContentURI).
Msg("Avatar updated successfully")
puppet.AvatarSet = true
return true
}
func (puppet *Puppet) updateName(ctx context.Context, contact *types.Recipient) bool {
// TODO set name quality
newName := puppet.bridge.Config.Bridge.FormatDisplayname(contact)
if puppet.NameSet && puppet.Name == newName {
return false
}
puppet.Name = newName
puppet.NameSet = false
err := puppet.DefaultIntent().SetDisplayName(ctx, newName)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to update user displayname")
} else {
puppet.NameSet = true
}
return true
}