go-ircevent/src/irc_callback.go

137 lines
3.4 KiB
Go
Raw Normal View History

2010-01-06 18:37:07 +00:00
package irc
import (
"fmt"
"strconv"
"strings"
"time"
2010-01-06 18:37:07 +00:00
)
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
eventcode = strings.ToUpper(eventcode)
2012-03-22 03:50:21 +00:00
if _, ok := irc.events[eventcode]; ok {
irc.events[eventcode] = append(irc.events[eventcode], callback)
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
} else {
irc.events[eventcode] = make([]func(*IRCEvent), 1)
irc.events[eventcode][0] = callback
2010-03-12 20:20:10 +00:00
}
}
func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func(*IRCEvent)) {
2010-03-12 20:20:10 +00:00
eventcode = strings.ToUpper(eventcode)
2012-03-22 03:50:21 +00:00
2010-03-12 20:20:10 +00:00
if event, ok := irc.events[eventcode]; ok {
if i < len(event) {
event[i] = callback
return
}
2012-03-22 03:50:21 +00:00
fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i)
return
2010-01-06 18:37:07 +00:00
}
2012-03-22 03:50:21 +00:00
fmt.Printf("Event not found. Use AddCallBack\n")
2010-01-06 18:37:07 +00:00
}
func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
2010-11-21 20:20:18 +00:00
if event.Code == "PRIVMSG" && len(event.Message) > 0 && event.Message[0] == '\x01' {
2010-01-06 18:37:07 +00:00
event.Code = "CTCP" //Unknown CTCP
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
if i := strings.LastIndex(event.Message, "\x01"); i > -1 {
event.Message = event.Message[1:i]
}
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
if event.Message == "VERSION" {
event.Code = "CTCP_VERSION"
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
} else if event.Message == "TIME" {
event.Code = "CTCP_TIME"
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
} else if event.Message[0:4] == "PING" {
event.Code = "CTCP_PING"
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
} else if event.Message == "USERINFO" {
event.Code = "CTCP_USERINFO"
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
} else if event.Message == "CLIENTINFO" {
event.Code = "CTCP_CLIENTINFO"
}
}
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
if callbacks, ok := irc.events[event.Code]; ok {
if irc.VerboseCallbackHandler {
fmt.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
}
2012-03-22 03:50:21 +00:00
2010-01-06 18:37:07 +00:00
for _, callback := range callbacks {
go callback(event)
}
2012-03-22 03:50:21 +00:00
} else if irc.VerboseCallbackHandler {
fmt.Printf("%v (0) >> %#v\n", event.Code, event)
2010-01-06 18:37:07 +00:00
}
}
func (irc *IRCConnection) setupCallbacks() {
irc.events = make(map[string][]func(*IRCEvent))
//Handle ping events
irc.AddCallback("PING", func(e *IRCEvent) { irc.SendRaw("PONG :" + e.Message) })
//Version handler
irc.AddCallback("CTCP_VERSION", func(e *IRCEvent) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01VERSION %s\x01", e.Nick, VERSION))
})
irc.AddCallback("CTCP_USERINFO", func(e *IRCEvent) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01USERINFO %s\x01", e.Nick, irc.user))
})
irc.AddCallback("CTCP_CLIENTINFO", func(e *IRCEvent) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01CLIENTINFO PING VERSION TIME USERINFO CLIENTINFO\x01", e.Nick))
})
irc.AddCallback("CTCP_TIME", func(e *IRCEvent) {
ltime := time.Now()
2010-03-12 20:43:50 +00:00
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
2010-01-06 18:37:07 +00:00
})
2012-03-22 03:50:21 +00:00
irc.AddCallback("CTCP_PING", func(e *IRCEvent) {
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01%s\x01", e.Nick, e.Message))
})
2010-01-06 18:37:07 +00:00
irc.AddCallback("437", func(e *IRCEvent) {
2011-02-03 00:16:13 +00:00
irc.nickcurrent = irc.nickcurrent + "_"
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
2010-01-06 18:37:07 +00:00
})
2010-03-12 20:33:20 +00:00
irc.AddCallback("433", func(e *IRCEvent) {
2011-02-03 00:16:13 +00:00
if len(irc.nickcurrent) > 8 {
irc.nickcurrent = "_" + irc.nickcurrent
2012-03-22 03:50:21 +00:00
} else {
2011-02-03 00:16:13 +00:00
irc.nickcurrent = irc.nickcurrent + "_"
}
2012-03-22 03:50:21 +00:00
2011-02-03 00:16:13 +00:00
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nickcurrent))
2010-03-12 20:33:20 +00:00
})
2010-01-06 18:37:07 +00:00
irc.AddCallback("PONG", func(e *IRCEvent) {
ns, _ := strconv.ParseInt(e.Message, 10, 64)
delta := time.Duration(time.Now().UnixNano() - ns)
fmt.Printf("Lag: %vs\n", delta)
2011-02-03 00:16:13 +00:00
})
irc.AddCallback("NICK", func(e *IRCEvent) {
if e.Nick == irc.nick {
irc.nickcurrent = e.Arguments[0]
}
2010-01-06 18:37:07 +00:00
})
irc.AddCallback("001", func(e *IRCEvent) {
irc.nickcurrent = e.Arguments[0]
})
2010-01-06 18:37:07 +00:00
}