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
165
166
167
168
169
170
|
use nanoid::nanoid;
use quick_xml::{
events::{BytesStart, Event},
name::QName,
Reader, Writer,
};
use crate::{JabberClient, JabberError, JID};
use crate::Result;
#[derive(Debug)]
pub struct IQ {
to: Option<JID>,
from: Option<JID>,
id: String,
r#type: IQType,
lang: Option<String>,
child: Element<'static>,
}
#[derive(Debug)]
enum IQType {
Get,
Set,
Result,
Error,
}
impl IQ {
pub async fn set<'j, R: IntoElement<'static>>(
client: &mut JabberClient<'j>,
to: Option<JID>,
from: Option<JID>,
element: R,
) -> Result<Element<'static>> {
let id = nanoid!();
let iq = IQ {
to,
from,
id: id.clone(),
r#type: IQType::Set,
lang: None,
child: Element::from(element),
};
println!("{:?}", iq);
let iq = Element::from(iq);
println!("{:?}", iq);
iq.write(&mut client.writer).await?;
let result = Element::read(&mut client.reader).await?;
let iq = IQ::try_from(result)?;
if iq.id == id {
return Ok(iq.child);
}
Err(JabberError::IDMismatch)
}
}
impl<'e> IntoElement<'e> for IQ {
fn event(&self) -> quick_xml::events::Event<'e> {
let mut start = BytesStart::new("iq");
if let Some(to) = &self.to {
start.push_attribute(("to", to.to_string().as_str()));
}
if let Some(from) = &self.from {
start.push_attribute(("from", from.to_string().as_str()));
}
start.push_attribute(("id", self.id.as_str()));
match self.r#type {
IQType::Get => start.push_attribute(("type", "get")),
IQType::Set => start.push_attribute(("type", "set")),
IQType::Result => start.push_attribute(("type", "result")),
IQType::Error => start.push_attribute(("type", "error")),
}
if let Some(lang) = &self.lang {
start.push_attribute(("from", lang.to_string().as_str()));
}
quick_xml::events::Event::Start(start)
}
fn children(&self) -> Option<Vec<Element<'e>>> {
Some(vec![self.child.clone()])
}
}
impl TryFrom<Element<'static>> for IQ {
type Error = JabberError;
fn try_from(element: Element<'static>) -> std::result::Result<Self, Self::Error> {
if let Event::Start(start) = &element.event {
if start.name() == QName(b"iq") {
let mut to: Option<JID> = None;
let mut from: Option<JID> = None;
let mut id = None;
let mut r#type = None;
let mut lang = None;
start
.attributes()
.into_iter()
.try_for_each(|attribute| -> Result<()> {
if let Ok(attribute) = attribute {
let buf: Vec<u8> = Vec::new();
let reader = Reader::from_reader(buf);
match attribute.key {
QName(b"to") => {
to = Some(
attribute
.decode_and_unescape_value(&reader)
.or(Err(JabberError::Utf8Decode))?
.into_owned()
.try_into()?,
)
}
QName(b"from") => {
from = Some(
attribute
.decode_and_unescape_value(&reader)
.or(Err(JabberError::Utf8Decode))?
.into_owned()
.try_into()?,
)
}
QName(b"id") => {
id = Some(
attribute
.decode_and_unescape_value(&reader)
.or(Err(JabberError::Utf8Decode))?
.into_owned(),
)
}
QName(b"type") => {
let value = attribute
.decode_and_unescape_value(&reader)
.or(Err(JabberError::Utf8Decode))?;
match value.as_ref() {
"get" => r#type = Some(IQType::Get),
"set" => r#type = Some(IQType::Set),
"result" => r#type = Some(IQType::Result),
"error" => r#type = Some(IQType::Error),
_ => return Err(JabberError::ParseError),
}
}
QName(b"lang") => {
lang = Some(
attribute
.decode_and_unescape_value(&reader)
.or(Err(JabberError::Utf8Decode))?
.into_owned(),
)
}
_ => return Err(JabberError::UnknownAttribute),
}
}
Ok(())
})?;
let iq = IQ {
to,
from,
id: id.ok_or(JabberError::NoID)?,
r#type: r#type.ok_or(JabberError::NoType)?,
lang,
child: element.child()?.to_owned(),
};
return Ok(iq);
}
}
Err(JabberError::ParseError)
}
}
|