go-ircevent/irc_callback.go

245 lines
6.5 KiB
Go
Raw Normal View History

2012-11-05 22:46:47 +00:00
package irc
import (
2014-02-12 07:41:22 +00:00
"strconv"
"strings"
"time"
2012-11-05 22:46:47 +00:00
)
// Register a callback to a connection and event code. A callback is a function
2014-02-14 15:41:58 +00:00
// which takes only an Event pointer as parameter. Valid event codes are all
// IRC/CTCP commands and error/response codes. This function returns the ID of
2014-02-14 15:41:58 +00:00
// the registered callback for later management.
func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) int {
2012-11-05 22:46:47 +00:00
eventcode = strings.ToUpper(eventcode)
id := 0
irc.eventsMutex.Lock()
_, ok := irc.events[eventcode]
if !ok {
irc.events[eventcode] = make(map[int]func(*Event))
id = 0
} else {
id = len(irc.events[eventcode])
}
irc.events[eventcode][id] = callback
irc.eventsMutex.Unlock()
return id
}
2014-02-14 15:30:09 +00:00
// Remove callback i (ID) from the given event code. This functions returns
// true upon success, false if any error occurs.
func (irc *Connection) RemoveCallback(eventcode string, i int) bool {
eventcode = strings.ToUpper(eventcode)
2012-03-22 03:50:21 +00:00
irc.eventsMutex.Lock()
event, ok := irc.events[eventcode]
if ok {
if _, ok := event[i]; ok {
delete(irc.events[eventcode], i)
irc.eventsMutex.Unlock()
return true
}
2016-07-24 23:29:47 +00:00
irc.Log.Printf("Event found, but no callback found at id %d\n", i)
irc.eventsMutex.Unlock()
return false
2012-11-05 22:46:47 +00:00
}
irc.eventsMutex.Unlock()
irc.Log.Println("Event not found")
return false
2012-11-05 22:46:47 +00:00
}
// Remove all callbacks from a given event code. It returns true
2014-02-14 15:12:16 +00:00
// if given event code is found and cleared.
func (irc *Connection) ClearCallback(eventcode string) bool {
eventcode = strings.ToUpper(eventcode)
irc.eventsMutex.Lock()
_, ok := irc.events[eventcode]
if ok {
irc.events[eventcode] = make(map[int]func(*Event))
irc.eventsMutex.Unlock()
return true
}
irc.eventsMutex.Unlock()
irc.Log.Println("Event not found")
return false
}
2014-02-14 15:30:09 +00:00
// Replace callback i (ID) associated with a given event code with a new callback function.
func (irc *Connection) ReplaceCallback(eventcode string, i int, callback func(*Event)) {
2012-11-05 22:46:47 +00:00
eventcode = strings.ToUpper(eventcode)
2012-03-22 03:50:21 +00:00
irc.eventsMutex.Lock()
event, ok := irc.events[eventcode]
irc.eventsMutex.Unlock()
if ok {
if _, ok := event[i]; ok {
2012-11-05 22:46:47 +00:00
event[i] = callback
return
}
2016-07-24 23:29:47 +00:00
irc.Log.Printf("Event found, but no callback found at id %d\n", i)
2012-11-05 22:46:47 +00:00
}
irc.Log.Printf("Event not found. Use AddCallBack\n")
2012-11-05 22:46:47 +00:00
}
2014-02-14 15:12:16 +00:00
// Execute all callbacks associated with a given event.
2012-11-05 22:46:47 +00:00
func (irc *Connection) RunCallbacks(event *Event) {
msg := event.Message()
if event.Code == "PRIVMSG" && len(msg) > 2 && msg[0] == '\x01' {
2012-11-05 22:46:47 +00:00
event.Code = "CTCP" //Unknown CTCP
2012-03-22 03:50:21 +00:00
if i := strings.LastIndex(msg, "\x01"); i > 0 {
msg = msg[1:i]
} else {
irc.Log.Printf("Invalid CTCP Message: %s\n", strconv.Quote(msg))
2015-07-30 17:03:55 +00:00
return
2012-11-05 22:46:47 +00:00
}
2012-03-22 03:50:21 +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
} else if msg == "TIME" {
2012-11-05 22:46:47 +00:00
event.Code = "CTCP_TIME"
2012-03-22 03:50:21 +00:00
} else if strings.HasPrefix(msg, "PING") {
2012-11-05 22:46:47 +00:00
event.Code = "CTCP_PING"
2012-03-22 03:50:21 +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
} else if msg == "CLIENTINFO" {
2012-11-05 22:46:47 +00:00
event.Code = "CTCP_CLIENTINFO"
2014-02-02 05:38:45 +00:00
} else if strings.HasPrefix(msg, "ACTION") {
2014-02-02 05:38:45 +00:00
event.Code = "CTCP_ACTION"
2015-07-31 11:03:09 +00:00
if len(msg) > 6 {
msg = msg[7:]
} else {
msg = ""
}
2012-11-05 22:46:47 +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
irc.eventsMutex.Lock()
callbacks, ok := irc.events[event.Code]
irc.eventsMutex.Unlock()
if ok {
2012-11-05 22:46:47 +00:00
if irc.VerboseCallbackHandler {
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 {
callback(event)
2012-11-05 22:46:47 +00:00
}
} else if irc.VerboseCallbackHandler {
irc.Log.Printf("%v (0) >> %#v\n", event.Code, event)
2012-11-05 22:46:47 +00:00
}
irc.eventsMutex.Lock()
allcallbacks, ok := irc.events["*"]
irc.eventsMutex.Unlock()
if ok {
if irc.VerboseCallbackHandler {
irc.Log.Printf("%v (0) >> %#v\n", event.Code, event)
}
for _, callback := range allcallbacks {
callback(event)
}
}
2012-11-05 22:46:47 +00:00
}
2014-02-14 15:30:09 +00:00
// Set up some initial callbacks to handle the IRC/CTCP protocol.
2012-11-05 22:46:47 +00:00
func (irc *Connection) setupCallbacks() {
irc.events = make(map[string]map[int]func(*Event))
2012-11-05 22:46:47 +00:00
//Handle error events.
irc.AddCallback("ERROR", func(e *Event) { irc.Disconnect() })
2014-06-18 13:58:18 +00:00
2012-11-05 22:46:47 +00:00
//Handle ping events
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) {
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())
})
2014-06-18 13:58:18 +00:00
irc.AddCallback("CTCP_PING", func(e *Event) { irc.SendRawf("NOTICE %s :\x01%s\x01", e.Nick, e.Message()) })
2012-11-05 22:46:47 +00:00
// 437: ERR_UNAVAILRESOURCE "<nick/channel> :Nick/channel is temporarily unavailable"
// Add a _ to current nick. If irc.nickcurrent is empty this cannot
// work. It has to be set somewhere first in case the nick is already
// taken or unavailable from the beginning.
irc.AddCallback("437", func(e *Event) {
// If irc.nickcurrent hasn't been set yet, set to irc.nick
if irc.nickcurrent == "" {
irc.nickcurrent = irc.nick
}
if len(irc.nickcurrent) > 8 {
irc.nickcurrent = "_" + irc.nickcurrent
} else {
irc.nickcurrent = irc.nickcurrent + "_"
}
2012-11-05 22:46:47 +00:00
irc.SendRawf("NICK %s", irc.nickcurrent)
})
// 433: ERR_NICKNAMEINUSE "<nick> :Nickname is already in use"
// Add a _ to current nick.
2012-11-05 22:46:47 +00:00
irc.AddCallback("433", func(e *Event) {
// If irc.nickcurrent hasn't been set yet, set to irc.nick
if irc.nickcurrent == "" {
irc.nickcurrent = irc.nick
}
2012-11-05 22:46:47 +00:00
if len(irc.nickcurrent) > 8 {
irc.nickcurrent = "_" + irc.nickcurrent
} else {
irc.nickcurrent = irc.nickcurrent + "_"
}
irc.SendRawf("NICK %s", irc.nickcurrent)
})
irc.AddCallback("PONG", func(e *Event) {
ns, _ := strconv.ParseInt(e.Message(), 10, 64)
2012-11-05 22:46:47 +00:00
delta := time.Duration(time.Now().UnixNano() - ns)
if irc.Debug {
2016-11-05 17:53:35 +00:00
irc.Log.Printf("Lag: %.3f s\n", delta.Seconds())
}
2012-11-05 22:46:47 +00:00
})
// NICK Define a nickname.
// Set irc.nickcurrent to the new nick actually used in this connection.
2012-11-05 22:46:47 +00:00
irc.AddCallback("NICK", func(e *Event) {
if e.Nick == irc.nick {
irc.nickcurrent = e.Message()
2012-11-05 22:46:47 +00:00
}
})
// 1: RPL_WELCOME "Welcome to the Internet Relay Network <nick>!<user>@<host>"
// Set irc.nickcurrent to the actually used nick in this connection.
2012-11-05 22:46:47 +00:00
irc.AddCallback("001", func(e *Event) {
2016-11-05 17:53:35 +00:00
irc.Lock()
2012-11-05 22:46:47 +00:00
irc.nickcurrent = e.Arguments[0]
2016-11-05 17:53:35 +00:00
irc.Unlock()
2012-11-05 22:46:47 +00:00
})
}