2022-05-22 19:16:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2023-04-25 22:39:17 +00:00
|
|
|
"strings"
|
2022-05-22 19:16:42 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2023-03-12 12:25:24 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-05-28 20:03:24 +00:00
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
|
|
|
"maunium.net/go/mautrix/bridge"
|
2023-04-18 03:58:46 +00:00
|
|
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
2022-05-22 19:16:42 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-discord/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Puppet struct {
|
|
|
|
*database.Puppet
|
|
|
|
|
|
|
|
bridge *DiscordBridge
|
2023-03-12 12:25:24 +00:00
|
|
|
log zerolog.Logger
|
2022-05-22 19:16:42 +00:00
|
|
|
|
|
|
|
MXID id.UserID
|
|
|
|
|
|
|
|
customIntent *appservice.IntentAPI
|
|
|
|
customUser *User
|
|
|
|
|
|
|
|
syncLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ bridge.Ghost = (*Puppet)(nil)
|
2022-06-30 19:00:12 +00:00
|
|
|
var _ bridge.GhostWithProfile = (*Puppet)(nil)
|
2022-05-22 19:16:42 +00:00
|
|
|
|
|
|
|
func (puppet *Puppet) GetMXID() id.UserID {
|
|
|
|
return puppet.MXID
|
|
|
|
}
|
|
|
|
|
|
|
|
var userIDRegex *regexp.Regexp
|
|
|
|
|
|
|
|
func (br *DiscordBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
|
|
|
return &Puppet{
|
|
|
|
Puppet: dbPuppet,
|
|
|
|
bridge: br,
|
2023-03-12 12:25:24 +00:00
|
|
|
log: br.ZLog.With().Str("discord_user_id", dbPuppet.ID).Logger(),
|
2022-05-22 19:16:42 +00:00
|
|
|
|
|
|
|
MXID: br.FormatPuppetMXID(dbPuppet.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) ParsePuppetMXID(mxid id.UserID) (string, bool) {
|
|
|
|
if userIDRegex == nil {
|
|
|
|
pattern := fmt.Sprintf(
|
|
|
|
"^@%s:%s$",
|
|
|
|
br.Config.Bridge.FormatUsername("([0-9]+)"),
|
|
|
|
br.Config.Homeserver.Domain,
|
|
|
|
)
|
|
|
|
|
|
|
|
userIDRegex = regexp.MustCompile(pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
|
|
|
if len(match) == 2 {
|
|
|
|
return match[1], true
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
2022-05-28 20:03:24 +00:00
|
|
|
discordID, ok := br.ParsePuppetMXID(mxid)
|
2022-05-22 19:16:42 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
return br.GetPuppetByID(discordID)
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetPuppetByID(id string) *Puppet {
|
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
|
|
|
|
puppet, ok := br.puppets[id]
|
|
|
|
if !ok {
|
|
|
|
dbPuppet := br.DB.Puppet.Get(id)
|
|
|
|
if dbPuppet == nil {
|
|
|
|
dbPuppet = br.DB.Puppet.New()
|
|
|
|
dbPuppet.ID = id
|
|
|
|
dbPuppet.Insert()
|
|
|
|
}
|
|
|
|
|
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[puppet.ID] = puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
|
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
|
|
|
|
puppet, ok := br.puppetsByCustomMXID[mxid]
|
|
|
|
if !ok {
|
|
|
|
dbPuppet := br.DB.Puppet.GetByCustomMXID(mxid)
|
|
|
|
if dbPuppet == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[puppet.ID] = puppet
|
|
|
|
br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
|
|
|
|
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAllWithCustomMXID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetAllPuppets() []*Puppet {
|
|
|
|
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAll())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) 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.ID]
|
|
|
|
if !ok {
|
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[dbPuppet.ID] = puppet
|
|
|
|
|
|
|
|
if dbPuppet.CustomMXID != "" {
|
|
|
|
br.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output[index] = puppet
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) FormatPuppetMXID(did string) id.UserID {
|
|
|
|
return id.NewUserID(
|
|
|
|
br.Config.Bridge.FormatUsername(did),
|
|
|
|
br.Config.Homeserver.Domain,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-08 13:58:30 +00:00
|
|
|
func (br *DiscordBridge) updatePuppetsContactInfo() {
|
|
|
|
if br.Config.Homeserver.Software != bridgeconfig.SoftwareHungry {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, puppet := range br.GetAllPuppets() {
|
|
|
|
if !puppet.ContactInfoSet && puppet.NameSet {
|
|
|
|
puppet.ResendContactInfo()
|
|
|
|
puppet.Update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 19:00:12 +00:00
|
|
|
func (puppet *Puppet) GetDisplayname() string {
|
|
|
|
return puppet.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetAvatarURL() id.ContentURI {
|
|
|
|
return puppet.AvatarURL
|
|
|
|
}
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
|
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
|
2022-05-28 20:03:24 +00:00
|
|
|
if puppet.customIntent == nil || (portal.Key.Receiver != "" && portal.Key.Receiver != puppet.ID) {
|
2022-05-22 19:16:42 +00:00
|
|
|
return puppet.DefaultIntent()
|
|
|
|
}
|
|
|
|
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
|
2023-01-20 13:07:18 +00:00
|
|
|
if puppet == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-22 19:16:42 +00:00
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
|
2022-05-23 20:18:10 +00:00
|
|
|
for _, portal := range puppet.bridge.GetDMPortalsWith(puppet.ID) {
|
2022-05-22 19:16:42 +00:00
|
|
|
// Get room create lock to prevent races between receiving contact info and room creation.
|
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
meta(portal)
|
|
|
|
portal.roomCreateLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (puppet *Puppet) UpdateName(info *discordgo.User) bool {
|
2023-05-27 10:01:24 +00:00
|
|
|
newName := puppet.bridge.Config.Bridge.FormatDisplayname(info, puppet.IsWebhook)
|
2022-05-28 20:03:24 +00:00
|
|
|
if puppet.Name == newName && puppet.NameSet {
|
2022-05-22 19:16:42 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-05-28 20:03:24 +00:00
|
|
|
puppet.Name = newName
|
|
|
|
puppet.NameSet = false
|
|
|
|
err := puppet.DefaultIntent().SetDisplayName(newName)
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-03-12 12:25:24 +00:00
|
|
|
puppet.log.Warn().Err(err).Msg("Failed to update displayname")
|
2022-05-28 20:03:24 +00:00
|
|
|
} else {
|
|
|
|
go puppet.updatePortalMeta(func(portal *Portal) {
|
2023-04-21 23:50:14 +00:00
|
|
|
if portal.UpdateNameDirect(puppet.Name, false) {
|
2022-05-28 20:03:24 +00:00
|
|
|
portal.Update()
|
2022-07-08 17:48:36 +00:00
|
|
|
portal.UpdateBridgeInfo()
|
2022-05-28 20:03:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
puppet.NameSet = true
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
2022-05-28 20:03:24 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-05-22 19:16:42 +00:00
|
|
|
|
2023-05-27 10:35:37 +00:00
|
|
|
func (br *DiscordBridge) reuploadUserAvatar(intent *appservice.IntentAPI, userID, avatarID string) (id.ContentURI, error) {
|
|
|
|
downloadURL := discordgo.EndpointUserAvatar(userID, avatarID)
|
|
|
|
ext := "png"
|
|
|
|
if strings.HasPrefix(avatarID, "a_") {
|
|
|
|
downloadURL = discordgo.EndpointUserAvatarAnimated(userID, avatarID)
|
|
|
|
ext = "gif"
|
|
|
|
}
|
|
|
|
url := br.Config.Bridge.MediaPatterns.Avatar(userID, avatarID, ext)
|
|
|
|
if !url.IsEmpty() {
|
|
|
|
return url, nil
|
|
|
|
}
|
|
|
|
copied, err := br.copyAttachmentToMatrix(intent, downloadURL, false, AttachmentMeta{
|
|
|
|
AttachmentID: fmt.Sprintf("avatar/%s/%s", userID, avatarID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return url, err
|
|
|
|
}
|
|
|
|
return copied.MXC, nil
|
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
func (puppet *Puppet) UpdateAvatar(info *discordgo.User) bool {
|
2023-05-27 10:35:37 +00:00
|
|
|
avatarID := info.Avatar
|
2023-05-27 10:01:24 +00:00
|
|
|
if puppet.IsWebhook && !puppet.bridge.Config.Bridge.EnableWebhookAvatars {
|
2023-05-27 10:35:37 +00:00
|
|
|
avatarID = ""
|
2023-05-27 10:01:24 +00:00
|
|
|
}
|
2023-05-27 10:35:37 +00:00
|
|
|
if puppet.Avatar == avatarID && puppet.AvatarSet {
|
2022-05-22 19:16:42 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-27 10:35:37 +00:00
|
|
|
avatarChanged := avatarID != puppet.Avatar
|
|
|
|
puppet.Avatar = avatarID
|
2022-05-28 20:03:24 +00:00
|
|
|
puppet.AvatarSet = false
|
2022-05-30 21:34:21 +00:00
|
|
|
puppet.AvatarURL = id.ContentURI{}
|
2022-05-22 19:16:42 +00:00
|
|
|
|
2022-05-30 21:34:21 +00:00
|
|
|
if puppet.Avatar != "" && (puppet.AvatarURL.IsEmpty() || avatarChanged) {
|
2023-05-27 10:35:37 +00:00
|
|
|
url, err := puppet.bridge.reuploadUserAvatar(puppet.DefaultIntent(), info.ID, puppet.Avatar)
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warn().Err(err).Str("avatar_id", puppet.Avatar).Msg("Failed to reupload user avatar")
|
|
|
|
return true
|
2022-05-28 20:03:24 +00:00
|
|
|
}
|
|
|
|
puppet.AvatarURL = url
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 20:03:24 +00:00
|
|
|
err := puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-03-12 12:25:24 +00:00
|
|
|
puppet.log.Warn().Err(err).Msg("Failed to update avatar")
|
2022-05-28 20:03:24 +00:00
|
|
|
} else {
|
|
|
|
go puppet.updatePortalMeta(func(portal *Portal) {
|
|
|
|
if portal.UpdateAvatarFromPuppet(puppet) {
|
|
|
|
portal.Update()
|
2022-07-08 17:48:36 +00:00
|
|
|
portal.UpdateBridgeInfo()
|
2022-05-28 20:03:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
puppet.AvatarSet = true
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-05-27 09:31:10 +00:00
|
|
|
func (puppet *Puppet) UpdateInfo(source *User, info *discordgo.User, webhookID string) {
|
2022-05-22 19:16:42 +00:00
|
|
|
puppet.syncLock.Lock()
|
|
|
|
defer puppet.syncLock.Unlock()
|
|
|
|
|
2023-04-18 15:43:32 +00:00
|
|
|
if info == nil || len(info.Username) == 0 || len(info.Discriminator) == 0 {
|
|
|
|
if puppet.Name != "" || source == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
puppet.log.Debug().Str("source_user", source.DiscordID).Msg("Fetching info through user to update puppet")
|
|
|
|
info, err = source.Session.User(puppet.ID)
|
|
|
|
if err != nil {
|
|
|
|
puppet.log.Error().Err(err).Str("source_user", source.DiscordID).Msg("Failed to fetch info through user")
|
|
|
|
return
|
|
|
|
}
|
2022-05-28 20:03:24 +00:00
|
|
|
}
|
2022-05-22 19:16:42 +00:00
|
|
|
|
2023-04-18 15:43:32 +00:00
|
|
|
err := puppet.DefaultIntent().EnsureRegistered()
|
2022-05-22 19:16:42 +00:00
|
|
|
if err != nil {
|
2023-03-12 12:25:24 +00:00
|
|
|
puppet.log.Error().Err(err).Msg("Failed to ensure registered")
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:18:45 +00:00
|
|
|
changed := false
|
2023-05-27 09:31:10 +00:00
|
|
|
if webhookID != "" && webhookID == info.ID && !puppet.IsWebhook {
|
|
|
|
puppet.IsWebhook = true
|
|
|
|
changed = true
|
|
|
|
}
|
2023-04-26 18:18:45 +00:00
|
|
|
changed = puppet.UpdateContactInfo(info) || changed
|
2022-05-28 20:03:24 +00:00
|
|
|
changed = puppet.UpdateName(info) || changed
|
|
|
|
changed = puppet.UpdateAvatar(info) || changed
|
|
|
|
if changed {
|
2022-05-22 19:16:42 +00:00
|
|
|
puppet.Update()
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 03:58:46 +00:00
|
|
|
|
2023-04-26 18:18:45 +00:00
|
|
|
func (puppet *Puppet) UpdateContactInfo(info *discordgo.User) bool {
|
|
|
|
changed := false
|
|
|
|
if puppet.Username != info.Username {
|
|
|
|
puppet.Username = info.Username
|
|
|
|
changed = true
|
2023-04-18 03:58:46 +00:00
|
|
|
}
|
2023-05-27 09:31:10 +00:00
|
|
|
if puppet.GlobalName != info.GlobalName {
|
|
|
|
puppet.GlobalName = info.GlobalName
|
|
|
|
changed = true
|
|
|
|
}
|
2023-04-26 18:18:45 +00:00
|
|
|
if puppet.Discriminator != info.Discriminator {
|
|
|
|
puppet.Discriminator = info.Discriminator
|
|
|
|
changed = true
|
2023-04-18 03:58:46 +00:00
|
|
|
}
|
2023-04-26 18:18:45 +00:00
|
|
|
if puppet.IsBot != info.Bot {
|
|
|
|
puppet.IsBot = info.Bot
|
|
|
|
changed = true
|
|
|
|
}
|
2023-05-27 10:01:24 +00:00
|
|
|
if (changed && !puppet.IsWebhook) || !puppet.ContactInfoSet {
|
2023-04-26 18:18:45 +00:00
|
|
|
puppet.ContactInfoSet = false
|
|
|
|
puppet.ResendContactInfo()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-04-18 03:58:46 +00:00
|
|
|
|
2023-04-26 18:18:45 +00:00
|
|
|
func (puppet *Puppet) ResendContactInfo() {
|
|
|
|
if puppet.bridge.Config.Homeserver.Software != bridgeconfig.SoftwareHungry || puppet.ContactInfoSet {
|
|
|
|
return
|
|
|
|
}
|
2023-04-18 03:58:46 +00:00
|
|
|
contactInfo := map[string]any{
|
|
|
|
"com.beeper.bridge.identifiers": []string{
|
2023-04-26 18:18:45 +00:00
|
|
|
fmt.Sprintf("discord:%s#%s", puppet.Username, puppet.Discriminator),
|
2023-04-18 03:58:46 +00:00
|
|
|
},
|
2023-04-18 16:14:38 +00:00
|
|
|
"com.beeper.bridge.remote_id": puppet.ID,
|
|
|
|
"com.beeper.bridge.service": puppet.bridge.BeeperServiceName,
|
|
|
|
"com.beeper.bridge.network": puppet.bridge.BeeperNetworkName,
|
2023-04-26 18:18:45 +00:00
|
|
|
"com.beeper.bridge.is_network_bot": puppet.IsBot,
|
2023-04-18 03:58:46 +00:00
|
|
|
}
|
2023-05-27 10:01:24 +00:00
|
|
|
if puppet.IsWebhook {
|
|
|
|
contactInfo["com.beeper.bridge.identifiers"] = []string{}
|
|
|
|
}
|
2023-04-18 15:43:32 +00:00
|
|
|
err := puppet.DefaultIntent().BeeperUpdateProfile(contactInfo)
|
2023-04-18 03:58:46 +00:00
|
|
|
if err != nil {
|
|
|
|
puppet.log.Warn().Err(err).Msg("Failed to store custom contact info in profile")
|
|
|
|
} else {
|
|
|
|
puppet.ContactInfoSet = true
|
|
|
|
}
|
|
|
|
}
|