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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
use std::collections::{HashMap, HashSet};
use peanuts::element::{Content, ElementBuilder, FromElement, IntoElement, NamespaceDeclaration};
use peanuts::XML_NS;
use peanuts::{element::Name, Element};
use tracing::debug;
use crate::stanza::bind;
use crate::{Error, JID};
use super::client;
use super::sasl::{self, Mechanisms};
use super::starttls::{self, StartTls};
pub const XMLNS: &str = "http://etherx.jabber.org/streams";
// MUST be qualified by stream namespace
// #[derive(XmlSerialize, XmlDeserialize)]
// #[peanuts(xmlns = XMLNS)]
#[derive(Debug)]
pub struct Stream {
pub from: Option<JID>,
to: Option<JID>,
id: Option<String>,
version: Option<String>,
// TODO: lang enum
lang: Option<String>,
// #[peanuts(content)]
// content: Message,
}
impl FromElement for Stream {
fn from_element(mut element: Element) -> std::result::Result<Self, peanuts::DeserializeError> {
element.check_namespace(XMLNS)?;
element.check_name("stream")?;
let from = element.attribute_opt("from")?;
let to = element.attribute_opt("to")?;
let id = element.attribute_opt("id")?;
let version = element.attribute_opt("version")?;
let lang = element.attribute_opt_namespaced("lang", peanuts::XML_NS)?;
Ok(Stream {
from,
to,
id,
version,
lang,
})
}
}
impl IntoElement for Stream {
fn builder(&self) -> ElementBuilder {
Element::builder("stream", Some(XMLNS.to_string()))
.push_namespace_declaration_override(Some("stream"), XMLNS)
.push_namespace_declaration_override(None::<&str>, client::XMLNS)
.push_attribute_opt("to", self.to.clone())
.push_attribute_opt("from", self.from.clone())
.push_attribute_opt("id", self.id.clone())
.push_attribute_opt("version", self.version.clone())
.push_attribute_opt_namespaced(peanuts::XML_NS, "to", self.lang.clone())
}
}
impl<'s> Stream {
pub fn new(
from: Option<JID>,
to: Option<JID>,
id: Option<String>,
version: Option<String>,
lang: Option<String>,
) -> Self {
Self {
from,
to,
id,
version,
lang,
}
}
/// 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<String>, lang: String) -> Self {
Self {
from,
to: Some(to),
id,
version: Some("1.0".to_string()),
lang: Some(lang),
}
}
}
#[derive(Debug)]
pub struct Features {
pub features: Vec<Feature>,
}
impl IntoElement for Features {
fn builder(&self) -> ElementBuilder {
Element::builder("features", Some(XMLNS)).push_children(self.features.clone())
}
}
impl FromElement for Features {
fn from_element(
mut element: Element,
) -> std::result::Result<Features, peanuts::DeserializeError> {
element.check_namespace(XMLNS)?;
element.check_name("features")?;
debug!("got features stanza");
let features = element.children()?;
debug!("got features period");
Ok(Features { features })
}
}
#[derive(Debug, Clone)]
pub enum Feature {
StartTls(StartTls),
Sasl(Mechanisms),
Bind,
Unknown,
}
impl IntoElement for Feature {
fn builder(&self) -> ElementBuilder {
match self {
Feature::StartTls(start_tls) => start_tls.builder(),
Feature::Sasl(mechanisms) => mechanisms.builder(),
Feature::Bind => todo!(),
Feature::Unknown => todo!(),
}
}
}
impl FromElement for Feature {
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
let identity = element.identify();
debug!("identity: {:?}", identity);
match element.identify() {
(Some(starttls::XMLNS), "starttls") => {
debug!("identified starttls");
Ok(Feature::StartTls(StartTls::from_element(element)?))
}
(Some(sasl::XMLNS), "mechanisms") => {
debug!("identified mechanisms");
Ok(Feature::Sasl(Mechanisms::from_element(element)?))
}
(Some(bind::XMLNS), "bind") => {
debug!("identified bind");
Ok(Feature::Bind)
}
_ => {
debug!("identified unknown feature");
Ok(Feature::Unknown)
}
}
}
}
|