2021-04-24 14:36:34 -04:00
|
|
|
package bridge
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
import (
|
2021-02-01 16:03:38 -05:00
|
|
|
"log"
|
2021-01-19 01:06:08 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"layeh.com/gumble/gumble"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MumbleListener Handle mumble events
|
|
|
|
type MumbleListener struct {
|
|
|
|
Bridge *BridgeState
|
|
|
|
}
|
|
|
|
|
2021-04-24 14:36:34 -04:00
|
|
|
func (l *MumbleListener) MumbleConnect(e *gumble.ConnectEvent) {
|
2021-02-08 13:16:09 -05:00
|
|
|
//join specified channel
|
|
|
|
startingChannel := e.Client.Channels.Find(l.Bridge.BridgeConfig.MumbleChannel...)
|
|
|
|
if startingChannel != nil {
|
|
|
|
e.Client.Self.Move(startingChannel)
|
2021-01-19 01:06:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 14:36:34 -04:00
|
|
|
func (l *MumbleListener) MumbleUserChange(e *gumble.UserChangeEvent) {
|
2021-01-19 01:06:08 -05:00
|
|
|
l.Bridge.MumbleUsersMutex.Lock()
|
|
|
|
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) || e.Type.Has(gumble.UserChangeDisconnected) {
|
|
|
|
l.Bridge.MumbleUsers = make(map[string]bool)
|
|
|
|
for _, user := range l.Bridge.MumbleClient.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 != l.Bridge.MumbleClient.Self.Name {
|
|
|
|
l.Bridge.MumbleUsers[user.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Bridge.MumbleUsersMutex.Unlock()
|
|
|
|
|
|
|
|
if e.Type.Has(gumble.UserChangeConnected) {
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("User connected to mumble " + e.User.Name)
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
if !l.Bridge.BridgeConfig.MumbleDisableText {
|
2021-04-24 14:36:34 -04:00
|
|
|
e.User.Send("Mumble-Discord-Bridge v" + l.Bridge.BridgeConfig.Version)
|
2021-01-19 01:06:08 -05:00
|
|
|
|
|
|
|
// Tell the user who is connected to discord
|
2021-04-07 01:24:17 -04:00
|
|
|
l.Bridge.DiscordUsersMutex.Lock()
|
2021-01-19 01:06:08 -05:00
|
|
|
if len(l.Bridge.DiscordUsers) == 0 {
|
|
|
|
e.User.Send("No users connected to Discord")
|
|
|
|
} else {
|
|
|
|
s := "Connected to Discord: "
|
|
|
|
|
|
|
|
arr := []string{}
|
|
|
|
for u := range l.Bridge.DiscordUsers {
|
|
|
|
arr = append(arr, l.Bridge.DiscordUsers[u].username)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s + strings.Join(arr[:], ",")
|
|
|
|
|
|
|
|
e.User.Send(s)
|
|
|
|
}
|
2021-04-07 01:24:17 -04:00
|
|
|
l.Bridge.DiscordUsersMutex.Unlock()
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send discord a notice
|
|
|
|
l.Bridge.discordSendMessageAll(e.User.Name + " has joined mumble")
|
|
|
|
}
|
2021-02-01 16:03:38 -05:00
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
if e.Type.Has(gumble.UserChangeDisconnected) {
|
|
|
|
l.Bridge.discordSendMessageAll(e.User.Name + " has left mumble")
|
2021-02-01 16:03:38 -05:00
|
|
|
log.Println("User disconnected from mumble " + e.User.Name)
|
2021-01-19 01:06:08 -05:00
|
|
|
}
|
|
|
|
}
|