2012-11-05 22:46:47 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) {
|
|
|
|
eventcode = strings.ToUpper(eventcode)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
if _, ok := irc.events[eventcode]; ok {
|
|
|
|
irc.events[eventcode] = append(irc.events[eventcode], callback)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
} else {
|
|
|
|
irc.events[eventcode] = make([]func(*Event), 1)
|
|
|
|
irc.events[eventcode][0] = callback
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event)) {
|
|
|
|
eventcode = strings.ToUpper(eventcode)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
if event, ok := irc.events[eventcode]; ok {
|
|
|
|
if i < len(event) {
|
|
|
|
event[i] = callback
|
|
|
|
return
|
|
|
|
}
|
2014-02-09 13:42:14 +00:00
|
|
|
irc.Log.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i)
|
2012-11-05 22:46:47 +00:00
|
|
|
return
|
|
|
|
}
|
2014-02-09 13:42:14 +00:00
|
|
|
irc.Log.Printf("Event not found. Use AddCallBack\n")
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) RunCallbacks(event *Event) {
|
2014-02-11 17:22:13 +00:00
|
|
|
msg := event.Message()
|
|
|
|
if event.Code == "PRIVMSG" && len(msg) > 0 && msg[0] == '\x01' {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP" //Unknown CTCP
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
if i := strings.LastIndex(msg, "\x01"); i > -1 {
|
|
|
|
msg = msg[1:i]
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
if msg == "VERSION" {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP_VERSION"
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
} else if msg == "TIME" {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP_TIME"
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
} else if msg[0:4] == "PING" {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP_PING"
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
} else if msg == "USERINFO" {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP_USERINFO"
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
} else if msg == "CLIENTINFO" {
|
2012-11-05 22:46:47 +00:00
|
|
|
event.Code = "CTCP_CLIENTINFO"
|
2014-02-02 05:38:45 +00:00
|
|
|
|
2014-02-11 17:22:13 +00:00
|
|
|
} else if msg[0:6] == "ACTION" {
|
2014-02-02 05:38:45 +00:00
|
|
|
event.Code = "CTCP_ACTION"
|
2014-02-11 17:22:13 +00:00
|
|
|
msg = msg[7:]
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2014-02-11 17:22:13 +00:00
|
|
|
event.Arguments[len(event.Arguments)-1] = msg
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
if callbacks, ok := irc.events[event.Code]; ok {
|
|
|
|
if irc.VerboseCallbackHandler {
|
2014-02-09 13:42:14 +00:00
|
|
|
irc.Log.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
for _, callback := range callbacks {
|
|
|
|
go callback(event)
|
|
|
|
}
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
} else if irc.VerboseCallbackHandler {
|
2014-02-09 13:42:14 +00:00
|
|
|
irc.Log.Printf("%v (0) >> %#v\n", event.Code, event)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irc *Connection) setupCallbacks() {
|
|
|
|
irc.events = make(map[string][]func(*Event))
|
|
|
|
|
|
|
|
//Handle ping events
|
2014-02-11 17:22:13 +00:00
|
|
|
irc.AddCallback("PING", func(e *Event) { irc.SendRaw("PONG :" + e.Message()) })
|
2012-11-05 22:46:47 +00:00
|
|
|
|
|
|
|
//Version handler
|
|
|
|
irc.AddCallback("CTCP_VERSION", func(e *Event) {
|
2014-02-09 10:20:58 +00:00
|
|
|
irc.SendRawf("NOTICE %s :\x01VERSION %s\x01", e.Nick, irc.Version)
|
2012-11-05 22:46:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("CTCP_USERINFO", func(e *Event) {
|
|
|
|
irc.SendRawf("NOTICE %s :\x01USERINFO %s\x01", e.Nick, irc.user)
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("CTCP_CLIENTINFO", func(e *Event) {
|
|
|
|
irc.SendRawf("NOTICE %s :\x01CLIENTINFO PING VERSION TIME USERINFO CLIENTINFO\x01", e.Nick)
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("CTCP_TIME", func(e *Event) {
|
|
|
|
ltime := time.Now()
|
|
|
|
irc.SendRawf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("CTCP_PING", func(e *Event) { irc.SendRawf("NOTICE %s :\x01%s\x01", e.Nick, e.Message) })
|
|
|
|
|
|
|
|
irc.AddCallback("437", func(e *Event) {
|
|
|
|
irc.nickcurrent = irc.nickcurrent + "_"
|
|
|
|
irc.SendRawf("NICK %s", irc.nickcurrent)
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("433", func(e *Event) {
|
|
|
|
if len(irc.nickcurrent) > 8 {
|
|
|
|
irc.nickcurrent = "_" + irc.nickcurrent
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2012-11-05 22:46:47 +00:00
|
|
|
} else {
|
|
|
|
irc.nickcurrent = irc.nickcurrent + "_"
|
|
|
|
}
|
|
|
|
irc.SendRawf("NICK %s", irc.nickcurrent)
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("PONG", func(e *Event) {
|
2014-02-11 17:22:13 +00:00
|
|
|
ns, _ := strconv.ParseInt(e.Message(), 10, 64)
|
2012-11-05 22:46:47 +00:00
|
|
|
delta := time.Duration(time.Now().UnixNano() - ns)
|
2013-09-26 03:37:11 +00:00
|
|
|
if irc.Debug {
|
2014-02-09 13:42:14 +00:00
|
|
|
irc.Log.Printf("Lag: %vs\n", delta)
|
2013-09-26 03:37:11 +00:00
|
|
|
}
|
2012-11-05 22:46:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("NICK", func(e *Event) {
|
|
|
|
if e.Nick == irc.nick {
|
2014-02-11 17:22:13 +00:00
|
|
|
irc.nickcurrent = e.Message()
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
irc.AddCallback("001", func(e *Event) {
|
|
|
|
irc.nickcurrent = e.Arguments[0]
|
|
|
|
})
|
|
|
|
}
|