forked from lavina/lavina
1
0
Fork 0

new version

This commit is contained in:
Nikita Vilunov 2024-05-07 22:04:32 +02:00
parent e1ab3b02ee
commit 53775fe8da
2 changed files with 20 additions and 63 deletions

View File

@ -3,8 +3,8 @@
use quick_xml::events::Event;
use lavina_core::room::{RoomId, RoomRegistry};
use proto_xmpp::bind::{BindResponse, Jid, Name, Server};
use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType, Message, MessageType};
use proto_xmpp::bind::{BindRequest, BindResponse, Jid, Name, Server};
use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType};
use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery};
use proto_xmpp::mam::{Fin, Set};
use proto_xmpp::roster::RosterQuery;
@ -17,17 +17,13 @@ use crate::XmppConnection;
impl<'a> XmppConnection<'a> {
pub async fn handle_iq(&self, output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
match iq.body {
IqClientBody::Bind(_) => {
IqClientBody::Bind(req) => {
let req = Iq {
from: None,
id: iq.id,
to: None,
r#type: IqType::Result,
body: BindResponse(Jid {
name: Some(self.user.xmpp_name.clone()),
server: Server(self.hostname.clone()),
resource: Some(self.user.xmpp_resource.clone()),
}),
body: self.bind(&req).await,
};
req.serialize(output);
}
@ -114,6 +110,14 @@ impl<'a> XmppConnection<'a> {
}
}
async fn bind(&self, req: &BindRequest) -> BindResponse {
BindResponse(Jid {
name: Some(self.user.xmpp_name.clone()),
server: Server(self.hostname.clone()),
resource: Some(self.user.xmpp_resource.clone()),
})
}
async fn disco_info(&self, to: Option<&Jid>, req: &InfoQuery) -> Result<InfoQuery, IqError> {
let identity;
let feature;
@ -277,7 +281,6 @@ mod tests {
let server = TestServer::start().await.unwrap();
server.storage.create_user("tester").await.unwrap();
let mut output = Vec::new();
let player_id = PlayerId::from("tester").unwrap();
let mut conn = server.core.players.connect_to_player(&player_id).await;
let user = Authenticated {
@ -293,60 +296,13 @@ mod tests {
hostname: "localhost".into(),
hostname_rooms: "rooms.localhost".into(),
};
let iq = Iq {
from: None,
id: "test".into(),
to: None,
r#type: IqType::Result,
body: IqClientBody::Bind(BindRequest(Resource("whatever".into()))),
};
conn.handle_iq(&mut output, iq).await;
let expected = "<iq type=\"result\" id=\"test\" xmlns=\"jabber:client\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><jid>tester@localhost/tester</jid></bind></iq>";
let mut reader = Reader::from_str(expected);
let mut result_iter = output.into_iter();
loop {
let result = result_iter.next();
let expected = reader.read_event().unwrap();
match (&result, &expected) {
(None, Event::Eof) => {
break;
}
(Some(result), expected) => match (result, expected) {
(Event::Text(result), Event::Text(expected)) => {
assert_eq!(result.as_ref(), expected.as_ref());
}
(Event::Start(result), Event::Start(expected)) => {
assert_eq!(result.name(), expected.name());
let result: HashMap<String, String> =
HashMap::from_iter(result.attributes().into_iter().map(|attr| attr.unwrap()).map(|attr| {
(
std::str::from_utf8(attr.key.as_ref()).unwrap().to_owned(),
std::str::from_utf8(attr.value.as_ref()).unwrap().to_owned(),
)
}));
let expected: HashMap<String, String> = HashMap::from_iter(
expected.attributes().into_iter().map(|attr| attr.unwrap()).map(|attr| {
(
std::str::from_utf8(attr.key.as_ref()).unwrap().to_owned(),
std::str::from_utf8(attr.value.as_ref()).unwrap().to_owned(),
)
}),
);
assert_eq!(result, expected);
}
(Event::End(result), Event::End(expected)) => {
assert_eq!(result.as_ref(), expected.as_ref());
}
(Event::Empty(result), Event::Empty(expected)) => {}
_ => {
panic!("Unexpected result: {:?}, expected: {:?}", result, expected);
}
},
_ => {
panic!("Unexpected result: {:?}, expected: {:?}", result, expected);
}
}
}
let result = conn.bind(&BindRequest(Resource("whatever".into()))).await;
let expected = BindResponse(Jid {
name: Some(Name("tester".into())),
server: Server("localhost".into()),
resource: Some(Resource("tester".into())),
});
assert_eq!(expected, result);
server.shutdown().await.unwrap();
}

View File

@ -127,6 +127,7 @@ impl FromXml for BindRequest {
}
}
#[derive(PartialEq, Eq, Debug)]
pub struct BindResponse(pub Jid);
impl ToXml for BindResponse {