xmpp_parsers/
mam.rs

1// Copyright (c) 2017-2021 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::data_forms::DataForm;
10use crate::date::DateTime;
11use crate::forwarding::Forwarded;
12use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
13use crate::message::MessagePayload;
14use crate::ns;
15use crate::pubsub::NodeName;
16use crate::rsm::{SetQuery, SetResult};
17
18generate_id!(
19    /// An identifier matching a result message to the query requesting it.
20    QueryId
21);
22
23/// Starts a query to the archive.
24#[derive(FromXml, AsXml, Debug)]
25#[xml(namespace = ns::MAM, name = "query")]
26pub struct Query {
27    /// An optional identifier for matching forwarded messages to this
28    /// query.
29    #[xml(attribute(default))]
30    pub queryid: Option<QueryId>,
31
32    /// Must be set to Some when querying a PubSub node’s archive.
33    #[xml(attribute(default))]
34    pub node: Option<NodeName>,
35
36    /// Used for filtering the results.
37    #[xml(child(default))]
38    pub form: Option<DataForm>,
39
40    /// Used for paging through results.
41    #[xml(child(default))]
42    pub set: Option<SetQuery>,
43
44    /// Used for reversing the order of the results.
45    #[xml(flag(name = "flip-page"))]
46    pub flip_page: bool,
47}
48
49impl IqGetPayload for Query {}
50impl IqSetPayload for Query {}
51impl IqResultPayload for Query {}
52
53/// The wrapper around forwarded stanzas.
54#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
55#[xml(namespace = ns::MAM, name = "result")]
56pub struct Result_ {
57    /// The stanza-id under which the archive stored this stanza.
58    #[xml(attribute)]
59    pub id: String,
60
61    /// The same queryid as the one requested in the
62    /// [query](struct.Query.html).
63    #[xml(attribute(default))]
64    pub queryid: Option<QueryId>,
65
66    /// The actual stanza being forwarded.
67    #[xml(child)]
68    pub forwarded: Forwarded,
69}
70
71impl MessagePayload for Result_ {}
72
73/// Notes the end of a page in a query.
74#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
75#[xml(namespace = ns::MAM, name = "fin")]
76pub struct Fin {
77    /// True when the end of a MAM query has been reached.
78    #[xml(attribute(default))]
79    pub complete: bool,
80
81    /// Describes the current page, it should contain at least [first]
82    /// (with an [index]) and [last], and generally [count].
83    ///
84    /// [first]: ../rsm/struct.SetResult.html#structfield.first
85    /// [index]: ../rsm/struct.SetResult.html#structfield.first_index
86    /// [last]: ../rsm/struct.SetResult.html#structfield.last
87    /// [count]: ../rsm/struct.SetResult.html#structfield.count
88    #[xml(child)]
89    pub set: SetResult,
90}
91
92impl IqResultPayload for Fin {}
93
94/// Metadata of the first message in the archive.
95#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
96#[xml(namespace = ns::MAM, name = "start")]
97pub struct Start {
98    /// The id of the first message in the archive.
99    #[xml(attribute)]
100    pub id: String,
101
102    /// Time at which that message was sent.
103    #[xml(attribute)]
104    pub timestamp: DateTime,
105}
106
107/// Metadata of the last message in the archive.
108#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
109#[xml(namespace = ns::MAM, name = "end")]
110pub struct End {
111    /// The id of the last message in the archive.
112    #[xml(attribute)]
113    pub id: String,
114
115    /// Time at which that message was sent.
116    #[xml(attribute)]
117    pub timestamp: DateTime,
118}
119
120/// Request an archive for its metadata.
121#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
122#[xml(namespace = ns::MAM, name = "metadata")]
123pub struct MetadataQuery;
124
125impl IqGetPayload for MetadataQuery {}
126
127/// Response from the archive, containing the start and end metadata if it isn’t empty.
128#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
129#[xml(namespace = ns::MAM, name = "metadata")]
130pub struct MetadataResponse {
131    /// Metadata about the first message in the archive.
132    #[xml(child(default))]
133    pub start: Option<Start>,
134
135    /// Metadata about the last message in the archive.
136    #[xml(child(default))]
137    pub end: Option<End>,
138}
139
140impl IqResultPayload for MetadataResponse {}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145    use minidom::Element;
146    use xso::error::{Error, FromElementError};
147
148    #[cfg(target_pointer_width = "32")]
149    #[test]
150    fn test_size() {
151        assert_size!(QueryId, 12);
152        assert_size!(Query, 120);
153        assert_size!(Result_, 164);
154        assert_size!(Fin, 44);
155        assert_size!(Start, 28);
156        assert_size!(End, 28);
157        assert_size!(MetadataQuery, 0);
158        assert_size!(MetadataResponse, 56);
159    }
160
161    #[cfg(target_pointer_width = "64")]
162    #[test]
163    fn test_size() {
164        assert_size!(QueryId, 24);
165        assert_size!(Query, 240);
166        assert_size!(Result_, 312);
167        assert_size!(Fin, 88);
168        assert_size!(Start, 40);
169        assert_size!(End, 40);
170        assert_size!(MetadataQuery, 0);
171        assert_size!(MetadataResponse, 80);
172    }
173
174    #[test]
175    fn test_query() {
176        let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
177        Query::try_from(elem).unwrap();
178    }
179
180    #[test]
181    fn test_result() {
182        #[cfg(not(feature = "component"))]
183        let elem: Element = r#"<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
184  <forwarded xmlns='urn:xmpp:forward:0'>
185    <delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
186    <message xmlns='jabber:client' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
187      <body>Hail to thee</body>
188    </message>
189  </forwarded>
190</result>
191"#
192        .parse()
193        .unwrap();
194        #[cfg(feature = "component")]
195        let elem: Element = r#"<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
196  <forwarded xmlns='urn:xmpp:forward:0'>
197    <delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
198    <message xmlns='jabber:component:accept' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
199      <body>Hail to thee</body>
200    </message>
201  </forwarded>
202</result>
203"#.parse().unwrap();
204        Result_::try_from(elem).unwrap();
205    }
206
207    #[test]
208    fn test_fin() {
209        let elem: Element = r#"<fin xmlns='urn:xmpp:mam:2'>
210  <set xmlns='http://jabber.org/protocol/rsm'>
211    <first index='0'>28482-98726-73623</first>
212    <last>09af3-cc343-b409f</last>
213  </set>
214</fin>
215"#
216        .parse()
217        .unwrap();
218        Fin::try_from(elem).unwrap();
219    }
220
221    #[test]
222    fn test_query_x() {
223        let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
224  <x xmlns='jabber:x:data' type='submit'>
225    <field var='FORM_TYPE' type='hidden'>
226      <value>urn:xmpp:mam:2</value>
227    </field>
228    <field var='with'>
229      <value>juliet@capulet.lit</value>
230    </field>
231  </x>
232</query>
233"#
234        .parse()
235        .unwrap();
236        Query::try_from(elem).unwrap();
237    }
238
239    #[test]
240    fn test_query_x_set() {
241        let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
242  <x xmlns='jabber:x:data' type='submit'>
243    <field var='FORM_TYPE' type='hidden'>
244      <value>urn:xmpp:mam:2</value>
245    </field>
246    <field var='start'>
247      <value>2010-08-07T00:00:00Z</value>
248    </field>
249  </x>
250  <set xmlns='http://jabber.org/protocol/rsm'>
251    <max>10</max>
252  </set>
253</query>
254"#
255        .parse()
256        .unwrap();
257        Query::try_from(elem).unwrap();
258    }
259
260    #[test]
261    fn test_query_x_set_flipped() {
262        let elem: Element = r#"<query xmlns='urn:xmpp:mam:2'>
263  <x xmlns='jabber:x:data' type='submit'>
264    <field var='FORM_TYPE' type='hidden'>
265      <value>urn:xmpp:mam:2</value>
266    </field>
267    <field var='start'>
268      <value>2010-08-07T00:00:00Z</value>
269    </field>
270  </x>
271  <set xmlns='http://jabber.org/protocol/rsm'>
272    <max>10</max>
273  </set>
274  <flip-page/>
275</query>
276"#
277        .parse()
278        .unwrap();
279        Query::try_from(elem).unwrap();
280    }
281
282    #[test]
283    fn test_metadata() {
284        let elem: Element = r"<metadata xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
285        MetadataQuery::try_from(elem).unwrap();
286
287        let elem: Element = r"<metadata xmlns='urn:xmpp:mam:2'>
288  <start id='YWxwaGEg' timestamp='2008-08-22T21:09:04Z' />
289  <end id='b21lZ2Eg' timestamp='2020-04-20T14:34:21Z' />
290</metadata>"
291            .parse()
292            .unwrap();
293        let metadata = MetadataResponse::try_from(elem).unwrap();
294        let start = metadata.start.unwrap();
295        let end = metadata.end.unwrap();
296        assert_eq!(start.id, "YWxwaGEg");
297        assert_eq!(start.timestamp.0.timestamp(), 1219439344);
298        assert_eq!(end.id, "b21lZ2Eg");
299        assert_eq!(end.timestamp.0.timestamp(), 1587393261);
300    }
301
302    #[test]
303    fn test_invalid_child() {
304        let elem: Element = "<query xmlns='urn:xmpp:mam:2'><coucou/></query>"
305            .parse()
306            .unwrap();
307        let error = Query::try_from(elem).unwrap_err();
308        let message = match error {
309            FromElementError::Invalid(Error::Other(string)) => string,
310            _ => panic!(),
311        };
312        assert_eq!(message, "Unknown child in Query element.");
313    }
314
315    #[test]
316    fn test_serialise_empty() {
317        let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
318        let replace = Query {
319            queryid: None,
320            node: None,
321            form: None,
322            set: None,
323            flip_page: false,
324        };
325        let elem2 = replace.into();
326        assert_eq!(elem, elem2);
327    }
328
329    #[test]
330    fn test_serialize_query_with_form() {
331        let reference: Element = "<query xmlns='urn:xmpp:mam:2'><x xmlns='jabber:x:data' type='submit'><field xmlns='jabber:x:data' var='FORM_TYPE' type='hidden'><value xmlns='jabber:x:data'>urn:xmpp:mam:2</value></field><field xmlns='jabber:x:data' var='with'><value xmlns='jabber:x:data'>juliet@capulet.lit</value></field></x><flip-page/></query>"
332        .parse()
333        .unwrap();
334
335        let elem: Element = "<x xmlns='jabber:x:data' type='submit'><field xmlns='jabber:x:data' var='FORM_TYPE' type='hidden'><value xmlns='jabber:x:data'>urn:xmpp:mam:2</value></field><field xmlns='jabber:x:data' var='with'><value xmlns='jabber:x:data'>juliet@capulet.lit</value></field></x>"
336          .parse()
337          .unwrap();
338
339        let form = DataForm::try_from(elem).unwrap();
340
341        let query = Query {
342            queryid: None,
343            node: None,
344            set: None,
345            form: Some(form),
346            flip_page: true,
347        };
348        let serialized: Element = query.into();
349        assert_eq!(serialized, reference);
350    }
351
352    #[test]
353    fn test_serialize_result() {
354        let reference: Element = "<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'><forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded></result>"
355        .parse()
356        .unwrap();
357
358        let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:08:25+00:00'/><message xmlns='jabber:client' to='juliet@capulet.example/balcony' from='romeo@montague.example/home'/></forwarded>"
359          .parse()
360          .unwrap();
361
362        let forwarded = Forwarded::try_from(elem).unwrap();
363
364        let result = Result_ {
365            id: String::from("28482-98726-73623"),
366            queryid: Some(QueryId(String::from("f27"))),
367            forwarded,
368        };
369        let serialized: Element = result.into();
370        assert_eq!(serialized, reference);
371    }
372
373    #[test]
374    fn test_serialize_fin() {
375        let reference: Element = "<fin xmlns='urn:xmpp:mam:2' complete='false'><set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set></fin>"
376        .parse()
377        .unwrap();
378
379        let elem: Element = "<set xmlns='http://jabber.org/protocol/rsm'><first index='0'>28482-98726-73623</first><last>09af3-cc343-b409f</last></set>"
380          .parse()
381          .unwrap();
382
383        let set = SetResult::try_from(elem).unwrap();
384
385        let fin = Fin {
386            set,
387            complete: false,
388        };
389        let serialized: Element = fin.into();
390        assert_eq!(serialized, reference);
391    }
392}