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-0221: Data Forms Media Element
44pub mod media_element;
45
46/// XEP-0224: Attention
47pub mod attention;
48
49/// XEP-0308: Last Message Correction
50pub mod message_correct;
51
52/// XEP-0380: Explicit Message Encryption
53pub mod eme;
54
55/// XEP-0390: Entity Capabilities 2.0
56pub mod ecaps2;
57
58/// Lists every known payload of a `<message/>`.
59#[derive(Debug)]
60pub enum MessagePayload {
61    Body(body::Body),
62    ChatState(chatstates::ChatState),
63    Receipt(receipts::Receipt),
64    Attention(attention::Attention),
65    MessageCorrect(message_correct::MessageCorrect),
66    ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
67}
68
69/// Parse one of the payloads of a `<message/>` element, and return `Some` of a
70/// `MessagePayload` if parsing it succeeded, `None` otherwise.
71pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
72    if let Ok(body) = body::parse_body(elem) {
73        Some(MessagePayload::Body(body))
74    } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) {
75        Some(MessagePayload::ChatState(chatstate))
76    } else if let Ok(receipt) = receipts::parse_receipt(elem) {
77        Some(MessagePayload::Receipt(receipt))
78    } else if let Ok(attention) = attention::parse_attention(elem) {
79        Some(MessagePayload::Attention(attention))
80    } else if let Ok(replace) = message_correct::parse_message_correct(elem) {
81        Some(MessagePayload::MessageCorrect(replace))
82    } else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
83        Some(MessagePayload::ExplicitMessageEncryption(eme))
84    } else {
85        None
86    }
87}