Add irccon.Quit() and irccon.Cycle()

Quit() Sends QUIT to server and exits the main loop.
Cycle() Sends QUIT to server and reconnects.
This commit is contained in:
tj 2010-10-18 21:46:34 +02:00
parent 8b8321be96
commit 79ac1741ea
2 changed files with 30 additions and 11 deletions

21
irc.go
View File

@ -59,7 +59,7 @@ func reader(irc *IRCConnection) {
}
func writer(irc *IRCConnection) {
for !error && ! closed(irc.pwrite) {
for !error && !closed(irc.pwrite) {
b := []byte(<-irc.pwrite)
if b == nil || irc.socket == nil {
break
@ -92,6 +92,16 @@ func pinger(i *IRCConnection) {
}
}
func (irc *IRCConnection) Cycle() {
irc.SendRaw("QUIT")
irc.Reconnect()
}
func (irc *IRCConnection) Quit() {
irc.quitting = true
irc.SendRaw("QUIT")
}
func (irc *IRCConnection) Join(channel string) {
irc.pwrite <- fmt.Sprintf("JOIN %s\r\n", channel)
}
@ -133,12 +143,19 @@ func (i *IRCConnection) Reconnect() os.Error {
}
func (i *IRCConnection) Loop() {
for {
for !i.quitting {
e := <-i.Error
if i.quitting {
break
}
fmt.Printf("Error: %s\n", e)
error = true
i.Reconnect()
}
close(i.pwrite)
close(i.pread)
<-i.syncreader
<-i.syncwriter
}
func (i *IRCConnection) Connect(server string) os.Error {

View File

@ -10,20 +10,22 @@ import (
)
type IRCConnection struct {
socket net.Conn
pread, pwrite chan string
Error chan os.Error
socket net.Conn
pread, pwrite chan string
Error chan os.Error
syncreader, syncwriter chan bool
nick string
user string
registered bool
server string
Password string
events map[string][]func(*IRCEvent)
nick string
user string
registered bool
server string
Password string
events map[string][]func(*IRCEvent)
lastMessage int64
ticker <-chan int64
ticker2 <-chan int64
quitting bool
}
type IRCEvent struct {