go fix. Clearly, I need to run this more often ;)

This commit is contained in:
Matt Goodall 2012-07-18 11:40:05 +01:00
parent 82015bcab4
commit 9ba9dcbe86
9 changed files with 54 additions and 55 deletions

View File

@ -18,7 +18,7 @@ func main() {
// Create stream and configure it as a client connection. // Create stream and configure it as a client connection.
jid := must(xmpp.ParseJID(*jid)).(xmpp.JID) jid := must(xmpp.ParseJID(*jid)).(xmpp.JID)
stream := must(xmpp.NewStream(jid.Domain + ":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream) stream := must(xmpp.NewStream(jid.Domain+":5222", &xmpp.StreamConfig{LogStanzas: true})).(*xmpp.Stream)
client := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP) client := must(xmpp.NewClientXMPP(stream, jid, *password, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
log.Printf("Connection established for %s\n", client.JID) log.Printf("Connection established for %s\n", client.JID)

View File

@ -142,12 +142,12 @@ func authenticate(stream *Stream, mechanisms []string, user, password string) er
type authHandler struct { type authHandler struct {
Mechanism string Mechanism string
Fn func(*Stream, string, string) error Fn func(*Stream, string, string) error
} }
var authHandlers = []authHandler{ var authHandlers = []authHandler{
authHandler{"PLAIN", authenticatePlain}, {"PLAIN", authenticatePlain},
} }
func authenticatePlain(stream *Stream, user, password string) error { func authenticatePlain(stream *Stream, user, password string) error {
auth := saslAuth{Mechanism: "PLAIN", Text: saslEncodePlain(user, password)} auth := saslAuth{Mechanism: "PLAIN", Text: saslEncodePlain(user, password)}
@ -181,9 +181,9 @@ func authenticateResponse(stream *Stream) error {
} }
type saslAuth struct { type saslAuth struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
Mechanism string `xml:"mechanism,attr"` Mechanism string `xml:"mechanism,attr"`
Text string `xml:",chardata"` Text string `xml:",chardata"`
} }
func bindResource(stream *Stream, jid JID) (JID, error) { func bindResource(stream *Stream, jid JID) (JID, error) {
@ -211,9 +211,9 @@ func bindResource(stream *Stream, jid JID) (JID, error) {
} }
type bindIq struct { type bindIq struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
Resource string `xml:"resource,omitempty"` Resource string `xml:"resource,omitempty"`
JID string `xml:"jid,omitempty"` JID string `xml:"jid,omitempty"`
} }
func establishSession(stream *Stream, domain string) error { func establishSession(stream *Stream, domain string) error {
@ -244,11 +244,11 @@ func stringSliceContains(l []string, m string) bool {
} }
type features struct { type features struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"` XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
StartTLS *tlsStartTLS `xml:"starttls"` StartTLS *tlsStartTLS `xml:"starttls"`
Mechanisms *mechanisms `xml:"mechanisms"` Mechanisms *mechanisms `xml:"mechanisms"`
Bind *bind `xml:"bind"` Bind *bind `xml:"bind"`
Session *session `xml:"session"` Session *session `xml:"session"`
} }
type session struct { type session struct {
@ -256,25 +256,25 @@ type session struct {
} }
type bind struct { type bind struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
Required *required `xml:"required"` Required *required `xml:"required"`
} }
type mechanisms struct { type mechanisms struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
Mechanisms []string `xml:"mechanism"` Mechanisms []string `xml:"mechanism"`
} }
type tlsStartTLS struct { type tlsStartTLS struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"`
Required *required `xml:"required"` Required *required `xml:"required"`
} }
type required struct {} type required struct{}
type saslFailure struct { type saslFailure struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
Reason xml.Name `xml:",any"` Reason xml.Name `xml:",any"`
} }
// BUG(matt): authentication incorrectly reports, "No supported SASL mechanism // BUG(matt): authentication incorrectly reports, "No supported SASL mechanism

View File

@ -86,5 +86,5 @@ func handshake(stream *Stream, streamId, secret string) error {
type saslHandshake struct { type saslHandshake struct {
XMLName xml.Name `xml:"jabber:component:accept handshake"` XMLName xml.Name `xml:"jabber:component:accept handshake"`
Value string `xml:",chardata"` Value string `xml:",chardata"`
} }

View File

@ -9,12 +9,12 @@ import (
// XMPP <iq/> stanza. // XMPP <iq/> stanza.
type Iq struct { type Iq struct {
XMLName xml.Name `xml:"iq"` XMLName xml.Name `xml:"iq"`
Id string `xml:"id,attr"` Id string `xml:"id,attr"`
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
To string `xml:"to,attr,omitempty"` To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"` From string `xml:"from,attr,omitempty"`
Payload string `xml:",innerxml"` Payload string `xml:",innerxml"`
Error *Error `xml:"error"` Error *Error `xml:"error"`
} }
// Encode the value to an XML string and set as the payload. See xml.Marshal // Encode the value to an XML string and set as the payload. See xml.Marshal
@ -57,29 +57,29 @@ func (iq *Iq) Response(type_ string) *Iq {
// XMPP <message/> stanza. // XMPP <message/> stanza.
type Message struct { type Message struct {
XMLName xml.Name `xml:"message"` XMLName xml.Name `xml:"message"`
Id string `xml:"id,attr,omitempty"` Id string `xml:"id,attr,omitempty"`
Type string `xml:"type,attr,omitempty"` Type string `xml:"type,attr,omitempty"`
To string `xml:"to,attr,omitempty"` To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"` From string `xml:"from,attr,omitempty"`
Subject string `xml:"subject,omitempty"` Subject string `xml:"subject,omitempty"`
Body string `xml:"body,omitempty"` Body string `xml:"body,omitempty"`
} }
// XMPP <presence/> stanza. // XMPP <presence/> stanza.
type Presence struct { type Presence struct {
XMLName xml.Name `xml:"presence"` XMLName xml.Name `xml:"presence"`
Id string `xml:"id,attr,omitempty"` Id string `xml:"id,attr,omitempty"`
Type string `xml:"type,attr,omitempty"` Type string `xml:"type,attr,omitempty"`
To string `xml:"to,attr,omitempty"` To string `xml:"to,attr,omitempty"`
From string `xml:"from,attr,omitempty"` From string `xml:"from,attr,omitempty"`
} }
// XMPP <error/>. May occur as a top-level stanza or embedded in another // XMPP <error/>. May occur as a top-level stanza or embedded in another
// stanza, e.g. an <iq type="error"/>. // stanza, e.g. an <iq type="error"/>.
type Error struct { type Error struct {
XMLName xml.Name `xml:"error"` XMLName xml.Name `xml:"error"`
Type string `xml:"type,attr"` Type string `xml:"type,attr"`
Payload string `xml:",innerxml"` Payload string `xml:",innerxml"`
} }
func (e Error) Error() string { func (e Error) Error() string {
@ -93,7 +93,7 @@ func (e Error) Error() string {
type errorText struct { type errorText struct {
XMLName xml.Name XMLName xml.Name
Text string `xml:",chardata"` Text string `xml:",chardata"`
} }
// Create a new Error instance using the args as the payload. // Create a new Error instance using the args as the payload.

View File

@ -19,10 +19,10 @@ type StreamConfig struct {
} }
type Stream struct { type Stream struct {
conn net.Conn conn net.Conn
dec *xml.Decoder dec *xml.Decoder
config *StreamConfig config *StreamConfig
stanzaBuf string stanzaBuf string
incomingNamespace nsMap incomingNamespace nsMap
} }

View File

@ -15,4 +15,3 @@ func UUID4() string {
uuid[8] = (uuid[8] &^ 0x40) | 0x80 uuid[8] = (uuid[8] &^ 0x40) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
} }

View File

@ -12,7 +12,7 @@ type XMPP struct {
// JID associated with the stream. Note: this may be negotiated with the // JID associated with the stream. Note: this may be negotiated with the
// server during setup and so must be used for all messages. // server during setup and so must be used for all messages.
JID JID JID JID
stream *Stream stream *Stream
// Channel of incoming messages. Values will be one of Iq, Message, // Channel of incoming messages. Values will be one of Iq, Message,
@ -26,17 +26,17 @@ type XMPP struct {
Out chan interface{} Out chan interface{}
// Incoming stanza filters. // Incoming stanza filters.
filterLock sync.Mutex filterLock sync.Mutex
nextFilterId FilterId nextFilterId FilterId
filters []filter filters []filter
} }
func newXMPP(jid JID, stream *Stream) *XMPP { func newXMPP(jid JID, stream *Stream) *XMPP {
x := &XMPP{ x := &XMPP{
JID: jid, JID: jid,
stream: stream, stream: stream,
In: make(chan interface{}), In: make(chan interface{}),
Out: make(chan interface{}), Out: make(chan interface{}),
} }
go x.sender() go x.sender()
go x.receiver() go x.receiver()
@ -84,7 +84,7 @@ func (fid FilterId) Error() string {
type filter struct { type filter struct {
id FilterId id FilterId
m Matcher m Matcher
ch chan interface{} ch chan interface{}
} }
@ -100,7 +100,7 @@ func (x *XMPP) AddFilter(m Matcher) (FilterId, chan interface{}) {
// Allocate chan and id. // Allocate chan and id.
ch := make(chan interface{}) ch := make(chan interface{})
id := x.nextFilterId id := x.nextFilterId
x.nextFilterId ++ x.nextFilterId++
// Insert at head of filters list. // Insert at head of filters list.
filters := make([]filter, len(x.filters)+1) filters := make([]filter, len(x.filters)+1)

View File

@ -11,7 +11,7 @@ var (
skipverify = false skipverify = false
jid = "" jid = ""
pass = "" pass = ""
debug = false debug = false
) )
func init() { func init() {