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"
|
|
|
|
"os"
|
|
|
|
"net"
|
2010-08-30 11:16:20 +00:00
|
|
|
"crypto/tls"
|
2009-12-17 21:30:18 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2009-12-19 13:40:50 +00:00
|
|
|
"time"
|
2009-11-29 20:23:15 +00:00
|
|
|
)
|
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
const (
|
|
|
|
second = int64(1e9)
|
|
|
|
)
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// An IRC connection is represented by this struct. Once connected, any errors
|
|
|
|
// encountered are piped down *Conn.Err; this channel is closed on disconnect.
|
2009-12-17 17:22:31 +00:00
|
|
|
type Conn struct {
|
2009-12-18 22:39:22 +00:00
|
|
|
// Connection Hostname and Nickname
|
2010-11-04 00:25:46 +00:00
|
|
|
Host string
|
|
|
|
Me *Nick
|
2010-11-02 21:47:05 +00:00
|
|
|
Network string
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2010-11-23 22:18:08 +00:00
|
|
|
// Event handler mapping
|
|
|
|
events map[string][]func(*Conn, *Line)
|
|
|
|
// Map of channels we're on
|
|
|
|
chans map[string]*Channel
|
|
|
|
// Map of nicks we know about
|
|
|
|
nicks map[string]*Nick
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// I/O stuff to server
|
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
|
|
|
|
connected bool
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
// Control channels to goroutines
|
|
|
|
cSend, cLoop chan bool
|
|
|
|
|
2010-11-23 22:18:08 +00:00
|
|
|
// Error channel to transmit any fail back to the user
|
|
|
|
Err chan os.Error
|
|
|
|
|
|
|
|
// Misc knobs to tweak client behaviour:
|
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
|
|
|
|
2011-07-17 12:48:12 +00:00
|
|
|
// Socket timeout, in seconds. Defaulted to 5m in New().
|
2011-07-21 20:59:01 +00:00
|
|
|
Timeout int64
|
2011-07-17 12:48:12 +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
|
2009-12-19 13:40:50 +00:00
|
|
|
|
2011-07-22 00:08:42 +00:00
|
|
|
// Internal counters for flood protection
|
|
|
|
badness, lastsent int64
|
|
|
|
|
2010-11-23 22:01:26 +00:00
|
|
|
// Function which returns a *time.Time for use as a timestamp
|
|
|
|
Timestamp func() *time.Time
|
|
|
|
|
2010-11-23 22:18:08 +00:00
|
|
|
// Enable debugging? Set format for timestamps on debug output.
|
2011-07-18 08:14:58 +00:00
|
|
|
Debug bool
|
2010-11-23 22:18:08 +00:00
|
|
|
TSFormat string
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-18 22:39:22 +00:00
|
|
|
// 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.
|
2009-12-17 17:22:31 +00:00
|
|
|
func New(nick, user, name string) *Conn {
|
2011-07-21 22:03:11 +00:00
|
|
|
conn := &Conn{
|
|
|
|
in: make(chan *Line, 32),
|
|
|
|
out: make(chan string, 32),
|
|
|
|
Err: make(chan os.Error, 4),
|
2011-07-22 00:08:42 +00:00
|
|
|
cSend: make(chan bool),
|
|
|
|
cLoop: make(chan bool),
|
2011-07-21 22:03:11 +00:00
|
|
|
SSL: false,
|
|
|
|
SSLConfig: nil,
|
|
|
|
Timeout: 300,
|
2011-07-22 00:08:42 +00:00
|
|
|
Flood: false,
|
|
|
|
badness: 0,
|
|
|
|
lastsent: 0,
|
2011-07-21 22:03:11 +00:00
|
|
|
Timestamp: time.LocalTime,
|
|
|
|
TSFormat: "15:04:05",
|
|
|
|
}
|
|
|
|
conn.initialise()
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.setupEvents()
|
2011-07-22 00:20:07 +00:00
|
|
|
conn.Me = conn.NewNick(nick, user, name, "")
|
2009-12-17 21:30:18 +00:00
|
|
|
return conn
|
2009-11-29 20:23:15 +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.nicks = make(map[string]*Nick)
|
|
|
|
conn.chans = make(map[string]*Channel)
|
|
|
|
conn.io = nil
|
|
|
|
conn.sock = nil
|
2009-12-19 19:00:27 +00:00
|
|
|
|
2011-07-21 22:03:11 +00:00
|
|
|
// If this is being called because we are reconnecting, conn.Me
|
2009-12-19 19:00:27 +00:00
|
|
|
// will still have all the old channels referenced -- nuke them!
|
|
|
|
if conn.Me != nil {
|
|
|
|
conn.Me = conn.NewNick(conn.Me.Nick, conn.Me.Ident, conn.Me.Name, "")
|
|
|
|
}
|
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.
|
|
|
|
func (conn *Conn) Connect(host string, pass ...string) os.Error {
|
2009-12-17 17:22:31 +00:00
|
|
|
if conn.connected {
|
2010-11-03 23:49:28 +00:00
|
|
|
return os.NewError(fmt.Sprintf(
|
2010-11-04 00:25:46 +00:00
|
|
|
"irc.Connect(): already connected to %s, cannot connect to %s",
|
|
|
|
conn.Host, host))
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2010-11-03 23:49:28 +00:00
|
|
|
|
|
|
|
if conn.SSL {
|
|
|
|
if !hasPort(host) {
|
2010-08-30 11:16:20 +00:00
|
|
|
host += ":6697"
|
2010-11-03 23:49:28 +00:00
|
|
|
}
|
2011-07-21 21:00:45 +00:00
|
|
|
if s, err := tls.Dial("tcp", host, conn.SSLConfig); err == nil {
|
|
|
|
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 {
|
2010-11-03 23:49:28 +00:00
|
|
|
if !hasPort(host) {
|
|
|
|
host += ":6667"
|
|
|
|
}
|
2011-07-18 08:14:58 +00:00
|
|
|
if s, err := net.Dial("tcp", host); err == nil {
|
2010-11-03 23:49:28 +00:00
|
|
|
conn.sock = s
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2010-08-30 11:16:20 +00:00
|
|
|
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.Host = host
|
2009-12-17 17:22:31 +00:00
|
|
|
conn.io = bufio.NewReadWriter(
|
|
|
|
bufio.NewReader(conn.sock),
|
2009-12-17 21:30:18 +00:00
|
|
|
bufio.NewWriter(conn.sock))
|
2011-07-17 12:48:12 +00:00
|
|
|
conn.sock.SetTimeout(conn.Timeout * 1e9)
|
2011-07-22 00:17:35 +00:00
|
|
|
conn.connected = true
|
2009-12-17 21:30:18 +00:00
|
|
|
go conn.send()
|
|
|
|
go conn.recv()
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2010-10-24 09:06:18 +00:00
|
|
|
if len(pass) > 0 {
|
|
|
|
conn.Pass(pass[0])
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.Nick(conn.Me.Nick)
|
|
|
|
conn.User(conn.Me.Ident, conn.Me.Name)
|
2009-11-29 20:23:15 +00:00
|
|
|
|
2009-12-17 21:30:18 +00:00
|
|
|
go conn.runLoop()
|
|
|
|
return nil
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
// dispatch a nicely formatted os.Error to the error channel
|
2010-11-03 23:46:58 +00:00
|
|
|
func (conn *Conn) error(s string, a ...interface{}) {
|
|
|
|
conn.Err <- os.NewError(fmt.Sprintf(s, a...))
|
|
|
|
}
|
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() {
|
2011-07-22 00:08:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line := <-conn.out:
|
|
|
|
conn.write(line)
|
|
|
|
case <-conn.cSend:
|
|
|
|
// strobe on control channel, bail out
|
|
|
|
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 {
|
2009-12-17 21:30:18 +00:00
|
|
|
conn.error("irc.recv(): %s", err.String())
|
|
|
|
conn.shutdown()
|
|
|
|
break
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
2010-08-21 18:23:07 +00:00
|
|
|
s = strings.Trim(s, "\r\n")
|
2011-07-22 00:11:15 +00:00
|
|
|
t := conn.Timestamp()
|
2010-11-02 21:47:05 +00:00
|
|
|
if conn.Debug {
|
2010-12-20 22:14:22 +00:00
|
|
|
fmt.Println(t.Format(conn.TSFormat) + " <- " + s)
|
2010-11-02 21:47:05 +00:00
|
|
|
}
|
2009-12-17 17:22:31 +00:00
|
|
|
|
2011-07-22 00:11:15 +00:00
|
|
|
line := parseLine(s)
|
|
|
|
line.Time = t
|
2009-12-17 17:22:31 +00:00
|
|
|
conn.in <- line
|
|
|
|
}
|
|
|
|
}
|
2009-11-29 20:23:15 +00:00
|
|
|
|
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() {
|
2011-07-22 00:08:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line := <-conn.in:
|
|
|
|
conn.dispatchEvent(line)
|
|
|
|
case <-conn.cLoop:
|
|
|
|
// strobe on control channel, bail out
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a \r\n terminated line of output to the connected server,
|
|
|
|
// using Hybrid's algorithm to rate limit if conn.Flood is false.
|
|
|
|
func (conn *Conn) write(line string) {
|
|
|
|
if !conn.Flood {
|
|
|
|
conn.rateLimit(int64(len(line)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conn.io.WriteString(line + "\r\n"); err != nil {
|
|
|
|
conn.error("irc.send(): %s", err.String())
|
|
|
|
conn.shutdown()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.io.Flush()
|
|
|
|
if conn.Debug {
|
|
|
|
fmt.Println(conn.Timestamp().Format(conn.TSFormat) + " -> " + line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement Hybrid's flood control algorithm to rate-limit outgoing lines.
|
|
|
|
func (conn *Conn) rateLimit(chars int64) {
|
|
|
|
// Hybrid's algorithm allows for 2 seconds per line and an additional
|
|
|
|
// 1/120 of a second per character on that line.
|
|
|
|
linetime := 2*second + chars*second/120
|
|
|
|
elapsed := time.Nanoseconds() - conn.lastsent
|
|
|
|
if conn.badness += linetime - elapsed; conn.badness < 0 {
|
|
|
|
// negative badness times are badness...
|
|
|
|
conn.badness = int64(0)
|
|
|
|
}
|
|
|
|
conn.lastsent = time.Nanoseconds()
|
|
|
|
// 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".
|
|
|
|
if conn.badness > 10*second && !conn.Flood {
|
|
|
|
// so sleep for the current line's time value before sending it
|
|
|
|
time.Sleep(linetime)
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
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()
|
|
|
|
// as calling sock.Close() will cause recv() to recieve EOF in readstring()
|
|
|
|
if conn.connected {
|
|
|
|
conn.connected = false
|
|
|
|
conn.sock.Close()
|
|
|
|
conn.cSend <- true
|
|
|
|
conn.cLoop <- true
|
|
|
|
conn.dispatchEvent(&Line{Cmd: "DISCONNECTED"})
|
|
|
|
// reinit datastructures ready for next connection
|
|
|
|
// do this here rather than after runLoop()'s for due to race
|
|
|
|
conn.initialise()
|
|
|
|
}
|
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
|
|
|
|
// 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"
|
2009-12-17 17:22:31 +00:00
|
|
|
if conn.connected {
|
|
|
|
str += "Connected to " + conn.Host + "\n\n"
|
|
|
|
} else {
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "Not currently connected!\n\n"
|
2009-11-29 20:23:15 +00:00
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
str += conn.Me.String() + "\n"
|
|
|
|
str += "GoIRC Channels\n"
|
|
|
|
str += "--------------\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for _, ch := range conn.chans {
|
|
|
|
str += ch.String() + "\n"
|
|
|
|
}
|
2009-12-17 21:30:18 +00:00
|
|
|
str += "GoIRC NickNames\n"
|
|
|
|
str += "---------------\n\n"
|
2009-12-17 17:22:31 +00:00
|
|
|
for _, n := range conn.nicks {
|
|
|
|
if n != conn.Me {
|
|
|
|
str += n.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
|
|
|
}
|