diff --git a/crates/lavina-core/src/player.rs b/crates/lavina-core/src/player.rs index 0ab8e09..a35a263 100644 --- a/crates/lavina-core/src/player.rs +++ b/crates/lavina-core/src/player.rs @@ -8,6 +8,7 @@ //! A player actor is a serial handler of commands from a single player. It is preferable to run all per-player validations in the player actor, //! so that they don't overload the room actor. use std::collections::{HashMap, HashSet}; +use std::ops::Deref; use std::sync::Arc; use chrono::{DateTime, Utc}; @@ -125,6 +126,15 @@ impl PlayerConnection { self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await; Ok(deferred.await?) } + + /// Handler in [Player::check_user_existence]. + #[tracing::instrument(skip(self), name = "PlayerConnection::check_user_existence")] + pub async fn check_user_existence(&self, recipient: PlayerId) -> Result { + let (promise, deferred) = oneshot(); + let cmd = ClientCommand::GetInfo { recipient, promise }; + self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await; + Ok(deferred.await?) + } } /// Handle to a player actor. @@ -201,6 +211,15 @@ pub enum ClientCommand { body: Str, promise: Promise<()>, }, + GetInfo { + recipient: PlayerId, + promise: Promise, + }, +} + +pub enum GetInfoResult { + UserExists, + UserDoesntExist, } pub enum JoinResult { @@ -537,6 +556,10 @@ impl Player { self.send_dialog_message(connection_id, recipient, body).await; let _ = promise.send(()); } + ClientCommand::GetInfo { recipient, promise } => { + let result = self.check_user_existence(recipient).await; + let _ = promise.send(result); + } } } @@ -678,6 +701,15 @@ impl Player { self.broadcast_update(update, connection_id).await; } + #[tracing::instrument(skip(self), name = "Player::check_user_existence")] + async fn check_user_existence(&self, recipient: PlayerId) -> GetInfoResult { + if self.storage.check_user_existence(recipient.as_inner().as_ref()).await.unwrap() { + GetInfoResult::UserExists + } else { + GetInfoResult::UserDoesntExist + } + } + /// Broadcasts an update to all connections except the one with the given id. /// /// This is called after handling a client command. diff --git a/crates/lavina-core/src/repo/mod.rs b/crates/lavina-core/src/repo/mod.rs index d173849..0089c22 100644 --- a/crates/lavina-core/src/repo/mod.rs +++ b/crates/lavina-core/src/repo/mod.rs @@ -7,7 +7,7 @@ use anyhow::anyhow; use chrono::{DateTime, Utc}; use serde::Deserialize; use sqlx::sqlite::SqliteConnectOptions; -use sqlx::{ConnectOptions, Connection, FromRow, Sqlite, SqliteConnection, Transaction}; +use sqlx::{ConnectOptions, Connection, Execute, FromRow, Sqlite, SqliteConnection, Transaction}; use tokio::sync::Mutex; use crate::prelude::*; @@ -56,6 +56,17 @@ impl Storage { Ok(res) } + #[tracing::instrument(skip(self), name = "Storage::check_user_existence")] + pub async fn check_user_existence(&self, username: &str) -> Result { + let mut executor = self.conn.lock().await; + let result: Option<(String,)> = sqlx::query_as("select name from users where name = ?;") + .bind(username) + .fetch_optional(&mut *executor) + .await?; + + Ok(result.is_some()) + } + #[tracing::instrument(skip(self), name = "Storage::retrieve_room_by_name")] pub async fn retrieve_room_by_name(&self, name: &str) -> Result> { let mut executor = self.conn.lock().await; diff --git a/crates/projection-irc/src/handler.rs b/crates/projection-irc/src/handler.rs new file mode 100644 index 0000000..2e7e0da --- /dev/null +++ b/crates/projection-irc/src/handler.rs @@ -0,0 +1,18 @@ +use lavina_core::{player::PlayerConnection, prelude::Str, LavinaCore}; +use std::future::Future; +use tokio::io::AsyncWrite; + +pub struct IrcConnection<'a, T: AsyncWrite + Unpin> { + pub server_name: Str, + /// client is nick of requester + pub client: Str, + pub writer: &'a mut T, + pub player_connection: &'a mut PlayerConnection, +} + +pub trait Handler +where + T: AsyncWrite + Unpin, +{ + fn handle(&self, arg: IrcConnection) -> impl Future>; +} diff --git a/crates/projection-irc/src/lib.rs b/crates/projection-irc/src/lib.rs index c153c09..bb64824 100644 --- a/crates/projection-irc/src/lib.rs +++ b/crates/projection-irc/src/lib.rs @@ -27,11 +27,14 @@ use proto_irc::server::{AwayStatus, ServerMessage, ServerMessageBody}; use proto_irc::user::PrefixedNick; use proto_irc::{Chan, Recipient, Tag}; use sasl::AuthBody; - mod cap; +use handler::Handler; +mod whois; use crate::cap::Capabilities; +mod handler; + pub const APP_VERSION: &str = concat!("lavina", "_", env!("CARGO_PKG_VERSION")); #[derive(Deserialize, Debug, Clone)] @@ -802,6 +805,17 @@ async fn handle_incoming_message( log::warn!("Local chans not supported"); } }, + ClientMessage::Whois { arg } => { + arg.handle(handler::IrcConnection { + server_name: config.server_name.clone(), + client: user.nickname.clone(), + writer, + player_connection: user_handle, + }) + .await?; + + writer.flush().await?; + } ClientMessage::Mode { target } => { match target { Recipient::Nick(nickname) => { diff --git a/crates/projection-irc/src/whois.rs b/crates/projection-irc/src/whois.rs new file mode 100644 index 0000000..f48b90f --- /dev/null +++ b/crates/projection-irc/src/whois.rs @@ -0,0 +1,67 @@ +use lavina_core::{ + player::{GetInfoResult, PlayerId}, + prelude::Str, +}; +use proto_irc::{ + client::command_args::Whois, + commands::whois::{ + error::{ErrNoNicknameGiven431, ErrNoSuchNick401}, + response::RplEndOfWhois318, + }, + response::{IrcResponseMessage, WriteResponse}, +}; +use tokio::io::AsyncWrite; + +use crate::handler::{Handler, IrcConnection}; + +impl Handler for Whois { + async fn handle(&self, body: IrcConnection<'_, T>) -> anyhow::Result<()> { + match self { + Whois::Nick(nick) => handle_nick_target(nick.clone(), body).await?, + Whois::TargetNick(_, nick) => handle_nick_target(nick.clone(), body).await?, + Whois::EmptyArgs => { + let IrcConnection { + server_name, + mut writer, + .. + } = body; + IrcResponseMessage::empty_tags( + Some(server_name.clone()), + ErrNoNicknameGiven431::new(server_name.clone()), + ) + .write_response(&mut writer) + .await? + } + } + Ok(()) + } +} + +async fn handle_nick_target(nick: Str, body: IrcConnection<'_, impl AsyncWrite + Unpin>) -> anyhow::Result<()> { + let IrcConnection { + server_name, + mut writer, + client, + player_connection, + } = body; + + if let GetInfoResult::UserDoesntExist = + player_connection.check_user_existence(PlayerId::from(nick.clone())?).await? + { + IrcResponseMessage::empty_tags( + Some(server_name.clone()), + ErrNoSuchNick401::new(client.clone(), nick.clone()), + ) + .write_response(&mut writer) + .await? + } + + IrcResponseMessage::empty_tags( + Some(server_name.clone()), + RplEndOfWhois318::new(client.clone(), nick.clone()), + ) + .write_response(&mut writer) + .await?; + + Ok(()) +} diff --git a/crates/projection-irc/tests/lib.rs b/crates/projection-irc/tests/lib.rs index 193cd4d..583b18f 100644 --- a/crates/projection-irc/tests/lib.rs +++ b/crates/projection-irc/tests/lib.rs @@ -383,7 +383,14 @@ async fn scenario_two_users() -> Result<()> { // The second user should receive the PART message s2.expect(":tester1 PART #test").await?; + s1.send("WHOIS tester2").await?; + s1.expect(":testserver 318 tester1 tester2 :End of /WHOIS list").await?; + stream1.shutdown().await?; + s2.send("WHOIS tester3").await?; + s2.expect(":testserver 401 tester2 tester3 :No such nick/channel").await?; + s2.expect(":testserver 318 tester2 tester3 :End of /WHOIS list").await?; + stream2.shutdown().await?; server.shutdown().await?; diff --git a/crates/projection-xmpp/src/iq.rs b/crates/projection-xmpp/src/iq.rs index 0031c36..fdff68d 100644 --- a/crates/projection-xmpp/src/iq.rs +++ b/crates/projection-xmpp/src/iq.rs @@ -4,16 +4,16 @@ 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}; +use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType, Message, MessageType}; use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery}; +use proto_xmpp::mam::{Fin, Set}; use proto_xmpp::roster::RosterQuery; use proto_xmpp::session::Session; +use proto_xmpp::xml::ToXml; use crate::proto::IqClientBody; use crate::XmppConnection; -use proto_xmpp::xml::ToXml; - impl<'a> XmppConnection<'a> { pub async fn handle_iq(&self, output: &mut Vec>, iq: Iq) { match iq.body { @@ -87,6 +87,18 @@ impl<'a> XmppConnection<'a> { }; req.serialize(output); } + IqClientBody::MessageArchiveRequest(_) => { + let response = Iq { + from: iq.to, + id: iq.id, + to: None, + r#type: IqType::Result, + body: Fin { + set: Set { count: Some(0) }, + }, + }; + response.serialize(output); + } _ => { let req = Iq { from: None, diff --git a/crates/projection-xmpp/src/lib.rs b/crates/projection-xmpp/src/lib.rs index 0cbef87..a391b72 100644 --- a/crates/projection-xmpp/src/lib.rs +++ b/crates/projection-xmpp/src/lib.rs @@ -193,7 +193,7 @@ async fn handle_socket( pin!(termination); select! { biased; - _ = &mut termination =>{ + _ = &mut termination => { log::info!("Socket handling was terminated"); return Ok(()) }, diff --git a/crates/projection-xmpp/src/proto.rs b/crates/projection-xmpp/src/proto.rs index 0b157a6..2943509 100644 --- a/crates/projection-xmpp/src/proto.rs +++ b/crates/projection-xmpp/src/proto.rs @@ -7,6 +7,7 @@ use lavina_core::prelude::*; use proto_xmpp::bind::BindRequest; use proto_xmpp::client::{Iq, Message, Presence}; use proto_xmpp::disco::{InfoQuery, ItemQuery}; +use proto_xmpp::mam::MessageArchiveRequest; use proto_xmpp::roster::RosterQuery; use proto_xmpp::session::Session; use proto_xmpp::xml::*; @@ -18,6 +19,7 @@ pub enum IqClientBody { Roster(RosterQuery), DiscoInfo(InfoQuery), DiscoItem(ItemQuery), + MessageArchiveRequest(MessageArchiveRequest), Unknown(Ignore), } @@ -38,6 +40,7 @@ impl FromXml for IqClientBody { RosterQuery, InfoQuery, ItemQuery, + MessageArchiveRequest, { delegate_parsing!(Ignore, namespace, event).into() } diff --git a/crates/projection-xmpp/tests/lib.rs b/crates/projection-xmpp/tests/lib.rs index 85e917c..29e0a69 100644 --- a/crates/projection-xmpp/tests/lib.rs +++ b/crates/projection-xmpp/tests/lib.rs @@ -1,4 +1,5 @@ use std::io::ErrorKind; +use std::str::from_utf8; use std::sync::Arc; use std::time::Duration; @@ -6,6 +7,7 @@ use anyhow::Result; use assert_matches::*; use prometheus::Registry as MetricsRegistry; use quick_xml::events::Event; +use quick_xml::name::LocalName; use quick_xml::NsReader; use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}; use tokio::io::{ReadHalf as GenericReadHalf, WriteHalf as GenericWriteHalf}; @@ -22,6 +24,10 @@ use lavina_core::LavinaCore; use projection_xmpp::{launch, RunningServer, ServerConfig}; use proto_xmpp::xml::{Continuation, FromXml, Parser}; +fn element_name<'a>(local_name: &LocalName<'a>) -> &'a str { + from_utf8(local_name.into_inner()).unwrap() +} + pub async fn read_irc_message(reader: &mut BufReader>, buf: &mut Vec) -> Result { let mut size = 0; let res = reader.read_until(b'\n', buf).await?; @@ -35,10 +41,6 @@ struct TestScope<'a> { buffer: Vec, } -fn element_name<'a>(event: &quick_xml::events::BytesStart<'a>) -> &'a str { - std::str::from_utf8(event.local_name().into_inner()).unwrap() -} - impl<'a> TestScope<'a> { fn new(stream: &mut TcpStream) -> TestScope<'_> { let (reader, writer) = stream.split(); @@ -60,11 +62,11 @@ impl<'a> TestScope<'a> { } async fn expect_starttls_required(&mut self) -> Result<()> { - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b), "features")); - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); - assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"required")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"starttls")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "starttls")); + assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "required")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "starttls")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features")); Ok(()) } } @@ -97,20 +99,20 @@ impl<'a> TestScopeTls<'a> { } async fn expect_auth_mechanisms(&mut self) -> Result<()> { - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms")); - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanism")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "mechanisms")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "mechanism")); assert_matches!(self.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanism")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "mechanism")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "mechanisms")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features")); Ok(()) } async fn expect_bind_feature(&mut self) -> Result<()> { - assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features")); - assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"bind")); - assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features")); + assert_matches!(self.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "features")); + assert_matches!(self.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "bind")); + assert_matches!(self.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "features")); Ok(()) } @@ -123,6 +125,7 @@ impl<'a> TestScopeTls<'a> { } struct IgnoreCertVerification; + impl ServerCertVerifier for IgnoreCertVerification { fn verify_server_cert( &self, @@ -143,6 +146,7 @@ struct TestServer { core: LavinaCore, server: RunningServer, } + impl TestServer { async fn start() -> Result { let _ = tracing_subscriber::fmt::try_init(); @@ -191,10 +195,10 @@ async fn scenario_basic() -> Result<()> { s.send(r#""#).await?; s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_starttls_required().await?; s.send(r#""#).await?; - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed")); let buffer = s.buffer; tracing::info!("TLS feature negotiation complete"); @@ -213,26 +217,26 @@ async fn scenario_basic() -> Result<()> { s.send(r#""#).await?; s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_auth_mechanisms().await?; - // base64-encoded b"\x00tester\x00password" + // base64-encoded "\x00tester\x00password" s.send(r#"AHRlc3RlcgBwYXNzd29yZA=="#) .await?; - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"success")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "success")); s.send(r#""#).await?; s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_bind_feature().await?; s.send(r#"kek"#).await?; - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"iq")); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"bind")); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"jid")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "iq")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "bind")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "jid")); assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester")); - assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"jid")); - assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"bind")); - assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"iq")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "jid")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "bind")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "iq")); s.send(r#"Logged out"#).await?; stream.shutdown().await?; @@ -259,10 +263,10 @@ async fn scenario_wrong_password() -> Result<()> { s.send(r#""#).await?; s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_starttls_required().await?; s.send(r#""#).await?; - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed")); let buffer = s.buffer; tracing::info!("TLS feature negotiation complete"); @@ -281,14 +285,14 @@ async fn scenario_wrong_password() -> Result<()> { s.send(r#""#).await?; s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_auth_mechanisms().await?; - // base64-encoded b"\x00tester\x00password2" + // base64-encoded "\x00tester\x00password2" s.send(r#"AHRlc3RlcgBwYXNzd29yZDI="#) .await?; - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"failure")); - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"not-authorized")); - assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"failure")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "failure")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "not-authorized")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "failure")); let _ = stream.shutdown().await; @@ -313,10 +317,10 @@ async fn scenario_basic_without_headers() -> Result<()> { s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_starttls_required().await?; s.send(r#""#).await?; - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed")); let buffer = s.buffer; tracing::info!("TLS feature negotiation complete"); @@ -334,7 +338,7 @@ async fn scenario_basic_without_headers() -> Result<()> { s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); stream.shutdown().await?; @@ -361,10 +365,10 @@ async fn terminate_socket() -> Result<()> { s.send(r#""#).await?; assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); - assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); s.expect_starttls_required().await?; s.send(r#""#).await?; - assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"proceed")); + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed")); let connector = TlsConnector::from(Arc::new( ClientConfig::builder() @@ -383,3 +387,89 @@ async fn terminate_socket() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn test_message_archive_request() -> Result<()> { + let mut server = TestServer::start().await?; + + // test scenario + + server.storage.create_user("tester").await?; + Authenticator::new(server.storage.clone()).set_password("tester", "password").await?; + + let mut stream = TcpStream::connect(server.server.addr).await?; + let mut s = TestScope::new(&mut stream); + tracing::info!("TCP connection established"); + + s.send(r#""#).await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); + s.expect_starttls_required().await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "proceed")); + let buffer = s.buffer; + tracing::info!("TLS feature negotiation complete"); + + let connector = TlsConnector::from(Arc::new( + ClientConfig::builder() + .with_safe_defaults() + .with_custom_certificate_verifier(Arc::new(IgnoreCertVerification)) + .with_no_client_auth(), + )); + tracing::info!("Initiating TLS connection..."); + let mut stream = connector.connect(ServerName::IpAddress(server.server.addr.ip()), stream).await?; + tracing::info!("TLS connection established"); + + let mut s = TestScopeTls::new(&mut stream, buffer); + + s.send(r#""#).await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); + s.expect_auth_mechanisms().await?; + + // base64-encoded "\x00tester\x00password" + s.send(r#"AHRlc3RlcgBwYXNzd29yZA=="#) + .await?; + assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(element_name(&b.local_name()), "success")); + s.send(r#""#).await?; + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {}); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "stream")); + s.expect_bind_feature().await?; + s.send(r#"kek"#).await?; + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "iq")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "bind")); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(element_name(&b.local_name()), "jid")); + assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "jid")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "bind")); + assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(element_name(&b.local_name()), "iq")); + + s.send(r#""#).await?; + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "iq") + }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "fin") + }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "set") + }); + assert_matches!(s.next_xml_event().await?, Event::Start(b) => { + assert_eq!(element_name(&b.local_name()), "count") + }); + assert_matches!(s.next_xml_event().await?, Event::Text(b) => { + assert_eq!(&*b, b"0") + }); + + s.send(r#"Logged out"#).await?; + + stream.shutdown().await?; + + // wrap up + + server.shutdown().await?; + Ok(()) +} diff --git a/crates/proto-irc/src/client.rs b/crates/proto-irc/src/client.rs index a692e92..5a62427 100644 --- a/crates/proto-irc/src/client.rs +++ b/crates/proto-irc/src/client.rs @@ -1,9 +1,9 @@ -use super::*; - use anyhow::{anyhow, Result}; use nom::combinator::{all_consuming, opt}; use nonempty::NonEmpty; +use super::*; + /// Client-to-server command. #[derive(Clone, Debug, PartialEq, Eq)] pub enum ClientMessage { @@ -42,6 +42,10 @@ pub enum ClientMessage { Who { target: Recipient, // aka mask }, + /// WHOIS [] + Whois { + arg: command_args::Whois, + }, /// `TOPIC :` Topic { chan: Chan, @@ -63,6 +67,17 @@ pub enum ClientMessage { Authenticate(Str), } +pub mod command_args { + use crate::prelude::Str; + + #[derive(Clone, Debug, PartialEq, Eq)] + pub enum Whois { + Nick(Str), + TargetNick(Str, Str), + EmptyArgs, + } +} + pub fn client_message(input: &str) -> Result { let res = all_consuming(alt(( client_message_capability, @@ -74,6 +89,7 @@ pub fn client_message(input: &str) -> Result { client_message_join, client_message_mode, client_message_who, + client_message_whois, client_message_topic, client_message_part, client_message_privmsg, @@ -177,6 +193,31 @@ fn client_message_who(input: &str) -> IResult<&str, ClientMessage> { Ok((input, ClientMessage::Who { target })) } +fn client_message_whois(input: &str) -> IResult<&str, ClientMessage> { + let (input, _) = tag("WHOIS ")(input)?; + let args: Vec<_> = input.split_whitespace().collect(); + match args.as_slice()[..] { + [nick] => Ok(( + "", + ClientMessage::Whois { + arg: command_args::Whois::Nick(nick.into()), + }, + )), + [target, nick, ..] => Ok(( + "", + ClientMessage::Whois { + arg: command_args::Whois::TargetNick(target.into(), nick.into()), + }, + )), + [] => Ok(( + "", + ClientMessage::Whois { + arg: command_args::Whois::EmptyArgs, + }, + )), + } +} + fn client_message_topic(input: &str) -> IResult<&str, ClientMessage> { let (input, _) = tag("TOPIC ")(input)?; let (input, chan) = chan(input)?; @@ -311,6 +352,7 @@ mod test { use nonempty::nonempty; use super::*; + #[test] fn test_client_message_cap_ls() { let input = "CAP LS 302"; @@ -360,6 +402,66 @@ mod test { assert_matches!(result, Ok(result) => assert_eq!(expected, result)); } #[test] + fn test_client_message_whois() { + let test_user = "WHOIS val"; + let test_user_user = "WHOIS val val"; + let test_server_user = "WHOIS com.test.server user"; + let test_user_server = "WHOIS user com.test.server"; + let test_users_list = "WHOIS user_1,user_2,user_3"; + let test_server_users_list = "WHOIS com.test.server user_1,user_2,user_3"; + let test_more_than_two_params = "WHOIS test.server user_1,user_2,user_3 whatever spam"; + let test_none_none_params = "WHOIS "; + + let res_one_arg = client_message(test_user); + let res_user_user = client_message(test_user_user); + + let res_server_user = client_message(test_server_user); + let res_user_server = client_message(test_user_server); + let res_users_list = client_message(test_users_list); + let res_server_users_list = client_message(test_server_users_list); + let res_more_than_two_params = client_message(test_more_than_two_params); + let res_none_none_params = client_message(test_none_none_params); + + let expected_arg = ClientMessage::Whois { + arg: command_args::Whois::Nick("val".into()), + }; + let expected_user_user = ClientMessage::Whois { + arg: command_args::Whois::TargetNick("val".into(), "val".into()), + }; + + let expected_server_user = ClientMessage::Whois { + arg: command_args::Whois::TargetNick("com.test.server".into(), "user".into()), + }; + + let expected_user_server = ClientMessage::Whois { + arg: command_args::Whois::TargetNick("user".into(), "com.test.server".into()), + }; + + let expected_user_list = ClientMessage::Whois { + arg: command_args::Whois::Nick("user_1,user_2,user_3".into()), + }; + let expected_server_user_list = ClientMessage::Whois { + arg: command_args::Whois::TargetNick("com.test.server".into(), "user_1,user_2,user_3".into()), + }; + + let expected_more_than_two_params = ClientMessage::Whois { + arg: command_args::Whois::TargetNick("test.server".into(), "user_1,user_2,user_3".into()), + }; + + let expected_none_none_params = ClientMessage::Whois { + arg: command_args::Whois::EmptyArgs, + }; + + assert_matches!(res_one_arg, Ok(result) => assert_eq!(expected_arg, result)); + assert_matches!(res_user_user, Ok(result) => assert_eq!(expected_user_user, result)); + assert_matches!(res_server_user, Ok(result) => assert_eq!(expected_server_user, result)); + assert_matches!(res_user_server, Ok(result) => assert_eq!(expected_user_server, result)); + assert_matches!(res_users_list, Ok(result) => assert_eq!(expected_user_list, result)); + assert_matches!(res_server_users_list, Ok(result) => assert_eq!(expected_server_user_list, result)); + assert_matches!(res_more_than_two_params, Ok(result) => assert_eq!(expected_more_than_two_params, result)); + assert_matches!(res_none_none_params, Ok(result) => assert_eq!(expected_none_none_params, result)) + } + #[test] fn test_client_message_user() { let input = "USER SomeNick 8 * :Real Name"; let expected = ClientMessage::User { diff --git a/crates/proto-irc/src/commands/mod.rs b/crates/proto-irc/src/commands/mod.rs new file mode 100644 index 0000000..80525f8 --- /dev/null +++ b/crates/proto-irc/src/commands/mod.rs @@ -0,0 +1 @@ +pub mod whois; diff --git a/crates/proto-irc/src/commands/whois/error.rs b/crates/proto-irc/src/commands/whois/error.rs new file mode 100644 index 0000000..426095b --- /dev/null +++ b/crates/proto-irc/src/commands/whois/error.rs @@ -0,0 +1,67 @@ +use tokio::io::{AsyncWrite, AsyncWriteExt}; + +use crate::{prelude::Str, response::WriteResponse}; + +/// ErrNoSuchNick401 +pub struct ErrNoSuchNick401 { + client: Str, + nick: Str, +} + +impl ErrNoSuchNick401 { + pub fn new(client: Str, nick: Str) -> Self { + ErrNoSuchNick401 { client, nick } + } +} + +/// ErrNoSuchServer402 +struct ErrNoSuchServer402 { + client: Str, + /// target parameter in WHOIS + /// example: `/whois ` + server_name: Str, +} + +/// ErrNoNicknameGiven431 +pub struct ErrNoNicknameGiven431 { + client: Str, +} +impl ErrNoNicknameGiven431 { + pub fn new(client: Str) -> Self { + ErrNoNicknameGiven431 { client } + } +} + +impl WriteResponse for ErrNoSuchNick401 { + async fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { + writer.write_all(b"401 ").await?; + writer.write_all(self.client.as_bytes()).await?; + writer.write_all(b" ").await?; + writer.write_all(self.nick.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all("No such nick/channel".as_bytes()).await?; + Ok(()) + } +} + +impl WriteResponse for ErrNoNicknameGiven431 { + async fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { + writer.write_all(b"431").await?; + writer.write_all(self.client.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all("No nickname given".as_bytes()).await?; + Ok(()) + } +} + +impl WriteResponse for ErrNoSuchServer402 { + async fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { + writer.write_all(b"402 ").await?; + writer.write_all(self.client.as_bytes()).await?; + writer.write_all(b" ").await?; + writer.write_all(self.server_name.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all("No such server".as_bytes()).await?; + Ok(()) + } +} diff --git a/crates/proto-irc/src/commands/whois/mod.rs b/crates/proto-irc/src/commands/whois/mod.rs new file mode 100644 index 0000000..52897d3 --- /dev/null +++ b/crates/proto-irc/src/commands/whois/mod.rs @@ -0,0 +1,2 @@ +pub mod error; +pub mod response; diff --git a/crates/proto-irc/src/commands/whois/response.rs b/crates/proto-irc/src/commands/whois/response.rs new file mode 100644 index 0000000..506ebf4 --- /dev/null +++ b/crates/proto-irc/src/commands/whois/response.rs @@ -0,0 +1,24 @@ +use tokio::io::{AsyncWrite, AsyncWriteExt}; + +use crate::{prelude::Str, response::WriteResponse}; + +pub struct RplEndOfWhois318 { + client: Str, + nick: Str, +} +impl RplEndOfWhois318 { + pub fn new(client: Str, nick: Str) -> Self { + RplEndOfWhois318 { client, nick } + } +} +impl WriteResponse for RplEndOfWhois318 { + async fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { + writer.write_all(b"318 ").await?; + writer.write_all(self.client.as_bytes()).await?; + writer.write_all(b" ").await?; + writer.write_all(self.nick.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all("End of /WHOIS list".as_bytes()).await?; + Ok(()) + } +} diff --git a/crates/proto-irc/src/lib.rs b/crates/proto-irc/src/lib.rs index 3c7ade2..b2c8a37 100644 --- a/crates/proto-irc/src/lib.rs +++ b/crates/proto-irc/src/lib.rs @@ -1,6 +1,8 @@ //! Client-to-Server IRC protocol. pub mod client; +pub mod commands; mod prelude; +pub mod response; pub mod server; #[cfg(test)] mod testkit; diff --git a/crates/proto-irc/src/response.rs b/crates/proto-irc/src/response.rs new file mode 100644 index 0000000..8c2ac4f --- /dev/null +++ b/crates/proto-irc/src/response.rs @@ -0,0 +1,47 @@ +use std::future::Future; + +use tokio::io::{AsyncWrite, AsyncWriteExt}; + +use crate::prelude::Str; +use crate::Tag; + +pub trait WriteResponse { + fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> impl Future>; +} + +/// Server-to-client enum agnostic message +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct IrcResponseMessage { + /// Optional tags section, prefixed with `@` + pub tags: Vec, + /// Optional server name, prefixed with `:`. + pub sender: Option, + pub body: T, +} + +impl IrcResponseMessage { + pub fn empty_tags(sender: Option, body: T) -> Self { + IrcResponseMessage { + tags: vec![], + sender, + body, + } + } + + pub fn new(tags: Vec, sender: Option, body: T) -> Self { + IrcResponseMessage { tags, sender, body } + } +} + +impl WriteResponse for IrcResponseMessage { + async fn write_response(&self, writer: &mut (impl AsyncWrite + Unpin)) -> std::io::Result<()> { + if let Some(sender) = &self.sender { + writer.write_all(b":").await?; + writer.write_all(sender.as_bytes()).await?; + writer.write_all(b" ").await?; + } + self.body.write_response(writer).await?; + writer.write_all(b"\r\n").await?; + Ok(()) + } +} diff --git a/crates/proto-irc/src/server.rs b/crates/proto-irc/src/server.rs index 494f456..ac50f72 100644 --- a/crates/proto-irc/src/server.rs +++ b/crates/proto-irc/src/server.rs @@ -2,9 +2,10 @@ use nonempty::NonEmpty; use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; -use super::*; use crate::user::PrefixedNick; +use super::*; + /// Server-to-client message. #[derive(Clone, Debug, PartialEq, Eq)] pub struct ServerMessage { @@ -112,6 +113,12 @@ pub enum ServerMessageBody { /// Usually `b"End of WHO list"` msg: Str, }, + N318EndOfWhois { + client: Str, + nick: Str, + /// Usually `b"End of /WHOIS list"` + msg: Str, + }, N332Topic { client: Str, chat: Chan, @@ -141,6 +148,10 @@ pub enum ServerMessageBody { client: Str, chan: Chan, }, + N431ErrNoNicknameGiven { + client: Str, + message: Str, + }, N474BannedFromChan { client: Str, chan: Chan, @@ -278,6 +289,14 @@ impl ServerMessageBody { writer.write_all(b" :").await?; writer.write_all(msg.as_bytes()).await?; } + ServerMessageBody::N318EndOfWhois { client, nick, msg } => { + writer.write_all(b"318 ").await?; + writer.write_all(client.as_bytes()).await?; + writer.write_all(b" ").await?; + writer.write_all(nick.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all(msg.as_bytes()).await?; + } ServerMessageBody::N332Topic { client, chat, topic } => { writer.write_all(b"332 ").await?; writer.write_all(client.as_bytes()).await?; @@ -340,6 +359,12 @@ impl ServerMessageBody { chan.write_async(writer).await?; writer.write_all(b" :End of /NAMES list").await?; } + ServerMessageBody::N431ErrNoNicknameGiven { client, message } => { + writer.write_all(b"431").await?; + writer.write_all(client.as_bytes()).await?; + writer.write_all(b" :").await?; + writer.write_all(message.as_bytes()).await?; + } ServerMessageBody::N474BannedFromChan { client, chan, message } => { writer.write_all(b"474 ").await?; writer.write_all(client.as_bytes()).await?; @@ -468,9 +493,10 @@ fn server_message_body_cap(input: &str) -> IResult<&str, ServerMessageBody> { mod test { use assert_matches::*; - use super::*; use crate::testkit::*; + use super::*; + #[test] fn test_server_message_notice() { let input = "NOTICE * :*** Looking up your hostname...\r\n"; diff --git a/crates/proto-xmpp/src/bind.rs b/crates/proto-xmpp/src/bind.rs index d27a00e..d546141 100644 --- a/crates/proto-xmpp/src/bind.rs +++ b/crates/proto-xmpp/src/bind.rs @@ -74,8 +74,8 @@ impl Jid { pub struct BindRequest(pub Resource); impl FromXmlTag for BindRequest { - const NS: &'static str = XMLNS; const NAME: &'static str = "bind"; + const NS: &'static str = XMLNS; } impl FromXml for BindRequest { diff --git a/crates/proto-xmpp/src/client.rs b/crates/proto-xmpp/src/client.rs index 05807bd..08d203a 100644 --- a/crates/proto-xmpp/src/client.rs +++ b/crates/proto-xmpp/src/client.rs @@ -658,7 +658,7 @@ mod tests { #[tokio::test] async fn parse_message() { - let input = r#"daabbb"#; + let input = r#"daabbb"#; let result: Message = crate::xml::parse(input).unwrap(); assert_eq!( result, @@ -666,8 +666,8 @@ mod tests { from: None, id: Some("aacea".to_string()), to: Some(Jid { - name: Some(Name("nikita".into())), - server: Server("vlnv.dev".into()), + name: Some(Name("chelik".into())), + server: Server("xmpp.ru".into()), resource: None }), r#type: MessageType::Chat, @@ -681,7 +681,7 @@ mod tests { #[tokio::test] async fn parse_message_empty_custom() { - let input = r#"daabbb"#; + let input = r#"daabbb"#; let result: Message = crate::xml::parse(input).unwrap(); assert_eq!( result, @@ -689,8 +689,8 @@ mod tests { from: None, id: Some("aacea".to_string()), to: Some(Jid { - name: Some(Name("nikita".into())), - server: Server("vlnv.dev".into()), + name: Some(Name("chelik".into())), + server: Server("xmpp.ru".into()), resource: None }), r#type: MessageType::Chat, diff --git a/crates/proto-xmpp/src/lib.rs b/crates/proto-xmpp/src/lib.rs index d3e25ba..71e8a94 100644 --- a/crates/proto-xmpp/src/lib.rs +++ b/crates/proto-xmpp/src/lib.rs @@ -3,6 +3,7 @@ pub mod bind; pub mod client; pub mod disco; +pub mod mam; pub mod muc; mod prelude; pub mod roster; diff --git a/crates/proto-xmpp/src/mam.rs b/crates/proto-xmpp/src/mam.rs new file mode 100644 index 0000000..c8151f2 --- /dev/null +++ b/crates/proto-xmpp/src/mam.rs @@ -0,0 +1,225 @@ +use anyhow::{anyhow, Result}; +use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; +use quick_xml::name::{Namespace, ResolveResult}; +use std::io::Read; + +use crate::xml::*; + +pub const MAM_XMLNS: &'static str = "urn:xmpp:mam:2"; +pub const DATA_XMLNS: &'static str = "jabber:x:data"; +pub const RESULT_SET_XMLNS: &'static str = "http://jabber.org/protocol/rsm"; + +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct MessageArchiveRequest { + pub x: Option, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct X { + pub fields: Vec, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct Field { + pub values: Vec, +} + +// Message archive response styled as a result set. +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct Fin { + pub set: Set, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub struct Set { + pub count: Option, +} + +impl ToXml for Fin { + fn serialize(&self, events: &mut Vec>) { + let fin_bytes = BytesStart::new(format!(r#"fin xmlns="{}" complete=True"#, MAM_XMLNS)); + let set_bytes = BytesStart::new(format!(r#"set xmlns="{}""#, RESULT_SET_XMLNS)); + events.push(Event::Start(fin_bytes)); + events.push(Event::Start(set_bytes)); + + if let &Some(count) = &self.set.count { + events.push(Event::Start(BytesStart::new("count"))); + events.push(Event::Text(BytesText::new(count.to_string().as_str()).into_owned())); + events.push(Event::End(BytesEnd::new("count"))); + } + events.push(Event::End(BytesEnd::new("set"))); + events.push(Event::End(BytesEnd::new("fin"))); + } +} + +impl FromXmlTag for X { + const NAME: &'static str = "x"; + const NS: &'static str = DATA_XMLNS; +} + +impl FromXmlTag for MessageArchiveRequest { + const NAME: &'static str = "query"; + const NS: &'static str = MAM_XMLNS; +} + +impl FromXml for X { + type P = impl Parser>; + + fn parse() -> Self::P { + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + println!("X::parse {:?}", event); + + let bytes = match event { + Event::Start(bytes) if bytes.name().0 == X::NAME.as_bytes() => bytes, + Event::Empty(bytes) if bytes.name().0 == X::NAME.as_bytes() => return Ok(X { fields: vec![] }), + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + }; + let mut fields = vec![]; + loop { + (namespace, event) = yield; + match event { + Event::Start(_) => { + // start of + let mut values = vec![]; + loop { + (namespace, event) = yield; + match event { + Event::Start(bytes) if bytes.name().0 == b"value" => { + // start of + } + Event::End(bytes) if bytes.name().0 == b"field" => { + // end of + break; + } + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + (namespace, event) = yield; + let text: String = match event { + Event::Text(bytes) => { + // text inside + String::from_utf8(bytes.to_vec())? + } + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + }; + (namespace, event) = yield; + match event { + Event::End(bytes) if bytes.name().0 == b"value" => { + // end of + } + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + values.push(text); + } + fields.push(Field { values }) + } + Event::End(bytes) if bytes.name().0 == X::NAME.as_bytes() => { + // end of + return Ok(X { fields }); + } + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + } + } + } + } +} + +impl FromXml for MessageArchiveRequest { + type P = impl Parser>; + + fn parse() -> Self::P { + |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + println!("MessageArchiveRequest::parse {:?}", event); + + let bytes = match event { + Event::Empty(_) => return Ok(MessageArchiveRequest { x: None }), + Event::Start(bytes) => bytes, + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), + }; + if bytes.name().0 != MessageArchiveRequest::NAME.as_bytes() { + return Err(anyhow!("Unexpected XML tag: {:?}", bytes.name())); + } + let ResolveResult::Bound(Namespace(ns)) = namespace else { + return Err(anyhow!("No namespace provided")); + }; + if ns != MAM_XMLNS.as_bytes() { + return Err(anyhow!("Incorrect namespace")); + } + (namespace, event) = yield; + match event { + Event::End(bytes) if bytes.name().0 == MessageArchiveRequest::NAME.as_bytes() => { + Ok(MessageArchiveRequest { x: None }) + } + Event::Start(bytes) | Event::Empty(bytes) if bytes.name().0 == X::NAME.as_bytes() => { + let x = delegate_parsing!(X, namespace, event)?; + Ok(MessageArchiveRequest { x: Some(x) }) + } + _ => Err(anyhow!("Unexpected XML event: {event:?}")), + } + } + } +} + +impl MessageArchiveRequest {} + +#[cfg(test)] +mod tests { + use super::*; + use crate::bind::{Jid, Name, Server}; + use crate::client::{Iq, IqType}; + + #[test] + fn test_parse_archive_query() { + let input = r#""#; + + let result: Iq = parse(input).unwrap(); + assert_eq!( + result, + Iq { + from: None, + id: "juliet1".to_string(), + to: Option::from(Jid { + name: None, + server: Server("pubsub.shakespeare.lit".into()), + resource: None, + }), + r#type: IqType::Set, + body: MessageArchiveRequest { x: None }, + } + ); + } + + #[test] + fn test_parse_query_messages_from_jid() { + let input = r#"value1juliet@capulet.lit"#; + + let result: Iq = parse(input).unwrap(); + assert_eq!( + result, + Iq { + from: None, + id: "juliet1".to_string(), + to: None, + r#type: IqType::Set, + body: MessageArchiveRequest { + x: Some(X { + fields: vec![ + Field { + values: vec!["value1".to_string()], + }, + Field { + values: vec!["juliet@capulet.lit".to_string()], + }, + ] + }) + }, + } + ); + } + + #[test] + fn test_parse_query_messages_from_jid_with_unclosed_tag() { + let input = r#"value1juliet@capulet.lit"#; + + assert!(parse::>(input).is_err()) + } +} diff --git a/crates/proto-xmpp/src/roster.rs b/crates/proto-xmpp/src/roster.rs index f7d0305..7eb97d1 100644 --- a/crates/proto-xmpp/src/roster.rs +++ b/crates/proto-xmpp/src/roster.rs @@ -2,7 +2,7 @@ use quick_xml::events::{BytesStart, Event}; use crate::xml::*; use anyhow::{anyhow, Result}; -use quick_xml::name::ResolveResult; +use quick_xml::name::{Namespace, ResolveResult}; pub const XMLNS: &'static str = "jabber:iq:roster"; @@ -14,6 +14,9 @@ impl FromXml for RosterQuery { fn parse() -> Self::P { |(mut namespace, mut event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { + let ResolveResult::Bound(Namespace(ns)) = namespace else { + return Err(anyhow!("No namespace provided")); + }; match event { Event::Start(_) => (), Event::Empty(_) => return Ok(RosterQuery), @@ -38,3 +41,39 @@ impl ToXml for RosterQuery { events.push(Event::Empty(BytesStart::new(format!(r#"query xmlns="{}""#, XMLNS)))); } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::bind::{Jid, Name, Resource, Server}; + use crate::client::{Iq, IqType}; + + #[test] + fn test_parse() { + let input = + r#""#; + + let result: Iq = parse(input).unwrap(); + assert_eq!( + result, + Iq { + from: Option::from(Jid { + name: Option::from(Name("juliet".into())), + server: Server("example.com".into()), + resource: Option::from(Resource("balcony".into())), + }), + id: "bv1bs71f".to_string(), + to: None, + r#type: IqType::Get, + body: RosterQuery, + } + ) + } + + #[test] + fn test_missing_namespace() { + let input = r#""#; + + assert!(parse::>(input).is_err()); + } +} diff --git a/crates/proto-xmpp/src/stream.rs b/crates/proto-xmpp/src/stream.rs index 8f46f31..b12891e 100644 --- a/crates/proto-xmpp/src/stream.rs +++ b/crates/proto-xmpp/src/stream.rs @@ -170,14 +170,14 @@ mod test { #[tokio::test] async fn client_stream_start_correct_parse() { - let input = r###""###; + let input = r###""###; let mut reader = NsReader::from_reader(input.as_bytes()); let mut buf = vec![]; let res = ClientStreamStart::parse(&mut reader, &mut buf).await.unwrap(); assert_eq!( res, ClientStreamStart { - to: "vlnv.dev".to_owned(), + to: "xmpp.ru".to_owned(), lang: Some("en".to_owned()), version: "1.0".to_owned() } @@ -187,12 +187,12 @@ mod test { #[tokio::test] async fn server_stream_start_write() { let input = ServerStreamStart { - from: "vlnv.dev".to_owned(), + from: "xmpp.ru".to_owned(), lang: "en".to_owned(), id: "stream_id".to_owned(), version: "1.0".to_owned(), }; - let expected = r###""###; + let expected = r###""###; let mut output: Vec = vec![]; let mut writer = Writer::new(&mut output); input.write_xml(&mut writer).await.unwrap();