dispatch/irc/conn.go

224 lines
3.2 KiB
Go
Raw Normal View History

2015-06-05 22:34:13 +00:00
package irc
import (
"bufio"
"crypto/tls"
2017-07-03 05:35:38 +00:00
"errors"
2015-06-05 22:34:13 +00:00
"fmt"
"net"
"strings"
"time"
)
2017-07-03 05:35:38 +00:00
var (
ErrBadProtocol = errors.New("This server does not speak IRC")
)
2015-06-05 22:34:13 +00:00
func (c *Client) Connect(address string) {
if idx := strings.Index(address, ":"); idx < 0 {
c.Host = address
if c.TLS {
address += ":6697"
} else {
address += ":6667"
}
} else {
c.Host = address[:idx]
}
c.Server = address
c.dialer = &net.Dialer{Timeout: 10 * time.Second}
c.connChange(false, nil)
2015-06-05 22:34:13 +00:00
go c.run()
}
func (c *Client) Write(data string) {
c.out <- data + "\r\n"
}
func (c *Client) Writef(format string, a ...interface{}) {
c.out <- fmt.Sprintf(format+"\r\n", a...)
}
func (c *Client) write(data string) {
c.conn.Write([]byte(data + "\r\n"))
}
func (c *Client) writef(format string, a ...interface{}) {
fmt.Fprintf(c.conn, format+"\r\n", a...)
}
2016-01-15 07:26:06 +00:00
func (c *Client) run() {
c.tryConnect()
2015-06-11 02:57:52 +00:00
2016-01-15 07:26:06 +00:00
for {
select {
case <-c.quit:
if c.Connected() {
c.disconnect()
}
2016-01-11 20:04:57 +00:00
2016-01-15 07:26:06 +00:00
c.sendRecv.Wait()
close(c.Messages)
return
2016-01-11 20:04:57 +00:00
2016-01-15 07:26:06 +00:00
case <-c.reconnect:
c.disconnect()
2017-07-03 05:35:38 +00:00
c.connChange(false, nil)
2015-06-05 22:34:13 +00:00
2016-01-15 07:26:06 +00:00
c.sendRecv.Wait()
c.reconnect = make(chan struct{})
2015-06-05 22:34:13 +00:00
2016-01-15 07:26:06 +00:00
c.tryConnect()
}
}
}
2015-06-05 22:34:13 +00:00
type ConnectionState struct {
Connected bool
Error error
}
func (c *Client) connChange(connected bool, err error) {
c.ConnectionChanged <- ConnectionState{
Connected: connected,
Error: err,
}
}
2016-01-15 07:26:06 +00:00
func (c *Client) disconnect() {
c.lock.Lock()
c.connected = false
c.lock.Unlock()
2015-06-05 22:34:13 +00:00
2016-01-15 07:26:06 +00:00
c.conn.Close()
2015-06-05 22:34:13 +00:00
}
func (c *Client) tryConnect() {
for {
select {
case <-c.quit:
return
2016-01-12 23:12:51 +00:00
2015-06-05 22:34:13 +00:00
default:
}
err := c.connect()
if err != nil {
c.connChange(false, err)
} else {
2016-01-13 00:00:57 +00:00
c.backoff.Reset()
2016-02-03 20:12:32 +00:00
c.flushChannels()
2015-06-05 22:34:13 +00:00
return
}
2016-01-12 23:12:51 +00:00
2016-01-13 00:00:57 +00:00
time.Sleep(c.backoff.Duration())
2015-06-05 22:34:13 +00:00
}
}
2016-01-15 07:26:06 +00:00
func (c *Client) connect() error {
c.lock.Lock()
defer c.lock.Unlock()
2015-06-05 22:34:13 +00:00
2016-01-15 07:26:06 +00:00
if c.TLS {
conn, err := tls.DialWithDialer(c.dialer, "tcp", c.Server, c.TLSConfig)
if err != nil {
return err
}
2016-01-12 23:12:51 +00:00
2016-01-15 07:26:06 +00:00
c.conn = conn
} else {
conn, err := c.dialer.Dial("tcp", c.Server)
if err != nil {
return err
2015-06-05 22:34:13 +00:00
}
2016-01-15 07:26:06 +00:00
c.conn = conn
2015-06-05 22:34:13 +00:00
}
2016-01-15 07:26:06 +00:00
c.connected = true
c.connChange(true, nil)
2016-01-15 07:26:06 +00:00
c.reader = bufio.NewReader(c.conn)
c.register()
2017-07-03 05:35:38 +00:00
c.sendRecv.Add(1)
2016-01-15 07:26:06 +00:00
go c.recv()
return nil
2015-06-05 22:34:13 +00:00
}
func (c *Client) send() {
2016-01-12 23:12:51 +00:00
defer c.sendRecv.Done()
2015-06-05 22:34:13 +00:00
for {
select {
case <-c.quit:
return
case <-c.reconnect:
return
case msg := <-c.out:
_, err := c.conn.Write([]byte(msg))
if err != nil {
return
}
}
}
}
func (c *Client) recv() {
2016-01-12 23:12:51 +00:00
defer c.sendRecv.Done()
2015-06-05 22:34:13 +00:00
for {
line, err := c.reader.ReadString('\n')
if err != nil {
2015-06-11 00:04:51 +00:00
select {
case <-c.quit:
return
2016-01-12 23:12:51 +00:00
2015-06-11 00:04:51 +00:00
default:
close(c.reconnect)
return
}
2015-06-05 22:34:13 +00:00
}
msg := parseMessage(line)
2017-07-03 05:35:38 +00:00
if msg == nil {
close(c.quit)
c.connChange(false, ErrBadProtocol)
return
}
2015-06-05 22:34:13 +00:00
switch msg.Command {
case Ping:
2016-12-16 23:11:44 +00:00
go c.write("PONG :" + msg.LastParam())
2015-06-05 22:34:13 +00:00
2016-02-03 20:12:32 +00:00
case Join:
if msg.Nick == c.GetNick() {
c.addChannel(msg.Params[0])
}
2017-04-11 04:04:59 +00:00
case Nick:
if msg.Nick == c.GetNick() {
c.setNick(msg.LastParam())
}
2015-06-05 22:34:13 +00:00
case ReplyWelcome:
c.setNick(msg.Params[0])
2017-07-03 05:35:38 +00:00
c.sendRecv.Add(1)
go c.send()
2017-04-06 21:31:11 +00:00
case ErrNicknameInUse:
if c.HandleNickInUse != nil {
go c.writeNick(c.HandleNickInUse(msg.Params[1]))
}
2015-06-05 22:34:13 +00:00
}
2017-04-11 04:04:59 +00:00
c.Messages <- msg
2015-06-05 22:34:13 +00:00
}
}