2010-11-21 19:53:14 +00:00
|
|
|
package client
|
2009-11-29 20:23:15 +00:00
|
|
|
|
|
|
|
import (
|
2009-12-17 21:30:18 +00:00
|
|
|
"bufio"
|
2010-08-30 11:16:20 +00:00
|
|
|
"crypto/tls"
|
2011-11-13 13:34:32 +00:00
|
|
|
"fmt"
|
2013-09-27 21:19:40 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2011-11-06 04:56:46 +00:00
|
|
|
"github.com/fluffle/goirc/state"
|
2013-03-17 21:13:20 +00:00
|
|
|
"io"
|
2011-07-27 20:10:01 +00:00
|
|
|
"net"
|
2009-12-17 21:30:18 +00:00
|
|
|
"strings"
|
2013-02-18 01:36:52 +00:00
|
|
|
"sync"
|
2009-12-19 13:40:50 +00:00
|
|
|
"time"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
2011-08-21 12:57:47 +00:00
|
|
|
// An IRC connection is represented by this struct.
|
2009-12-17 17:22:31 +00:00
|
|
|
type Conn struct {
|
2013-02-18 01:36:52 +00:00
|
|
|
// For preventing races on (dis)connect.
|
2013-09-30 12:55:55 +00:00
|
|
|
mu sync.RWMutex
|
2013-02-16 18:05:56 +00:00
|
|
|
|
|
|
|
// Contains parameters that people can tweak to change client behaviour.
|
|
|
|
cfg *Config
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2013-02-27 20:23:24 +00:00
|
|
|
// Handlers
|
2013-09-27 23:15:54 +00:00
|
|
|
intHandlers *hSet
|
|
|
|
fgHandlers *hSet
|
|
|
|
bgHandlers *hSet
|
2011-07-27 20:35:45 +00:00
|
|
|
|
2011-09-28 19:48:58 +00:00
|
|
|
// State tracker for nicks and channels
|
2013-02-16 11:29:56 +00:00
|
|
|
st state.Tracker
|
2013-02-16 00:17:31 +00:00
|
|
|
stRemovers []Remover
|
2011-11-06 04:56:46 +00:00
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// I/O stuff to server
|
2014-10-12 17:22:31 +00:00
|
|
|
dialer *net.Dialer
|
2010-08-30 11:16:20 +00:00
|
|
|
sock net.Conn
|
2009-12-17 21:30:18 +00:00
|
|
|
io *bufio.ReadWriter
|
|
|
|
in chan *Line
|
|
|
|
out chan string
|
2013-02-18 01:36:17 +00:00
|
|
|
connected bool
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2013-09-30 12:24:14 +00:00
|
|
|
// Control channel and WaitGroup for goroutines
|
2013-04-08 17:02:34 +00:00
|
|
|
die chan struct{}
|
2013-09-30 12:24:14 +00:00
|
|
|
wg sync.WaitGroup
|
2011-07-22 00:08:42 +00:00
|
|
|
|
2013-02-16 18:05:56 +00:00
|
|
|
// Internal counters for flood protection
|
|
|
|
badness time.Duration
|
|
|
|
lastsent time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// Misc knobs to tweak client behaviour go in here
|
|
|
|
type Config struct {
|
|
|
|
// Set this to provide the Nick, Ident and Name for the client to use.
|
|
|
|
Me *state.Nick
|
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
// Hostname to connect to and optional connect password.
|
|
|
|
Server, Pass string
|
|
|
|
|
2010-11-03 23:49:28 +00:00
|
|
|
// Are we connecting via SSL? Do we care about certificate validity?
|
2010-11-04 00:25:46 +00:00
|
|
|
SSL bool
|
2010-11-03 23:49:28 +00:00
|
|
|
SSLConfig *tls.Config
|
2010-08-30 11:16:20 +00:00
|
|
|
|
2014-10-12 17:22:31 +00:00
|
|
|
// Local address to connect to the server.
|
|
|
|
LocalAddr string
|
|
|
|
|
2013-02-16 00:17:31 +00:00
|
|
|
// Replaceable function to customise the 433 handler's new nick
|
|
|
|
NewNick func(string) string
|
|
|
|
|
2011-11-15 22:17:29 +00:00
|
|
|
// Client->server ping frequency, in seconds. Defaults to 3m.
|
2012-06-06 16:12:07 +00:00
|
|
|
PingFreq time.Duration
|
2011-11-15 22:17:29 +00:00
|
|
|
|
2009-12-19 18:09:29 +00:00
|
|
|
// Set this to true to disable flood protection and false to re-enable
|
2010-08-30 11:16:20 +00:00
|
|
|
Flood bool
|
2013-02-18 06:28:22 +00:00
|
|
|
|
|
|
|
// Sent as the reply to a CTCP VERSION message
|
|
|
|
Version string
|
|
|
|
|
|
|
|
// Sent as the QUIT message.
|
|
|
|
QuitMessage string
|
2013-03-10 13:30:00 +00:00
|
|
|
|
|
|
|
// Configurable panic recovery for all handlers.
|
|
|
|
Recover func(*Conn, *Line)
|
2013-03-17 01:21:09 +00:00
|
|
|
|
|
|
|
// Split PRIVMSGs, NOTICEs and CTCPs longer than
|
|
|
|
// SplitLen characters over multiple lines.
|
|
|
|
SplitLen int
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 18:05:56 +00:00
|
|
|
func NewConfig(nick string, args ...string) *Config {
|
|
|
|
cfg := &Config{
|
2013-02-18 01:36:17 +00:00
|
|
|
Me: state.NewNick(nick),
|
2013-02-17 23:55:39 +00:00
|
|
|
PingFreq: 3 * time.Minute,
|
|
|
|
NewNick: func(s string) string { return s + "_" },
|
2013-03-10 13:31:04 +00:00
|
|
|
Recover: (*Conn).LogPanic, // in dispatch.go
|
2013-03-17 01:21:09 +00:00
|
|
|
SplitLen: 450,
|
2013-02-16 18:05:56 +00:00
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
cfg.Me.Ident = "goirc"
|
|
|
|
if len(args) > 0 && args[0] != "" {
|
|
|
|
cfg.Me.Ident = args[0]
|
|
|
|
}
|
|
|
|
cfg.Me.Name = "Powered by GoIRC"
|
|
|
|
if len(args) > 1 && args[1] != "" {
|
|
|
|
cfg.Me.Name = args[1]
|
|
|
|
}
|
2013-02-18 06:28:22 +00:00
|
|
|
cfg.Version = "Powered by GoIRC"
|
|
|
|
cfg.QuitMessage = "GoBye!"
|
2013-02-16 18:05:56 +00:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new IRC connection object, but doesn't connect to anything so
|
|
|
|
// that you can add event handlers to it. See AddHandler() for details
|
2013-02-18 01:42:44 +00:00
|
|
|
func SimpleClient(nick string, args ...string) *Conn {
|
2013-03-15 17:04:58 +00:00
|
|
|
conn := Client(NewConfig(nick, args...))
|
2013-02-18 01:42:44 +00:00
|
|
|
return conn
|
2013-02-16 18:05:56 +00:00
|
|
|
}
|
|
|
|
|
2013-03-15 17:04:58 +00:00
|
|
|
func Client(cfg *Config) *Conn {
|
2013-09-27 21:19:40 +00:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = NewConfig("__idiot__")
|
|
|
|
}
|
|
|
|
if cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
|
|
|
|
cfg.Me = state.NewNick("__idiot__")
|
|
|
|
cfg.Me.Ident = "goirc"
|
|
|
|
cfg.Me.Name = "Powered by GoIRC"
|
2013-02-16 18:05:56 +00:00
|
|
|
}
|
2014-10-12 17:22:31 +00:00
|
|
|
|
|
|
|
dialer := new(net.Dialer)
|
|
|
|
if cfg.LocalAddr != "" {
|
|
|
|
if !hasPort(cfg.LocalAddr) {
|
|
|
|
cfg.LocalAddr += ":0"
|
|
|
|
}
|
|
|
|
|
|
|
|
local, err := net.ResolveTCPAddr("tcp", cfg.LocalAddr)
|
|
|
|
if err == nil {
|
|
|
|
dialer.LocalAddr = local
|
|
|
|
} else {
|
|
|
|
logging.Error("irc.Client(): Cannot resolve local address %s: %s", cfg.LocalAddr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 22:03:11 +00:00
|
|
|
conn := &Conn{
|
2013-09-27 23:15:54 +00:00
|
|
|
cfg: cfg,
|
2014-10-12 17:22:31 +00:00
|
|
|
dialer: dialer,
|
2013-09-27 23:15:54 +00:00
|
|
|
in: make(chan *Line, 32),
|
|
|
|
out: make(chan string, 32),
|
|
|
|
intHandlers: handlerSet(),
|
|
|
|
fgHandlers: handlerSet(),
|
|
|
|
bgHandlers: handlerSet(),
|
|
|
|
stRemovers: make([]Remover, 0, len(stHandlers)),
|
|
|
|
lastsent: time.Now(),
|
2011-07-21 22:03:11 +00:00
|
|
|
}
|
2011-11-06 04:56:46 +00:00
|
|
|
conn.addIntHandlers()
|
2011-07-21 22:03:11 +00:00
|
|
|
conn.initialise()
|
2013-03-15 17:04:58 +00:00
|
|
|
return conn
|
2013-02-16 18:05:56 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) Connected() bool {
|
2013-09-30 12:55:55 +00:00
|
|
|
conn.mu.RLock()
|
|
|
|
defer conn.mu.RUnlock()
|
2013-02-18 01:36:17 +00:00
|
|
|
return conn.connected
|
|
|
|
}
|
|
|
|
|
2013-02-16 18:05:56 +00:00
|
|
|
func (conn *Conn) Config() *Config {
|
|
|
|
return conn.cfg
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 01:38:04 +00:00
|
|
|
func (conn *Conn) Me() *state.Nick {
|
|
|
|
return conn.cfg.Me
|
|
|
|
}
|
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) StateTracker() state.Tracker {
|
|
|
|
return conn.st
|
|
|
|
}
|
|
|
|
|
2011-11-07 13:34:13 +00:00
|
|
|
func (conn *Conn) EnableStateTracking() {
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st == nil {
|
2013-02-18 01:36:17 +00:00
|
|
|
n := conn.cfg.Me
|
2013-02-16 11:29:56 +00:00
|
|
|
conn.st = state.NewTracker(n.Nick)
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.cfg.Me = conn.st.Me()
|
|
|
|
conn.cfg.Me.Ident = n.Ident
|
|
|
|
conn.cfg.Me.Name = n.Name
|
2011-11-07 13:34:13 +00:00
|
|
|
conn.addSTHandlers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) DisableStateTracking() {
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st != nil {
|
2011-11-07 13:34:13 +00:00
|
|
|
conn.delSTHandlers()
|
2013-02-16 11:29:56 +00:00
|
|
|
conn.st.Wipe()
|
|
|
|
conn.st = nil
|
2011-11-07 13:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-21 22:03:11 +00:00
|
|
|
// Per-connection state initialisation.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) initialise() {
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.io = nil
|
|
|
|
conn.sock = nil
|
2013-04-08 17:02:34 +00:00
|
|
|
conn.die = make(chan struct{})
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st != nil {
|
|
|
|
conn.st.Wipe()
|
2009-12-19 19:00:27 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Connect the IRC connection object to "host[:port]" which should be either
|
2010-08-30 11:16:20 +00:00
|
|
|
// a hostname or an IP address, with an optional port. To enable explicit SSL
|
2010-11-03 23:49:28 +00:00
|
|
|
// on the connection to the IRC server, set Conn.SSL to true before calling
|
|
|
|
// Connect(). The port will default to 6697 if ssl is enabled, and 6667
|
|
|
|
// otherwise. You can also provide an optional connect password.
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) ConnectTo(host string, pass ...string) error {
|
|
|
|
conn.cfg.Server = host
|
|
|
|
if len(pass) > 0 {
|
|
|
|
conn.cfg.Pass = pass[0]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
return conn.Connect()
|
|
|
|
}
|
2010-11-03 23:49:28 +00:00
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) Connect() error {
|
2013-02-18 01:36:52 +00:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
if conn.cfg.Server == "" {
|
|
|
|
return fmt.Errorf("irc.Connect(): cfg.Server must be non-empty")
|
|
|
|
}
|
|
|
|
if conn.connected {
|
|
|
|
return fmt.Errorf("irc.Connect(): Cannot connect to %s, already connected.", conn.cfg.Server)
|
|
|
|
}
|
2013-02-16 18:05:56 +00:00
|
|
|
if conn.cfg.SSL {
|
2013-02-18 01:36:17 +00:00
|
|
|
if !hasPort(conn.cfg.Server) {
|
|
|
|
conn.cfg.Server += ":6697"
|
2010-11-03 23:49:28 +00:00
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
logging.Info("irc.Connect(): Connecting to %s with SSL.", conn.cfg.Server)
|
2014-10-12 17:22:31 +00:00
|
|
|
if s, err := tls.DialWithDialer(conn.dialer, "tcp", conn.cfg.Server, conn.cfg.SSLConfig); err == nil {
|
2011-07-21 21:00:45 +00:00
|
|
|
conn.sock = s
|
2010-08-30 11:16:20 +00:00
|
|
|
} else {
|
2010-11-03 23:49:28 +00:00
|
|
|
return err
|
2010-08-30 11:16:20 +00:00
|
|
|
}
|
2010-11-03 20:42:39 +00:00
|
|
|
} else {
|
2013-02-18 01:36:17 +00:00
|
|
|
if !hasPort(conn.cfg.Server) {
|
|
|
|
conn.cfg.Server += ":6667"
|
2010-11-03 23:49:28 +00:00
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
logging.Info("irc.Connect(): Connecting to %s without SSL.", conn.cfg.Server)
|
2014-10-12 17:22:31 +00:00
|
|
|
if s, err := conn.dialer.Dial("tcp", conn.cfg.Server); err == nil {
|
2010-11-03 23:49:28 +00:00
|
|
|
conn.sock = s
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.connected = true
|
2013-04-08 17:02:34 +00:00
|
|
|
conn.postConnect(true)
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.dispatch(&Line{Cmd: REGISTER})
|
2011-08-21 12:23:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2011-08-21 12:23:42 +00:00
|
|
|
// Post-connection setup (for ease of testing)
|
2013-04-08 17:02:34 +00:00
|
|
|
func (conn *Conn) postConnect(start bool) {
|
2011-08-21 12:23:42 +00:00
|
|
|
conn.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(conn.sock),
|
|
|
|
bufio.NewWriter(conn.sock))
|
2013-04-08 17:02:34 +00:00
|
|
|
if start {
|
2014-09-05 14:39:56 +00:00
|
|
|
conn.wg.Add(3)
|
2013-04-08 17:02:34 +00:00
|
|
|
go conn.send()
|
|
|
|
go conn.recv()
|
|
|
|
go conn.runLoop()
|
|
|
|
if conn.cfg.PingFreq > 0 {
|
2014-09-05 14:39:56 +00:00
|
|
|
conn.wg.Add(1)
|
2013-04-08 17:02:34 +00:00
|
|
|
go conn.ping()
|
|
|
|
}
|
2011-11-15 22:22:40 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copied from http.client for great justice
|
2010-11-03 23:46:58 +00:00
|
|
|
func hasPort(s string) bool {
|
|
|
|
return strings.LastIndex(s, ":") > strings.LastIndex(s, "]")
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
// goroutine to pass data from output channel to write()
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) send() {
|
2013-09-30 12:24:14 +00:00
|
|
|
defer conn.wg.Done()
|
2011-07-22 00:08:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line := <-conn.out:
|
|
|
|
conn.write(line)
|
2013-04-08 17:02:34 +00:00
|
|
|
case <-conn.die:
|
|
|
|
// control channel closed, bail out
|
2011-07-22 00:08:42 +00:00
|
|
|
return
|
2010-11-02 21:47:05 +00:00
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// receive one \r\n terminated line from peer, parse and dispatch it
|
|
|
|
func (conn *Conn) recv() {
|
|
|
|
for {
|
2009-12-17 21:30:18 +00:00
|
|
|
s, err := conn.io.ReadString('\n')
|
2009-12-17 17:22:31 +00:00
|
|
|
if err != nil {
|
2013-03-17 21:13:20 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
logging.Error("irc.recv(): %s", err.Error())
|
|
|
|
}
|
2013-09-30 12:24:14 +00:00
|
|
|
// We can't defer this, because shutdown() waits for it.
|
|
|
|
conn.wg.Done()
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.shutdown()
|
2011-09-29 21:54:54 +00:00
|
|
|
return
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2010-08-21 18:23:07 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Debug("<- %s", s)
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2014-07-06 18:26:34 +00:00
|
|
|
if line := ParseLine(s); line != nil {
|
2012-02-04 00:51:06 +00:00
|
|
|
line.Time = time.Now()
|
2011-07-27 20:15:09 +00:00
|
|
|
conn.in <- line
|
2011-11-06 04:56:46 +00:00
|
|
|
} else {
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Warn("irc.recv(): problems parsing line:\n %s", s)
|
2011-07-27 20:15:09 +00:00
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2011-11-15 22:17:29 +00:00
|
|
|
// Repeatedly pings the server every PingFreq seconds (no matter what)
|
|
|
|
func (conn *Conn) ping() {
|
2013-09-30 12:24:14 +00:00
|
|
|
defer conn.wg.Done()
|
2013-02-16 18:05:56 +00:00
|
|
|
tick := time.NewTicker(conn.cfg.PingFreq)
|
2011-11-15 22:17:29 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2013-03-08 01:43:33 +00:00
|
|
|
conn.Ping(fmt.Sprintf("%d", time.Now().UnixNano()))
|
2013-04-08 17:02:34 +00:00
|
|
|
case <-conn.die:
|
|
|
|
// control channel closed, bail out
|
2011-11-15 22:17:29 +00:00
|
|
|
tick.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
// goroutine to dispatch events for lines received on input channel
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) runLoop() {
|
2013-09-30 12:24:14 +00:00
|
|
|
defer conn.wg.Done()
|
2011-07-22 00:08:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line := <-conn.in:
|
2013-02-16 00:17:31 +00:00
|
|
|
conn.dispatch(line)
|
2013-04-08 17:02:34 +00:00
|
|
|
case <-conn.die:
|
|
|
|
// control channel closed, bail out
|
2011-07-22 00:08:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a \r\n terminated line of output to the connected server,
|
2013-02-16 18:05:56 +00:00
|
|
|
// using Hybrid's algorithm to rate limit if conn.cfg.Flood is false.
|
2011-07-22 00:08:42 +00:00
|
|
|
func (conn *Conn) write(line string) {
|
2013-02-16 18:05:56 +00:00
|
|
|
if !conn.cfg.Flood {
|
2012-02-04 01:13:07 +00:00
|
|
|
if t := conn.rateLimit(len(line)); t != 0 {
|
2011-11-11 11:17:18 +00:00
|
|
|
// sleep for the current line's time value before sending it
|
2013-09-27 21:19:40 +00:00
|
|
|
logging.Info("irc.rateLimit(): Flood! Sleeping for %.2f secs.",
|
2012-02-04 01:13:07 +00:00
|
|
|
t.Seconds())
|
2011-11-11 11:17:18 +00:00
|
|
|
<-time.After(t)
|
|
|
|
}
|
2011-07-22 00:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conn.io.WriteString(line + "\r\n"); err != nil {
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Error("irc.send(): %s", err.Error())
|
2011-07-22 00:08:42 +00:00
|
|
|
conn.shutdown()
|
|
|
|
return
|
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
if err := conn.io.Flush(); err != nil {
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Error("irc.send(): %s", err.Error())
|
2011-11-11 10:17:17 +00:00
|
|
|
conn.shutdown()
|
|
|
|
return
|
|
|
|
}
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Debug("-> %s", line)
|
2011-07-22 00:08:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Hybrid's flood control algorithm to rate-limit outgoing lines.
|
2012-02-04 01:13:07 +00:00
|
|
|
func (conn *Conn) rateLimit(chars int) time.Duration {
|
2011-07-22 00:08:42 +00:00
|
|
|
// Hybrid's algorithm allows for 2 seconds per line and an additional
|
|
|
|
// 1/120 of a second per character on that line.
|
2012-02-04 01:13:07 +00:00
|
|
|
linetime := 2*time.Second + time.Duration(chars)*time.Second/120
|
2012-02-04 00:51:06 +00:00
|
|
|
elapsed := time.Now().Sub(conn.lastsent)
|
2011-07-22 00:08:42 +00:00
|
|
|
if conn.badness += linetime - elapsed; conn.badness < 0 {
|
|
|
|
// negative badness times are badness...
|
2012-02-04 01:13:07 +00:00
|
|
|
conn.badness = 0
|
2011-07-22 00:08:42 +00:00
|
|
|
}
|
2012-02-04 00:51:06 +00:00
|
|
|
conn.lastsent = time.Now()
|
2011-07-22 00:08:42 +00:00
|
|
|
// If we've sent more than 10 second's worth of lines according to the
|
|
|
|
// calculation above, then we're at risk of "Excess Flood".
|
2012-02-04 01:13:07 +00:00
|
|
|
if conn.badness > 10*time.Second {
|
2011-11-11 11:17:18 +00:00
|
|
|
return linetime
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2011-11-11 11:17:18 +00:00
|
|
|
return 0
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) shutdown() {
|
2011-07-22 00:17:35 +00:00
|
|
|
// Guard against double-call of shutdown() if we get an error in send()
|
2013-02-16 18:05:56 +00:00
|
|
|
// as calling sock.Close() will cause recv() to receive EOF in readstring()
|
2013-02-18 01:36:52 +00:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
if !conn.connected {
|
|
|
|
return
|
2011-07-22 00:17:35 +00:00
|
|
|
}
|
2013-02-18 01:36:52 +00:00
|
|
|
logging.Info("irc.shutdown(): Disconnected from server.")
|
|
|
|
conn.connected = false
|
|
|
|
conn.sock.Close()
|
2013-04-08 17:02:34 +00:00
|
|
|
close(conn.die)
|
2013-09-30 12:24:14 +00:00
|
|
|
conn.wg.Wait()
|
2013-02-18 01:36:52 +00:00
|
|
|
// reinit datastructures ready for next connection
|
|
|
|
conn.initialise()
|
2014-05-31 18:34:21 +00:00
|
|
|
conn.dispatch(&Line{Cmd: DISCONNECTED})
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// Dumps a load of information about the current state of the connection to a
|
2011-11-06 04:56:46 +00:00
|
|
|
// string for debugging state tracking and other such things.
|
2009-12-17 17:22:31 +00:00
|
|
|
func (conn *Conn) String() string {
|
2009-12-17 21:30:18 +00:00
|
|
|
str := "GoIRC Connection\n"
|
|
|
|
str += "----------------\n\n"
|
2014-06-14 14:50:52 +00:00
|
|
|
if conn.Connected() {
|
2013-02-18 01:36:17 +00:00
|
|
|
str += "Connected to " + conn.cfg.Server + "\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "Not currently connected!\n\n"
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2014-06-14 14:50:52 +00:00
|
|
|
str += conn.Me().String() + "\n"
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st != nil {
|
|
|
|
str += conn.st.String() + "\n"
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
return str
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|