Skip to content

Commit

Permalink
irc: Add support for multiple IP addresses (closes #324) (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwflory authored Oct 29, 2020
1 parent e57c192 commit 53b73d7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
5 changes: 5 additions & 0 deletions docs/user/config-file-glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ This glossary is intended for advanced users.
IRC settings
************

Host connection settings
========================

``IRC_HOST_IP=""``
Specify IP address to use for IRC connection.
Useful for hosts with multiple IP addresses.

Server connection settings
==============================
Expand Down
3 changes: 3 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# #
###############################################################################

#####----- Host connection settings -----#####
IRC_HOST_IP=""


#####----- IRC server connection settings -----#####
IRC_SERVER=chat.freenode.net
Expand Down
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaultPath = ".env"

// IRCSettings includes settings related to the IRC bot/message relaying
type IRCSettings struct {
BindAddress string `env:"IRC_HOST_IP" envDefault:""`
Server string `env:"IRC_SERVER,required"`
ServerPass string `env:"IRC_SERVER_PASSWORD" envDefault:""`
Port int `env:"IRC_PORT" envDefault:"6667" validate:"min=0,max=65535"`
Expand Down
5 changes: 5 additions & 0 deletions internal/handlers/irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func NewClient(settings *internal.IRCSettings, telegramSettings *internal.Telegr
SSL: settings.UseSSL,
})

// Bind an IP address for IRC connection
if settings.BindAddress != "" {
client.Config.Bind = settings.BindAddress
}

// IRC server authentication
if settings.ServerPass != "" {
client.Config.ServerPass = settings.ServerPass
Expand Down
44 changes: 23 additions & 21 deletions internal/handlers/irc/irc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestNewClientBasic(t *testing.T) {
ircSettings := &internal.IRCSettings{
Server: "irc.batcave.intl",
Port: 1337,
Port: 1337,
BotIdent: "alfred",
BotName: "Alfred Pennyworth",
BotNick: "alfred-p",
Expand All @@ -22,13 +22,13 @@ func TestNewClientBasic(t *testing.T) {
}
client := NewClient(ircSettings, nil, logger)

expectedPing, _:= time.ParseDuration("20s")
expectedPing, _ := time.ParseDuration("20s")
expectedConfig := girc.Config{
Server: "irc.batcave.intl",
Port: 1337,
Nick: "alfred-p",
Name: "Alfred Pennyworth",
User: "alfred",
Port: 1337,
Nick: "alfred-p",
Name: "Alfred Pennyworth",
User: "alfred",
PingDelay: expectedPing,
}
assert.Equal(t, client.Settings, ircSettings, "Client settings should be properly set")
Expand All @@ -37,13 +37,14 @@ func TestNewClientBasic(t *testing.T) {

func TestNewClientFull(t *testing.T) {
ircSettings := &internal.IRCSettings{
Server: "irc.batcave.intl",
ServerPass: "BatmanNeverDies!",
Port: 1337,
BotIdent: "alfred",
BotName: "Alfred Pennyworth",
BotNick: "alfred-p",
NickServUser: "irc_moderators",
BindAddress: "129.21.13.37",
Server: "irc.batcave.intl",
ServerPass: "BatmanNeverDies!",
Port: 1337,
BotIdent: "alfred",
BotName: "Alfred Pennyworth",
BotNick: "alfred-p",
NickServUser: "irc_moderators",
NickServPassword: "ProtectGotham",
}
logger := internal.Debug{
Expand All @@ -52,14 +53,15 @@ func TestNewClientFull(t *testing.T) {
client := NewClient(ircSettings, nil, logger)
expectedPing, _ := time.ParseDuration("20s")
expectedConfig := girc.Config{
Server: "irc.batcave.intl",
ServerPass: "BatmanNeverDies!",
Port: 1337,
Nick: "alfred-p",
Name: "Alfred Pennyworth",
User: "alfred",
PingDelay: expectedPing,
SASL: &girc.SASLPlain{
Bind: "129.21.13.37",
Server: "irc.batcave.intl",
ServerPass: "BatmanNeverDies!",
Port: 1337,
Nick: "alfred-p",
Name: "Alfred Pennyworth",
User: "alfred",
PingDelay: expectedPing,
SASL: &girc.SASLPlain{
User: "irc_moderators",
Pass: "ProtectGotham",
},
Expand Down

0 comments on commit 53b73d7

Please sign in to comment.