2017-02-06 07:45:31 +00:00
|
|
|
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
|
|
|
|
// of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
2016-11-14 11:50:14 +00:00
|
|
|
|
2016-11-13 08:30:43 +00:00
|
|
|
package girc
|
|
|
|
|
|
|
|
import (
|
2017-05-07 16:39:16 +00:00
|
|
|
"context"
|
2016-11-13 08:30:43 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2017-02-21 04:02:45 +00:00
|
|
|
"runtime"
|
2017-07-04 05:29:22 +00:00
|
|
|
"sort"
|
2017-02-12 03:00:01 +00:00
|
|
|
"sync"
|
2016-11-13 08:30:43 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-11-14 11:50:14 +00:00
|
|
|
// Client contains all of the information necessary to run a single IRC
|
|
|
|
// client.
|
2016-11-13 08:30:43 +00:00
|
|
|
type Client struct {
|
2017-02-08 07:55:38 +00:00
|
|
|
// Config represents the configuration
|
|
|
|
Config Config
|
2017-02-14 02:57:18 +00:00
|
|
|
// rx is a buffer of events waiting to be processed.
|
|
|
|
rx chan *Event
|
|
|
|
// tx is a buffer of events waiting to be sent.
|
|
|
|
tx chan *Event
|
2016-11-14 11:59:08 +00:00
|
|
|
|
2016-11-19 01:11:13 +00:00
|
|
|
// state represents the throw-away state for the irc session.
|
2016-11-14 11:59:08 +00:00
|
|
|
state *state
|
2016-11-14 11:50:14 +00:00
|
|
|
// initTime represents the creation time of the client.
|
|
|
|
initTime time.Time
|
2016-11-19 01:11:13 +00:00
|
|
|
|
2017-02-12 03:51:05 +00:00
|
|
|
// Handlers is a handler which manages internal and external handlers.
|
|
|
|
Handlers *Caller
|
2016-12-26 08:06:15 +00:00
|
|
|
// CTCP is a handler which manages internal and external CTCP handlers.
|
2017-02-13 12:56:00 +00:00
|
|
|
CTCP *CTCP
|
2017-06-15 08:46:10 +00:00
|
|
|
// Cmd contains various helper methods to interact with the server.
|
|
|
|
Cmd *Commands
|
2016-11-19 01:11:13 +00:00
|
|
|
|
2017-05-07 16:39:16 +00:00
|
|
|
// mu is the mux used for connections/disconnections from the server,
|
2017-02-12 03:00:01 +00:00
|
|
|
// so multiple threads aren't trying to connect at the same time, and
|
|
|
|
// vice versa.
|
2017-07-18 23:27:45 +00:00
|
|
|
mu sync.RWMutex
|
2017-05-07 16:39:16 +00:00
|
|
|
|
|
|
|
// stop is used to communicate with Connect(), letting it know that the
|
|
|
|
// client wishes to cancel/close.
|
|
|
|
stop context.CancelFunc
|
|
|
|
|
|
|
|
// conn is a net.Conn reference to the IRC server. If this is nil, it is
|
|
|
|
// safe to assume that we're not connected. If this is not nil, this
|
|
|
|
// means we're either connected, connecting, or cleaning up. This should
|
|
|
|
// be guarded with Client.mu.
|
|
|
|
conn *ircConn
|
2017-02-12 03:00:01 +00:00
|
|
|
|
2017-02-03 13:54:44 +00:00
|
|
|
// debug is used if a writer is supplied for Client.Config.Debugger.
|
|
|
|
debug *log.Logger
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains configuration options for an IRC client
|
|
|
|
type Config struct {
|
2017-02-21 02:03:35 +00:00
|
|
|
// Server is a host/ip of the server you want to connect to. This only
|
|
|
|
// has an affect during the dial process
|
2016-11-14 11:50:14 +00:00
|
|
|
Server string
|
2017-04-23 16:16:44 +00:00
|
|
|
// ServerPass is the server password used to authenticate. This only has
|
|
|
|
// an affect during the dial process.
|
|
|
|
ServerPass string
|
2017-02-21 02:03:35 +00:00
|
|
|
// Port is the port that will be used during server connection. This only
|
|
|
|
// has an affect during the dial process.
|
2016-11-14 11:50:14 +00:00
|
|
|
Port int
|
2017-02-21 02:03:35 +00:00
|
|
|
// Nick is an rfc-valid nickname used during connection. This only has an
|
|
|
|
// affect during the dial process.
|
2016-11-14 11:50:14 +00:00
|
|
|
Nick string
|
2017-02-21 02:03:35 +00:00
|
|
|
// User is the username/ident to use on connect. Ignored if an identd
|
|
|
|
// server is used. This only has an affect during the dial process.
|
2016-11-14 11:50:14 +00:00
|
|
|
User string
|
2017-02-21 02:03:35 +00:00
|
|
|
// Name is the "realname" that's used during connection. This only has an
|
|
|
|
// affect during the dial process.
|
2016-11-14 11:50:14 +00:00
|
|
|
Name string
|
2017-04-23 18:03:12 +00:00
|
|
|
// SASL contains the necessary authentication data to authenticate
|
2017-06-15 07:29:52 +00:00
|
|
|
// with SASL. See the documentation for SASLMech for what is currently
|
2017-06-14 11:19:31 +00:00
|
|
|
// supported. Capability tracking must be enabled for this to work, as
|
|
|
|
// this requires IRCv3 CAP handling.
|
2017-06-15 07:29:52 +00:00
|
|
|
SASL SASLMech
|
2017-02-12 06:39:55 +00:00
|
|
|
// Proxy is a proxy based address, used during the dial process when
|
2017-02-21 02:03:35 +00:00
|
|
|
// connecting to the server. This only has an affect during the dial
|
|
|
|
// process. Currently, x/net/proxy only supports socks5, however you can
|
|
|
|
// add your own proxy functionality using:
|
2017-02-12 06:39:55 +00:00
|
|
|
// proxy.RegisterDialerType
|
|
|
|
//
|
|
|
|
// Examples of how Proxy may be used:
|
|
|
|
// socks5://localhost:8080
|
|
|
|
// socks5://1.2.3.4:8888
|
|
|
|
// customProxy://example.com:8000
|
|
|
|
//
|
|
|
|
Proxy string
|
2017-02-21 02:03:35 +00:00
|
|
|
// Bind is used to bind to a specific host or ip during the dial process
|
|
|
|
// when connecting to the server. This can be a hostname, however it must
|
|
|
|
// resolve to an IPv4/IPv6 address bindable on your system. Otherwise,
|
|
|
|
// you can simply use a IPv4/IPv6 address directly. This only has an
|
|
|
|
// affect during the dial process.
|
2017-02-12 06:39:55 +00:00
|
|
|
Bind string
|
2017-02-21 02:03:35 +00:00
|
|
|
// SSL allows dialing via TLS. See TLSConfig to set your own TLS
|
|
|
|
// configuration (e.g. to not force hostname checking). This only has an
|
|
|
|
// affect during the dial process.
|
2017-02-12 06:39:55 +00:00
|
|
|
SSL bool
|
2016-11-14 11:50:14 +00:00
|
|
|
// TLSConfig is an optional user-supplied tls configuration, used during
|
2017-02-12 06:39:55 +00:00
|
|
|
// socket creation to the server. SSL must be enabled for this to be used.
|
2017-02-21 02:03:35 +00:00
|
|
|
// This only has an affect during the dial process.
|
2016-11-14 11:50:14 +00:00
|
|
|
TLSConfig *tls.Config
|
2017-02-12 03:00:01 +00:00
|
|
|
// AllowFlood allows the client to bypass the rate limit of outbound
|
|
|
|
// messages.
|
|
|
|
AllowFlood bool
|
2017-06-07 08:39:52 +00:00
|
|
|
// GlobalFormat enables passing through all events which have trailing
|
2017-06-07 10:35:02 +00:00
|
|
|
// text through the color Fmt() function, so you don't have to wrap
|
|
|
|
// every response in the Fmt() method.
|
2017-06-07 08:39:52 +00:00
|
|
|
//
|
|
|
|
// Note that this only actually applies to PRIVMSG, NOTICE and TOPIC
|
|
|
|
// events, to ensure it doesn't clobber unwanted events.
|
|
|
|
GlobalFormat bool
|
2017-02-14 07:14:38 +00:00
|
|
|
// Debug is an optional, user supplied location to log the raw lines
|
2017-02-03 13:54:44 +00:00
|
|
|
// sent from the server, or other useful debug logs. Defaults to
|
2017-02-13 06:15:53 +00:00
|
|
|
// ioutil.Discard. For quick debugging, this could be set to os.Stdout.
|
2017-02-14 07:14:38 +00:00
|
|
|
Debug io.Writer
|
2017-07-03 11:34:34 +00:00
|
|
|
// Out is used to write out a prettified version of incoming events. For
|
|
|
|
// example, channel JOIN/PART, PRIVMSG/NOTICE, KICk, etc. Useful to get
|
|
|
|
// a brief output of the activity of the client. If you are looking to
|
|
|
|
// log raw messages, look at a handler and girc.ALLEVENTS and the relevant
|
|
|
|
// Event.Bytes() or Event.String() methods.
|
2017-02-14 07:14:38 +00:00
|
|
|
Out io.Writer
|
2017-02-13 06:15:53 +00:00
|
|
|
// RecoverFunc is called when a handler throws a panic. If RecoverFunc is
|
2017-02-13 09:42:45 +00:00
|
|
|
// set, the panic will be considered recovered, otherwise the client will
|
|
|
|
// panic. Set this to DefaultRecoverHandler if you don't want the client
|
|
|
|
// to panic, however you don't want to handle the panic yourself.
|
2017-02-14 07:14:38 +00:00
|
|
|
// DefaultRecoverHandler will log the panic to Debug or os.Stdout if
|
|
|
|
// Debug is unset.
|
2017-02-13 06:15:53 +00:00
|
|
|
RecoverFunc func(c *Client, e *HandlerError)
|
2017-01-06 14:00:29 +00:00
|
|
|
// SupportedCaps are the IRCv3 capabilities you would like the client to
|
2017-07-03 11:34:34 +00:00
|
|
|
// support on top of the ones which the client already supports (see
|
|
|
|
// cap.go for which ones the client enables by default). Only use this
|
|
|
|
// if you have not called DisableTracking(). The keys value gets passed
|
|
|
|
// to the server if supported.
|
2017-01-19 11:58:08 +00:00
|
|
|
SupportedCaps map[string][]string
|
2017-01-12 07:34:29 +00:00
|
|
|
// Version is the application version information that will be used in
|
|
|
|
// response to a CTCP VERSION, if default CTCP replies have not been
|
|
|
|
// overwritten or a VERSION handler was already supplied.
|
|
|
|
Version string
|
2017-06-26 03:38:50 +00:00
|
|
|
// PingDelay is the frequency between when the client sends a keep-alive
|
|
|
|
// PING to the server, and awaits a response (and times out if the server
|
|
|
|
// doesn't respond in time). This should be between 20-600 seconds. See
|
2017-02-21 02:03:35 +00:00
|
|
|
// Client.Lag() if you want to determine the delay between the server
|
2017-06-26 03:38:50 +00:00
|
|
|
// and the client. If this is set to -1, the client will not attempt to
|
|
|
|
// send client -> server PING requests.
|
2017-02-14 07:14:38 +00:00
|
|
|
PingDelay time.Duration
|
2017-02-08 07:55:38 +00:00
|
|
|
|
|
|
|
// disableTracking disables all channel and user-level tracking. Useful
|
2017-07-03 11:34:34 +00:00
|
|
|
// for highly embedded scripts with single purposes. This has an exported
|
|
|
|
// method which enables this and ensures prop cleanup, see
|
|
|
|
// Client.DisableTracking().
|
2017-02-08 07:55:38 +00:00
|
|
|
disableTracking bool
|
2017-02-21 02:03:35 +00:00
|
|
|
// HandleNickCollide when set, allows the client to handle nick collisions
|
|
|
|
// in a custom way. If unset, the client will attempt to append a
|
|
|
|
// underscore to the end of the nickname, in order to bypass using
|
|
|
|
// an invalid nickname. For example, if "test" is already in use, or is
|
|
|
|
// blocked by the network/a service, the client will try and use "test_",
|
|
|
|
// then it will attempt "test__", "test___", and so on.
|
|
|
|
HandleNickCollide func(oldNick string) (newNick string)
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 05:24:23 +00:00
|
|
|
// isValid checks some basic settings to ensure the config is valid.
|
2017-07-18 08:56:51 +00:00
|
|
|
func (conf *Config) isValid() error {
|
2017-02-22 05:24:23 +00:00
|
|
|
if conf.Server == "" {
|
|
|
|
return errors.New("invalid server specified")
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:59:21 +00:00
|
|
|
// Default port to 6667 (the standard IRC port).
|
|
|
|
if conf.Port == 0 {
|
|
|
|
conf.Port = 6667
|
|
|
|
}
|
|
|
|
|
2017-02-22 05:24:23 +00:00
|
|
|
if conf.Port < 21 || conf.Port > 65535 {
|
|
|
|
return errors.New("invalid port (21-65535)")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidNick(conf.Nick) || !IsValidUser(conf.User) {
|
|
|
|
return errors.New("invalid nickname or user")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-25 00:18:39 +00:00
|
|
|
// ErrNotConnected is returned if a method is used when the client isn't
|
|
|
|
// connected.
|
2016-12-07 00:42:00 +00:00
|
|
|
var ErrNotConnected = errors.New("client is not connected to server")
|
2016-11-25 00:18:39 +00:00
|
|
|
|
2017-02-13 08:11:50 +00:00
|
|
|
// ErrDisconnected is called when Config.Retries is less than 1, and we
|
|
|
|
// non-intentionally disconnected from the server.
|
2017-02-08 08:08:13 +00:00
|
|
|
var ErrDisconnected = errors.New("unexpectedly disconnected")
|
|
|
|
|
2016-12-07 00:17:35 +00:00
|
|
|
// ErrInvalidTarget should be returned if the target which you are
|
|
|
|
// attempting to send an event to is invalid or doesn't match RFC spec.
|
|
|
|
type ErrInvalidTarget struct {
|
|
|
|
Target string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrInvalidTarget) Error() string { return "invalid target: " + e.Target }
|
2016-11-25 00:18:39 +00:00
|
|
|
|
2016-12-09 10:58:18 +00:00
|
|
|
// New creates a new IRC client with the specified server, name and config.
|
2016-11-13 08:30:43 +00:00
|
|
|
func New(config Config) *Client {
|
2017-02-12 03:00:01 +00:00
|
|
|
c := &Client{
|
2017-02-08 07:55:38 +00:00
|
|
|
Config: config,
|
2017-02-14 03:21:04 +00:00
|
|
|
rx: make(chan *Event, 25),
|
2017-02-14 02:57:18 +00:00
|
|
|
tx: make(chan *Event, 25),
|
2017-02-05 11:09:35 +00:00
|
|
|
CTCP: newCTCP(),
|
|
|
|
initTime: time.Now(),
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 08:46:10 +00:00
|
|
|
c.Cmd = &Commands{c: c}
|
2017-02-13 12:52:29 +00:00
|
|
|
|
2017-06-26 03:38:50 +00:00
|
|
|
if c.Config.PingDelay >= 0 && c.Config.PingDelay < (20*time.Second) {
|
2017-02-14 07:14:38 +00:00
|
|
|
c.Config.PingDelay = 20 * time.Second
|
|
|
|
} else if c.Config.PingDelay > (600 * time.Second) {
|
|
|
|
c.Config.PingDelay = 600 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Config.Debug == nil {
|
2017-02-13 06:27:57 +00:00
|
|
|
c.debug = log.New(ioutil.Discard, "", 0)
|
|
|
|
} else {
|
2017-02-14 07:14:38 +00:00
|
|
|
c.debug = log.New(c.Config.Debug, "debug:", log.Ltime|log.Lshortfile)
|
2017-02-13 06:27:57 +00:00
|
|
|
c.debug.Print("initializing debugging")
|
2017-02-03 13:54:44 +00:00
|
|
|
}
|
2017-02-05 11:09:35 +00:00
|
|
|
|
|
|
|
// Setup the caller.
|
2017-02-12 03:51:05 +00:00
|
|
|
c.Handlers = newCaller(c.debug)
|
2017-01-12 09:19:35 +00:00
|
|
|
|
2016-12-09 10:37:01 +00:00
|
|
|
// Give ourselves a new state.
|
2017-04-24 15:55:30 +00:00
|
|
|
c.state = &state{}
|
2017-07-04 05:29:22 +00:00
|
|
|
c.state.reset()
|
2016-12-09 10:37:01 +00:00
|
|
|
|
2017-01-04 02:13:05 +00:00
|
|
|
// Register builtin handlers.
|
2017-02-12 04:54:42 +00:00
|
|
|
c.registerBuiltins()
|
2016-11-13 08:30:43 +00:00
|
|
|
|
2016-12-26 08:51:42 +00:00
|
|
|
// Register default CTCP responses.
|
2017-02-12 03:00:01 +00:00
|
|
|
c.CTCP.addDefaultHandlers()
|
2016-12-26 08:51:42 +00:00
|
|
|
|
2017-02-12 03:00:01 +00:00
|
|
|
return c
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 19:31:57 +00:00
|
|
|
// String returns a brief description of the current client state.
|
|
|
|
func (c *Client) String() string {
|
2017-04-24 15:42:29 +00:00
|
|
|
connected := c.IsConnected()
|
2017-02-12 19:31:57 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf(
|
2017-03-31 18:30:52 +00:00
|
|
|
"<Client init:%q handlers:%d connected:%t>", c.initTime.String(), c.Handlers.Len(), connected,
|
2017-02-12 19:31:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-07 16:39:16 +00:00
|
|
|
// Close closes the network connection to the server, and sends a STOPPED
|
|
|
|
// event. This should cause Connect() to return with nil. This should be
|
2017-07-03 11:34:34 +00:00
|
|
|
// safe to call multiple times. See Connect()'s documentation on how
|
|
|
|
// handlers and goroutines are handled when disconnected from the server.
|
2017-05-07 16:39:16 +00:00
|
|
|
func (c *Client) Close() {
|
2017-04-18 15:06:19 +00:00
|
|
|
c.RunHandlers(&Event{Command: STOPPED, Trailing: c.Server()})
|
|
|
|
|
2017-07-18 23:27:45 +00:00
|
|
|
c.mu.RLock()
|
2017-05-07 16:39:16 +00:00
|
|
|
if c.stop != nil {
|
|
|
|
c.debug.Print("requesting client to stop")
|
|
|
|
c.stop()
|
2017-03-31 18:30:52 +00:00
|
|
|
}
|
2017-07-18 23:27:45 +00:00
|
|
|
c.mu.RUnlock()
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 22:16:54 +00:00
|
|
|
func (c *Client) execLoop(ctx context.Context, wg *sync.WaitGroup) {
|
2017-04-18 15:06:19 +00:00
|
|
|
c.debug.Print("starting execLoop")
|
|
|
|
defer c.debug.Print("closing execLoop")
|
|
|
|
|
2017-07-03 11:34:34 +00:00
|
|
|
var event *Event
|
|
|
|
|
2016-11-13 08:30:43 +00:00
|
|
|
for {
|
|
|
|
select {
|
2017-07-18 22:16:54 +00:00
|
|
|
case <-ctx.Done():
|
2017-07-03 11:34:34 +00:00
|
|
|
// We've been told to exit, however we shouldn't bail on the
|
|
|
|
// current events in the queue that should be processed, as one
|
|
|
|
// may want to handle an ERROR, QUIT, etc.
|
2017-07-18 23:27:45 +00:00
|
|
|
c.debug.Printf("received signal to close, flushing %d events and executing", len(c.rx))
|
2017-07-03 11:34:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event = <-c.rx:
|
|
|
|
c.RunHandlers(event)
|
|
|
|
default:
|
|
|
|
goto done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2017-04-18 15:06:19 +00:00
|
|
|
wg.Done()
|
2017-02-12 03:00:01 +00:00
|
|
|
return
|
2017-07-03 11:34:34 +00:00
|
|
|
case event = <-c.rx:
|
2017-04-18 15:06:19 +00:00
|
|
|
c.RunHandlers(event)
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 16:39:16 +00:00
|
|
|
// DisableTracking disables all channel/user-level/CAP tracking, and clears
|
2017-02-12 06:39:55 +00:00
|
|
|
// all internal handlers. Useful for highly embedded scripts with single
|
2017-05-07 16:39:16 +00:00
|
|
|
// purposes. This cannot be un-done on a client.
|
2017-02-12 06:39:55 +00:00
|
|
|
func (c *Client) DisableTracking() {
|
|
|
|
c.debug.Print("disabling tracking")
|
|
|
|
c.Config.disableTracking = true
|
|
|
|
c.Handlers.clearInternal()
|
2017-04-18 07:17:15 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.Lock()
|
2017-02-12 06:39:55 +00:00
|
|
|
c.state.channels = nil
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.Unlock()
|
|
|
|
c.state.notify(c, UPDATE_STATE)
|
2017-04-18 07:17:15 +00:00
|
|
|
|
2017-02-12 06:39:55 +00:00
|
|
|
c.registerBuiltins()
|
|
|
|
}
|
|
|
|
|
2016-12-13 15:24:51 +00:00
|
|
|
// Server returns the string representation of host+port pair for net.Conn.
|
|
|
|
func (c *Client) Server() string {
|
2017-02-08 07:55:38 +00:00
|
|
|
return fmt.Sprintf("%s:%d", c.Config.Server, c.Config.Port)
|
2016-12-13 15:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lifetime returns the amount of time that has passed since the client was
|
|
|
|
// created.
|
|
|
|
func (c *Client) Lifetime() time.Duration {
|
|
|
|
return time.Since(c.initTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uptime is the time at which the client successfully connected to the
|
|
|
|
// server.
|
|
|
|
func (c *Client) Uptime() (up *time.Time, err error) {
|
|
|
|
if !c.IsConnected() {
|
|
|
|
return nil, ErrNotConnected
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:04:12 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
c.conn.mu.RLock()
|
2017-02-12 07:22:41 +00:00
|
|
|
up = c.conn.connTime
|
2017-07-19 00:04:12 +00:00
|
|
|
c.conn.mu.RUnlock()
|
|
|
|
c.mu.RUnlock()
|
2016-12-13 15:24:51 +00:00
|
|
|
|
|
|
|
return up, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnSince is the duration that has past since the client successfully
|
|
|
|
// connected to the server.
|
|
|
|
func (c *Client) ConnSince() (since *time.Duration, err error) {
|
|
|
|
if !c.IsConnected() {
|
|
|
|
return nil, ErrNotConnected
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:04:12 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
c.conn.mu.RLock()
|
2017-02-12 07:22:41 +00:00
|
|
|
timeSince := time.Since(*c.conn.connTime)
|
2017-07-19 00:04:12 +00:00
|
|
|
c.conn.mu.RUnlock()
|
|
|
|
c.mu.RUnlock()
|
2016-12-13 15:24:51 +00:00
|
|
|
|
|
|
|
return &timeSince, nil
|
|
|
|
}
|
|
|
|
|
2016-11-14 11:50:14 +00:00
|
|
|
// IsConnected returns true if the client is connected to the server.
|
2016-12-09 11:30:06 +00:00
|
|
|
func (c *Client) IsConnected() (connected bool) {
|
2017-07-19 00:04:12 +00:00
|
|
|
c.mu.RLock()
|
2017-02-12 07:22:41 +00:00
|
|
|
if c.conn == nil {
|
2017-07-19 00:04:12 +00:00
|
|
|
c.mu.RUnlock()
|
2017-02-12 07:22:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2017-04-24 15:42:29 +00:00
|
|
|
|
2017-07-19 00:04:12 +00:00
|
|
|
c.conn.mu.RLock()
|
|
|
|
connected = c.conn.connected
|
|
|
|
c.conn.mu.RUnlock()
|
|
|
|
c.mu.RUnlock()
|
2017-04-24 15:42:29 +00:00
|
|
|
|
2017-07-19 00:04:12 +00:00
|
|
|
return connected
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 04:02:45 +00:00
|
|
|
// GetNick returns the current nickname of the active connection. Panics if
|
|
|
|
// tracking is disabled.
|
|
|
|
func (c *Client) GetNick() string {
|
|
|
|
c.panicIfNotTracking()
|
2016-11-14 11:50:14 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
|
|
|
defer c.state.RUnlock()
|
2017-04-24 15:22:45 +00:00
|
|
|
|
2016-11-14 11:59:08 +00:00
|
|
|
if c.state.nick == "" {
|
2017-04-24 15:22:45 +00:00
|
|
|
return c.Config.Nick
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 15:22:45 +00:00
|
|
|
return c.state.nick
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 04:02:45 +00:00
|
|
|
// GetIdent returns the current ident of the active connection. Panics if
|
|
|
|
// tracking is disabled. May be empty, as this is obtained from when we join
|
|
|
|
// a channel, as there is no other more efficient method to return this info.
|
|
|
|
func (c *Client) GetIdent() string {
|
|
|
|
c.panicIfNotTracking()
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
|
|
|
defer c.state.RUnlock()
|
2017-04-24 15:22:45 +00:00
|
|
|
|
2017-02-21 04:02:45 +00:00
|
|
|
if c.state.ident == "" {
|
2017-04-24 15:22:45 +00:00
|
|
|
return c.Config.User
|
2017-02-21 04:02:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 15:22:45 +00:00
|
|
|
return c.state.ident
|
2017-02-21 04:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHost returns the current host of the active connection. Panics if
|
|
|
|
// tracking is disabled. May be empty, as this is obtained from when we join
|
|
|
|
// a channel, as there is no other more efficient method to return this info.
|
|
|
|
func (c *Client) GetHost() string {
|
|
|
|
c.panicIfNotTracking()
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
|
|
|
defer c.state.RUnlock()
|
2017-02-21 04:02:45 +00:00
|
|
|
|
2017-04-24 15:22:45 +00:00
|
|
|
return c.state.host
|
2017-02-21 04:02:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 10:22:06 +00:00
|
|
|
// Channels returns the active list of channels that the client is in.
|
2017-02-05 12:09:48 +00:00
|
|
|
// Panics if tracking is disabled.
|
2016-12-10 10:22:06 +00:00
|
|
|
func (c *Client) Channels() []string {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2016-12-10 10:22:06 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
2017-07-04 05:29:22 +00:00
|
|
|
channels := make([]string, len(c.state.channels))
|
2016-12-10 10:22:06 +00:00
|
|
|
var i int
|
|
|
|
for channel := range c.state.channels {
|
|
|
|
channels[i] = channel
|
|
|
|
i++
|
|
|
|
}
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RUnlock()
|
2017-07-04 05:29:22 +00:00
|
|
|
sort.Strings(channels)
|
2016-11-13 08:30:43 +00:00
|
|
|
|
2016-12-10 10:22:06 +00:00
|
|
|
return channels
|
2016-11-13 08:30:43 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 05:29:22 +00:00
|
|
|
// Users returns the active list of users that the client is tracking across
|
|
|
|
// all files. Panics if tracking is disabled.
|
|
|
|
func (c *Client) Users() []string {
|
|
|
|
c.panicIfNotTracking()
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
2017-07-04 05:29:22 +00:00
|
|
|
users := make([]string, len(c.state.users))
|
|
|
|
var i int
|
|
|
|
for user := range c.state.users {
|
|
|
|
users[i] = user
|
|
|
|
i++
|
|
|
|
}
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RUnlock()
|
2017-07-04 05:29:22 +00:00
|
|
|
sort.Strings(users)
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupChannel looks up a given channel in state. If the channel doesn't
|
|
|
|
// exist, nil is returned. Panics if tracking is disabled.
|
|
|
|
func (c *Client) LookupChannel(name string) *Channel {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-07-04 05:29:22 +00:00
|
|
|
if name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
|
|
|
defer c.state.RUnlock()
|
2017-02-14 12:33:05 +00:00
|
|
|
|
2017-02-17 10:30:29 +00:00
|
|
|
channel := c.state.lookupChannel(name)
|
2017-02-14 12:33:05 +00:00
|
|
|
if channel == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel.Copy()
|
|
|
|
}
|
|
|
|
|
2017-07-04 05:29:22 +00:00
|
|
|
// LookupUser looks up a given user in state. If the user doesn't exist, nil
|
|
|
|
// is returned. Panics if tracking is disabled.
|
|
|
|
func (c *Client) LookupUser(nick string) *User {
|
|
|
|
c.panicIfNotTracking()
|
|
|
|
if nick == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
|
|
|
defer c.state.RUnlock()
|
2017-07-04 05:29:22 +00:00
|
|
|
|
|
|
|
user := c.state.lookupUser(nick)
|
|
|
|
if user == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.Copy()
|
|
|
|
}
|
|
|
|
|
2017-02-05 12:09:48 +00:00
|
|
|
// IsInChannel returns true if the client is in channel. Panics if tracking
|
|
|
|
// is disabled.
|
2016-12-10 09:14:03 +00:00
|
|
|
func (c *Client) IsInChannel(channel string) bool {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-02-05 12:09:48 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
2017-07-04 05:29:22 +00:00
|
|
|
_, inChannel := c.state.channels[ToRFC1459(channel)]
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RUnlock()
|
2016-12-10 09:14:03 +00:00
|
|
|
|
|
|
|
return inChannel
|
|
|
|
}
|
|
|
|
|
2017-01-06 13:30:09 +00:00
|
|
|
// GetServerOption retrieves a server capability setting that was retrieved
|
2017-01-06 13:53:41 +00:00
|
|
|
// during client connection. This is also known as ISUPPORT (or RPL_PROTOCTL).
|
|
|
|
// Will panic if used when tracking has been disabled. Examples of usage:
|
2017-01-06 13:30:09 +00:00
|
|
|
//
|
|
|
|
// nickLen, success := GetServerOption("MAXNICKLEN")
|
|
|
|
//
|
2017-02-06 08:53:05 +00:00
|
|
|
func (c *Client) GetServerOption(key string) (result string, ok bool) {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-01-06 13:30:09 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
2017-02-06 08:53:05 +00:00
|
|
|
result, ok = c.state.serverOptions[key]
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RUnlock()
|
2017-01-06 13:30:09 +00:00
|
|
|
|
2017-02-06 08:53:05 +00:00
|
|
|
return result, ok
|
2017-01-06 13:30:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerName returns the server host/name that the server itself identifies
|
2017-01-06 13:40:01 +00:00
|
|
|
// as. May be empty if the server does not support RPL_MYINFO. Will panic if
|
|
|
|
// used when tracking has been disabled.
|
2017-01-06 13:30:09 +00:00
|
|
|
func (c *Client) ServerName() (name string) {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-01-06 13:40:01 +00:00
|
|
|
|
2017-08-09 06:28:46 +00:00
|
|
|
name, _ = c.GetServerOption("NETWORK")
|
2017-01-06 13:30:09 +00:00
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkName returns the network identifier. E.g. "EsperNet", "ByteIRC".
|
2017-01-06 13:53:41 +00:00
|
|
|
// May be empty if the server does not support RPL_ISUPPORT (or RPL_PROTOCTL).
|
|
|
|
// Will panic if used when tracking has been disabled.
|
2017-01-06 13:30:09 +00:00
|
|
|
func (c *Client) NetworkName() (name string) {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-01-06 13:40:01 +00:00
|
|
|
|
2017-01-06 13:30:09 +00:00
|
|
|
name, _ = c.GetServerOption("NETWORK")
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerVersion returns the server software version, if the server has
|
|
|
|
// supplied this information during connection. May be empty if the server
|
2017-01-06 13:40:01 +00:00
|
|
|
// does not support RPL_MYINFO. Will panic if used when tracking has been
|
|
|
|
// disabled.
|
2017-01-06 13:30:09 +00:00
|
|
|
func (c *Client) ServerVersion() (version string) {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-01-06 13:40:01 +00:00
|
|
|
|
2017-01-06 13:30:09 +00:00
|
|
|
version, _ = c.GetServerOption("VERSION")
|
|
|
|
|
|
|
|
return version
|
|
|
|
}
|
2017-01-06 13:40:01 +00:00
|
|
|
|
|
|
|
// ServerMOTD returns the servers message of the day, if the server has sent
|
2017-01-06 13:53:41 +00:00
|
|
|
// it upon connect. Will panic if used when tracking has been disabled.
|
2017-01-06 13:40:01 +00:00
|
|
|
func (c *Client) ServerMOTD() (motd string) {
|
2017-02-21 04:02:45 +00:00
|
|
|
c.panicIfNotTracking()
|
2017-01-06 13:53:41 +00:00
|
|
|
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RLock()
|
2017-01-06 13:40:01 +00:00
|
|
|
motd = c.state.motd
|
2017-07-12 04:44:44 +00:00
|
|
|
c.state.RUnlock()
|
2017-01-06 13:40:01 +00:00
|
|
|
|
|
|
|
return motd
|
|
|
|
}
|
2017-02-21 01:39:16 +00:00
|
|
|
|
|
|
|
// Lag is the latency between the server and the client. This is measured by
|
|
|
|
// determining the difference in time between when we ping the server, and
|
|
|
|
// when we receive a pong.
|
|
|
|
func (c *Client) Lag() time.Duration {
|
2017-07-19 00:04:12 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
c.conn.mu.RLock()
|
2017-02-21 01:39:16 +00:00
|
|
|
delta := c.conn.lastPong.Sub(c.conn.lastPing)
|
2017-07-19 00:04:12 +00:00
|
|
|
c.conn.mu.RUnlock()
|
|
|
|
c.mu.RUnlock()
|
2017-04-24 15:42:29 +00:00
|
|
|
|
2017-02-21 01:39:16 +00:00
|
|
|
if delta < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta
|
|
|
|
}
|
2017-02-21 04:02:45 +00:00
|
|
|
|
|
|
|
// panicIfNotTracking will throw a panic when it's called, and tracking is
|
|
|
|
// disabled. Adds useful info like what function specifically, and where it
|
|
|
|
// was called from.
|
|
|
|
func (c *Client) panicIfNotTracking() {
|
|
|
|
if !c.Config.disableTracking {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, _, _, _ := runtime.Caller(1)
|
|
|
|
fn := runtime.FuncForPC(pc)
|
|
|
|
_, file, line, _ := runtime.Caller(2)
|
|
|
|
|
|
|
|
panic(fmt.Sprintf("%s used when tracking is disabled (caller %s:%d)", fn.Name(), file, line))
|
|
|
|
}
|