mqttim

MQTT ↔ Instant Messaging Bridge
git clone https://git.instinctive.eu/mqttim.git
Log | Files | Refs | README | LICENSE

commit ce41f81a6a2f6ab6ba55623953ac7517eacb88ef
parent 057d4883622518649fa9860ade5a3d261f375ef6
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sat, 29 Jun 2024 09:07:03 +0000

Large messages are now split between multiple IRC lines
Diffstat:
Mmain.go | 42+++++++++++++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/main.go b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "errors" "fmt" "github.com/go-mqtt/mqtt" @@ -13,12 +14,15 @@ import ( ) type IrcConfig struct { - Channel string - Server string - Nick string - CmdStart string - CmdMid string - CmdEnd string + Channel string + Server string + Nick string + CmdStart string + CmdMid string + CmdEnd string + MaxLine int + ContSuffix string + ContPrefix string } type MqttConfig struct { @@ -128,8 +132,32 @@ func mqtt2irc(m *mqtt.Client, c chan Msg, config *Config) error { } func ircSender(config *IrcConfig, i *irc.Connection, c chan Msg) error { + var buf bytes.Buffer + for { m := <-c - i.Privmsgf(config.Channel, "%s: %s", m.Topic, m.Message) + + if len(m.Topic)+2+len(m.Message) < config.MaxLine { + i.Privmsgf(config.Channel, "%s: %s", m.Topic, m.Message) + } else { + for s := 0; s < len(m.Message); { + l := len(m.Message) - s + buf.Reset() + buf.Write(m.Topic) + buf.WriteString(": ") + if s > 0 { + buf.WriteString(config.ContPrefix) + } + if buf.Len()+l <= config.MaxLine { + buf.Write(m.Message[s:]) + } else { + l = config.MaxLine - buf.Len() - len(config.ContSuffix) + buf.Write(m.Message[s : s+l]) + buf.WriteString(config.ContSuffix) + } + i.Privmsg(config.Channel, string(buf.Bytes())) + s += l + } + } } }