hello_bot.rs

  1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  2//
  3// This Source Code Form is subject to the terms of the Mozilla Public
  4// License, v. 2.0. If a copy of the MPL was not distributed with this
  5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6
  7use std::env::args;
  8use std::str::FromStr;
  9use tokio_xmpp::jid::{BareJid, Jid};
 10use tokio_xmpp::parsers::message::MessageType;
 11use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
 12
 13#[tokio::main]
 14async fn main() -> Result<(), Option<()>> {
 15    env_logger::init();
 16
 17    let args: Vec<String> = args().collect();
 18    if args.len() != 3 {
 19        println!("Usage: {} <jid> <password>", args[0]);
 20        return Err(None);
 21    }
 22
 23    let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
 24    let password = &args[2];
 25
 26    // Client instance
 27    let mut client = ClientBuilder::new(jid, password)
 28        .set_client(ClientType::Bot, "xmpp-rs")
 29        .set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
 30        .set_default_nick("bot")
 31        .enable_feature(ClientFeature::Avatars)
 32        .enable_feature(ClientFeature::ContactList)
 33        .enable_feature(ClientFeature::JoinRooms)
 34        .build();
 35
 36    while let Some(events) = client.wait_for_events().await {
 37        for event in events {
 38            match event {
 39                Event::Online => {
 40                    println!("Online.");
 41                }
 42                Event::Disconnected(e) => {
 43                    println!("Disconnected because of {}.", e);
 44                    return Err(None);
 45                }
 46                Event::ContactAdded(contact) => {
 47                    println!("Contact {} added.", contact.jid);
 48                }
 49                Event::ContactRemoved(contact) => {
 50                    println!("Contact {} removed.", contact.jid);
 51                }
 52                Event::ContactChanged(contact) => {
 53                    println!("Contact {} changed.", contact.jid);
 54                }
 55                Event::ChatMessage(_id, jid, body, time_info) => {
 56                    println!("Message from {} at {}: {}", jid, time_info.received, body.0);
 57                }
 58                Event::JoinRoom(jid, conference) => {
 59                    println!("Joining room {} ({:?})…", jid, conference.name);
 60                    client
 61                        .join_room(
 62                            jid,
 63                            conference.nick,
 64                            conference.password,
 65                            "en",
 66                            "Yet another bot!",
 67                        )
 68                        .await;
 69                }
 70                Event::LeaveRoom(jid) => {
 71                    println!("Leaving room {}", jid);
 72                }
 73                Event::LeaveAllRooms => {
 74                    println!("Leaving all rooms…");
 75                }
 76                Event::RoomJoined(jid) => {
 77                    println!("Joined room {}.", jid);
 78                    client
 79                        .send_message(Jid::from(jid), MessageType::Groupchat, "en", "Hello world!")
 80                        .await;
 81                }
 82                Event::RoomLeft(jid) => {
 83                    println!("Left room {}.", jid);
 84                }
 85                Event::RoomMessage(_id, jid, nick, body, time_info) => {
 86                    println!(
 87                        "Message in room {} from {} at {}: {}",
 88                        jid, nick, time_info.received, body.0
 89                    );
 90                }
 91                Event::AvatarRetrieved(jid, path) => {
 92                    println!("Received avatar for {} in {}.", jid, path);
 93                }
 94                _ => (),
 95            }
 96        }
 97    }
 98
 99    Ok(())
100}