Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
update ircmsg for v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
slingamn committed Jul 14, 2019
1 parent 6fb1b63 commit 29f6faa
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 40 deletions.
84 changes: 55 additions & 29 deletions github.com/goshuirc/irc-go/ircfmt/ircfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ const (
underline string = "\x1f"
reset string = "\x0f"

runecolour rune = '\x03'
runecolour rune = '\x03'
runebold rune = '\x02'
runemonospace rune = '\x11'
runereverseColour rune = '\x16'
runeitalic rune = '\x1d'
runestrikethrough rune = '\x1e'
runereset rune = '\x0f'
runeunderline rune = '\x1f'

// valid characters in a colour code character, for speed
colours1 string = "0123456789"
Expand Down Expand Up @@ -171,8 +178,16 @@ func Escape(in string) string {
out.WriteRune(']')

} else {
out.WriteRune(inRunes[0])
inRunes = inRunes[1:]
// special case for $$c
if len(inRunes) > 2 && inRunes[0] == '$' && inRunes[1] == '$' && inRunes[2] == 'c' {
out.WriteRune(inRunes[0])
out.WriteRune(inRunes[1])
out.WriteRune(inRunes[2])
inRunes = inRunes[3:]
} else {
out.WriteRune(inRunes[0])
inRunes = inRunes[1:]
}
}
}

Expand All @@ -183,37 +198,48 @@ func Escape(in string) string {
// IE, it turns this: "This is a \x02cool\x02, \x034red\x0f message!"
// into: "This is a cool, red message!"
func Strip(in string) string {
// replace all our usual escapes
in = valToStrip.Replace(in)

inRunes := []rune(in)
out := strings.Builder{}
for 0 < len(inRunes) {
if 1 < len(inRunes) && inRunes[0] == '$' && inRunes[1] == 'c' {
inRunes = inRunes[2:] // strip colour code chars

if len(inRunes) < 1 || !strings.ContainsRune(colours1, inRunes[0]) {
continue
}
runes := []rune(in)
if out.Len() < len(runes) { // Reduce allocations where needed
out.Grow(len(in) - out.Len())
}
for len(runes) > 0 {
switch runes[0] {
case runebold, runemonospace, runereverseColour, runeitalic, runestrikethrough, runeunderline, runereset:
runes = runes[1:]
case runecolour:
runes = removeColour(runes)
default:
out.WriteRune(runes[0])
runes = runes[1:]
}
}
return out.String()
}

inRunes = inRunes[1:]
if 0 < len(inRunes) && strings.ContainsRune(colours1, inRunes[0]) {
inRunes = inRunes[1:]
}
if 1 < len(inRunes) && inRunes[0] == ',' && strings.ContainsRune(colours1, inRunes[1]) {
inRunes = inRunes[2:]
if 0 < len(inRunes) && strings.ContainsRune(colours1, inRunes[0]) {
inRunes = inRunes[1:]
}
}
func removeNumber(runes []rune) []rune {
if len(runes) > 0 && runes[0] >= '0' && runes[0] <= '9' {
runes = runes[1:]
}
return runes
}

} else {
out.WriteRune(inRunes[0])
inRunes = inRunes[1:]
}
func removeColour(runes []rune) []rune {
if runes[0] != runecolour {
return runes
}

return out.String()
runes = runes[1:]
runes = removeNumber(runes)
runes = removeNumber(runes)

if len(runes) > 1 && runes[0] == ',' && runes[1] >= '0' && runes[1] <= '9' {
runes = runes[2:]
} else {
return runes // Nothing else because we dont have a comma
}
runes = removeNumber(runes)
return runes
}

// Unescape takes our escaped string and returns a raw IRC string.
Expand Down
43 changes: 32 additions & 11 deletions github.com/goshuirc/irc-go/ircmsg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func ParseLineStrict(line string, fromClient bool, truncateLen int) (ircmsg IrcM
return parseLine(line, maxTagDataLength, truncateLen)
}

// slice off any amount of '\r' or '\n' from the end of the string
func trimFinalNewlines(str string) string {
// slice off any amount of ' ' from the front of the string
func trimInitialSpaces(str string) string {
var i int
for i = len(str) - 1; 0 <= i && (str[i] == '\r' || str[i] == '\n'); i -= 1 {
for i = 0; i < len(str) && str[i] == ' '; i += 1 {
}
return str[:i+1]
return str[i:]
}

func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMessage, err error) {
Expand All @@ -161,7 +161,15 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
return
}

line = trimFinalNewlines(line)
// trim to the first appearance of either '\r' or '\n':
lineEnd := strings.IndexByte(line, '\r')
newlineIndex := strings.IndexByte(line, '\n')
if newlineIndex != -1 && (lineEnd == -1 || newlineIndex < lineEnd) {
lineEnd = newlineIndex
}
if lineEnd != -1 {
line = line[:lineEnd]
}

if len(line) < 1 {
return ircmsg, ErrorLineIsEmpty
Expand Down Expand Up @@ -190,8 +198,12 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
line = line[:truncateLen]
}

// modern: "These message parts, and parameters themselves, are separated
// by one or more ASCII SPACE characters"
line = trimInitialSpaces(line)

// prefix
if line[0] == ':' {
if 0 < len(line) && line[0] == ':' {
prefixEnd := strings.IndexByte(line, ' ')
if prefixEnd == -1 {
return ircmsg, ErrorLineIsEmpty
Expand All @@ -201,6 +213,8 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
line = line[prefixEnd+1:]
}

line = trimInitialSpaces(line)

// command
commandEnd := strings.IndexByte(line, ' ')
paramStart := commandEnd + 1
Expand All @@ -209,13 +223,17 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
paramStart = len(line)
}
// normalize command to uppercase:
ircmsg.Command = strings.ToUpper(strings.TrimSpace(line[:commandEnd]))
ircmsg.Command = strings.ToUpper(line[:commandEnd])
if len(ircmsg.Command) == 0 {
return ircmsg, ErrorLineIsEmpty
}
line = line[paramStart:]

for 0 < len(line) {
for {
line = trimInitialSpaces(line)
if len(line) == 0 {
break
}
// handle trailing
if line[0] == ':' {
ircmsg.Params = append(ircmsg.Params, line[1:])
Expand All @@ -225,9 +243,6 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
if paramEnd == -1 {
ircmsg.Params = append(ircmsg.Params, line)
break
} else if paramEnd == 0 {
// only a trailing parameter can be empty
return ircmsg, ErrorLineContainsBadChar
}
ircmsg.Params = append(ircmsg.Params, line[:paramEnd])
line = line[paramEnd+1:]
Expand Down Expand Up @@ -280,6 +295,12 @@ func (ircmsg *IrcMessage) Line() (result string, err error) {
return
}

// LineBytes returns a sendable line created from an IrcMessage.
func (ircmsg *IrcMessage) LineBytes() (result []byte, err error) {
result, err = ircmsg.line(0, 0, 0, 0)
return
}

// LineBytesStrict returns a sendable line, as a []byte, created from an IrcMessage.
// fromClient controls whether the server-side or client-side tag length limit
// is enforced. If truncateLen is nonzero, it is the length at which the
Expand Down

0 comments on commit 29f6faa

Please sign in to comment.