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_ {
|
for !error_ {
|
||||||
msg, err := br.ReadString('\n')
|
msg, err := br.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
irc.Error <- err
|
irc.Error <-err
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ func reader(irc *IRCConnection) {
|
|||||||
event.Source = msg[1:i]
|
event.Source = msg[1:i]
|
||||||
msg = msg[i+1 : len(msg)]
|
msg = msg[i+1 : len(msg)]
|
||||||
} else {
|
} 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 {
|
if i, j := strings.Index(event.Source, "!"), strings.Index(event.Source, "@"); i > -1 && j > -1 {
|
||||||
event.Nick = event.Source[0:i]
|
event.Nick = event.Source[0:i]
|
||||||
@ -61,7 +61,7 @@ func reader(irc *IRCConnection) {
|
|||||||
irc.RunCallbacks(event)
|
irc.RunCallbacks(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
irc.syncreader <- true
|
irc.syncreader <-true
|
||||||
}
|
}
|
||||||
|
|
||||||
func writer(irc *IRCConnection) {
|
func writer(irc *IRCConnection) {
|
||||||
@ -74,14 +74,13 @@ func writer(irc *IRCConnection) {
|
|||||||
|
|
||||||
_, err := irc.socket.Write([]byte(b))
|
_, err := irc.socket.Write([]byte(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("%s\n", err)
|
irc.Error <-err
|
||||||
irc.Error <- err
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
b, ok = <-irc.pwrite
|
b, ok = <-irc.pwrite
|
||||||
}
|
}
|
||||||
irc.syncwriter <- true
|
irc.syncwriter <-true
|
||||||
}
|
}
|
||||||
|
|
||||||
//Pings the server if we have not recived any messages for 5 minutes
|
//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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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)
|
irc.pwrite <-fmt.Sprintf("%s\r\n", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRCConnection) Reconnect() error {
|
func (i *IRCConnection) Reconnect() error {
|
||||||
@ -148,24 +147,26 @@ func (i *IRCConnection) Reconnect() error {
|
|||||||
<-i.syncwriter
|
<-i.syncwriter
|
||||||
|
|
||||||
for {
|
for {
|
||||||
fmt.Printf("Reconnecting to %s\n", i.server)
|
i.log(fmt.Sprintf("Reconnecting to %s\n", i.server))
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
i.socket, err = net.Dial("tcp", i.server)
|
i.socket, err = net.Dial("tcp", i.server)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
fmt.Printf("Error: %s\n", err)
|
|
||||||
|
i.log(fmt.Sprintf("Error: %s\n", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
error_ = false
|
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 reader(i)
|
||||||
go writer(i)
|
go writer(i)
|
||||||
|
|
||||||
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
|
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("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -178,7 +179,7 @@ func (i *IRCConnection) Loop() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Error: %s\n", e)
|
i.log(fmt.Sprintf("Error: %s\n", e))
|
||||||
error_ = true
|
error_ = true
|
||||||
i.Reconnect()
|
i.Reconnect()
|
||||||
}
|
}
|
||||||
@ -202,38 +203,50 @@ func (i *IRCConnection) postConnect() error {
|
|||||||
go pinger(i)
|
go pinger(i)
|
||||||
|
|
||||||
if len(i.Password) > 0 {
|
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("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("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRCConnection) Connect(server string) error {
|
func (i *IRCConnection) Connect(server string) error {
|
||||||
i.server = server
|
i.server = server
|
||||||
fmt.Printf("Connecting to %s\n", i.server)
|
i.log(fmt.Sprintf("Connecting to %s\n", i.server))
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
i.socket, err = net.Dial("tcp", i.server)
|
i.socket, err = net.Dial("tcp", i.server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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()
|
return i.postConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRCConnection) ConnectSSL(server string) error {
|
func (i *IRCConnection) ConnectSSL(server string) error {
|
||||||
i.server = server
|
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
|
var err error
|
||||||
i.socket, err = tls.Dial("tcp", i.server, i.SSLConfig)
|
i.socket, err = tls.Dial("tcp", i.server, i.SSLConfig)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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()
|
return i.postConnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IRCConnection) log(msg string) {
|
||||||
|
if i.Log != nil {
|
||||||
|
i.Log <-msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func IRC(nick, user string) *IRCConnection {
|
func IRC(nick, user string) *IRCConnection {
|
||||||
irc := new(IRCConnection)
|
irc := new(IRCConnection)
|
||||||
irc.registered = false
|
irc.registered = false
|
||||||
|
@ -28,11 +28,11 @@ func (irc *IRCConnection) ReplaceCallback(eventcode string, i int, callback func
|
|||||||
return
|
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
|
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) {
|
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 callbacks, ok := irc.events[event.Code]; ok {
|
||||||
if irc.VerboseCallbackHandler {
|
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 {
|
for _, callback := range callbacks {
|
||||||
@ -70,7 +70,7 @@ func (irc *IRCConnection) RunCallbacks(event *IRCEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if irc.VerboseCallbackHandler {
|
} 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) {
|
irc.AddCallback("PONG", func(e *IRCEvent) {
|
||||||
ns, _ := strconv.ParseInt(e.Message, 10, 64)
|
ns, _ := strconv.ParseInt(e.Message, 10, 64)
|
||||||
delta := time.Duration(time.Now().UnixNano() - ns)
|
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) {
|
irc.AddCallback("NICK", func(e *IRCEvent) {
|
||||||
|
@ -11,37 +11,38 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type IRCConnection struct {
|
type IRCConnection struct {
|
||||||
socket net.Conn
|
Error chan error
|
||||||
pread, pwrite chan string
|
Log chan string
|
||||||
Error chan error
|
Password string
|
||||||
|
SSLConfig *tls.Config
|
||||||
|
|
||||||
|
socket net.Conn
|
||||||
|
pread, pwrite chan string
|
||||||
syncreader, syncwriter chan bool
|
syncreader, syncwriter chan bool
|
||||||
nick string //The nickname we want.
|
nick string //The nickname we want.
|
||||||
nickcurrent string //The nickname we currently have.
|
nickcurrent string //The nickname we currently have.
|
||||||
user string
|
user string
|
||||||
registered bool
|
registered bool
|
||||||
server string
|
server string
|
||||||
Password string
|
events map[string][]func(*IRCEvent)
|
||||||
events map[string][]func(*IRCEvent)
|
|
||||||
|
|
||||||
lastMessage time.Time
|
lastMessage time.Time
|
||||||
ticker <-chan time.Time
|
ticker <-chan time.Time
|
||||||
ticker2 <-chan time.Time
|
ticker2 <-chan time.Time
|
||||||
|
|
||||||
VerboseCallbackHandler bool
|
VerboseCallbackHandler bool
|
||||||
|
|
||||||
quitting bool
|
quitting bool
|
||||||
|
|
||||||
SSLConfig *tls.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type IRCEvent struct {
|
type IRCEvent struct {
|
||||||
Code string
|
Code string
|
||||||
Message string
|
Message string
|
||||||
Raw string
|
Raw string
|
||||||
Nick string //<nick>
|
Nick string //<nick>
|
||||||
Host string //<nick>!<usr>@<host>
|
Host string //<nick>!<usr>@<host>
|
||||||
Source string //<host>
|
Source string //<host>
|
||||||
User string //<usr>
|
User string //<usr>
|
||||||
|
|
||||||
Arguments []string
|
Arguments []string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user