From 93eb412394d893f7264c94e1506fec3b29e660eb Mon Sep 17 00:00:00 2001 From: s3lph <5564491+s3lph@users.noreply.github.com> Date: Tue, 29 Sep 2020 19:57:57 +0200 Subject: [PATCH] Drop channel path, only use channel IDs for joining. --- bridge/mumble/handlers.go | 4 ++-- bridge/mumble/helpers.go | 50 --------------------------------------- bridge/mumble/mumble.go | 23 +++++++++--------- matterbridge.toml.sample | 3 +-- 4 files changed, 15 insertions(+), 65 deletions(-) diff --git a/bridge/mumble/handlers.go b/bridge/mumble/handlers.go index 8d7b8cc2..4a452929 100644 --- a/bridge/mumble/handlers.go +++ b/bridge/mumble/handlers.go @@ -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) } } diff --git a/bridge/mumble/helpers.go b/bridge/mumble/helpers.go index 11008df9..c828df2c 100644 --- a/bridge/mumble/helpers.go +++ b/bridge/mumble/helpers.go @@ -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 -} diff --git a/bridge/mumble/mumble.go b/bridge/mumble/mumble.go index bf59135b..2281d1c2 100644 --- a/bridge/mumble/mumble.go +++ b/bridge/mumble/mumble.go @@ -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 } diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index a979ef97..c9263e7b 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -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 # -------------------------------------------------------------------------------------------------------------------------------------