2012-11-05 22:46:47 +00:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2018-05-12 01:41:43 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"runtime"
|
2014-02-12 07:41:22 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2012-11-05 22:46:47 +00:00
|
|
|
)
|
|
|
|
|
2014-05-12 03:27:02 +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
|
2018-08-10 00:04:31 +00:00
|
|
|
// IRC/CTCP commands and error/response codes. To register a callback for all
|
|
|
|
// events pass "*" as the event code. This function returns the ID of the
|
|
|
|
// registered callback for later management.
|
2016-02-06 20:33:04 +00:00
|
|
|
func (irc *Connection) AddCallback(eventcode string, callback func(*Event)) int {
|
2012-11-05 22:46:47 +00:00
|
|
|
eventcode = strings.ToUpper(eventcode)
|
2016-02-06 20:33:04 +00:00
|
|
|
id := 0
|
2017-10-02 21:03:43 +00:00
|
|
|
|
|
|
|
irc.eventsMutex.Lock()
|
|
|
|
_, ok := irc.events[eventcode]
|
|
|
|
if !ok {
|
2016-02-06 20:33:04 +00:00
|
|
|
irc.events[eventcode] = make(map[int]func(*Event))
|
|
|
|
id = 0
|
|
|
|
} else {
|
|
|
|
id = len(irc.events[eventcode])
|
2014-02-10 22:20:18 +00:00
|
|
|
}
|
2014-02-11 23:35:13 +00:00
|
|
|
irc.events[eventcode][id] = callback
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:35:13 +00:00
|
|
|
return id
|
2014-02-10 22:20:18 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2016-02-06 20:33:04 +00:00
|
|
|
func (irc *Connection) RemoveCallback(eventcode string, i int) bool {
|
2014-02-10 22:20:18 +00:00
|
|
|
eventcode = strings.ToUpper(eventcode)
|
2012-03-22 03:50:21 +00:00
|
|
|
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Lock()
|
|
|
|
event, ok := irc.events[eventcode]
|
|
|
|
if ok {
|
2014-02-11 23:35:13 +00:00
|
|
|
if _, ok := event[i]; ok {
|
|
|
|
delete(irc.events[eventcode], i)
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:35:13 +00:00
|
|
|
return true
|
2014-02-10 22:20:18 +00:00
|
|
|
}
|
2016-07-24 23:29:47 +00:00
|
|
|
irc.Log.Printf("Event found, but no callback found at id %d\n", i)
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:35:13 +00:00
|
|
|
return false
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
2014-02-11 23:35:13 +00:00
|
|
|
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:35:13 +00:00
|
|
|
irc.Log.Println("Event not found")
|
|
|
|
return false
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 03:27:02 +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.
|
2014-02-11 23:57:08 +00:00
|
|
|
func (irc *Connection) ClearCallback(eventcode string) bool {
|
|
|
|
eventcode = strings.ToUpper(eventcode)
|
|
|
|
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Lock()
|
|
|
|
_, ok := irc.events[eventcode]
|
|
|
|
if ok {
|
2016-02-06 20:33:04 +00:00
|
|
|
irc.events[eventcode] = make(map[int]func(*Event))
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:57:08 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-11 23:57:08 +00:00
|
|
|
|
|
|
|
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.
|
2016-02-06 20:33:04 +00:00
|
|
|
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
|
|
|
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Lock()
|
|
|
|
event, ok := irc.events[eventcode]
|
|
|
|
irc.eventsMutex.Unlock()
|
|
|
|
if ok {
|
2014-02-11 23:35:13 +00:00
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2014-02-11 17:22:13 +00:00
|
|
|
msg := event.Message()
|
2015-03-31 13:14:13 +00:00
|
|
|
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
|
|
|
|
2015-07-30 16:58:23 +00:00
|
|
|
if i := strings.LastIndex(msg, "\x01"); i > 0 {
|
2014-02-11 17:22:13 +00:00
|
|
|
msg = msg[1:i]
|
2015-07-30 16:58:23 +00:00
|
|
|
} 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
|
|
|
|
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-08-05 22:27:45 +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
|
|
|
|
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-08-05 22:27: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
|
|
|
}
|
2014-08-05 22:27:45 +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
|
|
|
|
2017-10-02 21:03:43 +00:00
|
|
|
irc.eventsMutex.Lock()
|
2018-05-12 01:41:43 +00:00
|
|
|
callbacks := []func(*Event){}
|
|
|
|
eventCallbacks, ok := irc.events[event.Code]
|
2017-10-02 21:03:43 +00:00
|
|
|
if ok {
|
2018-05-12 01:41:43 +00:00
|
|
|
for _, callback := range eventCallbacks {
|
|
|
|
callbacks = append(callbacks, callback)
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 01:41:43 +00:00
|
|
|
allCallbacks, ok := irc.events["*"]
|
2017-10-02 21:03:43 +00:00
|
|
|
if ok {
|
2018-05-12 01:41:43 +00:00
|
|
|
for _, callback := range allCallbacks {
|
|
|
|
callbacks = append(callbacks, callback)
|
2014-02-10 22:20:18 +00:00
|
|
|
}
|
2018-05-12 01:41:43 +00:00
|
|
|
}
|
|
|
|
irc.eventsMutex.Unlock()
|
2014-02-10 22:20:18 +00:00
|
|
|
|
2018-05-12 01:41:43 +00:00
|
|
|
if irc.VerboseCallbackHandler {
|
|
|
|
irc.Log.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
possibleLogs := []string{}
|
|
|
|
for i, callback := range callbacks {
|
|
|
|
go func(done chan bool) {
|
2016-02-06 20:33:04 +00:00
|
|
|
callback(event)
|
2018-05-12 01:41:43 +00:00
|
|
|
done <- true
|
|
|
|
}(done)
|
|
|
|
callbackName := getFunctionName(callback)
|
|
|
|
start := time.Now()
|
|
|
|
select {
|
|
|
|
case <-event.Ctx.Done(): // context timed out!
|
|
|
|
irc.Log.Printf("TIMEOUT: %s timeout expired while executing %s, abandoning remaining callbacks", irc.CallbackTimeout, callbackName)
|
|
|
|
|
|
|
|
// If we timed out let's include context for how long each previous handler took
|
|
|
|
for _, logItem := range possibleLogs {
|
|
|
|
irc.Log.Println(logItem)
|
|
|
|
}
|
|
|
|
irc.Log.Printf("Callback %s ran for %s prior to timeout", callbackName, time.Since(start))
|
|
|
|
if len(callbacks) > i {
|
|
|
|
for _, callback := range callbacks[i+1:] {
|
|
|
|
irc.Log.Printf("Callback %s did not run", getFunctionName(callback))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point our context has expired and it's not safe to execute anything else, lets bail.
|
|
|
|
return
|
|
|
|
case <-done:
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
logMsg := fmt.Sprintf("Callback %s took %s", getFunctionName(callback), elapsed)
|
|
|
|
possibleLogs = append(possibleLogs, logMsg)
|
2014-02-10 22:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:41:43 +00:00
|
|
|
func getFunctionName(f func(*Event)) string {
|
|
|
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-02-06 20:33:04 +00:00
|
|
|
irc.events = make(map[string]map[int]func(*Event))
|
2012-11-05 22:46:47 +00:00
|
|
|
|
|
|
|
//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())
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2014-08-27 13:51:06 +00:00
|
|
|
// 437: ERR_UNAVAILRESOURCE "<nick/channel> :Nick/channel is temporarily unavailable"
|
2015-07-20 20:13:15 +00:00
|
|
|
// 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
|
|
|
|
}
|
2014-08-27 13:51:06 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2014-08-27 13:51:06 +00:00
|
|
|
// 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) {
|
2015-07-20 20:13:15 +00:00
|
|
|
// If irc.nickcurrent hasn't been set yet, set to irc.nick
|
|
|
|
if irc.nickcurrent == "" {
|
|
|
|
irc.nickcurrent = irc.nick
|
|
|
|
}
|
2014-08-27 13:51:06 +00:00
|
|
|
|
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) {
|
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 {
|
2016-11-05 17:53:35 +00:00
|
|
|
irc.Log.Printf("Lag: %.3f s\n", delta.Seconds())
|
2013-09-26 03:37:11 +00:00
|
|
|
}
|
2012-11-05 22:46:47 +00:00
|
|
|
})
|
|
|
|
|
2014-08-27 13:51:06 +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 {
|
2014-02-11 17:22:13 +00:00
|
|
|
irc.nickcurrent = e.Message()
|
2012-11-05 22:46:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-08-27 13:51:06 +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
|
|
|
})
|
|
|
|
}
|