1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-09-15 12:53:19 +00:00

Compare commits

..

No commits in common. "master" and "v1.3.2" have entirely different histories.

3 changed files with 54 additions and 80 deletions

View file

@ -36,7 +36,6 @@ const (
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".
@ -66,34 +65,21 @@ 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, marker string) (msgs []string) { func splitMessage(msg string, splitLen int) (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-markerLen]) idx := indexFragment(msg[:splitLen-3])
if idx < 0 { if idx < 0 {
idx = splitLen - markerLen idx = splitLen - 3
} }
msgs = append(msgs, msg[:idx]+marker) msgs = append(msgs, msg[:idx]+"...")
msg = msg[idx:] msg = msg[idx:]
} }
return append(msgs, msg) return append(msgs, msg)
@ -191,7 +177,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, conn.cfg.SplitMarker) { for _, s := range splitMessage(msg, conn.cfg.SplitLen) {
conn.Raw(prefix + s) conn.Raw(prefix + s)
} }
} }
@ -221,7 +207,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, conn.cfg.SplitMarker) { for _, s := range splitMessage(msg, conn.cfg.SplitLen) {
conn.Raw(NOTICE + " " + t + " :" + s) conn.Raw(NOTICE + " " + t + " :" + s)
} }
} }
@ -231,7 +217,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, conn.cfg.SplitMarker) { for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) {
if s != "" { if s != "" {
s = " " + s s = " " + s
} }
@ -244,7 +230,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, conn.cfg.SplitMarker) { for _, s := range splitMessage(strings.Join(arg, " "), conn.cfg.SplitLen) {
if s != "" { if s != "" {
s = " " + s s = " " + s
} }

View file

@ -62,35 +62,26 @@ 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{"0123456789012", "3456"}}, {"01234567890123456", 13, []string{"0123456789...", "0123456"}},
{"01234567890123456", 14, ".", []string{"0123456789012.", "3456"}}, {"01234567890123456", 14, []string{"01234567890...", "123456"}},
{"01234567890123456", 15, "..", []string{"0123456789012..", "3456"}}, {"01234567890123456", 15, []string{"012345678901...", "23456"}},
{"01234567890123456", 16, "...", []string{"0123456789012...", "3456"}}, {"01234567890123456", 16, []string{"0123456789012...", "3456"}},
{"01234567890123456", 17, "...", []string{"01234567890123456"}}, {"01234567890123456", 17, []string{"01234567890123456"}},
{"01234567890123456", 14, "abcdefg", []string{"0123456abcdefg", "7890123456"}}, {longString, 0, []string{longString[:longIdx] + "...", longString[longIdx:]}},
{"01234567890123456", 14, "abcdefghijkl", []string{ {longString[:defaultSplit-1], 0, []string{longString[:defaultSplit-1]}},
"01abcdefghijkl", {longString[:defaultSplit], 0, []string{longString[:defaultSplit]}},
"23abcdefghijkl", {longString[:defaultSplit+1], 0, []string{longString[:longIdx] + "...", longString[longIdx : defaultSplit+1]}},
"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, test.mk) out := splitMessage(test.in, test.sp)
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,8 +136,6 @@ 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.
@ -151,7 +149,6 @@ 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,
} }