restore simplified ParseLine interface

This commit is contained in:
Shivaram Lingamneni 2019-03-03 14:40:00 -05:00
parent e0907adffb
commit 5853469061
3 changed files with 26 additions and 16 deletions

@ -129,6 +129,24 @@ func (msg *IrcMessage) ClientOnlyTags() map[string]string {
return msg.clientOnlyTags
}
// ParseLine creates and returns a message from the given IRC line.
func ParseLine(line string) (ircmsg IrcMessage, err error) {
return parseLine(line, 0, 0)
}
// ParseLineStrict creates and returns an IrcMessage from the given IRC line,
// taking the maximum length into account and truncating the message as appropriate.
// If fromClient is true, it enforces the client limit on tag data length (4094 bytes),
// allowing the server to return ERR_INPUTTOOLONG as appropriate. If truncateLen is
// nonzero, it is the length at which the non-tag portion of the message is truncated.
func ParseLineStrict(line string, fromClient bool, truncateLen int) (ircmsg IrcMessage, err error) {
maxTagDataLength := MaxlenTagData
if fromClient {
maxTagDataLength = MaxlenClientTagData
}
return parseLine(line, maxTagDataLength, truncateLen)
}
// slice off any amount of '\r' or '\n' from the end of the string
func trimFinalNewlines(str string) string {
var i int
@ -137,11 +155,7 @@ func trimFinalNewlines(str string) string {
return str[:i+1]
}
// ParseLine creates and returns an IrcMessage from the given IRC line,
// taking the maximum length into account and truncating the message as appropriate.
// If fromClient is true, it enforces the client limit on tag data length (4094 bytes),
// allowing the server to return ERR_INPUTTOOLONG as appropriate.
func ParseLine(line string, fromClient bool, truncateLen int) (ircmsg IrcMessage, err error) {
func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMessage, err error) {
if strings.IndexByte(line, '\x00') != -1 {
err = ErrorLineContainsBadChar
return
@ -160,11 +174,7 @@ func ParseLine(line string, fromClient bool, truncateLen int) (ircmsg IrcMessage
return ircmsg, ErrorLineIsEmpty
}
tags := line[1:tagEnd]
maxTagDataLength := MaxlenTagData
if fromClient {
maxTagDataLength = MaxlenClientTagData
}
if maxTagDataLength < len(tags) {
if 0 < maxTagDataLength && maxTagDataLength < len(tags) {
return ircmsg, ErrorLineTooLong
}
err = ircmsg.parseTags(tags)

@ -90,7 +90,7 @@ var decodetesterrors = []testparseerror{
func TestDecode(t *testing.T) {
for _, pair := range decodelentests {
ircmsg, err := ParseLine(pair.raw, true, pair.length)
ircmsg, err := ParseLineStrict(pair.raw, true, pair.length)
if err != nil {
t.Error(
"For", pair.raw,
@ -107,7 +107,7 @@ func TestDecode(t *testing.T) {
}
}
for _, pair := range decodetests {
ircmsg, err := ParseLine(pair.raw, true, 0)
ircmsg, err := ParseLine(pair.raw)
if err != nil {
t.Error(
"For", pair.raw,
@ -124,7 +124,7 @@ func TestDecode(t *testing.T) {
}
}
for _, pair := range decodetesterrors {
_, err := ParseLine(pair.raw, true, 0)
_, err := ParseLineStrict(pair.raw, true, 0)
if err != pair.err {
t.Error(
"For", pair.raw,
@ -275,7 +275,7 @@ func TestEncodeDecode(t *testing.T) {
if err != nil {
t.Errorf("Couldn't encode %v: %v", message, err)
}
parsed, err := ParseLine(encoded, true, 0)
parsed, err := ParseLineStrict(encoded, true, 0)
if err != nil {
t.Errorf("Couldn't re-decode %v: %v", encoded, err)
}
@ -360,6 +360,6 @@ func BenchmarkGenerate(b *testing.B) {
func BenchmarkParse(b *testing.B) {
line := "@account=shivaram;draft/msgid=dqhkgglocqikjqikbkcdnv5dsq;time=2019-03-01T20:11:21.833Z :shivaram!~shivaram@good-fortune PRIVMSG #darwin :you're an EU citizen, right? it's illegal for you to be here now"
for i := 0; i < b.N; i++ {
ParseLine(line, false, 0)
ParseLineStrict(line, false, 0)
}
}

@ -84,7 +84,7 @@ var tagdecodetests = []testtags{
}
func parseTags(rawTags string) (map[string]string, error) {
message, err := ParseLine(fmt.Sprintf("@%s :shivaram TAGMSG #darwin\r\n", rawTags), true, 0)
message, err := ParseLineStrict(fmt.Sprintf("@%s :shivaram TAGMSG #darwin\r\n", rawTags), true, 0)
return message.AllTags(), err
}