forked from lavina/lavina
1
0
Fork 0

split xmpp stanzas handling into functions

This commit is contained in:
Nikita Vilunov 2023-03-30 14:49:39 +02:00
parent 123781d145
commit 65471a6c7f
1 changed files with 114 additions and 93 deletions

View File

@ -1,6 +1,5 @@
mod proto;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader as SyncBufReader;
@ -26,13 +25,15 @@ use crate::prelude::*;
use crate::protos::xmpp;
use crate::protos::xmpp::bind::{BindResponse, Jid, Name, Resource, Server};
use crate::protos::xmpp::client::{Iq, Presence};
use crate::protos::xmpp::disco::{InfoQuery, Feature, ItemQuery, Item};
use crate::protos::xmpp::disco::{Feature, InfoQuery, Item, ItemQuery};
use crate::protos::xmpp::roster::RosterQuery;
use crate::protos::xmpp::session::Session;
use crate::protos::xmpp::stream::*;
use crate::util::xml::{Continuation, FromXml, Parser, ToXml};
use crate::util::Terminator;
use self::proto::{ClientPacket, IqClientBody};
#[derive(Deserialize, Debug, Clone)]
pub struct ServerConfig {
pub listen_on: SocketAddr,
@ -285,8 +286,36 @@ async fn socket_final(
Continuation::Final(res) => {
let res = res?;
dbg!(&res);
match res {
proto::ClientPacket::Iq(iq) => match iq.body {
handle_packet(&mut events, res);
for i in &events {
xml_writer.write_event_async(i).await?;
}
events.clear();
xml_writer.get_mut().flush().await?;
parser = proto::ClientPacket::parse();
}
Continuation::Continue(p) => parser = p,
}
}
}
fn handle_packet(output: &mut Vec<Event<'static>>, packet: ClientPacket) {
match packet {
proto::ClientPacket::Iq(iq) => handle_iq(output, iq),
proto::ClientPacket::Message(_) => todo!(),
proto::ClientPacket::Presence(p) => {
let response = Presence::<()> {
to: Some("darova@localhost/kek".to_string()),
from: Some("darova@localhost/kek".to_string()),
..Default::default()
};
response.serialize(output);
}
}
}
fn handle_iq(output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
match iq.body {
proto::IqClientBody::Bind(b) => {
let req = Iq {
from: None,
@ -299,7 +328,7 @@ async fn socket_final(
resource: Some(Resource("kek".to_string())),
}),
};
req.serialize(&mut events);
req.serialize(output);
}
proto::IqClientBody::Session(_) => {
let req = Iq {
@ -309,7 +338,7 @@ async fn socket_final(
r#type: xmpp::client::IqType::Result,
body: Session,
};
req.serialize(&mut events);
req.serialize(output);
}
proto::IqClientBody::Roster(_) => {
let req = Iq {
@ -319,7 +348,7 @@ async fn socket_final(
r#type: xmpp::client::IqType::Result,
body: RosterQuery,
};
req.serialize(&mut events);
req.serialize(output);
}
proto::IqClientBody::DiscoInfo(info) => {
let response = disco_info(iq.to.as_deref(), &info);
@ -330,7 +359,7 @@ async fn socket_final(
r#type: xmpp::client::IqType::Result,
body: response,
};
req.serialize(&mut events);
req.serialize(output);
}
proto::IqClientBody::DiscoItem(item) => {
let response = disco_items(iq.to.as_deref(), &item);
@ -341,7 +370,7 @@ async fn socket_final(
r#type: xmpp::client::IqType::Result,
body: response,
};
req.serialize(&mut events);
req.serialize(output);
}
_ => {
let req = Iq {
@ -351,30 +380,9 @@ async fn socket_final(
r#type: xmpp::client::IqType::Error,
body: (),
};
req.serialize(&mut events);
}
},
proto::ClientPacket::Message(_) => todo!(),
proto::ClientPacket::Presence(p) => {
let response = Presence::<()> {
to: Some("darova@localhost/kek".to_string()),
from: Some("darova@localhost/kek".to_string()),
..Default::default()
};
response.serialize(&mut events);
req.serialize(output);
}
}
for i in &events {
xml_writer.write_event_async(i).await?;
}
events.clear();
xml_writer.get_mut().flush().await?;
parser = proto::ClientPacket::parse();
}
Continuation::Continue(p) => parser = p,
}
}
Ok(())
}
fn disco_info(to: Option<&str>, req: &InfoQuery) -> InfoQuery {
@ -396,33 +404,46 @@ fn disco_info(to: Option<&str>, req: &InfoQuery) -> InfoQuery {
}
_ => vec![],
};
InfoQuery { node: None, identity: vec![], feature }
InfoQuery {
node: None,
identity: vec![],
feature,
}
}
fn disco_items(to: Option<&str>, req: &ItemQuery) -> ItemQuery {
let item = match to {
Some("localhost") => {
vec![
Item {
jid: Jid { name: None, server: Server("rooms.localhost".to_string()), resource: None },
vec![Item {
jid: Jid {
name: None,
server: Server("rooms.localhost".to_string()),
resource: None,
},
name: None,
node: None,
}
]
}]
}
Some("rooms.localhost") => {
vec![
Item {
jid: Jid { name: Some(Name("room1".to_string())), server: Server("rooms.localhost".to_string()), resource: None },
jid: Jid {
name: Some(Name("room1".to_string())),
server: Server("rooms.localhost".to_string()),
resource: None,
},
name: None,
node: None,
},
Item {
jid: Jid { name: Some(Name("room2".to_string())), server: Server("rooms.localhost".to_string()), resource: None },
jid: Jid {
name: Some(Name("room2".to_string())),
server: Server("rooms.localhost".to_string()),
resource: None,
},
name: None,
node: None,
}
},
]
}
_ => vec![],