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
112/// Error type returned by every parser on failure.
113pub mod error;
114/// XML namespace definitions used through XMPP.
115pub mod ns;
116
117/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
118pub mod message;
119/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
120pub mod presence;
121/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
122pub mod iq;
123/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
124pub mod stanza_error;
125
126/// RFC 6121: Extensible Messaging and Presence Protocol (XMPP): Instant Messaging and Presence
127pub mod roster;
128
129/// XEP-0004: Data Forms
130pub mod data_forms;
131
132/// XEP-0030: Service Discovery
133pub mod disco;
134
135/// XEP-0045: Multi-User Chat
136pub mod muc;
137
138/// XEP-0047: In-Band Bytestreams
139pub mod ibb;
140
141/// XEP-0059: Result Set Management
142pub mod rsm;
143
144/// XEP-0060: Publish-Subscribe
145pub mod pubsub;
146
147/// XEP-0077: In-Band Registration
148pub mod ibr;
149
150/// XEP-0085: Chat State Notifications
151pub mod chatstates;
152
153/// XEP-0115: Entity Capabilities
154pub mod caps;
155
156/// XEP-0166: Jingle
157pub mod jingle;
158
159/// XEP-0184: Message Delivery Receipts
160pub mod receipts;
161
162/// XEP-0199: XMPP Ping
163pub mod ping;
164
165/// XEP-0203: Delayed Delivery
166pub mod delay;
167
168/// XEP-0221: Data Forms Media Element
169pub mod media_element;
170
171/// XEP-0224: Attention
172pub mod attention;
173
174/// XEP-0234: Jingle File Transfer
175pub mod jingle_ft;
176
177/// XEP-0260: Jingle SOCKS5 Bytestreams Transport Method
178pub mod jingle_s5b;
179
180/// XEP-0261: Jingle In-Band Bytestreams Transport Method
181pub mod jingle_ibb;
182
183/// XEP-0297: Stanza Forwarding
184pub mod forwarding;
185
186/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
187pub mod hashes;
188
189/// XEP-0308: Last Message Correction
190pub mod message_correct;
191
192/// XEP-0313: Message Archive Management
193pub mod mam;
194
195/// XEP-0319: Last User Interaction in Presence
196pub mod idle;
197
198/// XEP-0359: Unique and Stable Stanza IDs
199pub mod stanza_id;
200
201/// XEP-0380: Explicit Message Encryption
202pub mod eme;
203
204/// XEP-0390: Entity Capabilities 2.0
205pub mod ecaps2;