Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions docs/user/config-file-glossary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ Telegram settings
``SHOW_DISCONNECT_MESSAGE=true``
Sends a message to Telegram when the bot disconnects from the IRC side.

``WHITELIST_USERNAMES=""``
(Optional) Comma-separated list of Telegram usernames whose messages will be sent to IRC channel.
(This always compares the actual @username - not «First name» or alike)


**************
Imgur settings
**************
Expand Down
2 changes: 2 additions & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ SHOW_NICK_MESSAGE=false
SHOW_LEAVE_MESSAGE=false
LEAVE_MESSAGE_ALLOW_LIST=""
SHOW_DISCONNECT_MESSAGE=true
WHITELIST_USERNAMES=""



################################################################################
Expand Down
1 change: 1 addition & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type TelegramSettings struct {
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"`
UsernameWhitelist []string `env:"WHITELIST_USERNAMES" envSeparator:","`
}

// ImgurSettings includes settings related to Imgur uploading for Telegram photos
Expand Down
8 changes: 7 additions & 1 deletion internal/handlers/telegram/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ type Handler = func(tg *Client, u tgbotapi.Update)

/*
updateHandler takes in a Telegram Update channel, and determines
which handler to fire off
which handler to fire off (optionally only if user is in whitelist)
*/
func updateHandler(tg *Client, updates tgbotapi.UpdatesChannel) {
for u := range updates {
if u.Message != nil && u.Message.From != nil && !checkAllowedUsernames(tg.Settings.UsernameWhitelist, u.Message.From.UserName) {
tg.logger.LogDebug("Telegram Username is not in whitelist: " + u.Message.From.UserName)
tg.logger.LogDebug("Whitelisted Telegram Users: " + strings.Join(tg.Settings.UsernameWhitelist, ", "))
continue
}

switch {
case u.Message == nil:
tg.logger.LogError("Missing message data")
Expand Down
24 changes: 24 additions & 0 deletions internal/handlers/telegram/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package telegram

import (
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)

Expand Down Expand Up @@ -67,3 +69,25 @@ func uploadImage(tg *Client, u tgbotapi.Update) string {

return getImgurLink(tg, tgLink)
}

/*
checkAllowedUsernames checks the Telegram whitelist for a username, and returns whether
or not the name is in the allow list
*/
func checkAllowedUsernames(whitelist []string, username string) bool {
// Empty whitelist means no filtering - all users allowed
if len(whitelist) == 0 {
return true
}
// Empty username with non-empty whitelist should be rejected
if len(username) == 0 {
return false
}
// Filter username against provided whitelist
for _, name := range whitelist {
if strings.EqualFold(username, name) {
return true
}
}
return false
}