summaryrefslogtreecommitdiffstats
path: root/src/stanza/stream.rs
blob: 07f7e6e404afb50dde3e15bd861da9c3697f7319 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use serde::Serialize;

use crate::JID;

pub static XMLNS: &str = "http://etherx.jabber.org/streams";
pub static XMLNS_CLIENT: &str = "jabber:client";

// MUST be qualified by stream namespace
#[derive(Serialize)]
pub struct Stream<'s> {
    #[serde(rename = "@from")]
    from: Option<&'s JID>,
    #[serde(rename = "@to")]
    to: Option<&'s JID>,
    #[serde(rename = "@id")]
    id: Option<&'s str>,
    #[serde(rename = "@version")]
    version: Option<&'s str>,
    // TODO: lang enum
    #[serde(rename = "@lang")]
    lang: Option<&'s str>,
    #[serde(rename = "@xmlns")]
    xmlns: &'s str,
    #[serde(rename = "@xmlns:stream")]
    xmlns_stream: &'s str,
}

impl Stream {
    pub fn new(
        from: Option<&JID>,
        to: Option<&JID>,
        id: Option<&str>,
        version: Option<&str>,
        lang: Option<&str>,
    ) -> Self {
        Self {
            from,
            to,
            id,
            version,
            lang,
            xmlns: XMLNS_CLIENT,
            xmlns_stream: XMLNS,
        }
    }

    /// For initial stream headers, the initiating entity SHOULD include the 'xml:lang' attribute.
    /// For privacy, it is better to not set `from` when sending a client stanza over an unencrypted connection.
    pub fn new_client(from: Option<&JID>, to: &JID, id: Option<&str>, lang: &str) -> Self {
        Self {
            from,
            to: Some(to),
            id,
            version: Some("1.0"),
            lang: Some(lang),
            xmlns: XMLNS_CLIENT,
            xmlns_stream: XMLNS,
        }
    }
}