From bcd7ee97ba6ff6fbf60d9754d29ca00d039b4af8 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 5 Jan 2021 19:43:55 -0500 Subject: [PATCH] mumble event listeners use non-global state --- bridge.go | 18 +++++++++--------- handlers.go | 23 ++++++++++++++--------- main.go | 5 +++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/bridge.go b/bridge.go index 48a84b8..b8a7caf 100644 --- a/bridge.go +++ b/bridge.go @@ -26,7 +26,7 @@ type BridgeState struct { 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) if err != nil { log.Println(err) @@ -43,14 +43,14 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin } var tlsConfig tls.Config - if mumbleInsecure { + if l.BridgeConf.MumbleInsecure { tlsConfig.InsecureSkipVerify = true } - config.Attach(gumbleutil.Listener{ - Connect: mumbleConnect, - UserChange: mumbleUserChange, + l.BridgeConf.Config.Attach(gumbleutil.Listener{ + Connect: l.mumbleConnect, + 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 { log.Println(err) @@ -68,7 +68,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin // Start Passing Between // Mumble go m.fromMumbleMixer(toDiscord, die) - det := config.AudioListeners.Attach(m) + det := l.BridgeConf.Config.AudioListeners.Attach(m) //Discord go discordReceivePCM(dgv, die) @@ -173,7 +173,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") for { select { @@ -187,7 +187,7 @@ func AutoBridge(s *discordgo.Session) { log.Println("users detected in mumble and discord, bridging") die := make(chan bool) 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 { log.Println("no one online, killing bridge") diff --git a/handlers.go b/handlers.go index 256108f..b0a4c25 100644 --- a/handlers.go +++ b/handlers.go @@ -10,6 +10,11 @@ import ( "layeh.com/gumble/gumble" ) +type Listener struct { + BridgeConf *BridgeConfig + Bridge *BridgeState +} + func ready(s *discordgo.Session, event *discordgo.Ready) { 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) die := make(chan bool) 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 } } @@ -107,7 +112,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { DiscordReset() time.Sleep(5 * time.Second) 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 } } @@ -117,7 +122,7 @@ func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if BridgeConf.Mode != BridgeModeAuto { BridgeConf.Mode = BridgeModeAuto Bridge.AutoChan = make(chan bool) - go AutoBridge(s) + //go AutoBridge(s) } else { Bridge.AutoChan <- true BridgeConf.Mode = BridgeModeManual @@ -198,8 +203,8 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { return } -func mumbleConnect(e *gumble.ConnectEvent) { - if BridgeConf.MumbleChannel != "" { +func (l *Listener) mumbleConnect(e *gumble.ConnectEvent) { + if l.BridgeConf.MumbleChannel != "" { //join specified channel startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel) if startingChannel != nil { @@ -208,15 +213,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) { 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? //event listeners block while processing //also probably bad to rebuild the set every user change. - if user.Name != Bridge.Client.Self.Name { - Bridge.MumbleUsers[user.Name] = true + if user.Name != l.Bridge.Client.Self.Name { + l.Bridge.MumbleUsers[user.Name] = true } } } diff --git a/main.go b/main.go index 10dad71..b4d0fff 100644 --- a/main.go +++ b/main.go @@ -109,19 +109,20 @@ func main() { DiscordUserCount: 0, DiscordUsers: make(map[string]bool), } + l := &Listener{BridgeConf, Bridge} switch *mode { case "auto": log.Println("bridge starting in automatic mode") Bridge.AutoChan = make(chan bool) BridgeConf.Mode = BridgeModeAuto - go AutoBridge(discord) + //go AutoBridge(discord,l) case "manual": log.Println("bridge starting in manual mode") BridgeConf.Mode = BridgeModeManual case "constant": log.Println("bridge starting in constant mode") 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: discord.Close() log.Fatalln("invalid bridge mode set")