From ca78ca8c100fd5904f35b3ceb3997e0512dcbc69 Mon Sep 17 00:00:00 2001 From: tj Date: Wed, 29 Sep 2010 11:53:44 +0200 Subject: [PATCH 1/4] Fixing compile for new go version, again. (Sorry about that) --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4bda80c..393ab31 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -include $(GOROOT)/src/Make.${GOARCH} +include $(GOROOT)/src/Make.inc TARG=irc GOFILES=irc.go irc_struct.go irc_callback.go From e554b8b4a0d8a0d016be4c1ce88527852f81c547 Mon Sep 17 00:00:00 2001 From: tj Date: Sat, 9 Oct 2010 15:09:59 +0200 Subject: [PATCH 2/4] More than one callback in example --- example/test.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/example/test.go b/example/test.go index 7022235..b146d05 100644 --- a/example/test.go +++ b/example/test.go @@ -1,9 +1,10 @@ package main import ( - irc "github.com/thoj/Go-IRC-Client-Library" +// irc "github.com/thoj/Go-IRC-Client-Library" "fmt" "os" + "irc" ) func main() { @@ -14,6 +15,17 @@ func main() { fmt.Printf("%#v\n", irccon) os.Exit(1) } - irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo1") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo2") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo3") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo4") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo5") }) + irccon.AddCallback("001", func(e *irc.IRCEvent) { irccon.Join("#testgo6") }) + irccon.ReplaceCallback("001", 0, func(e *irc.IRCEvent) { irccon.Join("#testgo01") }) + irccon.ReplaceCallback("001", 1, func(e *irc.IRCEvent) { irccon.Join("#testgo02") }) + irccon.ReplaceCallback("001", 2, func(e *irc.IRCEvent) { irccon.Join("#testgo03") }) + irccon.ReplaceCallback("001", 3, func(e *irc.IRCEvent) { irccon.Join("#testgo04") }) + irccon.ReplaceCallback("001", 4, func(e *irc.IRCEvent) { irccon.Join("#testgo05") }) + irccon.ReplaceCallback("001", 6, func(e *irc.IRCEvent) { irccon.Join("#testgo06") }) irccon.Loop(); } From abc18957ffa54a0d3a8e7a0ef088d5e985eee220 Mon Sep 17 00:00:00 2001 From: tj Date: Sat, 9 Oct 2010 15:10:37 +0200 Subject: [PATCH 3/4] More than one callback in example --- example/test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/test.go b/example/test.go index b146d05..9f7e81d 100644 --- a/example/test.go +++ b/example/test.go @@ -1,7 +1,7 @@ package main import ( -// irc "github.com/thoj/Go-IRC-Client-Library" + // irc "github.com/thoj/Go-IRC-Client-Library" "fmt" "os" "irc" @@ -27,5 +27,5 @@ func main() { irccon.ReplaceCallback("001", 3, func(e *irc.IRCEvent) { irccon.Join("#testgo04") }) irccon.ReplaceCallback("001", 4, func(e *irc.IRCEvent) { irccon.Join("#testgo05") }) irccon.ReplaceCallback("001", 6, func(e *irc.IRCEvent) { irccon.Join("#testgo06") }) - irccon.Loop(); + irccon.Loop() } From fd854c475a2218551b1cf35b4ea4b7179ad57b05 Mon Sep 17 00:00:00 2001 From: tj Date: Sat, 9 Oct 2010 15:11:28 +0200 Subject: [PATCH 4/4] Fix adding more then one callback for same eventcode, grow slice dynamically. --- irc_callback.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/irc_callback.go b/irc_callback.go index 10ff827..7875fc3 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -7,29 +7,44 @@ import ( "strconv" ) +func AppendCallback(slice, data []func(*IRCEvent)) []func(*IRCEvent) { + l := len(slice) + if l+len(data) > cap(slice) { + newSlice := make([]func(*IRCEvent), (l+len(data))*2) + copy(newSlice, slice) + slice = newSlice + } + slice = slice[0 : l+len(data)] + for i, c := range data { + slice[l+i] = c + } + return slice +} func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) { eventcode = strings.ToUpper(eventcode) if event, ok := irc.events[eventcode]; ok { - // TODO: Grow this dynamically - event = event[0 : len(event)+1] - event[len(event)-1] = callback + newevent := make([]func(*IRCEvent), 1) + newevent[0] = callback + irc.events[eventcode] = AppendCallback(event, newevent) } else { - event = make([]func(*IRCEvent), 1, 20) + event = make([]func(*IRCEvent), 1) event[0] = callback irc.events[eventcode] = event } } -func (irc *IRCConnection) ReplaceCallback(eventcode string, i uint8, callback func(*IRCEvent)) { +func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func(*IRCEvent)) { eventcode = strings.ToUpper(eventcode) if event, ok := irc.events[eventcode]; ok { - event[i] = callback - } else { - event = make([]func(*IRCEvent), 1, 20) - event[0] = callback - irc.events[eventcode] = event + if i < len(event) { + event[i] = callback + return + } + fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i) + return } + fmt.Printf("Event not found. Use AddCallBack\n") } func (irc *IRCConnection) RunCallbacks(event *IRCEvent) { @@ -92,7 +107,7 @@ func (irc *IRCConnection) setupCallbacks() { irc.AddCallback("433", func(e *IRCEvent) { if len(irc.nick) > 8 { - irc.nick = "_" + irc.nick; + irc.nick = "_" + irc.nick } else { irc.nick = irc.nick + "_" }