2022-05-22 19:16:42 +00:00
|
|
|
// mautrix-discord - A Matrix-Discord puppeting bridge.
|
|
|
|
// Copyright (C) 2022 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-11-18 07:30:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-22 19:16:42 +00:00
|
|
|
_ "embed"
|
|
|
|
"sync"
|
2021-11-18 07:30:50 +00:00
|
|
|
|
2023-08-16 21:54:38 +00:00
|
|
|
"go.mau.fi/util/configupgrade"
|
|
|
|
"go.mau.fi/util/exsync"
|
2023-06-23 12:32:18 +00:00
|
|
|
"golang.org/x/sync/semaphore"
|
2022-05-22 19:16:42 +00:00
|
|
|
"maunium.net/go/mautrix/bridge"
|
|
|
|
"maunium.net/go/mautrix/bridge/commands"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2021-11-18 07:30:50 +00:00
|
|
|
|
2022-05-20 13:37:41 +00:00
|
|
|
"go.mau.fi/mautrix-discord/config"
|
2022-05-28 20:03:24 +00:00
|
|
|
"go.mau.fi/mautrix-discord/database"
|
2021-11-18 07:30:50 +00:00
|
|
|
)
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
// Information to find out exactly which commit the bridge was built from.
|
|
|
|
// These are filled at build time with the -X linker flag.
|
|
|
|
var (
|
|
|
|
Tag = "unknown"
|
|
|
|
Commit = "unknown"
|
|
|
|
BuildTime = "unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed example-config.yaml
|
|
|
|
var ExampleConfig string
|
|
|
|
|
|
|
|
type DiscordBridge struct {
|
|
|
|
bridge.Bridge
|
|
|
|
|
|
|
|
Config *config.Config
|
|
|
|
DB *database.Database
|
|
|
|
|
2024-02-18 21:10:19 +00:00
|
|
|
DMA *DirectMediaAPI
|
2022-05-22 19:16:42 +00:00
|
|
|
provisioning *ProvisioningAPI
|
|
|
|
|
|
|
|
usersByMXID map[id.UserID]*User
|
|
|
|
usersByID map[string]*User
|
|
|
|
usersLock sync.Mutex
|
|
|
|
|
|
|
|
managementRooms map[id.RoomID]*User
|
|
|
|
managementRoomsLock sync.Mutex
|
|
|
|
|
|
|
|
portalsByMXID map[id.RoomID]*Portal
|
|
|
|
portalsByID map[database.PortalKey]*Portal
|
|
|
|
portalsLock sync.Mutex
|
|
|
|
|
2022-10-28 20:35:31 +00:00
|
|
|
threadsByID map[string]*Thread
|
|
|
|
threadsByRootMXID map[id.EventID]*Thread
|
|
|
|
threadsByCreationNoticeMXID map[id.EventID]*Thread
|
|
|
|
threadsLock sync.Mutex
|
2022-05-28 20:03:24 +00:00
|
|
|
|
|
|
|
guildsByMXID map[id.RoomID]*Guild
|
|
|
|
guildsByID map[string]*Guild
|
|
|
|
guildsLock sync.Mutex
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
puppets map[string]*Puppet
|
|
|
|
puppetsByCustomMXID map[id.UserID]*Puppet
|
|
|
|
puppetsLock sync.Mutex
|
2023-01-31 11:11:02 +00:00
|
|
|
|
2023-08-16 21:54:38 +00:00
|
|
|
attachmentTransfers *exsync.Map[attachmentKey, *exsync.ReturnableOnce[*database.File]]
|
2023-06-23 12:32:18 +00:00
|
|
|
parallelAttachmentSemaphore *semaphore.Weighted
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetExampleConfig() string {
|
|
|
|
return ExampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetConfigPtr() interface{} {
|
|
|
|
br.Config = &config.Config{
|
|
|
|
BaseConfig: &br.Bridge.Config,
|
|
|
|
}
|
|
|
|
br.Config.BaseConfig.Bridge = &br.Config.Bridge
|
|
|
|
return br.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) Init() {
|
|
|
|
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
|
|
|
|
br.RegisterCommands()
|
|
|
|
|
2023-02-27 20:47:45 +00:00
|
|
|
matrixHTMLParser.PillConverter = br.pillConverter
|
|
|
|
|
2022-08-15 13:43:55 +00:00
|
|
|
br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
|
2023-02-26 19:50:16 +00:00
|
|
|
discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
|
2022-05-22 19:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) Start() {
|
|
|
|
if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
|
|
|
|
br.provisioning = newProvisioningAPI(br)
|
|
|
|
}
|
2024-02-18 21:10:19 +00:00
|
|
|
br.DMA = newDirectMediaAPI(br)
|
2023-06-09 14:28:24 +00:00
|
|
|
br.WaitWebsocketConnected()
|
2022-05-22 19:16:42 +00:00
|
|
|
go br.startUsers()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) Stop() {
|
|
|
|
for _, user := range br.usersByMXID {
|
|
|
|
if user.Session == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
br.Log.Debugln("Disconnecting", user.MXID)
|
|
|
|
user.Session.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
|
|
|
|
p := br.GetPortalByMXID(mxid)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
|
|
|
|
p := br.GetUserByMXID(mxid)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) IsGhost(mxid id.UserID) bool {
|
|
|
|
_, isGhost := br.ParsePuppetMXID(mxid)
|
|
|
|
return isGhost
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *DiscordBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
|
|
|
|
p := br.GetPuppetByMXID(mxid)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2021-11-18 07:30:50 +00:00
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
func (br *DiscordBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
|
|
|
|
//TODO implement
|
2021-11-18 07:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-05-22 19:16:42 +00:00
|
|
|
br := &DiscordBridge{
|
|
|
|
usersByMXID: make(map[id.UserID]*User),
|
|
|
|
usersByID: make(map[string]*User),
|
|
|
|
|
|
|
|
managementRooms: make(map[id.RoomID]*User),
|
|
|
|
|
|
|
|
portalsByMXID: make(map[id.RoomID]*Portal),
|
|
|
|
portalsByID: make(map[database.PortalKey]*Portal),
|
|
|
|
|
2022-10-28 20:35:31 +00:00
|
|
|
threadsByID: make(map[string]*Thread),
|
|
|
|
threadsByRootMXID: make(map[id.EventID]*Thread),
|
|
|
|
threadsByCreationNoticeMXID: make(map[id.EventID]*Thread),
|
2022-05-28 20:03:24 +00:00
|
|
|
|
|
|
|
guildsByID: make(map[string]*Guild),
|
|
|
|
guildsByMXID: make(map[id.RoomID]*Guild),
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
puppets: make(map[string]*Puppet),
|
|
|
|
puppetsByCustomMXID: make(map[id.UserID]*Puppet),
|
2023-01-31 11:11:02 +00:00
|
|
|
|
2023-08-16 21:54:38 +00:00
|
|
|
attachmentTransfers: exsync.NewMap[attachmentKey, *exsync.ReturnableOnce[*database.File]](),
|
2023-06-23 12:32:18 +00:00
|
|
|
parallelAttachmentSemaphore: semaphore.NewWeighted(3),
|
2021-11-18 07:30:50 +00:00
|
|
|
}
|
2022-05-22 19:16:42 +00:00
|
|
|
br.Bridge = bridge.Bridge{
|
2023-04-14 06:16:58 +00:00
|
|
|
Name: "mautrix-discord",
|
|
|
|
URL: "https://github.com/mautrix/discord",
|
|
|
|
Description: "A Matrix-Discord puppeting bridge.",
|
2024-01-16 14:19:53 +00:00
|
|
|
Version: "0.6.5",
|
2023-04-14 06:16:58 +00:00
|
|
|
ProtocolName: "Discord",
|
|
|
|
BeeperServiceName: "discordgo",
|
|
|
|
BeeperNetworkName: "discord",
|
2022-05-22 19:16:42 +00:00
|
|
|
|
2022-05-31 10:07:41 +00:00
|
|
|
CryptoPickleKey: "maunium.net/go/mautrix-whatsapp",
|
|
|
|
|
2022-05-22 19:16:42 +00:00
|
|
|
ConfigUpgrader: &configupgrade.StructUpgrader{
|
|
|
|
SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
|
|
|
|
Blocks: config.SpacedBlocks,
|
|
|
|
Base: ExampleConfig,
|
|
|
|
},
|
|
|
|
|
|
|
|
Child: br,
|
|
|
|
}
|
|
|
|
br.InitVersion(Tag, Commit, BuildTime)
|
|
|
|
|
|
|
|
br.Main()
|
2021-11-18 07:30:50 +00:00
|
|
|
}
|