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

Compare commits

...

4 commits

Author SHA1 Message Date
Alex Bramley
de089e2f58 Make split marker configurable; tests. 2025-09-05 15:23:38 +01:00
Alex Bramley
53eac75162 Bump x/net dep to shut dependabot up; mod tidy. 2025-09-05 14:35:19 +01:00
Alex Bramley
893a0b4077 Additional splitMessage tests. 2025-09-05 14:31:19 +01:00
Tamás Solymos
40e594abf8 Fix NewNick's comment
It wasn't updated after fixing https://github.com/fluffle/goirc/issues/108.
2023-11-27 11:37:13 +00:00
5 changed files with 91 additions and 85 deletions

View file

@ -6,36 +6,37 @@ import (
)
const (
REGISTER = "REGISTER"
CONNECTED = "CONNECTED"
DISCONNECTED = "DISCONNECTED"
ACTION = "ACTION"
AUTHENTICATE = "AUTHENTICATE"
AWAY = "AWAY"
CAP = "CAP"
CTCP = "CTCP"
CTCPREPLY = "CTCPREPLY"
ERROR = "ERROR"
INVITE = "INVITE"
JOIN = "JOIN"
KICK = "KICK"
MODE = "MODE"
NICK = "NICK"
NOTICE = "NOTICE"
OPER = "OPER"
PART = "PART"
PASS = "PASS"
PING = "PING"
PONG = "PONG"
PRIVMSG = "PRIVMSG"
QUIT = "QUIT"
TOPIC = "TOPIC"
USER = "USER"
VERSION = "VERSION"
VHOST = "VHOST"
WHO = "WHO"
WHOIS = "WHOIS"
defaultSplit = 450
REGISTER = "REGISTER"
CONNECTED = "CONNECTED"
DISCONNECTED = "DISCONNECTED"
ACTION = "ACTION"
AUTHENTICATE = "AUTHENTICATE"
AWAY = "AWAY"
CAP = "CAP"
CTCP = "CTCP"
CTCPREPLY = "CTCPREPLY"
ERROR = "ERROR"
INVITE = "INVITE"
JOIN = "JOIN"
KICK = "KICK"
MODE = "MODE"
NICK = "NICK"
NOTICE = "NOTICE"
OPER = "OPER"
PART = "PART"
PASS = "PASS"
PING = "PING"
PONG = "PONG"
PRIVMSG = "PRIVMSG"
QUIT = "QUIT"
TOPIC = "TOPIC"
USER = "USER"
VERSION = "VERSION"
VHOST = "VHOST"
WHO = "WHO"
WHOIS = "WHOIS"
defaultSplit = 450
defaultMarker = "..."
)
// 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
}
// 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:
// 1. the end of the last sentence fragment before splitLen
// 2. the end of the last word before splitLen
// 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 ;-)
if splitLen < 13 {
splitLen = defaultSplit
}
markerLen := len(marker)
if markerLen >= splitLen {
// This will end badly otherwise :-)
markerLen = len(defaultMarker)
marker = defaultMarker
}
for len(msg) > splitLen {
idx := indexFragment(msg[:splitLen-3])
idx := indexFragment(msg[:splitLen-markerLen])
if idx < 0 {
idx = splitLen - 3
idx = splitLen - markerLen
}
msgs = append(msgs, msg[:idx]+"...")
msgs = append(msgs, msg[:idx]+marker)
msg = msg[idx:]
}
return append(msgs, msg)
@ -177,7 +191,7 @@ func (conn *Conn) Who(nick string) { conn.Raw(WHO + " " + nick) }
// PRIVMSG t :msg
func (conn *Conn) Privmsg(t, msg string) {
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)
}
}
@ -207,7 +221,7 @@ func (conn *Conn) Privmsgf(t, format string, a ...interface{}) {
// will be sent to the target containing sequential parts of msg.
// NOTICE t :msg
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)
}
}
@ -217,7 +231,7 @@ func (conn *Conn) Notice(t, msg string) {
// PRIVMSG t :\001CTCP arg\001
func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
// 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 != "" {
s = " " + s
}
@ -230,7 +244,7 @@ func (conn *Conn) Ctcp(t, ctcp string, arg ...string) {
// or channel t, with an optional argument.
// NOTICE t :\001CTCP arg\001
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 != "" {
s = " " + s
}

View file

@ -53,23 +53,44 @@ func TestIndexFragment(t *testing.T) {
}
}
var (
longString = strings.Repeat("long ", 100)
longIdx = indexFragment(longString[:defaultSplit-3])
)
func TestSplitMessage(t *testing.T) {
tests := []struct {
in string
sp int
mk string
out []string
}{
{"", 0, []string{""}},
{"foo", 0, []string{"foo"}},
{"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"}},
{"0123456789012345", 0, []string{"0123456789012345"}},
{"0123456789012345", 15, []string{"012345678901...", "2345"}},
{"0123456789012345", 16, []string{"0123456789012345"}},
{"", 0, "...", []string{""}},
{"foo", 0, "...", []string{"foo"}},
{"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"}},
{"01234567890123456", 0, "...", []string{"01234567890123456"}},
{"01234567890123456", 13, "", []string{"0123456789012", "3456"}},
{"01234567890123456", 14, ".", []string{"0123456789012.", "3456"}},
{"01234567890123456", 15, "..", []string{"0123456789012..", "3456"}},
{"01234567890123456", 16, "...", []string{"0123456789012...", "3456"}},
{"01234567890123456", 17, "...", []string{"01234567890123456"}},
{"01234567890123456", 14, "abcdefg", []string{"0123456abcdefg", "7890123456"}},
{"01234567890123456", 14, "abcdefghijkl", []string{
"01abcdefghijkl",
"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 {
out := splitMessage(test.in, test.sp)
out := splitMessage(test.in, test.sp, test.mk)
if !reflect.DeepEqual(test.out, out) {
t.Errorf("test %d: expected %q, got %q", i, test.out, out)
}

View file

@ -108,7 +108,8 @@ type Config struct {
Sasl sasl.Client
// Replaceable function to customise the 433 handler's new nick.
// By default an underscore "_" is appended to the current nick.
// By default the current nick's last character is "incremented".
// See DefaultNewNick implementation below for details.
NewNick func(string) string
// Client->server ping frequency, in seconds. Defaults to 3m.
@ -135,6 +136,8 @@ type Config struct {
// Split PRIVMSGs, NOTICEs and CTCPs longer than SplitLen characters
// over multiple lines. Default to 450 if not set.
SplitLen int
// Defaults to appending "..." to a split line to indicate a split.
SplitMarker string
}
// NewConfig creates a Config struct containing sensible defaults.
@ -148,6 +151,7 @@ func NewConfig(nick string, args ...string) *Config {
NewNick: DefaultNewNick,
Recover: (*Conn).LogPanic, // in dispatch.go
SplitLen: defaultSplit,
SplitMarker: defaultMarker,
Timeout: 60 * time.Second,
EnableCapabilityNegotiation: false,
}

4
go.mod
View file

@ -5,7 +5,7 @@ require (
github.com/fluffle/golog/logging v0.0.0-20180928190033-7d99e85061cb
github.com/golang/glog v1.0.0
github.com/golang/mock v1.5.0
golang.org/x/net v0.18.0
golang.org/x/net v0.43.0
)
go 1.13
go 1.23.0

37
go.sum
View file

@ -7,51 +7,18 @@ github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0L
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.5.0 h1:jlYHihg//f7RRwuPfptm04yp4s7O6Kw8EZiVYIGcH0g=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.14.0/go.mod h1:TySc+nGkYR6qt8km8wUhuFRTVSMIX3XPR58y2lC8vww=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=