mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
rewrite the messaging plugin
This commit is contained in:
parent
1b2e4aca21
commit
6c073aca3c
2 changed files with 100 additions and 22 deletions
|
@ -7,7 +7,7 @@ use xmpp::plugins::unhandled_iq::UnhandledIqPlugin;
|
||||||
use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent};
|
use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent};
|
||||||
use xmpp::plugins::presence::{PresencePlugin, Type};
|
use xmpp::plugins::presence::{PresencePlugin, Type};
|
||||||
use xmpp::plugins::ibb::IbbPlugin;
|
use xmpp::plugins::ibb::IbbPlugin;
|
||||||
use xmpp::plugins::ping::{PingPlugin, PingEvent};
|
use xmpp::plugins::ping::PingPlugin;
|
||||||
use xmpp::event::{Priority, Propagation};
|
use xmpp::event::{Priority, Propagation};
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
|
@ -1,18 +1,55 @@
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use plugin::PluginProxy;
|
use plugin::PluginProxy;
|
||||||
use event::{Event, ReceiveElement, Priority, Propagation};
|
use event::{Event, Priority, Propagation};
|
||||||
use minidom::Element;
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
use ns;
|
|
||||||
|
|
||||||
|
use plugins::stanza::Message;
|
||||||
|
use xmpp_parsers::message::{MessagePayload, MessageType};
|
||||||
|
use xmpp_parsers::chatstates::ChatState;
|
||||||
|
use xmpp_parsers::receipts::Receipt;
|
||||||
|
use xmpp_parsers::stanza_id::StanzaId;
|
||||||
|
|
||||||
|
// TODO: use the id (maybe even stanza-id) to identify every message.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MessageEvent {
|
pub struct MessageEvent {
|
||||||
pub from: Jid,
|
pub from: Jid,
|
||||||
pub to: Jid,
|
|
||||||
pub body: String,
|
pub body: String,
|
||||||
|
pub subject: Option<String>,
|
||||||
|
pub thread: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ChatStateEvent {
|
||||||
|
pub from: Jid,
|
||||||
|
pub chat_state: ChatState,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ReceiptRequestEvent {
|
||||||
|
pub from: Jid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct ReceiptReceivedEvent {
|
||||||
|
pub from: Jid,
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct StanzaIdEvent {
|
||||||
|
pub from: Jid,
|
||||||
|
pub stanza_id: StanzaId,
|
||||||
|
pub message: Message,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for MessageEvent {}
|
impl Event for MessageEvent {}
|
||||||
|
impl Event for ChatStateEvent {}
|
||||||
|
impl Event for ReceiptRequestEvent {}
|
||||||
|
impl Event for ReceiptReceivedEvent {}
|
||||||
|
impl Event for StanzaIdEvent {}
|
||||||
|
|
||||||
pub struct MessagingPlugin {
|
pub struct MessagingPlugin {
|
||||||
proxy: PluginProxy,
|
proxy: PluginProxy,
|
||||||
|
@ -26,30 +63,71 @@ impl MessagingPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_message(&self, to: &Jid, body: &str) -> Result<(), Error> {
|
pub fn send_message(&self, to: &Jid, body: &str) -> Result<(), Error> {
|
||||||
let mut elem = Element::builder("message")
|
let message = Message {
|
||||||
.attr("type", "chat")
|
from: None,
|
||||||
.attr("to", to.to_string())
|
to: Some(to.clone()),
|
||||||
.build();
|
type_: MessageType::Chat,
|
||||||
elem.append_child(Element::builder("body").append(body).build());
|
// TODO: always use an id.
|
||||||
self.proxy.send(elem);
|
id: None,
|
||||||
|
bodies: {
|
||||||
|
let mut bodies = BTreeMap::new();
|
||||||
|
bodies.insert(String::new(), String::from(body));
|
||||||
|
bodies
|
||||||
|
},
|
||||||
|
subjects: BTreeMap::new(),
|
||||||
|
thread: None,
|
||||||
|
payloads: vec!(),
|
||||||
|
};
|
||||||
|
self.proxy.send(message.into());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_receive_element(&self, evt: &ReceiveElement) -> Propagation {
|
fn handle_message(&self, message: &Message) -> Propagation {
|
||||||
let elem = &evt.0;
|
let from = message.from.clone().unwrap();
|
||||||
if elem.is("message", ns::CLIENT) && elem.attr("type") == Some("chat") {
|
for payload in message.payloads.clone() {
|
||||||
if let Some(body) = elem.get_child("body", ns::CLIENT) {
|
let payload = match MessagePayload::try_from(payload) {
|
||||||
self.proxy.dispatch(MessageEvent { // TODO: safety!!!
|
Ok(payload) => payload,
|
||||||
from: elem.attr("from").unwrap().parse().unwrap(),
|
Err(err) => {
|
||||||
to: elem.attr("to").unwrap().parse().unwrap(),
|
println!("MessagePayload: {:?}", err);
|
||||||
body: body.text(),
|
continue;
|
||||||
});
|
}
|
||||||
|
};
|
||||||
|
match payload {
|
||||||
|
// XEP-0085
|
||||||
|
MessagePayload::ChatState(chat_state) => self.proxy.dispatch(ChatStateEvent {
|
||||||
|
from: from.clone(),
|
||||||
|
chat_state: chat_state,
|
||||||
|
}),
|
||||||
|
// XEP-0184
|
||||||
|
MessagePayload::Receipt(Receipt::Request) => self.proxy.dispatch(ReceiptRequestEvent {
|
||||||
|
from: from.clone(),
|
||||||
|
}),
|
||||||
|
// XEP-0184
|
||||||
|
MessagePayload::Receipt(Receipt::Received(id)) => self.proxy.dispatch(ReceiptReceivedEvent {
|
||||||
|
from: from.clone(),
|
||||||
|
id: id.unwrap(),
|
||||||
|
}),
|
||||||
|
// XEP-0359
|
||||||
|
MessagePayload::StanzaId(stanza_id) => self.proxy.dispatch(StanzaIdEvent {
|
||||||
|
from: from.clone(),
|
||||||
|
stanza_id: stanza_id,
|
||||||
|
message: message.clone(),
|
||||||
|
}),
|
||||||
|
payload => println!("Unhandled payload: {:?}", payload),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Propagation::Continue
|
if message.bodies.contains_key("") {
|
||||||
|
self.proxy.dispatch(MessageEvent {
|
||||||
|
from: from,
|
||||||
|
body: message.bodies[""].clone(),
|
||||||
|
subject: if message.subjects.contains_key("") { Some(message.subjects[""].clone()) } else { None },
|
||||||
|
thread: message.thread.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Propagation::Stop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_plugin!(MessagingPlugin, proxy, [
|
impl_plugin!(MessagingPlugin, proxy, [
|
||||||
(ReceiveElement, Priority::Default) => handle_receive_element,
|
(Message, Priority::Default) => handle_message,
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in a new issue