Remove calls to fmt.Printf; send to IRCConn.Log chan if it is non-nil
This commit is contained in:
parent
a910f6d47d
commit
31036ff926
63
src/irc.go
63
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
|
||||
|
@ -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) {
|
||||
|
@ -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 //<nick>
|
||||
Host string //<nick>!<usr>@<host>
|
||||
Source string //<host>
|
||||
User string //<usr>
|
||||
Raw string
|
||||
Nick string //<nick>
|
||||
Host string //<nick>!<usr>@<host>
|
||||
Source string //<host>
|
||||
User string //<usr>
|
||||
|
||||
Arguments []string
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user