mirror of https://github.com/fluffle/goirc
Break out tweakable things into a Config struct.
This commit is contained in:
parent
45d7b3317f
commit
b1242aa351
|
@ -14,10 +14,18 @@ import (
|
||||||
|
|
||||||
// An IRC connection is represented by this struct.
|
// An IRC connection is represented by this struct.
|
||||||
type Conn struct {
|
type Conn struct {
|
||||||
// Connection Hostname and Nickname
|
// Connection related vars people will care about
|
||||||
Host string
|
Me *state.Nick
|
||||||
Me *state.Nick
|
Host string
|
||||||
Network string
|
Network string
|
||||||
|
Connected bool
|
||||||
|
|
||||||
|
// Deprecated: future work to turn Conn into an interface will break this.
|
||||||
|
// Use the State field to store external state that handlers might need.
|
||||||
|
State interface{}
|
||||||
|
|
||||||
|
// Contains parameters that people can tweak to change client behaviour.
|
||||||
|
cfg *Config
|
||||||
|
|
||||||
// Handlers and Commands
|
// Handlers and Commands
|
||||||
handlers *hSet
|
handlers *hSet
|
||||||
|
@ -27,21 +35,25 @@ type Conn struct {
|
||||||
st state.Tracker
|
st state.Tracker
|
||||||
stRemovers []Remover
|
stRemovers []Remover
|
||||||
|
|
||||||
// Use the State field to store external state that handlers might need.
|
|
||||||
// Remember ... you might need locking for this ;-)
|
|
||||||
State interface{}
|
|
||||||
|
|
||||||
// I/O stuff to server
|
// I/O stuff to server
|
||||||
sock net.Conn
|
sock net.Conn
|
||||||
io *bufio.ReadWriter
|
io *bufio.ReadWriter
|
||||||
in chan *Line
|
in chan *Line
|
||||||
out chan string
|
out chan string
|
||||||
Connected bool
|
|
||||||
|
|
||||||
// Control channels to goroutines
|
// Control channels to goroutines
|
||||||
cSend, cLoop, cPing chan bool
|
cSend, cLoop, cPing chan bool
|
||||||
|
|
||||||
// Misc knobs to tweak client behaviour:
|
// 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
|
||||||
|
|
||||||
// Are we connecting via SSL? Do we care about certificate validity?
|
// Are we connecting via SSL? Do we care about certificate validity?
|
||||||
SSL bool
|
SSL bool
|
||||||
SSLConfig *tls.Config
|
SSLConfig *tls.Config
|
||||||
|
@ -57,16 +69,9 @@ type Conn struct {
|
||||||
|
|
||||||
// Set this to true to disable flood protection and false to re-enable
|
// Set this to true to disable flood protection and false to re-enable
|
||||||
Flood bool
|
Flood bool
|
||||||
|
|
||||||
// Internal counters for flood protection
|
|
||||||
badness time.Duration
|
|
||||||
lastsent time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new IRC connection object, but doesn't connect to anything so
|
func NewConfig(nick string, args ...string) *Config {
|
||||||
// that you can add event handlers to it. See AddHandler() for details
|
|
||||||
func Client(nick string, args ...string) *Conn {
|
|
||||||
logging.InitFromFlags()
|
|
||||||
ident := "goirc"
|
ident := "goirc"
|
||||||
name := "Powered by GoIRC"
|
name := "Powered by GoIRC"
|
||||||
|
|
||||||
|
@ -76,7 +81,30 @@ func Client(nick string, args ...string) *Conn {
|
||||||
if len(args) > 1 && args[1] != "" {
|
if len(args) > 1 && args[1] != "" {
|
||||||
name = args[1]
|
name = args[1]
|
||||||
}
|
}
|
||||||
|
cfg := &Config{
|
||||||
|
PingFreq: 3 * time.Minute,
|
||||||
|
NewNick: func(s string) string { return s + "_" },
|
||||||
|
}
|
||||||
|
cfg.Me = state.NewNick(nick)
|
||||||
|
cfg.Me.Ident = ident
|
||||||
|
cfg.Me.Name = name
|
||||||
|
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
|
||||||
|
func SimpleClient(nick string, args ...string) (*Conn, error) {
|
||||||
|
return Client(NewConfig(nick, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Client(cfg *Config) (*Conn, error) {
|
||||||
|
logging.InitFromFlags()
|
||||||
|
if cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
|
||||||
|
return nil, fmt.Errorf("Must provide a valid state.Nick in cfg.Me.")
|
||||||
|
}
|
||||||
conn := &Conn{
|
conn := &Conn{
|
||||||
|
Me: cfg.Me,
|
||||||
|
cfg: cfg,
|
||||||
in: make(chan *Line, 32),
|
in: make(chan *Line, 32),
|
||||||
out: make(chan string, 32),
|
out: make(chan string, 32),
|
||||||
cSend: make(chan bool),
|
cSend: make(chan bool),
|
||||||
|
@ -85,17 +113,15 @@ func Client(nick string, args ...string) *Conn {
|
||||||
handlers: handlerSet(),
|
handlers: handlerSet(),
|
||||||
commands: commandSet(),
|
commands: commandSet(),
|
||||||
stRemovers: make([]Remover, 0, len(stHandlers)),
|
stRemovers: make([]Remover, 0, len(stHandlers)),
|
||||||
PingFreq: 3 * time.Minute,
|
|
||||||
NewNick: func(s string) string { return s + "_" },
|
|
||||||
lastsent: time.Now(),
|
lastsent: time.Now(),
|
||||||
}
|
}
|
||||||
conn.addIntHandlers()
|
conn.addIntHandlers()
|
||||||
conn.Me = state.NewNick(nick)
|
|
||||||
conn.Me.Ident = ident
|
|
||||||
conn.Me.Name = name
|
|
||||||
|
|
||||||
conn.initialise()
|
conn.initialise()
|
||||||
return conn
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *Conn) Config() *Config {
|
||||||
|
return conn.cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) EnableStateTracking() {
|
func (conn *Conn) EnableStateTracking() {
|
||||||
|
@ -142,12 +168,12 @@ func (conn *Conn) Connect(host string, pass ...string) error {
|
||||||
conn.Host, host))
|
conn.Host, host))
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.SSL {
|
if conn.cfg.SSL {
|
||||||
if !hasPort(host) {
|
if !hasPort(host) {
|
||||||
host += ":6697"
|
host += ":6697"
|
||||||
}
|
}
|
||||||
logging.Info("irc.Connect(): Connecting to %s with SSL.", host)
|
logging.Info("irc.Connect(): Connecting to %s with SSL.", host)
|
||||||
if s, err := tls.Dial("tcp", host, conn.SSLConfig); err == nil {
|
if s, err := tls.Dial("tcp", host, conn.cfg.SSLConfig); err == nil {
|
||||||
conn.sock = s
|
conn.sock = s
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
|
@ -182,7 +208,7 @@ func (conn *Conn) postConnect() {
|
||||||
bufio.NewWriter(conn.sock))
|
bufio.NewWriter(conn.sock))
|
||||||
go conn.send()
|
go conn.send()
|
||||||
go conn.recv()
|
go conn.recv()
|
||||||
if conn.PingFreq > 0 {
|
if conn.cfg.PingFreq > 0 {
|
||||||
go conn.ping()
|
go conn.ping()
|
||||||
} else {
|
} else {
|
||||||
// Otherwise the send in shutdown will hang :-/
|
// Otherwise the send in shutdown will hang :-/
|
||||||
|
@ -232,7 +258,7 @@ func (conn *Conn) recv() {
|
||||||
|
|
||||||
// Repeatedly pings the server every PingFreq seconds (no matter what)
|
// Repeatedly pings the server every PingFreq seconds (no matter what)
|
||||||
func (conn *Conn) ping() {
|
func (conn *Conn) ping() {
|
||||||
tick := time.NewTicker(conn.PingFreq)
|
tick := time.NewTicker(conn.cfg.PingFreq)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tick.C:
|
case <-tick.C:
|
||||||
|
@ -258,9 +284,9 @@ func (conn *Conn) runLoop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write a \r\n terminated line of output to the connected server,
|
// Write a \r\n terminated line of output to the connected server,
|
||||||
// using Hybrid's algorithm to rate limit if conn.Flood is false.
|
// using Hybrid's algorithm to rate limit if conn.cfg.Flood is false.
|
||||||
func (conn *Conn) write(line string) {
|
func (conn *Conn) write(line string) {
|
||||||
if !conn.Flood {
|
if !conn.cfg.Flood {
|
||||||
if t := conn.rateLimit(len(line)); t != 0 {
|
if t := conn.rateLimit(len(line)); t != 0 {
|
||||||
// sleep for the current line's time value before sending it
|
// sleep for the current line's time value before sending it
|
||||||
logging.Debug("irc.rateLimit(): Flood! Sleeping for %.2f secs.",
|
logging.Debug("irc.rateLimit(): Flood! Sleeping for %.2f secs.",
|
||||||
|
@ -303,7 +329,7 @@ func (conn *Conn) rateLimit(chars int) time.Duration {
|
||||||
|
|
||||||
func (conn *Conn) shutdown() {
|
func (conn *Conn) shutdown() {
|
||||||
// Guard against double-call of shutdown() if we get an error in send()
|
// Guard against double-call of shutdown() if we get an error in send()
|
||||||
// as calling sock.Close() will cause recv() to recieve EOF in readstring()
|
// as calling sock.Close() will cause recv() to receive EOF in readstring()
|
||||||
if conn.Connected {
|
if conn.Connected {
|
||||||
logging.Info("irc.shutdown(): Disconnected from server.")
|
logging.Info("irc.shutdown(): Disconnected from server.")
|
||||||
conn.dispatch(&Line{Cmd: "disconnected"})
|
conn.dispatch(&Line{Cmd: "disconnected"})
|
||||||
|
|
|
@ -21,12 +21,12 @@ func setUp(t *testing.T, start ...bool) (*Conn, *testState) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
st := state.NewMockTracker(ctrl)
|
st := state.NewMockTracker(ctrl)
|
||||||
nc := MockNetConn(t)
|
nc := MockNetConn(t)
|
||||||
c := Client("test", "test", "Testing IRC")
|
c, _ := SimpleClient("test", "test", "Testing IRC")
|
||||||
logging.SetLogLevel(logging.LogFatal)
|
logging.SetLogLevel(logging.LogFatal)
|
||||||
|
|
||||||
c.st = st
|
c.st = st
|
||||||
c.sock = nc
|
c.sock = nc
|
||||||
c.Flood = true // Tests can take a while otherwise
|
c.cfg.Flood = true // Tests can take a while otherwise
|
||||||
c.Connected = true
|
c.Connected = true
|
||||||
if len(start) == 0 {
|
if len(start) == 0 {
|
||||||
// Hack to allow tests of send, recv, write etc.
|
// Hack to allow tests of send, recv, write etc.
|
||||||
|
@ -82,7 +82,7 @@ func TestEOF(t *testing.T) {
|
||||||
func TestClientAndStateTracking(t *testing.T) {
|
func TestClientAndStateTracking(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
st := state.NewMockTracker(ctrl)
|
st := state.NewMockTracker(ctrl)
|
||||||
c := Client("test", "test", "Testing IRC")
|
c, _ := SimpleClient("test", "test", "Testing IRC")
|
||||||
|
|
||||||
// Assert some basic things about the initial state of the Conn struct
|
// Assert some basic things about the initial state of the Conn struct
|
||||||
if c.Me.Nick != "test" || c.Me.Ident != "test" ||
|
if c.Me.Nick != "test" || c.Me.Ident != "test" ||
|
||||||
|
@ -278,7 +278,7 @@ func TestPing(t *testing.T) {
|
||||||
defer s.ctrl.Finish()
|
defer s.ctrl.Finish()
|
||||||
|
|
||||||
// Set a low ping frequency for testing.
|
// Set a low ping frequency for testing.
|
||||||
c.PingFreq = 50 * time.Millisecond
|
c.cfg.PingFreq = 50 * time.Millisecond
|
||||||
|
|
||||||
// reader is a helper to do a "non-blocking" read of c.out
|
// reader is a helper to do a "non-blocking" read of c.out
|
||||||
reader := func() string {
|
reader := func() string {
|
||||||
|
@ -429,13 +429,13 @@ func TestWrite(t *testing.T) {
|
||||||
c.write("yo momma")
|
c.write("yo momma")
|
||||||
s.nc.Expect("yo momma")
|
s.nc.Expect("yo momma")
|
||||||
|
|
||||||
// Flood control is disabled -- setUp sets c.Flood = true -- so we should
|
// Flood control is disabled -- setUp sets c.cfg.Flood = true -- so we should
|
||||||
// not have set c.badness at this point.
|
// not have set c.badness at this point.
|
||||||
if c.badness != 0 {
|
if c.badness != 0 {
|
||||||
t.Errorf("Flood control used when Flood = true.")
|
t.Errorf("Flood control used when Flood = true.")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Flood = false
|
c.cfg.Flood = false
|
||||||
c.write("she so useless")
|
c.write("she so useless")
|
||||||
s.nc.Expect("she so useless")
|
s.nc.Expect("she so useless")
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (conn *Conn) h_001(line *Line) {
|
||||||
// Handler to deal with "433 :Nickname already in use"
|
// Handler to deal with "433 :Nickname already in use"
|
||||||
func (conn *Conn) h_433(line *Line) {
|
func (conn *Conn) h_433(line *Line) {
|
||||||
// Args[1] is the new nick we were attempting to acquire
|
// Args[1] is the new nick we were attempting to acquire
|
||||||
neu := conn.NewNick(line.Args[1])
|
neu := conn.cfg.NewNick(line.Args[1])
|
||||||
conn.Nick(neu)
|
conn.Nick(neu)
|
||||||
// if this is happening before we're properly connected (i.e. the nick
|
// if this is happening before we're properly connected (i.e. the nick
|
||||||
// we sent in the initial NICK command is in use) we will not receive
|
// we sent in the initial NICK command is in use) we will not receive
|
||||||
|
@ -87,7 +87,7 @@ func (conn *Conn) h_NICK(line *Line) {
|
||||||
// Handle PRIVMSGs that trigger Commands
|
// Handle PRIVMSGs that trigger Commands
|
||||||
func (conn *Conn) h_PRIVMSG(line *Line) {
|
func (conn *Conn) h_PRIVMSG(line *Line) {
|
||||||
txt := line.Args[1]
|
txt := line.Args[1]
|
||||||
if conn.CommandStripNick && strings.HasPrefix(txt, conn.Me.Nick) {
|
if conn.cfg.CommandStripNick && strings.HasPrefix(txt, conn.Me.Nick) {
|
||||||
// Look for '^${nick}[:;>,-]? '
|
// Look for '^${nick}[:;>,-]? '
|
||||||
l := len(conn.Me.Nick)
|
l := len(conn.Me.Nick)
|
||||||
switch txt[l] {
|
switch txt[l] {
|
||||||
|
@ -102,7 +102,7 @@ func (conn *Conn) h_PRIVMSG(line *Line) {
|
||||||
if cmd == nil {
|
if cmd == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if conn.CommandStripPrefix {
|
if conn.cfg.CommandStripPrefix {
|
||||||
txt = strings.TrimSpace(txt[l:])
|
txt = strings.TrimSpace(txt[l:])
|
||||||
}
|
}
|
||||||
if txt != line.Args[1] {
|
if txt != line.Args[1] {
|
||||||
|
|
|
@ -157,26 +157,26 @@ func TestPRIVMSG(t *testing.T) {
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
||||||
s.nc.ExpectNothing()
|
s.nc.ExpectNothing()
|
||||||
|
|
||||||
c.CommandStripNick = true
|
c.cfg.CommandStripNick = true
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
||||||
s.nc.Expect("PRIVMSG #foo :prefix bar")
|
s.nc.Expect("PRIVMSG #foo :prefix bar")
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
||||||
s.nc.Expect("PRIVMSG #foo :prefix bar")
|
s.nc.Expect("PRIVMSG #foo :prefix bar")
|
||||||
|
|
||||||
c.CommandStripPrefix = true
|
c.cfg.CommandStripPrefix = true
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
||||||
s.nc.Expect("PRIVMSG #foo :bar")
|
s.nc.Expect("PRIVMSG #foo :bar")
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
||||||
s.nc.Expect("PRIVMSG #foo :bar")
|
s.nc.Expect("PRIVMSG #foo :bar")
|
||||||
|
|
||||||
c.CommandStripNick = false
|
c.cfg.CommandStripNick = false
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :prefix bar"))
|
||||||
s.nc.Expect("PRIVMSG #foo :bar")
|
s.nc.Expect("PRIVMSG #foo :bar")
|
||||||
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
c.h_PRIVMSG(parseLine(":blah!moo@cows.com PRIVMSG #foo :test: prefix bar"))
|
||||||
s.nc.ExpectNothing()
|
s.nc.ExpectNothing()
|
||||||
|
|
||||||
// Check the various nick addressing notations that are supported.
|
// Check the various nick addressing notations that are supported.
|
||||||
c.CommandStripNick = true
|
c.cfg.CommandStripNick = true
|
||||||
for _, addr := range []string{":", ";", ",", ">", "-", ""} {
|
for _, addr := range []string{":", ";", ",", ">", "-", ""} {
|
||||||
c.h_PRIVMSG(parseLine(fmt.Sprintf(
|
c.h_PRIVMSG(parseLine(fmt.Sprintf(
|
||||||
":blah!moo@cows.com PRIVMSG #foo :test%s prefix bar", addr)))
|
":blah!moo@cows.com PRIVMSG #foo :test%s prefix bar", addr)))
|
||||||
|
|
Loading…
Reference in New Issue