36 var _ fmt.Stringer = &JID{} |
36 var _ fmt.Stringer = &JID{} |
37 var _ flag.Value = &JID{} |
37 var _ flag.Value = &JID{} |
38 |
38 |
39 // XMPP's <stream:stream> XML element |
39 // XMPP's <stream:stream> XML element |
40 type Stream struct { |
40 type Stream struct { |
41 to string `xml:"attr"` |
41 To string `xml:"attr"` |
42 from string `xml:"attr"` |
42 From string `xml:"attr"` |
43 id string `xml:"attr"` |
43 Id string `xml:"attr"` |
44 lang string `xml:"attr"` |
44 Lang string `xml:"attr"` |
45 version string `xml:"attr"` |
45 Version string `xml:"attr"` |
46 } |
46 } |
47 var _ xml.Marshaler = &Stream{} |
47 var _ xml.Marshaler = &Stream{} |
|
48 var _ fmt.Stringer = &Stream{} |
48 |
49 |
|
50 // <stream:error> |
49 type StreamError struct { |
51 type StreamError struct { |
50 cond definedCondition |
52 Any definedCondition |
51 text *errText |
53 Text *errText |
52 } |
54 } |
53 var _ xml.Marshaler = &StreamError{} |
55 var _ xml.Marshaler = &StreamError{} |
54 |
56 |
55 type definedCondition struct { |
57 type definedCondition struct { |
56 // Must always be in namespace nsStreams |
58 // Must always be in namespace nsStreams |
57 XMLName xml.Name |
59 XMLName xml.Name |
58 } |
60 } |
59 |
61 |
60 type errText struct { |
62 type errText struct { |
61 Lang string |
63 Lang string `xml:"attr"` |
62 text string `xml:"chardata"` |
64 Text string `xml:"chardata"` |
63 } |
65 } |
64 var _ xml.Marshaler = &errText{} |
66 var _ xml.Marshaler = &errText{} |
65 |
67 |
66 type Unrecognized struct { |
68 type Unrecognized struct { |
67 XMLName xml.Name |
69 XMLName xml.Name |
99 } |
101 } |
100 |
102 |
101 func (s *Stream) MarshalXML() ([]byte, os.Error) { |
103 func (s *Stream) MarshalXML() ([]byte, os.Error) { |
102 buf := bytes.NewBuffer(nil) |
104 buf := bytes.NewBuffer(nil) |
103 buf.WriteString("<stream:stream") |
105 buf.WriteString("<stream:stream") |
104 writeField(buf, "to", s.to) |
106 writeField(buf, "xmlns", "jabber:client") |
105 writeField(buf, "from", s.from) |
107 writeField(buf, "xmlns:stream", nsStream) |
106 writeField(buf, "id", s.id) |
108 writeField(buf, "to", s.To) |
107 writeField(buf, "xml:lang", s.lang) |
109 writeField(buf, "from", s.From) |
108 writeField(buf, "version", s.version) |
110 writeField(buf, "id", s.Id) |
|
111 writeField(buf, "xml:lang", s.Lang) |
|
112 writeField(buf, "version", s.Version) |
109 buf.WriteString(">") |
113 buf.WriteString(">") |
110 // We never write </stream:stream> |
114 // We never write </stream:stream> |
111 return buf.Bytes(), nil |
115 return buf.Bytes(), nil |
112 } |
116 } |
113 |
117 |
|
118 func (s *Stream) String() string { |
|
119 result, _ := s.MarshalXML() |
|
120 return string(result) |
|
121 } |
|
122 |
114 func parseStream(se xml.StartElement) (*Stream, os.Error) { |
123 func parseStream(se xml.StartElement) (*Stream, os.Error) { |
115 s := &Stream{} |
124 s := &Stream{} |
116 se = se.Copy() |
|
117 for _, attr := range se.Attr { |
125 for _, attr := range se.Attr { |
118 switch strings.ToLower(attr.Name.Local) { |
126 switch strings.ToLower(attr.Name.Local) { |
119 case "to": |
127 case "to": |
120 s.to = attr.Value |
128 s.To = attr.Value |
121 case "from": |
129 case "from": |
122 s.from = attr.Value |
130 s.From = attr.Value |
123 case "id": |
131 case "id": |
124 s.id = attr.Value |
132 s.Id = attr.Value |
125 case "lang": |
133 case "lang": |
126 s.lang = attr.Value |
134 s.Lang = attr.Value |
127 case "version": |
135 case "version": |
128 s.version = attr.Value |
136 s.Version = attr.Value |
129 } |
137 } |
130 } |
138 } |
131 return s, nil |
139 return s, nil |
132 } |
140 } |
133 |
141 |
134 func (s *StreamError) MarshalXML() ([]byte, os.Error) { |
142 func (s *StreamError) MarshalXML() ([]byte, os.Error) { |
135 buf := bytes.NewBuffer(nil) |
143 buf := bytes.NewBuffer(nil) |
136 buf.WriteString("<stream:error>") |
144 buf.WriteString("<stream:error>") |
137 xml.Marshal(buf, s.cond) |
145 xml.Marshal(buf, s.Any) |
138 if s.text != nil { |
146 if s.Text != nil { |
139 xml.Marshal(buf, s.text) |
147 xml.Marshal(buf, s.Text) |
140 } |
148 } |
141 buf.WriteString("</stream:error>") |
149 buf.WriteString("</stream:error>") |
142 return buf.Bytes(), nil |
150 return buf.Bytes(), nil |
143 } |
151 } |
144 |
152 |
146 buf := bytes.NewBuffer(nil) |
154 buf := bytes.NewBuffer(nil) |
147 buf.WriteString("<text") |
155 buf.WriteString("<text") |
148 writeField(buf, "xmlns", nsStreams) |
156 writeField(buf, "xmlns", nsStreams) |
149 writeField(buf, "xml:lang", e.Lang) |
157 writeField(buf, "xml:lang", e.Lang) |
150 buf.WriteString(">") |
158 buf.WriteString(">") |
151 xml.Escape(buf, []byte(e.text)) |
159 xml.Escape(buf, []byte(e.Text)) |
152 buf.WriteString("</text>") |
160 buf.WriteString("</text>") |
153 return buf.Bytes(), nil |
161 return buf.Bytes(), nil |
154 } |
162 } |
155 |
163 |
156 func writeField(w io.Writer, field, value string) { |
164 func writeField(w io.Writer, field, value string) { |