structs.go
changeset 22 d6b7b4cbf50d
parent 21 8f6ae5cfc9b9
child 24 fff79efe06f6
equal deleted inserted replaced
21:8f6ae5cfc9b9 22:d6b7b4cbf50d
    28 }
    28 }
    29 var _ fmt.Stringer = &JID{}
    29 var _ fmt.Stringer = &JID{}
    30 var _ flag.Value = &JID{}
    30 var _ flag.Value = &JID{}
    31 
    31 
    32 // XMPP's <stream:stream> XML element
    32 // XMPP's <stream:stream> XML element
    33 // BUG(cjyar) Hide this.
    33 type stream struct {
    34 type Stream struct {
       
    35 	To string `xml:"attr"`
    34 	To string `xml:"attr"`
    36 	From string `xml:"attr"`
    35 	From string `xml:"attr"`
    37 	Id string `xml:"attr"`
    36 	Id string `xml:"attr"`
    38 	Lang string `xml:"attr"`
    37 	Lang string `xml:"attr"`
    39 	Version string `xml:"attr"`
    38 	Version string `xml:"attr"`
    40 }
    39 }
    41 var _ xml.Marshaler = &Stream{}
    40 var _ xml.Marshaler = &stream{}
    42 var _ fmt.Stringer = &Stream{}
    41 var _ fmt.Stringer = &stream{}
    43 
    42 
    44 // <stream:error>
    43 // <stream:error>
    45 // BUG(cjyar) Can this be consolidated with Error? Hide it if not.
    44 // BUG(cjyar) Can this be consolidated with Error? Hide it if not.
    46 type StreamError struct {
    45 type StreamError struct {
    47 	Any definedCondition
    46 	Any definedCondition
    48 	Text *errText
    47 	Text *errText
    49 }
    48 }
    50 var _ xml.Marshaler = &StreamError{}
    49 var _ xml.Marshaler = &StreamError{}
    51 
    50 
    52 // BUG(cjyar) Replace this with Unrecognized.
    51 // BUG(cjyar) Replace this with Generic.
    53 type definedCondition struct {
    52 type definedCondition struct {
    54 	// Must always be in namespace nsStreams
    53 	// Must always be in namespace nsStreams
    55 	XMLName xml.Name
    54 	XMLName xml.Name
    56 	Chardata string `xml:"chardata"`
    55 	Chardata string `xml:"chardata"`
    57 }
    56 }
   187 	jid.Domain = parts[3]
   186 	jid.Domain = parts[3]
   188 	jid.Resource = parts[5]
   187 	jid.Resource = parts[5]
   189 	return true
   188 	return true
   190 }
   189 }
   191 
   190 
   192 func (s *Stream) MarshalXML() ([]byte, os.Error) {
   191 func (s *stream) MarshalXML() ([]byte, os.Error) {
   193 	buf := bytes.NewBuffer(nil)
   192 	buf := bytes.NewBuffer(nil)
   194 	buf.WriteString("<stream:stream")
   193 	buf.WriteString("<stream:stream")
   195 	writeField(buf, "xmlns", "jabber:client")
   194 	writeField(buf, "xmlns", "jabber:client")
   196 	writeField(buf, "xmlns:stream", nsStream)
   195 	writeField(buf, "xmlns:stream", nsStream)
   197 	writeField(buf, "to", s.To)
   196 	writeField(buf, "to", s.To)
   202 	buf.WriteString(">")
   201 	buf.WriteString(">")
   203 	// We never write </stream:stream>
   202 	// We never write </stream:stream>
   204 	return buf.Bytes(), nil
   203 	return buf.Bytes(), nil
   205 }
   204 }
   206 
   205 
   207 func (s *Stream) String() string {
   206 func (s *stream) String() string {
   208 	result, _ := s.MarshalXML()
   207 	result, _ := s.MarshalXML()
   209 	return string(result)
   208 	return string(result)
   210 }
   209 }
   211 
   210 
   212 func parseStream(se xml.StartElement) (*Stream, os.Error) {
   211 func parseStream(se xml.StartElement) (*stream, os.Error) {
   213 	s := &Stream{}
   212 	s := &stream{}
   214 	for _, attr := range se.Attr {
   213 	for _, attr := range se.Attr {
   215 		switch strings.ToLower(attr.Name.Local) {
   214 		switch strings.ToLower(attr.Name.Local) {
   216 		case "to":
   215 		case "to":
   217 			s.To = attr.Value
   216 			s.To = attr.Value
   218 		case "from":
   217 		case "from":