Somewhat correctly setting up rooms and puppets
This commit is contained in:
331
main.go
331
main.go
@@ -18,8 +18,11 @@ package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"github.com/beeper/groupme-lib"
|
||||
"go.mau.fi/util/configupgrade"
|
||||
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
||||
"regexp"
|
||||
"sync"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
@@ -43,6 +46,8 @@ var (
|
||||
//go:embed example-config.yaml
|
||||
var ExampleConfig string
|
||||
|
||||
const unstableFeatureBatchSending = "org.matrix.msc2716"
|
||||
|
||||
type GMBridge struct {
|
||||
bridge.Bridge
|
||||
Config *config.Config
|
||||
@@ -150,7 +155,331 @@ func (br *GMBridge) GetConfigPtr() interface{} {
|
||||
return br.Config
|
||||
}
|
||||
|
||||
const unstableFeatureBatchSending = "org.matrix.msc2716"
|
||||
func (bridge *GMBridge) GetPortalByGMID(key database.PortalKey) *Portal {
|
||||
bridge.portalsLock.Lock()
|
||||
defer bridge.portalsLock.Unlock()
|
||||
portal, ok := bridge.portalsByGMID[key]
|
||||
if !ok {
|
||||
return bridge.loadDBPortal(bridge.DB.Portal.GetByGMID(key), &key)
|
||||
}
|
||||
return portal
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetAllPortals() []*Portal {
|
||||
return br.dbPortalsToPortals(br.DB.Portal.GetAll())
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetAllIPortals() (iportals []bridge.Portal) {
|
||||
portals := br.GetAllPortals()
|
||||
iportals = make([]bridge.Portal, len(portals))
|
||||
for i, portal := range portals {
|
||||
iportals[i] = portal
|
||||
}
|
||||
return iportals
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetAllPortalsByGMID(gmid groupme.ID) []*Portal {
|
||||
return br.dbPortalsToPortals(br.DB.Portal.GetAllByGMID(gmid))
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetPortalByMXID(mxid id.RoomID) *Portal {
|
||||
bridge.portalsLock.Lock()
|
||||
defer bridge.portalsLock.Unlock()
|
||||
portal, ok := bridge.portalsByMXID[mxid]
|
||||
if !ok {
|
||||
return bridge.loadDBPortal(bridge.DB.Portal.GetByMXID(mxid), nil)
|
||||
}
|
||||
return portal
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
|
||||
p := br.GetPortalByMXID(mxid)
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (br *GMBridge) getUserByMXID(userID id.UserID, onlyIfExists bool) *User {
|
||||
_, isPuppet := br.ParsePuppetMXID(userID)
|
||||
if isPuppet || userID == br.Bot.UserID {
|
||||
return nil
|
||||
}
|
||||
br.usersLock.Lock()
|
||||
defer br.usersLock.Unlock()
|
||||
user, ok := br.usersByMXID[userID]
|
||||
if !ok {
|
||||
userIDPtr := &userID
|
||||
if onlyIfExists {
|
||||
userIDPtr = nil
|
||||
}
|
||||
return br.loadDBUser(br.DB.User.GetByMXID(userID), userIDPtr)
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetUserByMXID(userID id.UserID) *User {
|
||||
return br.getUserByMXID(userID, false)
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetIUser(userID id.UserID, create bool) bridge.User {
|
||||
u := br.getUserByMXID(userID, !create)
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
return u
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetUserByMXIDIfExists(userID id.UserID) *User {
|
||||
return br.getUserByMXID(userID, true)
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetUserByGMID(gmid groupme.ID) *User {
|
||||
bridge.usersLock.Lock()
|
||||
defer bridge.usersLock.Unlock()
|
||||
user, ok := bridge.usersByGMID[gmid]
|
||||
if !ok {
|
||||
return bridge.loadDBUser(bridge.DB.User.GetByGMID(gmid), nil)
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetAllUsers() []*User {
|
||||
br.usersLock.Lock()
|
||||
defer br.usersLock.Unlock()
|
||||
dbUsers := br.DB.User.GetAll()
|
||||
output := make([]*User, len(dbUsers))
|
||||
for index, dbUser := range dbUsers {
|
||||
user, ok := br.usersByMXID[dbUser.MXID]
|
||||
if !ok {
|
||||
user = br.loadDBUser(dbUser, nil)
|
||||
}
|
||||
output[index] = user
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (br *GMBridge) loadDBUser(dbUser *database.User, mxid *id.UserID) *User {
|
||||
if dbUser == nil {
|
||||
if mxid == nil {
|
||||
return nil
|
||||
}
|
||||
dbUser = br.DB.User.New()
|
||||
dbUser.MXID = *mxid
|
||||
dbUser.Insert()
|
||||
}
|
||||
user := br.NewUser(dbUser)
|
||||
br.usersByMXID[user.MXID] = user
|
||||
if len(user.GMID) > 0 {
|
||||
br.usersByGMID[user.GMID] = user
|
||||
}
|
||||
if len(user.ManagementRoom) > 0 {
|
||||
br.managementRooms[user.ManagementRoom] = user
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (br *GMBridge) NewUser(dbUser *database.User) *User {
|
||||
user := &User{
|
||||
User: dbUser,
|
||||
bridge: br,
|
||||
log: br.Log.Sub("User").Sub(string(dbUser.MXID)),
|
||||
|
||||
chatListReceived: make(chan struct{}, 1),
|
||||
syncPortalsDone: make(chan struct{}, 1),
|
||||
syncStart: make(chan struct{}, 1),
|
||||
messageInput: make(chan PortalMessage),
|
||||
messageOutput: make(chan PortalMessage, br.Config.Bridge.PortalMessageBuffer),
|
||||
}
|
||||
|
||||
user.PermissionLevel = user.bridge.Config.Bridge.Permissions.Get(user.MXID)
|
||||
user.Whitelisted = user.PermissionLevel >= bridgeconfig.PermissionLevelUser
|
||||
user.Admin = user.PermissionLevel >= bridgeconfig.PermissionLevelAdmin
|
||||
user.BridgeState = br.NewBridgeStateQueue(user)
|
||||
go user.handleMessageLoop()
|
||||
go user.runMessageRingBuffer()
|
||||
return user
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
|
||||
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAllWithCustomMXID())
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetAllPuppets() []*Puppet {
|
||||
return bridge.dbPuppetsToPuppets(bridge.DB.Puppet.GetAll())
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
|
||||
bridge.puppetsLock.Lock()
|
||||
defer bridge.puppetsLock.Unlock()
|
||||
output := make([]*Puppet, len(dbPuppets))
|
||||
for index, dbPuppet := range dbPuppets {
|
||||
if dbPuppet == nil {
|
||||
continue
|
||||
}
|
||||
puppet, ok := bridge.puppets[dbPuppet.GMID]
|
||||
if !ok {
|
||||
puppet = bridge.NewPuppet(dbPuppet)
|
||||
bridge.puppets[dbPuppet.GMID] = puppet
|
||||
if len(dbPuppet.CustomMXID) > 0 {
|
||||
bridge.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
|
||||
}
|
||||
}
|
||||
output[index] = puppet
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) FormatPuppetMXID(gmid groupme.ID) id.UserID {
|
||||
return id.NewUserID(
|
||||
bridge.Config.Bridge.FormatUsername(gmid.String()),
|
||||
bridge.Config.Homeserver.Domain)
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
||||
return &Puppet{
|
||||
Puppet: dbPuppet,
|
||||
bridge: bridge,
|
||||
log: bridge.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.GMID)),
|
||||
|
||||
MXID: bridge.FormatPuppetMXID(dbPuppet.GMID),
|
||||
}
|
||||
}
|
||||
|
||||
func (br *GMBridge) IsGhost(id id.UserID) bool {
|
||||
_, ok := br.ParsePuppetMXID(id)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (br *GMBridge) GetIGhost(id id.UserID) bridge.Ghost {
|
||||
p := br.GetPuppetByMXID(id)
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) ParsePuppetMXID(mxid id.UserID) (groupme.ID, bool) {
|
||||
if userIDRegex == nil {
|
||||
userIDRegex = regexp.MustCompile(fmt.Sprintf("^@%s:%s$",
|
||||
bridge.Config.Bridge.FormatUsername("([0-9]+)"),
|
||||
bridge.Config.Homeserver.Domain))
|
||||
}
|
||||
match := userIDRegex.FindStringSubmatch(string(mxid))
|
||||
if match == nil || len(match) != 2 {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return groupme.ID(match[1]), true
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
||||
gmid, ok := bridge.ParsePuppetMXID(mxid)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return bridge.GetPuppetByGMID(gmid)
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetPuppetByGMID(gmid groupme.ID) *Puppet {
|
||||
bridge.puppetsLock.Lock()
|
||||
defer bridge.puppetsLock.Unlock()
|
||||
puppet, ok := bridge.puppets[gmid]
|
||||
if !ok {
|
||||
dbPuppet := bridge.DB.Puppet.Get(gmid)
|
||||
if dbPuppet == nil {
|
||||
dbPuppet = bridge.DB.Puppet.New()
|
||||
dbPuppet.GMID = gmid
|
||||
dbPuppet.Insert()
|
||||
}
|
||||
puppet = bridge.NewPuppet(dbPuppet)
|
||||
bridge.puppets[puppet.GMID] = puppet
|
||||
if len(puppet.CustomMXID) > 0 {
|
||||
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
||||
}
|
||||
}
|
||||
return puppet
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
|
||||
bridge.puppetsLock.Lock()
|
||||
defer bridge.puppetsLock.Unlock()
|
||||
puppet, ok := bridge.puppetsByCustomMXID[mxid]
|
||||
if !ok {
|
||||
dbPuppet := bridge.DB.Puppet.GetByCustomMXID(mxid)
|
||||
if dbPuppet == nil {
|
||||
return nil
|
||||
}
|
||||
puppet = bridge.NewPuppet(dbPuppet)
|
||||
bridge.puppets[puppet.GMID] = puppet
|
||||
bridge.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
||||
}
|
||||
return puppet
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal {
|
||||
bridge.portalsLock.Lock()
|
||||
defer bridge.portalsLock.Unlock()
|
||||
output := make([]*Portal, len(dbPortals))
|
||||
for index, dbPortal := range dbPortals {
|
||||
if dbPortal == nil {
|
||||
continue
|
||||
}
|
||||
portal, ok := bridge.portalsByGMID[dbPortal.Key]
|
||||
if !ok {
|
||||
portal = bridge.loadDBPortal(dbPortal, nil)
|
||||
}
|
||||
output[index] = portal
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) loadDBPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal {
|
||||
if dbPortal == nil {
|
||||
if key == nil {
|
||||
return nil
|
||||
}
|
||||
dbPortal = bridge.DB.Portal.New()
|
||||
dbPortal.Key = *key
|
||||
dbPortal.Insert()
|
||||
}
|
||||
portal := bridge.NewPortal(dbPortal)
|
||||
bridge.portalsByGMID[portal.Key] = portal
|
||||
if len(portal.MXID) > 0 {
|
||||
bridge.portalsByMXID[portal.MXID] = portal
|
||||
}
|
||||
return portal
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) NewManualPortal(key database.PortalKey) *Portal {
|
||||
portal := &Portal{
|
||||
Portal: bridge.DB.Portal.New(),
|
||||
bridge: bridge,
|
||||
log: bridge.Log.Sub(fmt.Sprintf("Portal/%s", key)),
|
||||
|
||||
recentlyHandled: make([]string, recentlyHandledLength),
|
||||
|
||||
messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer),
|
||||
}
|
||||
portal.Key = key
|
||||
go portal.handleMessageLoop()
|
||||
return portal
|
||||
}
|
||||
|
||||
func (bridge *GMBridge) NewPortal(dbPortal *database.Portal) *Portal {
|
||||
portal := &Portal{
|
||||
Portal: dbPortal,
|
||||
bridge: bridge,
|
||||
log: bridge.Log.Sub(fmt.Sprintf("Portal/%s", dbPortal.Key)),
|
||||
|
||||
recentlyHandled: make([]string, recentlyHandledLength),
|
||||
|
||||
messages: make(chan PortalMessage, bridge.Config.Bridge.PortalMessageBuffer),
|
||||
}
|
||||
go portal.handleMessageLoop()
|
||||
return portal
|
||||
}
|
||||
|
||||
func (br *GMBridge) CheckFeatures(versions *mautrix.RespVersions) (string, bool) {
|
||||
if br.Config.Bridge.HistorySync.Backfill {
|
||||
|
||||
Reference in New Issue
Block a user