Detailed changes
@@ -6,14 +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};
-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, TokioXmppClient};
+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,
@@ -22,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
}
@@ -5,15 +5,16 @@
// 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,
},
- BareJid, Jid,
+ AsyncClient as TokioXmppClient, AsyncConfig, BareJid, Jid,
};
-use crate::{Agent, ClientFeature, TokioXmppClient};
+use crate::{Agent, ClientFeature};
#[derive(Debug)]
pub enum ClientType {
@@ -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;
@@ -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");
@@ -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();
@@ -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>,
@@ -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
@@ -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>,
@@ -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>,
@@ -7,7 +7,6 @@
#![deny(bare_trait_objects)]
pub use tokio_xmpp::parsers;
-use tokio_xmpp::AsyncClient;
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
#[macro_use]
extern crate log;
@@ -32,15 +31,13 @@ pub use builder::{ClientBuilder, ClientType};
pub use event::Event;
pub use feature::ClientFeature;
-type TokioXmppClient = AsyncClient<tokio_xmpp::starttls::ServerConfig>;
-
pub type Error = tokio_xmpp::Error;
pub type Id = Option<String>;
pub type RoomNick = String;
#[cfg(test)]
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;
@@ -60,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] {
@@ -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,
@@ -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,
@@ -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);
@@ -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,
@@ -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,
@@ -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>,
@@ -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![];
@@ -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();
@@ -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);
@@ -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;
@@ -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();