From ea57336e8f2addab842b292b11eec05f55b50be3 Mon Sep 17 00:00:00 2001 From: Kaleb Elwert Date: Tue, 11 Feb 2014 12:22:13 -0500 Subject: [PATCH] Replaces the Event.Message field with a function Note that this purposefully breaks backwords compatibility. Because of how Arguments are now handled, this will force users using this library to update anything using this. This is not bad, as it seems, because otherwise (just updating the Event.Arguments to include the Message) the change could cause silent, strange breakages. --- irc.go | 15 --------------- irc_callback.go | 29 +++++++++++++++-------------- irc_struct.go | 9 ++++++++- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/irc.go b/irc.go index 90d52b8..2541f0d 100644 --- a/irc.go +++ b/irc.go @@ -74,21 +74,6 @@ func (irc *Connection) readLoop() { if len(split) > 1 { event.Arguments = append(event.Arguments, split[1]) } - if len(event.Arguments) > 0 { - event.Message = event.Arguments[len(event.Arguments)-1] - } - - /*args := strings.SplitN(msg, " :", 2) - if len(args) > 1 { - event.Message = args[1] - } - - args = strings.Split(args[0], " ") - event.Code = strings.ToUpper(args[0]) - - if len(args) > 1 { - event.Arguments = args[1:len(args)] - }*/ /* XXX: len(args) == 0: args should be empty */ diff --git a/irc_callback.go b/irc_callback.go index 6560c36..3035f3a 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -33,33 +33,34 @@ func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*E } func (irc *Connection) RunCallbacks(event *Event) { - if event.Code == "PRIVMSG" && len(event.Message) > 0 && event.Message[0] == '\x01' { + msg := event.Message() + if event.Code == "PRIVMSG" && len(msg) > 0 && msg[0] == '\x01' { event.Code = "CTCP" //Unknown CTCP - if i := strings.LastIndex(event.Message, "\x01"); i > -1 { - event.Message = event.Message[1:i] + if i := strings.LastIndex(msg, "\x01"); i > -1 { + msg = msg[1:i] } - if event.Message == "VERSION" { + if msg == "VERSION" { event.Code = "CTCP_VERSION" - } else if event.Message == "TIME" { + } else if msg == "TIME" { event.Code = "CTCP_TIME" - } else if event.Message[0:4] == "PING" { + } else if msg[0:4] == "PING" { event.Code = "CTCP_PING" - } else if event.Message == "USERINFO" { + } else if msg == "USERINFO" { event.Code = "CTCP_USERINFO" - } else if event.Message == "CLIENTINFO" { + } else if msg == "CLIENTINFO" { event.Code = "CTCP_CLIENTINFO" - } else if event.Message[0:6] == "ACTION" { + } else if msg[0:6] == "ACTION" { event.Code = "CTCP_ACTION" - event.Message = event.Message[7:] - + msg = msg[7:] } + event.Arguments[len(event.Arguments)-1] = msg } if callbacks, ok := irc.events[event.Code]; ok { @@ -80,7 +81,7 @@ func (irc *Connection) setupCallbacks() { irc.events = make(map[string][]func(*Event)) //Handle ping events - irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message) }) + irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) }) //Version handler irc.AddCallback("CTCP_VERSION", func(e *Event) { @@ -118,7 +119,7 @@ func (irc *Connection) setupCallbacks() { }) irc.AddCallback("PONG", func(e *Event) { - ns, _ := strconv.ParseInt(e.Message, 10, 64) + ns, _ := strconv.ParseInt(e.Message(), 10, 64) delta := time.Duration(time.Now().UnixNano() - ns) if irc.Debug { irc.Log.Printf("Lag: %vs\n", delta) @@ -127,7 +128,7 @@ func (irc *Connection) setupCallbacks() { irc.AddCallback("NICK", func(e *Event) { if e.Nick == irc.nick { - irc.nickcurrent = e.Message + irc.nickcurrent = e.Message() } }) diff --git a/irc_struct.go b/irc_struct.go index f3c1883..fa872ba 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -45,7 +45,6 @@ type Connection struct { type Event struct { Code string - Message string Raw string Nick string // Host string //!@ @@ -53,3 +52,11 @@ type Event struct { User string // Arguments []string } + +// Convenience func to get the last arg, now that the Message field is gone +func (e *Event) Message() string { + if len(e.Arguments) == 0 { + return "" + } + return e.Arguments[len(e.Arguments)-1] +}