From 31036ff926bed33b3c22a58007f11d9f0bff0d14 Mon Sep 17 00:00:00 2001 From: lye Date: Wed, 21 Mar 2012 22:57:35 -0500 Subject: [PATCH] Remove calls to fmt.Printf; send to IRCConn.Log chan if it is non-nil --- src/irc.go | 63 +++++++++++++++++++++++++++------------------ src/irc_callback.go | 10 +++---- src/irc_struct.go | 41 +++++++++++++++-------------- 3 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/irc.go b/src/irc.go index ea438c7..ad3e9d4 100644 --- a/src/irc.go +++ b/src/irc.go @@ -25,7 +25,7 @@ func reader(irc *IRCConnection) { for !error_ { msg, err := br.ReadString('\n') if err != nil { - irc.Error <- err + irc.Error <-err break } @@ -38,7 +38,7 @@ func reader(irc *IRCConnection) { event.Source = msg[1:i] msg = msg[i+1 : len(msg)] } else { - fmt.Printf("Misformed msg from server: %#s\n", msg) + irc.log(fmt.Sprintf("Misformed msg from server: %#s\n", msg)) } if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 { event.Nick = event.Source[0:i] @@ -61,7 +61,7 @@ func reader(irc *IRCConnection) { irc.RunCallbacks(event) } - irc.syncreader <- true + irc.syncreader <-true } func writer(irc *IRCConnection) { @@ -74,14 +74,13 @@ func writer(irc *IRCConnection) { _, err := irc.socket.Write([]byte(b)) if err != nil { - fmt.Printf("%s\n", err) - irc.Error <- err + irc.Error <-err break } b, ok = <-irc.pwrite } - irc.syncwriter <- true + irc.syncwriter <-true } //Pings the server if we have not recived any messages for 5 minutes @@ -120,24 +119,24 @@ func (irc *IRCConnection) Quit() { } func (irc *IRCConnection) Join(channel string) { - irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel) + irc.pwrite <-fmt.Sprintf("JOIN %s\r\n", channel) } func (irc *IRCConnection) Part(channel string) { - irc.pwrite <- fmt.Sprintf("PART %s\r\n", channel) + irc.pwrite <-fmt.Sprintf("PART %s\r\n", channel) } func (irc *IRCConnection) Notice(target, message string) { - irc.pwrite <- fmt.Sprintf("NOTICE %s :%s\r\n", target, message) + irc.pwrite <-fmt.Sprintf("NOTICE %s :%s\r\n", target, message) } func (irc *IRCConnection) Privmsg(target, message string) { - irc.pwrite <- fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message) + irc.pwrite <-fmt.Sprintf("PRIVMSG %s :%s\r\n", target, message) } func (irc *IRCConnection) SendRaw(message string) { - fmt.Printf("--> %s\n", message) - irc.pwrite <- fmt.Sprintf("%s\r\n", message) + irc.log(fmt.Sprintf("--> %s\n", message)) + irc.pwrite <-fmt.Sprintf("%s\r\n", message) } func (i *IRCConnection) Reconnect() error { @@ -148,24 +147,26 @@ func (i *IRCConnection) Reconnect() error { <-i.syncwriter for { - fmt.Printf("Reconnecting to %s\n", i.server) + i.log(fmt.Sprintf("Reconnecting to %s\n", i.server)) + var err error i.socket, err = net.Dial("tcp", i.server) if err == nil { break } - fmt.Printf("Error: %s\n", err) + + i.log(fmt.Sprintf("Error: %s\n", err)) } error_ = false - fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) + i.log(fmt.Sprintf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())) go reader(i) go writer(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) + 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) return nil } @@ -178,7 +179,7 @@ func (i *IRCConnection) Loop() { break } - fmt.Printf("Error: %s\n", e) + i.log(fmt.Sprintf("Error: %s\n", e)) error_ = true i.Reconnect() } @@ -202,38 +203,50 @@ func (i *IRCConnection) postConnect() error { go pinger(i) if len(i.Password) > 0 { - i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.Password) + i.pwrite <-fmt.Sprintf("PASS %s\r\n", i.Password) } - 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) + 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) return nil } func (i *IRCConnection) Connect(server string) error { i.server = server - fmt.Printf("Connecting to %s\n", i.server) + i.log(fmt.Sprintf("Connecting to %s\n", i.server)) + var err error i.socket, err = net.Dial("tcp", i.server) if err != nil { return err } - fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) + + i.log(fmt.Sprintf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())) return i.postConnect() } func (i *IRCConnection) ConnectSSL(server string) error { i.server = server - fmt.Printf("Connecting to %s over SSL\n", i.server) + i.log(fmt.Sprintf("Connecting to %s over SSL\n", i.server)) + var err error i.socket, err = tls.Dial("tcp", i.server, i.SSLConfig) + if err != nil { return err } - fmt.Printf("Connected to %s (%s) over SSL\n", i.server, i.socket.RemoteAddr()) + + i.log(fmt.Sprintf("Connected to %s (%s) over SSL\n", i.server, i.socket.RemoteAddr())) + return i.postConnect() } +func (i *IRCConnection) log(msg string) { + if i.Log != nil { + i.Log <-msg + } +} + func IRC(nick, user string) *IRCConnection { irc := new(IRCConnection) irc.registered = false diff --git a/src/irc_callback.go b/src/irc_callback.go index 6acc685..f23356f 100644 --- a/src/irc_callback.go +++ b/src/irc_callback.go @@ -28,11 +28,11 @@ func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func return } - fmt.Printf("Event found, but no callback found at index %d. Use AddCallback\n", i) + irc.log(fmt.Sprintf("Event found, but no callback found at index %d. Use AddCallback\n", i)) return } - fmt.Printf("Event not found. Use AddCallBack\n") + irc.log(fmt.Sprintf("Event not found. Use AddCallBack\n")) } func (irc *IRCConnection) RunCallbacks(event *IRCEvent) { @@ -62,7 +62,7 @@ func (irc *IRCConnection) RunCallbacks(event *IRCEvent) { if callbacks, ok := irc.events[event.Code]; ok { if irc.VerboseCallbackHandler { - fmt.Printf("%v (%v) >> %#v\n", event.Code, len(callbacks), event) + irc.log(fmt.Sprintf("%v (%v) >> %#v\n", event.Code, len(callbacks), event)) } for _, callback := range callbacks { @@ -70,7 +70,7 @@ func (irc *IRCConnection) RunCallbacks(event *IRCEvent) { } } else if irc.VerboseCallbackHandler { - fmt.Printf("%v (0) >> %#v\n", event.Code, event) + irc.log(fmt.Sprintf("%v (0) >> %#v\n", event.Code, event)) } } @@ -121,7 +121,7 @@ func (irc *IRCConnection) setupCallbacks() { 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) + irc.log(fmt.Sprintf("Lag: %vs\n", delta)) }) irc.AddCallback("NICK", func(e *IRCEvent) { diff --git a/src/irc_struct.go b/src/irc_struct.go index 9a7f538..7bf4022 100644 --- a/src/irc_struct.go +++ b/src/irc_struct.go @@ -11,37 +11,38 @@ import ( ) type IRCConnection struct { - socket net.Conn - pread, pwrite chan string - Error chan error + Error chan error + Log chan string + Password string + SSLConfig *tls.Config + + socket net.Conn + pread, pwrite chan string syncreader, syncwriter chan bool - nick string //The nickname we want. - nickcurrent string //The nickname we currently have. - user string - registered bool - server string - Password string - events map[string][]func(*IRCEvent) + nick string //The nickname we want. + nickcurrent string //The nickname we currently have. + user string + registered bool + server string + events map[string][]func(*IRCEvent) lastMessage time.Time - ticker <-chan time.Time - ticker2 <-chan time.Time + ticker <-chan time.Time + ticker2 <-chan time.Time VerboseCallbackHandler bool quitting bool - - SSLConfig *tls.Config } type IRCEvent struct { - Code string + Code string Message string - Raw string - Nick string // - Host string //!@ - Source string // - User string // + Raw string + Nick string // + Host string //!@ + Source string // + User string // Arguments []string }