client: use stanzastream!
This commit is contained in:
parent
80f899daa0
commit
35ce86243f
29 changed files with 238 additions and 565 deletions
|
|
@ -8,7 +8,6 @@ use std::collections::HashMap;
|
|||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio_xmpp::connect::ServerConnector;
|
||||
pub use tokio_xmpp::parsers;
|
||||
use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
|
||||
pub use tokio_xmpp::{
|
||||
|
|
@ -19,8 +18,8 @@ pub use tokio_xmpp::{
|
|||
|
||||
use crate::{event_loop, message, muc, upload, Error, Event, RoomNick};
|
||||
|
||||
pub struct Agent<C: ServerConnector> {
|
||||
pub(crate) client: TokioXmppClient<C>,
|
||||
pub struct Agent {
|
||||
pub(crate) client: TokioXmppClient,
|
||||
pub(crate) default_nick: Arc<RwLock<String>>,
|
||||
pub(crate) lang: Arc<Vec<String>>,
|
||||
pub(crate) disco: DiscoInfoResult,
|
||||
|
|
@ -33,8 +32,8 @@ pub struct Agent<C: ServerConnector> {
|
|||
pub(crate) rooms_leaving: HashMap<BareJid, String>,
|
||||
}
|
||||
|
||||
impl<C: ServerConnector> Agent<C> {
|
||||
pub async fn disconnect(&mut self) -> Result<(), Error> {
|
||||
impl Agent {
|
||||
pub async fn disconnect(self) -> Result<(), Error> {
|
||||
self.client.send_end().await
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -152,25 +152,24 @@ impl<C: ServerConnector> ClientBuilder<'_, C> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Agent<C> {
|
||||
pub fn build(self) -> Agent {
|
||||
let jid: Jid = if let Some(resource) = &self.resource {
|
||||
self.jid.with_resource_str(resource).unwrap().into()
|
||||
} else {
|
||||
self.jid.clone().into()
|
||||
};
|
||||
|
||||
let mut client = TokioXmppClient::new_with_connector(
|
||||
let client = TokioXmppClient::new_with_connector(
|
||||
jid,
|
||||
self.password,
|
||||
self.server_connector.clone(),
|
||||
self.timeouts,
|
||||
);
|
||||
client.set_reconnect(true);
|
||||
self.build_impl(client)
|
||||
}
|
||||
|
||||
// This function is meant to be used for testing build
|
||||
pub(crate) fn build_impl(self, client: TokioXmppClient<C>) -> Agent<C> {
|
||||
pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent {
|
||||
let disco = self.make_disco();
|
||||
let node = self.website;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
parsers::{
|
||||
|
|
@ -19,11 +18,7 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::Agent;
|
||||
|
||||
pub async fn handle_disco_info_result<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
disco: DiscoInfoResult,
|
||||
from: Jid,
|
||||
) {
|
||||
pub async fn handle_disco_info_result(agent: &mut Agent, 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,7 +5,6 @@
|
|||
// 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, roster::Roster},
|
||||
Event as TokioXmppEvent, Stanza,
|
||||
|
|
@ -14,7 +13,7 @@ use tokio_xmpp::{
|
|||
use crate::{iq, message, presence, Agent, Event};
|
||||
|
||||
/// Wait for new events, or Error::Disconnected when stream is closed and will not reconnect.
|
||||
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Vec<Event> {
|
||||
pub async fn wait_for_events(agent: &mut Agent) -> Vec<Event> {
|
||||
if let Some(event) = agent.client.next().await {
|
||||
let mut events = Vec::new();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
minidom::Element,
|
||||
|
|
@ -18,8 +17,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{Agent, Event};
|
||||
|
||||
pub async fn handle_iq_get<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn handle_iq_get(
|
||||
agent: &mut Agent,
|
||||
_events: &mut Vec<Event>,
|
||||
from: Jid,
|
||||
_to: Option<Jid>,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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};
|
||||
|
|
@ -13,7 +12,7 @@ pub mod get;
|
|||
pub mod result;
|
||||
pub mod set;
|
||||
|
||||
pub async fn handle_iq<C: ServerConnector>(agent: &mut Agent<C>, iq: Iq) -> Vec<Event> {
|
||||
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
|
||||
let mut events = vec![];
|
||||
let from = iq
|
||||
.from
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
minidom::Element,
|
||||
|
|
@ -13,8 +12,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{disco, muc::room::JoinRoomSettings, pubsub, upload, Agent, Event};
|
||||
|
||||
pub async fn handle_iq_result<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn handle_iq_result(
|
||||
agent: &mut Agent,
|
||||
events: &mut Vec<Event>,
|
||||
from: Jid,
|
||||
_to: Option<Jid>,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
minidom::Element,
|
||||
|
|
@ -16,8 +15,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{Agent, Event};
|
||||
|
||||
pub async fn handle_iq_set<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn handle_iq_set(
|
||||
agent: &mut Agent,
|
||||
_events: &mut Vec<Event>,
|
||||
from: Jid,
|
||||
_to: Option<Jid>,
|
||||
|
|
|
|||
|
|
@ -38,6 +38,10 @@ pub type Error = tokio_xmpp::Error;
|
|||
pub type Id = Option<String>;
|
||||
pub type RoomNick = String;
|
||||
|
||||
// The test below is dysfunctional since we have moved to StanzaStream. The
|
||||
// StanzaStream will attempt to connect to foo@bar indefinitely.
|
||||
// Keeping it here as inspiration for future integration tests.
|
||||
/*
|
||||
#[cfg(all(test, any(feature = "starttls-rust", feature = "starttls-native")))]
|
||||
mod tests {
|
||||
use super::jid::BareJid;
|
||||
|
|
@ -74,3 +78,4 @@ mod tests {
|
|||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
parsers::{message::Message, muc::user::MucUser},
|
||||
|
|
@ -12,8 +11,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
||||
|
||||
pub async fn handle_message_chat<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn handle_message_chat(
|
||||
agent: &mut Agent,
|
||||
events: &mut Vec<Event>,
|
||||
from: Jid,
|
||||
message: &Message,
|
||||
|
|
|
|||
|
|
@ -4,13 +4,12 @@
|
|||
// 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::{jid::Jid, parsers::message::Message};
|
||||
|
||||
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
||||
|
||||
pub async fn handle_message_group_chat<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn handle_message_group_chat(
|
||||
agent: &mut Agent,
|
||||
events: &mut Vec<Event>,
|
||||
from: Jid,
|
||||
message: &Message,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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,
|
||||
|
|
@ -15,10 +14,7 @@ use crate::{delay::message_time_info, pubsub, Agent, Event};
|
|||
pub mod chat;
|
||||
pub mod group_chat;
|
||||
|
||||
pub async fn handle_message<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
message: Message,
|
||||
) -> Vec<Event> {
|
||||
pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec<Event> {
|
||||
let mut events = vec![];
|
||||
let from = message.from.clone().unwrap();
|
||||
let time_info = message_time_info(&message);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::Jid,
|
||||
parsers::message::{Body, Message, MessageType},
|
||||
|
|
@ -12,8 +11,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::Agent;
|
||||
|
||||
pub async fn send_message<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn send_message(
|
||||
agent: &mut Agent,
|
||||
recipient: Jid,
|
||||
type_: MessageType,
|
||||
lang: &str,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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::{
|
||||
jid::{BareJid, Jid},
|
||||
parsers::{
|
||||
|
|
@ -15,8 +14,8 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{Agent, RoomNick};
|
||||
|
||||
pub async fn send_room_private_message<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
pub async fn send_room_private_message(
|
||||
agent: &mut Agent,
|
||||
room: BareJid,
|
||||
recipient: RoomNick,
|
||||
lang: &str,
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
use crate::parsers::message::MessageType;
|
||||
use tokio_xmpp::connect::ServerConnector;
|
||||
use tokio_xmpp::{
|
||||
jid::BareJid,
|
||||
parsers::{
|
||||
|
|
@ -51,10 +50,7 @@ impl<'a> JoinRoomSettings<'a> {
|
|||
}
|
||||
|
||||
/// TODO: this method should add bookmark and ensure autojoin is true
|
||||
pub async fn join_room<'a, C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
settings: JoinRoomSettings<'a>,
|
||||
) {
|
||||
pub async fn join_room<'a>(agent: &mut Agent, settings: JoinRoomSettings<'a>) {
|
||||
let JoinRoomSettings {
|
||||
room,
|
||||
nick,
|
||||
|
|
@ -122,10 +118,7 @@ impl<'a> LeaveRoomSettings<'a> {
|
|||
/// If successful, a `RoomLeft` event should be received later as a confirmation. See [XEP-0045](https://xmpp.org/extensions/xep-0045.html#exit).
|
||||
///
|
||||
/// TODO: this method should set autojoin false on bookmark
|
||||
pub async fn leave_room<'a, C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
settings: LeaveRoomSettings<'a>,
|
||||
) {
|
||||
pub async fn leave_room<'a>(agent: &mut Agent, settings: LeaveRoomSettings<'a>) {
|
||||
let LeaveRoomSettings { room, status } = settings;
|
||||
|
||||
if agent.rooms_leaving.contains_key(&room) {
|
||||
|
|
@ -188,10 +181,7 @@ impl<'a> RoomMessageSettings<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn send_room_message<'a, C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
settings: RoomMessageSettings<'a>,
|
||||
) {
|
||||
pub async fn send_room_message<'a>(agent: &mut Agent, settings: RoomMessageSettings<'a>) {
|
||||
let RoomMessageSettings {
|
||||
room,
|
||||
message,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
// 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},
|
||||
|
|
@ -13,10 +12,7 @@ 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<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
presence: Presence,
|
||||
) -> Vec<Event> {
|
||||
pub async fn handle_presence(agent: &mut Agent, presence: Presence) -> Vec<Event> {
|
||||
// Allocate an empty vector to store the events.
|
||||
let mut events = vec![];
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ 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,
|
||||
|
|
@ -21,9 +20,9 @@ use tokio_xmpp::parsers::{
|
|||
},
|
||||
};
|
||||
|
||||
pub(crate) async fn handle_metadata_pubsub_event<C: ServerConnector>(
|
||||
pub(crate) async fn handle_metadata_pubsub_event(
|
||||
from: &Jid,
|
||||
agent: &mut Agent<C>,
|
||||
agent: &mut Agent,
|
||||
items: Vec<Item>,
|
||||
) -> Vec<Event> {
|
||||
let mut events = Vec::new();
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ use crate::{
|
|||
};
|
||||
use std::str::FromStr;
|
||||
use tokio_xmpp::{
|
||||
connect::ServerConnector,
|
||||
jid::{BareJid, Jid},
|
||||
minidom::Element,
|
||||
parsers::{
|
||||
|
|
@ -23,10 +22,10 @@ use tokio_xmpp::{
|
|||
#[cfg(feature = "avatars")]
|
||||
pub(crate) mod avatar;
|
||||
|
||||
pub(crate) async fn handle_event<C: ServerConnector>(
|
||||
pub(crate) async fn handle_event(
|
||||
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] from: &Jid,
|
||||
elem: Element,
|
||||
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] agent: &mut Agent<C>,
|
||||
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] agent: &mut Agent,
|
||||
) -> Vec<Event> {
|
||||
// We allow the useless mut warning for no-default-features,
|
||||
// since for now only avatars pushes events here.
|
||||
|
|
@ -101,10 +100,10 @@ pub(crate) async fn handle_event<C: ServerConnector>(
|
|||
events
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_iq_result<C: ServerConnector>(
|
||||
pub(crate) async fn handle_iq_result(
|
||||
#[cfg_attr(not(feature = "avatars"), allow(unused_variables))] from: &Jid,
|
||||
elem: Element,
|
||||
agent: &mut Agent<C>,
|
||||
agent: &mut Agent,
|
||||
) -> impl IntoIterator<Item = Event> {
|
||||
// We allow the useless mut warning for no-default-features,
|
||||
// since for now only avatars pushes events here.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ use reqwest::{
|
|||
use std::path::PathBuf;
|
||||
use tokio::fs::File;
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
use tokio_xmpp::connect::ServerConnector;
|
||||
use tokio_xmpp::{
|
||||
jid::Jid,
|
||||
minidom::Element,
|
||||
|
|
@ -19,11 +18,11 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::{Agent, Event};
|
||||
|
||||
pub async fn handle_upload_result<C: ServerConnector>(
|
||||
pub async fn handle_upload_result(
|
||||
from: &Jid,
|
||||
iqid: String,
|
||||
elem: Element,
|
||||
agent: &mut Agent<C>,
|
||||
agent: &mut Agent,
|
||||
) -> impl IntoIterator<Item = Event> {
|
||||
let mut res: Option<(usize, PathBuf)> = None;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
use std::path::Path;
|
||||
use tokio::fs::File;
|
||||
use tokio_xmpp::connect::ServerConnector;
|
||||
use tokio_xmpp::{
|
||||
jid::Jid,
|
||||
parsers::{http_upload::SlotRequest, iq::Iq},
|
||||
|
|
@ -14,11 +13,7 @@ use tokio_xmpp::{
|
|||
|
||||
use crate::Agent;
|
||||
|
||||
pub async fn upload_file_with<C: ServerConnector>(
|
||||
agent: &mut Agent<C>,
|
||||
service: &str,
|
||||
path: &Path,
|
||||
) {
|
||||
pub async fn upload_file_with(agent: &mut Agent, 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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue