1//! A crate parsing common XMPP elements into Rust structures.
2//!
3//! Each module implements the `TryFrom<&minidom::Element>` trait, which takes
4//! a minidom `Element` reference and returns a `Result`.
5//!
6//! Parsed structs can then be manipulated manually, and must be serialised
7//! back before being sent over the wire.
8
9// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
10// Copyright (c) 2017 Maxime “pep” Buquet <pep+code@bouah.net>
11//
12// This Source Code Form is subject to the terms of the Mozilla Public
13// License, v. 2.0. If a copy of the MPL was not distributed with this
14// file, You can obtain one at http://mozilla.org/MPL/2.0/.
15
16#![feature(try_from)]
17
18extern crate minidom;
19extern crate jid;
20extern crate base64;
21extern crate digest;
22extern crate sha_1;
23extern crate sha2;
24extern crate sha3;
25extern crate blake2;
26extern crate chrono;
27
28macro_rules! get_attr {
29 ($elem:ident, $attr:tt, $type:tt) => (
30 get_attr!($elem, $attr, $type, value, value.parse()?)
31 );
32 ($elem:ident, $attr:tt, optional, $value:ident, $func:expr) => (
33 match $elem.attr($attr) {
34 Some($value) => Some($func),
35 None => None,
36 }
37 );
38 ($elem:ident, $attr:tt, required, $value:ident, $func:expr) => (
39 match $elem.attr($attr) {
40 Some($value) => $func,
41 None => return Err(Error::ParseError(concat!("Required attribute '", $attr, "' missing."))),
42 }
43 );
44 ($elem:ident, $attr:tt, default, $value:ident, $func:expr) => (
45 match $elem.attr($attr) {
46 Some($value) => $func,
47 None => Default::default(),
48 }
49 );
50}
51
52macro_rules! generate_attribute {
53 ($elem:ident, $name:tt, {$($a:ident => $b:tt),+,}) => (
54 generate_attribute!($elem, $name, {$($a => $b),+});
55 );
56 ($elem:ident, $name:tt, {$($a:ident => $b:tt),+,}, Default = $default:ident) => (
57 generate_attribute!($elem, $name, {$($a => $b),+}, Default = $default);
58 );
59 ($elem:ident, $name:tt, {$($a:ident => $b:tt),+}) => (
60 #[derive(Debug, Clone, PartialEq)]
61 pub enum $elem {
62 $($a),+
63 }
64 impl FromStr for $elem {
65 type Err = Error;
66 fn from_str(s: &str) -> Result<$elem, Error> {
67 Ok(match s {
68 $($b => $elem::$a),+,
69 _ => return Err(Error::ParseError(concat!("Unknown value for '", $name, "' attribute."))),
70 })
71 }
72 }
73 impl IntoAttributeValue for $elem {
74 fn into_attribute_value(self) -> Option<String> {
75 Some(String::from(match self {
76 $($elem::$a => $b),+
77 }))
78 }
79 }
80 );
81 ($elem:ident, $name:tt, {$($a:ident => $b:tt),+}, Default = $default:ident) => (
82 #[derive(Debug, Clone, PartialEq)]
83 pub enum $elem {
84 $($a),+
85 }
86 impl FromStr for $elem {
87 type Err = Error;
88 fn from_str(s: &str) -> Result<$elem, Error> {
89 Ok(match s {
90 $($b => $elem::$a),+,
91 _ => return Err(Error::ParseError(concat!("Unknown value for '", $name, "' attribute."))),
92 })
93 }
94 }
95 impl IntoAttributeValue for $elem {
96 #[allow(unreachable_patterns)]
97 fn into_attribute_value(self) -> Option<String> {
98 Some(String::from(match self {
99 $elem::$default => return None,
100 $($elem::$a => $b),+
101 }))
102 }
103 }
104 impl Default for $elem {
105 fn default() -> $elem {
106 $elem::$default
107 }
108 }
109 );
110}
111
112macro_rules! generate_id {
113 ($elem:ident) => (
114 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
115 pub struct $elem(String);
116 impl FromStr for $elem {
117 type Err = Error;
118 fn from_str(s: &str) -> Result<$elem, Error> {
119 // TODO: add a way to parse that differently when needed.
120 Ok($elem(String::from(s)))
121 }
122 }
123 impl IntoAttributeValue for $elem {
124 fn into_attribute_value(self) -> Option<String> {
125 Some(self.0)
126 }
127 }
128 );
129}
130
131/// Error type returned by every parser on failure.
132pub mod error;
133/// XML namespace definitions used through XMPP.
134pub mod ns;
135
136/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
137pub mod message;
138/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
139pub mod presence;
140/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
141pub mod iq;
142/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
143pub mod stanza_error;
144
145/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
146pub mod roster;
147
148/// XEP-0004: Data Forms
149pub mod data_forms;
150
151/// XEP-0030: Service Discovery
152pub mod disco;
153
154/// XEP-0045: Multi-User Chat
155pub mod muc;
156
157/// XEP-0047: In-Band Bytestreams
158pub mod ibb;
159
160/// XEP-0059: Result Set Management
161pub mod rsm;
162
163/// XEP-0060: Publish-Subscribe
164pub mod pubsub;
165
166/// XEP-0077: In-Band Registration
167pub mod ibr;
168
169/// XEP-0085: Chat State Notifications
170pub mod chatstates;
171
172/// XEP-0115: Entity Capabilities
173pub mod caps;
174
175/// XEP-0166: Jingle
176pub mod jingle;
177
178/// XEP-0184: Message Delivery Receipts
179pub mod receipts;
180
181/// XEP-0199: XMPP Ping
182pub mod ping;
183
184/// XEP-0203: Delayed Delivery
185pub mod delay;
186
187/// XEP-0221: Data Forms Media Element
188pub mod media_element;
189
190/// XEP-0224: Attention
191pub mod attention;
192
193/// XEP-0234: Jingle File Transfer
194pub mod jingle_ft;
195
196/// XEP-0260: Jingle SOCKS5 Bytestreams Transport Method
197pub mod jingle_s5b;
198
199/// XEP-0261: Jingle In-Band Bytestreams Transport Method
200pub mod jingle_ibb;
201
202/// XEP-0297: Stanza Forwarding
203pub mod forwarding;
204
205/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
206pub mod hashes;
207
208/// XEP-0308: Last Message Correction
209pub mod message_correct;
210
211/// XEP-0313: Message Archive Management
212pub mod mam;
213
214/// XEP-0319: Last User Interaction in Presence
215pub mod idle;
216
217/// XEP-0359: Unique and Stable Stanza IDs
218pub mod stanza_id;
219
220/// XEP-0380: Explicit Message Encryption
221pub mod eme;
222
223/// XEP-0390: Entity Capabilities 2.0
224pub mod ecaps2;