mumble event listeners use non-global state

This commit is contained in:
stryan 2021-01-05 19:43:55 -05:00
parent 8e2fe4f3b7
commit 83712bfe15
3 changed files with 26 additions and 20 deletions

View File

@ -26,7 +26,7 @@ type BridgeState struct {
AutoChan chan bool AutoChan chan bool
} }
func startBridge(discord *discordgo.Session, discordGID string, discordCID string, config *gumble.Config, mumbleAddr string, mumbleInsecure bool, die chan bool) { func startBridge(discord *discordgo.Session, discordGID string, discordCID string, l *Listener, die chan bool) {
dgv, err := discord.ChannelVoiceJoin(discordGID, discordCID, false, false) dgv, err := discord.ChannelVoiceJoin(discordGID, discordCID, false, false)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -43,14 +43,14 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
} }
var tlsConfig tls.Config var tlsConfig tls.Config
if mumbleInsecure { if l.BridgeConf.MumbleInsecure {
tlsConfig.InsecureSkipVerify = true tlsConfig.InsecureSkipVerify = true
} }
config.Attach(gumbleutil.Listener{ l.BridgeConf.Config.Attach(gumbleutil.Listener{
Connect: mumbleConnect, Connect: l.mumbleConnect,
UserChange: mumbleUserChange, UserChange: l.mumbleUserChange,
}) })
mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig) mumble, err := gumble.DialWithDialer(new(net.Dialer), l.BridgeConf.MumbleAddr, l.BridgeConf.Config, &tlsConfig)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -68,7 +68,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin
// Start Passing Between // Start Passing Between
// Mumble // Mumble
go m.fromMumbleMixer(toDiscord, die) go m.fromMumbleMixer(toDiscord, die)
det := config.AudioListeners.Attach(m) det := l.BridgeConf.Config.AudioListeners.Attach(m)
//Discord //Discord
go discordReceivePCM(dgv, die) go discordReceivePCM(dgv, die)
@ -174,7 +174,7 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string) {
} }
} }
func AutoBridge(s *discordgo.Session) { func AutoBridge(s *discordgo.Session, l *Listener) {
log.Println("beginning auto mode") log.Println("beginning auto mode")
for { for {
select { select {
@ -188,7 +188,7 @@ func AutoBridge(s *discordgo.Session) {
log.Println("users detected in mumble and discord, bridging") log.Println("users detected in mumble and discord, bridging")
die := make(chan bool) die := make(chan bool)
Bridge.ActiveConn = die Bridge.ActiveConn = die
go startBridge(s, BridgeConf.GID, BridgeConf.CID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die) go startBridge(s, BridgeConf.GID, BridgeConf.CID, l, die)
} }
if Bridge.Connected && Bridge.MumbleUserCount == 0 && Bridge.DiscordUserCount <= 1 { if Bridge.Connected && Bridge.MumbleUserCount == 0 && Bridge.DiscordUserCount <= 1 {
log.Println("no one online, killing bridge") log.Println("no one online, killing bridge")

View File

@ -10,6 +10,11 @@ import (
"layeh.com/gumble/gumble" "layeh.com/gumble/gumble"
) )
type Listener struct {
BridgeConf *BridgeConfig
Bridge *BridgeState
}
func ready(s *discordgo.Session, event *discordgo.Ready) { func ready(s *discordgo.Session, event *discordgo.Ready) {
log.Println("READY event registered") log.Println("READY event registered")
} }
@ -47,7 +52,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID) log.Printf("Trying to join GID %v and VID %v\n", g.ID, vs.ChannelID)
die := make(chan bool) die := make(chan bool)
Bridge.ActiveConn = die Bridge.ActiveConn = die
go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die) //go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, die)
return return
} }
} }
@ -107,7 +112,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
DiscordReset() DiscordReset()
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
Bridge.ActiveConn = make(chan bool) Bridge.ActiveConn = make(chan bool)
go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, Bridge.ActiveConn) //go startBridge(s, g.ID, vs.ChannelID, BridgeConf.Config, BridgeConf.MumbleAddr, BridgeConf.MumbleInsecure, Bridge.ActiveConn)
return return
} }
} }
@ -117,7 +122,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if BridgeConf.Mode != BridgeModeAuto { if BridgeConf.Mode != BridgeModeAuto {
BridgeConf.Mode = BridgeModeAuto BridgeConf.Mode = BridgeModeAuto
Bridge.AutoChan = make(chan bool) Bridge.AutoChan = make(chan bool)
go AutoBridge(s) //go AutoBridge(s)
} else { } else {
Bridge.AutoChan <- true Bridge.AutoChan <- true
BridgeConf.Mode = BridgeModeManual BridgeConf.Mode = BridgeModeManual
@ -197,8 +202,8 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) {
return return
} }
func mumbleConnect(e *gumble.ConnectEvent) { func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) {
if BridgeConf.MumbleChannel != "" { if l.BridgeConf.MumbleChannel != "" {
//join specified channel //join specified channel
startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel) startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel)
if startingChannel != nil { if startingChannel != nil {
@ -207,15 +212,15 @@ func mumbleConnect(e *gumble.ConnectEvent) {
} }
} }
func mumbleUserChange(e *gumble.UserChangeEvent) { func (l *Listener) mumbleUserChange(e *gumble.UserChangeEvent) {
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) { if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) {
Bridge.MumbleUsers = make(map[string]bool) Bridge.MumbleUsers = make(map[string]bool)
for _, user := range Bridge.Client.Self.Channel.Users { for _, user := range l.Bridge.Client.Self.Channel.Users {
//note, this might be too slow for really really big channels? //note, this might be too slow for really really big channels?
//event listeners block while processing //event listeners block while processing
//also probably bad to rebuild the set every user change. //also probably bad to rebuild the set every user change.
if user.Name != Bridge.Client.Self.Name { if user.Name != l.Bridge.Client.Self.Name {
Bridge.MumbleUsers[user.Name] = true l.Bridge.MumbleUsers[user.Name] = true
} }
} }
} }

View File

@ -109,19 +109,20 @@ func main() {
DiscordUserCount: 0, DiscordUserCount: 0,
DiscordUsers: make(map[string]bool), DiscordUsers: make(map[string]bool),
} }
l := &Listener{BridgeConf, Bridge}
switch *mode { switch *mode {
case "auto": case "auto":
log.Println("bridge starting in automatic mode") log.Println("bridge starting in automatic mode")
Bridge.AutoChan = make(chan bool) Bridge.AutoChan = make(chan bool)
BridgeConf.Mode = BridgeModeAuto BridgeConf.Mode = BridgeModeAuto
go AutoBridge(discord) //go AutoBridge(discord,l)
case "manual": case "manual":
log.Println("bridge starting in manual mode") log.Println("bridge starting in manual mode")
BridgeConf.Mode = BridgeModeManual BridgeConf.Mode = BridgeModeManual
case "constant": case "constant":
log.Println("bridge starting in constant mode") log.Println("bridge starting in constant mode")
BridgeConf.Mode = BridgeModeConstant BridgeConf.Mode = BridgeModeConstant
go startBridge(discord, *discordGID, *discordCID, config, BridgeConf.MumbleAddr, *mumbleInsecure, make(chan bool)) go startBridge(discord, *discordGID, *discordCID, l, make(chan bool))
default: default:
discord.Close() discord.Close()
log.Fatalln("invalid bridge mode set") log.Fatalln("invalid bridge mode set")