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

View File

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