# HG changeset patch # User Chris Jones # Date 1324712358 25200 # Node ID a01e06faf0db3c34a9abf23229b7251f58daad7f # Parent 6dbd969c8f3a171e26c7c56b8c7585e87554a8c4 Added code to look up SRV records and open a TCP connection. diff -r 6dbd969c8f3a -r a01e06faf0db Makefile --- a/Makefile Fri Dec 23 22:44:51 2011 -0700 +++ b/Makefile Sat Dec 24 00:39:18 2011 -0700 @@ -7,5 +7,6 @@ TARG=cjyar/xmpp GOFILES=\ xmpp.go \ + structs.go \ include $(GOROOT)/src/Make.pkg diff -r 6dbd969c8f3a -r a01e06faf0db structs.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/structs.go Sat Dec 24 00:39:18 2011 -0700 @@ -0,0 +1,115 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package xmpp + +// This file contains data structures. + +import ( + "bytes" + "fmt" + "io" + "os" + "xml" +) + +const ( + // Version of RFC 3920 that we implement. + Version = "1.0" + nsStreams = "urn:ietf:params:xml:ns:xmpp-streams" + nsStream = "http://etherx.jabber.org/streams" + nsTLS = "urn:ietf:params:xml:ns:xmpp-tls" +) + +// JID represents an entity that can communicate with other +// entities. It looks like node@domain/resource. Node and resource are +// sometimes optional. +type JID struct { + Node *string + Domain string + Resource *string +} +var _ fmt.Stringer = &JID{} + +// XMPP's XML element +type Stream struct { + to string `xml:"attr"` + from string `xml:"attr"` + id string `xml:"attr"` + lang string `xml:"attr"` + version string `xml:"attr"` +} +var _ xml.Marshaler = &Stream{} + +type StreamError struct { + cond definedCondition + text errText +} +var _ xml.Marshaler = &StreamError{} + +type definedCondition struct { + // Must always be in namespace nsStreams + XMLName xml.Name +} + +type errText struct { + XMLName xml.Name + Lang string + text string `xml:"chardata"` +} +var _ xml.Marshaler = &errText{} + +func (jid *JID) String() string { + result := jid.Domain + if jid.Node != nil { + result = *jid.Node + "@" + result + } + if jid.Resource != nil { + result = result + "/" + *jid.Resource + } + return result +} + +func (s *Stream) MarshalXML() ([]byte, os.Error) { + buf := bytes.NewBuffer(nil) + buf.WriteString("") + // We never write + return buf.Bytes(), nil +} + +func (s *StreamError) MarshalXML() ([]byte, os.Error) { + buf := bytes.NewBuffer(nil) + buf.WriteString("") + xml.Marshal(buf, s.cond) + xml.Marshal(buf, s.text) + buf.WriteString("") + return buf.Bytes(), nil +} + +func (e *errText) MarshalXML() ([]byte, os.Error) { + buf := bytes.NewBuffer(nil) + buf.WriteString("") + xml.Escape(buf, []byte(e.text)) + buf.WriteString("") + return buf.Bytes(), nil +} + +func writeField(w io.Writer, field, value string) { + if value != "" { + io.WriteString(w, " ") + io.WriteString(w, field) + io.WriteString(w, `="`) + xml.Escape(w, []byte(value)) + io.WriteString(w, `"`) + } +} diff -r 6dbd969c8f3a -r a01e06faf0db xmpp.go --- a/xmpp.go Fri Dec 23 22:44:51 2011 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,113 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// This package implements a simple XMPP client according to RFCs 3920 -// and 3921, plus the various XEPs at http://xmpp.org/protocols/. -package xmpp - -import ( - "bytes" - "fmt" - "io" - "os" - "xml" -) - -const ( - // Version of RFC 3920 that we implement. - Version = "1.0" - nsStreams = "urn:ietf:params:xml:ns:xmpp-streams" -) - -// JID represents an entity that can communicate with other -// entities. It looks like node@domain/resource. Node and resource are -// sometimes optional. -type JID struct { - Node *string - Domain string - Resource *string -} -var _ fmt.Stringer = &JID{} - -// XMPP's XML element -type Stream struct { - to string `xml:"attr"` - from string `xml:"attr"` - id string `xml:"attr"` - lang string `xml:"attr"` - version string `xml:"attr"` -} -var _ xml.Marshaler = &Stream{} - -type StreamError struct { - cond definedCondition - text errText -} -var _ xml.Marshaler = &StreamError{} - -type definedCondition struct { - // Must always be in namespace nsStreams - XMLName xml.Name -} - -type errText struct { - XMLName xml.Name - Lang string - text string `xml:"chardata"` -} -var _ xml.Marshaler = &errText{} - -func (jid *JID) String() string { - result := jid.Domain - if jid.Node != nil { - result = *jid.Node + "@" + result - } - if jid.Resource != nil { - result = result + "/" + *jid.Resource - } - return result -} - -func (s *Stream) MarshalXML() ([]byte, os.Error) { - buf := bytes.NewBuffer(nil) - buf.WriteString("") - // We never write - return buf.Bytes(), nil -} - -func (s *StreamError) MarshalXML() ([]byte, os.Error) { - buf := bytes.NewBuffer(nil) - buf.WriteString("") - xml.Marshal(buf, s.cond) - xml.Marshal(buf, s.text) - buf.WriteString("") - return buf.Bytes(), nil -} - -func (e *errText) MarshalXML() ([]byte, os.Error) { - buf := bytes.NewBuffer(nil) - buf.WriteString("") - xml.Escape(buf, []byte(e.text)) - buf.WriteString("") - return buf.Bytes(), nil -} - -func writeField(w io.Writer, field, value string) { - if value != "" { - io.WriteString(w, " ") - io.WriteString(w, field) - io.WriteString(w, `="`) - xml.Escape(w, []byte(value)) - io.WriteString(w, `"`) - } -}