Skip to content

Commit ce71bc5

Browse files
authored
Merge pull request #163 from remicorniere/XEP-0334
Support for XEP-0334Support for XEP-0334 (Message Hints)
2 parents fe4c366 + 6a3ee5b commit ce71bc5

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
201201
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
202202
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
203203
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
204-
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
204+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
205205
gotest.tools v2.1.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
206206
gotest.tools/gotestsum v0.3.5/go.mod h1:Mnf3e5FUzXbkCfynWBGOwLssY7gTQgCHObK9tMpAriY=
207207
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

stanza/msg_hint.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package stanza
2+
3+
import "encoding/xml"
4+
5+
/*
6+
Support for:
7+
- XEP-0334: Message Processing Hints: https://xmpp.org/extensions/xep-0334.html
8+
Pointers should be used to keep consistent with unmarshal. Eg :
9+
msg.Extensions = append(msg.Extensions, &stanza.HintNoCopy{}, &stanza.HintStore{})
10+
*/
11+
12+
type HintNoPermanentStore struct {
13+
MsgExtension
14+
XMLName xml.Name `xml:"urn:xmpp:hints no-permanent-store"`
15+
}
16+
17+
type HintNoStore struct {
18+
MsgExtension
19+
XMLName xml.Name `xml:"urn:xmpp:hints no-store"`
20+
}
21+
22+
type HintNoCopy struct {
23+
MsgExtension
24+
XMLName xml.Name `xml:"urn:xmpp:hints no-copy"`
25+
}
26+
type HintStore struct {
27+
MsgExtension
28+
XMLName xml.Name `xml:"urn:xmpp:hints store"`
29+
}
30+
31+
func init() {
32+
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-permanent-store"}, HintNoPermanentStore{})
33+
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-store"}, HintNoStore{})
34+
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "no-copy"}, HintNoCopy{})
35+
TypeRegistry.MapExtension(PKTMessage, xml.Name{Space: "urn:xmpp:hints", Local: "store"}, HintStore{})
36+
}

stanza/msg_hint_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package stanza_test
2+
3+
import (
4+
"encoding/xml"
5+
"gosrc.io/xmpp/stanza"
6+
"reflect"
7+
"strings"
8+
"testing"
9+
)
10+
11+
const msg_const = `
12+
<message
13+
from="romeo@montague.lit/laptop"
14+
to="juliet@capulet.lit/laptop">
15+
<body>V unir avtugf pybnx gb uvqr zr sebz gurve fvtug</body>
16+
<no-copy xmlns="urn:xmpp:hints"></no-copy>
17+
<no-permanent-store xmlns="urn:xmpp:hints"></no-permanent-store>
18+
<no-store xmlns="urn:xmpp:hints"></no-store>
19+
<store xmlns="urn:xmpp:hints"></store>
20+
</message>`
21+
22+
func TestSerializationHint(t *testing.T) {
23+
msg := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
24+
msg.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
25+
msg.Extensions = append(msg.Extensions, stanza.HintNoCopy{}, stanza.HintNoPermanentStore{}, stanza.HintNoStore{}, stanza.HintStore{})
26+
data, _ := xml.Marshal(msg)
27+
if strings.ReplaceAll(strings.Join(strings.Fields(msg_const), ""), "\n", "") != strings.Join(strings.Fields(string(data)), "") {
28+
t.Fatalf("marshalled message does not match expected message")
29+
}
30+
}
31+
32+
func TestUnmarshalHints(t *testing.T) {
33+
// Init message as in the const value
34+
msgConst := stanza.NewMessage(stanza.Attrs{To: "juliet@capulet.lit/laptop", From: "romeo@montague.lit/laptop"})
35+
msgConst.Body = "V unir avtugf pybnx gb uvqr zr sebz gurve fvtug"
36+
msgConst.Extensions = append(msgConst.Extensions, &stanza.HintNoCopy{}, &stanza.HintNoPermanentStore{}, &stanza.HintNoStore{}, &stanza.HintStore{})
37+
38+
// Compare message with the const value
39+
msg := stanza.Message{}
40+
err := xml.Unmarshal([]byte(msg_const), &msg)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
if msgConst.XMLName.Local != msg.XMLName.Local {
46+
t.Fatalf("message tags do not match. Expected: %s, Actual: %s", msgConst.XMLName.Local, msg.XMLName.Local)
47+
}
48+
if msgConst.Body != msg.Body {
49+
t.Fatalf("message bodies do not match. Expected: %s, Actual: %s", msgConst.Body, msg.Body)
50+
}
51+
52+
if !reflect.DeepEqual(msgConst.Attrs, msg.Attrs) {
53+
t.Fatalf("attributes do not match")
54+
}
55+
56+
if !reflect.DeepEqual(msgConst.Error, msg.Error) {
57+
t.Fatalf("attributes do not match")
58+
}
59+
var found bool
60+
for _, ext := range msgConst.Extensions {
61+
for _, strExt := range msg.Extensions {
62+
if reflect.TypeOf(ext) == reflect.TypeOf(strExt) {
63+
found = true
64+
break
65+
}
66+
}
67+
if !found {
68+
t.Fatalf("extensions do not match")
69+
}
70+
found = false
71+
}
72+
}

0 commit comments

Comments
 (0)