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 env_logger;
 8use std::env::args;
 9use std::str::FromStr;
10use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
11use xmpp_parsers::{message::MessageType, BareJid, Jid};
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 => {
43                    println!("Disconnected");
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) => {
56                    println!("Message from {}: {}", jid, 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::Bare(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) => {
86                    println!("Message in room {} from {}: {}", jid, nick, body.0);
87                }
88                Event::AvatarRetrieved(jid, path) => {
89                    println!("Received avatar for {} in {}.", jid, path);
90                }
91                _ => (),
92            }
93        }
94    }
95
96    Ok(())
97}