commit 4f8d1cb56ca5c72c67dd919b5e46f666ec43be6b
parent 4df78cdda4d55677081aed20664067a0becbf086
Author: lash <dev@holbrook.no>
Date: Wed, 12 Oct 2022 12:56:18 +0000
Allow email header fields values from args
Diffstat:
M | Cargo.toml | | | 2 | ++ |
M | src/main.rs | | | 71 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
2 files changed, 66 insertions(+), 7 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -2,6 +2,7 @@
name = "gpgmime"
version = "0.0.1"
edition = "2021"
+authors = ["Louis Holbrook <dev@holbrook.no> (https://holbrook.no)"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -9,3 +10,4 @@ edition = "2021"
sequoia-openpgp = "1.10.0"
mail-builder = "0.2.2"
base64 = "0.13.0"
+clap = "2.34.0"
diff --git a/src/main.rs b/src/main.rs
@@ -2,6 +2,7 @@ use std::io::Write;
use std::io::Read;
use std::fs::read;
use std::env::args;
+use std::path::Path;
use sequoia_openpgp::parse::Parse;
use sequoia_openpgp::Message;
@@ -10,12 +11,67 @@ use sequoia_openpgp::armor::Writer as ArmorWriter;
use sequoia_openpgp::armor::Kind;
use mail_builder::MessageBuilder;
use mail_builder::mime::MimePart;
+use clap::App;
+use clap::Arg;
+use clap::ArgMatches;
+
+struct Settings {
+ to: String,
+ from: String,
+ subject: String,
+ path: String,
+}
+
+impl Settings {
+ fn from_args() -> Settings {
+ let mut o = App::new("gpgmime");
+ o = o.version(env!("CARGO_PKG_VERSION"));
+ o = o.author(env!("CARGO_PKG_AUTHORS"));
+ o = o.arg(
+ Arg::with_name("to")
+ .long("to")
+ .short("t")
+ .value_name("Email recipient")
+ .takes_value(true)
+ .required(true)
+ );
+ o = o.arg(
+ Arg::with_name("from")
+ .long("from")
+ .short("f")
+ .value_name("Email sender")
+ .takes_value(true)
+ .required(true)
+ );
+ o = o.arg(
+ Arg::with_name("subject")
+ .long("subject")
+ .short("s")
+ .value_name("Email subject")
+ .takes_value(true)
+ .required(true)
+ );
+ o = o.arg(
+ Arg::with_name("PATH")
+ .help("Path to PGP message")
+ .required(true)
+ );
+ let arg = o.get_matches();
+ let settings = Settings{
+ to: arg.value_of("to").unwrap().to_string(),
+ from: arg.value_of("from").unwrap().to_string(),
+ subject: arg.value_of("subject").unwrap().to_string(),
+ path: arg.value_of("PATH").unwrap().to_string(),
+ };
+ settings
+ }
+}
+
fn main() {
- let mut argv = args();
- argv.next();
- let fp_s = argv.next().unwrap();
- let msg = Message::from_file(fp_s).unwrap();
+ let settings = Settings::from_args();
+
+ let msg = Message::from_file(settings.path).unwrap();
let mut msg_w = ArmorWriter::new(Vec::new(), Kind::Message).unwrap();
msg_w.write_all(msg.to_vec().unwrap().as_ref());
@@ -41,10 +97,11 @@ fn main() {
)
.inline();
+ let subject: &str = settings.subject.as_ref();
let eml = MessageBuilder::new()
- .from(("Forro contact form", "no-reply@holbrook.no"))
- .to(("Louis Holbrook", "l@holbrook.no"))
- .subject("Rust pgp enc message bridge")
+ .from((settings.from.as_ref(), settings.from.as_ref()))
+ .to((settings.to.as_ref(), settings.to.as_ref()))
+ .subject(subject)
.body(enc_envelope);
println!("{}", eml.write_to_string().unwrap());