2019-03-21 17:41:29 +00:00
|
|
|
// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// 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/.
|
|
|
|
|
2019-07-25 13:03:22 +00:00
|
|
|
#![deny(bare_trait_objects)]
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
use futures::stream::StreamExt;
|
2022-01-14 09:43:40 +00:00
|
|
|
use reqwest::{
|
|
|
|
header::HeaderMap as ReqwestHeaderMap, Body as ReqwestBody, Client as ReqwestClient,
|
|
|
|
};
|
2019-09-11 14:11:32 +00:00
|
|
|
use std::convert::TryFrom;
|
2021-12-26 23:48:37 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-06-01 09:44:49 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2021-12-26 23:48:37 +00:00
|
|
|
use tokio::fs::File;
|
|
|
|
use tokio_util::codec::{BytesCodec, FramedRead};
|
2020-05-29 22:24:58 +00:00
|
|
|
use tokio_xmpp::{AsyncClient as TokioXmppClient, Event as TokioXmppEvent};
|
2019-03-21 17:41:29 +00:00
|
|
|
use xmpp_parsers::{
|
2019-09-29 01:59:44 +00:00
|
|
|
bookmarks2::Conference,
|
2019-03-21 17:41:29 +00:00
|
|
|
caps::{compute_disco, hash_caps, Caps},
|
|
|
|
disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity},
|
|
|
|
hashes::Algo,
|
2022-01-14 09:43:40 +00:00
|
|
|
http_upload::{Header as HttpUploadHeader, SlotRequest, SlotResult},
|
2019-03-21 17:41:29 +00:00
|
|
|
iq::{Iq, IqType},
|
2019-10-22 23:32:41 +00:00
|
|
|
message::{Body, Message, MessageType},
|
2019-03-21 17:41:29 +00:00
|
|
|
muc::{
|
|
|
|
user::{MucUser, Status},
|
2019-10-22 23:32:41 +00:00
|
|
|
Muc,
|
2019-03-21 17:41:29 +00:00
|
|
|
},
|
|
|
|
ns,
|
|
|
|
presence::{Presence, Type as PresenceType},
|
2019-10-22 23:32:41 +00:00
|
|
|
pubsub::pubsub::{Items, PubSub},
|
|
|
|
roster::{Item as RosterItem, Roster},
|
|
|
|
stanza_error::{DefinedCondition, ErrorType, StanzaError},
|
2021-12-26 23:48:37 +00:00
|
|
|
BareJid, Element, FullJid, Jid,
|
2019-03-21 17:41:29 +00:00
|
|
|
};
|
2019-09-29 02:08:56 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2019-09-29 01:59:44 +00:00
|
|
|
mod pubsub;
|
2019-03-21 17:41:29 +00:00
|
|
|
|
2019-07-05 16:59:05 +00:00
|
|
|
pub type Error = tokio_xmpp::Error;
|
|
|
|
|
2019-03-21 17:41:29 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ClientType {
|
|
|
|
Bot,
|
|
|
|
Pc,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ClientType {
|
|
|
|
fn default() -> Self {
|
|
|
|
ClientType::Bot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for ClientType {
|
|
|
|
fn to_string(&self) -> String {
|
2019-10-22 23:32:41 +00:00
|
|
|
String::from(match self {
|
|
|
|
ClientType::Bot => "bot",
|
|
|
|
ClientType::Pc => "pc",
|
|
|
|
})
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum ClientFeature {
|
2019-09-29 02:02:32 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
2019-03-21 17:41:29 +00:00
|
|
|
Avatars,
|
2019-06-01 15:56:46 +00:00
|
|
|
ContactList,
|
2019-07-25 13:11:39 +00:00
|
|
|
JoinRooms,
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 19:09:20 +00:00
|
|
|
pub type Id = Option<String>;
|
2020-04-29 21:28:57 +00:00
|
|
|
pub type RoomNick = String;
|
|
|
|
|
2019-07-25 13:11:39 +00:00
|
|
|
#[derive(Debug)]
|
2019-03-21 17:41:29 +00:00
|
|
|
pub enum Event {
|
|
|
|
Online,
|
|
|
|
Disconnected,
|
2019-06-01 15:56:46 +00:00
|
|
|
ContactAdded(RosterItem),
|
|
|
|
ContactRemoved(RosterItem),
|
|
|
|
ContactChanged(RosterItem),
|
2019-09-29 02:02:32 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
2019-03-21 17:41:29 +00:00
|
|
|
AvatarRetrieved(Jid, String),
|
2023-02-26 19:09:20 +00:00
|
|
|
ChatMessage(Id, BareJid, Body),
|
2019-09-29 01:59:44 +00:00
|
|
|
JoinRoom(BareJid, Conference),
|
|
|
|
LeaveRoom(BareJid),
|
|
|
|
LeaveAllRooms,
|
2019-09-11 14:11:32 +00:00
|
|
|
RoomJoined(BareJid),
|
|
|
|
RoomLeft(BareJid),
|
2023-02-26 19:09:20 +00:00
|
|
|
RoomMessage(Id, BareJid, RoomNick, Body),
|
|
|
|
ServiceMessage(Id, BareJid, Body),
|
2021-12-26 23:48:37 +00:00
|
|
|
HttpUploadedFile(String),
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ClientBuilder<'a> {
|
2023-06-01 09:58:04 +00:00
|
|
|
jid: BareJid,
|
2019-03-21 17:41:29 +00:00
|
|
|
password: &'a str,
|
|
|
|
website: String,
|
2019-07-25 13:11:39 +00:00
|
|
|
default_nick: String,
|
2020-04-29 21:28:57 +00:00
|
|
|
lang: Vec<String>,
|
2019-03-21 17:41:29 +00:00
|
|
|
disco: (ClientType, String),
|
|
|
|
features: Vec<ClientFeature>,
|
2023-06-01 09:58:04 +00:00
|
|
|
resource: Option<String>,
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientBuilder<'_> {
|
2023-06-01 09:58:04 +00:00
|
|
|
pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a> {
|
2019-03-21 17:41:29 +00:00
|
|
|
ClientBuilder {
|
|
|
|
jid,
|
|
|
|
password,
|
|
|
|
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
|
2019-07-25 13:11:39 +00:00
|
|
|
default_nick: String::from("xmpp-rs"),
|
2020-04-29 21:28:57 +00:00
|
|
|
lang: vec![String::from("en")],
|
2019-03-21 17:41:29 +00:00
|
|
|
disco: (ClientType::default(), String::from("tokio-xmpp")),
|
|
|
|
features: vec![],
|
2023-06-01 09:58:04 +00:00
|
|
|
resource: None,
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:58:04 +00:00
|
|
|
/// Optionally set a resource associated to this device on the client
|
|
|
|
pub fn set_resource(mut self, resource: &str) -> Self {
|
|
|
|
self.resource = Some(resource.to_string());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:41:29 +00:00
|
|
|
pub fn set_client(mut self, type_: ClientType, name: &str) -> Self {
|
|
|
|
self.disco = (type_, String::from(name));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_website(mut self, url: &str) -> Self {
|
|
|
|
self.website = String::from(url);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-07-25 13:11:39 +00:00
|
|
|
pub fn set_default_nick(mut self, nick: &str) -> Self {
|
|
|
|
self.default_nick = String::from(nick);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:28:57 +00:00
|
|
|
pub fn set_lang(mut self, lang: Vec<String>) -> Self {
|
|
|
|
self.lang = lang;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-21 17:41:29 +00:00
|
|
|
pub fn enable_feature(mut self, feature: ClientFeature) -> Self {
|
|
|
|
self.features.push(feature);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_disco(&self) -> DiscoInfoResult {
|
2019-10-22 23:32:41 +00:00
|
|
|
let identities = vec![Identity::new(
|
|
|
|
"client",
|
|
|
|
self.disco.0.to_string(),
|
|
|
|
"en",
|
|
|
|
self.disco.1.to_string(),
|
|
|
|
)];
|
|
|
|
let mut features = vec![Feature::new(ns::DISCO_INFO)];
|
2019-09-29 02:02:32 +00:00
|
|
|
#[cfg(feature = "avatars")]
|
|
|
|
{
|
|
|
|
if self.features.contains(&ClientFeature::Avatars) {
|
|
|
|
features.push(Feature::new(format!("{}+notify", ns::AVATAR_METADATA)));
|
|
|
|
}
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2019-07-25 13:11:39 +00:00
|
|
|
if self.features.contains(&ClientFeature::JoinRooms) {
|
2019-09-29 01:59:44 +00:00
|
|
|
features.push(Feature::new(format!("{}+notify", ns::BOOKMARKS2)));
|
2019-07-25 13:11:39 +00:00
|
|
|
}
|
2019-03-21 17:41:29 +00:00
|
|
|
DiscoInfoResult {
|
|
|
|
node: None,
|
|
|
|
identities,
|
|
|
|
features,
|
|
|
|
extensions: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:58:04 +00:00
|
|
|
pub fn build(self) -> Agent {
|
|
|
|
let jid: Jid = if let Some(resource) = &self.resource {
|
|
|
|
self.jid.clone().with_resource(resource.to_string()).into()
|
|
|
|
} else {
|
|
|
|
self.jid.clone().into()
|
|
|
|
};
|
|
|
|
|
|
|
|
let client = TokioXmppClient::new(jid, self.password);
|
|
|
|
self.build_impl(client)
|
2019-07-05 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function is meant to be used for testing build
|
2023-06-01 09:58:04 +00:00
|
|
|
pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent {
|
2019-03-21 17:41:29 +00:00
|
|
|
let disco = self.make_disco();
|
|
|
|
let node = self.website;
|
|
|
|
|
2023-06-01 09:58:04 +00:00
|
|
|
Agent {
|
2020-03-08 18:57:59 +00:00
|
|
|
client,
|
2023-06-01 09:44:49 +00:00
|
|
|
default_nick: Arc::new(RwLock::new(self.default_nick)),
|
|
|
|
lang: Arc::new(self.lang),
|
2020-03-08 18:57:59 +00:00
|
|
|
disco,
|
|
|
|
node,
|
2021-12-26 23:48:37 +00:00
|
|
|
uploads: Vec::new(),
|
2023-06-01 09:58:04 +00:00
|
|
|
}
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:59:05 +00:00
|
|
|
pub struct Agent {
|
2020-03-08 18:57:59 +00:00
|
|
|
client: TokioXmppClient,
|
2023-06-01 09:44:49 +00:00
|
|
|
default_nick: Arc<RwLock<String>>,
|
|
|
|
lang: Arc<Vec<String>>,
|
2020-03-08 18:57:59 +00:00
|
|
|
disco: DiscoInfoResult,
|
|
|
|
node: String,
|
2021-12-26 23:48:37 +00:00
|
|
|
uploads: Vec<(String, Jid, PathBuf)>,
|
2019-07-05 16:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Agent {
|
2021-12-26 23:50:23 +00:00
|
|
|
pub async fn disconnect(&mut self) -> Result<(), Error> {
|
|
|
|
self.client.send_end().await
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
pub async fn join_room(
|
2019-10-22 23:32:41 +00:00
|
|
|
&mut self,
|
|
|
|
room: BareJid,
|
|
|
|
nick: Option<String>,
|
|
|
|
password: Option<String>,
|
|
|
|
lang: &str,
|
|
|
|
status: &str,
|
|
|
|
) {
|
2019-07-25 13:11:39 +00:00
|
|
|
let mut muc = Muc::new();
|
|
|
|
if let Some(password) = password {
|
|
|
|
muc = muc.with_password(password);
|
|
|
|
}
|
|
|
|
|
2023-06-01 09:44:49 +00:00
|
|
|
let nick = nick.unwrap_or_else(|| self.default_nick.read().unwrap().clone());
|
2019-07-25 13:11:39 +00:00
|
|
|
let room_jid = room.with_resource(nick);
|
2019-10-22 23:32:41 +00:00
|
|
|
let mut presence = Presence::new(PresenceType::None).with_to(Jid::Full(room_jid));
|
2019-07-25 13:11:39 +00:00
|
|
|
presence.add_payload(muc);
|
2019-03-21 17:41:29 +00:00
|
|
|
presence.set_status(String::from(lang), String::from(status));
|
2020-03-08 18:57:59 +00:00
|
|
|
let _ = self.client.send_stanza(presence.into()).await;
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
pub async fn send_message(
|
|
|
|
&mut self,
|
|
|
|
recipient: Jid,
|
|
|
|
type_: MessageType,
|
|
|
|
lang: &str,
|
|
|
|
text: &str,
|
|
|
|
) {
|
2019-03-21 17:41:29 +00:00
|
|
|
let mut message = Message::new(Some(recipient));
|
|
|
|
message.type_ = type_;
|
2019-10-22 23:32:41 +00:00
|
|
|
message
|
|
|
|
.bodies
|
|
|
|
.insert(String::from(lang), Body(String::from(text)));
|
2020-03-08 18:57:59 +00:00
|
|
|
let _ = self.client.send_stanza(message.into()).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_initial_presence(disco: &DiscoInfoResult, node: &str) -> Presence {
|
|
|
|
let caps_data = compute_disco(disco);
|
|
|
|
let hash = hash_caps(&caps_data, Algo::Sha_1).unwrap();
|
|
|
|
let caps = Caps::new(node, hash);
|
|
|
|
|
|
|
|
let mut presence = Presence::new(PresenceType::None);
|
|
|
|
presence.add_payload(caps);
|
|
|
|
presence
|
|
|
|
}
|
|
|
|
|
2020-09-07 09:41:40 +00:00
|
|
|
async fn handle_iq(&mut self, iq: Iq) -> Vec<Event> {
|
|
|
|
let mut events = vec![];
|
|
|
|
let from = iq
|
|
|
|
.from
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| self.client.bound_jid().unwrap().clone());
|
2020-09-07 07:56:04 +00:00
|
|
|
if let IqType::Get(payload) = iq.payload {
|
|
|
|
if payload.is("query", ns::DISCO_INFO) {
|
|
|
|
let query = DiscoInfoQuery::try_from(payload);
|
|
|
|
match query {
|
|
|
|
Ok(query) => {
|
|
|
|
let mut disco_info = self.disco.clone();
|
|
|
|
disco_info.node = query.node;
|
|
|
|
let iq = Iq::from_result(iq.id, Some(disco_info))
|
|
|
|
.with_to(iq.from.unwrap())
|
|
|
|
.into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
let error = StanzaError::new(
|
|
|
|
ErrorType::Modify,
|
|
|
|
DefinedCondition::BadRequest,
|
|
|
|
"en",
|
|
|
|
&format!("{}", err),
|
|
|
|
);
|
|
|
|
let iq = Iq::from_error(iq.id, error)
|
|
|
|
.with_to(iq.from.unwrap())
|
|
|
|
.into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We MUST answer unhandled get iqs with a service-unavailable error.
|
|
|
|
let error = StanzaError::new(
|
|
|
|
ErrorType::Cancel,
|
|
|
|
DefinedCondition::ServiceUnavailable,
|
|
|
|
"en",
|
|
|
|
"No handler defined for this kind of iq.",
|
|
|
|
);
|
|
|
|
let iq = Iq::from_error(iq.id, error)
|
|
|
|
.with_to(iq.from.unwrap())
|
|
|
|
.into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
|
|
|
}
|
2020-09-07 09:41:40 +00:00
|
|
|
} else if let IqType::Result(Some(payload)) = iq.payload {
|
|
|
|
// TODO: move private iqs like this one somewhere else, for
|
|
|
|
// security reasons.
|
2023-05-15 12:33:43 +00:00
|
|
|
if payload.is("query", ns::ROSTER) && Some(from.clone()) == iq.from {
|
2020-09-07 09:41:40 +00:00
|
|
|
let roster = Roster::try_from(payload).unwrap();
|
|
|
|
for item in roster.items.into_iter() {
|
|
|
|
events.push(Event::ContactAdded(item));
|
|
|
|
}
|
|
|
|
} else if payload.is("pubsub", ns::PUBSUB) {
|
|
|
|
let new_events = pubsub::handle_iq_result(&from, payload);
|
|
|
|
events.extend(new_events);
|
2021-12-26 23:48:37 +00:00
|
|
|
} else if payload.is("slot", ns::HTTP_UPLOAD) {
|
|
|
|
let new_events = handle_upload_result(&from, iq.id, payload, self).await;
|
|
|
|
events.extend(new_events);
|
2020-09-07 09:41:40 +00:00
|
|
|
}
|
|
|
|
} else if let IqType::Set(_) = iq.payload {
|
|
|
|
// We MUST answer unhandled set iqs with a service-unavailable error.
|
|
|
|
let error = StanzaError::new(
|
|
|
|
ErrorType::Cancel,
|
|
|
|
DefinedCondition::ServiceUnavailable,
|
|
|
|
"en",
|
|
|
|
"No handler defined for this kind of iq.",
|
|
|
|
);
|
|
|
|
let iq = Iq::from_error(iq.id, error)
|
|
|
|
.with_to(iq.from.unwrap())
|
|
|
|
.into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
2020-09-07 07:56:04 +00:00
|
|
|
}
|
2020-09-07 09:41:40 +00:00
|
|
|
|
|
|
|
events
|
2020-09-07 07:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_message(&mut self, message: Message) -> Vec<Event> {
|
|
|
|
let mut events = vec![];
|
|
|
|
let from = message.from.clone().unwrap();
|
|
|
|
let langs: Vec<&str> = self.lang.iter().map(String::as_str).collect();
|
|
|
|
match message.get_best_body(langs) {
|
|
|
|
Some((_lang, body)) => match message.type_ {
|
|
|
|
MessageType::Groupchat => {
|
2023-02-26 16:42:44 +00:00
|
|
|
let event = match from.clone() {
|
|
|
|
Jid::Full(full) => Event::RoomMessage(
|
2023-02-26 19:09:20 +00:00
|
|
|
message.id.clone(),
|
2023-02-26 16:42:44 +00:00
|
|
|
from.clone().into(),
|
|
|
|
full.resource,
|
|
|
|
body.clone(),
|
|
|
|
),
|
2023-02-26 23:07:57 +00:00
|
|
|
Jid::Bare(bare) => {
|
|
|
|
Event::ServiceMessage(message.id.clone(), bare, body.clone())
|
|
|
|
}
|
2023-02-26 16:42:44 +00:00
|
|
|
};
|
2020-09-07 07:56:04 +00:00
|
|
|
events.push(event)
|
|
|
|
}
|
|
|
|
MessageType::Chat | MessageType::Normal => {
|
2023-02-26 23:07:57 +00:00
|
|
|
let event =
|
|
|
|
Event::ChatMessage(message.id.clone(), from.clone().into(), body.clone());
|
2020-09-07 07:56:04 +00:00
|
|
|
events.push(event)
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
None => (),
|
|
|
|
}
|
|
|
|
for child in message.payloads {
|
|
|
|
if child.is("event", ns::PUBSUB_EVENT) {
|
|
|
|
let new_events = pubsub::handle_event(&from, child, self).await;
|
|
|
|
events.extend(new_events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
events
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_presence(&mut self, presence: Presence) -> Vec<Event> {
|
|
|
|
let mut events = vec![];
|
|
|
|
let from: BareJid = match presence.from.clone().unwrap() {
|
|
|
|
Jid::Full(FullJid { node, domain, .. }) => BareJid { node, domain },
|
|
|
|
Jid::Bare(bare) => bare,
|
|
|
|
};
|
|
|
|
for payload in presence.payloads.into_iter() {
|
|
|
|
let muc_user = match MucUser::try_from(payload) {
|
|
|
|
Ok(muc_user) => muc_user,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
for status in muc_user.status.into_iter() {
|
|
|
|
if status == Status::SelfPresence {
|
|
|
|
events.push(Event::RoomJoined(from.clone()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
events
|
|
|
|
}
|
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
pub async fn wait_for_events(&mut self) -> Option<Vec<Event>> {
|
|
|
|
if let Some(event) = self.client.next().await {
|
|
|
|
let mut events = Vec::new();
|
|
|
|
|
|
|
|
match event {
|
2020-05-29 22:24:58 +00:00
|
|
|
TokioXmppEvent::Online { resumed: false, .. } => {
|
2020-03-08 18:57:59 +00:00
|
|
|
let presence = Self::make_initial_presence(&self.disco, &self.node).into();
|
|
|
|
let _ = self.client.send_stanza(presence).await;
|
|
|
|
events.push(Event::Online);
|
|
|
|
// TODO: only send this when the ContactList feature is enabled.
|
|
|
|
let iq = Iq::from_get(
|
|
|
|
"roster",
|
|
|
|
Roster {
|
|
|
|
ver: None,
|
|
|
|
items: vec![],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
|
|
|
// TODO: only send this when the JoinRooms feature is enabled.
|
|
|
|
let iq =
|
|
|
|
Iq::from_get("bookmarks", PubSub::Items(Items::new(ns::BOOKMARKS2))).into();
|
|
|
|
let _ = self.client.send_stanza(iq).await;
|
|
|
|
}
|
2020-05-29 22:24:58 +00:00
|
|
|
TokioXmppEvent::Online { resumed: true, .. } => {}
|
2020-03-08 18:57:59 +00:00
|
|
|
TokioXmppEvent::Disconnected(_) => {
|
|
|
|
events.push(Event::Disconnected);
|
|
|
|
}
|
2020-09-07 07:56:04 +00:00
|
|
|
TokioXmppEvent::Stanza(elem) => {
|
|
|
|
if elem.is("iq", "jabber:client") {
|
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2020-09-07 09:41:40 +00:00
|
|
|
let new_events = self.handle_iq(iq).await;
|
|
|
|
events.extend(new_events);
|
2020-09-07 07:56:04 +00:00
|
|
|
} else if elem.is("message", "jabber:client") {
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
|
|
|
let new_events = self.handle_message(message).await;
|
|
|
|
events.extend(new_events);
|
|
|
|
} else if elem.is("presence", "jabber:client") {
|
|
|
|
let presence = Presence::try_from(elem).unwrap();
|
|
|
|
let new_events = self.handle_presence(presence).await;
|
|
|
|
events.extend(new_events);
|
|
|
|
} else if elem.is("error", "http://etherx.jabber.org/streams") {
|
|
|
|
println!("Received a fatal stream error: {}", String::from(&elem));
|
2020-03-08 18:57:59 +00:00
|
|
|
} else {
|
2020-09-07 07:56:04 +00:00
|
|
|
panic!("Unknown stanza: {}", String::from(&elem));
|
2020-03-08 18:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(events)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2021-12-26 23:48:37 +00:00
|
|
|
|
|
|
|
pub async fn upload_file_with(&mut self, 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();
|
|
|
|
let slot_request = SlotRequest {
|
|
|
|
filename: name,
|
|
|
|
size: size,
|
|
|
|
content_type: None,
|
|
|
|
};
|
|
|
|
let to = service.parse::<Jid>().unwrap();
|
|
|
|
let request = Iq::from_get("upload1", slot_request).with_to(to.clone());
|
|
|
|
self.uploads
|
|
|
|
.push((String::from("upload1"), to, path.to_path_buf()));
|
|
|
|
self.client.send_stanza(request.into()).await.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_upload_result(
|
|
|
|
from: &Jid,
|
|
|
|
iqid: String,
|
|
|
|
elem: Element,
|
|
|
|
agent: &mut Agent,
|
|
|
|
) -> impl IntoIterator<Item = Event> {
|
|
|
|
let mut res: Option<(usize, PathBuf)> = None;
|
|
|
|
|
|
|
|
for (i, (id, to, filepath)) in agent.uploads.iter().enumerate() {
|
|
|
|
if to == from && id == &iqid {
|
|
|
|
res = Some((i, filepath.to_path_buf()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((index, file)) = res {
|
|
|
|
agent.uploads.remove(index);
|
|
|
|
let slot = SlotResult::try_from(elem).unwrap();
|
2022-01-14 09:43:40 +00:00
|
|
|
|
|
|
|
let mut headers = ReqwestHeaderMap::new();
|
|
|
|
for header in slot.put.headers {
|
|
|
|
let (attr, val) = match header {
|
|
|
|
HttpUploadHeader::Authorization(val) => ("Authorization", val),
|
|
|
|
HttpUploadHeader::Cookie(val) => ("Cookie", val),
|
|
|
|
HttpUploadHeader::Expires(val) => ("Expires", val),
|
|
|
|
};
|
|
|
|
headers.insert(attr, val.parse().unwrap());
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:48:37 +00:00
|
|
|
let web = ReqwestClient::new();
|
|
|
|
let stream = FramedRead::new(File::open(file).await.unwrap(), BytesCodec::new());
|
|
|
|
let body = ReqwestBody::wrap_stream(stream);
|
|
|
|
let res = web
|
|
|
|
.put(slot.put.url.as_str())
|
2022-01-14 09:43:40 +00:00
|
|
|
.headers(headers)
|
2021-12-26 23:48:37 +00:00
|
|
|
.body(body)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
if res.status() == 201 {
|
|
|
|
return vec![Event::HttpUploadedFile(slot.get.url)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec![];
|
2019-03-21 17:41:29 +00:00
|
|
|
}
|
2019-10-22 21:46:50 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-06-01 09:58:04 +00:00
|
|
|
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
|
|
|
|
use std::str::FromStr;
|
2020-05-29 22:24:58 +00:00
|
|
|
use tokio_xmpp::AsyncClient as TokioXmppClient;
|
2019-10-22 21:46:50 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_simple() {
|
2023-06-01 09:58:04 +00:00
|
|
|
let jid = BareJid::from_str("foo@bar").unwrap();
|
|
|
|
|
|
|
|
let client = TokioXmppClient::new(jid.clone(), "meh");
|
2019-10-22 21:46:50 +00:00
|
|
|
|
|
|
|
// Client instance
|
2023-06-01 09:58:04 +00:00
|
|
|
let client_builder = ClientBuilder::new(jid, "meh")
|
2019-10-22 21:46:50 +00:00
|
|
|
.set_client(ClientType::Bot, "xmpp-rs")
|
|
|
|
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
|
|
|
.set_default_nick("bot")
|
|
|
|
.enable_feature(ClientFeature::Avatars)
|
|
|
|
.enable_feature(ClientFeature::ContactList);
|
|
|
|
|
2023-06-01 09:58:04 +00:00
|
|
|
let mut agent: Agent = client_builder.build_impl(client);
|
2019-10-22 21:46:50 +00:00
|
|
|
|
2020-03-08 18:57:59 +00:00
|
|
|
while let Some(events) = agent.wait_for_events().await {
|
|
|
|
assert!(match events[0] {
|
|
|
|
Event::Disconnected => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
assert_eq!(events.len(), 1);
|
|
|
|
break;
|
|
|
|
}
|
2019-10-22 21:46:50 +00:00
|
|
|
}
|
|
|
|
}
|