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
}
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)
@ -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")
for {
select {
@ -188,7 +188,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")

View File

@ -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
@ -197,8 +202,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 {
@ -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) {
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
}
}
}

View File

@ -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")