mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
xmpp crate now supports ServerConnector
This commit is contained in:
parent
733d005f51
commit
38bfba4a18
20 changed files with 115 additions and 54 deletions
|
@ -6,14 +6,15 @@
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
pub use tokio_xmpp::parsers;
|
pub use tokio_xmpp::parsers;
|
||||||
use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
|
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 struct Agent<C: ServerConnector> {
|
||||||
pub(crate) client: TokioXmppClient,
|
pub(crate) client: TokioXmppClient<C>,
|
||||||
pub(crate) default_nick: Arc<RwLock<String>>,
|
pub(crate) default_nick: Arc<RwLock<String>>,
|
||||||
pub(crate) lang: Arc<Vec<String>>,
|
pub(crate) lang: Arc<Vec<String>>,
|
||||||
pub(crate) disco: DiscoInfoResult,
|
pub(crate) disco: DiscoInfoResult,
|
||||||
|
@ -22,7 +23,7 @@ pub struct Agent {
|
||||||
pub(crate) awaiting_disco_bookmarks_type: bool,
|
pub(crate) awaiting_disco_bookmarks_type: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Agent {
|
impl<C: ServerConnector> Agent<C> {
|
||||||
pub async fn disconnect(&mut self) -> Result<(), Error> {
|
pub async fn disconnect(&mut self) -> Result<(), Error> {
|
||||||
self.client.send_end().await
|
self.client.send_end().await
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,15 +5,16 @@
|
||||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
disco::{DiscoInfoResult, Feature, Identity},
|
disco::{DiscoInfoResult, Feature, Identity},
|
||||||
ns,
|
ns,
|
||||||
},
|
},
|
||||||
BareJid, Jid,
|
AsyncClient as TokioXmppClient, AsyncConfig, BareJid, Jid,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Agent, ClientFeature, TokioXmppClient};
|
use crate::{Agent, ClientFeature};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ClientType {
|
pub enum ClientType {
|
||||||
|
@ -36,9 +37,10 @@ impl ToString for ClientType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClientBuilder<'a> {
|
pub struct ClientBuilder<'a, C: ServerConnector> {
|
||||||
jid: BareJid,
|
jid: BareJid,
|
||||||
password: &'a str,
|
password: &'a str,
|
||||||
|
server_connector: C,
|
||||||
website: String,
|
website: String,
|
||||||
default_nick: String,
|
default_nick: String,
|
||||||
lang: Vec<String>,
|
lang: Vec<String>,
|
||||||
|
@ -47,11 +49,26 @@ pub struct ClientBuilder<'a> {
|
||||||
resource: Option<String>,
|
resource: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ClientBuilder<'_> {
|
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
|
||||||
pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a> {
|
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 {
|
ClientBuilder {
|
||||||
jid,
|
jid,
|
||||||
password,
|
password,
|
||||||
|
server_connector,
|
||||||
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
|
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
|
||||||
default_nick: String::from("xmpp-rs"),
|
default_nick: String::from("xmpp-rs"),
|
||||||
lang: vec![String::from("en")],
|
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 {
|
let jid: Jid = if let Some(resource) = &self.resource {
|
||||||
self.jid.with_resource_str(resource).unwrap().into()
|
self.jid.with_resource_str(resource).unwrap().into()
|
||||||
} else {
|
} else {
|
||||||
self.jid.clone().into()
|
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)
|
self.build_impl(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is meant to be used for testing build
|
// 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 disco = self.make_disco();
|
||||||
let node = self.website;
|
let node = self.website;
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
bookmarks,
|
bookmarks,
|
||||||
|
@ -23,7 +24,11 @@ use crate::Agent;
|
||||||
// FIXME: To be removed in the future
|
// FIXME: To be removed in the future
|
||||||
// The server doesn't return disco#info feature when querying the account
|
// The server doesn't return disco#info feature when querying the account
|
||||||
// so we add it manually because we know it's true
|
// 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()) {
|
match DiscoInfoResult::try_from(payload.clone()) {
|
||||||
Ok(disco) => {
|
Ok(disco) => {
|
||||||
handle_disco_info_result(agent, disco, from).await;
|
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
|
// 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 {
|
if from == agent.client.bound_jid().unwrap().to_bare() && agent.awaiting_disco_bookmarks_type {
|
||||||
info!("Received disco info about 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
disco::DiscoInfoQuery, iq::Iq, message::Message, presence::Presence, roster::Roster,
|
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.
|
/// - `Some(events)` if there are new events; multiple may be returned at once.
|
||||||
/// - `None` if the underlying stream is closed.
|
/// - `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 {
|
if let Some(event) = agent.client.next().await {
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
disco::DiscoInfoQuery,
|
disco::DiscoInfoQuery,
|
||||||
|
@ -16,8 +17,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_iq_get(
|
pub async fn handle_iq_get<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
_events: &mut Vec<Event>,
|
_events: &mut Vec<Event>,
|
||||||
from: Jid,
|
from: Jid,
|
||||||
_to: Option<Jid>,
|
_to: Option<Jid>,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// 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 tokio_xmpp::parsers::iq::{Iq, IqType};
|
||||||
|
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
@ -12,7 +13,7 @@ pub mod get;
|
||||||
pub mod result;
|
pub mod result;
|
||||||
pub mod set;
|
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 mut events = vec![];
|
||||||
let from = iq
|
let from = iq
|
||||||
.from
|
.from
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{ns, private::Query as PrivateXMLQuery, roster::Roster},
|
parsers::{ns, private::Query as PrivateXMLQuery, roster::Roster},
|
||||||
Element, Jid,
|
Element, Jid,
|
||||||
|
@ -11,8 +12,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{disco, pubsub, upload, Agent, Event};
|
use crate::{disco, pubsub, upload, Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_iq_result(
|
pub async fn handle_iq_result<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
events: &mut Vec<Event>,
|
events: &mut Vec<Event>,
|
||||||
from: Jid,
|
from: Jid,
|
||||||
_to: Option<Jid>,
|
_to: Option<Jid>,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
iq::Iq,
|
iq::Iq,
|
||||||
|
@ -14,8 +15,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_iq_set(
|
pub async fn handle_iq_set<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
_events: &mut Vec<Event>,
|
_events: &mut Vec<Event>,
|
||||||
from: Jid,
|
from: Jid,
|
||||||
_to: Option<Jid>,
|
_to: Option<Jid>,
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
#![deny(bare_trait_objects)]
|
#![deny(bare_trait_objects)]
|
||||||
|
|
||||||
pub use tokio_xmpp::parsers;
|
pub use tokio_xmpp::parsers;
|
||||||
use tokio_xmpp::AsyncClient;
|
|
||||||
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
|
pub use tokio_xmpp::{BareJid, Element, FullJid, Jid};
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
@ -32,15 +31,13 @@ pub use builder::{ClientBuilder, ClientType};
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use feature::ClientFeature;
|
pub use feature::ClientFeature;
|
||||||
|
|
||||||
type TokioXmppClient = AsyncClient<tokio_xmpp::starttls::ServerConfig>;
|
|
||||||
|
|
||||||
pub type Error = tokio_xmpp::Error;
|
pub type Error = tokio_xmpp::Error;
|
||||||
pub type Id = Option<String>;
|
pub type Id = Option<String>;
|
||||||
pub type RoomNick = String;
|
pub type RoomNick = String;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
|
use super::{BareJid, ClientBuilder, ClientFeature, ClientType, Event};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::AsyncClient as TokioXmppClient;
|
use tokio_xmpp::AsyncClient as TokioXmppClient;
|
||||||
|
|
||||||
|
@ -60,7 +57,7 @@ mod tests {
|
||||||
#[cfg(feature = "avatars")]
|
#[cfg(feature = "avatars")]
|
||||||
let client_builder = client_builder.enable_feature(ClientFeature::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 {
|
while let Some(events) = agent.wait_for_events().await {
|
||||||
assert!(match events[0] {
|
assert!(match events[0] {
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{message::Message, muc::user::MucUser},
|
parsers::{message::Message, muc::user::MucUser},
|
||||||
Jid,
|
Jid,
|
||||||
|
@ -11,8 +12,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_message_chat(
|
pub async fn handle_message_chat<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
events: &mut Vec<Event>,
|
events: &mut Vec<Event>,
|
||||||
from: Jid,
|
from: Jid,
|
||||||
message: &Message,
|
message: &Message,
|
||||||
|
|
|
@ -4,12 +4,13 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// 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 tokio_xmpp::{parsers::message::Message, Jid};
|
||||||
|
|
||||||
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
use crate::{delay::StanzaTimeInfo, Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_message_group_chat(
|
pub async fn handle_message_group_chat<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
events: &mut Vec<Event>,
|
events: &mut Vec<Event>,
|
||||||
from: Jid,
|
from: Jid,
|
||||||
message: &Message,
|
message: &Message,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::parsers::{
|
use tokio_xmpp::parsers::{
|
||||||
message::{Message, MessageType},
|
message::{Message, MessageType},
|
||||||
ns,
|
ns,
|
||||||
|
@ -14,7 +15,10 @@ use crate::{delay::message_time_info, pubsub, Agent, Event};
|
||||||
pub mod chat;
|
pub mod chat;
|
||||||
pub mod group_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 mut events = vec![];
|
||||||
let from = message.from.clone().unwrap();
|
let from = message.from.clone().unwrap();
|
||||||
let time_info = message_time_info(&message);
|
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
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::message::{Body, Message, MessageType},
|
parsers::message::{Body, Message, MessageType},
|
||||||
Jid,
|
Jid,
|
||||||
|
@ -11,8 +12,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::Agent;
|
use crate::Agent;
|
||||||
|
|
||||||
pub async fn send_message(
|
pub async fn send_message<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
recipient: Jid,
|
recipient: Jid,
|
||||||
type_: MessageType,
|
type_: MessageType,
|
||||||
lang: &str,
|
lang: &str,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
message::{Body, Message, MessageType},
|
message::{Body, Message, MessageType},
|
||||||
|
@ -14,8 +15,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{Agent, RoomNick};
|
use crate::{Agent, RoomNick};
|
||||||
|
|
||||||
pub async fn send_room_private_message(
|
pub async fn send_room_private_message<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
room: BareJid,
|
room: BareJid,
|
||||||
recipient: RoomNick,
|
recipient: RoomNick,
|
||||||
lang: &str,
|
lang: &str,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{
|
parsers::{
|
||||||
muc::Muc,
|
muc::Muc,
|
||||||
|
@ -14,8 +15,8 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{Agent, RoomNick};
|
use crate::{Agent, RoomNick};
|
||||||
|
|
||||||
pub async fn join_room(
|
pub async fn join_room<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
room: BareJid,
|
room: BareJid,
|
||||||
nick: Option<String>,
|
nick: Option<String>,
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
|
@ -57,8 +58,8 @@ pub async fn join_room(
|
||||||
/// * `nickname`: The nickname to use in the room.
|
/// * `nickname`: The nickname to use in the room.
|
||||||
/// * `lang`: The language of the status message.
|
/// * `lang`: The language of the status message.
|
||||||
/// * `status`: The status message to send.
|
/// * `status`: The status message to send.
|
||||||
pub async fn leave_room(
|
pub async fn leave_room<C: ServerConnector>(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
room_jid: BareJid,
|
room_jid: BareJid,
|
||||||
nickname: RoomNick,
|
nickname: RoomNick,
|
||||||
lang: impl Into<String>,
|
lang: impl Into<String>,
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
// 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/.
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::parsers::{
|
use tokio_xmpp::parsers::{
|
||||||
muc::user::{MucUser, Status},
|
muc::user::{MucUser, Status},
|
||||||
presence::{Presence, Type as PresenceType},
|
presence::{Presence, Type as PresenceType},
|
||||||
|
@ -12,7 +13,10 @@ use tokio_xmpp::parsers::{
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
|
||||||
/// Translate a `Presence` stanza into a list of higher-level `Event`s.
|
/// 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.
|
// Allocate an empty vector to store the events.
|
||||||
let mut events = vec![];
|
let mut events = vec![];
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use super::Agent;
|
||||||
use crate::Event;
|
use crate::Event;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::parsers::{
|
use tokio_xmpp::parsers::{
|
||||||
avatar::{Data, Metadata},
|
avatar::{Data, Metadata},
|
||||||
iq::Iq,
|
iq::Iq,
|
||||||
|
@ -20,9 +21,9 @@ use tokio_xmpp::parsers::{
|
||||||
Jid,
|
Jid,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) async fn handle_metadata_pubsub_event(
|
pub(crate) async fn handle_metadata_pubsub_event<C: ServerConnector>(
|
||||||
from: &Jid,
|
from: &Jid,
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
items: Vec<Item>,
|
items: Vec<Item>,
|
||||||
) -> Vec<Event> {
|
) -> Vec<Event> {
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
|
|
|
@ -7,18 +7,25 @@
|
||||||
use super::Agent;
|
use super::Agent;
|
||||||
use crate::Event;
|
use crate::Event;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::parsers::{
|
use tokio_xmpp::{
|
||||||
bookmarks2::{self, Autojoin},
|
connect::ServerConnector,
|
||||||
ns,
|
parsers::{
|
||||||
pubsub::event::PubSubEvent,
|
bookmarks2::{self, Autojoin},
|
||||||
pubsub::pubsub::PubSub,
|
ns,
|
||||||
BareJid, Element, Jid,
|
pubsub::event::PubSubEvent,
|
||||||
|
pubsub::pubsub::PubSub,
|
||||||
|
BareJid, Element, Jid,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "avatars")]
|
#[cfg(feature = "avatars")]
|
||||||
pub(crate) mod avatar;
|
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 mut events = Vec::new();
|
||||||
let event = PubSubEvent::try_from(elem);
|
let event = PubSubEvent::try_from(elem);
|
||||||
trace!("PubSub event: {:#?}", event);
|
trace!("PubSub event: {:#?}", event);
|
||||||
|
|
|
@ -10,6 +10,7 @@ use reqwest::{
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::http_upload::{Header as HttpUploadHeader, SlotResult},
|
parsers::http_upload::{Header as HttpUploadHeader, SlotResult},
|
||||||
Element, Jid,
|
Element, Jid,
|
||||||
|
@ -17,11 +18,11 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::{Agent, Event};
|
use crate::{Agent, Event};
|
||||||
|
|
||||||
pub async fn handle_upload_result(
|
pub async fn handle_upload_result<C: ServerConnector>(
|
||||||
from: &Jid,
|
from: &Jid,
|
||||||
iqid: String,
|
iqid: String,
|
||||||
elem: Element,
|
elem: Element,
|
||||||
agent: &mut Agent,
|
agent: &mut Agent<C>,
|
||||||
) -> impl IntoIterator<Item = Event> {
|
) -> impl IntoIterator<Item = Event> {
|
||||||
let mut res: Option<(usize, PathBuf)> = None;
|
let mut res: Option<(usize, PathBuf)> = None;
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
|
use tokio_xmpp::connect::ServerConnector;
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
parsers::{http_upload::SlotRequest, iq::Iq},
|
parsers::{http_upload::SlotRequest, iq::Iq},
|
||||||
Jid,
|
Jid,
|
||||||
|
@ -13,7 +14,11 @@ use tokio_xmpp::{
|
||||||
|
|
||||||
use crate::Agent;
|
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 name = path.file_name().unwrap().to_str().unwrap().to_string();
|
||||||
let file = File::open(path).await.unwrap();
|
let file = File::open(path).await.unwrap();
|
||||||
let size = file.metadata().await.unwrap().len();
|
let size = file.metadata().await.unwrap().len();
|
||||||
|
|
Loading…
Reference in a new issue