Skip to content

Commit 1d74711

Browse files
committed
Split long messages to runes, according to IRC server's max line length
1 parent 4e787d8 commit 1d74711

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

internal/handlers/irc/irc.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package irc
22

33
import (
44
"net"
5+
"strings"
56
"time"
7+
"unicode/utf8"
68

79
"github.com/lrstanley/girc"
810
"github.com/ritlug/teleirc/internal"
@@ -91,7 +93,27 @@ func (c Client) ConnectDialer(dialer girc.Dialer) error {
9193
Message sends a PRIVMSG to target (either channel, service, or user).
9294
*/
9395
func (c Client) Message(channel string, msg string) {
94-
c.Cmd.Message(channel, msg)
96+
maxLength := c.MaxEventLength()
97+
98+
if len(msg) <= maxLength {
99+
c.Cmd.Message(channel, msg)
100+
return
101+
}
102+
103+
var msgPart strings.Builder
104+
105+
for _, r := range msg {
106+
if nextLen := msgPart.Len() + utf8.RuneLen(r); nextLen > maxLength {
107+
c.Cmd.Message(channel, msgPart.String())
108+
msgPart.Reset()
109+
}
110+
111+
msgPart.WriteRune(r)
112+
}
113+
114+
if msgPart.Len() > 0 {
115+
c.Cmd.Message(channel, msgPart.String())
116+
}
95117
}
96118

97119
/*

0 commit comments

Comments
 (0)