Use the internal event handling for initial pass/user/nick messages.

Added constants for internal events.
gofmt'ed all files and updated client.go/documentation.
This commit is contained in:
Chris Rhodes 2013-02-15 17:24:54 -08:00
parent 91c71da2dd
commit 70a2700cbd
11 changed files with 85 additions and 64 deletions

View File

@ -24,11 +24,11 @@ Synopsis:
// Add handlers to do things here! // Add handlers to do things here!
// e.g. join a channel on connect. // e.g. join a channel on connect.
c.AddHandler("connected", c.HandleFunc(irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) { conn.Join("#channel") }) func(conn *irc.Conn, line *irc.Line) { conn.Join("#channel") })
// And a signal on disconnect // And a signal on disconnect
quit := make(chan bool) quit := make(chan bool)
c.AddHandler("disconnected", c.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) { quit <- true }) func(conn *irc.Conn, line *irc.Line) { quit <- true })
// Tell client to connect // Tell client to connect

View File

@ -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"
) )
@ -16,14 +16,14 @@ func main() {
flag.Parse() flag.Parse()
// create new IRC connection // create new IRC connection
c := irc.SimpleClient("GoTest", "gotest") c := irc.Client("GoTest", "gotest")
c.EnableStateTracking() c.EnableStateTracking()
c.AddHandler("connected", c.HandleFunc(irc.CONNECTED,
func(conn *irc.Conn, line *irc.Line) { conn.Join(*channel) }) func(conn *irc.Conn, line *irc.Line) { conn.Join(*channel) })
// Set up a handler to notify of disconnect events. // Set up a handler to notify of disconnect events.
quit := make(chan bool) quit := make(chan bool)
c.AddHandler("disconnected", c.HandleFunc(irc.DISCONNECTED,
func(conn *irc.Conn, line *irc.Line) { quit <- true }) func(conn *irc.Conn, line *irc.Line) { quit <- true })
// set up a goroutine to read commands from stdin // set up a goroutine to read commands from stdin
@ -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

View File

@ -2,6 +2,12 @@ package client
import "strings" import "strings"
const (
INIT = "init"
CONNECTED = "connected"
DISCONNECTED = "disconnected"
)
// this file contains the various commands you can // this file contains the various commands you can
// send to the server using an Conn connection // send to the server using an Conn connection

View File

@ -18,6 +18,7 @@ type Conn struct {
Host string Host string
Me *state.Nick Me *state.Nick
Network string Network string
password string
// Handlers and Commands // Handlers and Commands
handlers *hSet handlers *hSet
@ -164,13 +165,12 @@ func (conn *Conn) Connect(host string, pass ...string) error {
} }
conn.Host = host conn.Host = host
conn.Connected = true conn.Connected = true
conn.postConnect()
if len(pass) > 0 { if len(pass) > 0 {
conn.Pass(pass[0]) conn.password = pass[0]
} else {
conn.password = ""
} }
conn.Nick(conn.Me.Nick) conn.postConnect()
conn.User(conn.Me.Ident, conn.Me.Name)
return nil return nil
} }
@ -188,6 +188,7 @@ func (conn *Conn) postConnect() {
go func() { <-conn.cPing }() go func() { <-conn.cPing }()
} }
go conn.runLoop() go conn.runLoop()
conn.dispatch(&Line{Cmd: INIT})
} }
// copied from http.client for great justice // copied from http.client for great justice
@ -305,7 +306,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 {
logging.Info("irc.shutdown(): Disconnected from server.") logging.Info("irc.shutdown(): Disconnected from server.")
conn.dispatch(&Line{Cmd: "disconnected"}) conn.dispatch(&Line{Cmd: DISCONNECTED})
conn.Connected = false conn.Connected = false
conn.sock.Close() conn.sock.Close()
conn.cSend <- true conn.cSend <- true

View File

@ -3,8 +3,8 @@ package client
import ( import (
"bufio" "bufio"
"code.google.com/p/gomock/gomock" "code.google.com/p/gomock/gomock"
"github.com/fluffle/golog/logging"
"github.com/fluffle/goirc/state" "github.com/fluffle/goirc/state"
"github.com/fluffle/golog/logging"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -57,7 +57,7 @@ func TestEOF(t *testing.T) {
// Set up a handler to detect whether disconnected handlers are called // Set up a handler to detect whether disconnected handlers are called
dcon := false dcon := false
c.HandleFunc("disconnected", func (conn *Conn, line *Line) { c.HandleFunc(DISCONNECTED, func(conn *Conn, line *Line) {
dcon = true dcon = true
}) })
@ -470,7 +470,7 @@ func TestRateLimit(t *testing.T) {
// We'll be needing this later... // We'll be needing this later...
abs := func(i time.Duration) time.Duration { abs := func(i time.Duration) time.Duration {
if (i < 0) { if i < 0 {
return -i return -i
} }
return i return i

View File

@ -105,7 +105,9 @@ func (hs *hSet) dispatch(conn *Conn, line *Line) {
defer hs.RUnlock() defer hs.RUnlock()
ev := strings.ToLower(line.Cmd) ev := strings.ToLower(line.Cmd)
list, ok := hs.set[ev] list, ok := hs.set[ev]
if !ok { return } if !ok {
return
}
for hn := list.start; hn != nil; hn = hn.next { for hn := list.start; hn != nil; hn = hn.next {
go hn.Handle(conn, line) go hn.Handle(conn, line)
} }

View File

@ -9,6 +9,7 @@ import (
// 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]HandlerFunc{ var intHandlers = map[string]HandlerFunc{
INIT: (*Conn).h_init,
"001": (*Conn).h_001, "001": (*Conn).h_001,
"433": (*Conn).h_433, "433": (*Conn).h_433,
"CTCP": (*Conn).h_CTCP, "CTCP": (*Conn).h_CTCP,
@ -24,6 +25,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])
@ -32,7 +42,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.dispatch(&Line{Cmd: "connected"}) conn.dispatch(&Line{Cmd: CONNECTED})
// 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 {
@ -99,7 +109,9 @@ func (conn *Conn) h_PRIVMSG(line *Line) {
} }
} }
cmd, l := conn.cmdMatch(txt) cmd, l := conn.cmdMatch(txt)
if cmd == nil { return } if cmd == nil {
return
}
if conn.CommandStripPrefix { if conn.CommandStripPrefix {
txt = strings.TrimSpace(txt[l:]) txt = strings.TrimSpace(txt[l:])
} }

View File

@ -27,7 +27,7 @@ func Test001(t *testing.T) {
l := parseLine(":irc.server.org 001 test :Welcome to IRC test!ident@somehost.com") l := parseLine(":irc.server.org 001 test :Welcome to IRC test!ident@somehost.com")
// Set up a handler to detect whether connected handler is called from 001 // Set up a handler to detect whether connected handler is called from 001
hcon := false hcon := false
c.HandleFunc("connected", func (conn *Conn, line *Line) { c.HandleFunc(CONNECTED, func(conn *Conn, line *Line) {
hcon = true hcon = true
}) })
@ -188,7 +188,6 @@ 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()
} }
// Test the handler for JOIN messages // Test the handler for JOIN messages
@ -317,7 +316,6 @@ func TestMODE(t *testing.T) {
t.Errorf("Channel.ParseModes() not called correctly.") t.Errorf("Channel.ParseModes() not called correctly.")
} }
// Send a nick mode line, returning Me // Send a nick mode line, returning Me
gomock.InOrder( gomock.InOrder(
s.st.EXPECT().GetChannel("test").Return(nil), s.st.EXPECT().GetChannel("test").Return(nil),