xmpp: More nicks are typed as RoomNick
This commit is contained in:
parent
48cb79b2d1
commit
793b76d0d5
7 changed files with 43 additions and 44 deletions
|
|
@ -4,11 +4,14 @@
|
||||||
// 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 xmpp::{
|
||||||
|
jid::BareJid,
|
||||||
|
muc::room::{JoinRoomSettings, RoomMessageSettings},
|
||||||
|
ClientBuilder, ClientFeature, ClientType, Event, RoomNick,
|
||||||
|
};
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::jid::{BareJid, ResourcePart};
|
|
||||||
use xmpp::muc::room::{JoinRoomSettings, RoomMessageSettings};
|
|
||||||
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Option<()>> {
|
async fn main() -> Result<(), Option<()>> {
|
||||||
|
|
@ -39,7 +42,7 @@ async fn main() -> Result<(), Option<()>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let nick = ResourcePart::new("bot").unwrap();
|
let nick = RoomNick::from_str("bot").unwrap();
|
||||||
|
|
||||||
// Client instance
|
// Client instance
|
||||||
let mut client = ClientBuilder::new(jid, password)
|
let mut client = ClientBuilder::new(jid, password)
|
||||||
|
|
|
||||||
|
|
@ -11,25 +11,25 @@ use tokio::sync::RwLock;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
event_loop,
|
event_loop,
|
||||||
jid::{BareJid, Jid, ResourcePart},
|
jid::{BareJid, Jid},
|
||||||
message, muc,
|
message, muc,
|
||||||
parsers::disco::DiscoInfoResult,
|
parsers::disco::DiscoInfoResult,
|
||||||
upload, Error, Event,
|
upload, Error, Event, RoomNick,
|
||||||
};
|
};
|
||||||
use tokio_xmpp::Client as TokioXmppClient;
|
use tokio_xmpp::Client as TokioXmppClient;
|
||||||
|
|
||||||
pub struct Agent {
|
pub struct Agent {
|
||||||
pub(crate) client: TokioXmppClient,
|
pub(crate) client: TokioXmppClient,
|
||||||
pub(crate) default_nick: Arc<RwLock<ResourcePart>>,
|
pub(crate) default_nick: Arc<RwLock<RoomNick>>,
|
||||||
pub(crate) lang: Arc<Vec<String>>,
|
pub(crate) lang: Arc<Vec<String>>,
|
||||||
pub(crate) disco: DiscoInfoResult,
|
pub(crate) disco: DiscoInfoResult,
|
||||||
pub(crate) node: String,
|
pub(crate) node: String,
|
||||||
pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
|
pub(crate) uploads: Vec<(String, Jid, PathBuf)>,
|
||||||
pub(crate) awaiting_disco_bookmarks_type: bool,
|
pub(crate) awaiting_disco_bookmarks_type: bool,
|
||||||
// Mapping of room->nick
|
// Mapping of room->nick
|
||||||
pub(crate) rooms_joined: HashMap<BareJid, ResourcePart>,
|
pub(crate) rooms_joined: HashMap<BareJid, RoomNick>,
|
||||||
pub(crate) rooms_joining: HashMap<BareJid, ResourcePart>,
|
pub(crate) rooms_joining: HashMap<BareJid, RoomNick>,
|
||||||
pub(crate) rooms_leaving: HashMap<BareJid, ResourcePart>,
|
pub(crate) rooms_leaving: HashMap<BareJid, RoomNick>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Agent {
|
impl Agent {
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,23 @@
|
||||||
// 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/.
|
||||||
|
|
||||||
|
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
|
||||||
|
use crate::tokio_xmpp::connect::{DnsConfig, StartTlsServerConnector};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
|
|
||||||
use tokio_xmpp::connect::{DnsConfig, StartTlsServerConnector};
|
use crate::{
|
||||||
use tokio_xmpp::{
|
jid::{BareJid, Jid, ResourceRef},
|
||||||
connect::ServerConnector,
|
|
||||||
jid::{BareJid, Jid, ResourcePart, ResourceRef},
|
|
||||||
parsers::{
|
parsers::{
|
||||||
disco::{DiscoInfoResult, Feature, Identity},
|
disco::{DiscoInfoResult, Feature, Identity},
|
||||||
ns,
|
ns,
|
||||||
},
|
},
|
||||||
xmlstream::Timeouts,
|
tokio_xmpp::{connect::ServerConnector, xmlstream::Timeouts, Client as TokioXmppClient},
|
||||||
Client as TokioXmppClient,
|
Agent, ClientFeature, RoomNick,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Agent, ClientFeature};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ClientType {
|
pub enum ClientType {
|
||||||
Bot,
|
Bot,
|
||||||
|
|
@ -48,7 +47,7 @@ pub struct ClientBuilder<'a, C: ServerConnector> {
|
||||||
password: &'a str,
|
password: &'a str,
|
||||||
server_connector: C,
|
server_connector: C,
|
||||||
website: String,
|
website: String,
|
||||||
default_nick: ResourcePart,
|
default_nick: RoomNick,
|
||||||
lang: Vec<String>,
|
lang: Vec<String>,
|
||||||
disco: (ClientType, String),
|
disco: (ClientType, String),
|
||||||
features: Vec<ClientFeature>,
|
features: Vec<ClientFeature>,
|
||||||
|
|
@ -78,7 +77,7 @@ impl<C: ServerConnector> ClientBuilder<'_, C> {
|
||||||
password,
|
password,
|
||||||
server_connector,
|
server_connector,
|
||||||
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
|
website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"),
|
||||||
default_nick: ResourcePart::new("xmpp-rs").unwrap().into(),
|
default_nick: RoomNick::from_str("xmpp-rs").unwrap(),
|
||||||
lang: vec![String::from("en")],
|
lang: vec![String::from("en")],
|
||||||
disco: (ClientType::default(), String::from("tokio-xmpp")),
|
disco: (ClientType::default(), String::from("tokio-xmpp")),
|
||||||
features: vec![],
|
features: vec![],
|
||||||
|
|
@ -104,7 +103,7 @@ impl<C: ServerConnector> ClientBuilder<'_, C> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_default_nick(mut self, nick: impl AsRef<ResourceRef>) -> Self {
|
pub fn set_default_nick(mut self, nick: impl AsRef<ResourceRef>) -> Self {
|
||||||
self.default_nick = nick.as_ref().to_owned();
|
self.default_nick = RoomNick::from_resource_ref(nick.as_ref());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,15 @@
|
||||||
// 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::{
|
use crate::{
|
||||||
|
disco,
|
||||||
jid::Jid,
|
jid::Jid,
|
||||||
minidom::Element,
|
minidom::Element,
|
||||||
|
muc::room::JoinRoomSettings,
|
||||||
parsers::{disco::DiscoInfoResult, ns, private::Query as PrivateXMLQuery, roster::Roster},
|
parsers::{disco::DiscoInfoResult, ns, private::Query as PrivateXMLQuery, roster::Roster},
|
||||||
|
pubsub, upload, Agent, Event, RoomNick,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{disco, muc::room::JoinRoomSettings, pubsub, upload, Agent, Event};
|
|
||||||
|
|
||||||
pub async fn handle_iq_result(
|
pub async fn handle_iq_result(
|
||||||
agent: &mut Agent,
|
agent: &mut Agent,
|
||||||
events: &mut Vec<Event>,
|
events: &mut Vec<Event>,
|
||||||
|
|
@ -41,7 +42,7 @@ pub async fn handle_iq_result(
|
||||||
agent
|
agent
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
room: jid,
|
room: jid,
|
||||||
nick: room.nick,
|
nick: room.nick.map(RoomNick::new),
|
||||||
password: room.password,
|
password: room.password,
|
||||||
status: None,
|
status: None,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ mod tests {
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_simple() {
|
async fn test_simple() {
|
||||||
let jid = BareJid::from_str("foo@bar").unwrap();
|
let jid = BareJid::from_str("foo@bar").unwrap();
|
||||||
let nick = ResourcePart::new("bot").unwrap();
|
let nick = RoomNick::from_str("bot").unwrap();
|
||||||
|
|
||||||
let client = TokioXmppClient::new(jid.clone(), "meh");
|
let client = TokioXmppClient::new(jid.clone(), "meh");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,20 +5,20 @@
|
||||||
// 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 crate::{
|
use crate::{
|
||||||
jid::{BareJid, ResourcePart, ResourceRef},
|
jid::{BareJid, ResourceRef},
|
||||||
message::send::RawMessageSettings,
|
message::send::RawMessageSettings,
|
||||||
parsers::{
|
parsers::{
|
||||||
message::MessageType,
|
message::MessageType,
|
||||||
muc::Muc,
|
muc::Muc,
|
||||||
presence::{Presence, Type as PresenceType},
|
presence::{Presence, Type as PresenceType},
|
||||||
},
|
},
|
||||||
Agent,
|
Agent, RoomNick,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct JoinRoomSettings<'a> {
|
pub struct JoinRoomSettings<'a> {
|
||||||
pub room: BareJid,
|
pub room: BareJid,
|
||||||
pub nick: Option<ResourcePart>,
|
pub nick: Option<RoomNick>,
|
||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
pub status: Option<(&'a str, &'a str)>,
|
pub status: Option<(&'a str, &'a str)>,
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +34,7 @@ impl<'a> JoinRoomSettings<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_nick(mut self, nick: impl AsRef<ResourceRef>) -> Self {
|
pub fn with_nick(mut self, nick: impl AsRef<ResourceRef>) -> Self {
|
||||||
self.nick = Some(nick.as_ref().into());
|
self.nick = Some(RoomNick::from_resource_ref(nick.as_ref()));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ pub async fn join_room<'a>(agent: &mut Agent, settings: JoinRoomSettings<'a>) {
|
||||||
agent.default_nick.read().await.clone()
|
agent.default_nick.read().await.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let room_jid = room.with_resource(&nick);
|
let room_jid = room.with_resource(nick.as_ref());
|
||||||
let mut presence = Presence::new(PresenceType::None).with_to(room_jid);
|
let mut presence = Presence::new(PresenceType::None).with_to(room_jid);
|
||||||
presence.add_payload(muc);
|
presence.add_payload(muc);
|
||||||
|
|
||||||
|
|
@ -138,10 +138,8 @@ pub async fn leave_room<'a>(agent: &mut Agent, settings: LeaveRoomSettings<'a>)
|
||||||
|
|
||||||
// XEP-0045 specifies that, to leave a room, the client must send a presence stanza
|
// XEP-0045 specifies that, to leave a room, the client must send a presence stanza
|
||||||
// with type="unavailable".
|
// with type="unavailable".
|
||||||
let mut presence = Presence::new(PresenceType::Unavailable).with_to(
|
let mut presence =
|
||||||
room.with_resource_str(nickname.as_str())
|
Presence::new(PresenceType::Unavailable).with_to(room.with_resource(nickname.as_ref()));
|
||||||
.expect("Invalid room JID after adding resource part."),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Optionally, the client may include a status message in the presence stanza.
|
// Optionally, the client may include a status message in the presence stanza.
|
||||||
// TODO: Should this be optional? The XEP says "MAY", but the method signature requires the arguments.
|
// TODO: Should this be optional? The XEP says "MAY", but the method signature requires the arguments.
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,19 @@
|
||||||
// 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 super::Agent;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
muc::room::{JoinRoomSettings, LeaveRoomSettings},
|
|
||||||
Event,
|
|
||||||
};
|
|
||||||
use std::str::FromStr;
|
|
||||||
use tokio_xmpp::{
|
|
||||||
jid::{BareJid, Jid},
|
jid::{BareJid, Jid},
|
||||||
minidom::Element,
|
minidom::Element,
|
||||||
|
muc::room::{JoinRoomSettings, LeaveRoomSettings},
|
||||||
parsers::{
|
parsers::{
|
||||||
bookmarks2, ns,
|
bookmarks2, ns,
|
||||||
pubsub::{event::PubSubEvent, pubsub::PubSub},
|
pubsub::{event::PubSubEvent, pubsub::PubSub},
|
||||||
},
|
},
|
||||||
|
Agent, Event, RoomNick,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[cfg(feature = "avatars")]
|
#[cfg(feature = "avatars")]
|
||||||
pub(crate) mod avatar;
|
pub(crate) mod avatar;
|
||||||
|
|
||||||
|
|
@ -56,7 +54,7 @@ pub(crate) async fn handle_event(
|
||||||
agent
|
agent
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
room: jid,
|
room: jid,
|
||||||
nick: conference.nick,
|
nick: conference.nick.map(RoomNick::new),
|
||||||
password: conference.password,
|
password: conference.password,
|
||||||
status: None,
|
status: None,
|
||||||
})
|
})
|
||||||
|
|
@ -139,7 +137,7 @@ pub(crate) async fn handle_iq_result(
|
||||||
agent
|
agent
|
||||||
.join_room(JoinRoomSettings {
|
.join_room(JoinRoomSettings {
|
||||||
room: jid,
|
room: jid,
|
||||||
nick: conference.nick,
|
nick: conference.nick.map(RoomNick::new),
|
||||||
password: conference.password,
|
password: conference.password,
|
||||||
status: None,
|
status: None,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue