Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a) encryption, b) emoji verification, c) updated to matrix-sdk 0.6 #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,278 changes: 1,033 additions & 245 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "matrix-send"
version = "0.2.1"
version = "0.2.2"
authors = ["Tilo Spannagel <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -25,18 +25,24 @@ serde = { version = "1.0", features = ["derive"] }
url = { version = "2.3", features = ["serde"] }
serde_json = "1.0"
directories = "4.0"
tokio = { version = "1.21", default-features = false, features = [
# ruma-client-api 0.15.2 had a bug: ruma-client-api = { version = ">=0.15.0, <0.15.2" }
tokio = { version = "1.20", default-features = false, features = [
"rt-multi-thread",
"macros",
] }
clap = { version = "3.2", features = ["derive"] }
clap = { version = "*", features = ["derive"] }
atty = "0.2"
matrix-sdk = { version = "0.5", default-features = false, features = [
matrix-sdk = { version = "0.6", default-features = false, features = [
"rustls-tls",
"markdown",
"anyhow",
"e2e-encryption",
"sled",
] }
mime = "0.3"
mime_guess = "2.0"
tracing-subscriber = "0.3.15"
tracing = "*"

[profile.release]
strip = "symbols"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ FLAGS:
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
login Login to Matrix Account
verify Emoji verify device of Matrix Account
logout Logout from Matrix Account
room Room Subcommands
```
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2021"
32 changes: 22 additions & 10 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs;
use std::io::{self, Write};

use crate::{dir::Directories, matrix::MatrixClient, Error, Result};

Expand All @@ -16,6 +16,9 @@ pub(crate) enum Command {
/// Login to Matrix Account
Login(LoginCommand),

/// Emoji verify device of Matrix Account
Verify(VerifyCommand),

/// Logout from Matrix Account
Logout(LogoutCommand),
}
Expand All @@ -24,6 +27,7 @@ impl Command {
pub(super) async fn run(self, client: Result<MatrixClient>, dirs: &Directories) -> Result {
match self {
Self::Login(command) => command.run(client, dirs).await,
Self::Verify(command) => command.run(client, dirs).await,
Self::Logout(command) => command.run(client, dirs).await,
Self::LoggedInCommands(command) => {
let client = client?;
Expand Down Expand Up @@ -62,26 +66,34 @@ impl LoginCommand {
}

fn user_input(message: &'static str) -> Result<String> {
println!("{}", message);
print!("{} ", message);
io::stdout().flush().unwrap();
let mut line = String::new();
std::io::stdin().read_line(&mut line)?;
Ok(line)
}
}

#[derive(Debug, Parser)]
pub(crate) struct LogoutCommand {}
pub(crate) struct VerifyCommand {}

impl LogoutCommand {
async fn run(self, client: Result<MatrixClient>, dirs: &Directories) -> Result {
impl VerifyCommand {
async fn run(self, client: Result<MatrixClient>, _dirs: &Directories) -> Result {
if let Ok(client) = client {
client.logout().await?;
client.verify().await?;
Ok(())
} else {
if dirs.session_file.exists() {
fs::remove_file(&dirs.session_file)?;
}
client?;
Error::custom("Not logged in")
}
}
}

#[derive(Debug, Parser)]
pub(crate) struct LogoutCommand {}

impl LogoutCommand {
async fn run(self, client: Result<MatrixClient>, dirs: &Directories) -> Result {
MatrixClient::logout(client, dirs).await?;
Ok(())
}
}
11 changes: 6 additions & 5 deletions src/command/loggedin/room.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::borrow::Cow;
use std::fs::{self, File};
use std::fs::{self};
use std::io::{self, Read};
use std::path::PathBuf;

use crate::{matrix::MatrixClient, Error, Result};

use atty::Stream;

use clap::{ArgEnum, ArgGroup, Parser};
use clap::{ArgGroup, Parser, ValueEnum};

use matrix_sdk::{
attachment::AttachmentConfig,
Expand Down Expand Up @@ -186,11 +186,11 @@ impl SendCommand {
#[derive(Debug, Parser)]
pub(crate) struct ListCommand {
/// Kind
#[clap(arg_enum, default_value = "joined")]
#[clap(value_enum, default_value = "joined")]
kind: Vec<Kind>,
}

#[derive(Clone, ArgEnum, Debug)]
#[derive(Clone, ValueEnum, Debug)]
enum Kind {
All,
Joined,
Expand Down Expand Up @@ -255,6 +255,7 @@ pub(crate) struct SendFileCommand {

impl SendFileCommand {
async fn run(self, client: MatrixClient) -> Result {
let data = fs::read(&self.file)?;
client
.joined_room(&self.room)?
.send_attachment(
Expand All @@ -267,7 +268,7 @@ impl SendFileCommand {
self.mime.as_ref().unwrap_or(
&mime_guess::from_path(&self.file).first_or(mime::APPLICATION_OCTET_STREAM),
),
&mut File::open(&self.file)?,
&data,
AttachmentConfig::new(),
)
.await?;
Expand Down
16 changes: 15 additions & 1 deletion src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ use crate::{Error, Result};

use directories::ProjectDirs;

use tracing::info;

const SESSION_FILE: &str = "session.json";
const SLED_STORE_DIR: &str = "sledstore";

#[derive(Clone)]
pub(crate) struct Directories {
pub(crate) session_file: PathBuf,
pub(crate) sled_store_dir: PathBuf,
}

impl Directories {
Expand All @@ -17,8 +22,17 @@ impl Directories {
ProjectDirs::from_path(PathBuf::from(crate::APP_NAME)).ok_or(Error::NoNomeDirectory)?;

fs::create_dir_all(dirs.data_dir())?;
let sf = dirs.data_dir().join(SESSION_FILE);
let ssd = dirs.data_dir().join(SLED_STORE_DIR);
info!(
"Data will be put into project directory {}.",
dirs.data_dir().display()
);
info!("Session file with access token is {}.", sf.display());
info!("Sled store directory for encryption is {}.", ssd.display());
Ok(Directories {
session_file: dirs.data_dir().join(SESSION_FILE),
session_file: sf,
sled_store_dir: ssd,
})
}
}
23 changes: 22 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
//! # Welcome to matrix-send-rs
//!
//! Requirements: you must install openssl-devel in your operating system otherwise
//! cargo will not build. There are different names for this package depending on the
//! OS. E.g. `libssl-dev`, `openssl-devel`, etc.
//!
//! Files are created and installed in local data directory $XDG_DATA_HOME
//! according to XDG standard.
//! E.g. on Linux under /home/someuser/.local/share/matrix-send/
//!

use crate::dir::Directories;
use crate::matrix::MatrixClient;

Expand All @@ -9,6 +20,9 @@ mod command;
mod dir;
mod matrix;

use tracing::{debug, enabled, Level};

// records an event outside of any span context:
const APP_NAME: &str = env!("CARGO_PKG_NAME");

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -57,11 +71,18 @@ pub(crate) type Result<T = ()> = std::result::Result<T, Error>;

#[tokio::main]
async fn main() -> Result {
// set log level e.g. via RUST_LOG=matrix-send=DEBUG cargo run
tracing_subscriber::fmt::init();
if enabled!(Level::TRACE) {
debug!("Log level is set to TRACE.");
} else if enabled!(Level::DEBUG) {
debug!("Log level is set to DEBUG.");
}
let Opt { command } = Opt::parse();

let dirs = Directories::new()?;

let client = MatrixClient::load(&dirs).await;
let client = MatrixClient::load(&dirs).await; // re-login

command.run(client, &dirs).await
}
Loading