main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. matrixHTMLParser.PillConverter = br.pillConverter
  80. br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
  81. discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
  82. // TODO move this to mautrix-go?
  83. zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
  84. files := strings.Split(file, "/")
  85. file = files[len(files)-1]
  86. name := runtime.FuncForPC(pc).Name()
  87. fns := strings.Split(name, ".")
  88. name = fns[len(fns)-1]
  89. return fmt.Sprintf("%s:%d:%s()", file, line, name)
  90. }
  91. }
  92. func (br *DiscordBridge) Start() {
  93. if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
  94. br.provisioning = newProvisioningAPI(br)
  95. }
  96. go br.startUsers()
  97. }
  98. func (br *DiscordBridge) Stop() {
  99. for _, user := range br.usersByMXID {
  100. if user.Session == nil {
  101. continue
  102. }
  103. br.Log.Debugln("Disconnecting", user.MXID)
  104. user.Session.Close()
  105. }
  106. }
  107. func (br *DiscordBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
  108. p := br.GetPortalByMXID(mxid)
  109. if p == nil {
  110. return nil
  111. }
  112. return p
  113. }
  114. func (br *DiscordBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
  115. p := br.GetUserByMXID(mxid)
  116. if p == nil {
  117. return nil
  118. }
  119. return p
  120. }
  121. func (br *DiscordBridge) IsGhost(mxid id.UserID) bool {
  122. _, isGhost := br.ParsePuppetMXID(mxid)
  123. return isGhost
  124. }
  125. func (br *DiscordBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
  126. p := br.GetPuppetByMXID(mxid)
  127. if p == nil {
  128. return nil
  129. }
  130. return p
  131. }
  132. func (br *DiscordBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
  133. //TODO implement
  134. }
  135. func main() {
  136. br := &DiscordBridge{
  137. usersByMXID: make(map[id.UserID]*User),
  138. usersByID: make(map[string]*User),
  139. managementRooms: make(map[id.RoomID]*User),
  140. portalsByMXID: make(map[id.RoomID]*Portal),
  141. portalsByID: make(map[database.PortalKey]*Portal),
  142. threadsByID: make(map[string]*Thread),
  143. threadsByRootMXID: make(map[id.EventID]*Thread),
  144. threadsByCreationNoticeMXID: make(map[id.EventID]*Thread),
  145. guildsByID: make(map[string]*Guild),
  146. guildsByMXID: make(map[id.RoomID]*Guild),
  147. puppets: make(map[string]*Puppet),
  148. puppetsByCustomMXID: make(map[id.UserID]*Puppet),
  149. attachmentTransfers: util.NewSyncMap[attachmentKey, *util.ReturnableOnce[*database.File]](),
  150. }
  151. br.Bridge = bridge.Bridge{
  152. Name: "mautrix-discord",
  153. URL: "https://github.com/mautrix/discord",
  154. Description: "A Matrix-Discord puppeting bridge.",
  155. Version: "0.1.1",
  156. ProtocolName: "Discord",
  157. CryptoPickleKey: "maunium.net/go/mautrix-whatsapp",
  158. ConfigUpgrader: &configupgrade.StructUpgrader{
  159. SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
  160. Blocks: config.SpacedBlocks,
  161. Base: ExampleConfig,
  162. },
  163. Child: br,
  164. }
  165. br.InitVersion(Tag, Commit, BuildTime)
  166. br.Main()
  167. }