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"
|
2021-02-20 01:54:09 +00:00
|
|
|
"context"
|
2010-08-30 11:16:20 +00:00
|
|
|
"crypto/tls"
|
2021-02-20 01:54:09 +00:00
|
|
|
"errors"
|
2011-11-13 13:34:32 +00:00
|
|
|
"fmt"
|
2013-03-17 21:13:20 +00:00
|
|
|
"io"
|
2011-07-27 20:10:01 +00:00
|
|
|
"net"
|
2015-09-20 07:37:26 +00:00
|
|
|
"net/url"
|
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"
|
2015-09-20 07:37:26 +00:00
|
|
|
|
2022-10-30 10:01:33 +00:00
|
|
|
"github.com/emersion/go-sasl"
|
2015-09-20 07:37:26 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
|
|
|
"github.com/fluffle/goirc/state"
|
2015-09-14 04:32:23 +00:00
|
|
|
"golang.org/x/net/proxy"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Conn encapsulates a connection to a single IRC server. Create
|
|
|
|
// one with Client or SimpleClient.
|
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
|
2014-12-20 14:56:58 +00:00
|
|
|
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
|
2015-09-20 07:37:26 +00:00
|
|
|
dialer *net.Dialer
|
2021-02-20 01:54:09 +00:00
|
|
|
proxyDialer proxy.ContextDialer
|
2015-09-20 07:37:26 +00:00
|
|
|
sock net.Conn
|
|
|
|
io *bufio.ReadWriter
|
|
|
|
in chan *Line
|
|
|
|
out chan string
|
|
|
|
connected bool
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2022-03-06 22:20:06 +00:00
|
|
|
// Capabilities supported by the server
|
|
|
|
supportedCaps *capSet
|
|
|
|
|
|
|
|
// Capabilites currently enabled
|
|
|
|
currCaps *capSet
|
|
|
|
|
2022-10-30 10:01:33 +00:00
|
|
|
// SASL internals
|
|
|
|
saslRemainingData []byte
|
|
|
|
|
2021-03-26 12:02:36 +00:00
|
|
|
// CancelFunc and WaitGroup for goroutines
|
|
|
|
die context.CancelFunc
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Config contains options that can be passed to Client to change the
|
|
|
|
// behaviour of the library during use. It is recommended that NewConfig
|
|
|
|
// is used to create this struct rather than instantiating one directly.
|
|
|
|
// Passing a Config with no Nick in the Me field to Client will result
|
|
|
|
// in unflattering consequences.
|
2013-02-16 18:05:56 +00:00
|
|
|
type Config struct {
|
|
|
|
// Set this to provide the Nick, Ident and Name for the client to use.
|
2015-04-15 21:27:50 +00:00
|
|
|
// It is recommended to call Conn.Me to get up-to-date information
|
|
|
|
// about the current state of the client's IRC nick after connecting.
|
2013-02-16 18:05:56 +00:00
|
|
|
Me *state.Nick
|
|
|
|
|
2013-02-18 01:36:17 +00:00
|
|
|
// Hostname to connect to and optional connect password.
|
2015-04-15 21:27:50 +00:00
|
|
|
// Changing these after connection will have no effect until the
|
|
|
|
// client reconnects.
|
2013-02-18 01:36:17 +00:00
|
|
|
Server, Pass string
|
|
|
|
|
2010-11-03 23:49:28 +00:00
|
|
|
// Are we connecting via SSL? Do we care about certificate validity?
|
2015-04-15 21:27:50 +00:00
|
|
|
// Changing these after connection will have no effect until the
|
|
|
|
// client reconnects.
|
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
|
|
|
|
2015-09-20 07:37:26 +00:00
|
|
|
// To connect via proxy set the proxy url here.
|
2015-09-14 04:32:23 +00:00
|
|
|
// Changing these after connection will have no effect until the
|
|
|
|
// client reconnects.
|
2015-09-20 07:37:26 +00:00
|
|
|
Proxy string
|
2015-09-14 04:32:23 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Local address to bind to when connecting to the server.
|
2014-10-12 17:22:31 +00:00
|
|
|
LocalAddr string
|
|
|
|
|
2018-02-16 21:04:56 +00:00
|
|
|
// To attempt RFC6555 parallel IPv4 and IPv6 connections if both
|
|
|
|
// address families are returned for a hostname, set this to true.
|
|
|
|
// Passed through to https://golang.org/pkg/net/#Dialer
|
|
|
|
DualStack bool
|
|
|
|
|
2022-03-06 22:20:06 +00:00
|
|
|
// Enable IRCv3 capability negotiation.
|
|
|
|
EnableCapabilityNegotiation bool
|
|
|
|
|
|
|
|
// A list of capabilities to request to the server during registration.
|
|
|
|
Capabilites []string
|
|
|
|
|
2022-10-30 10:01:33 +00:00
|
|
|
// SASL configuration to use to authenticate the connection.
|
|
|
|
Sasl sasl.Client
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Replaceable function to customise the 433 handler's new nick.
|
|
|
|
// By default an underscore "_" is appended to the current nick.
|
2013-02-16 00:17:31 +00:00
|
|
|
NewNick func(string) string
|
|
|
|
|
2011-11-15 22:17:29 +00:00
|
|
|
// Client->server ping frequency, in seconds. Defaults to 3m.
|
2015-04-15 21:27:50 +00:00
|
|
|
// Set to 0 to disable client-side pings.
|
2012-06-06 16:12:07 +00:00
|
|
|
PingFreq time.Duration
|
2011-11-15 22:17:29 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// The duration before a connection timeout is triggered. Defaults to 1m.
|
|
|
|
// Set to 0 to wait indefinitely.
|
|
|
|
Timeout time.Duration
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Sent as the reply to a CTCP VERSION message.
|
2013-02-18 06:28:22 +00:00
|
|
|
Version string
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Sent as the default QUIT message if Quit is called with no args.
|
2013-02-18 06:28:22 +00:00
|
|
|
QuitMessage string
|
2013-03-10 13:30:00 +00:00
|
|
|
|
|
|
|
// Configurable panic recovery for all handlers.
|
2015-04-15 21:27:50 +00:00
|
|
|
// Defaults to logging an error, see LogPanic.
|
2013-03-10 13:30:00 +00:00
|
|
|
Recover func(*Conn, *Line)
|
2013-03-17 01:21:09 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Split PRIVMSGs, NOTICEs and CTCPs longer than SplitLen characters
|
|
|
|
// over multiple lines. Default to 450 if not set.
|
2013-03-17 01:21:09 +00:00
|
|
|
SplitLen int
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// NewConfig creates a Config struct containing sensible defaults.
|
|
|
|
// It takes one required argument: the nick to use for the client.
|
|
|
|
// Subsequent string arguments set the client's ident and "real"
|
|
|
|
// name, but these are optional.
|
2013-02-16 18:05:56 +00:00
|
|
|
func NewConfig(nick string, args ...string) *Config {
|
|
|
|
cfg := &Config{
|
2022-03-06 22:20:06 +00:00
|
|
|
Me: &state.Nick{Nick: nick},
|
|
|
|
PingFreq: 3 * time.Minute,
|
|
|
|
NewNick: DefaultNewNick,
|
|
|
|
Recover: (*Conn).LogPanic, // in dispatch.go
|
|
|
|
SplitLen: defaultSplit,
|
|
|
|
Timeout: 60 * time.Second,
|
|
|
|
EnableCapabilityNegotiation: false,
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-17 19:27:52 +00:00
|
|
|
// Because networks limit nick lengths, the easy approach of appending
|
|
|
|
// an '_' to a nick that is already in use can cause problems. When the
|
|
|
|
// length limit is reached, the clients idea of what its nick is
|
|
|
|
// ends up being different from the server. Hilarity ensues.
|
|
|
|
// Thanks to github.com/purpleidea for the bug report!
|
|
|
|
// Thanks to 'man ascii' for
|
|
|
|
func DefaultNewNick(old string) string {
|
|
|
|
if len(old) == 0 {
|
|
|
|
return "_"
|
|
|
|
}
|
|
|
|
c := old[len(old)-1]
|
|
|
|
switch {
|
|
|
|
case c >= '0' && c <= '9':
|
|
|
|
c = '0' + (((c - '0') + 1) % 10)
|
|
|
|
case c >= 'A' && c <= '}':
|
|
|
|
c = 'A' + (((c - 'A') + 1) % 61)
|
|
|
|
default:
|
|
|
|
c = '_'
|
|
|
|
}
|
|
|
|
return old[:len(old)-1] + string(c)
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// SimpleClient creates a new Conn, passing its arguments to NewConfig.
|
|
|
|
// If you don't need to change any client options and just want to get
|
|
|
|
// started quickly, this is a convenient shortcut.
|
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
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Client takes a Config struct and returns a new Conn ready to have
|
|
|
|
// handlers added and connect to a server.
|
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 == "" {
|
2015-01-02 12:58:50 +00:00
|
|
|
cfg.Me = &state.Nick{Nick: "__idiot__"}
|
2013-09-27 21:19:40 +00:00
|
|
|
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)
|
2014-12-20 18:39:44 +00:00
|
|
|
dialer.Timeout = cfg.Timeout
|
2018-02-16 21:04:56 +00:00
|
|
|
dialer.DualStack = cfg.DualStack
|
2014-10-12 17:22:31 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 10:01:33 +00:00
|
|
|
if cfg.Sasl != nil && !cfg.EnableCapabilityNegotiation {
|
|
|
|
logging.Warn("Enabling capability negotiation as it's required for SASL")
|
|
|
|
cfg.EnableCapabilityNegotiation = true
|
|
|
|
}
|
|
|
|
|
2011-07-21 22:03:11 +00:00
|
|
|
conn := &Conn{
|
2022-10-30 10:01:33 +00:00
|
|
|
cfg: cfg,
|
|
|
|
dialer: dialer,
|
|
|
|
intHandlers: handlerSet(),
|
|
|
|
fgHandlers: handlerSet(),
|
|
|
|
bgHandlers: handlerSet(),
|
|
|
|
stRemovers: make([]Remover, 0, len(stHandlers)),
|
|
|
|
lastsent: time.Now(),
|
|
|
|
supportedCaps: capabilitySet(),
|
|
|
|
currCaps: capabilitySet(),
|
|
|
|
saslRemainingData: nil,
|
2011-07-21 22:03:11 +00:00
|
|
|
}
|
2011-11-06 04:56:46 +00:00
|
|
|
conn.addIntHandlers()
|
2013-03-15 17:04:58 +00:00
|
|
|
return conn
|
2013-02-16 18:05:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Connected returns true if the client is successfully connected to
|
|
|
|
// an IRC server. It becomes true when the TCP connection is established,
|
|
|
|
// and false again when the connection is closed.
|
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
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Config returns a pointer to the Config struct used by the client.
|
|
|
|
// Many of the elements of Config may be changed at any point to
|
|
|
|
// affect client behaviour. To disable flood protection temporarily,
|
|
|
|
// for example, a handler could do:
|
|
|
|
//
|
2022-10-30 10:01:33 +00:00
|
|
|
// conn.Config().Flood = true
|
|
|
|
// // Send many lines to the IRC server, risking "excess flood"
|
|
|
|
// conn.Config().Flood = false
|
2013-02-16 18:05:56 +00:00
|
|
|
func (conn *Conn) Config() *Config {
|
|
|
|
return conn.cfg
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Me returns a state.Nick that reflects the client's IRC nick at the
|
|
|
|
// time it is called. If state tracking is enabled, this comes from
|
|
|
|
// the tracker, otherwise it is equivalent to conn.cfg.Me.
|
2013-02-18 01:38:04 +00:00
|
|
|
func (conn *Conn) Me() *state.Nick {
|
2015-01-02 12:58:50 +00:00
|
|
|
if conn.st != nil {
|
|
|
|
conn.cfg.Me = conn.st.Me()
|
|
|
|
}
|
2013-02-18 01:38:04 +00:00
|
|
|
return conn.cfg.Me
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// StateTracker returns the state tracker being used by the client,
|
|
|
|
// if tracking is enabled, and nil otherwise.
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) StateTracker() state.Tracker {
|
|
|
|
return conn.st
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// EnableStateTracking causes the client to track information about
|
|
|
|
// all channels it is joined to, and all the nicks in those channels.
|
|
|
|
// This can be rather handy for a number of bot-writing tasks. See
|
|
|
|
// the state package for more details.
|
|
|
|
//
|
|
|
|
// NOTE: Calling this while connected to an IRC server may cause the
|
|
|
|
// state tracker to become very confused all over STDERR if logging
|
|
|
|
// is enabled. State tracking should enabled before connecting or
|
|
|
|
// at a pinch while the client is not joined to any channels.
|
2011-11-07 13:34:13 +00:00
|
|
|
func (conn *Conn) EnableStateTracking() {
|
2015-01-02 12:58:50 +00:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
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)
|
2015-04-01 16:54:33 +00:00
|
|
|
conn.st.NickInfo(n.Nick, n.Ident, n.Host, n.Name)
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.cfg.Me = conn.st.Me()
|
2011-11-07 13:34:13 +00:00
|
|
|
conn.addSTHandlers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// DisableStateTracking causes the client to stop tracking information
|
|
|
|
// about the channels and nicks it knows of. It will also wipe current
|
|
|
|
// state from the state tracker.
|
2011-11-07 13:34:13 +00:00
|
|
|
func (conn *Conn) DisableStateTracking() {
|
2015-01-02 12:58:50 +00:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
2013-02-16 11:29:56 +00:00
|
|
|
if conn.st != nil {
|
2015-01-02 12:58:50 +00:00
|
|
|
conn.cfg.Me = conn.st.Me()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 22:20:06 +00:00
|
|
|
// SupportsCapability returns true if the server supports the given capability.
|
|
|
|
func (conn *Conn) SupportsCapability(cap string) bool {
|
|
|
|
return conn.supportedCaps.Has(cap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasCapability returns true if the given capability has been acked by the server during negotiation.
|
|
|
|
func (conn *Conn) HasCapability(cap string) bool {
|
|
|
|
return conn.currCaps.Has(cap)
|
|
|
|
}
|
|
|
|
|
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
|
2015-11-02 21:16:07 +00:00
|
|
|
conn.in = make(chan *Line, 32)
|
|
|
|
conn.out = make(chan string, 32)
|
2021-03-26 12:02:36 +00:00
|
|
|
conn.die = nil
|
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
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// ConnectTo connects the IRC client to "host[:port]", which should be either
|
|
|
|
// a hostname or an IP address, with an optional port. It sets the client's
|
|
|
|
// Config.Server to host, Config.Pass to pass if one is provided, and then
|
|
|
|
// calls Connect.
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) ConnectTo(host string, pass ...string) error {
|
2021-02-20 01:54:09 +00:00
|
|
|
return conn.ConnectToContext(context.Background(), host, pass...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectToContext works like ConnectTo but uses the provided context.
|
|
|
|
func (conn *Conn) ConnectToContext(ctx context.Context, host string, pass ...string) error {
|
2013-02-18 01:36:17 +00:00
|
|
|
conn.cfg.Server = host
|
|
|
|
if len(pass) > 0 {
|
|
|
|
conn.cfg.Pass = pass[0]
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2021-02-20 01:54:09 +00:00
|
|
|
return conn.ConnectContext(ctx)
|
2013-02-18 01:36:17 +00:00
|
|
|
}
|
2010-11-03 23:49:28 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// Connect connects the IRC client to the server configured in Config.Server.
|
|
|
|
// To enable explicit SSL on the connection to the IRC server, set Config.SSL
|
|
|
|
// to true before calling Connect(). The port will default to 6697 if SSL is
|
|
|
|
// enabled, and 6667 otherwise.
|
2015-09-20 07:37:26 +00:00
|
|
|
// To enable connecting via a proxy server, set Config.Proxy to the proxy URL
|
|
|
|
// (example socks5://localhost:9000) before calling Connect().
|
2015-04-15 21:27:50 +00:00
|
|
|
//
|
|
|
|
// Upon successful connection, Connected will return true and a REGISTER event
|
|
|
|
// will be fired. This is mostly for internal use; it is suggested that a
|
|
|
|
// handler for the CONNECTED event is used to perform any initial client work
|
|
|
|
// like joining channels and sending messages.
|
2013-02-18 01:36:17 +00:00
|
|
|
func (conn *Conn) Connect() error {
|
2021-02-20 01:54:09 +00:00
|
|
|
return conn.ConnectContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectContext works like Connect but uses the provided context.
|
|
|
|
func (conn *Conn) ConnectContext(ctx context.Context) error {
|
2016-12-18 14:22:12 +00:00
|
|
|
// We don't want to hold conn.mu while firing the REGISTER event,
|
|
|
|
// and it's much easier and less error prone to defer the unlock,
|
|
|
|
// so the connect mechanics have been delegated to internalConnect.
|
2021-02-20 01:54:09 +00:00
|
|
|
err := conn.internalConnect(ctx)
|
2016-12-18 14:22:12 +00:00
|
|
|
if err == nil {
|
|
|
|
conn.dispatch(&Line{Cmd: REGISTER, Time: time.Now()})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// internalConnect handles the work of actually connecting to the server.
|
2021-02-20 01:54:09 +00:00
|
|
|
func (conn *Conn) internalConnect(ctx context.Context) error {
|
2013-02-18 01:36:52 +00:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
2015-04-13 18:05:42 +00:00
|
|
|
conn.initialise()
|
2013-02-18 01:36:52 +00:00
|
|
|
|
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)
|
|
|
|
}
|
2015-09-14 04:32:23 +00:00
|
|
|
|
|
|
|
if !hasPort(conn.cfg.Server) {
|
|
|
|
if conn.cfg.SSL {
|
2014-11-30 18:09:17 +00:00
|
|
|
conn.cfg.Server = net.JoinHostPort(conn.cfg.Server, "6697")
|
2015-09-14 04:32:23 +00:00
|
|
|
} else {
|
|
|
|
conn.cfg.Server = net.JoinHostPort(conn.cfg.Server, "6667")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 07:37:26 +00:00
|
|
|
if conn.cfg.Proxy != "" {
|
|
|
|
proxyURL, err := url.Parse(conn.cfg.Proxy)
|
2015-09-14 04:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-20 01:54:09 +00:00
|
|
|
proxyDialer, err := proxy.FromURL(proxyURL, conn.dialer)
|
2015-09-14 04:32:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2010-11-03 23:49:28 +00:00
|
|
|
}
|
2021-02-20 01:54:09 +00:00
|
|
|
contextProxyDialer, ok := proxyDialer.(proxy.ContextDialer)
|
2023-11-22 17:24:19 +00:00
|
|
|
if ok {
|
|
|
|
conn.proxyDialer = contextProxyDialer
|
|
|
|
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
|
|
|
|
if s, err := conn.proxyDialer.DialContext(ctx, "tcp", conn.cfg.Server); err == nil {
|
|
|
|
conn.sock = s
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2010-08-30 11:16:20 +00:00
|
|
|
} else {
|
2023-11-22 17:24:19 +00:00
|
|
|
logging.Warn("Dialer for proxy does not support context, please implement DialContext")
|
|
|
|
|
|
|
|
conn.proxyDialer = proxyDialer
|
|
|
|
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
|
|
|
|
if s, err := conn.proxyDialer.Dial("tcp", conn.cfg.Server); err == nil {
|
|
|
|
conn.sock = s
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2010-08-30 11:16:20 +00:00
|
|
|
}
|
2010-11-03 20:42:39 +00:00
|
|
|
} else {
|
2015-09-14 04:32:23 +00:00
|
|
|
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
|
2021-02-20 01:54:09 +00:00
|
|
|
if s, err := conn.dialer.DialContext(ctx, "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
|
|
|
}
|
2015-09-14 04:32:23 +00:00
|
|
|
|
|
|
|
if conn.cfg.SSL {
|
|
|
|
logging.Info("irc.Connect(): Performing SSL handshake.")
|
2016-08-24 07:28:48 +00:00
|
|
|
s := tls.Client(conn.sock, conn.cfg.SSLConfig)
|
|
|
|
if err := s.Handshake(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn.sock = s
|
2015-09-14 04:32:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 12:02:36 +00:00
|
|
|
conn.postConnect(ctx, true)
|
2015-04-15 21:27:50 +00:00
|
|
|
conn.connected = true
|
2011-08-21 12:23:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// postConnect performs post-connection setup, for ease of testing.
|
2021-03-26 12:02:36 +00:00
|
|
|
func (conn *Conn) postConnect(ctx context.Context, 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 {
|
2021-03-26 12:02:36 +00:00
|
|
|
ctx, conn.die = context.WithCancel(ctx)
|
2014-09-05 14:39:56 +00:00
|
|
|
conn.wg.Add(3)
|
2021-03-26 12:02:36 +00:00
|
|
|
go conn.send(ctx)
|
2013-04-08 17:02:34 +00:00
|
|
|
go conn.recv()
|
2021-03-26 12:02:36 +00:00
|
|
|
go conn.runLoop(ctx)
|
2013-04-08 17:02:34 +00:00
|
|
|
if conn.cfg.PingFreq > 0 {
|
2014-09-05 14:39:56 +00:00
|
|
|
conn.wg.Add(1)
|
2021-03-26 12:02:36 +00:00
|
|
|
go conn.ping(ctx)
|
2013-04-08 17:02:34 +00:00
|
|
|
}
|
2011-11-15 22:22:40 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// hasPort returns true if the string hostname has a :port suffix.
|
|
|
|
// It was copied from net/http 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
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// send is started as a goroutine after a connection is established.
|
|
|
|
// It shuttles data from the output channel to write(), and is killed
|
2021-03-26 12:02:36 +00:00
|
|
|
// when the context is cancelled.
|
|
|
|
func (conn *Conn) send(ctx context.Context) {
|
2011-07-22 00:08:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line := <-conn.out:
|
2015-04-02 08:22:42 +00:00
|
|
|
if err := conn.write(line); err != nil {
|
|
|
|
logging.Error("irc.send(): %s", err.Error())
|
2016-09-16 18:40:27 +00:00
|
|
|
// We can't defer this, because Close() waits for it.
|
2015-04-02 08:22:42 +00:00
|
|
|
conn.wg.Done()
|
2016-09-16 18:40:27 +00:00
|
|
|
conn.Close()
|
2015-04-02 08:22:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-26 12:02:36 +00:00
|
|
|
case <-ctx.Done():
|
2013-04-08 17:02:34 +00:00
|
|
|
// control channel closed, bail out
|
2015-04-02 08:22:42 +00:00
|
|
|
conn.wg.Done()
|
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
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// recv is started as a goroutine after a connection is established.
|
|
|
|
// It receives "\r\n" terminated lines from the server, parses them into
|
|
|
|
// Lines, and sends them to the input channel.
|
2009-12-17 17:22:31 +00:00
|
|
|
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())
|
|
|
|
}
|
2016-09-16 18:40:27 +00:00
|
|
|
// We can't defer this, because Close() waits for it.
|
2013-09-30 12:24:14 +00:00
|
|
|
conn.wg.Done()
|
2016-09-16 18:40:27 +00:00
|
|
|
conn.Close()
|
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
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// ping is started as a goroutine after a connection is established, as
|
|
|
|
// long as Config.PingFreq >0. It pings the server every PingFreq seconds.
|
2021-03-26 12:02:36 +00:00
|
|
|
func (conn *Conn) ping(ctx context.Context) {
|
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()))
|
2021-03-26 12:02:36 +00:00
|
|
|
case <-ctx.Done():
|
2013-04-08 17:02:34 +00:00
|
|
|
// control channel closed, bail out
|
2011-11-15 22:17:29 +00:00
|
|
|
tick.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// runLoop is started as a goroutine after a connection is established.
|
|
|
|
// It pulls Lines from the input channel and dispatches them to any
|
|
|
|
// handlers that have been registered for that IRC verb.
|
2021-03-26 12:02:36 +00:00
|
|
|
func (conn *Conn) runLoop(ctx context.Context) {
|
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)
|
2021-03-26 12:02:36 +00:00
|
|
|
case <-ctx.Done():
|
2021-03-27 10:35:19 +00:00
|
|
|
// control channel closed, trigger Cancel() to clean
|
|
|
|
// things up properly and bail out
|
|
|
|
|
|
|
|
// We can't defer this, because Close() waits for it.
|
|
|
|
conn.wg.Done()
|
|
|
|
conn.Close()
|
2011-07-22 00:08:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// write writes 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.
|
2015-04-02 08:22:42 +00:00
|
|
|
func (conn *Conn) write(line string) error {
|
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 {
|
2015-04-02 08:22:42 +00:00
|
|
|
return err
|
2011-07-22 00:08:42 +00:00
|
|
|
}
|
2011-11-11 10:17:17 +00:00
|
|
|
if err := conn.io.Flush(); err != nil {
|
2015-04-02 08:22:42 +00:00
|
|
|
return err
|
2011-11-11 10:17:17 +00:00
|
|
|
}
|
2016-05-14 08:12:49 +00:00
|
|
|
if strings.HasPrefix(line, "PASS") {
|
|
|
|
line = "PASS **************"
|
|
|
|
}
|
2013-02-16 00:11:39 +00:00
|
|
|
logging.Debug("-> %s", line)
|
2015-04-02 08:22:42 +00:00
|
|
|
return nil
|
2011-07-22 00:08:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:27:50 +00:00
|
|
|
// rateLimit implements Hybrid's flood control algorithm for 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
|
|
|
}
|
|
|
|
|
2016-09-16 18:40:27 +00:00
|
|
|
// Close tears down all connection-related state. It is called when either
|
2015-04-15 21:27:50 +00:00
|
|
|
// the sending or receiving goroutines encounter an error.
|
2016-09-12 19:55:10 +00:00
|
|
|
// It may also be used to forcibly shut down the connection to the server.
|
2016-09-16 18:40:27 +00:00
|
|
|
func (conn *Conn) Close() error {
|
|
|
|
// Guard against double-call of Close() 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()
|
|
|
|
if !conn.connected {
|
2015-04-14 06:50:39 +00:00
|
|
|
conn.mu.Unlock()
|
2016-09-16 18:40:27 +00:00
|
|
|
return nil
|
2011-07-22 00:17:35 +00:00
|
|
|
}
|
2016-09-16 18:40:27 +00:00
|
|
|
logging.Info("irc.Close(): Disconnected from server.")
|
2013-02-18 01:36:52 +00:00
|
|
|
conn.connected = false
|
2016-09-16 18:40:27 +00:00
|
|
|
err := conn.sock.Close()
|
2021-03-26 12:02:36 +00:00
|
|
|
if conn.die != nil {
|
|
|
|
conn.die()
|
|
|
|
}
|
2015-11-02 21:16:07 +00:00
|
|
|
// Drain both in and out channels to avoid a deadlock if the buffers
|
|
|
|
// have filled. See TestSendDeadlockOnFullBuffer in connection_test.go.
|
|
|
|
conn.drainIn()
|
|
|
|
conn.drainOut()
|
2013-09-30 12:24:14 +00:00
|
|
|
conn.wg.Wait()
|
2015-04-14 06:50:39 +00:00
|
|
|
conn.mu.Unlock()
|
|
|
|
// Dispatch after closing connection but before reinit
|
|
|
|
// so event handlers can still access state information.
|
2015-04-15 21:27:50 +00:00
|
|
|
conn.dispatch(&Line{Cmd: DISCONNECTED, Time: time.Now()})
|
2016-09-16 18:40:27 +00:00
|
|
|
return err
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2015-11-02 21:16:07 +00:00
|
|
|
// drainIn sends all data buffered in conn.in to /dev/null.
|
|
|
|
func (conn *Conn) drainIn() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-conn.in:
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// drainOut does the same for conn.out. Generics!
|
|
|
|
func (conn *Conn) drainOut() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-conn.out:
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|