From f13d7066868a138567f23b0b14126725c10b84d3 Mon Sep 17 00:00:00 2001 From: tj Date: Fri, 6 Aug 2010 12:49:56 +0200 Subject: [PATCH 1/8] Correct splitting of event codes --- irc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/irc.go b/irc.go index 88ab7d7..981598e 100644 --- a/irc.go +++ b/irc.go @@ -46,7 +46,7 @@ func reader(irc *IRCConnection) { if len(args) > 1 { event.Message = args[1] } - args = strings.Split(args[0], " ", 0) + args = strings.Split(args[0], " ", -1) event.Code = strings.ToUpper(args[0]) if len(args) > 1 { event.Arguments = args[1:len(args)] From 2235f8b8ed5481c3957329cddd5f5a39102b0b90 Mon Sep 17 00:00:00 2001 From: tj Date: Fri, 6 Aug 2010 23:25:03 +0200 Subject: [PATCH 2/8] Fix edge case crash --- example/test.go | 2 +- irc.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/example/test.go b/example/test.go index 4759dbc..7022235 100644 --- a/example/test.go +++ b/example/test.go @@ -1,7 +1,7 @@ package main import ( - "irc" + irc "github.com/thoj/Go-IRC-Client-Library" "fmt" "os" ) diff --git a/irc.go b/irc.go index 981598e..1fb44da 100644 --- a/irc.go +++ b/irc.go @@ -58,6 +58,9 @@ func reader(irc *IRCConnection) { func writer(irc *IRCConnection) { for { b := []byte(<-irc.pwrite) + if b == nil { + return + } _, err := irc.socket.Write(b) if err != nil { fmt.Printf("%s\n", err) From bcec3b6f112e7ebb99ad21a6e46307f787b555a4 Mon Sep 17 00:00:00 2001 From: tj Date: Sun, 8 Aug 2010 23:10:54 +0200 Subject: [PATCH 3/8] Prefix nick instead of suffix if the nick is in use and over 8 characters long --- irc_callback.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/irc_callback.go b/irc_callback.go index d8d7d00..f3d6f98 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -91,7 +91,11 @@ func (irc *IRCConnection) setupCallbacks() { }) irc.AddCallback("433", func(e *IRCEvent) { - irc.nick = irc.nick + "_" + if len(irc.nick) > 8 { + irc.nick = "_" + irc.nick; + } else { + irc.nick = irc.nick + "_" + } irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick)) }) From 1165a7fbf2be2590d9376da1c83022a977ebcf5b Mon Sep 17 00:00:00 2001 From: tj Date: Thu, 23 Sep 2010 18:54:34 +0200 Subject: [PATCH 4/8] Fix compile + bit more robust reconnect code --- Makefile | 2 +- irc.go | 21 ++++++++++++++++----- irc_callback.go | 2 +- irc_struct.go | 23 ++++++++++++----------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index d266c78..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 diff --git a/irc.go b/irc.go index 88ab7d7..14517db 100644 --- a/irc.go +++ b/irc.go @@ -17,14 +17,16 @@ const ( VERSION = "GolangBOT v1.0" ) +var error bool + func reader(irc *IRCConnection) { br := bufio.NewReader(irc.socket) - for { + for !error { msg, err := br.ReadString('\n') if err != nil { irc.Error <- err - return + break } irc.lastMessage = time.Seconds() msg = msg[0 : len(msg)-2] //Remove \r\n @@ -53,18 +55,20 @@ func reader(irc *IRCConnection) { } irc.RunCallbacks(event) } + irc.syncreader <- true } func writer(irc *IRCConnection) { - for { + for !error { b := []byte(<-irc.pwrite) _, err := irc.socket.Write(b) if err != nil { fmt.Printf("%s\n", err) irc.Error <- err - return + break } } + irc.syncwriter <- true } //Pings the server if we have not recived any messages for 5 minutes @@ -101,6 +105,8 @@ func (irc *IRCConnection) SendRaw(message string) { } func (i *IRCConnection) Reconnect() os.Error { + <-i.syncreader + <-i.syncwriter for { fmt.Printf("Reconnecting to %s\n", i.server) var err os.Error @@ -110,6 +116,7 @@ func (i *IRCConnection) Reconnect() os.Error { } fmt.Printf("Error: %s\n", err) } + error = false fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) go reader(i) go writer(i) @@ -120,7 +127,9 @@ func (i *IRCConnection) Reconnect() os.Error { func (i *IRCConnection) Loop() { for { - <-i.Error + e := <-i.Error + fmt.Printf("Error: %s\n", e) + error = true i.Reconnect() } } @@ -137,6 +146,8 @@ func (i *IRCConnection) Connect(server string) os.Error { i.pread = make(chan string, 100) i.pwrite = make(chan string, 100) i.Error = make(chan os.Error, 10) + i.syncreader = make(chan bool) + i.syncwriter = make(chan bool) go reader(i) go writer(i) go pinger(i) diff --git a/irc_callback.go b/irc_callback.go index d8d7d00..83dcbd5 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -79,7 +79,7 @@ func (irc *IRCConnection) setupCallbacks() { }) irc.AddCallback("CTCP_TIME", func(e *IRCEvent) { - ltime := time.LocalTime(); + ltime := time.LocalTime() irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String())) }) diff --git a/irc_struct.go b/irc_struct.go index bd99452..4ca8105 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -10,19 +10,20 @@ import ( ) type IRCConnection struct { - socket net.Conn - pread, pwrite chan string - Error chan os.Error - nick string - user string - registered bool - server string + socket net.Conn + pread, pwrite chan string + Error chan os.Error + syncreader, syncwriter chan bool + nick string + user string + registered bool + server string events map[string][]func(*IRCEvent) - - lastMessage int64; - ticker <-chan int64; - ticker2 <-chan int64; + + lastMessage int64 + ticker <-chan int64 + ticker2 <-chan int64 } type IRCEvent struct { From 998e1c3050bda0125044d0a3e251c153fd47517b Mon Sep 17 00:00:00 2001 From: tj Date: Wed, 29 Sep 2010 08:58:32 +0200 Subject: [PATCH 5/8] Add support for server PASSWORD --- irc.go | 8 ++++++-- irc_struct.go | 9 +++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/irc.go b/irc.go index 1fb44da..562d905 100644 --- a/irc.go +++ b/irc.go @@ -58,7 +58,7 @@ func reader(irc *IRCConnection) { func writer(irc *IRCConnection) { for { b := []byte(<-irc.pwrite) - if b == nil { + if b == nil || irc.socket == nil { return } _, err := irc.socket.Write(b) @@ -145,10 +145,13 @@ func (i *IRCConnection) Connect(server string) os.Error { go pinger(i) i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick) i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user) + if len(i.password) > 0 { + i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.password) + } return nil } -func IRC(nick string, user string) *IRCConnection { +func IRC(nick, user, password string) *IRCConnection { irc := new(IRCConnection) irc.registered = false irc.pread = make(chan string, 100) @@ -156,6 +159,7 @@ func IRC(nick string, user string) *IRCConnection { irc.Error = make(chan os.Error) irc.nick = nick irc.user = user + irc.password = password irc.setupCallbacks() return irc } diff --git a/irc_struct.go b/irc_struct.go index bd99452..79d35d9 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -17,12 +17,13 @@ type IRCConnection struct { user string registered bool server string + password string events map[string][]func(*IRCEvent) - - lastMessage int64; - ticker <-chan int64; - ticker2 <-chan int64; + + lastMessage int64 + ticker <-chan int64 + ticker2 <-chan int64 } type IRCEvent struct { From 95eedcedd3b625bff77ba81ed78e3ad2f16279ec Mon Sep 17 00:00:00 2001 From: tj Date: Wed, 29 Sep 2010 09:04:14 +0200 Subject: [PATCH 6/8] Fix bad merge --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 393ab31..4bda80c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -include $(GOROOT)/src/Make.inc +include $(GOROOT)/src/Make.${GOARCH} TARG=irc GOFILES=irc.go irc_struct.go irc_callback.go From 9fa6bbdecad6111a93c7b2902d147138ea0d4747 Mon Sep 17 00:00:00 2001 From: tj Date: Wed, 29 Sep 2010 09:52:43 +0200 Subject: [PATCH 7/8] Avoid changing interface for password support --- irc.go | 7 +++---- irc_struct.go | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/irc.go b/irc.go index dba5ef0..e11626c 100644 --- a/irc.go +++ b/irc.go @@ -156,13 +156,13 @@ func (i *IRCConnection) Connect(server string) os.Error { go pinger(i) i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick) i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user) - if len(i.password) > 0 { - i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.password) + if len(i.Password) > 0 { + i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.Password) } return nil } -func IRC(nick, user, password string) *IRCConnection { +func IRC(nick, user string) *IRCConnection { irc := new(IRCConnection) irc.registered = false irc.pread = make(chan string, 100) @@ -170,7 +170,6 @@ func IRC(nick, user, password string) *IRCConnection { irc.Error = make(chan os.Error) irc.nick = nick irc.user = user - irc.password = password irc.setupCallbacks() return irc } diff --git a/irc_struct.go b/irc_struct.go index 79e2e74..e0b9a07 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -18,7 +18,7 @@ type IRCConnection struct { user string registered bool server string - password string + Password string events map[string][]func(*IRCEvent) lastMessage int64 From bebd74ffc0464fa903eacbcfc9404871980e5dbc Mon Sep 17 00:00:00 2001 From: tj Date: Wed, 29 Sep 2010 09:55:31 +0200 Subject: [PATCH 8/8] Updating readme for Password support --- README.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 9ef1bee..5ccd47d 100644 --- a/README.markdown +++ b/README.markdown @@ -50,8 +50,11 @@ AddCallback Example Commands -------- + irc.IRC("", "") //Create new ircobj + ircobj.Password = "[server password]" + ircobj.Connect("irc.someserver.com:6667") //Connect to server ircobj.Sendraw("") //sends string to server. Adds \r\n ircobj.Join("#channel [password]") ircobj.Privmsg("#channel", "msg") ircobj.Privmsg("nickname", "msg") - ircobj.Notice("nickname or #channel", "msg") + ircobj.Notice("", "msg")