modified some stuff
This commit is contained in:
parent
e09e97b93c
commit
d1f1159c20
48
src/main.rs
48
src/main.rs
|
@ -1,40 +1,48 @@
|
||||||
use std::fs;
|
use std::{fs, thread};
|
||||||
use tokio_xmpp::SimpleClient;
|
use tokio_xmpp::SimpleClient;
|
||||||
use xmpp_parsers::message::{Body, Message, MessageType};
|
use xmpp_parsers::message::{Body, Message};
|
||||||
use xmpp_parsers::{Element, Jid};
|
use xmpp_parsers::{Element, Jid};
|
||||||
|
|
||||||
const CREDS: &str = "creds";
|
const CREDS: &str = "creds";
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let creds = fs::read_to_string(CREDS)
|
let creds = fs::read_to_string(CREDS).expect("Should have been able to read the file");
|
||||||
.expect("Should have been able to read the file");
|
|
||||||
let mut creds = creds.lines();
|
let mut creds = creds.lines();
|
||||||
let (jid, password) = (creds.next().expect("No JID"), creds.next().expect("No Pass"));
|
let (jid, password) = (
|
||||||
let recipients = vec!["suguivy@fai.st", "bizcochito@fai.st", "bizcochito@cronut.cafe"];
|
creds.next().expect("No JID"),
|
||||||
|
creds.next().expect("No Pass"),
|
||||||
|
);
|
||||||
|
let recipients = vec!["suguivy@fai.st"];
|
||||||
|
|
||||||
xmp(jid, password, "Hi from the main", recipients).await;
|
xmp(
|
||||||
|
jid,
|
||||||
|
password,
|
||||||
|
format!("Hi from the {}", thread::current().name().unwrap()).as_str(),
|
||||||
|
recipients,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn xmp(jid: &str, password: &str, data: &str, recipients: Vec<&str>) {
|
async fn xmp(jid: &str, password: &str, data: &str, recipients: Vec<&str>) {
|
||||||
let data = data.trim();
|
let data = data.trim();
|
||||||
if data.is_empty() {/* don't send empty stanzas*/ return;}
|
if data.is_empty() {return;} // don't send empty stanzas
|
||||||
|
let mut client = SimpleClient::new(jid, password)
|
||||||
let mut client = SimpleClient::new(jid, password).await.expect("could not connect to xmpp server");
|
.await
|
||||||
for recipient in recipients{
|
.expect("could not connect to xmpp server");
|
||||||
let recipient = recipient.parse::<Jid>().unwrap();
|
for recipient in recipients {
|
||||||
let reply = make_reply(recipient.clone(), &data, false);
|
let recipient = recipient.parse::<Jid>().unwrap();
|
||||||
client.send_stanza(reply).await.expect("sending message failed");
|
let reply = make_reply(recipient.clone(), &data);
|
||||||
}
|
client
|
||||||
|
.send_stanza(reply)
|
||||||
|
.await
|
||||||
|
.expect("sending message failed");
|
||||||
|
}
|
||||||
// Close SimpleClient connection
|
// Close SimpleClient connection
|
||||||
client.end().await.ok(); // ignore errors here, I guess
|
client.end().await.ok(); // ignore errors here, I guess
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_reply(to: Jid, body: &str, groupchat: bool) -> Element {
|
fn make_reply(to: Jid, body: &str) -> Element {
|
||||||
let mut message = Message::new(Some(to));
|
let mut message = Message::new(Some(to));
|
||||||
if groupchat {
|
|
||||||
message.type_ = MessageType::Groupchat;
|
|
||||||
}
|
|
||||||
message.bodies.insert(String::new(), Body(body.to_owned()));
|
message.bodies.insert(String::new(), Body(body.to_owned()));
|
||||||
message.into()
|
message.into()
|
||||||
}
|
}
|
Loading…
Reference in New Issue