puppet.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "sync"
  6. log "maunium.net/go/maulogger/v2"
  7. "github.com/bwmarrin/discordgo"
  8. "maunium.net/go/mautrix/appservice"
  9. "maunium.net/go/mautrix/bridge"
  10. "maunium.net/go/mautrix/id"
  11. "go.mau.fi/mautrix-discord/database"
  12. )
  13. type Puppet struct {
  14. *database.Puppet
  15. bridge *DiscordBridge
  16. log log.Logger
  17. MXID id.UserID
  18. customIntent *appservice.IntentAPI
  19. customUser *User
  20. syncLock sync.Mutex
  21. }
  22. var _ bridge.Ghost = (*Puppet)(nil)
  23. func (puppet *Puppet) GetMXID() id.UserID {
  24. return puppet.MXID
  25. }
  26. var userIDRegex *regexp.Regexp
  27. func (br *DiscordBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
  28. return &Puppet{
  29. Puppet: dbPuppet,
  30. bridge: br,
  31. log: br.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.ID)),
  32. MXID: br.FormatPuppetMXID(dbPuppet.ID),
  33. }
  34. }
  35. func (br *DiscordBridge) ParsePuppetMXID(mxid id.UserID) (string, bool) {
  36. if userIDRegex == nil {
  37. pattern := fmt.Sprintf(
  38. "^@%s:%s$",
  39. br.Config.Bridge.FormatUsername("([0-9]+)"),
  40. br.Config.Homeserver.Domain,
  41. )
  42. userIDRegex = regexp.MustCompile(pattern)
  43. }
  44. match := userIDRegex.FindStringSubmatch(string(mxid))
  45. if len(match) == 2 {
  46. return match[1], true
  47. }
  48. return "", false
  49. }
  50. func (br *DiscordBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
  51. discordID, ok := br.ParsePuppetMXID(mxid)
  52. if !ok {
  53. return nil
  54. }
  55. return br.GetPuppetByID(discordID)
  56. }
  57. func (br *DiscordBridge) GetPuppetByID(id string) *Puppet {
  58. br.puppetsLock.Lock()
  59. defer br.puppetsLock.Unlock()
  60. puppet, ok := br.puppets[id]
  61. if !ok {
  62. dbPuppet := br.DB.Puppet.Get(id)
  63. if dbPuppet == nil {
  64. dbPuppet = br.DB.Puppet.New()
  65. dbPuppet.ID = id
  66. dbPuppet.Insert()
  67. }
  68. puppet = br.NewPuppet(dbPuppet)
  69. br.puppets[puppet.ID] = puppet
  70. }
  71. return puppet
  72. }
  73. func (br *DiscordBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
  74. br.puppetsLock.Lock()
  75. defer br.puppetsLock.Unlock()
  76. puppet, ok := br.puppetsByCustomMXID[mxid]
  77. if !ok {
  78. dbPuppet := br.DB.Puppet.GetByCustomMXID(mxid)
  79. if dbPuppet == nil {
  80. return nil
  81. }
  82. puppet = br.NewPuppet(dbPuppet)
  83. br.puppets[puppet.ID] = puppet
  84. br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
  85. }
  86. return puppet
  87. }
  88. func (br *DiscordBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
  89. return br.dbPuppetsToPuppets(br.DB.Puppet.GetAllWithCustomMXID())
  90. }
  91. func (br *DiscordBridge) GetAllPuppets() []*Puppet {
  92. return br.dbPuppetsToPuppets(br.DB.Puppet.GetAll())
  93. }
  94. func (br *DiscordBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
  95. br.puppetsLock.Lock()
  96. defer br.puppetsLock.Unlock()
  97. output := make([]*Puppet, len(dbPuppets))
  98. for index, dbPuppet := range dbPuppets {
  99. if dbPuppet == nil {
  100. continue
  101. }
  102. puppet, ok := br.puppets[dbPuppet.ID]
  103. if !ok {
  104. puppet = br.NewPuppet(dbPuppet)
  105. br.puppets[dbPuppet.ID] = puppet
  106. if dbPuppet.CustomMXID != "" {
  107. br.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
  108. }
  109. }
  110. output[index] = puppet
  111. }
  112. return output
  113. }
  114. func (br *DiscordBridge) FormatPuppetMXID(did string) id.UserID {
  115. return id.NewUserID(
  116. br.Config.Bridge.FormatUsername(did),
  117. br.Config.Homeserver.Domain,
  118. )
  119. }
  120. func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
  121. return puppet.bridge.AS.Intent(puppet.MXID)
  122. }
  123. func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
  124. if puppet.customIntent == nil || (portal.Key.Receiver != "" && portal.Key.Receiver != puppet.ID) {
  125. return puppet.DefaultIntent()
  126. }
  127. return puppet.customIntent
  128. }
  129. func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
  130. return puppet.customIntent
  131. }
  132. func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
  133. for _, portal := range puppet.bridge.GetDMPortalsWith(puppet.ID) {
  134. // Get room create lock to prevent races between receiving contact info and room creation.
  135. portal.roomCreateLock.Lock()
  136. meta(portal)
  137. portal.roomCreateLock.Unlock()
  138. }
  139. }
  140. func (puppet *Puppet) UpdateName(info *discordgo.User) bool {
  141. newName := puppet.bridge.Config.Bridge.FormatDisplayname(info)
  142. if puppet.Name == newName && puppet.NameSet {
  143. return false
  144. }
  145. puppet.Name = newName
  146. puppet.NameSet = false
  147. err := puppet.DefaultIntent().SetDisplayName(newName)
  148. if err != nil {
  149. puppet.log.Warnln("Failed to update displayname:", err)
  150. } else {
  151. go puppet.updatePortalMeta(func(portal *Portal) {
  152. if portal.UpdateName(puppet.Name) {
  153. portal.Update()
  154. }
  155. })
  156. puppet.NameSet = true
  157. }
  158. return true
  159. }
  160. func (puppet *Puppet) UpdateAvatar(info *discordgo.User) bool {
  161. if puppet.Avatar == info.Avatar && puppet.AvatarSet {
  162. return false
  163. }
  164. avatarChanged := info.Avatar != puppet.Avatar
  165. puppet.Avatar = info.Avatar
  166. puppet.AvatarSet = false
  167. puppet.AvatarURL = id.ContentURI{}
  168. // TODO should we just use discord's default avatars for users with no avatar?
  169. if puppet.Avatar != "" && (puppet.AvatarURL.IsEmpty() || avatarChanged) {
  170. url, err := uploadAvatar(puppet.DefaultIntent(), info.AvatarURL(""))
  171. if err != nil {
  172. puppet.log.Warnfln("Failed to reupload user avatar %s: %v", puppet.Avatar, err)
  173. return true
  174. }
  175. puppet.AvatarURL = url
  176. }
  177. err := puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
  178. if err != nil {
  179. puppet.log.Warnln("Failed to update avatar:", err)
  180. } else {
  181. go puppet.updatePortalMeta(func(portal *Portal) {
  182. if portal.UpdateAvatarFromPuppet(puppet) {
  183. portal.Update()
  184. }
  185. })
  186. puppet.AvatarSet = true
  187. }
  188. return true
  189. }
  190. func (puppet *Puppet) UpdateInfo(source *User, info *discordgo.User) {
  191. puppet.syncLock.Lock()
  192. defer puppet.syncLock.Unlock()
  193. if info == nil || len(info.Username) == 0 || len(info.Discriminator) == 0 {
  194. if puppet.Name != "" {
  195. return
  196. }
  197. var err error
  198. puppet.log.Debugfln("Fetching info through %s to update", source.DiscordID)
  199. info, err = source.Session.User(puppet.ID)
  200. if err != nil {
  201. puppet.log.Errorfln("Failed to fetch info through %s: %v", source.DiscordID, err)
  202. return
  203. }
  204. }
  205. err := puppet.DefaultIntent().EnsureRegistered()
  206. if err != nil {
  207. puppet.log.Errorln("Failed to ensure registered:", err)
  208. }
  209. changed := false
  210. changed = puppet.UpdateName(info) || changed
  211. changed = puppet.UpdateAvatar(info) || changed
  212. if changed {
  213. puppet.Update()
  214. }
  215. }