main.go 5.3 KB

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