Skip to content

Commit

Permalink
Split long messages to runes, according to IRC server's max line length
Browse files Browse the repository at this point in the history
  • Loading branch information
user890104 committed Nov 28, 2023
1 parent 4e787d8 commit 1d74711
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion internal/handlers/irc/irc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package irc

import (
"net"
"strings"
"time"
"unicode/utf8"

"github.com/lrstanley/girc"
"github.com/ritlug/teleirc/internal"
Expand Down Expand Up @@ -91,7 +93,27 @@ func (c Client) ConnectDialer(dialer girc.Dialer) error {
Message sends a PRIVMSG to target (either channel, service, or user).
*/
func (c Client) Message(channel string, msg string) {
c.Cmd.Message(channel, msg)
maxLength := c.MaxEventLength()

if len(msg) <= maxLength {
c.Cmd.Message(channel, msg)
return
}

var msgPart strings.Builder

for _, r := range msg {
if nextLen := msgPart.Len() + utf8.RuneLen(r); nextLen > maxLength {
c.Cmd.Message(channel, msgPart.String())
msgPart.Reset()
}

msgPart.WriteRune(r)
}

if msgPart.Len() > 0 {
c.Cmd.Message(channel, msgPart.String())
}
}

/*
Expand Down

0 comments on commit 1d74711

Please sign in to comment.