1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-26 08:58:27 +02:00

Compare commits

...

6 Commits

Author SHA1 Message Date
moparisthebest
54de8d9951
Fix building xmpp with no-default-features 2024-01-03 20:01:05 -05:00
moparisthebest
019450ff4b
Add disabled-by-default insecure-tcp feature to tokio-xmpp for use by component connections 2024-01-03 19:34:03 -05:00
moparisthebest
38bfba4a18
xmpp crate now supports ServerConnector 2024-01-03 19:34:02 -05:00
moparisthebest
733d005f51
DNS/TLS deps are now optional, component now also uses ServerConnector 2024-01-01 02:09:41 -05:00
moparisthebest
e784b15402
ServerConnector and AsyncClient support channel binding, SimpleClient uses ServerConnector 2024-01-01 02:08:40 -05:00
moparisthebest
3cab603a4c
Add AsyncServerConnector to AsyncClient to be able to support any stream
Unfortunately API breaking unless we do some export mangling
2024-01-01 02:08:13 -05:00
40 changed files with 718 additions and 450 deletions

View File

@ -14,17 +14,12 @@ edition = "2021"
[dependencies]
bytes = "1"
futures = "0.3"
idna = "0.4"
log = "0.4"
native-tls = { version = "0.2", optional = true }
tokio = { version = "1", features = ["net", "rt", "rt-multi-thread", "macros"] }
tokio-native-tls = { version = "0.3", optional = true }
tokio-rustls = { version = "0.24", optional = true }
tokio-stream = { version = "0.1", features = [] }
tokio-util = { version = "0.7", features = ["codec"] }
hickory-resolver = "0.24"
rxml = "0.9.1"
webpki-roots = { version = "0.25", optional = true }
rxml = "0.9.1"
rand = "^0.8"
syntect = { version = "5", optional = true }
# same repository dependencies
@ -32,11 +27,24 @@ minidom = { version = "0.15", path = "../minidom" }
sasl = { version = "0.5", path = "../sasl" }
xmpp-parsers = { version = "0.20", path = "../parsers" }
# these are only needed for starttls ServerConnector support
hickory-resolver = { version = "0.24", optional = true}
idna = { version = "0.4", optional = true}
native-tls = { version = "0.2", optional = true }
tokio-native-tls = { version = "0.3", optional = true }
tokio-rustls = { version = "0.24", optional = true }
[dev-dependencies]
env_logger = { version = "0.10", default-features = false, features = ["auto-color", "humantime"] }
# this is needed for echo-component example
tokio-xmpp = { path = ".", features = ["insecure-tcp"]}
[features]
default = ["tls-native"]
default = ["starttls-rust"]
starttls = ["hickory-resolver", "idna"]
tls-rust = ["tokio-rustls", "webpki-roots"]
tls-native = ["tokio-native-tls", "native-tls"]
starttls-native = ["starttls", "tls-native"]
starttls-rust = ["starttls", "tls-rust"]
insecure-tcp = []
syntax-highlighting = ["syntect"]

View File

@ -2,7 +2,7 @@ use futures::stream::StreamExt;
use std::env::args;
use std::process::exit;
use std::str::FromStr;
use tokio_xmpp::Component;
use tokio_xmpp::tcp::TcpComponent as Component;
use xmpp_parsers::message::{Body, Message, MessageType};
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
use xmpp_parsers::{Element, Jid};
@ -12,22 +12,21 @@ async fn main() {
env_logger::init();
let args: Vec<String> = args().collect();
if args.len() < 3 || args.len() > 5 {
println!("Usage: {} <jid> <password> [server] [port]", args[0]);
if args.len() < 3 || args.len() > 4 {
println!("Usage: {} <jid> <password> [server:port]", args[0]);
exit(1);
}
let jid = &args[1];
let password = &args[2];
let server = &args
let server = args
.get(3)
.unwrap()
.parse()
.unwrap_or("127.0.0.1".to_owned());
let port: u16 = args.get(4).unwrap().parse().unwrap_or(5347u16);
.unwrap_or("127.0.0.1:5347".to_owned());
// Component instance
println!("{} {} {} {}", jid, password, server, port);
let mut component = Component::new(jid, password, server, port).await.unwrap();
println!("{} {} {}", jid, password, server);
let mut component = Component::new(jid, password, server).await.unwrap();
// Make the two interfaces for sending and receiving independent
// of each other so we can move one into a closure.

View File

@ -1,23 +1,15 @@
use futures::{sink::SinkExt, task::Poll, Future, Sink, Stream};
use sasl::common::{ChannelBinding, Credentials};
use std::mem::replace;
use std::pin::Pin;
use std::task::Context;
use tokio::net::TcpStream;
use tokio::task::JoinHandle;
#[cfg(feature = "tls-native")]
use tokio_native_tls::TlsStream;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::client::TlsStream;
use xmpp_parsers::{ns, Element, Jid};
use super::auth::auth;
use super::bind::bind;
use super::connect::client_login;
use crate::connect::{AsyncReadAndWrite, ServerConnector};
use crate::event::Event;
use crate::happy_eyeballs::{connect_to_host, connect_with_srv};
use crate::starttls::starttls;
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::{self, add_stanza_id};
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
use crate::{Error, ProtocolError};
/// XMPP client connection and state
@ -26,65 +18,35 @@ use crate::{Error, ProtocolError};
///
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
/// [`Sink`](#impl-Sink<Packet>) traits.
pub struct Client {
config: Config,
state: ClientState,
pub struct Client<C: ServerConnector> {
config: Config<C>,
state: ClientState<C::Stream>,
reconnect: bool,
// TODO: tls_required=true
}
/// XMPP server connection configuration
#[derive(Clone, Debug)]
pub enum ServerConfig {
/// Use SRV record to find server host
UseSrv,
#[allow(unused)]
/// Manually define server host and port
Manual {
/// Server host name
host: String,
/// Server port
port: u16,
},
}
/// XMPP client configuration
#[derive(Clone, Debug)]
pub struct Config {
pub struct Config<C> {
/// jid of the account
pub jid: Jid,
/// password of the account
pub password: String,
/// server configuration for the account
pub server: ServerConfig,
pub server: C,
}
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
enum ClientState {
enum ClientState<S: AsyncReadAndWrite> {
Invalid,
Disconnected,
Connecting(JoinHandle<Result<XMPPStream, Error>>),
Connected(XMPPStream),
Connecting(JoinHandle<Result<XMPPStream<S>, Error>>),
Connected(XMPPStream<S>),
}
impl Client {
/// Start a new XMPP client
///
/// Start polling the returned instance so that it will connect
/// and yield events.
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
let config = Config {
jid: jid.into(),
password: password.into(),
server: ServerConfig::UseSrv,
};
Self::new_with_config(config)
}
impl<C: ServerConnector> Client<C> {
/// Start a new client given that the JID is already parsed.
pub fn new_with_config(config: Config) -> Self {
let connect = tokio::spawn(Self::connect(
pub fn new_with_config(config: Config<C>) -> Self {
let connect = tokio::spawn(client_login(
config.server.clone(),
config.jid.clone(),
config.password.clone(),
@ -104,52 +66,6 @@ impl Client {
self
}
async fn connect(
server: ServerConfig,
jid: Jid,
password: String,
) -> Result<XMPPStream, Error> {
let username = jid.node_str().unwrap();
let password = password;
// TCP connection
let tcp_stream = match server {
ServerConfig::UseSrv => {
connect_with_srv(jid.domain_str(), "_xmpp-client._tcp", 5222).await?
}
ServerConfig::Manual { host, port } => connect_to_host(host.as_str(), port).await?,
};
// Unencryped XMPPStream
let xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
.await?;
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
// Encrypted XMPPStream
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
.await?
} else {
return Err(Error::Protocol(ProtocolError::NoTls));
};
let creds = Credentials::default()
.with_username(username)
.with_password(password)
.with_channel_binding(ChannelBinding::None);
// Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?;
// Authenticated XMPPStream
let xmpp_stream =
xmpp_stream::XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?;
// XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?;
Ok(xmpp_stream)
}
/// Get the client's bound JID (the one reported by the XMPP
/// server).
pub fn bound_jid(&self) -> Option<&Jid> {
@ -180,7 +96,7 @@ impl Client {
///
/// In an `async fn` you may want to use this with `use
/// futures::stream::StreamExt;`
impl Stream for Client {
impl<C: ServerConnector> Stream for Client<C> {
type Item = Event;
/// Low-level read on the XMPP stream, allowing the underlying
@ -200,7 +116,7 @@ impl Stream for Client {
ClientState::Invalid => panic!("Invalid client state"),
ClientState::Disconnected if self.reconnect => {
// TODO: add timeout
let connect = tokio::spawn(Self::connect(
let connect = tokio::spawn(client_login(
self.config.server.clone(),
self.config.jid.clone(),
self.config.password.clone(),
@ -297,7 +213,7 @@ impl Stream for Client {
/// Outgoing XMPP packets
///
/// See `send_stanza()` for an `async fn`
impl Sink<Packet> for Client {
impl<C: ServerConnector> Sink<Packet> for Client<C> {
type Error = Error;
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {

View File

@ -0,0 +1,35 @@
use sasl::common::Credentials;
use xmpp_parsers::{ns, Jid};
use crate::client::auth::auth;
use crate::client::bind::bind;
use crate::connect::ServerConnector;
use crate::{xmpp_stream::XMPPStream, Error};
/// Log into an XMPP server as a client with a jid+pass
/// does channel binding if supported
pub async fn client_login<C: ServerConnector>(
server: C,
jid: Jid,
password: String,
) -> Result<XMPPStream<C::Stream>, Error> {
let username = jid.node_str().unwrap();
let password = password;
let xmpp_stream = server.connect(&jid, ns::JABBER_CLIENT).await?;
let channel_binding = C::channel_binding(xmpp_stream.stream.get_ref())?;
let creds = Credentials::default()
.with_username(username)
.with_password(password)
.with_channel_binding(channel_binding);
// Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?;
// Authenticated XMPPStream
let xmpp_stream = XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?;
// XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?;
Ok(xmpp_stream)
}

View File

@ -1,5 +1,7 @@
mod auth;
mod bind;
pub(crate) mod connect;
pub mod async_client;
pub mod simple_client;

View File

@ -1,119 +1,40 @@
use futures::{sink::SinkExt, Sink, Stream};
use idna;
#[cfg(feature = "tls-native")]
use log::warn;
use sasl::common::{ChannelBinding, Credentials};
use std::pin::Pin;
use std::str::FromStr;
use std::task::{Context, Poll};
use tokio::net::TcpStream;
#[cfg(feature = "tls-native")]
use tokio_native_tls::TlsStream;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::{client::TlsStream, rustls::ProtocolVersion};
use tokio_stream::StreamExt;
use xmpp_parsers::{ns, Element, Jid};
use super::auth::auth;
use super::bind::bind;
use crate::happy_eyeballs::connect_with_srv;
use crate::starttls::starttls;
use crate::connect::ServerConnector;
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::{self, add_stanza_id};
use crate::{Error, ProtocolError};
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
use crate::Error;
use super::connect::client_login;
/// A simple XMPP client connection
///
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
/// [`Sink`](#impl-Sink<Packet>) traits.
pub struct Client {
stream: XMPPStream,
pub struct Client<C: ServerConnector> {
stream: XMPPStream<C::Stream>,
}
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
impl Client {
/// Start a new XMPP client and wait for a usable session
pub async fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, Error> {
let jid = Jid::from_str(jid)?;
let client = Self::new_with_jid(jid, password.into()).await?;
Ok(client)
}
impl<C: ServerConnector> Client<C> {
/// Start a new client given that the JID is already parsed.
pub async fn new_with_jid(jid: Jid, password: String) -> Result<Self, Error> {
let stream = Self::connect(jid, password).await?;
pub async fn new_with_jid_connector(
connector: C,
jid: Jid,
password: String,
) -> Result<Self, Error> {
let stream = client_login(connector, jid, password).await?;
Ok(Client { stream })
}
/// Get direct access to inner XMPP Stream
pub fn into_inner(self) -> XMPPStream {
pub fn into_inner(self) -> XMPPStream<C::Stream> {
self.stream
}
async fn connect(jid: Jid, password: String) -> Result<XMPPStream, Error> {
let username = jid.node_str().unwrap();
let password = password;
let domain = idna::domain_to_ascii(jid.domain_str()).map_err(|_| Error::Idna)?;
// TCP connection
let tcp_stream = connect_with_srv(&domain, "_xmpp-client._tcp", 5222).await?;
// Unencryped XMPPStream
let xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
.await?;
let channel_binding;
let xmpp_stream = if xmpp_stream.stream_features.can_starttls() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
#[cfg(feature = "tls-native")]
{
warn!("tls-native doesnt support channel binding, please use tls-rust if you want this feature!");
channel_binding = ChannelBinding::None;
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
{
let (_, connection) = tls_stream.get_ref();
match connection.protocol_version() {
// TODO: Add support for TLS 1.2 and earlier.
Some(ProtocolVersion::TLSv1_3) => {
let data = vec![0u8; 32];
let data = connection.export_keying_material(
data,
b"EXPORTER-Channel-Binding",
None,
)?;
channel_binding = ChannelBinding::TlsExporter(data);
}
_ => {
channel_binding = ChannelBinding::None;
}
}
}
// Encrypted XMPPStream
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
.await?
} else {
return Err(Error::Protocol(ProtocolError::NoTls));
};
let creds = Credentials::default()
.with_username(username)
.with_password(password)
.with_channel_binding(channel_binding);
// Authenticated (unspecified) stream
let stream = auth(xmpp_stream, creds).await?;
// Authenticated XMPPStream
let xmpp_stream =
xmpp_stream::XMPPStream::start(stream, jid, ns::JABBER_CLIENT.to_owned()).await?;
// XMPPStream bound to user session
let xmpp_stream = bind(xmpp_stream).await?;
Ok(xmpp_stream)
}
/// Get the client's bound JID (the one reported by the XMPP
/// server).
pub fn bound_jid(&self) -> &Jid {
@ -150,7 +71,7 @@ impl Client {
///
/// In an `async fn` you may want to use this with `use
/// futures::stream::StreamExt;`
impl Stream for Client {
impl<C: ServerConnector> Stream for Client<C> {
type Item = Result<Element, Error>;
/// Low-level read on the XMPP stream
@ -177,7 +98,7 @@ impl Stream for Client {
/// Outgoing XMPP packets
///
/// See `send_stanza()` for an `async fn`
impl Sink<Packet> for Client {
impl<C: ServerConnector> Sink<Packet> for Client<C> {
type Error = Error;
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {

View File

@ -0,0 +1,18 @@
use xmpp_parsers::{ns, Jid};
use crate::connect::ServerConnector;
use crate::{xmpp_stream::XMPPStream, Error};
use super::auth::auth;
/// Log into an XMPP server as a client with a jid+pass
pub async fn component_login<C: ServerConnector>(
connector: C,
jid: Jid,
password: String,
) -> Result<XMPPStream<C::Stream>, Error> {
let password = password;
let mut xmpp_stream = connector.connect(&jid, ns::COMPONENT).await?;
auth(&mut xmpp_stream, password).await?;
Ok(xmpp_stream)
}

View File

@ -5,53 +5,43 @@ use futures::{sink::SinkExt, task::Poll, Sink, Stream};
use std::pin::Pin;
use std::str::FromStr;
use std::task::Context;
use tokio::net::TcpStream;
use xmpp_parsers::{ns, Element, Jid};
use super::happy_eyeballs::connect_to_host;
use self::connect::component_login;
use super::xmpp_codec::Packet;
use super::xmpp_stream;
use super::Error;
use crate::connect::ServerConnector;
use crate::xmpp_stream::add_stanza_id;
use crate::xmpp_stream::XMPPStream;
mod auth;
pub(crate) mod connect;
/// Component connection to an XMPP server
///
/// This simplifies the `XMPPStream` to a `Stream`/`Sink` of `Element`
/// (stanzas). Connection handling however is up to the user.
pub struct Component {
pub struct Component<C: ServerConnector> {
/// The component's Jabber-Id
pub jid: Jid,
stream: XMPPStream,
stream: XMPPStream<C::Stream>,
}
type XMPPStream = xmpp_stream::XMPPStream<TcpStream>;
impl Component {
impl<C: ServerConnector> Component<C> {
/// Start a new XMPP component
pub async fn new(jid: &str, password: &str, server: &str, port: u16) -> Result<Self, Error> {
pub async fn new_with_connector(
jid: &str,
password: &str,
connector: C,
) -> Result<Self, Error> {
let jid = Jid::from_str(jid)?;
let password = password.to_owned();
let stream = Self::connect(jid.clone(), password, server, port).await?;
let stream = component_login(connector, jid.clone(), password).await?;
Ok(Component { jid, stream })
}
async fn connect(
jid: Jid,
password: String,
server: &str,
port: u16,
) -> Result<XMPPStream, Error> {
let password = password;
let tcp_stream = connect_to_host(server, port).await?;
let mut xmpp_stream =
xmpp_stream::XMPPStream::start(tcp_stream, jid, ns::COMPONENT_ACCEPT.to_owned())
.await?;
auth::auth(&mut xmpp_stream, password).await?;
Ok(xmpp_stream)
}
/// Send stanza
pub async fn send_stanza(&mut self, stanza: Element) -> Result<(), Error> {
self.send(add_stanza_id(stanza, ns::COMPONENT_ACCEPT)).await
@ -63,7 +53,7 @@ impl Component {
}
}
impl Stream for Component {
impl<C: ServerConnector> Stream for Component<C> {
type Item = Element;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
@ -86,7 +76,7 @@ impl Stream for Component {
}
}
impl Sink<Element> for Component {
impl<C: ServerConnector> Sink<Element> for Component<C> {
type Error = Error;
fn start_send(mut self: Pin<&mut Self>, item: Element) -> Result<(), Self::Error> {

35
tokio-xmpp/src/connect.rs Normal file
View File

@ -0,0 +1,35 @@
//! `ServerConnector` provides streams for XMPP clients
use sasl::common::ChannelBinding;
use tokio::io::{AsyncRead, AsyncWrite};
use xmpp_parsers::Jid;
use crate::xmpp_stream::XMPPStream;
/// trait returned wrapped in XMPPStream by ServerConnector
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncReadAndWrite for T {}
/// Trait that must be extended by the implementation of ServerConnector
pub trait ServerConnectorError: std::error::Error + Send {}
/// Trait called to connect to an XMPP server, perhaps called multiple times
pub trait ServerConnector: Clone + core::fmt::Debug + Send + Unpin + 'static {
/// The type of Stream this ServerConnector produces
type Stream: AsyncReadAndWrite;
/// Error type to return
type Error: ServerConnectorError;
/// This must return the connection ready to login, ie if starttls is involved, after TLS has been started, and then after the <stream headers are exchanged
fn connect(
&self,
jid: &Jid,
ns: &str,
) -> impl std::future::Future<Output = Result<XMPPStream<Self::Stream>, Self::Error>> + Send;
/// Return channel binding data if available
/// do not fail if channel binding is simply unavailable, just return Ok(None)
/// this should only be called after the TLS handshake is finished
fn channel_binding(_stream: &Self::Stream) -> Result<ChannelBinding, Self::Error> {
Ok(ChannelBinding::None)
}
}

View File

@ -1,41 +1,26 @@
use hickory_resolver::{error::ResolveError, proto::error::ProtoError};
#[cfg(feature = "tls-native")]
use native_tls::Error as TlsError;
use sasl::client::MechanismError as SaslMechanismError;
use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;
use std::str::Utf8Error;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::rustls::client::InvalidDnsNameError;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::rustls::Error as TlsError;
use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
use xmpp_parsers::{Error as ParsersError, JidParseError};
use crate::connect::ServerConnectorError;
/// Top-level error type
#[derive(Debug)]
pub enum Error {
/// I/O error
Io(IoError),
/// Error resolving DNS and establishing a connection
Connection(ConnecterError),
/// DNS label conversion error, no details available from module
/// `idna`
Idna,
/// Error parsing Jabber-Id
JidParse(JidParseError),
/// Protocol-level error
Protocol(ProtocolError),
/// Authentication error
Auth(AuthError),
/// TLS error
Tls(TlsError),
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
/// DNS name parsing error
DnsNameError(InvalidDnsNameError),
/// Connection closed
Disconnected,
/// Shoud never happen
@ -44,6 +29,8 @@ pub enum Error {
Fmt(fmt::Error),
/// Utf8 error
Utf8(Utf8Error),
/// Error resolving DNS and/or establishing a connection, returned by a ServerConnector impl
Connection(Box<dyn ServerConnectorError>),
}
impl fmt::Display for Error {
@ -51,13 +38,9 @@ impl fmt::Display for Error {
match self {
Error::Io(e) => write!(fmt, "IO error: {}", e),
Error::Connection(e) => write!(fmt, "connection error: {}", e),
Error::Idna => write!(fmt, "IDNA error"),
Error::JidParse(e) => write!(fmt, "jid parse error: {}", e),
Error::Protocol(e) => write!(fmt, "protocol error: {}", e),
Error::Auth(e) => write!(fmt, "authentication error: {}", e),
Error::Tls(e) => write!(fmt, "TLS error: {}", e),
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
Error::Disconnected => write!(fmt, "disconnected"),
Error::InvalidState => write!(fmt, "invalid state"),
Error::Fmt(e) => write!(fmt, "Fmt error: {}", e),
@ -74,9 +57,9 @@ impl From<IoError> for Error {
}
}
impl From<ConnecterError> for Error {
fn from(e: ConnecterError) -> Self {
Error::Connection(e)
impl<T: ServerConnectorError + 'static> From<T> for Error {
fn from(e: T) -> Self {
Error::Connection(Box::new(e))
}
}
@ -98,12 +81,6 @@ impl From<AuthError> for Error {
}
}
impl From<TlsError> for Error {
fn from(e: TlsError) -> Self {
Error::Tls(e)
}
}
impl From<fmt::Error> for Error {
fn from(e: fmt::Error) -> Self {
Error::Fmt(e)
@ -116,13 +93,6 @@ impl From<Utf8Error> for Error {
}
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
impl From<InvalidDnsNameError> for Error {
fn from(e: InvalidDnsNameError) -> Self {
Error::DnsNameError(e)
}
}
/// XML parse error wrapper type
#[derive(Debug)]
pub struct ParseError(pub Cow<'static, str>);
@ -227,22 +197,3 @@ impl fmt::Display for AuthError {
}
}
}
/// Error establishing connection
#[derive(Debug)]
pub enum ConnecterError {
/// All attempts failed, no error available
AllFailed,
/// DNS protocol error
Dns(ProtoError),
/// DNS resolution error
Resolve(ResolveError),
}
impl StdError for ConnecterError {}
impl std::fmt::Display for ConnecterError {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(fmt, "{:?}", self)
}
}

View File

@ -5,28 +5,37 @@
#[cfg(all(feature = "tls-native", feature = "tls-rust"))]
compile_error!("Both tls-native and tls-rust features can't be enabled at the same time.");
#[cfg(all(not(feature = "tls-native"), not(feature = "tls-rust")))]
compile_error!("One of tls-native and tls-rust features must be enabled.");
#[cfg(all(
feature = "starttls",
not(feature = "tls-native"),
not(feature = "tls-rust")
))]
compile_error!(
"when starttls feature enabled one of tls-native and tls-rust features must be enabled."
);
mod starttls;
#[cfg(feature = "starttls")]
pub mod starttls;
mod stream_start;
#[cfg(feature = "insecure-tcp")]
pub mod tcp;
mod xmpp_codec;
pub use crate::xmpp_codec::Packet;
mod event;
pub use event::Event;
mod client;
mod happy_eyeballs;
pub mod connect;
pub mod stream_features;
pub mod xmpp_stream;
pub use client::{
async_client::Client as AsyncClient, async_client::Config as AsyncConfig,
async_client::ServerConfig as AsyncServerConfig, simple_client::Client as SimpleClient,
async_client::{Client as AsyncClient, Config as AsyncConfig},
simple_client::Client as SimpleClient,
};
mod component;
pub use crate::component::Component;
mod error;
pub use crate::error::{AuthError, ConnecterError, Error, ParseError, ProtocolError};
pub use starttls::starttls;
pub use crate::error::{AuthError, Error, ParseError, ProtocolError};
// Re-exports
pub use minidom::Element;

View File

@ -1,85 +0,0 @@
use futures::{sink::SinkExt, stream::StreamExt};
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use {
std::sync::Arc,
tokio_rustls::{
client::TlsStream,
rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName},
TlsConnector,
},
webpki_roots,
};
#[cfg(feature = "tls-native")]
use {
native_tls::TlsConnector as NativeTlsConnector,
tokio_native_tls::{TlsConnector, TlsStream},
};
use tokio::io::{AsyncRead, AsyncWrite};
use xmpp_parsers::{ns, Element};
use crate::xmpp_codec::Packet;
use crate::xmpp_stream::XMPPStream;
use crate::{Error, ProtocolError};
#[cfg(feature = "tls-native")]
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let domain = xmpp_stream.jid.domain_str().to_owned();
let stream = xmpp_stream.into_inner();
let tls_stream = TlsConnector::from(NativeTlsConnector::builder().build().unwrap())
.connect(&domain, stream)
.await?;
Ok(tls_stream)
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let domain = xmpp_stream.jid.domain_str().to_owned();
let domain = ServerName::try_from(domain.as_str())?;
let stream = xmpp_stream.into_inner();
let mut root_store = RootCertStore::empty();
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let config = ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
let tls_stream = TlsConnector::from(Arc::new(config))
.connect(domain, stream)
.await?;
Ok(tls_stream)
}
/// Performs `<starttls/>` on an XMPPStream and returns a binary
/// TlsStream.
pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
mut xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let nonza = Element::builder("starttls", ns::TLS).build();
let packet = Packet::Stanza(nonza);
xmpp_stream.send(packet).await?;
loop {
match xmpp_stream.next().await {
Some(Ok(Packet::Stanza(ref stanza))) if stanza.name() == "proceed" => break,
Some(Ok(Packet::Text(_))) => {}
Some(Err(e)) => return Err(e.into()),
_ => {
return Err(ProtocolError::NoTls.into());
}
}
}
get_tls_stream(xmpp_stream).await
}

View File

@ -0,0 +1,35 @@
use std::str::FromStr;
use xmpp_parsers::Jid;
use crate::{AsyncClient, AsyncConfig, Error, SimpleClient};
use super::ServerConfig;
impl AsyncClient<ServerConfig> {
/// Start a new XMPP client
///
/// Start polling the returned instance so that it will connect
/// and yield events.
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
let config = AsyncConfig {
jid: jid.into(),
password: password.into(),
server: ServerConfig::UseSrv,
};
Self::new_with_config(config)
}
}
impl SimpleClient<ServerConfig> {
/// Start a new XMPP client and wait for a usable session
pub async fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, Error> {
let jid = Jid::from_str(jid)?;
Self::new_with_jid(jid, password.into()).await
}
/// Start a new client given that the JID is already parsed.
pub async fn new_with_jid(jid: Jid, password: String) -> Result<Self, Error> {
Self::new_with_jid_connector(ServerConfig::UseSrv, jid, password).await
}
}

View File

@ -0,0 +1,107 @@
//! StartTLS ServerConnector Error
use hickory_resolver::{error::ResolveError, proto::error::ProtoError};
#[cfg(feature = "tls-native")]
use native_tls::Error as TlsError;
use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::rustls::client::InvalidDnsNameError;
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use tokio_rustls::rustls::Error as TlsError;
/// StartTLS ServerConnector Error
#[derive(Debug)]
pub enum Error {
/// Error resolving DNS and establishing a connection
Connection(ConnectorError),
/// DNS label conversion error, no details available from module
/// `idna`
Idna,
/// TLS error
Tls(TlsError),
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
/// DNS name parsing error
DnsNameError(InvalidDnsNameError),
/// tokio-xmpp error
TokioXMPP(crate::error::Error),
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Connection(e) => write!(fmt, "connection error: {}", e),
Error::Idna => write!(fmt, "IDNA error"),
Error::Tls(e) => write!(fmt, "TLS error: {}", e),
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
Error::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
Error::TokioXMPP(e) => write!(fmt, "TokioXMPP error: {}", e),
}
}
}
impl StdError for Error {}
impl From<crate::error::Error> for Error {
fn from(e: crate::error::Error) -> Self {
Error::TokioXMPP(e)
}
}
impl From<ConnectorError> for Error {
fn from(e: ConnectorError) -> Self {
Error::Connection(e)
}
}
impl From<TlsError> for Error {
fn from(e: TlsError) -> Self {
Error::Tls(e)
}
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
impl From<InvalidDnsNameError> for Error {
fn from(e: InvalidDnsNameError) -> Self {
Error::DnsNameError(e)
}
}
/// XML parse error wrapper type
#[derive(Debug)]
pub struct ParseError(pub Cow<'static, str>);
impl StdError for ParseError {
fn description(&self) -> &str {
self.0.as_ref()
}
fn cause(&self) -> Option<&dyn StdError> {
None
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
/// Error establishing connection
#[derive(Debug)]
pub enum ConnectorError {
/// All attempts failed, no error available
AllFailed,
/// DNS protocol error
Dns(ProtoError),
/// DNS resolution error
Resolve(ResolveError),
}
impl StdError for ConnectorError {}
impl std::fmt::Display for ConnectorError {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(fmt, "{:?}", self)
}
}

View File

@ -1,4 +1,4 @@
use crate::{ConnecterError, Error};
use super::error::{ConnectorError, Error};
use hickory_resolver::{IntoName, TokioAsyncResolver};
use idna;
use log::debug;
@ -9,22 +9,24 @@ pub async fn connect_to_host(domain: &str, port: u16) -> Result<TcpStream, Error
let ascii_domain = idna::domain_to_ascii(&domain).map_err(|_| Error::Idna)?;
if let Ok(ip) = ascii_domain.parse() {
return Ok(TcpStream::connect(&SocketAddr::new(ip, port)).await?);
return Ok(TcpStream::connect(&SocketAddr::new(ip, port))
.await
.map_err(|e| Error::from(crate::Error::Io(e)))?);
}
let resolver = TokioAsyncResolver::tokio_from_system_conf().map_err(ConnecterError::Resolve)?;
let resolver = TokioAsyncResolver::tokio_from_system_conf().map_err(ConnectorError::Resolve)?;
let ips = resolver
.lookup_ip(ascii_domain)
.await
.map_err(ConnecterError::Resolve)?;
.map_err(ConnectorError::Resolve)?;
for ip in ips.iter() {
match TcpStream::connect(&SocketAddr::new(ip, port)).await {
Ok(stream) => return Ok(stream),
Err(_) => {}
}
}
Err(Error::Disconnected)
Err(crate::Error::Disconnected.into())
}
pub async fn connect_with_srv(
@ -36,14 +38,16 @@ pub async fn connect_with_srv(
if let Ok(ip) = ascii_domain.parse() {
debug!("Attempting connection to {ip}:{fallback_port}");
return Ok(TcpStream::connect(&SocketAddr::new(ip, fallback_port)).await?);
return Ok(TcpStream::connect(&SocketAddr::new(ip, fallback_port))
.await
.map_err(|e| Error::from(crate::Error::Io(e)))?);
}
let resolver = TokioAsyncResolver::tokio_from_system_conf().map_err(ConnecterError::Resolve)?;
let resolver = TokioAsyncResolver::tokio_from_system_conf().map_err(ConnectorError::Resolve)?;
let srv_domain = format!("{}.{}.", srv, ascii_domain)
.into_name()
.map_err(ConnecterError::Dns)?;
.map_err(ConnectorError::Dns)?;
let srv_records = resolver.srv_lookup(srv_domain.clone()).await.ok();
match srv_records {
@ -56,7 +60,7 @@ pub async fn connect_with_srv(
Err(_) => {}
}
}
Err(Error::Disconnected)
Err(crate::Error::Disconnected.into())
}
None => {
// SRV lookup error, retry with hostname

View File

@ -0,0 +1,173 @@
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
use futures::{sink::SinkExt, stream::StreamExt};
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
use {
std::sync::Arc,
tokio_rustls::{
client::TlsStream,
rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName},
TlsConnector,
},
webpki_roots,
};
#[cfg(feature = "tls-native")]
use {
native_tls::TlsConnector as NativeTlsConnector,
tokio_native_tls::{TlsConnector, TlsStream},
};
use sasl::common::ChannelBinding;
use tokio::{
io::{AsyncRead, AsyncWrite},
net::TcpStream,
};
use xmpp_parsers::{ns, Element, Jid};
use crate::{connect::ServerConnector, xmpp_codec::Packet, AsyncClient, SimpleClient};
use crate::{connect::ServerConnectorError, xmpp_stream::XMPPStream};
use self::error::Error;
use self::happy_eyeballs::{connect_to_host, connect_with_srv};
mod client;
pub mod error;
mod happy_eyeballs;
/// AsyncClient that connects over StartTls
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
/// SimpleClient that connects over StartTls
pub type StartTlsSimpleClient = SimpleClient<ServerConfig>;
/// StartTLS XMPP server connection configuration
#[derive(Clone, Debug)]
pub enum ServerConfig {
/// Use SRV record to find server host
UseSrv,
#[allow(unused)]
/// Manually define server host and port
Manual {
/// Server host name
host: String,
/// Server port
port: u16,
},
}
impl ServerConnectorError for Error {}
impl ServerConnector for ServerConfig {
type Stream = TlsStream<TcpStream>;
type Error = Error;
async fn connect(&self, jid: &Jid, ns: &str) -> Result<XMPPStream<Self::Stream>, Error> {
// TCP connection
let tcp_stream = match self {
ServerConfig::UseSrv => {
connect_with_srv(jid.domain_str(), "_xmpp-client._tcp", 5222).await?
}
ServerConfig::Manual { host, port } => connect_to_host(host.as_str(), *port).await?,
};
// Unencryped XMPPStream
let xmpp_stream = XMPPStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
if xmpp_stream.stream_features.can_starttls() {
// TlsStream
let tls_stream = starttls(xmpp_stream).await?;
// Encrypted XMPPStream
Ok(XMPPStream::start(tls_stream, jid.clone(), ns.to_owned()).await?)
} else {
return Err(crate::Error::Protocol(crate::ProtocolError::NoTls).into());
}
}
fn channel_binding(
#[allow(unused_variables)] stream: &Self::Stream,
) -> Result<sasl::common::ChannelBinding, Error> {
#[cfg(feature = "tls-native")]
{
log::warn!("tls-native doesnt support channel binding, please use tls-rust if you want this feature!");
Ok(ChannelBinding::None)
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
{
let (_, connection) = stream.get_ref();
Ok(match connection.protocol_version() {
// TODO: Add support for TLS 1.2 and earlier.
Some(tokio_rustls::rustls::ProtocolVersion::TLSv1_3) => {
let data = vec![0u8; 32];
let data = connection.export_keying_material(
data,
b"EXPORTER-Channel-Binding",
None,
)?;
ChannelBinding::TlsExporter(data)
}
_ => ChannelBinding::None,
})
}
}
}
#[cfg(feature = "tls-native")]
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let domain = xmpp_stream.jid.domain_str().to_owned();
let stream = xmpp_stream.into_inner();
let tls_stream = TlsConnector::from(NativeTlsConnector::builder().build().unwrap())
.connect(&domain, stream)
.await?;
Ok(tls_stream)
}
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let domain = xmpp_stream.jid.domain_str().to_owned();
let domain = ServerName::try_from(domain.as_str())?;
let stream = xmpp_stream.into_inner();
let mut root_store = RootCertStore::empty();
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));
let config = ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();
let tls_stream = TlsConnector::from(Arc::new(config))
.connect(domain, stream)
.await
.map_err(|e| Error::from(crate::Error::Io(e)))?;
Ok(tls_stream)
}
/// Performs `<starttls/>` on an XMPPStream and returns a binary
/// TlsStream.
pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
mut xmpp_stream: XMPPStream<S>,
) -> Result<TlsStream<S>, Error> {
let nonza = Element::builder("starttls", ns::TLS).build();
let packet = Packet::Stanza(nonza);
xmpp_stream.send(packet).await?;
loop {
match xmpp_stream.next().await {
Some(Ok(Packet::Stanza(ref stanza))) if stanza.name() == "proceed" => break,
Some(Ok(Packet::Text(_))) => {}
Some(Err(e)) => return Err(e.into()),
_ => {
return Err(crate::Error::Protocol(crate::ProtocolError::NoTls).into());
}
}
}
get_tls_stream(xmpp_stream).await
}

View File

@ -0,0 +1,10 @@
use crate::{Component, Error};
use super::TcpServerConnector;
impl Component<TcpServerConnector> {
/// Start a new XMPP component
pub async fn new(jid: &str, password: &str, server: String) -> Result<Self, Error> {
Self::new_with_connector(jid, password, TcpServerConnector::new(server)).await
}
}

View File

@ -0,0 +1,26 @@
//! TCP ServerConnector Error
use core::fmt;
/// TCP ServerConnector Error
#[derive(Debug)]
pub enum Error {
/// tokio-xmpp error
TokioXMPP(crate::error::Error),
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::TokioXMPP(e) => write!(fmt, "TokioXMPP error: {}", e),
}
}
}
impl From<crate::error::Error> for Error {
fn from(e: crate::error::Error) -> Self {
Error::TokioXMPP(e)
}
}

49
tokio-xmpp/src/tcp/mod.rs Normal file
View File

@ -0,0 +1,49 @@
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
use std::sync::Arc;
use tokio::net::TcpStream;
use crate::{
connect::{ServerConnector, ServerConnectorError},
xmpp_stream::XMPPStream,
Component,
};
use self::error::Error;
mod component;
pub mod error;
/// Component that connects over TCP
pub type TcpComponent = Component<TcpServerConnector>;
/// Connect via insecure plaintext TCP to an XMPP server
/// This should only be used over localhost or otherwise when you know what you are doing
/// Probably mostly useful for Components
#[derive(Debug, Clone)]
pub struct TcpServerConnector(Arc<String>);
impl TcpServerConnector {
/// Create a new connector with the given address
pub fn new(addr: String) -> Self {
Self(addr.into())
}
}
impl ServerConnectorError for Error {}
impl ServerConnector for TcpServerConnector {
type Stream = TcpStream;
type Error = Error;
async fn connect(
&self,
jid: &xmpp_parsers::Jid,
ns: &str,
) -> Result<XMPPStream<Self::Stream>, Self::Error> {
let stream = TcpStream::connect(&*self.0)
.await
.map_err(|e| crate::Error::Io(e))?;
Ok(XMPPStream::start(stream, jid.clone(), ns.to_owned()).await?)
}
}

View File

@ -21,7 +21,7 @@ log = "0.4"
reqwest = { version = "0.11.8", features = ["stream"] }
tokio-util = { version = "0.7", features = ["codec"] }
# same repository dependencies
tokio-xmpp = { version = "3.4", path = "../tokio-xmpp" }
tokio-xmpp = { version = "3.4", path = "../tokio-xmpp", default-features = false }
[dev-dependencies]
env_logger = { version = "0.10", default-features = false, features = ["auto-color", "humantime"] }
@ -31,5 +31,7 @@ name = "hello_bot"
required-features = ["avatars"]
[features]
default = ["avatars"]
default = ["avatars", "starttls-rust"]
starttls-native = ["tokio-xmpp/starttls", "tokio-xmpp/tls-native"]
starttls-rust = ["tokio-xmpp/starttls", "tokio-xmpp/tls-rust"]
avatars = []

View File

@ -6,15 +6,15 @@
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};
use tokio_xmpp::connect::ServerConnector;
pub use tokio_xmpp::parsers;
use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
use tokio_xmpp::AsyncClient as TokioXmppClient;
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
pub use tokio_xmpp::{AsyncClient as TokioXmppClient, BareJid, Element, FullJid, Jid};
use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
pub struct Agent {
pub(crate) client: TokioXmppClient,
pub struct Agent<C: ServerConnector> {
pub(crate) client: TokioXmppClient<C>,
pub(crate) default_nick: Arc<RwLock<String>>,
pub(crate) lang: Arc<Vec<String>>,
pub(crate) disco: DiscoInfoResult,
@ -23,7 +23,7 @@ pub struct Agent {
pub(crate) awaiting_disco_bookmarks_type: bool,
}
impl Agent {
impl<C: ServerConnector> Agent<C> {
pub async fn disconnect(&mut self) -> Result<(), Error> {
self.client.send_end().await
}

View File

@ -5,12 +5,13 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::sync::{Arc, RwLock};
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
disco::{DiscoInfoResult, Feature, Identity},
ns,
},
AsyncClient as TokioXmppClient, BareJid, Jid,
AsyncClient as TokioXmppClient, AsyncConfig, BareJid, Jid,
};
use crate::{Agent, ClientFeature};
@ -36,9 +37,10 @@ impl ToString for ClientType {
}
}
pub struct ClientBuilder<'a> {
pub struct ClientBuilder<'a, C: ServerConnector> {
jid: BareJid,
password: &'a str,
server_connector: C,
website: String,
default_nick: String,
lang: Vec<String>,
@ -47,11 +49,26 @@ pub struct ClientBuilder<'a> {
resource: Option<String>,
}
impl ClientBuilder<'_> {
pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a> {
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
impl ClientBuilder<'_, tokio_xmpp::starttls::ServerConfig> {
pub fn new<'a>(
jid: BareJid,
password: &'a str,
) -> ClientBuilder<'a, tokio_xmpp::starttls::ServerConfig> {
Self::new_with_server(jid, password, tokio_xmpp::starttls::ServerConfig::UseSrv)
}
}
impl<C: ServerConnector> ClientBuilder<'_, C> {
pub fn new_with_server<'a>(
jid: BareJid,
password: &'a str,
server_connector: C,
) -> ClientBuilder<'a, C> {
ClientBuilder {
jid,
password,
server_connector,
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
default_nick: String::from("xmpp-rs"),
lang: vec![String::from("en")],
@ -117,19 +134,24 @@ impl ClientBuilder<'_> {
}
}
pub fn build(self) -> Agent {
pub fn build(self) -> Agent<C> {
let jid: Jid = if let Some(resource) = &self.resource {
self.jid.with_resource_str(resource).unwrap().into()
} else {
self.jid.clone().into()
};
let client = TokioXmppClient::new(jid, self.password);
let config = AsyncConfig {
jid,
password: self.password.into(),
server: self.server_connector.clone(),
};
let client = TokioXmppClient::new_with_config(config);
self.build_impl(client)
}
// This function is meant to be used for testing build
pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent {
pub(crate) fn build_impl(self, client: TokioXmppClient<C>) -> Agent<C> {
let disco = self.make_disco();
let node = self.website;

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
bookmarks,
@ -23,7 +24,11 @@ use crate::Agent;
// FIXME: To be removed in the future
// The server doesn't return disco#info feature when querying the account
// so we add it manually because we know it's true
pub async fn handle_disco_info_result_payload(agent: &mut Agent, payload: Element, from: Jid) {
pub async fn handle_disco_info_result_payload<C: ServerConnector>(
agent: &mut Agent<C>,
payload: Element,
from: Jid,
) {
match DiscoInfoResult::try_from(payload.clone()) {
Ok(disco) => {
handle_disco_info_result(agent, disco, from).await;
@ -55,7 +60,11 @@ pub async fn handle_disco_info_result_payload(agent: &mut Agent, payload: Elemen
}
}
pub async fn handle_disco_info_result(agent: &mut Agent, disco: DiscoInfoResult, from: Jid) {
pub async fn handle_disco_info_result<C: ServerConnector>(
agent: &mut Agent<C>,
disco: DiscoInfoResult,
from: Jid,
) {
// Safe unwrap because no DISCO is received when we are not online
if from == agent.client.bound_jid().unwrap().to_bare() && agent.awaiting_disco_bookmarks_type {
info!("Received disco info about bookmarks type");

View File

@ -5,6 +5,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use futures::StreamExt;
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
disco::DiscoInfoQuery, iq::Iq, message::Message, presence::Presence, roster::Roster,
@ -20,7 +21,7 @@ use crate::{iq, message, presence, Agent, Event};
///
/// - `Some(events)` if there are new events; multiple may be returned at once.
/// - `None` if the underlying stream is closed.
pub async fn wait_for_events(agent: &mut Agent) -> Option<Vec<Event>> {
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Option<Vec<Event>> {
if let Some(event) = agent.client.next().await {
let mut events = Vec::new();

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
disco::DiscoInfoQuery,
@ -16,8 +17,8 @@ use tokio_xmpp::{
use crate::{Agent, Event};
pub async fn handle_iq_get(
agent: &mut Agent,
pub async fn handle_iq_get<C: ServerConnector>(
agent: &mut Agent<C>,
_events: &mut Vec<Event>,
from: Jid,
_to: Option<Jid>,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::parsers::iq::{Iq, IqType};
use crate::{Agent, Event};
@ -12,7 +13,7 @@ pub mod get;
pub mod result;
pub mod set;
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
pub async fn handle_iq<C: ServerConnector>(agent: &mut Agent<C>, iq: Iq) -> Vec<Event> {
let mut events = vec![];
let from = iq
.from

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{ns, private::Query as PrivateXMLQuery, roster::Roster},
Element, Jid,
@ -11,8 +12,8 @@ use tokio_xmpp::{
use crate::{disco, pubsub, upload, Agent, Event};
pub async fn handle_iq_result(
agent: &mut Agent,
pub async fn handle_iq_result<C: ServerConnector>(
agent: &mut Agent<C>,
events: &mut Vec<Event>,
from: Jid,
_to: Option<Jid>,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
iq::Iq,
@ -14,8 +15,8 @@ use tokio_xmpp::{
use crate::{Agent, Event};
pub async fn handle_iq_set(
agent: &mut Agent,
pub async fn handle_iq_set<C: ServerConnector>(
agent: &mut Agent<C>,
_events: &mut Vec<Event>,
from: Jid,
_to: Option<Jid>,

View File

@ -35,9 +35,9 @@ pub type Error = tokio_xmpp::Error;
pub type Id = Option<String>;
pub type RoomNick = String;
#[cfg(test)]
#[cfg(all(test, any(feature = "starttls-rust", feature = "starttls-native")))]
mod tests {
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
use super::{BareJid, ClientBuilder, ClientFeature, ClientType, Event};
use std::str::FromStr;
use tokio_xmpp::AsyncClient as TokioXmppClient;
@ -57,7 +57,7 @@ mod tests {
#[cfg(feature = "avatars")]
let client_builder = client_builder.enable_feature(ClientFeature::Avatars);
let mut agent: Agent = client_builder.build_impl(client);
let mut agent = client_builder.build_impl(client);
while let Some(events) = agent.wait_for_events().await {
assert!(match events[0] {

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{message::Message, muc::user::MucUser},
Jid,
@ -11,8 +12,8 @@ use tokio_xmpp::{
use crate::{delay::StanzaTimeInfo, Agent, Event};
pub async fn handle_message_chat(
agent: &mut Agent,
pub async fn handle_message_chat<C: ServerConnector>(
agent: &mut Agent<C>,
events: &mut Vec<Event>,
from: Jid,
message: &Message,

View File

@ -4,12 +4,13 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{parsers::message::Message, Jid};
use crate::{delay::StanzaTimeInfo, Agent, Event};
pub async fn handle_message_group_chat(
agent: &mut Agent,
pub async fn handle_message_group_chat<C: ServerConnector>(
agent: &mut Agent<C>,
events: &mut Vec<Event>,
from: Jid,
message: &Message,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::parsers::{
message::{Message, MessageType},
ns,
@ -14,7 +15,10 @@ use crate::{delay::message_time_info, pubsub, Agent, Event};
pub mod chat;
pub mod group_chat;
pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec<Event> {
pub async fn handle_message<C: ServerConnector>(
agent: &mut Agent<C>,
message: Message,
) -> Vec<Event> {
let mut events = vec![];
let from = message.from.clone().unwrap();
let time_info = message_time_info(&message);

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::message::{Body, Message, MessageType},
Jid,
@ -11,8 +12,8 @@ use tokio_xmpp::{
use crate::Agent;
pub async fn send_message(
agent: &mut Agent,
pub async fn send_message<C: ServerConnector>(
agent: &mut Agent<C>,
recipient: Jid,
type_: MessageType,
lang: &str,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
message::{Body, Message, MessageType},
@ -14,8 +15,8 @@ use tokio_xmpp::{
use crate::{Agent, RoomNick};
pub async fn send_room_private_message(
agent: &mut Agent,
pub async fn send_room_private_message<C: ServerConnector>(
agent: &mut Agent<C>,
room: BareJid,
recipient: RoomNick,
lang: &str,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{
muc::Muc,
@ -14,8 +15,8 @@ use tokio_xmpp::{
use crate::{Agent, RoomNick};
pub async fn join_room(
agent: &mut Agent,
pub async fn join_room<C: ServerConnector>(
agent: &mut Agent<C>,
room: BareJid,
nick: Option<String>,
password: Option<String>,
@ -57,8 +58,8 @@ pub async fn join_room(
/// * `nickname`: The nickname to use in the room.
/// * `lang`: The language of the status message.
/// * `status`: The status message to send.
pub async fn leave_room(
agent: &mut Agent,
pub async fn leave_room<C: ServerConnector>(
agent: &mut Agent<C>,
room_jid: BareJid,
nickname: RoomNick,
lang: impl Into<String>,

View File

@ -4,6 +4,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::parsers::{
muc::user::{MucUser, Status},
presence::{Presence, Type as PresenceType},
@ -12,7 +13,10 @@ use tokio_xmpp::parsers::{
use crate::{Agent, Event};
/// Translate a `Presence` stanza into a list of higher-level `Event`s.
pub async fn handle_presence(_agent: &mut Agent, presence: Presence) -> Vec<Event> {
pub async fn handle_presence<C: ServerConnector>(
_agent: &mut Agent<C>,
presence: Presence,
) -> Vec<Event> {
// Allocate an empty vector to store the events.
let mut events = vec![];

View File

@ -8,6 +8,7 @@ use super::Agent;
use crate::Event;
use std::fs::{self, File};
use std::io::{self, Write};
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::parsers::{
avatar::{Data, Metadata},
iq::Iq,
@ -20,9 +21,9 @@ use tokio_xmpp::parsers::{
Jid,
};
pub(crate) async fn handle_metadata_pubsub_event(
pub(crate) async fn handle_metadata_pubsub_event<C: ServerConnector>(
from: &Jid,
agent: &mut Agent,
agent: &mut Agent<C>,
items: Vec<Item>,
) -> Vec<Event> {
let mut events = Vec::new();

View File

@ -7,18 +7,25 @@
use super::Agent;
use crate::Event;
use std::str::FromStr;
use tokio_xmpp::parsers::{
bookmarks2::{self, Autojoin},
ns,
pubsub::event::PubSubEvent,
pubsub::pubsub::PubSub,
BareJid, Element, Jid,
use tokio_xmpp::{
connect::ServerConnector,
parsers::{
bookmarks2::{self, Autojoin},
ns,
pubsub::event::PubSubEvent,
pubsub::pubsub::PubSub,
BareJid, Element, Jid,
},
};
#[cfg(feature = "avatars")]
pub(crate) mod avatar;
pub(crate) async fn handle_event(from: &Jid, elem: Element, agent: &mut Agent) -> Vec<Event> {
pub(crate) async fn handle_event<C: ServerConnector>(
from: &Jid,
elem: Element,
agent: &mut Agent<C>,
) -> Vec<Event> {
let mut events = Vec::new();
let event = PubSubEvent::try_from(elem);
trace!("PubSub event: {:#?}", event);

View File

@ -10,6 +10,7 @@ use reqwest::{
use std::path::PathBuf;
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::http_upload::{Header as HttpUploadHeader, SlotResult},
Element, Jid,
@ -17,11 +18,11 @@ use tokio_xmpp::{
use crate::{Agent, Event};
pub async fn handle_upload_result(
pub async fn handle_upload_result<C: ServerConnector>(
from: &Jid,
iqid: String,
elem: Element,
agent: &mut Agent,
agent: &mut Agent<C>,
) -> impl IntoIterator<Item = Event> {
let mut res: Option<(usize, PathBuf)> = None;

View File

@ -6,6 +6,7 @@
use std::path::Path;
use tokio::fs::File;
use tokio_xmpp::connect::ServerConnector;
use tokio_xmpp::{
parsers::{http_upload::SlotRequest, iq::Iq},
Jid,
@ -13,7 +14,11 @@ use tokio_xmpp::{
use crate::Agent;
pub async fn upload_file_with(agent: &mut Agent, service: &str, path: &Path) {
pub async fn upload_file_with<C: ServerConnector>(
agent: &mut Agent<C>,
service: &str,
path: &Path,
) {
let name = path.file_name().unwrap().to_str().unwrap().to_string();
let file = File::open(path).await.unwrap();
let size = file.metadata().await.unwrap().len();