Merge pull request #31 from slingamn/rename

rename IrcMessage to IRCMessage
This commit is contained in:
Shivaram Lingamneni 2021-02-15 11:24:35 -05:00 committed by GitHub
commit 14cd697c0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 32 deletions

@ -48,10 +48,10 @@ var (
ErrorBadParam = errors.New("Cannot have an empty param, a param with spaces, or a param that starts with ':' before the last parameter") ErrorBadParam = errors.New("Cannot have an empty param, a param with spaces, or a param that starts with ':' before the last parameter")
) )
// IrcMessage represents an IRC message, as defined by the RFCs and as // IRCMessage represents an IRC message, as defined by the RFCs and as
// extended by the IRCv3 Message Tags specification with the introduction // extended by the IRCv3 Message Tags specification with the introduction
// of message tags. // of message tags.
type IrcMessage struct { type IRCMessage struct {
Prefix string Prefix string
Command string Command string
Params []string Params []string
@ -64,12 +64,12 @@ type IrcMessage struct {
// will be encoded as a "trailing parameter" (preceded by a colon). This is // 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 // almost never necessary and should not be used except when having to interact
// with broken implementations that don't correctly interpret IRC messages. // with broken implementations that don't correctly interpret IRC messages.
func (msg *IrcMessage) ForceTrailing() { func (msg *IRCMessage) ForceTrailing() {
msg.forceTrailing = true msg.forceTrailing = true
} }
// GetTag returns whether a tag is present, and if so, what its value is. // GetTag returns whether a tag is present, and if so, what its value is.
func (msg *IrcMessage) GetTag(tagName string) (present bool, value string) { func (msg *IRCMessage) GetTag(tagName string) (present bool, value string) {
if len(tagName) == 0 { if len(tagName) == 0 {
return return
} else if tagName[0] == '+' { } else if tagName[0] == '+' {
@ -82,13 +82,13 @@ func (msg *IrcMessage) GetTag(tagName string) (present bool, value string) {
} }
// HasTag returns whether a tag is present. // HasTag returns whether a tag is present.
func (msg *IrcMessage) HasTag(tagName string) (present bool) { func (msg *IRCMessage) HasTag(tagName string) (present bool) {
present, _ = msg.GetTag(tagName) present, _ = msg.GetTag(tagName)
return return
} }
// SetTag sets a tag. // SetTag sets a tag.
func (msg *IrcMessage) SetTag(tagName, tagValue string) { func (msg *IRCMessage) SetTag(tagName, tagValue string) {
if len(tagName) == 0 { if len(tagName) == 0 {
return return
} else if tagName[0] == '+' { } else if tagName[0] == '+' {
@ -105,7 +105,7 @@ func (msg *IrcMessage) SetTag(tagName, tagValue string) {
} }
// DeleteTag deletes a tag. // DeleteTag deletes a tag.
func (msg *IrcMessage) DeleteTag(tagName string) { func (msg *IRCMessage) DeleteTag(tagName string) {
if len(tagName) == 0 { if len(tagName) == 0 {
return return
} else if tagName[0] == '+' { } else if tagName[0] == '+' {
@ -116,14 +116,14 @@ func (msg *IrcMessage) DeleteTag(tagName string) {
} }
// UpdateTags is a convenience to set multiple tags at once. // UpdateTags is a convenience to set multiple tags at once.
func (msg *IrcMessage) UpdateTags(tags map[string]string) { func (msg *IRCMessage) UpdateTags(tags map[string]string) {
for name, value := range tags { for name, value := range tags {
msg.SetTag(name, value) msg.SetTag(name, value)
} }
} }
// AllTags returns all tags as a single map. // AllTags returns all tags as a single map.
func (msg *IrcMessage) AllTags() (result map[string]string) { func (msg *IRCMessage) AllTags() (result map[string]string) {
result = make(map[string]string, len(msg.tags)+len(msg.clientOnlyTags)) result = make(map[string]string, len(msg.tags)+len(msg.clientOnlyTags))
for name, value := range msg.tags { for name, value := range msg.tags {
result[name] = value result[name] = value
@ -135,23 +135,23 @@ func (msg *IrcMessage) AllTags() (result map[string]string) {
} }
// ClientOnlyTags returns the client-only tags (the tags with the + prefix). // ClientOnlyTags returns the client-only tags (the tags with the + prefix).
// The returned map may be internal storage of the IrcMessage object and // The returned map may be internal storage of the IRCMessage object and
// should not be modified. // should not be modified.
func (msg *IrcMessage) ClientOnlyTags() map[string]string { func (msg *IRCMessage) ClientOnlyTags() map[string]string {
return msg.clientOnlyTags return msg.clientOnlyTags
} }
// ParseLine creates and returns a message from the given IRC line. // ParseLine creates and returns a message from the given IRC line.
func ParseLine(line string) (ircmsg IrcMessage, err error) { func ParseLine(line string) (ircmsg IRCMessage, err error) {
return parseLine(line, 0, 0) return parseLine(line, 0, 0)
} }
// ParseLineStrict creates and returns an IrcMessage from the given IRC line, // ParseLineStrict creates and returns an IRCMessage from the given IRC line,
// taking the maximum length into account and truncating the message as appropriate. // 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), // 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 // 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. // 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) { func ParseLineStrict(line string, fromClient bool, truncateLen int) (ircmsg IRCMessage, err error) {
maxTagDataLength := MaxlenTagData maxTagDataLength := MaxlenTagData
if fromClient { if fromClient {
maxTagDataLength = MaxlenClientTagData maxTagDataLength = MaxlenClientTagData
@ -167,7 +167,7 @@ func trimInitialSpaces(str string) string {
return str[i:] return str[i:]
} }
func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMessage, err error) { func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IRCMessage, err error) {
if strings.IndexByte(line, '\x00') != -1 { if strings.IndexByte(line, '\x00') != -1 {
err = ErrorLineContainsBadChar err = ErrorLineContainsBadChar
return return
@ -264,7 +264,7 @@ func parseLine(line string, maxTagDataLength int, truncateLen int) (ircmsg IrcMe
} }
// helper to parse tags // helper to parse tags
func (ircmsg *IrcMessage) parseTags(tags string) (err error) { func (ircmsg *IRCMessage) parseTags(tags string) (err error) {
for 0 < len(tags) { for 0 < len(tags) {
tagEnd := strings.IndexByte(tags, ';') tagEnd := strings.IndexByte(tags, ';')
endPos := tagEnd endPos := tagEnd
@ -297,8 +297,8 @@ func (ircmsg *IrcMessage) parseTags(tags string) (err error) {
return nil return nil
} }
// MakeMessage provides a simple way to create a new IrcMessage. // MakeMessage provides a simple way to create a new IRCMessage.
func MakeMessage(tags map[string]string, prefix string, command string, params ...string) (ircmsg IrcMessage) { func MakeMessage(tags map[string]string, prefix string, command string, params ...string) (ircmsg IRCMessage) {
ircmsg.Prefix = prefix ircmsg.Prefix = prefix
ircmsg.Command = command ircmsg.Command = command
ircmsg.Params = params ircmsg.Params = params
@ -306,8 +306,8 @@ func MakeMessage(tags map[string]string, prefix string, command string, params .
return ircmsg return ircmsg
} }
// Line returns a sendable line created from an IrcMessage. // Line returns a sendable line created from an IRCMessage.
func (ircmsg *IrcMessage) Line() (result string, err error) { func (ircmsg *IRCMessage) Line() (result string, err error) {
bytes, err := ircmsg.line(0, 0, 0, 0) bytes, err := ircmsg.line(0, 0, 0, 0)
if err == nil { if err == nil {
result = string(bytes) result = string(bytes)
@ -315,17 +315,17 @@ func (ircmsg *IrcMessage) Line() (result string, err error) {
return return
} }
// LineBytes returns a sendable line created from an IrcMessage. // LineBytes returns a sendable line created from an IRCMessage.
func (ircmsg *IrcMessage) LineBytes() (result []byte, err error) { func (ircmsg *IRCMessage) LineBytes() (result []byte, err error) {
result, err = ircmsg.line(0, 0, 0, 0) result, err = ircmsg.line(0, 0, 0, 0)
return return
} }
// LineBytesStrict returns a sendable line, as a []byte, created from an IrcMessage. // LineBytesStrict returns a sendable line, as a []byte, created from an IRCMessage.
// fromClient controls whether the server-side or client-side tag length limit // 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 // is enforced. If truncateLen is nonzero, it is the length at which the
// non-tag portion of the message is truncated. // non-tag portion of the message is truncated.
func (ircmsg *IrcMessage) LineBytesStrict(fromClient bool, truncateLen int) ([]byte, error) { func (ircmsg *IRCMessage) LineBytesStrict(fromClient bool, truncateLen int) ([]byte, error) {
var tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit int var tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit int
if fromClient { if fromClient {
// enforce client max tags: // enforce client max tags:
@ -345,8 +345,8 @@ func paramRequiresTrailing(param string) bool {
return len(param) == 0 || strings.IndexByte(param, ' ') != -1 || param[0] == ':' return len(param) == 0 || strings.IndexByte(param, ' ') != -1 || param[0] == ':'
} }
// line returns a sendable line created from an IrcMessage. // line returns a sendable line created from an IRCMessage.
func (ircmsg *IrcMessage) line(tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit, truncateLen int) ([]byte, error) { func (ircmsg *IRCMessage) line(tagLimit, clientOnlyTagDataLimit, serverAddedTagDataLimit, truncateLen int) ([]byte, error) {
if len(ircmsg.Command) < 1 { if len(ircmsg.Command) < 1 {
return nil, ErrorCommandMissing return nil, ErrorCommandMissing
} }

@ -8,12 +8,12 @@ import (
type testcode struct { type testcode struct {
raw string raw string
message IrcMessage message IRCMessage
} }
type testcodewithlen struct { type testcodewithlen struct {
raw string raw string
length int length int
message IrcMessage message IRCMessage
} }
var decodelentests = []testcodewithlen{ var decodelentests = []testcodewithlen{
@ -244,7 +244,7 @@ func TestEncode(t *testing.T) {
} }
} }
var testMessages = []IrcMessage{ var testMessages = []IRCMessage{
{ {
tags: map[string]string{"time": "2019-02-27T04:38:57.489Z", "account": "dan-"}, tags: map[string]string{"time": "2019-02-27T04:38:57.489Z", "account": "dan-"},
clientOnlyTags: map[string]string{"+status": "typing"}, clientOnlyTags: map[string]string{"+status": "typing"},
@ -317,7 +317,7 @@ func TestEncodeDecode(t *testing.T) {
} }
func TestForceTrailing(t *testing.T) { func TestForceTrailing(t *testing.T) {
message := IrcMessage{ message := IRCMessage{
Prefix: "shivaram", Prefix: "shivaram",
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{"#darwin", "nice"}, Params: []string{"#darwin", "nice"},
@ -340,7 +340,7 @@ func TestForceTrailing(t *testing.T) {
} }
func TestErrorLineTooLongGeneration(t *testing.T) { func TestErrorLineTooLongGeneration(t *testing.T) {
message := IrcMessage{ message := IRCMessage{
tags: map[string]string{"draft/msgid": "SAXV5OYJUr18CNJzdWa1qQ"}, tags: map[string]string{"draft/msgid": "SAXV5OYJUr18CNJzdWa1qQ"},
Prefix: "shivaram", Prefix: "shivaram",
Command: "PRIVMSG", Command: "PRIVMSG",

@ -27,7 +27,7 @@ func init() {
// EscapeTagValue takes a value, and returns an escaped message tag value. // EscapeTagValue takes a value, and returns an escaped message tag value.
// //
// This function is automatically used when lines are created from an // This function is automatically used when lines are created from an
// IrcMessage, so you don't need to call it yourself before creating a line. // IRCMessage, so you don't need to call it yourself before creating a line.
func EscapeTagValue(inString string) string { func EscapeTagValue(inString string) string {
return valtoescape.Replace(inString) return valtoescape.Replace(inString)
} }