diff --git a/README.md b/README.md index be474b1..f0f98a9 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ http_port: 8000 # Note: SSL is enabled by default, use "irc_use_ssl: no" to disable. irc_host: irc.example.com irc_port: 7000 +# Optionally set the server password +irc_host_password: myserver_password # Use this IRC nickname. irc_nickname: myalertbot diff --git a/config.go b/config.go index 827abe6..722c896 100644 --- a/config.go +++ b/config.go @@ -37,6 +37,7 @@ type Config struct { IRCRealName string `yaml:"irc_realname"` IRCHost string `yaml:"irc_host"` IRCPort int `yaml:"irc_port"` + IRCHostPass string `yaml:"irc_host_password"` IRCUseSSL bool `yaml:"irc_use_ssl"` IRCVerifySSL bool `yaml:"irc_verify_ssl"` IRCChannels []IRCChannel `yaml:"irc_channels"` @@ -54,6 +55,7 @@ func LoadConfig(configFile string) (*Config, error) { IRCRealName: "Alertmanager IRC Relay", IRCHost: "example.com", IRCPort: 7000, + IRCHostPass: "", IRCUseSSL: true, IRCVerifySSL: true, IRCChannels: []IRCChannel{}, diff --git a/config_test.go b/config_test.go index 4e0b438..db98312 100644 --- a/config_test.go +++ b/config_test.go @@ -40,6 +40,7 @@ func TestLoadGoodConfig(t *testing.T) { IRCNick: "foo", IRCHost: "irc.example.com", IRCPort: 1234, + IRCHostPass: "hostsecret", IRCUseSSL: true, IRCChannels: []IRCChannel{IRCChannel{Name: "#foobar"}}, MsgTemplate: defaultMsgTemplate, diff --git a/irc.go b/irc.go index d4c0b62..24b742b 100644 --- a/irc.go +++ b/irc.go @@ -95,6 +95,7 @@ func NewIRCNotifier(config *Config, alertMsgs chan AlertMsg) (*IRCNotifier, erro ircConfig.Me.Name = config.IRCRealName ircConfig.Server = strings.Join( []string{config.IRCHost, strconv.Itoa(config.IRCPort)}, ":") + ircConfig.Pass = config.IRCHostPass ircConfig.SSL = config.IRCUseSSL ircConfig.SSLConfig = &tls.Config{ ServerName: config.IRCHost, diff --git a/irc_test.go b/irc_test.go index 8d39761..30fb085 100644 --- a/irc_test.go +++ b/irc_test.go @@ -267,6 +267,46 @@ func TestPreJoinChannels(t *testing.T) { } } +func TestServerPassword(t *testing.T) { + server, port := makeTestServer(t) + config := makeTestIRCConfig(port) + config.IRCHostPass = "hostsecret" + notifier, _ := makeTestNotifier(t, config) + + var testStep sync.WaitGroup + + joinHandler := func(conn *bufio.ReadWriter, line *irc.Line) error { + // #baz is configured as the last channel to pre-join + if line.Args[0] == "#baz" { + testStep.Done() + } + return nil + } + server.SetHandler("JOIN", joinHandler) + + testStep.Add(1) + go notifier.Run() + + testStep.Wait() + + notifier.StopRunning <- true + server.Stop() + + expectedCommands := []string{ + "PASS hostsecret", + "NICK foo", + "USER foo 12 * :", + "JOIN #foo", + "JOIN #bar", + "JOIN #baz", + "QUIT :see ya", + } + + if !reflect.DeepEqual(expectedCommands, server.Log) { + t.Error("Did not send IRC server password") + } +} + func TestSendAlertOnPreJoinedChannel(t *testing.T) { server, port := makeTestServer(t) config := makeTestIRCConfig(port)