main.go 4.0 KB

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