Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add topic handler #383

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/user/config-file-glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ Telegram settings
``MAX_MESSAGES_PER_MINUTE=20``
Maximum number of messages sent to Telegram from IRC per minute

``SHOW_TOPIC_MESSAGE=true``
Send Telegram message when the topic in the IRC channel is changed.

``SHOW_ACTION_MESSAGE=true``
Relay action messages (e.g. ``/me thinks TeleIRC is cool!``)

Expand Down
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ IRC_QUIT_MESSAGE="TeleIRC bridge stopped."
TELEGRAM_CHAT_ID=-0000000000000
TELEIRC_TOKEN=000000000:AAAAAAaAAa2AaAAaoAAAA-a_aaAAaAaaaAA
MAX_MESSAGES_PER_MINUTE=20
SHOW_TOPIC_MESSAGE=true
SHOW_ACTION_MESSAGE=true
SHOW_JOIN_MESSAGE=false
SHOW_KICK_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 @@ -50,6 +50,7 @@ type TelegramSettings struct {
ChatID int64 `env:"TELEGRAM_CHAT_ID,required"`
Prefix string `env:"TELEGRAM_MESSAGE_PREFIX" envDefault:"<"`
Suffix string `env:"TELEGRAM_MESSAGE_SUFFIX" envDefault:">"`
ShowTopicMessage bool `env:"SHOW_TOPIC_MESSAGE" envDefault:"false"`
ShowJoinMessage bool `env:"SHOW_JOIN_MESSAGE" envDefault:"false"`
ShowActionMessage bool `env:"SHOW_ACTION_MESSAGE" envDefault:"false"`
ShowLeaveMessage bool `env:"SHOW_LEAVE_MESSAGE" envDefault:"false"`
Expand Down
28 changes: 24 additions & 4 deletions internal/handlers/irc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
)

const (
joinFmt = "* %s joins"
partFmt = "* %s parts"
quitFmt = "* %s quit (%s)"
kickFmt = "* %s kicked %s from %s: %s"
joinFmt = "* %s joins"
partFmt = "* %s parts"
quitFmt = "* %s quit (%s)"
kickFmt = "* %s kicked %s from %s: %s"
topicChangeFmt = "* %s changed topic to: %s"
topicClearedFmt = "* %s removed topic"
)

/*
Expand Down Expand Up @@ -119,6 +121,23 @@ func partHandler(c ClientInterface) func(*girc.Client, girc.Event) {
}
}

func topicHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.Logger().LogDebug("topicHandler triggered")
if c.TgSettings().ShowTopicMessage {
// e.Source.Name is the user who changed the topic.
// e.Params[0] is the channel where the topic changed.
// e.Params[1] is the new topic. We should assume that
// this may or may not appear as its possible to clear a topic.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its -> it's, but I'll allow it :(

if len(e.Params) <= 1 {
c.SendToTg(fmt.Sprintf(topicClearedFmt, e.Source.Name))
} else {
c.SendToTg(fmt.Sprintf(topicChangeFmt, e.Source.Name, e.Params[1]))
}
}
}
}

func quitHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.Logger().LogDebug("quitHandler triggered")
Expand Down Expand Up @@ -158,6 +177,7 @@ func getHandlerMapping() map[string]Handler {
girc.KICK: kickHandler,
girc.PRIVMSG: messageHandler,
girc.PART: partHandler,
girc.TOPIC: topicHandler,
girc.QUIT: quitHandler,
}
}
114 changes: 114 additions & 0 deletions internal/handlers/irc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,120 @@ func TestKickHandlerNoReason(t *testing.T) {
})
}

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

defer ctrl.Finish()

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

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("topicHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME changed topic to: NEW TOPIC!"))

myHandler := topicHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{
"#testchannel",
"NEW TOPIC!",
},
})
}

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

defer ctrl.Finish()

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

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

myHandler := topicHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{
"#testchannel",
"NEW TOPIC!",
},
})
}

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

defer ctrl.Finish()

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

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("topicHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME removed topic"))

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

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

Expand Down