forked from jshiffer/go-xmpp
Add Chat.OtherElem member
Also Chat.Other member is kept as original behavior.
This commit is contained in:
parent
f66ee47cd9
commit
f4550b5399
61
xmpp.go
61
xmpp.go
@ -536,12 +536,13 @@ func (c *Client) IsEncrypted() bool {
|
|||||||
|
|
||||||
// Chat is an incoming or outgoing XMPP chat message.
|
// Chat is an incoming or outgoing XMPP chat message.
|
||||||
type Chat struct {
|
type Chat struct {
|
||||||
Remote string
|
Remote string
|
||||||
Type string
|
Type string
|
||||||
Text string
|
Text string
|
||||||
Roster Roster
|
Roster Roster
|
||||||
Other []string
|
Other []string
|
||||||
Stamp time.Time
|
OtherElem []XMLElement
|
||||||
|
Stamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Roster []Contact
|
type Roster []Contact
|
||||||
@ -584,11 +585,12 @@ func (c *Client) Recv() (stanza interface{}, err error) {
|
|||||||
v.Delay.Stamp,
|
v.Delay.Stamp,
|
||||||
)
|
)
|
||||||
chat := Chat{
|
chat := Chat{
|
||||||
Remote: v.From,
|
Remote: v.From,
|
||||||
Type: v.Type,
|
Type: v.Type,
|
||||||
Text: v.Body,
|
Text: v.Body,
|
||||||
Other: v.Other,
|
Other: v.OtherStrings(),
|
||||||
Stamp: stamp,
|
OtherElem: v.Other,
|
||||||
|
Stamp: stamp,
|
||||||
}
|
}
|
||||||
return chat, nil
|
return chat, nil
|
||||||
case *clientQuery:
|
case *clientQuery:
|
||||||
@ -720,11 +722,46 @@ type clientMessage struct {
|
|||||||
Thread string `xml:"thread"`
|
Thread string `xml:"thread"`
|
||||||
|
|
||||||
// Any hasn't matched element
|
// Any hasn't matched element
|
||||||
Other []string `xml:",any"`
|
Other []XMLElement `xml:",any"`
|
||||||
|
|
||||||
Delay Delay `xml:"delay"`
|
Delay Delay `xml:"delay"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *clientMessage) OtherStrings() []string {
|
||||||
|
a := make([]string, len(m.Other))
|
||||||
|
for i, e := range m.Other {
|
||||||
|
a[i] = e.String()
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
type XMLElement struct {
|
||||||
|
XMLName xml.Name
|
||||||
|
InnerXML string `xml:",innerxml"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *XMLElement) String() string {
|
||||||
|
r := bytes.NewReader([]byte(e.InnerXML))
|
||||||
|
d := xml.NewDecoder(r)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
for {
|
||||||
|
tok, err := d.Token()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch v := tok.(type) {
|
||||||
|
case xml.StartElement:
|
||||||
|
err = d.Skip()
|
||||||
|
case xml.CharData:
|
||||||
|
_, err = buf.Write(v)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
type Delay struct {
|
type Delay struct {
|
||||||
Stamp string `xml:"stamp,attr"`
|
Stamp string `xml:"stamp,attr"`
|
||||||
}
|
}
|
||||||
|
19
xmpp_test.go
19
xmpp_test.go
@ -57,7 +57,7 @@ func (*testConn) SetWriteDeadline(time.Time) error {
|
|||||||
var text = strings.TrimSpace(`
|
var text = strings.TrimSpace(`
|
||||||
<message xmlns="jabber:client" id="3" type="error" to="123456789@gcm.googleapis.com/ABC">
|
<message xmlns="jabber:client" id="3" type="error" to="123456789@gcm.googleapis.com/ABC">
|
||||||
<gcm xmlns="google:mobile:data">
|
<gcm xmlns="google:mobile:data">
|
||||||
{"random": "text"}
|
{"random": "<text>"}
|
||||||
</gcm>
|
</gcm>
|
||||||
<error code="400" type="modify">
|
<error code="400" type="modify">
|
||||||
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
||||||
@ -80,9 +80,24 @@ func TestStanzaError(t *testing.T) {
|
|||||||
chat := Chat{
|
chat := Chat{
|
||||||
Type: "error",
|
Type: "error",
|
||||||
Other: []string{
|
Other: []string{
|
||||||
"\n\t\t{\"random\": \"text\"}\n\t",
|
"\n\t\t{\"random\": \"<text>\"}\n\t",
|
||||||
"\n\t\t\n\t\t\n\t",
|
"\n\t\t\n\t\t\n\t",
|
||||||
},
|
},
|
||||||
|
OtherElem: []XMLElement{
|
||||||
|
XMLElement{
|
||||||
|
XMLName: xml.Name{Space: "google:mobile:data", Local: "gcm"},
|
||||||
|
InnerXML: "\n\t\t{\"random\": \"<text>\"}\n\t",
|
||||||
|
},
|
||||||
|
XMLElement{
|
||||||
|
XMLName: xml.Name{Space: "jabber:client", Local: "error"},
|
||||||
|
InnerXML: `
|
||||||
|
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
|
||||||
|
<text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
|
||||||
|
InvalidJson: JSON_PARSING_ERROR : Missing Required Field: message_id\n
|
||||||
|
</text>
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(v, chat) {
|
if !reflect.DeepEqual(v, chat) {
|
||||||
t.Errorf("Recv() = %#v; want %#v", v, chat)
|
t.Errorf("Recv() = %#v; want %#v", v, chat)
|
||||||
|
Loading…
Reference in New Issue
Block a user