Drop channel path, only use channel IDs for joining.

This commit is contained in:
s3lph 2020-09-29 19:57:57 +02:00
parent 4825c1c2af
commit 93eb412394
4 changed files with 15 additions and 65 deletions

View File

@ -54,7 +54,7 @@ func (b *Bmumble) handleConnect(event *gumble.ConnectEvent) {
event.Client.Self.SetSelfMuted(true)
// if the Channel variable is set, this is a reconnect -> rejoin channel
if b.Channel != nil {
if err := b.doJoin(event.Client, b.Channel); err != nil {
if err := b.doJoin(event.Client, *b.Channel); err != nil {
b.Log.Error(err)
}
b.Remote <- config.Message{
@ -74,7 +74,7 @@ func (b *Bmumble) handleUserChange(event *gumble.UserChangeEvent) {
}
// Someone attempted to move the user out of the configured channel; attempt to join back
if b.Channel != nil {
if err := b.doJoin(event.Client, b.Channel); err != nil {
if err := b.doJoin(event.Client, *b.Channel); err != nil {
b.Log.Error(err)
}
}

View File

@ -1,18 +1,12 @@
package bmumble
import (
"errors"
"fmt"
"mime"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleutil"
"github.com/42wim/matterbridge/bridge/config"
"github.com/mattn/godown"
"github.com/vincent-petithory/dataurl"
@ -147,47 +141,3 @@ func (b *Bmumble) extractFiles(msg *config.Message) []config.Message {
msg.Extra["file"] = nil
return messages
}
func (b *Bmumble) parseChannelPath(client *gumble.Client, name string) ([]string, error) {
// Parse the channel name as a numeric ID if passed as "ID:42"
if strings.HasPrefix(name, "ID:") {
// Parse the ID string into an int
channelID, err := strconv.ParseUint(name[3:], 10, 32)
if err != nil {
b.Log.WithError(err).Fatalf("Cannot parse channel ID: %s", name)
return nil, err
}
// Verify that the ID refers to an existing channel
c, ok := client.Channels[uint32(channelID)]
if !ok {
b.Log.Fatalf("No channel with ID %d", channelID)
return nil, errors.New("no such channel: " + name)
}
return gumbleutil.ChannelPath(c)[1:], nil
}
if !strings.HasPrefix(name, "/") {
return nil, errors.New("channel path must start with a '/': " + name)
}
// Special treatment for the root channel: empty slice
if name == "/" {
return make([]string, 0), nil
}
// Iterate all path components, discarding the first token, which is the empty string before the leading /
tokens := strings.Split(name, "/")
var channelPath []string
for _, token := range tokens[1:] {
// Urldecode each token and append it to the path
if channelName, err := url.PathUnescape(token); err == nil && len(channelName) > 0 {
channelPath = append(channelPath, channelName)
} else {
b.Log.WithError(err).Fatalf("Error while decoding path component '%s'", token)
return nil, err
}
}
c := client.Channels.Find(channelPath...)
if c == nil {
return nil, errors.New("no such channel: " + name)
}
return gumbleutil.ChannelPath(c)[1:], nil
}

View File

@ -27,7 +27,7 @@ type Bmumble struct {
client *gumble.Client
Nick string
Host string
Channel []string
Channel *uint32
local chan config.Message
running chan error
connected chan gumble.DisconnectEvent
@ -76,15 +76,17 @@ func (b *Bmumble) Disconnect() error {
}
func (b *Bmumble) JoinChannel(channel config.ChannelInfo) error {
channelPath, err := b.parseChannelPath(b.client, channel.Name)
cid, err := strconv.ParseUint(channel.Name, 10, 32)
if err != nil {
return err
}
if b.Channel != nil {
b.Log.Fatalf("Cannot join channel '%v', already joined to channel '%v'", channel.Name, b.Channel)
channelID := uint32(cid)
if b.Channel != nil && *b.Channel != channelID {
b.Log.Fatalf("Cannot join channel ID '%d', already joined to channel ID %d", channelID, *b.Channel)
return errors.New("the Mumble bridge can only join a single channel")
}
return b.doJoin(b.client, channelPath)
b.Channel = &channelID
return b.doJoin(b.client, channelID)
}
func (b *Bmumble) Send(msg config.Message) (string, error) {
@ -190,13 +192,12 @@ func (b *Bmumble) doConnect() error {
return nil
}
func (b *Bmumble) doJoin(client *gumble.Client, channelPath []string) error {
c := client.Channels.Find(channelPath...)
if c == nil {
return fmt.Errorf("no such channel: %v", channelPath)
func (b *Bmumble) doJoin(client *gumble.Client, channelID uint32) error {
channel, ok := client.Channels[channelID]
if !ok {
return fmt.Errorf("no channel with ID %d", channelID)
}
b.Channel = gumbleutil.ChannelPath(c)[1:]
client.Self.Move(c)
client.Self.Move(channel)
return nil
}

View File

@ -1791,8 +1791,7 @@ enable=true
# -------------------------------------------------------------------------------------------------------------------------------------
# msteams | threadId | 19:82abcxx@thread.skype | You'll find the threadId in the URL
# -------------------------------------------------------------------------------------------------------------------------------------
# mumble | channel id | ID:42 | The channel ID, as shown in the channel's "Edit" window
# | channel path | /A%20channel/a%20subchannel | The urlencoded channel path, as contained in the channel's URL
# mumble | channel id | 42 | The channel ID, as shown in the channel's "Edit" window
# -------------------------------------------------------------------------------------------------------------------------------------
# rocketchat | channel | #channel | # is required for private channels too
# -------------------------------------------------------------------------------------------------------------------------------------