plugins/ibb: IBB enum not available in the parser anymore

This commit is contained in:
Maxime “pep” Buquet 2017-12-26 21:10:24 +00:00
parent 1a8ea2e383
commit 1a9a100bdc

View file

@ -10,7 +10,7 @@ use jid::Jid;
use plugins::stanza::Iq; use plugins::stanza::Iq;
use plugins::disco::DiscoPlugin; use plugins::disco::DiscoPlugin;
use xmpp_parsers::iq::{IqType, IqSetPayload}; use xmpp_parsers::iq::{IqType, IqSetPayload};
use xmpp_parsers::ibb::{IBB, Stanza}; use xmpp_parsers::ibb::{Open, Data, Close, Stanza};
use xmpp_parsers::stanza_error::{StanzaError, ErrorType, DefinedCondition}; use xmpp_parsers::stanza_error::{StanzaError, ErrorType, DefinedCondition};
use xmpp_parsers::ns; use xmpp_parsers::ns;
@ -86,74 +86,79 @@ impl IbbPlugin {
} }
} }
fn handle_ibb(&self, from: Jid, ibb: IBB) -> Result<(), StanzaError> { fn handle_ibb_open(&self, from: Jid, open: Open) -> Result<(), StanzaError> {
let mut sessions = self.sessions.lock().unwrap(); let mut sessions = self.sessions.lock().unwrap();
match ibb { let Open { block_size, sid, stanza } = open;
IBB::Open { block_size, sid, stanza } => { match sessions.entry((from.clone(), sid.clone())) {
match sessions.entry((from.clone(), sid.clone())) { Entry::Vacant(_) => Ok(()),
Entry::Vacant(_) => Ok(()), Entry::Occupied(_) => Err(generate_error(
Entry::Occupied(_) => Err(generate_error( ErrorType::Cancel,
ErrorType::Cancel, DefinedCondition::NotAcceptable,
DefinedCondition::NotAcceptable, "This session is already open."
"This session is already open." )),
)), }?;
}?; let session = Session {
let session = Session { stanza,
stanza, block_size,
block_size, cur_seq: 65535u16,
cur_seq: 65535u16, };
}; sessions.insert((from, sid), session.clone());
sessions.insert((from, sid), session.clone()); self.proxy.dispatch(IbbOpen {
self.proxy.dispatch(IbbOpen { session: session,
session: session, });
}); Ok(())
}, }
IBB::Data { seq, sid, data } => {
let entry = match sessions.entry((from, sid)) { fn handle_ibb_data(&self, from: Jid, data: Data) -> Result<(), StanzaError> {
Entry::Occupied(entry) => Ok(entry), let mut sessions = self.sessions.lock().unwrap();
Entry::Vacant(_) => Err(generate_error( let Data { seq, sid, data } = data;
ErrorType::Cancel, let entry = match sessions.entry((from, sid)) {
DefinedCondition::ItemNotFound, Entry::Occupied(entry) => Ok(entry),
"This session doesnt exist." Entry::Vacant(_) => Err(generate_error(
)), ErrorType::Cancel,
}?; DefinedCondition::ItemNotFound,
let mut session = entry.into_mut(); "This session doesnt exist."
if session.stanza != Stanza::Iq { )),
return Err(generate_error( }?;
ErrorType::Cancel, let session = entry.into_mut();
DefinedCondition::NotAcceptable, if session.stanza != Stanza::Iq {
"Wrong stanza type." return Err(generate_error(
)) ErrorType::Cancel,
} DefinedCondition::NotAcceptable,
let cur_seq = session.cur_seq.wrapping_add(1); "Wrong stanza type."
if seq != cur_seq { ))
return Err(generate_error(
ErrorType::Cancel,
DefinedCondition::NotAcceptable,
"Wrong seq number."
))
}
session.cur_seq = cur_seq;
self.proxy.dispatch(IbbData {
session: session.clone(),
data,
});
},
IBB::Close { sid } => {
let entry = match sessions.entry((from, sid)) {
Entry::Occupied(entry) => Ok(entry),
Entry::Vacant(_) => Err(generate_error(
ErrorType::Cancel,
DefinedCondition::ItemNotFound,
"This session doesnt exist."
)),
}?;
let session = entry.remove();
self.proxy.dispatch(IbbClose {
session,
});
},
} }
let cur_seq = session.cur_seq.wrapping_add(1);
if seq != cur_seq {
return Err(generate_error(
ErrorType::Cancel,
DefinedCondition::NotAcceptable,
"Wrong seq number."
))
}
session.cur_seq = cur_seq;
self.proxy.dispatch(IbbData {
session: session.clone(),
data,
});
Ok(())
}
fn handle_ibb_close(&self, from: Jid, close: Close) -> Result<(), StanzaError> {
let mut sessions = self.sessions.lock().unwrap();
let Close { sid } = close;
let entry = match sessions.entry((from, sid)) {
Entry::Occupied(entry) => Ok(entry),
Entry::Vacant(_) => Err(generate_error(
ErrorType::Cancel,
DefinedCondition::ItemNotFound,
"This session doesnt exist."
)),
}?;
let session = entry.remove();
self.proxy.dispatch(IbbClose {
session,
});
Ok(()) Ok(())
} }
@ -164,8 +169,20 @@ impl IbbPlugin {
let id = iq.id.unwrap(); let id = iq.id.unwrap();
// TODO: use an intermediate plugin to parse this payload. // TODO: use an intermediate plugin to parse this payload.
let payload = match IqSetPayload::try_from(payload) { let payload = match IqSetPayload::try_from(payload) {
Ok(IqSetPayload::IBB(ibb)) => { Ok(IqSetPayload::IbbOpen(open)) => {
match self.handle_ibb(from.clone(), ibb) { match self.handle_ibb_open(from.clone(), open) {
Ok(_) => IqType::Result(None),
Err(error) => IqType::Error(error),
}
},
Ok(IqSetPayload::IbbData(data)) => {
match self.handle_ibb_data(from.clone(), data) {
Ok(_) => IqType::Result(None),
Err(error) => IqType::Error(error),
}
},
Ok(IqSetPayload::IbbClose(close)) => {
match self.handle_ibb_close(from.clone(), close) {
Ok(_) => IqType::Result(None), Ok(_) => IqType::Result(None),
Err(error) => IqType::Error(error), Err(error) => IqType::Error(error),
} }