main.go 4.5 KB

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