main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
  76. discordLog = br.Log.Sub("Discord")
  77. }
  78. func (br *DiscordBridge) Start() {
  79. if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
  80. br.provisioning = newProvisioningAPI(br)
  81. }
  82. go br.startUsers()
  83. }
  84. func (br *DiscordBridge) Stop() {
  85. for _, user := range br.usersByMXID {
  86. if user.Session == nil {
  87. continue
  88. }
  89. br.Log.Debugln("Disconnecting", user.MXID)
  90. user.Session.Close()
  91. }
  92. }
  93. func (br *DiscordBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
  94. p := br.GetPortalByMXID(mxid)
  95. if p == nil {
  96. return nil
  97. }
  98. return p
  99. }
  100. func (br *DiscordBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
  101. p := br.GetUserByMXID(mxid)
  102. if p == nil {
  103. return nil
  104. }
  105. return p
  106. }
  107. func (br *DiscordBridge) IsGhost(mxid id.UserID) bool {
  108. _, isGhost := br.ParsePuppetMXID(mxid)
  109. return isGhost
  110. }
  111. func (br *DiscordBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
  112. p := br.GetPuppetByMXID(mxid)
  113. if p == nil {
  114. return nil
  115. }
  116. return p
  117. }
  118. func (br *DiscordBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
  119. //TODO implement
  120. }
  121. func main() {
  122. br := &DiscordBridge{
  123. usersByMXID: make(map[id.UserID]*User),
  124. usersByID: make(map[string]*User),
  125. managementRooms: make(map[id.RoomID]*User),
  126. portalsByMXID: make(map[id.RoomID]*Portal),
  127. portalsByID: make(map[database.PortalKey]*Portal),
  128. threadsByID: make(map[string]*Thread),
  129. threadsByRootMXID: make(map[id.EventID]*Thread),
  130. threadsByCreationNoticeMXID: make(map[id.EventID]*Thread),
  131. guildsByID: make(map[string]*Guild),
  132. guildsByMXID: make(map[id.RoomID]*Guild),
  133. puppets: make(map[string]*Puppet),
  134. puppetsByCustomMXID: make(map[id.UserID]*Puppet),
  135. attachmentTransfers: util.NewSyncMap[attachmentKey, *util.ReturnableOnce[*database.File]](),
  136. }
  137. br.Bridge = bridge.Bridge{
  138. Name: "mautrix-discord",
  139. URL: "https://github.com/mautrix/discord",
  140. Description: "A Matrix-Discord puppeting bridge.",
  141. Version: "0.1.0",
  142. ProtocolName: "Discord",
  143. CryptoPickleKey: "maunium.net/go/mautrix-whatsapp",
  144. ConfigUpgrader: &configupgrade.StructUpgrader{
  145. SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
  146. Blocks: config.SpacedBlocks,
  147. Base: ExampleConfig,
  148. },
  149. Child: br,
  150. }
  151. br.InitVersion(Tag, Commit, BuildTime)
  152. br.Main()
  153. }