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 017a4eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
9 changes: 6 additions & 3 deletions bridge/irc_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ func (i *ircConnection) OnPrivateMessage(e *irc.Event) {
}

d := i.manager.bridge.discord
msg := i.manager.formatDiscordMessage("PM", e, e.Message(), "")

// if we have an empty message
if msg == "" {
return // do nothing, Discord doesn't like those
}

i.introducePM(e.Nick)

msg := fmt.Sprintf(
"%s,%s - %s@%s: %s", e.Connection.Server, e.Source,
e.Nick, i.manager.bridge.Config.Discriminator, e.Message())
_, err := d.ChannelMessageSend(i.pmDiscordChannel, msg)
if err != nil {
log.Warnln("Could not send PM", i.discord, err)
Expand Down
11 changes: 3 additions & 8 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func (m *IRCManager) formatIRCMessage(message *DiscordMessage, content string) s
msg = strings.ReplaceAll(msg, "${USER}", message.Author.Username[:1]+"\u200B"+message.Author.Username[1:length])
msg = strings.ReplaceAll(msg, "${DISCRIMINATOR}", message.Author.Discriminator)
msg = strings.ReplaceAll(msg, "${CONTENT}", content)

// we don't do trimming and later checks here, IRC doesn't mind blank messages or at least doesn't complain
// as loudly as Discord
return msg
Expand All @@ -361,15 +362,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 Expand Up @@ -413,6 +406,8 @@ func (m *IRCManager) formatDiscordMessage(msgFormat string, e *irc.Event, conten
msg = strings.ReplaceAll(msg, "${HOST}", e.Host)
msg = strings.ReplaceAll(msg, "${CONTENT}", content)
msg = strings.ReplaceAll(msg, "${TARGET}", target)
msg = strings.ReplaceAll(msg, "${SERVER}", e.Connection.Server)
msg = strings.ReplaceAll(msg, "${DISCRIMINATOR}", m.bridge.Config.Discriminator)
}

return strings.Trim(msg, " ")
Expand Down
32 changes: 28 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,9 +281,11 @@ 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{
"pm": "${SERVER},${NICK}!${IDENT}@${HOST} - ${NICK}@${DISCRIMINATOR}: ${CONTENT}",
"join": "_${NICK} joined (${IDENT}@${HOST})_",
"part": "_${NICK} left (${IDENT}@${HOST}) - ${CONTENT}_",
"quit": "_${NICK} quit (${IDENT}@${HOST}) - Quit: ${CONTENT}_",
Expand All @@ -281,7 +298,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 017a4eb

Please sign in to comment.