Remove calls to fmt.Printf; send to IRCConn.Log chan if it is non-nil

This commit is contained in:
lye 2012-03-21 22:57:35 -05:00
parent a910f6d47d
commit 31036ff926
3 changed files with 64 additions and 50 deletions

View File

@ -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]
@ -74,7 +74,6 @@ func writer(irc *IRCConnection) {
_, err := irc.socket.Write([]byte(b))
if err != nil {
fmt.Printf("%s\n", err)
irc.Error <-err
break
}
@ -136,7 +135,7 @@ func (irc *IRCConnection) Privmsg(target, message string) {
}
func (irc *IRCConnection) SendRaw(message string) {
fmt.Printf("--> %s\n", message)
irc.log(fmt.Sprintf("--> %s\n", message))
irc.pwrite <-fmt.Sprintf("%s\r\n", message)
}
@ -148,18 +147,20 @@ 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)
@ -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()
}
@ -212,28 +213,40 @@ func (i *IRCConnection) postConnect() error {
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

View File

@ -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) {

View File

@ -11,16 +11,19 @@ import (
)
type IRCConnection struct {
Error chan error
Log chan string
Password string
SSLConfig *tls.Config
socket net.Conn
pread, pwrite chan string
Error chan error
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)
lastMessage time.Time
@ -30,8 +33,6 @@ type IRCConnection struct {
VerboseCallbackHandler bool
quitting bool
SSLConfig *tls.Config
}
type IRCEvent struct {