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.
This commit is contained in:
Kaleb Elwert 2014-02-11 12:22:13 -05:00 committed by Thomas Jager
parent e08cb2faf7
commit ea57336e8f
3 changed files with 23 additions and 30 deletions

15
irc.go
View File

@ -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 */

View File

@ -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()
}
})

View File

@ -45,7 +45,6 @@ type Connection struct {
type Event struct {
Code string
Message string
Raw string
Nick string //<nick>
Host string //<nick>!<usr>@<host>
@ -53,3 +52,11 @@ type Event struct {
User string //<usr>
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]
}