1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-09-05 16:33:19 +00:00

Make split marker configurable; tests.

This commit is contained in:
Alex Bramley 2025-09-05 15:23:38 +01:00
parent 53eac75162
commit de089e2f58
3 changed files with 80 additions and 54 deletions

View file

@ -6,36 +6,37 @@ import (
) )
const ( const (
REGISTER = "REGISTER" REGISTER = "REGISTER"
CONNECTED = "CONNECTED" CONNECTED = "CONNECTED"
DISCONNECTED = "DISCONNECTED" DISCONNECTED = "DISCONNECTED"
ACTION = "ACTION" ACTION = "ACTION"
AUTHENTICATE = "AUTHENTICATE" AUTHENTICATE = "AUTHENTICATE"
AWAY = "AWAY" AWAY = "AWAY"
CAP = "CAP" CAP = "CAP"
CTCP = "CTCP" CTCP = "CTCP"
CTCPREPLY = "CTCPREPLY" CTCPREPLY = "CTCPREPLY"
ERROR = "ERROR" ERROR = "ERROR"
INVITE = "INVITE" INVITE = "INVITE"
JOIN = "JOIN" JOIN = "JOIN"
KICK = "KICK" KICK = "KICK"
MODE = "MODE" MODE = "MODE"
NICK = "NICK" NICK = "NICK"
NOTICE = "NOTICE" NOTICE = "NOTICE"
OPER = "OPER" OPER = "OPER"
PART = "PART" PART = "PART"
PASS = "PASS" PASS = "PASS"
PING = "PING" PING = "PING"
PONG = "PONG" PONG = "PONG"
PRIVMSG = "PRIVMSG" PRIVMSG = "PRIVMSG"
QUIT = "QUIT" QUIT = "QUIT"
TOPIC = "TOPIC" TOPIC = "TOPIC"
USER = "USER" USER = "USER"
VERSION = "VERSION" VERSION = "VERSION"
VHOST = "VHOST" VHOST = "VHOST"
WHO = "WHO" WHO = "WHO"
WHOIS = "WHOIS" WHOIS = "WHOIS"
defaultSplit = 450 defaultSplit = 450
defaultMarker = "..."
) )
// cutNewLines() pares down a string to the part before the first "\r" or "\n". // cutNewLines() pares down a string to the part before the first "\r" or "\n".
@ -65,21 +66,34 @@ func indexFragment(s string) int {
return -1 return -1
} }
// SplitDefaults permits read-only access to goirc's default split params.
func SplitDefaults() (int, string) {
return defaultSplit, defaultMarker
}
// splitMessage splits a message > splitLen chars at: // splitMessage splits a message > splitLen chars at:
// 1. the end of the last sentence fragment before splitLen // 1. the end of the last sentence fragment before splitLen
// 2. the end of the last word before splitLen // 2. the end of the last word before splitLen
// 3. splitLen itself // 3. splitLen itself
func splitMessage(msg string, splitLen int) (msgs []string) { func splitMessage(msg string, splitLen int, marker string) (msgs []string) {
// This is quite short ;-) // This is quite short ;-)
if splitLen < 13 { if splitLen < 13 {
splitLen = defaultSplit splitLen = defaultSplit
} }
markerLen := len(marker)
if markerLen >= splitLen {
// This will end badly otherwise :-)
markerLen = len(defaultMarker)
marker = defaultMarker
}
for len(msg) > splitLen { for len(msg) > splitLen {
idx := indexFragment(msg[:splitLen-3]) idx := indexFragment(msg[:splitLen-markerLen])
if idx < 0 { if idx < 0 {
idx = splitLen - 3 idx = splitLen - markerLen
} }
msgs = append(msgs, msg[:idx]+"...") msgs = append(msgs, msg[:idx]+marker)
msg = msg[idx:] msg = msg[idx:]
} }
return append(msgs, msg) return append(msgs, msg)
@ -177,7 +191,7 @@ func (conn *Conn) Who(nick string) { conn.Raw(WHO + " " + nick) }
// PRIVMSG t :msg // PRIVMSG t :msg
func (conn *Conn) Privmsg(t, msg string) { func (conn *Conn) Privmsg(t, msg string) {
prefix := PRIVMSG + " " + t + " :" prefix := PRIVMSG + " " + t + " :"
for _, s := range splitMessage(msg, conn.cfg.SplitLen) { for _, s := range splitMessage(msg, conn.cfg.SplitLen, conn.cfg.SplitMarker) {
conn.Raw(prefix + s) conn.Raw(prefix + s)
} }
} }
@ -207,7 +221,7 @@ func (conn *Conn) Privmsgf(t, format string, a ...interface{}) {
// will be sent to the target containing sequential parts of msg. // will be sent to the target containing sequential parts of msg.
// NOTICE t :msg // NOTICE t :msg
func (conn *Conn) Notice(t, msg string) { func (conn *Conn) Notice(t, msg string) {
for _, s := range splitMessage(msg, conn.cfg.SplitLen) { for _, s := range splitMessage(msg, conn.cfg.SplitLen, conn.cfg.SplitMarker) {
conn.Raw(NOTICE + " " + t + " :" + s) conn.Raw(NOTICE + " " + t + " :" + s)
} }
} }
@ -217,7 +231,7 @@ func (conn *Conn) Notice(t, msg string) {
// PRIVMSG t :\001CTCP arg\001 // PRIVMSG t :\001CTCP arg\001
func (conn *Conn) Ctcp(t, ctcp string, arg ...string) { func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
// We need to split again here to ensure // We need to split again here to ensure
for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) { for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen, conn.cfg.SplitMarker) {
if s != "" { if s != "" {
s = " " + s s = " " + s
} }
@ -230,7 +244,7 @@ func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
// or channel t, with an optional argument. // or channel t, with an optional argument.
// NOTICE t :\001CTCP arg\001 // NOTICE t :\001CTCP arg\001
func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) { func (conn *Conn) CtcpReply(t, ctcp string, arg ...string) {
for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) { for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen, conn.cfg.SplitMarker) {
if s != "" { if s != "" {
s = " " + s s = " " + s
} }

View file

@ -62,26 +62,35 @@ func TestSplitMessage(t *testing.T) {
tests := []struct { tests := []struct {
in string in string
sp int sp int
mk string
out []string out []string
}{ }{
{"", 0, []string{""}}, {"", 0, "...", []string{""}},
{"foo", 0, []string{"foo"}}, {"foo", 0, "...", []string{"foo"}},
{"foo bar baz beep", 0, []string{"foo bar baz beep"}}, {"foo bar baz beep", 0, "...", []string{"foo bar baz beep"}},
{"foo bar baz beep", 15, []string{"foo bar baz ...", "beep"}}, {"foo bar baz beep", 15, "...", []string{"foo bar baz ...", "beep"}},
{"foo bar, baz beep", 15, []string{"foo bar, ...", "baz beep"}}, {"foo bar, baz beep", 15, "...", []string{"foo bar, ...", "baz beep"}},
{"01234567890123456", 0, []string{"01234567890123456"}}, {"01234567890123456", 0, "...", []string{"01234567890123456"}},
{"01234567890123456", 13, []string{"0123456789...", "0123456"}}, {"01234567890123456", 13, "", []string{"0123456789012", "3456"}},
{"01234567890123456", 14, []string{"01234567890...", "123456"}}, {"01234567890123456", 14, ".", []string{"0123456789012.", "3456"}},
{"01234567890123456", 15, []string{"012345678901...", "23456"}}, {"01234567890123456", 15, "..", []string{"0123456789012..", "3456"}},
{"01234567890123456", 16, []string{"0123456789012...", "3456"}}, {"01234567890123456", 16, "...", []string{"0123456789012...", "3456"}},
{"01234567890123456", 17, []string{"01234567890123456"}}, {"01234567890123456", 17, "...", []string{"01234567890123456"}},
{longString, 0, []string{longString[:longIdx] + "...", longString[longIdx:]}}, {"01234567890123456", 14, "abcdefg", []string{"0123456abcdefg", "7890123456"}},
{longString[:defaultSplit-1], 0, []string{longString[:defaultSplit-1]}}, {"01234567890123456", 14, "abcdefghijkl", []string{
{longString[:defaultSplit], 0, []string{longString[:defaultSplit]}}, "01abcdefghijkl",
{longString[:defaultSplit+1], 0, []string{longString[:longIdx] + "...", longString[longIdx : defaultSplit+1]}}, "23abcdefghijkl",
"4567890123456",
}},
// splitlen = markerlen => reset to default marker
{"01234567890123456", 14, "abcdefghijklmn", []string{"01234567890...", "123456"}},
{longString, 0, "...", []string{longString[:longIdx] + "...", longString[longIdx:]}},
{longString[:defaultSplit-1], 0, "...", []string{longString[:defaultSplit-1]}},
{longString[:defaultSplit], 0, "...", []string{longString[:defaultSplit]}},
{longString[:defaultSplit+1], 0, "...", []string{longString[:longIdx] + "...", longString[longIdx : defaultSplit+1]}},
} }
for i, test := range tests { for i, test := range tests {
out := splitMessage(test.in, test.sp) out := splitMessage(test.in, test.sp, test.mk)
if !reflect.DeepEqual(test.out, out) { if !reflect.DeepEqual(test.out, out) {
t.Errorf("test %d: expected %q, got %q", i, test.out, out) t.Errorf("test %d: expected %q, got %q", i, test.out, out)
} }

View file

@ -136,6 +136,8 @@ type Config struct {
// Split PRIVMSGs, NOTICEs and CTCPs longer than SplitLen characters // Split PRIVMSGs, NOTICEs and CTCPs longer than SplitLen characters
// over multiple lines. Default to 450 if not set. // over multiple lines. Default to 450 if not set.
SplitLen int SplitLen int
// Defaults to appending "..." to a split line to indicate a split.
SplitMarker string
} }
// NewConfig creates a Config struct containing sensible defaults. // NewConfig creates a Config struct containing sensible defaults.
@ -149,6 +151,7 @@ func NewConfig(nick string, args ...string) *Config {
NewNick: DefaultNewNick, NewNick: DefaultNewNick,
Recover: (*Conn).LogPanic, // in dispatch.go Recover: (*Conn).LogPanic, // in dispatch.go
SplitLen: defaultSplit, SplitLen: defaultSplit,
SplitMarker: defaultMarker,
Timeout: 60 * time.Second, Timeout: 60 * time.Second,
EnableCapabilityNegotiation: false, EnableCapabilityNegotiation: false,
} }