xmpp_parsers/
carbons.rs

1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{AsXml, FromXml};
8
9use crate::forwarding::Forwarded;
10use crate::iq::IqSetPayload;
11use crate::message::MessagePayload;
12use crate::ns;
13
14/// Enable carbons for this session.
15#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
16#[xml(namespace = ns::CARBONS, name = "enable")]
17pub struct Enable;
18
19impl IqSetPayload for Enable {}
20
21/// Disable a previously-enabled carbons.
22#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
23#[xml(namespace = ns::CARBONS, name = "disable")]
24pub struct Disable;
25
26impl IqSetPayload for Disable {}
27
28/// Request the enclosing message to not be copied to other carbons-enabled
29/// resources of the user.
30#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
31#[xml(namespace = ns::CARBONS, name = "private")]
32pub struct Private;
33
34impl MessagePayload for Private {}
35
36/// Wrapper for a message received on another resource.
37#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
38#[xml(namespace = ns::CARBONS, name = "received")]
39pub struct Received {
40    /// Wrapper for the enclosed message.
41    #[xml(child)]
42    pub forwarded: Forwarded,
43}
44
45impl MessagePayload for Received {}
46
47/// Wrapper for a message sent from another resource.
48#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
49#[xml(namespace = ns::CARBONS, name = "sent")]
50pub struct Sent {
51    /// Wrapper for the enclosed message.
52    #[xml(child)]
53    pub forwarded: Forwarded,
54}
55
56impl MessagePayload for Sent {}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use jid::Jid;
62    use minidom::Element;
63
64    #[cfg(target_pointer_width = "32")]
65    #[test]
66    fn test_size() {
67        assert_size!(Enable, 0);
68        assert_size!(Disable, 0);
69        assert_size!(Private, 0);
70        assert_size!(Received, 140);
71        assert_size!(Sent, 140);
72    }
73
74    #[cfg(target_pointer_width = "64")]
75    #[test]
76    fn test_size() {
77        assert_size!(Enable, 0);
78        assert_size!(Disable, 0);
79        assert_size!(Private, 0);
80        assert_size!(Received, 264);
81        assert_size!(Sent, 264);
82    }
83
84    #[test]
85    fn empty_elements() {
86        let elem: Element = "<enable xmlns='urn:xmpp:carbons:2'/>".parse().unwrap();
87        Enable::try_from(elem).unwrap();
88
89        let elem: Element = "<disable xmlns='urn:xmpp:carbons:2'/>".parse().unwrap();
90        Disable::try_from(elem).unwrap();
91
92        let elem: Element = "<private xmlns='urn:xmpp:carbons:2'/>".parse().unwrap();
93        Private::try_from(elem).unwrap();
94    }
95
96    #[test]
97    fn forwarded_elements() {
98        let elem: Element = "<received xmlns='urn:xmpp:carbons:2'>
99  <forwarded xmlns='urn:xmpp:forward:0'>
100    <message xmlns='jabber:client'
101             to='juliet@capulet.example/balcony'
102             from='romeo@montague.example/home'/>
103  </forwarded>
104</received>"
105            .parse()
106            .unwrap();
107        let received = Received::try_from(elem).unwrap();
108        assert_eq!(
109            received.forwarded.message.to.unwrap(),
110            Jid::new("juliet@capulet.example/balcony").unwrap()
111        );
112        assert_eq!(
113            received.forwarded.message.from.unwrap(),
114            Jid::new("romeo@montague.example/home").unwrap()
115        );
116
117        let elem: Element = "<sent xmlns='urn:xmpp:carbons:2'>
118  <forwarded xmlns='urn:xmpp:forward:0'>
119    <message xmlns='jabber:client'
120             to='juliet@capulet.example/balcony'
121             from='romeo@montague.example/home'/>
122  </forwarded>
123</sent>"
124            .parse()
125            .unwrap();
126        let sent = Sent::try_from(elem).unwrap();
127        assert_eq!(
128            sent.forwarded.message.to.unwrap(),
129            Jid::new("juliet@capulet.example/balcony").unwrap()
130        );
131        assert_eq!(
132            sent.forwarded.message.from.unwrap(),
133            Jid::new("romeo@montague.example/home").unwrap()
134        );
135    }
136
137    #[test]
138    fn test_serialize_received() {
139        let reference: Element = "<received xmlns='urn:xmpp:carbons:2'><forwarded xmlns='urn:xmpp:forward:0'><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded></received>"
140        .parse()
141        .unwrap();
142
143        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
144          .parse()
145          .unwrap();
146        let forwarded = Forwarded::try_from(elem).unwrap();
147
148        let received = Received {
149            forwarded: forwarded,
150        };
151
152        let serialized: Element = received.into();
153        assert_eq!(serialized, reference);
154    }
155
156    #[test]
157    fn test_serialize_sent() {
158        let reference: Element = "<sent xmlns='urn:xmpp:carbons:2'><forwarded xmlns='urn:xmpp:forward:0'><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded></sent>"
159        .parse()
160        .unwrap();
161
162        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
163          .parse()
164          .unwrap();
165        let forwarded = Forwarded::try_from(elem).unwrap();
166
167        let sent = Sent {
168            forwarded: forwarded,
169        };
170
171        let serialized: Element = sent.into();
172        assert_eq!(serialized, reference);
173    }
174}