diff options
author | tsudoko <flan@flande.re> | 2018-02-08 23:28:33 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2018-02-08 23:28:33 +0100 |
commit | 1d33e60e36fa7b0e361990ac347ee8d620d67dcc (patch) | |
tree | 5e2f4fc52602d1d96a27f93053ced9ab2534d044 /bridge/irc | |
parent | 83c28cb857e47e8e2958745eeac7f97448909968 (diff) | |
download | matterbridge-msglm-1d33e60e36fa7b0e361990ac347ee8d620d67dcc.tar.gz matterbridge-msglm-1d33e60e36fa7b0e361990ac347ee8d620d67dcc.tar.bz2 matterbridge-msglm-1d33e60e36fa7b0e361990ac347ee8d620d67dcc.zip |
Truncate messages sent to IRC based on byte count (#368)
* Truncate messages sent to IRC based on byte count
* Avoid unnecessary string allocations
Diffstat (limited to 'bridge/irc')
-rw-r--r-- | bridge/irc/irc.go | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index 0e90aaf9..d95aef4e 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -19,6 +19,7 @@ import ( "strconv" "strings" "time" + "unicode/utf8" ) type Birc struct { @@ -200,9 +201,12 @@ func (b *Birc) Send(msg config.Message) (string, error) { msg.Text = helper.SplitStringLength(msg.Text, b.Config.MessageLength) } for _, text := range strings.Split(msg.Text, "\n") { - input := []rune(text) if len(text) > b.Config.MessageLength { - text = string(input[:b.Config.MessageLength]) + " <message clipped>" + text = text[:b.Config.MessageLength-len(" <message clipped>")] + if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError { + text = text[:len(text)-size] + } + text += " <message clipped>" } if len(b.Local) < b.Config.MessageQueue { if len(b.Local) == b.Config.MessageQueue-1 { |