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