2020-12-09 16:18:23 +00:00
|
|
|
use std::{
|
|
|
|
env, io,
|
|
|
|
process::exit,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
2020-07-31 13:34:46 +00:00
|
|
|
use url::Url;
|
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
use matrix_sdk::{
|
2020-12-09 16:18:23 +00:00
|
|
|
self,
|
|
|
|
events::{
|
|
|
|
room::message::MessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent,
|
|
|
|
},
|
|
|
|
identifiers::UserId,
|
|
|
|
Client, ClientConfig, LoopCtrl, Sas, SyncSettings,
|
2020-08-19 12:50:35 +00:00
|
|
|
};
|
2020-08-03 12:51:45 +00:00
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
async fn wait_for_confirmation(client: Client, sas: Sas) {
|
|
|
|
println!("Does the emoji match: {:?}", sas.emoji());
|
2020-08-03 12:51:45 +00:00
|
|
|
|
|
|
|
let mut input = String::new();
|
|
|
|
io::stdin()
|
|
|
|
.read_line(&mut input)
|
|
|
|
.expect("error: unable to read user input");
|
|
|
|
|
|
|
|
match input.trim().to_lowercase().as_ref() {
|
|
|
|
"yes" | "true" | "ok" => {
|
|
|
|
sas.confirm().await.unwrap();
|
|
|
|
|
|
|
|
if sas.is_done() {
|
2020-08-19 12:50:35 +00:00
|
|
|
print_result(&sas);
|
|
|
|
print_devices(sas.other_device().user_id(), &client).await;
|
2020-08-03 12:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => sas.cancel().await.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
fn print_result(sas: &Sas) {
|
2020-08-03 12:51:45 +00:00
|
|
|
let device = sas.other_device();
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Successfully verified device {} {} {:?}",
|
|
|
|
device.user_id(),
|
|
|
|
device.device_id(),
|
2020-09-24 10:45:23 +00:00
|
|
|
device.local_trust_state()
|
2020-08-03 12:51:45 +00:00
|
|
|
);
|
|
|
|
}
|
2020-07-31 13:34:46 +00:00
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
async fn print_devices(user_id: &UserId, client: &Client) {
|
|
|
|
println!("Devices of user {}", user_id);
|
|
|
|
|
|
|
|
for device in client.get_user_devices(user_id).await.unwrap().devices() {
|
|
|
|
println!(
|
|
|
|
" {:<10} {:<30} {:<}",
|
|
|
|
device.device_id(),
|
|
|
|
device.display_name().as_deref().unwrap_or_default(),
|
|
|
|
device.is_trusted()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 13:34:46 +00:00
|
|
|
async fn login(
|
|
|
|
homeserver_url: String,
|
2020-08-15 01:09:13 +00:00
|
|
|
username: &str,
|
|
|
|
password: &str,
|
2020-07-31 13:34:46 +00:00
|
|
|
) -> Result<(), matrix_sdk::Error> {
|
2020-08-19 12:50:35 +00:00
|
|
|
let client_config = ClientConfig::new()
|
|
|
|
.disable_ssl_verification()
|
|
|
|
.proxy("http://localhost:8080")
|
|
|
|
.unwrap();
|
2020-07-31 13:34:46 +00:00
|
|
|
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
|
|
|
|
let client = Client::new_with_config(homeserver_url, client_config).unwrap();
|
|
|
|
|
|
|
|
client
|
2020-08-15 01:09:13 +00:00
|
|
|
.login(username, password, None, Some("rust-sdk"))
|
2020-07-31 13:34:46 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let client_ref = &client;
|
2020-12-09 16:18:23 +00:00
|
|
|
let initial_sync = Arc::new(AtomicBool::from(true));
|
|
|
|
let initial_ref = &initial_sync;
|
2020-07-31 13:34:46 +00:00
|
|
|
|
|
|
|
client
|
2020-10-06 09:37:29 +00:00
|
|
|
.sync_with_callback(SyncSettings::new(), |response| async move {
|
2020-07-31 13:34:46 +00:00
|
|
|
let client = &client_ref;
|
2020-12-09 16:18:23 +00:00
|
|
|
let initial = &initial_ref;
|
2020-07-31 13:34:46 +00:00
|
|
|
|
|
|
|
for event in &response.to_device.events {
|
|
|
|
let e = event
|
|
|
|
.deserialize()
|
|
|
|
.expect("Can't deserialize to-device event");
|
|
|
|
|
|
|
|
match e {
|
|
|
|
AnyToDeviceEvent::KeyVerificationStart(e) => {
|
|
|
|
let sas = client
|
|
|
|
.get_verification(&e.content.transaction_id)
|
|
|
|
.await
|
|
|
|
.expect("Sas object wasn't created");
|
2020-08-19 12:50:35 +00:00
|
|
|
println!(
|
|
|
|
"Starting verification with {} {}",
|
|
|
|
&sas.other_device().user_id(),
|
|
|
|
&sas.other_device().device_id()
|
|
|
|
);
|
|
|
|
print_devices(&e.sender, &client).await;
|
2020-07-31 13:34:46 +00:00
|
|
|
sas.accept().await.unwrap();
|
|
|
|
}
|
2020-08-03 12:51:45 +00:00
|
|
|
|
2020-07-31 13:34:46 +00:00
|
|
|
AnyToDeviceEvent::KeyVerificationKey(e) => {
|
|
|
|
let sas = client
|
|
|
|
.get_verification(&e.content.transaction_id)
|
|
|
|
.await
|
|
|
|
.expect("Sas object wasn't created");
|
|
|
|
|
2020-08-19 12:50:35 +00:00
|
|
|
tokio::spawn(wait_for_confirmation((*client).clone(), sas));
|
2020-08-03 12:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnyToDeviceEvent::KeyVerificationMac(e) => {
|
|
|
|
let sas = client
|
|
|
|
.get_verification(&e.content.transaction_id)
|
|
|
|
.await
|
|
|
|
.expect("Sas object wasn't created");
|
2020-07-31 13:34:46 +00:00
|
|
|
|
2020-08-03 12:51:45 +00:00
|
|
|
if sas.is_done() {
|
2020-08-19 12:50:35 +00:00
|
|
|
print_result(&sas);
|
|
|
|
print_devices(&e.sender, &client).await;
|
2020-07-31 13:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 12:51:45 +00:00
|
|
|
|
2020-07-31 13:34:46 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 09:37:29 +00:00
|
|
|
|
2020-12-09 16:18:23 +00:00
|
|
|
if !initial.load(Ordering::SeqCst) {
|
|
|
|
for (_room_id, room_info) in response.rooms.join {
|
|
|
|
for event in room_info.timeline.events {
|
2020-12-17 11:15:11 +00:00
|
|
|
if let AnySyncRoomEvent::Message(event) = event.deserialize().unwrap() {
|
|
|
|
match event {
|
|
|
|
AnySyncMessageEvent::RoomMessage(m) => {
|
|
|
|
if let MessageEventContent::VerificationRequest(_) = &m.content
|
|
|
|
{
|
|
|
|
let request = client
|
|
|
|
.get_verification_request(&m.event_id)
|
|
|
|
.await
|
|
|
|
.expect("Request object wasn't created");
|
|
|
|
|
|
|
|
request
|
|
|
|
.accept()
|
|
|
|
.await
|
|
|
|
.expect("Can't accept verification request");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AnySyncMessageEvent::KeyVerificationKey(e) => {
|
|
|
|
let sas = client
|
|
|
|
.get_verification(&e.content.relation.event_id.as_str())
|
|
|
|
.await
|
|
|
|
.expect("Sas object wasn't created");
|
|
|
|
|
|
|
|
tokio::spawn(wait_for_confirmation((*client).clone(), sas));
|
|
|
|
}
|
|
|
|
AnySyncMessageEvent::KeyVerificationMac(e) => {
|
|
|
|
let sas = client
|
|
|
|
.get_verification(&e.content.relation.event_id.as_str())
|
|
|
|
.await
|
|
|
|
.expect("Sas object wasn't created");
|
|
|
|
|
|
|
|
if sas.is_done() {
|
|
|
|
print_result(&sas);
|
|
|
|
print_devices(&e.sender, &client).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-12-09 16:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initial.store(false, Ordering::SeqCst);
|
|
|
|
|
2020-10-06 13:04:43 +00:00
|
|
|
LoopCtrl::Continue
|
2020-07-31 13:34:46 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), matrix_sdk::Error> {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
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
|
2020-07-31 13:34:46 +00:00
|
|
|
}
|