main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // mautrix-discord - A Matrix-Discord puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. _ "embed"
  19. "sync"
  20. "go.mau.fi/util/configupgrade"
  21. "go.mau.fi/util/exsync"
  22. "golang.org/x/sync/semaphore"
  23. "maunium.net/go/mautrix/bridge"
  24. "maunium.net/go/mautrix/bridge/commands"
  25. "maunium.net/go/mautrix/id"
  26. "go.mau.fi/mautrix-discord/config"
  27. "go.mau.fi/mautrix-discord/database"
  28. )
  29. // Information to find out exactly which commit the bridge was built from.
  30. // These are filled at build time with the -X linker flag.
  31. var (
  32. Tag = "unknown"
  33. Commit = "unknown"
  34. BuildTime = "unknown"
  35. )
  36. //go:embed example-config.yaml
  37. var ExampleConfig string
  38. type DiscordBridge struct {
  39. bridge.Bridge
  40. Config *config.Config
  41. DB *database.Database
  42. provisioning *ProvisioningAPI
  43. usersByMXID map[id.UserID]*User
  44. usersByID map[string]*User
  45. usersLock sync.Mutex
  46. managementRooms map[id.RoomID]*User
  47. managementRoomsLock sync.Mutex
  48. portalsByMXID map[id.RoomID]*Portal
  49. portalsByID map[database.PortalKey]*Portal
  50. portalsLock sync.Mutex
  51. threadsByID map[string]*Thread
  52. threadsByRootMXID map[id.EventID]*Thread
  53. threadsByCreationNoticeMXID map[id.EventID]*Thread
  54. threadsLock sync.Mutex
  55. guildsByMXID map[id.RoomID]*Guild
  56. guildsByID map[string]*Guild
  57. guildsLock sync.Mutex
  58. puppets map[string]*Puppet
  59. puppetsByCustomMXID map[id.UserID]*Puppet
  60. puppetsLock sync.Mutex
  61. attachmentTransfers *exsync.Map[attachmentKey, *exsync.ReturnableOnce[*database.File]]
  62. parallelAttachmentSemaphore *semaphore.Weighted
  63. }
  64. func (br *DiscordBridge) GetExampleConfig() string {
  65. return ExampleConfig
  66. }
  67. func (br *DiscordBridge) GetConfigPtr() interface{} {
  68. br.Config = &config.Config{
  69. BaseConfig: &br.Bridge.Config,
  70. }
  71. br.Config.BaseConfig.Bridge = &br.Config.Bridge
  72. return br.Config
  73. }
  74. func (br *DiscordBridge) Init() {
  75. br.CommandProcessor = commands.NewProcessor(&br.Bridge)
  76. br.RegisterCommands()
  77. matrixHTMLParser.PillConverter = br.pillConverter
  78. br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
  79. discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
  80. }
  81. func (br *DiscordBridge) Start() {
  82. if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
  83. br.provisioning = newProvisioningAPI(br)
  84. }
  85. br.WaitWebsocketConnected()
  86. go br.startUsers()
  87. }
  88. func (br *DiscordBridge) Stop() {
  89. for _, user := range br.usersByMXID {
  90. if user.Session == nil {
  91. continue
  92. }
  93. br.Log.Debugln("Disconnecting", user.MXID)
  94. user.Session.Close()
  95. }
  96. }
  97. func (br *DiscordBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
  98. p := br.GetPortalByMXID(mxid)
  99. if p == nil {
  100. return nil
  101. }
  102. return p
  103. }
  104. func (br *DiscordBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
  105. p := br.GetUserByMXID(mxid)
  106. if p == nil {
  107. return nil
  108. }
  109. return p
  110. }
  111. func (br *DiscordBridge) IsGhost(mxid id.UserID) bool {
  112. _, isGhost := br.ParsePuppetMXID(mxid)
  113. return isGhost
  114. }
  115. func (br *DiscordBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
  116. p := br.GetPuppetByMXID(mxid)
  117. if p == nil {
  118. return nil
  119. }
  120. return p
  121. }
  122. func (br *DiscordBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
  123. //TODO implement
  124. }
  125. func main() {
  126. br := &DiscordBridge{
  127. usersByMXID: make(map[id.UserID]*User),
  128. usersByID: make(map[string]*User),
  129. managementRooms: make(map[id.RoomID]*User),
  130. portalsByMXID: make(map[id.RoomID]*Portal),
  131. portalsByID: make(map[database.PortalKey]*Portal),
  132. threadsByID: make(map[string]*Thread),
  133. threadsByRootMXID: make(map[id.EventID]*Thread),
  134. threadsByCreationNoticeMXID: make(map[id.EventID]*Thread),
  135. guildsByID: make(map[string]*Guild),
  136. guildsByMXID: make(map[id.RoomID]*Guild),
  137. puppets: make(map[string]*Puppet),
  138. puppetsByCustomMXID: make(map[id.UserID]*Puppet),
  139. attachmentTransfers: exsync.NewMap[attachmentKey, *exsync.ReturnableOnce[*database.File]](),
  140. parallelAttachmentSemaphore: semaphore.NewWeighted(3),
  141. }
  142. br.Bridge = bridge.Bridge{
  143. Name: "mautrix-discord",
  144. URL: "https://github.com/mautrix/discord",
  145. Description: "A Matrix-Discord puppeting bridge.",
  146. Version: "0.6.2",
  147. ProtocolName: "Discord",
  148. BeeperServiceName: "discordgo",
  149. BeeperNetworkName: "discord",
  150. CryptoPickleKey: "maunium.net/go/mautrix-whatsapp",
  151. ConfigUpgrader: &configupgrade.StructUpgrader{
  152. SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
  153. Blocks: config.SpacedBlocks,
  154. Base: ExampleConfig,
  155. },
  156. Child: br,
  157. }
  158. br.InitVersion(Tag, Commit, BuildTime)
  159. br.Main()
  160. }