From ed8d5695d069e8869d408a83d61b47e8dd1eaf4c Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sat, 24 Jul 2021 08:27:41 -0700 Subject: [PATCH] split long messages to pnut.io pnut.io has a limit of 2048 charecters for messages. This will take incoming messages that exceed that length and split them into multiple messages while preserving the leading message prefix with the username and channel. --- go.mod | 1 + go.sum | 2 ++ pnut-bridge.go | 29 ++++++++++++++++++----------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index d43fbef..41d5aa7 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( git.sr.ht/~thrrgilag/woodstock v0.0.0-20210714032038-b22c4f10cc34 github.com/gabriel-vasile/mimetype v1.3.1 // indirect github.com/gorilla/websocket v1.4.2 + github.com/mitchellh/go-wordwrap v1.0.1 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect gopkg.in/ini.v1 v1.62.0 ) diff --git a/go.sum b/go.sum index 9b5e2c8..7ae2ce1 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= +github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= diff --git a/pnut-bridge.go b/pnut-bridge.go index 51c2eba..958e3ac 100644 --- a/pnut-bridge.go +++ b/pnut-bridge.go @@ -20,6 +20,7 @@ import ( "git.sr.ht/~thrrgilag/woodstock" "github.com/gabriel-vasile/mimetype" "github.com/gorilla/websocket" + "github.com/mitchellh/go-wordwrap" "gopkg.in/ini.v1" ) @@ -127,8 +128,6 @@ func subscribe(connection_id string, access_token string, rooms []Room) { } defer resp.Body.Close() log.Printf("<< %s", resp.Status) - //body, _ := ioutil.ReadAll(resp.Body) - //log.Println(string([]byte(body))) } } @@ -198,8 +197,6 @@ func pnutMsgHandler(conf *Config, msg woodstock.Message) { } defer resp.Body.Close() log.Printf("<< %s", resp.Status) - //body, _ := ioutil.ReadAll(resp.Body) - //log.Println(string([]byte(body))) } func getRoomGateway(rooms []Room, chan_id string) string { @@ -215,7 +212,8 @@ func bridgeMsgHandler(conf *Config, msg MbMessage) { client := woodstock.NewClient("", "") client.SetAccessToken(conf.PnutAccessToken) channel_id := getPnutChannel(conf.Rooms, msg.Gateway) - text := msg.Username + " " + msg.Text + + text := msg.Text var raw []woodstock.Raw for _, file := range msg.Extra.File { fdata, err := base64.StdEncoding.DecodeString(file.Data) @@ -242,13 +240,22 @@ func bridgeMsgHandler(conf *Config, msg MbMessage) { text = text + "\n" + file.URL } } - log.Printf(">> sending message from %s to channel %s ...", msg.Gateway, channel_id) - newmessage := woodstock.NewMessage{Text: text, Raw: raw} - _, err := client.CreateMessage(channel_id, newmessage) - if err != nil { - log.Println("!! problem posting message to pnut.io", err) + + maxlen := 2048 + prelen := len(msg.Username) + 1 + wrapped := wordwrap.WrapString(text, uint(maxlen-prelen)) + lines := strings.Split(wrapped, "\n") + for _, line := range lines { + line := msg.Username + " " + line + log.Printf(">> sending message from %s to channel %s ...", msg.Gateway, channel_id) + newmessage := woodstock.NewMessage{Text: line, Raw: raw} + _, err := client.CreateMessage(channel_id, newmessage) + if err != nil { + log.Println("!! problem posting message to pnut.io", err) + } + log.Println("<< ok") + raw = nil } - log.Println("<< ok") } func getPnutChannel(rooms []Room, gateway string) string {