From a41accd033a8d7e859a4bff89ae64e1f3500e4d2 Mon Sep 17 00:00:00 2001 From: Qais Patankar Date: Wed, 24 Jun 2020 23:25:10 +0100 Subject: Add sane RemoteNickFormat default for API (#1157) --- bridge/api/api.go | 9 ++++++++- bridge/bridge.go | 18 +++++++++++++----- bridge/config/config.go | 12 ++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/bridge/api/api.go b/bridge/api/api.go index 9a3f1178..38d1a4bd 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -10,7 +10,7 @@ import ( "github.com/42wim/matterbridge/bridge/config" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "github.com/zfjagann/golang-ring" + ring "github.com/zfjagann/golang-ring" ) type API struct { @@ -41,6 +41,13 @@ func New(cfg *bridge.Config) bridge.Bridger { return key == b.GetString("Token"), nil })) } + + // Set RemoteNickFormat to a sane default + if !b.IsKeySet("RemoteNickFormat") { + b.Log.Debugln("RemoteNickFormat is unset, defaulting to \"{NICK}\"") + b.Config.Config.Viper().Set(b.GetConfigKey("RemoteNickFormat"), "{NICK}") + } + e.GET("/api/health", b.handleHealthcheck) e.GET("/api/messages", b.handleMessages) e.GET("/api/stream", b.handleStream) diff --git a/bridge/bridge.go b/bridge/bridge.go index eec2bfaf..ef71f97e 100644 --- a/bridge/bridge.go +++ b/bridge/bridge.go @@ -86,8 +86,16 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map return nil } +func (b *Bridge) GetConfigKey(key string) string { + return b.Account + "." + key +} + +func (b *Bridge) IsKeySet(key string) bool { + return b.Config.IsKeySet(b.GetConfigKey(key)) || b.Config.IsKeySet("general."+key) +} + func (b *Bridge) GetBool(key string) bool { - val, ok := b.Config.GetBool(b.Account + "." + key) + val, ok := b.Config.GetBool(b.GetConfigKey(key)) if !ok { val, _ = b.Config.GetBool("general." + key) } @@ -95,7 +103,7 @@ func (b *Bridge) GetBool(key string) bool { } func (b *Bridge) GetInt(key string) int { - val, ok := b.Config.GetInt(b.Account + "." + key) + val, ok := b.Config.GetInt(b.GetConfigKey(key)) if !ok { val, _ = b.Config.GetInt("general." + key) } @@ -103,7 +111,7 @@ func (b *Bridge) GetInt(key string) int { } func (b *Bridge) GetString(key string) string { - val, ok := b.Config.GetString(b.Account + "." + key) + val, ok := b.Config.GetString(b.GetConfigKey(key)) if !ok { val, _ = b.Config.GetString("general." + key) } @@ -111,7 +119,7 @@ func (b *Bridge) GetString(key string) string { } func (b *Bridge) GetStringSlice(key string) []string { - val, ok := b.Config.GetStringSlice(b.Account + "." + key) + val, ok := b.Config.GetStringSlice(b.GetConfigKey(key)) if !ok { val, _ = b.Config.GetStringSlice("general." + key) } @@ -119,7 +127,7 @@ func (b *Bridge) GetStringSlice(key string) []string { } func (b *Bridge) GetStringSlice2D(key string) [][]string { - val, ok := b.Config.GetStringSlice2D(b.Account + "." + key) + val, ok := b.Config.GetStringSlice2D(b.GetConfigKey(key)) if !ok { val, _ = b.Config.GetStringSlice2D("general." + key) } diff --git a/bridge/config/config.go b/bridge/config/config.go index 59d7d4be..d98c9423 100644 --- a/bridge/config/config.go +++ b/bridge/config/config.go @@ -219,6 +219,7 @@ type BridgeValues struct { type Config interface { Viper() *viper.Viper BridgeValues() *BridgeValues + IsKeySet(key string) bool GetBool(key string) (bool, bool) GetInt(key string) (int, bool) GetString(key string) (string, bool) @@ -303,6 +304,12 @@ func (c *config) Viper() *viper.Viper { return c.v } +func (c *config) IsKeySet(key string) bool { + c.RLock() + defer c.RUnlock() + return c.v.IsSet(key) +} + func (c *config) GetBool(key string) (bool, bool) { c.RLock() defer c.RUnlock() @@ -362,6 +369,11 @@ type TestConfig struct { Overrides map[string]interface{} } +func (c *TestConfig) IsKeySet(key string) bool { + _, ok := c.Overrides[key] + return ok || c.Config.IsKeySet(key) +} + func (c *TestConfig) GetBool(key string) (bool, bool) { val, ok := c.Overrides[key] if ok { -- cgit v1.2.3