main.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "maunium.net/go/mautrix/bridge"
  21. "maunium.net/go/mautrix/bridge/commands"
  22. "maunium.net/go/mautrix/id"
  23. "maunium.net/go/mautrix/util"
  24. "maunium.net/go/mautrix/util/configupgrade"
  25. "go.mau.fi/mautrix-discord/config"
  26. "go.mau.fi/mautrix-discord/database"
  27. )
  28. // Information to find out exactly which commit the bridge was built from.
  29. // These are filled at build time with the -X linker flag.
  30. var (
  31. Tag = "unknown"
  32. Commit = "unknown"
  33. BuildTime = "unknown"
  34. )
  35. //go:embed example-config.yaml
  36. var ExampleConfig string
  37. type DiscordBridge struct {
  38. bridge.Bridge
  39. Config *config.Config
  40. DB *database.Database
  41. provisioning *ProvisioningAPI
  42. usersByMXID map[id.UserID]*User
  43. usersByID map[string]*User
  44. usersLock sync.Mutex
  45. managementRooms map[id.RoomID]*User
  46. managementRoomsLock sync.Mutex
  47. portalsByMXID map[id.RoomID]*Portal
  48. portalsByID map[database.PortalKey]*Portal
  49. portalsLock sync.Mutex
  50. threadsByID map[string]*Thread
  51. threadsByRootMXID map[id.EventID]*Thread
  52. threadsByCreationNoticeMXID map[id.EventID]*Thread
  53. threadsLock sync.Mutex
  54. guildsByMXID map[id.RoomID]*Guild
  55. guildsByID map[string]*Guild
  56. guildsLock sync.Mutex
  57. puppets map[string]*Puppet
  58. puppetsByCustomMXID map[id.UserID]*Puppet
  59. puppetsLock sync.Mutex
  60. attachmentTransfers *util.SyncMap[attachmentKey, *util.ReturnableOnce[*database.File]]
  61. }
  62. func (br *DiscordBridge) GetExampleConfig() string {
  63. return ExampleConfig
  64. }
  65. func (br *DiscordBridge) GetConfigPtr() interface{} {
  66. br.Config = &config.Config{
  67. BaseConfig: &br.Bridge.Config,
  68. }
  69. br.Config.BaseConfig.Bridge = &br.Config.Bridge
  70. return br.Config
  71. }
  72. func (br *DiscordBridge) Init() {
  73. br.CommandProcessor = commands.NewProcessor(&br.Bridge)
  74. br.RegisterCommands()
  75. matrixHTMLParser.PillConverter = br.pillConverter
  76. br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
  77. discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
  78. }
  79. func (br *DiscordBridge) Start() {
  80. if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
  81. br.provisioning = newProvisioningAPI(br)
  82. }
  83. br.WaitWebsocketConnected()
  84. go br.startUsers()
  85. }
  86. func (br *DiscordBridge) Stop() {
  87. for _, user := range br.usersByMXID {
  88. if user.Session == nil {
  89. continue
  90. }
  91. br.Log.Debugln("Disconnecting", user.MXID)
  92. user.Session.Close()
  93. }
  94. }
  95. func (br *DiscordBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
  96. p := br.GetPortalByMXID(mxid)
  97. if p == nil {
  98. return nil
  99. }
  100. return p
  101. }
  102. func (br *DiscordBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
  103. p := br.GetUserByMXID(mxid)
  104. if p == nil {
  105. return nil
  106. }
  107. return p
  108. }
  109. func (br *DiscordBridge) IsGhost(mxid id.UserID) bool {
  110. _, isGhost := br.ParsePuppetMXID(mxid)
  111. return isGhost
  112. }
  113. func (br *DiscordBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
  114. p := br.GetPuppetByMXID(mxid)
  115. if p == nil {
  116. return nil
  117. }
  118. return p
  119. }
  120. func (br *DiscordBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
  121. //TODO implement
  122. }
  123. func main() {
  124. br := &DiscordBridge{
  125. usersByMXID: make(map[id.UserID]*User),
  126. usersByID: make(map[string]*User),
  127. managementRooms: make(map[id.RoomID]*User),
  128. portalsByMXID: make(map[id.RoomID]*Portal),
  129. portalsByID: make(map[database.PortalKey]*Portal),
  130. threadsByID: make(map[string]*Thread),
  131. threadsByRootMXID: make(map[id.EventID]*Thread),
  132. threadsByCreationNoticeMXID: make(map[id.EventID]*Thread),
  133. guildsByID: make(map[string]*Guild),
  134. guildsByMXID: make(map[id.RoomID]*Guild),
  135. puppets: make(map[string]*Puppet),
  136. puppetsByCustomMXID: make(map[id.UserID]*Puppet),
  137. attachmentTransfers: util.NewSyncMap[attachmentKey, *util.ReturnableOnce[*database.File]](),
  138. }
  139. br.Bridge = bridge.Bridge{
  140. Name: "mautrix-discord",
  141. URL: "https://github.com/mautrix/discord",
  142. Description: "A Matrix-Discord puppeting bridge.",
  143. Version: "0.5.0",
  144. ProtocolName: "Discord",
  145. BeeperServiceName: "discordgo",
  146. BeeperNetworkName: "discord",
  147. CryptoPickleKey: "maunium.net/go/mautrix-whatsapp",
  148. ConfigUpgrader: &configupgrade.StructUpgrader{
  149. SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
  150. Blocks: config.SpacedBlocks,
  151. Base: ExampleConfig,
  152. },
  153. Child: br,
  154. }
  155. br.InitVersion(Tag, Commit, BuildTime)
  156. br.Main()
  157. }