lib.rs

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