send_message.rs

 1use std::env::args;
 2use std::io::{stdin, Read};
 3use std::process::exit;
 4use std::str::FromStr;
 5use tokio;
 6use tokio_xmpp::SimpleClient as Client;
 7use xmpp_parsers::message::{Body, Message};
 8use xmpp_parsers::Jid;
 9
10#[tokio::main]
11async fn main() {
12    let args: Vec<String> = args().collect();
13    if args.len() != 4 {
14        println!("Usage: {} <jid> <password> <recipient>", args[0]);
15        exit(1);
16    }
17    // Configuration
18    let jid = &args[1];
19    let password = &args[2];
20    let recipient = Jid::from_str(&args[3]).unwrap();
21
22    // Client instance
23    let mut client = Client::new(jid, password.to_owned()).await.unwrap();
24
25    // Read from stdin
26    println!("Client connected, type message and submit with Ctrl-D");
27    let mut body = String::new();
28    stdin().lock().read_to_string(&mut body).unwrap();
29
30    // Send message
31    let mut message = Message::new(Some(recipient));
32    message.bodies.insert(String::new(), Body(body.to_owned()));
33    client.send_stanza(message).await.unwrap();
34
35    // Close client connection
36    client.end().await.unwrap();
37}