2019-11-10 17:33:06 +00:00
|
|
|
use std::{env, process::exit};
|
|
|
|
use url::Url;
|
2019-10-20 11:56:46 +00:00
|
|
|
|
2020-02-21 16:01:34 +00:00
|
|
|
use matrix_sdk::{
|
2021-01-04 15:23:44 +00:00
|
|
|
self, async_trait,
|
2020-06-20 21:18:20 +00:00
|
|
|
events::{
|
2021-03-01 17:30:20 +00:00
|
|
|
room::message::{MessageEventContent, MessageType, TextMessageEventContent},
|
2020-07-18 12:37:43 +00:00
|
|
|
SyncMessageEvent,
|
2020-06-20 21:18:20 +00:00
|
|
|
},
|
2021-03-17 10:53:18 +00:00
|
|
|
room::Room,
|
|
|
|
Client, EventHandler, SyncSettings,
|
2019-10-20 11:56:46 +00:00
|
|
|
};
|
|
|
|
|
2020-03-31 23:34:11 +00:00
|
|
|
struct EventCallback;
|
|
|
|
|
2020-06-17 16:52:53 +00:00
|
|
|
#[async_trait]
|
2021-02-13 10:01:31 +00:00
|
|
|
impl EventHandler for EventCallback {
|
2021-03-17 10:53:18 +00:00
|
|
|
async fn on_room_message(&self, room: Room, event: &SyncMessageEvent<MessageEventContent>) {
|
|
|
|
if let Room::Joined(room) = room {
|
2020-07-18 12:37:43 +00:00
|
|
|
if let SyncMessageEvent {
|
2021-03-01 17:30:20 +00:00
|
|
|
content:
|
|
|
|
MessageEventContent {
|
|
|
|
msgtype: MessageType::Text(TextMessageEventContent { body: msg_body, .. }),
|
|
|
|
..
|
|
|
|
},
|
2020-05-06 23:45:27 +00:00
|
|
|
sender,
|
|
|
|
..
|
|
|
|
} = event
|
|
|
|
{
|
2021-01-18 16:31:33 +00:00
|
|
|
let member = room.get_member(&sender).await.unwrap().unwrap();
|
2020-12-19 19:20:39 +00:00
|
|
|
let name = member
|
|
|
|
.display_name()
|
|
|
|
.unwrap_or_else(|| member.user_id().as_str());
|
2020-05-06 23:45:27 +00:00
|
|
|
println!("{}: {}", name, msg_body);
|
|
|
|
}
|
2020-03-31 23:34:11 +00:00
|
|
|
}
|
2019-10-30 22:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 11:56:46 +00:00
|
|
|
async fn login(
|
|
|
|
homeserver_url: String,
|
2020-08-15 01:09:13 +00:00
|
|
|
username: &str,
|
|
|
|
password: &str,
|
2020-02-21 16:01:34 +00:00
|
|
|
) -> Result<(), matrix_sdk::Error> {
|
2020-05-08 10:39:36 +00:00
|
|
|
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
|
2021-01-22 09:01:21 +00:00
|
|
|
let client = Client::new(homeserver_url).unwrap();
|
2019-10-20 11:56:46 +00:00
|
|
|
|
2021-02-13 10:01:31 +00:00
|
|
|
client.set_event_handler(Box::new(EventCallback)).await;
|
2019-10-23 20:47:00 +00:00
|
|
|
|
2020-03-27 08:22:29 +00:00
|
|
|
client
|
2020-08-15 01:09:13 +00:00
|
|
|
.login(username, password, None, Some("rust-sdk"))
|
2020-03-27 08:22:29 +00:00
|
|
|
.await?;
|
2020-10-06 09:37:29 +00:00
|
|
|
client.sync(SyncSettings::new()).await;
|
2019-10-20 11:56:46 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
2020-02-21 16:01:34 +00:00
|
|
|
async fn main() -> Result<(), matrix_sdk::Error> {
|
2020-03-19 12:55:04 +00:00
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
2019-10-20 11:56:46 +00:00
|
|
|
let (homeserver_url, username, password) =
|
|
|
|
match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
|
|
|
|
(Some(a), Some(b), Some(c)) => (a, b, c),
|
|
|
|
_ => {
|
|
|
|
eprintln!(
|
|
|
|
"Usage: {} <homeserver_url> <username> <password>",
|
|
|
|
env::args().next().unwrap()
|
|
|
|
);
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-15 01:09:13 +00:00
|
|
|
login(homeserver_url, &username, &password).await
|
2019-10-20 11:56:46 +00:00
|
|
|
}
|