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:
committad av
Thomas Jager
förälder
e08cb2faf7
incheckning
ea57336e8f
@@ -74,21 +74,6 @@ func (irc *Connection) readLoop() {
|
|||||||
if len(split) > 1 {
|
if len(split) > 1 {
|
||||||
event.Arguments = append(event.Arguments, 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 */
|
/* XXX: len(args) == 0: args should be empty */
|
||||||
|
|
||||||
|
|||||||
+15
-14
@@ -33,33 +33,34 @@ func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*E
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (irc *Connection) RunCallbacks(event *Event) {
|
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
|
event.Code = "CTCP" //Unknown CTCP
|
||||||
|
|
||||||
if i := strings.LastIndex(event.Message, "\x01"); i > -1 {
|
if i := strings.LastIndex(msg, "\x01"); i > -1 {
|
||||||
event.Message = event.Message[1:i]
|
msg = msg[1:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
if event.Message == "VERSION" {
|
if msg == "VERSION" {
|
||||||
event.Code = "CTCP_VERSION"
|
event.Code = "CTCP_VERSION"
|
||||||
|
|
||||||
} else if event.Message == "TIME" {
|
} else if msg == "TIME" {
|
||||||
event.Code = "CTCP_TIME"
|
event.Code = "CTCP_TIME"
|
||||||
|
|
||||||
} else if event.Message[0:4] == "PING" {
|
} else if msg[0:4] == "PING" {
|
||||||
event.Code = "CTCP_PING"
|
event.Code = "CTCP_PING"
|
||||||
|
|
||||||
} else if event.Message == "USERINFO" {
|
} else if msg == "USERINFO" {
|
||||||
event.Code = "CTCP_USERINFO"
|
event.Code = "CTCP_USERINFO"
|
||||||
|
|
||||||
} else if event.Message == "CLIENTINFO" {
|
} else if msg == "CLIENTINFO" {
|
||||||
event.Code = "CTCP_CLIENTINFO"
|
event.Code = "CTCP_CLIENTINFO"
|
||||||
|
|
||||||
} else if event.Message[0:6] == "ACTION" {
|
} else if msg[0:6] == "ACTION" {
|
||||||
event.Code = "CTCP_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 {
|
if callbacks, ok := irc.events[event.Code]; ok {
|
||||||
@@ -80,7 +81,7 @@ func (irc *Connection) setupCallbacks() {
|
|||||||
irc.events = make(map[string][]func(*Event))
|
irc.events = make(map[string][]func(*Event))
|
||||||
|
|
||||||
//Handle ping events
|
//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
|
//Version handler
|
||||||
irc.AddCallback("CTCP_VERSION", func(e *Event) {
|
irc.AddCallback("CTCP_VERSION", func(e *Event) {
|
||||||
@@ -118,7 +119,7 @@ func (irc *Connection) setupCallbacks() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
irc.AddCallback("PONG", func(e *Event) {
|
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)
|
delta := time.Duration(time.Now().UnixNano() - ns)
|
||||||
if irc.Debug {
|
if irc.Debug {
|
||||||
irc.Log.Printf("Lag: %vs\n", delta)
|
irc.Log.Printf("Lag: %vs\n", delta)
|
||||||
@@ -127,7 +128,7 @@ func (irc *Connection) setupCallbacks() {
|
|||||||
|
|
||||||
irc.AddCallback("NICK", func(e *Event) {
|
irc.AddCallback("NICK", func(e *Event) {
|
||||||
if e.Nick == irc.nick {
|
if e.Nick == irc.nick {
|
||||||
irc.nickcurrent = e.Message
|
irc.nickcurrent = e.Message()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -45,7 +45,6 @@ type Connection struct {
|
|||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Code string
|
Code string
|
||||||
Message string
|
|
||||||
Raw string
|
Raw string
|
||||||
Nick string //<nick>
|
Nick string //<nick>
|
||||||
Host string //<nick>!<usr>@<host>
|
Host string //<nick>!<usr>@<host>
|
||||||
@@ -53,3 +52,11 @@ type Event struct {
|
|||||||
User string //<usr>
|
User string //<usr>
|
||||||
Arguments []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]
|
||||||
|
}
|
||||||
|
|||||||
Referens i nytt ärende
Block a user