mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 21:37:55 +08:00
add basic support of xdg-email (#162)
This commit is contained in:
@@ -7,6 +7,7 @@ use std::{
|
||||
io::{self, BufRead},
|
||||
ops::Deref,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
ctx::Ctx,
|
||||
@@ -594,3 +595,47 @@ fn msg_matches_save(ctx: &Ctx, matches: &clap::ArgMatches) -> Result<bool> {
|
||||
imap_conn.logout();
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn msg_matches_mailto(ctx: &Ctx, url: &Url) -> Result<()> {
|
||||
debug!("mailto command matched");
|
||||
|
||||
let mut imap_conn = ImapConnector::new(&ctx.account)?;
|
||||
let tpl = Tpl::mailto(&ctx, &url);
|
||||
let content = input::open_editor_with_tpl(tpl.to_string().as_bytes())?;
|
||||
let mut msg = Msg::from(content);
|
||||
|
||||
loop {
|
||||
match input::post_edit_choice() {
|
||||
Ok(choice) => match choice {
|
||||
input::PostEditChoice::Send => {
|
||||
debug!("sending message…");
|
||||
let msg = msg.to_sendable_msg()?;
|
||||
smtp::send(&ctx.account, &msg)?;
|
||||
imap_conn.append_msg("Sent", &msg.formatted(), vec![Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
ctx.output.print("Message successfully sent");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Edit => {
|
||||
let content = input::open_editor_with_draft()?;
|
||||
msg = Msg::from(content);
|
||||
}
|
||||
input::PostEditChoice::LocalDraft => break,
|
||||
input::PostEditChoice::RemoteDraft => {
|
||||
debug!("saving to draft…");
|
||||
imap_conn.append_msg("Drafts", &msg.to_vec()?, vec![Flag::Seen])?;
|
||||
input::remove_draft()?;
|
||||
ctx.output.print("Message successfully saved to Drafts");
|
||||
break;
|
||||
}
|
||||
input::PostEditChoice::Discard => {
|
||||
input::remove_draft()?;
|
||||
break;
|
||||
}
|
||||
},
|
||||
Err(err) => error!("{}", err),
|
||||
}
|
||||
}
|
||||
imap_conn.logout();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+47
-1
@@ -1,7 +1,8 @@
|
||||
use error_chain::error_chain;
|
||||
use mailparse::{self, MailHeaderMap};
|
||||
use serde::Serialize;
|
||||
use std::{collections::HashMap, fmt};
|
||||
use std::{borrow::Cow, collections::HashMap, fmt};
|
||||
use url::Url;
|
||||
|
||||
use crate::{ctx::Ctx, msg::model::Msg};
|
||||
|
||||
@@ -187,6 +188,51 @@ impl Tpl {
|
||||
tpl
|
||||
}
|
||||
|
||||
pub fn mailto(ctx: &Ctx, url: &Url) -> Self {
|
||||
let mut headers = HashMap::new();
|
||||
|
||||
let mut cc = Vec::new();
|
||||
let mut bcc = Vec::new();
|
||||
let mut subject = Cow::default();
|
||||
let mut body = Cow::default();
|
||||
|
||||
for (key, val) in url.query_pairs() {
|
||||
match key.as_bytes() {
|
||||
b"cc" => {
|
||||
cc.push(val);
|
||||
}
|
||||
b"bcc" => {
|
||||
bcc.push(val);
|
||||
}
|
||||
b"subject" => {
|
||||
subject = val;
|
||||
}
|
||||
b"body" => {
|
||||
body = val;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
headers.insert(String::from("To"), url.path().to_string());
|
||||
headers.insert(String::from("Subject"), subject.into());
|
||||
if !cc.is_empty() {
|
||||
headers.insert(String::from("Cc"), cc.join(", "));
|
||||
}
|
||||
if !bcc.is_empty() {
|
||||
headers.insert(String::from("Bcc"), cc.join(", "));
|
||||
}
|
||||
|
||||
let mut tpl = Self {
|
||||
headers,
|
||||
body: Some(body.into()),
|
||||
signature: ctx.config.signature(&ctx.account),
|
||||
raw: String::new(),
|
||||
};
|
||||
tpl.raw = tpl.to_string();
|
||||
tpl
|
||||
}
|
||||
|
||||
pub fn header<K: ToString, V: ToString>(&mut self, key: K, val: V) -> &Self {
|
||||
self.headers.insert(key.to_string(), val.to_string());
|
||||
self
|
||||
|
||||
Reference in New Issue
Block a user