1use std::str::FromStr;
2
3use minidom::{Element, IntoAttributeValue};
4use base64;
5
6use error::Error;
7
8use ns;
9
10#[derive(Debug, Clone, PartialEq)]
11pub enum Stanza {
12 Iq,
13 Message,
14}
15
16impl Default for Stanza {
17 fn default() -> Stanza {
18 Stanza::Iq
19 }
20}
21
22impl FromStr for Stanza {
23 type Err = Error;
24
25 fn from_str(s: &str) -> Result<Stanza, Error> {
26 Ok(match s {
27 "iq" => Stanza::Iq,
28 "message" => Stanza::Message,
29
30 _ => return Err(Error::ParseError("Invalid 'stanza' attribute.")),
31 })
32 }
33}
34
35impl IntoAttributeValue for Stanza {
36 fn into_attribute_value(self) -> Option<String> {
37 match self {
38 Stanza::Iq => None,
39 Stanza::Message => Some(String::from("message")),
40 }
41 }
42}
43
44#[derive(Debug, Clone)]
45pub enum IBB {
46 Open {
47 block_size: u16,
48 sid: String,
49 stanza: Stanza,
50 },
51 Data {
52 seq: u16,
53 sid: String,
54 data: Vec<u8>,
55 },
56 Close {
57 sid: String,
58 },
59}
60
61fn required_attr<T: FromStr>(root: &Element, attr: &str, err: Error) -> Result<T, Error> {
62 root.attr(attr)
63 .and_then(|value| value.parse().ok())
64 .ok_or(err)
65}
66
67pub fn parse_ibb(root: &Element) -> Result<IBB, Error> {
68 if root.is("open", ns::IBB) {
69 for _ in root.children() {
70 return Err(Error::ParseError("Unknown child in open element."));
71 }
72 let block_size = required_attr(root, "block-size", Error::ParseError("Required attribute 'block-size' missing in open element."))?;
73 let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in open element."))?;
74 let stanza = match root.attr("stanza") {
75 Some(stanza) => stanza.parse()?,
76 None => Default::default(),
77 };
78 Ok(IBB::Open {
79 block_size: block_size,
80 sid: sid,
81 stanza: stanza
82 })
83 } else if root.is("data", ns::IBB) {
84 for _ in root.children() {
85 return Err(Error::ParseError("Unknown child in data element."));
86 }
87 let seq = required_attr(root, "seq", Error::ParseError("Required attribute 'seq' missing in data element."))?;
88 let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
89 let data = base64::decode(&root.text())?;
90 Ok(IBB::Data {
91 seq: seq,
92 sid: sid,
93 data: data
94 })
95 } else if root.is("close", ns::IBB) {
96 let sid = required_attr(root, "sid", Error::ParseError("Required attribute 'sid' missing in data element."))?;
97 for _ in root.children() {
98 return Err(Error::ParseError("Unknown child in close element."));
99 }
100 Ok(IBB::Close {
101 sid: sid,
102 })
103 } else {
104 Err(Error::ParseError("This is not an ibb element."))
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use minidom::Element;
111 use error::Error;
112 use ibb;
113
114 #[test]
115 fn test_simple() {
116 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
117 let open = ibb::parse_ibb(&elem).unwrap();
118 match open {
119 ibb::IBB::Open { block_size, sid, stanza } => {
120 assert_eq!(block_size, 3);
121 assert_eq!(sid, "coucou");
122 assert_eq!(stanza, ibb::Stanza::Iq);
123 },
124 _ => panic!(),
125 }
126
127 let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
128 let data = ibb::parse_ibb(&elem).unwrap();
129 match data {
130 ibb::IBB::Data { seq, sid, data } => {
131 assert_eq!(seq, 0);
132 assert_eq!(sid, "coucou");
133 assert_eq!(data, vec!(0, 0, 0));
134 },
135 _ => panic!(),
136 }
137
138 let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
139 let close = ibb::parse_ibb(&elem).unwrap();
140 match close {
141 ibb::IBB::Close { sid } => {
142 assert_eq!(sid, "coucou");
143 },
144 _ => panic!(),
145 }
146 }
147
148 #[test]
149 fn test_invalid() {
150 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap();
151 let error = ibb::parse_ibb(&elem).unwrap_err();
152 let message = match error {
153 Error::ParseError(string) => string,
154 _ => panic!(),
155 };
156 assert_eq!(message, "Required attribute 'block-size' missing in open element.");
157
158 // TODO: maybe make a better error message here.
159 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap();
160 let error = ibb::parse_ibb(&elem).unwrap_err();
161 let message = match error {
162 Error::ParseError(string) => string,
163 _ => panic!(),
164 };
165 assert_eq!(message, "Required attribute 'block-size' missing in open element.");
166
167 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap();
168 let error = ibb::parse_ibb(&elem).unwrap_err();
169 let message = match error {
170 Error::ParseError(string) => string,
171 _ => panic!(),
172 };
173 assert_eq!(message, "Required attribute 'sid' missing in open element.");
174 }
175
176 #[test]
177 fn test_invalid_stanza() {
178 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
179 let error = ibb::parse_ibb(&elem).unwrap_err();
180 let message = match error {
181 Error::ParseError(string) => string,
182 _ => panic!(),
183 };
184 assert_eq!(message, "Invalid 'stanza' attribute.");
185 }
186}