From 79ac1741ead763666631fb38bd726eaa828769ed Mon Sep 17 00:00:00 2001 From: tj Date: Mon, 18 Oct 2010 21:46:34 +0200 Subject: [PATCH] Add irccon.Quit() and irccon.Cycle() Quit() Sends QUIT to server and exits the main loop. Cycle() Sends QUIT to server and reconnects. --- irc.go | 21 +++++++++++++++++++-- irc_struct.go | 20 +++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/irc.go b/irc.go index 75d7661..683d98b 100644 --- a/irc.go +++ b/irc.go @@ -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 { diff --git a/irc_struct.go b/irc_struct.go index e0b9a07..c9fd948 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -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 {