main.go 4.7 KB

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