From c469a65ed6a8bc1522579eab39fde797d51c1015 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 5 Jan 2021 13:16:03 -0500 Subject: [PATCH 1/3] begin switching to mumble event listeners --- bridge.go | 13 +++++-------- handlers.go | 11 +++++++++++ main.go | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bridge.go b/bridge.go index cd57238..d015741 100644 --- a/bridge.go +++ b/bridge.go @@ -12,6 +12,7 @@ import ( "github.com/bwmarrin/discordgo" "layeh.com/gumble/gumble" + "layeh.com/gumble/gumbleutil" ) type BridgeState struct { @@ -44,7 +45,9 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin if mumbleInsecure { tlsConfig.InsecureSkipVerify = true } - + config.Attach(gumbleutil.Listener{ + Connect: mumbleConnect, + }) mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig) if err != nil { @@ -53,13 +56,6 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin } defer mumble.Disconnect() Bridge.Client = mumble - if BridgeConf.MumbleChannel != "" { - //join specified channel - startingChannel := mumble.Channels.Find(BridgeConf.MumbleChannel) - if startingChannel != nil { - mumble.Self.Move(startingChannel) - } - } // Shared Channels // Shared channels pass PCM information in 10ms chunks [480]int16 var toMumble = mumble.AudioOutgoing() @@ -71,6 +67,7 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin // Mumble go m.fromMumbleMixer(toDiscord, die) det := config.AudioListeners.Attach(m) + //Discord go discordReceivePCM(dgv, die) go fromDiscordMixer(toMumble, die) diff --git a/handlers.go b/handlers.go index c8451a3..6e33cb7 100644 --- a/handlers.go +++ b/handlers.go @@ -7,6 +7,7 @@ import ( "time" "github.com/bwmarrin/discordgo" + "layeh.com/gumble/gumble" ) func ready(s *discordgo.Session, event *discordgo.Ready) { @@ -192,3 +193,13 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { } return } + +func mumbleConnect(e *gumble.ConnectEvent) { + if BridgeConf.MumbleChannel != "" { + //join specified channel + startingChannel := e.Client.Channels.Find(BridgeConf.MumbleChannel) + if startingChannel != nil { + e.Client.Self.Move(startingChannel) + } + } +} diff --git a/main.go b/main.go index 03b32f9..10dad71 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,7 @@ func main() { config.Password = *mumblePassword config.AudioInterval = time.Millisecond * 10 + // Bridge setup BridgeConf = &BridgeConfig{ Config: config, MumbleAddr: *mumbleAddr + ":" + strconv.Itoa(*mumblePort), From 1e8a0a4165c8c396aac069580e3d0c9c857226ea Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 5 Jan 2021 19:00:28 -0500 Subject: [PATCH 2/3] count how many users are actually in channel --- bridge.go | 10 ++++++++-- handlers.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/bridge.go b/bridge.go index d015741..1e6bffc 100644 --- a/bridge.go +++ b/bridge.go @@ -20,6 +20,7 @@ type BridgeState struct { Connected bool Client *gumble.Client DiscordUsers map[string]bool + MumbleUsers map[string]bool MumbleUserCount int DiscordUserCount int AutoChan chan bool @@ -46,7 +47,8 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin tlsConfig.InsecureSkipVerify = true } config.Attach(gumbleutil.Listener{ - Connect: mumbleConnect, + Connect: mumbleConnect, + UserChange: mumbleUserChange, }) mumble, err := gumble.DialWithDialer(new(net.Dialer), mumbleAddr, config, &tlsConfig) @@ -158,7 +160,11 @@ func discordStatusUpdate(dg *discordgo.Session, host, port string) { if curr == 0 { status = "" } else { - status = fmt.Sprintf("%v users in Mumble\n", curr) + if len(Bridge.MumbleUsers) > 0 { + status = fmt.Sprintf("%v/%v users in Mumble\n", len(Bridge.MumbleUsers), curr) + } else { + status = fmt.Sprintf("%v users in Mumble\n", curr) + } } dg.UpdateListeningStatus(status) } diff --git a/handlers.go b/handlers.go index 6e33cb7..cc7a652 100644 --- a/handlers.go +++ b/handlers.go @@ -203,3 +203,17 @@ func mumbleConnect(e *gumble.ConnectEvent) { } } } + +func 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 { + //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 + } + } + } +} From 4ae61769917e99510b6be8aa42c85596db1f2fb0 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 5 Jan 2021 19:08:17 -0500 Subject: [PATCH 3/3] use gumble.Client.Do for sending messages thread safely --- bridge.go | 4 +++- handlers.go | 8 ++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/bridge.go b/bridge.go index 1e6bffc..48a84b8 100644 --- a/bridge.go +++ b/bridge.go @@ -114,7 +114,9 @@ func startBridge(discord *discordgo.Session, discordGID string, discordCID strin if err != nil { log.Println("error looking up username") Bridge.DiscordUsers[u.Username] = true - Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) + Bridge.Client.Do(func() { + Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) + }) } } } diff --git a/handlers.go b/handlers.go index cc7a652..256108f 100644 --- a/handlers.go +++ b/handlers.go @@ -154,7 +154,9 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { } log.Println("user joined watched discord channel") if Bridge.Connected { - Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) + Bridge.Client.Do(func() { + Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has joined Discord channel\n", u.Username), false) + }) } Bridge.DiscordUsers[u.Username] = true log.Println(Bridge.DiscordUsers) @@ -184,7 +186,9 @@ func voiceUpdate(s *discordgo.Session, event *discordgo.VoiceStateUpdate) { delete(Bridge.DiscordUsers, u.Username) log.Println("user left watched discord channel") if Bridge.Connected { - Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false) + Bridge.Client.Do(func() { + Bridge.Client.Self.Channel.Send(fmt.Sprintf("%v has left Discord channel\n", u.Username), false) + }) } Bridge.DiscordUserCount = count }