mirror of https://github.com/fluffle/goirc
Use the internal event handling to process initial user/nick messages.
This commit is contained in:
parent
f4b53dfb24
commit
25efcccee1
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
irc "github.com/fluffle/goirc/client"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
irc "github.com/fluffle/goirc/client"
|
||||||
"os"
|
"os"
|
||||||
"bufio"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// wha?, maybe ctrl-D...
|
// wha?, maybe ctrl-D...
|
||||||
close(in)
|
close(in)
|
||||||
|
reallyquit = true
|
||||||
|
c.Quit("")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// no point in sending empty lines down the channel
|
// no point in sending empty lines down the channel
|
||||||
|
|
|
@ -16,9 +16,10 @@ 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 Hostname and Nickname
|
||||||
Host string
|
Host string
|
||||||
Me *state.Nick
|
Me *state.Nick
|
||||||
Network string
|
Network string
|
||||||
|
Password string
|
||||||
|
|
||||||
// Replaceable function to customise the 433 handler's new nick
|
// Replaceable function to customise the 433 handler's new nick
|
||||||
NewNick func(string) string
|
NewNick func(string) string
|
||||||
|
@ -50,7 +51,6 @@ type Conn struct {
|
||||||
|
|
||||||
// Misc knobs to tweak client behaviour:
|
// Misc knobs to tweak client behaviour:
|
||||||
// Are we connecting via SSL? Do we care about certificate validity?
|
// Are we connecting via SSL? Do we care about certificate validity?
|
||||||
SSL bool
|
|
||||||
SSLConfig *tls.Config
|
SSLConfig *tls.Config
|
||||||
|
|
||||||
// Client->server ping frequency, in seconds. Defaults to 3m.
|
// Client->server ping frequency, in seconds. Defaults to 3m.
|
||||||
|
@ -60,7 +60,7 @@ type Conn struct {
|
||||||
Flood bool
|
Flood bool
|
||||||
|
|
||||||
// Internal counters for flood protection
|
// Internal counters for flood protection
|
||||||
badness time.Duration
|
badness time.Duration
|
||||||
lastsent time.Time
|
lastsent time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +96,6 @@ func Client(nick, ident, name string,
|
||||||
cSend: make(chan bool),
|
cSend: make(chan bool),
|
||||||
cLoop: make(chan bool),
|
cLoop: make(chan bool),
|
||||||
cPing: make(chan bool),
|
cPing: make(chan bool),
|
||||||
SSL: false,
|
|
||||||
SSLConfig: nil,
|
SSLConfig: nil,
|
||||||
PingFreq: 3 * time.Minute,
|
PingFreq: 3 * time.Minute,
|
||||||
Flood: false,
|
Flood: false,
|
||||||
|
@ -145,8 +144,8 @@ func (conn *Conn) initialise() {
|
||||||
|
|
||||||
// Connect the IRC connection object to "host[:port]" which should be either
|
// Connect the IRC connection object to "host[:port]" which should be either
|
||||||
// a hostname or an IP address, with an optional port. To enable explicit SSL
|
// a hostname or an IP address, with an optional port. To enable explicit SSL
|
||||||
// on the connection to the IRC server, set Conn.SSL to true before calling
|
// on the connection to the IRC server, set Conn.SSLConfig.
|
||||||
// Connect(). The port will default to 6697 if ssl is enabled, and 6667
|
// The port will default to 6697 if SSL is enabled, and 6667
|
||||||
// otherwise. You can also provide an optional connect password.
|
// otherwise. You can also provide an optional connect password.
|
||||||
func (conn *Conn) Connect(host string, pass ...string) error {
|
func (conn *Conn) Connect(host string, pass ...string) error {
|
||||||
if conn.Connected {
|
if conn.Connected {
|
||||||
|
@ -154,37 +153,48 @@ func (conn *Conn) Connect(host string, pass ...string) error {
|
||||||
"irc.Connect(): already connected to %s, cannot connect to %s",
|
"irc.Connect(): already connected to %s, cannot connect to %s",
|
||||||
conn.Host, host))
|
conn.Host, host))
|
||||||
}
|
}
|
||||||
|
if conn.SSLConfig != nil {
|
||||||
if conn.SSL {
|
|
||||||
if !hasPort(host) {
|
if !hasPort(host) {
|
||||||
host += ":6697"
|
host += ":6697"
|
||||||
}
|
}
|
||||||
conn.l.Info("irc.Connect(): Connecting to %s with SSL.", host)
|
|
||||||
if s, err := tls.Dial("tcp", host, conn.SSLConfig); err == nil {
|
|
||||||
conn.sock = s
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if !hasPort(host) {
|
if !hasPort(host) {
|
||||||
host += ":6667"
|
host += ":6667"
|
||||||
}
|
}
|
||||||
conn.l.Info("irc.Connect(): Connecting to %s without SSL.", host)
|
}
|
||||||
if s, err := net.Dial("tcp", host); err == nil {
|
conn.Host = host
|
||||||
|
if len(pass) > 0 {
|
||||||
|
conn.Password = pass[0]
|
||||||
|
} else {
|
||||||
|
conn.Password = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn.Reconnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *Conn) Reconnect() error {
|
||||||
|
if conn.Connected {
|
||||||
|
return errors.New(fmt.Sprintf(
|
||||||
|
"irc.Reconnect(): already connected to %s, cannot reconnect", conn.Host))
|
||||||
|
}
|
||||||
|
if conn.SSLConfig != nil {
|
||||||
|
conn.l.Info("irc.Connect(): Connecting to %s with SSL.", conn.Host)
|
||||||
|
if s, err := tls.Dial("tcp", conn.Host, conn.SSLConfig); err == nil {
|
||||||
|
conn.sock = s
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
conn.l.Info("irc.Connect(): Connecting to %s without SSL.", conn.Host)
|
||||||
|
if s, err := net.Dial("tcp", conn.Host); err == nil {
|
||||||
conn.sock = s
|
conn.sock = s
|
||||||
} else {
|
} else {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn.Host = host
|
|
||||||
conn.Connected = true
|
conn.Connected = true
|
||||||
conn.postConnect()
|
conn.postConnect()
|
||||||
|
|
||||||
if len(pass) > 0 {
|
|
||||||
conn.Pass(pass[0])
|
|
||||||
}
|
|
||||||
conn.Nick(conn.Me.Nick)
|
|
||||||
conn.User(conn.Me.Ident, conn.Me.Name)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,6 +212,7 @@ func (conn *Conn) postConnect() {
|
||||||
go func() { <-conn.cPing }()
|
go func() { <-conn.cPing }()
|
||||||
}
|
}
|
||||||
go conn.runLoop()
|
go conn.runLoop()
|
||||||
|
conn.ED.Dispatch(INIT, conn, &Line{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// copied from http.client for great justice
|
// copied from http.client for great justice
|
||||||
|
@ -319,7 +330,7 @@ func (conn *Conn) shutdown() {
|
||||||
// as calling sock.Close() will cause recv() to recieve EOF in readstring()
|
// as calling sock.Close() will cause recv() to recieve EOF in readstring()
|
||||||
if conn.Connected {
|
if conn.Connected {
|
||||||
conn.l.Info("irc.shutdown(): Disconnected from server.")
|
conn.l.Info("irc.shutdown(): Disconnected from server.")
|
||||||
conn.ED.Dispatch("disconnected", conn, &Line{})
|
conn.ED.Dispatch(DISCONNECTED, conn, &Line{})
|
||||||
conn.Connected = false
|
conn.Connected = false
|
||||||
conn.sock.Close()
|
conn.sock.Close()
|
||||||
conn.cSend <- true
|
conn.cSend <- true
|
||||||
|
|
|
@ -8,6 +8,13 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Consts for unnamed events.
|
||||||
|
const (
|
||||||
|
INIT = "init"
|
||||||
|
CONNECTED = "connected"
|
||||||
|
DISCONNECTED = "disconnected"
|
||||||
|
)
|
||||||
|
|
||||||
// An IRC handler looks like this:
|
// An IRC handler looks like this:
|
||||||
type IRCHandler func(*Conn, *Line)
|
type IRCHandler func(*Conn, *Line)
|
||||||
|
|
||||||
|
@ -34,8 +41,10 @@ func NewHandler(f IRCHandler) event.Handler {
|
||||||
|
|
||||||
// sets up the internal event handlers to do essential IRC protocol things
|
// sets up the internal event handlers to do essential IRC protocol things
|
||||||
var intHandlers map[string]event.Handler
|
var intHandlers map[string]event.Handler
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
intHandlers = make(map[string]event.Handler)
|
intHandlers = make(map[string]event.Handler)
|
||||||
|
intHandlers[INIT] = NewHandler((*Conn).h_init)
|
||||||
intHandlers["001"] = NewHandler((*Conn).h_001)
|
intHandlers["001"] = NewHandler((*Conn).h_001)
|
||||||
intHandlers["433"] = NewHandler((*Conn).h_433)
|
intHandlers["433"] = NewHandler((*Conn).h_433)
|
||||||
intHandlers["CTCP"] = NewHandler((*Conn).h_CTCP)
|
intHandlers["CTCP"] = NewHandler((*Conn).h_CTCP)
|
||||||
|
@ -49,6 +58,15 @@ func (conn *Conn) addIntHandlers() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Password/User/Nick broadcast on connection.
|
||||||
|
func (conn *Conn) h_init(line *Line) {
|
||||||
|
if conn.Password != "" {
|
||||||
|
conn.Pass(conn.Password)
|
||||||
|
}
|
||||||
|
conn.Nick(conn.Me.Nick)
|
||||||
|
conn.User(conn.Me.Ident, conn.Me.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// Basic ping/pong handler
|
// Basic ping/pong handler
|
||||||
func (conn *Conn) h_PING(line *Line) {
|
func (conn *Conn) h_PING(line *Line) {
|
||||||
conn.Raw("PONG :" + line.Args[0])
|
conn.Raw("PONG :" + line.Args[0])
|
||||||
|
@ -57,7 +75,7 @@ func (conn *Conn) h_PING(line *Line) {
|
||||||
// Handler to trigger a "CONNECTED" event on receipt of numeric 001
|
// Handler to trigger a "CONNECTED" event on receipt of numeric 001
|
||||||
func (conn *Conn) h_001(line *Line) {
|
func (conn *Conn) h_001(line *Line) {
|
||||||
// we're connected!
|
// we're connected!
|
||||||
conn.ED.Dispatch("connected", conn, line)
|
conn.ED.Dispatch(CONNECTED, conn, line)
|
||||||
// and we're being given our hostname (from the server's perspective)
|
// and we're being given our hostname (from the server's perspective)
|
||||||
t := line.Args[len(line.Args)-1]
|
t := line.Args[len(line.Args)-1]
|
||||||
if idx := strings.LastIndex(t, " "); idx != -1 {
|
if idx := strings.LastIndex(t, " "); idx != -1 {
|
||||||
|
|
Loading…
Reference in New Issue