1
0
Fork 0
forked from External/ergo

upgrade irc-go to latest master

This commit is contained in:
Shivaram Lingamneni 2020-03-11 18:52:50 -04:00
parent b2207ef373
commit 3d13c1bc5f
4 changed files with 23 additions and 6 deletions

View file

@ -52,10 +52,19 @@ type IrcMessage struct {
Prefix string
Command string
Params []string
forceTrailing bool
tags map[string]string
clientOnlyTags map[string]string
}
// ForceTrailing ensures that when the message is serialized, the final parameter
// will be encoded as a "trailing parameter" (preceded by a colon). This is
// almost never necessary and should not be used except when having to interact
// with broken implementations that don't correctly interpret IRC messages.
func (msg *IrcMessage) ForceTrailing() {
msg.forceTrailing = true
}
// GetTag returns whether a tag is present, and if so, what its value is.
func (msg *IrcMessage) GetTag(tagName string) (present bool, value string) {
if len(tagName) == 0 {
@ -321,6 +330,10 @@ func (ircmsg *IrcMessage) LineBytesStrict(fromClient bool, truncateLen int) ([]b
return ircmsg.line(tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit, truncateLen)
}
func paramRequiresTrailing(param string) bool {
return len(param) == 0 || strings.IndexByte(param, ' ') != -1 || param[0] == ':'
}
// line returns a sendable line created from an IrcMessage.
func (ircmsg *IrcMessage) line(tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit, truncateLen int) ([]byte, error) {
if len(ircmsg.Command) < 1 {
@ -376,11 +389,12 @@ func (ircmsg *IrcMessage) line(tagLimit, clientOnlyTagDataLimit, serverAddedTagD
for i, param := range ircmsg.Params {
buf.WriteByte(' ')
if len(param) < 1 || strings.IndexByte(param, ' ') != -1 || param[0] == ':' {
if i != len(ircmsg.Params)-1 {
return nil, ErrorBadParam
}
requiresTrailing := paramRequiresTrailing(param)
lastParam := i == len(ircmsg.Params)-1
if (requiresTrailing || ircmsg.forceTrailing) && lastParam {
buf.WriteByte(':')
} else if requiresTrailing && !lastParam {
return nil, ErrorBadParam
}
buf.WriteString(param)
}