This commit is contained in:
tj 2011-11-07 01:26:12 +01:00
parent a303faccdb
commit 4e661a3954
2 changed files with 14 additions and 18 deletions

25
irc.go
View File

@ -5,10 +5,9 @@
package irc package irc
import ( import (
"bufio"
"fmt" "fmt"
"net" "net"
"os"
"bufio"
"strings" "strings"
"time" "time"
) )
@ -17,11 +16,11 @@ const (
VERSION = "GolangBOT v1.0" VERSION = "GolangBOT v1.0"
) )
var error bool var error_ bool
func reader(irc *IRCConnection) { func reader(irc *IRCConnection) {
br := bufio.NewReader(irc.socket) br := bufio.NewReader(irc.socket)
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
@ -59,7 +58,7 @@ func reader(irc *IRCConnection) {
func writer(irc *IRCConnection) { func writer(irc *IRCConnection) {
b, ok := <-irc.pwrite b, ok := <-irc.pwrite
for !error && ok { for !error_ && ok {
if b == "" || irc.socket == nil { if b == "" || irc.socket == nil {
break break
} }
@ -128,21 +127,21 @@ func (irc *IRCConnection) SendRaw(message string) {
irc.pwrite <- fmt.Sprintf("%s\r\n", message) irc.pwrite <- fmt.Sprintf("%s\r\n", message)
} }
func (i *IRCConnection) Reconnect() os.Error { func (i *IRCConnection) Reconnect() error {
close(i.pwrite) close(i.pwrite)
close(i.pread) close(i.pread)
<-i.syncreader <-i.syncreader
<-i.syncwriter <-i.syncwriter
for { for {
fmt.Printf("Reconnecting to %s\n", i.server) fmt.Printf("Reconnecting to %s\n", i.server)
var err os.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) fmt.Printf("Error: %s\n", err)
} }
error = false error_ = false
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
go reader(i) go reader(i)
go writer(i) go writer(i)
@ -158,7 +157,7 @@ func (i *IRCConnection) Loop() {
break 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.pwrite)
@ -167,10 +166,10 @@ func (i *IRCConnection) Loop() {
<-i.syncwriter <-i.syncwriter
} }
func (i *IRCConnection) Connect(server string) os.Error { func (i *IRCConnection) Connect(server string) error {
i.server = server i.server = server
fmt.Printf("Connecting to %s\n", i.server) fmt.Printf("Connecting to %s\n", i.server)
var err os.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
@ -178,7 +177,7 @@ func (i *IRCConnection) Connect(server string) os.Error {
fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr()) fmt.Printf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())
i.pread = make(chan string, 100) i.pread = make(chan string, 100)
i.pwrite = make(chan string, 100) i.pwrite = make(chan string, 100)
i.Error = make(chan os.Error, 10) i.Error = make(chan error, 10)
i.syncreader = make(chan bool) i.syncreader = make(chan bool)
i.syncwriter = make(chan bool) i.syncwriter = make(chan bool)
go reader(i) go reader(i)
@ -197,7 +196,7 @@ func IRC(nick, user string) *IRCConnection {
irc.registered = false irc.registered = false
irc.pread = make(chan string, 100) irc.pread = make(chan string, 100)
irc.pwrite = make(chan string, 100) irc.pwrite = make(chan string, 100)
irc.Error = make(chan os.Error) irc.Error = make(chan error)
irc.nick = nick irc.nick = nick
irc.user = user irc.user = user
irc.VerboseCallbackHandler = true irc.VerboseCallbackHandler = true

View File

@ -4,15 +4,12 @@
package irc package irc
import ( import "net"
"os"
"net"
)
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 error
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.