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
 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-0085: Chat State Notifications
148pub mod chatstates;
149
150/// XEP-0115: Entity Capabilities
151pub mod caps;
152
153/// XEP-0166: Jingle
154pub mod jingle;
155
156/// XEP-0184: Message Delivery Receipts
157pub mod receipts;
158
159/// XEP-0199: XMPP Ping
160pub mod ping;
161
162/// XEP-0203: Delayed Delivery
163pub mod delay;
164
165/// XEP-0221: Data Forms Media Element
166pub mod media_element;
167
168/// XEP-0224: Attention
169pub mod attention;
170
171/// XEP-0234: Jingle File Transfer
172pub mod jingle_ft;
173
174/// XEP-0260: Jingle SOCKS5 Bytestreams Transport Method
175pub mod jingle_s5b;
176
177/// XEP-0261: Jingle In-Band Bytestreams Transport Method
178pub mod jingle_ibb;
179
180/// XEP-0297: Stanza Forwarding
181pub mod forwarding;
182
183/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
184pub mod hashes;
185
186/// XEP-0308: Last Message Correction
187pub mod message_correct;
188
189/// XEP-0313: Message Archive Management
190pub mod mam;
191
192/// XEP-0319: Last User Interaction in Presence
193pub mod idle;
194
195/// XEP-0359: Unique and Stable Stanza IDs
196pub mod stanza_id;
197
198/// XEP-0380: Explicit Message Encryption
199pub mod eme;
200
201/// XEP-0390: Entity Capabilities 2.0
202pub mod ecaps2;