Skip to content

Commit

Permalink
Add handler so if someone changes their nickname on the IRC side, it …
Browse files Browse the repository at this point in the history
…gets forwarded to Telegram (#384)

* Added new setting for sending a message to telegram when a user changes their nick name.

* Added Nick handler and unit tests.

* formatting.
  • Loading branch information
xforever1313 committed Oct 31, 2021
1 parent 908c562 commit 4a7f31f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/user/config-file-glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ Telegram settings
``SHOW_KICK_MESSAGE=true``
Send Telegram message when someone is kicked from IRC channel.

``SHOW_NICK_MESSAGE=false``
Send Telegram message when someone changes their nickname in the IRC channel.

``SHOW_LEAVE_MESSAGE=false``
Send Telegram message when someone leaves IRC channel either by quitting or parting.

Expand Down
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ SHOW_ACTION_MESSAGE=true
SHOW_JOIN_MESSAGE=false
JOIN_MESSAGE_ALLOW_LIST=""
SHOW_KICK_MESSAGE=true
SHOW_NICK_MESSAGE=false
SHOW_LEAVE_MESSAGE=false
LEAVE_MESSAGE_ALLOW_LIST=""
SHOW_DISCONNECT_MESSAGE=true
Expand Down
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type TelegramSettings struct {
ShowLeaveMessage bool `env:"SHOW_LEAVE_MESSAGE" envDefault:"false"`
LeaveMessageAllowList []string `env:"LEAVE_MESSAGE_ALLOW_LIST" envDefault:"[]string{}"`
ShowKickMessage bool `env:"SHOW_KICK_MESSAGE" envDefault:"false"`
ShowNickMessage bool `env:"SHOW_NICK_MESSAGE" envDefault:"false"`
ShowDisconnectMessage bool `env:"SHOW_DISCONNECT_MESSAGE" envDefault:"false"`
MaxMessagePerMinute int `env:"MAX_MESSAGE_PER_MINUTE" envDefault:"20"`
}
Expand Down
21 changes: 21 additions & 0 deletions internal/handlers/irc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
kickFmt = "* %s kicked %s from %s: %s"
topicChangeFmt = "* %s changed topic to: %s"
topicClearedFmt = "* %s removed topic"
nickFmt = "* %s is now known as: %s"
)

/*
Expand Down Expand Up @@ -198,6 +199,25 @@ func kickHandler(c ClientInterface) func(*girc.Client, girc.Event) {
}
}

func nickHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.Logger().LogDebug("nickHandler triggered")
if c.TgSettings().ShowNickMessage {
// e.Source.Name is the original name.
// e.Params[0] is the new nick name.
// However, let's assume it is possible (though unlikely)
// e.Params can be empty.
var newName string
if len(e.Params) == 0 {
newName = "Unspecified Name"
} else {
newName = e.Params[0]
}
c.SendToTg(fmt.Sprintf(nickFmt, e.Source.Name, newName))
}
}
}

/*
getHandlerMapping returns a mapping of girc event types to handlers
*/
Expand All @@ -207,6 +227,7 @@ func getHandlerMapping() map[string]Handler {
girc.DISCONNECTED: disconnectHandler,
girc.JOIN: joinHandler,
girc.KICK: kickHandler,
girc.NICK: nickHandler,
girc.PRIVMSG: messageHandler,
girc.PART: partHandler,
girc.TOPIC: topicHandler,
Expand Down
110 changes: 110 additions & 0 deletions internal/handlers/irc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,116 @@ func TestTopicHandlerCleared(t *testing.T) {
})
}

func TestNickHandler_On(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowNickMessage: true,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("nickHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME is now known as: CHANGED_NAME"))

myHandler := nickHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{
"CHANGED_NAME",
},
})
}

func TestNickHandler_Off(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowNickMessage: false,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("nickHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Any()).
MaxTimes(0)

myHandler := nickHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{
"CHANGED_NAME",
},
})
}

func TestNickHandlerNoNick(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowNickMessage: true,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("nickHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME is now known as: Unspecified Name"))

myHandler := nickHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{},
})
}

func TestConnectHandlerKey(t *testing.T) {
ctrl := gomock.NewController(t)

Expand Down

0 comments on commit 4a7f31f

Please sign in to comment.