lib.rs

 1//! A crate parsing common XMPP elements into Rust structures.
 2//!
 3//! The main entrypoint is `parse_message_payload`, it takes a minidom
 4//! `Element` reference and optionally returns `Some(MessagePayload)` if the
 5//! parsing succeeded.
 6//!
 7//! Parsed structs can then be manipulated internally, and serialised back
 8//! before being sent over the wire.
 9
10extern crate minidom;
11extern crate base64;
12use minidom::Element;
13
14/// Error type returned by every parser on failure.
15pub mod error;
16/// XML namespace definitions used through XMPP.
17pub mod ns;
18
19/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
20pub mod body;
21
22/// XEP-0004: Data Forms
23pub mod data_forms;
24
25/// XEP-0030: Service Discovery
26pub mod disco;
27
28/// XEP-0047: In-Band Bytestreams
29pub mod ibb;
30
31/// XEP-0085: Chat State Notifications
32pub mod chatstates;
33
34/// XEP-0166: Jingle
35pub mod jingle;
36
37/// XEP-0184: Message Delivery Receipts
38pub mod receipts;
39
40/// XEP-0199: XMPP Ping
41pub mod ping;
42
43/// XEP-0203: Delayed Delivery
44pub mod delay;
45
46/// XEP-0221: Data Forms Media Element
47pub mod media_element;
48
49/// XEP-0224: Attention
50pub mod attention;
51
52/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
53pub mod hashes;
54
55/// XEP-0308: Last Message Correction
56pub mod message_correct;
57
58/// XEP-0380: Explicit Message Encryption
59pub mod eme;
60
61/// XEP-0390: Entity Capabilities 2.0
62pub mod ecaps2;
63
64/// Lists every known payload of a `<message/>`.
65#[derive(Debug)]
66pub enum MessagePayload {
67    Body(body::Body),
68    ChatState(chatstates::ChatState),
69    Receipt(receipts::Receipt),
70    Delay(delay::Delay),
71    Attention(attention::Attention),
72    MessageCorrect(message_correct::MessageCorrect),
73    ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
74}
75
76/// Parse one of the payloads of a `<message/>` element, and return `Some` of a
77/// `MessagePayload` if parsing it succeeded, `None` otherwise.
78pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
79    if let Ok(body) = body::parse_body(elem) {
80        Some(MessagePayload::Body(body))
81    } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) {
82        Some(MessagePayload::ChatState(chatstate))
83    } else if let Ok(receipt) = receipts::parse_receipt(elem) {
84        Some(MessagePayload::Receipt(receipt))
85    } else if let Ok(delay) = delay::parse_delay(elem) {
86        Some(MessagePayload::Delay(delay))
87    } else if let Ok(attention) = attention::parse_attention(elem) {
88        Some(MessagePayload::Attention(attention))
89    } else if let Ok(replace) = message_correct::parse_message_correct(elem) {
90        Some(MessagePayload::MessageCorrect(replace))
91    } else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
92        Some(MessagePayload::ExplicitMessageEncryption(eme))
93    } else {
94        None
95    }
96}