structs.go
changeset 6 8e425e340ca1
parent 5 faef59c8db05
child 7 4f0f66f9a441
--- a/structs.go	Sat Dec 24 13:11:36 2011 -0700
+++ b/structs.go	Sun Dec 25 18:46:13 2011 -0700
@@ -38,17 +38,19 @@
 
 // XMPP's <stream:stream> 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"`
+	To string `xml:"attr"`
+	From string `xml:"attr"`
+	Id string `xml:"attr"`
+	Lang string `xml:"attr"`
+	Version string `xml:"attr"`
 }
 var _ xml.Marshaler = &Stream{}
+var _ fmt.Stringer = &Stream{}
 
+// <stream:error>
 type StreamError struct {
-	cond definedCondition
-	text *errText
+	Any definedCondition
+	Text *errText
 }
 var _ xml.Marshaler = &StreamError{}
 
@@ -58,8 +60,8 @@
 }
 
 type errText struct {
-	Lang string
-	text string `xml:"chardata"`
+	Lang string `xml:"attr"`
+	Text string `xml:"chardata"`
 }
 var _ xml.Marshaler = &errText{}
 
@@ -101,31 +103,37 @@
 func (s *Stream) MarshalXML() ([]byte, os.Error) {
 	buf := bytes.NewBuffer(nil)
 	buf.WriteString("<stream:stream")
-	writeField(buf, "to", s.to)
-	writeField(buf, "from", s.from)
-	writeField(buf, "id", s.id)
-	writeField(buf, "xml:lang", s.lang)
-	writeField(buf, "version", s.version)
+	writeField(buf, "xmlns", "jabber:client")
+	writeField(buf, "xmlns:stream", nsStream)
+	writeField(buf, "to", s.To)
+	writeField(buf, "from", s.From)
+	writeField(buf, "id", s.Id)
+	writeField(buf, "xml:lang", s.Lang)
+	writeField(buf, "version", s.Version)
 	buf.WriteString(">")
 	// We never write </stream:stream>
 	return buf.Bytes(), nil
 }
 
+func (s *Stream) String() string {
+	result, _ := s.MarshalXML()
+	return string(result)
+}
+
 func parseStream(se xml.StartElement) (*Stream, os.Error) {
 	s := &Stream{}
-	se = se.Copy()
 	for _, attr := range se.Attr {
 		switch strings.ToLower(attr.Name.Local) {
 		case "to":
-			s.to = attr.Value
+			s.To = attr.Value
 		case "from":
-			s.from = attr.Value
+			s.From = attr.Value
 		case "id":
-			s.id = attr.Value
+			s.Id = attr.Value
 		case "lang":
-			s.lang = attr.Value
+			s.Lang = attr.Value
 		case "version":
-			s.version = attr.Value
+			s.Version = attr.Value
 		}
 	}
 	return s, nil
@@ -134,9 +142,9 @@
 func (s *StreamError) MarshalXML() ([]byte, os.Error) {
 	buf := bytes.NewBuffer(nil)
 	buf.WriteString("<stream:error>")
-	xml.Marshal(buf, s.cond)
-	if s.text != nil {
-		xml.Marshal(buf, s.text)
+	xml.Marshal(buf, s.Any)
+	if s.Text != nil {
+		xml.Marshal(buf, s.Text)
 	}
 	buf.WriteString("</stream:error>")
 	return buf.Bytes(), nil
@@ -148,7 +156,7 @@
 	writeField(buf, "xmlns", nsStreams)
 	writeField(buf, "xml:lang", e.Lang)
 	buf.WriteString(">")
-	xml.Escape(buf, []byte(e.text))
+	xml.Escape(buf, []byte(e.Text))
 	buf.WriteString("</text>")
 	return buf.Bytes(), nil
 }