Skip to content

Commit

Permalink
Provide more helpful errors for invalid keys.
Browse files Browse the repository at this point in the history
When it is first load, if an invalid key is present in `discord_format`
we inform the user of a fatal error and exit.
If it's on reload we don't apply the new format and inform the user such
was not done and that there is an error with an invalid key being
present in the `discord_format` setting.
  • Loading branch information
llmII committed Feb 19, 2021
1 parent 55f639a commit 5b6ddfe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
8 changes: 0 additions & 8 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,7 @@ func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {

// Person is appearing offline (or the bridge is running in Simple Mode)
if !ok {
// length := len(msg.Author.Username)
for _, line := range strings.Split(content, "\n") {
// m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
// "<%s#%s> %s",
// msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
// msg.Author.Discriminator,
// line,
// ))

m.bridge.ircListener.Privmsg(channel, m.formatIRCMessage(msg, line))
}
return
Expand Down
31 changes: 27 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -92,8 +93,14 @@ func main() {
//
viper.SetDefault("irc_puppet_prejoin_commands", []string{"MODE ${NICK} +D"})
ircPuppetPrejoinCommands := viper.GetStringSlice("irc_puppet_prejoin_commands") // Commands for each connection to send before joining channels
discordFormat := viper.GetStringMapString("discord_format")
discordFormat = setupDiscordFormat(discordFormat)
rawDiscordFormat := viper.GetStringMapString("discord_format")
var discordFormat map[string]string
if df, err := setupDiscordFormat(rawDiscordFormat); err == nil {
discordFormat = df
} else {
log.WithError(err).Fatal("discord_format setting is invalid")
return
}
//
viper.SetDefault("avatar_url", "https://ui-avatars.com/api/?name=${USERNAME}")
avatarURL := viper.GetString("avatar_url")
Expand Down Expand Up @@ -226,6 +233,14 @@ func main() {
}
dib.Config.DiscordIgnores = discordIgnores

rawDiscordFormat := viper.GetStringMapString("discord_format")
if discordFormat, err := setupDiscordFormat(rawDiscordFormat); err == nil {
dib.Config.DiscordFormat = discordFormat
} else {
log.WithError(err).Error("discord_format setting is invalid, this setting has not been updated")
}
dib.Config.IRCFormat = viper.GetString("irc_format")

chans := viper.GetStringMapString("channel_mappings")
equalChans := reflect.DeepEqual(chans, channelMappings)
if !equalChans {
Expand Down Expand Up @@ -266,7 +281,8 @@ func setupHostmaskMatchers(hostmasks []string) []glob.Glob {
return matchers
}

func setupDiscordFormat(discordFormat map[string]string) map[string]string {
func setupDiscordFormat(discordFormat map[string]string) (map[string]string, error) {
var err error
// lowercase to match that YAML lowercases it
discordFormatDefaults := map[string]string{
"join": "_${NICK} joined (${IDENT}@${HOST})_",
Expand All @@ -281,7 +297,14 @@ func setupDiscordFormat(discordFormat map[string]string) map[string]string {
}
}

return discordFormat
for ev := range discordFormat {
if _, ok := discordFormatDefaults[ev]; !ok {
err = fmt.Errorf("Unknown format key %s", ev)
break
}
}

return discordFormat, err
}

func SetLogDebug(debug bool) {
Expand Down

0 comments on commit 5b6ddfe

Please sign in to comment.