Standardized on 'irc' as the method receiver name for IRCConn methods

This commit is contained in:
lye 2012-03-21 23:02:29 -05:00
parent 31036ff926
commit b06c2dfa4a

View File

@ -84,25 +84,26 @@ func writer(irc *IRCConnection) {
} }
//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
func pinger(i *IRCConnection) { func pinger(irc *IRCConnection) {
i.ticker = time.Tick(1 * time.Minute) //Tick every minute. irc.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes. irc.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for { for {
select { select {
case <-i.ticker: case <-irc.ticker:
//Ping if we haven't recived anything from the server within 4 minutes //Ping if we haven't recived anything from the server within 4 minutes
if time.Since(i.lastMessage) >= (4 * time.Minute) { if time.Since(irc.lastMessage) >= (4 * time.Minute) {
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano())) irc.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
} }
case <-i.ticker2: case <-irc.ticker2:
//Ping every 15 minutes. //Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano())) irc.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
//Try to recapture nickname if it's not as configured. //Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent { if irc.nick != irc.nickcurrent {
i.nickcurrent = i.nick irc.nickcurrent = irc.nick
i.SendRaw(fmt.Sprintf("NICK %s", i.nick)) irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick))
} }
} }
} }
@ -139,111 +140,111 @@ 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() error { func (irc *IRCConnection) Reconnect() error {
close(i.pwrite) close(irc.pwrite)
close(i.pread) close(irc.pread)
<-i.syncreader <-irc.syncreader
<-i.syncwriter <-irc.syncwriter
for { for {
i.log(fmt.Sprintf("Reconnecting to %s\n", i.server)) irc.log(fmt.Sprintf("Reconnecting to %s\n", irc.server))
var err error var err error
i.socket, err = net.Dial("tcp", i.server) irc.socket, err = net.Dial("tcp", irc.server)
if err == nil { if err == nil {
break break
} }
i.log(fmt.Sprintf("Error: %s\n", err)) irc.log(fmt.Sprintf("Error: %s\n", err))
} }
error_ = false error_ = false
i.log(fmt.Sprintf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())) irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()))
go reader(i) go reader(irc)
go writer(i) go writer(irc)
i.pwrite <-fmt.Sprintf("NICK %s\r\n", i.nick) irc.pwrite <-fmt.Sprintf("NICK %s\r\n", irc.nick)
i.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user) irc.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user)
return nil return nil
} }
func (i *IRCConnection) Loop() { func (irc *IRCConnection) Loop() {
for !i.quitting { for !irc.quitting {
e := <-i.Error e := <-irc.Error
if i.quitting { if irc.quitting {
break break
} }
i.log(fmt.Sprintf("Error: %s\n", e)) irc.log(fmt.Sprintf("Error: %s\n", e))
error_ = true error_ = true
i.Reconnect() irc.Reconnect()
} }
close(i.pwrite) close(irc.pwrite)
close(i.pread) close(irc.pread)
<-i.syncreader <-irc.syncreader
<-i.syncwriter <-irc.syncwriter
} }
func (i *IRCConnection) postConnect() error { func (irc *IRCConnection) postConnect() error {
i.pread = make(chan string, 100) irc.pread = make(chan string, 100)
i.pwrite = make(chan string, 100) irc.pwrite = make(chan string, 100)
i.Error = make(chan error, 10) irc.Error = make(chan error, 10)
i.syncreader = make(chan bool) irc.syncreader = make(chan bool)
i.syncwriter = make(chan bool) irc.syncwriter = make(chan bool)
go reader(i) go reader(irc)
go writer(i) go writer(irc)
go pinger(i) go pinger(irc)
if len(i.Password) > 0 { if len(irc.Password) > 0 {
i.pwrite <-fmt.Sprintf("PASS %s\r\n", i.Password) irc.pwrite <-fmt.Sprintf("PASS %s\r\n", irc.Password)
} }
i.pwrite <-fmt.Sprintf("NICK %s\r\n", i.nick) irc.pwrite <-fmt.Sprintf("NICK %s\r\n", irc.nick)
i.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user) irc.pwrite <-fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", irc.user, irc.user)
return nil return nil
} }
func (i *IRCConnection) Connect(server string) error { func (irc *IRCConnection) Connect(server string) error {
i.server = server irc.server = server
i.log(fmt.Sprintf("Connecting to %s\n", i.server)) irc.log(fmt.Sprintf("Connecting to %s\n", irc.server))
var err error var err error
i.socket, err = net.Dial("tcp", i.server) irc.socket, err = net.Dial("tcp", irc.server)
if err != nil { if err != nil {
return err return err
} }
i.log(fmt.Sprintf("Connected to %s (%s)\n", i.server, i.socket.RemoteAddr())) irc.log(fmt.Sprintf("Connected to %s (%s)\n", irc.server, irc.socket.RemoteAddr()))
return i.postConnect() return irc.postConnect()
} }
func (i *IRCConnection) ConnectSSL(server string) error { func (irc *IRCConnection) ConnectSSL(server string) error {
i.server = server irc.server = server
i.log(fmt.Sprintf("Connecting to %s over SSL\n", i.server)) irc.log(fmt.Sprintf("Connecting to %s over SSL\n", irc.server))
var err error var err error
i.socket, err = tls.Dial("tcp", i.server, i.SSLConfig) irc.socket, err = tls.Dial("tcp", irc.server, irc.SSLConfig)
if err != nil { if err != nil {
return err return err
} }
i.log(fmt.Sprintf("Connected to %s (%s) over SSL\n", i.server, i.socket.RemoteAddr())) irc.log(fmt.Sprintf("Connected to %s (%s) over SSL\n", irc.server, irc.socket.RemoteAddr()))
return i.postConnect() return irc.postConnect()
} }
func (i *IRCConnection) log(msg string) { func (irc *IRCConnection) log(msg string) {
if i.Log != nil { if irc.Log != nil {
i.Log <-msg irc.Log <-msg
} }
} }