From 0b496304f9a70b97ec2865723a3b9ed2887c650c Mon Sep 17 00:00:00 2001 From: oliveratgithub Date: Wed, 12 Nov 2025 23:34:24 +0100 Subject: [PATCH 1/2] telegram: Adds option to put IRC nickname in quote Introduces a new optional `QUOTE_NICKNAME=true` setting, that adjusts how IRC messages - particularly the nickname part - are rendered when bridged to Telegram: instead of a whole unformatted string, it uses the supported `
`-formatting for the Telegram messages. This results in the sending IRC nickname to be better visible, but also clearly distinguishable from the IRC user's effective Text Message. - Telegram message default behaviour / before: ` Hello from IRC to Telegram` - New behaviour and format, if this mode is enabled, will rather look as follows: ``` > Hello from IRC to Telegram ``` For this to work: - the message's parse_mode has to be set to `HTML` - and this requires proper HTML-escaping / HTML-tag removal on the IRC nickname & message parts, before calling the Telegram Bot API sendMessage command. - The StripHTML() func invoked for that, was taken from here: https://go.dev/play/p/fqHzlCJa9ta (L31-35) --- docs/user/config-file-glossary.rst | 3 +++ env.example | 1 + internal/config.go | 1 + internal/handlers/irc/handlers.go | 9 ++++++++- internal/handlers/telegram/telegram.go | 3 +++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/user/config-file-glossary.rst b/docs/user/config-file-glossary.rst index 21beffc..00f8bf0 100644 --- a/docs/user/config-file-glossary.rst +++ b/docs/user/config-file-glossary.rst @@ -171,6 +171,9 @@ Telegram settings ``SHOW_DISCONNECT_MESSAGE=true`` Sends a message to Telegram when the bot disconnects from the IRC side. +``QUOTE_NICKNAME=false`` + Place IRC nickname in a blockquote section of the message to Telegram, instead of inline message prefix. + ************** Imgur settings ************** diff --git a/env.example b/env.example index c9282e7..554e81e 100644 --- a/env.example +++ b/env.example @@ -75,6 +75,7 @@ SHOW_NICK_MESSAGE=false SHOW_LEAVE_MESSAGE=false LEAVE_MESSAGE_ALLOW_LIST="" SHOW_DISCONNECT_MESSAGE=true +QUOTE_NICKNAME=false ################################################################################ diff --git a/internal/config.go b/internal/config.go index 4d22cc9..ddc78c6 100644 --- a/internal/config.go +++ b/internal/config.go @@ -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"` + QuoteNick bool `env:"QUOTE_NICKNAME" envDefault:"false"` } // ImgurSettings includes settings related to Imgur uploading for Telegram photos diff --git a/internal/handlers/irc/handlers.go b/internal/handlers/irc/handlers.go index be77130..6388a9e 100644 --- a/internal/handlers/irc/handlers.go +++ b/internal/handlers/irc/handlers.go @@ -4,6 +4,7 @@ import ( "fmt" "regexp" "strings" + "html" "github.com/lrstanley/girc" ) @@ -129,7 +130,13 @@ func messageHandler(c ClientInterface) func(*girc.Client, girc.Event) { // Strips out ACTION word from text formatted = "* " + e.Source.Name + msg[7:len(msg)-1] } else { - formatted = c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix + " " + e.Params[1] + if (c.TgSettings().QuoteNick) { + ircNicknameFormatted := c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix + ircMessageNoHtml := regexp.MustCompile(`<.*?>`).ReplaceAllString(e.Params[1], "") + formatted = "
" + html.EscapeString(ircNicknameFormatted) + "
\n" + strings.NewReplacer(">", "", "<", "").Replace(ircMessageNoHtml) + } else { + formatted = c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix + " " + e.Params[1] + } } if hasNoForwardPrefix(c, e.Params[1]) { diff --git a/internal/handlers/telegram/telegram.go b/internal/handlers/telegram/telegram.go index 1479f86..26178c3 100644 --- a/internal/handlers/telegram/telegram.go +++ b/internal/handlers/telegram/telegram.go @@ -33,6 +33,9 @@ SendMessage sends a message to the Telegram channel specified in the settings func (tg *Client) SendMessage(msg string) { newMsg := tgbotapi.NewMessage(tg.Settings.ChatID, "") newMsg.Text = msg + if (tg.Settings.QuoteNick) { + newMsg.ParseMode = "HTML" + } if _, err := tg.api.Send(newMsg); err != nil { var attempts int = 0 From c3429c3203d2b3c4443d0e1e3a7c169968191df7 Mon Sep 17 00:00:00 2001 From: oliveratgithub Date: Thu, 13 Nov 2025 19:54:46 +0100 Subject: [PATCH 2/2] irc(messageHandler): Adds also html.EscapeString() + A few Go syntax validation improvements --- internal/handlers/irc/handlers.go | 6 +++--- internal/handlers/telegram/telegram.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/handlers/irc/handlers.go b/internal/handlers/irc/handlers.go index 6388a9e..5289d25 100644 --- a/internal/handlers/irc/handlers.go +++ b/internal/handlers/irc/handlers.go @@ -2,9 +2,9 @@ package irc import ( "fmt" + "html" "regexp" "strings" - "html" "github.com/lrstanley/girc" ) @@ -130,10 +130,10 @@ func messageHandler(c ClientInterface) func(*girc.Client, girc.Event) { // Strips out ACTION word from text formatted = "* " + e.Source.Name + msg[7:len(msg)-1] } else { - if (c.TgSettings().QuoteNick) { + if c.TgSettings().QuoteNick { ircNicknameFormatted := c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix ircMessageNoHtml := regexp.MustCompile(`<.*?>`).ReplaceAllString(e.Params[1], "") - formatted = "
" + html.EscapeString(ircNicknameFormatted) + "
\n" + strings.NewReplacer(">", "", "<", "").Replace(ircMessageNoHtml) + formatted = "
" + html.EscapeString(ircNicknameFormatted) + "
\n" + html.EscapeString(strings.NewReplacer(">", "", "<", "").Replace(ircMessageNoHtml)) } else { formatted = c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix + " " + e.Params[1] } diff --git a/internal/handlers/telegram/telegram.go b/internal/handlers/telegram/telegram.go index 26178c3..fa4b5b1 100644 --- a/internal/handlers/telegram/telegram.go +++ b/internal/handlers/telegram/telegram.go @@ -33,7 +33,7 @@ SendMessage sends a message to the Telegram channel specified in the settings func (tg *Client) SendMessage(msg string) { newMsg := tgbotapi.NewMessage(tg.Settings.ChatID, "") newMsg.Text = msg - if (tg.Settings.QuoteNick) { + if tg.Settings.QuoteNick { newMsg.ParseMode = "HTML" }