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.
This commit is contained in:
Morgan McMillian 2021-07-24 08:27:41 -07:00
parent 151572154b
commit ed8d5695d0
3 changed files with 21 additions and 11 deletions

1
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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 {