mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-05 17:15:12 +00:00
init imap oauth2 support
This commit is contained in:
commit
e6c9a6e90e
1207
Cargo.lock
generated
1207
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -48,7 +48,7 @@ dialoguer = "0.10.2"
|
||||||
email_address = "0.2.4"
|
email_address = "0.2.4"
|
||||||
env_logger = "0.8"
|
env_logger = "0.8"
|
||||||
erased-serde = "0.3"
|
erased-serde = "0.3"
|
||||||
pimalaya-email = "0.7.1"
|
pimalaya-email = { git = "https://git.sr.ht/~soywod/pimalaya" }
|
||||||
indicatif = "0.17"
|
indicatif = "0.17"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
once_cell = "1.16.0"
|
once_cell = "1.16.0"
|
||||||
|
|
17
flake.lock
17
flake.lock
|
@ -21,6 +21,22 @@
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"flake-compat": {
|
||||||
|
"flake": false,
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1673956053,
|
||||||
|
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
|
||||||
|
"owner": "edolstra",
|
||||||
|
"repo": "flake-compat",
|
||||||
|
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "edolstra",
|
||||||
|
"repo": "flake-compat",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"flake-utils": {
|
"flake-utils": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
|
@ -98,6 +114,7 @@
|
||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"fenix": "fenix",
|
"fenix": "fenix",
|
||||||
|
"flake-compat": "flake-compat",
|
||||||
"flake-utils": "flake-utils",
|
"flake-utils": "flake-utils",
|
||||||
"gitignore": "gitignore",
|
"gitignore": "gitignore",
|
||||||
"naersk": "naersk",
|
"naersk": "naersk",
|
||||||
|
|
|
@ -16,9 +16,13 @@
|
||||||
url = "github:nix-community/naersk";
|
url = "github:nix-community/naersk";
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
};
|
};
|
||||||
|
flake-compat = {
|
||||||
|
url = "github:edolstra/flake-compat";
|
||||||
|
flake = false;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs, flake-utils, gitignore, fenix, naersk }:
|
outputs = { self, nixpkgs, flake-utils, gitignore, fenix, naersk, ... }:
|
||||||
let
|
let
|
||||||
inherit (gitignore.lib) gitignoreSource;
|
inherit (gitignore.lib) gitignoreSource;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
pub mod args;
|
pub mod args;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
mod wizard;
|
pub(crate) mod wizard;
|
||||||
|
|
||||||
pub use config::*;
|
pub use config::*;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use pimalaya_email::{
|
use pimalaya_email::{
|
||||||
folder::sync::Strategy as SyncFoldersStrategy, EmailHooks, EmailSender, EmailTextPlainFormat,
|
folder::sync::Strategy as SyncFoldersStrategy, EmailHooks, EmailSender, EmailTextPlainFormat,
|
||||||
MaildirConfig, SendmailConfig, SmtpConfig,
|
ImapAuthConfig, MaildirConfig, OAuth2ClientSecret, OAuth2Config, OAuth2Method, OAuth2Scopes,
|
||||||
|
SendmailConfig, SmtpConfig,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{collections::HashSet, path::PathBuf};
|
use std::{collections::HashSet, path::PathBuf};
|
||||||
|
@ -46,8 +47,8 @@ pub struct ImapConfigDef {
|
||||||
pub insecure: Option<bool>,
|
pub insecure: Option<bool>,
|
||||||
#[serde(rename = "imap-login")]
|
#[serde(rename = "imap-login")]
|
||||||
pub login: String,
|
pub login: String,
|
||||||
#[serde(rename = "imap-passwd-cmd")]
|
#[serde(rename = "imap-auth", with = "ImapAuthConfigDef")]
|
||||||
pub passwd_cmd: String,
|
pub auth: ImapAuthConfig,
|
||||||
#[serde(rename = "imap-notify-cmd")]
|
#[serde(rename = "imap-notify-cmd")]
|
||||||
pub notify_cmd: Option<String>,
|
pub notify_cmd: Option<String>,
|
||||||
#[serde(rename = "imap-notify-query")]
|
#[serde(rename = "imap-notify-query")]
|
||||||
|
@ -56,6 +57,55 @@ pub struct ImapConfigDef {
|
||||||
pub watch_cmds: Option<Vec<String>>,
|
pub watch_cmds: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
|
#[serde(remote = "ImapAuthConfig", rename_all = "kebab-case")]
|
||||||
|
pub enum ImapAuthConfigDef {
|
||||||
|
#[serde(skip)]
|
||||||
|
None,
|
||||||
|
RawPasswd(String),
|
||||||
|
PasswdCmd(String),
|
||||||
|
#[serde(with = "OAuth2ConfigDef", rename = "oauth2")]
|
||||||
|
OAuth2(OAuth2Config),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
|
#[serde(remote = "OAuth2Config", rename_all = "kebab-case")]
|
||||||
|
pub struct OAuth2ConfigDef {
|
||||||
|
#[serde(with = "OAuth2MethodDef")]
|
||||||
|
pub method: OAuth2Method,
|
||||||
|
pub client_id: String,
|
||||||
|
#[serde(with = "OAuth2ClientSecretDef")]
|
||||||
|
pub client_secret: OAuth2ClientSecret,
|
||||||
|
pub auth_url: String,
|
||||||
|
pub token_url: String,
|
||||||
|
#[serde(flatten, with = "OAuth2ScopesDef")]
|
||||||
|
pub scopes: OAuth2Scopes,
|
||||||
|
#[serde(default)]
|
||||||
|
pub pkce: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
|
#[serde(remote = "OAuth2ClientSecret", rename_all = "kebab-case")]
|
||||||
|
pub enum OAuth2ClientSecretDef {
|
||||||
|
Raw(String),
|
||||||
|
Cmd(String),
|
||||||
|
Keyring,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
|
#[serde(remote = "OAuth2Method", rename_all = "lowercase")]
|
||||||
|
pub enum OAuth2MethodDef {
|
||||||
|
XOAuth2,
|
||||||
|
OAuthBearer,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
|
#[serde(remote = "OAuth2Scopes", rename_all = "kebab-case")]
|
||||||
|
pub enum OAuth2ScopesDef {
|
||||||
|
Scope(String),
|
||||||
|
Scopes(Vec<String>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
|
||||||
#[serde(remote = "MaildirConfig", rename_all = "kebab-case")]
|
#[serde(remote = "MaildirConfig", rename_all = "kebab-case")]
|
||||||
pub struct MaildirConfigDef {
|
pub struct MaildirConfigDef {
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
use super::{SECURITY_PROTOCOLS, THEME};
|
|
||||||
use crate::account::{
|
|
||||||
DeserializedAccountConfig, DeserializedBaseAccountConfig, DeserializedImapAccountConfig,
|
|
||||||
};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use dialoguer::{Input, Select};
|
use dialoguer::{Input, Select};
|
||||||
use pimalaya_email::ImapConfig;
|
use pimalaya_email::ImapConfig;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use crate::account::{
|
||||||
|
DeserializedAccountConfig, DeserializedBaseAccountConfig, DeserializedImapAccountConfig,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{SECURITY_PROTOCOLS, THEME};
|
||||||
|
|
||||||
#[cfg(feature = "imap-backend")]
|
#[cfg(feature = "imap-backend")]
|
||||||
pub(crate) fn configure(base: DeserializedBaseAccountConfig) -> Result<DeserializedAccountConfig> {
|
pub(crate) fn configure(base: DeserializedBaseAccountConfig) -> Result<DeserializedAccountConfig> {
|
||||||
|
@ -46,12 +49,21 @@ pub(crate) fn configure(base: DeserializedBaseAccountConfig) -> Result<Deseriali
|
||||||
.default(base.email.clone())
|
.default(base.email.clone())
|
||||||
.interact()?;
|
.interact()?;
|
||||||
|
|
||||||
backend.passwd_cmd = Input::with_theme(&*THEME)
|
// FIXME: add all variants: password, password command and oauth2
|
||||||
.with_prompt("What shell command should we run to get your password?")
|
// backend.passwd_cmd = Input::with_theme(&*THEME)
|
||||||
.default(format!("pass show {}", &base.email))
|
// .with_prompt("What shell command should we run to get your password?")
|
||||||
.interact()?;
|
// .default(format!("pass show {}", &base.email))
|
||||||
|
// .interact()?;
|
||||||
|
|
||||||
Ok(DeserializedAccountConfig::Imap(
|
Ok(DeserializedAccountConfig::Imap(
|
||||||
DeserializedImapAccountConfig { base, backend },
|
DeserializedImapAccountConfig { base, backend },
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "imap-backend")]
|
||||||
|
pub(crate) fn configure_oauth2_client_secret() -> io::Result<String> {
|
||||||
|
Input::with_theme(&*THEME)
|
||||||
|
.with_prompt("Enter your OAuth 2.0 client secret:")
|
||||||
|
.report(false)
|
||||||
|
.interact()
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#[cfg(feature = "imap-backend")]
|
#[cfg(feature = "imap-backend")]
|
||||||
mod imap;
|
pub(crate) mod imap;
|
||||||
mod maildir;
|
mod maildir;
|
||||||
#[cfg(feature = "notmuch-backend")]
|
#[cfg(feature = "notmuch-backend")]
|
||||||
mod notmuch;
|
mod notmuch;
|
||||||
|
|
|
@ -10,11 +10,14 @@ use crate::{folder, ui::table};
|
||||||
|
|
||||||
const ARG_ACCOUNT: &str = "account";
|
const ARG_ACCOUNT: &str = "account";
|
||||||
const ARG_DRY_RUN: &str = "dry-run";
|
const ARG_DRY_RUN: &str = "dry-run";
|
||||||
|
const ARG_RESET: &str = "reset";
|
||||||
const CMD_ACCOUNTS: &str = "accounts";
|
const CMD_ACCOUNTS: &str = "accounts";
|
||||||
|
const CMD_CONFIGURE: &str = "configure";
|
||||||
const CMD_LIST: &str = "list";
|
const CMD_LIST: &str = "list";
|
||||||
const CMD_SYNC: &str = "sync";
|
const CMD_SYNC: &str = "sync";
|
||||||
|
|
||||||
type DryRun = bool;
|
type DryRun = bool;
|
||||||
|
type Reset = bool;
|
||||||
|
|
||||||
/// Represents the account commands.
|
/// Represents the account commands.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
@ -23,6 +26,8 @@ pub enum Cmd {
|
||||||
List(table::args::MaxTableWidth),
|
List(table::args::MaxTableWidth),
|
||||||
/// Represents the sync account command.
|
/// Represents the sync account command.
|
||||||
Sync(Option<SyncFoldersStrategy>, DryRun),
|
Sync(Option<SyncFoldersStrategy>, DryRun),
|
||||||
|
/// Configure the current selected account.
|
||||||
|
Configure(Reset),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the account command matcher.
|
/// Represents the account command matcher.
|
||||||
|
@ -51,6 +56,10 @@ pub fn matches(m: &ArgMatches) -> Result<Option<Cmd>> {
|
||||||
info!("list accounts subcommand matched");
|
info!("list accounts subcommand matched");
|
||||||
let max_table_width = table::args::parse_max_width(m);
|
let max_table_width = table::args::parse_max_width(m);
|
||||||
Some(Cmd::List(max_table_width))
|
Some(Cmd::List(max_table_width))
|
||||||
|
} else if let Some(m) = m.subcommand_matches(CMD_CONFIGURE) {
|
||||||
|
info!("configure account subcommand matched");
|
||||||
|
let reset = parse_reset_flag(m);
|
||||||
|
Some(Cmd::Configure(reset))
|
||||||
} else {
|
} else {
|
||||||
info!("no account subcommand matched, falling back to subcommand list");
|
info!("no account subcommand matched, falling back to subcommand list");
|
||||||
Some(Cmd::List(None))
|
Some(Cmd::List(None))
|
||||||
|
@ -80,6 +89,10 @@ pub fn subcmd() -> Command {
|
||||||
"Synchronize all folders except the given ones",
|
"Synchronize all folders except the given ones",
|
||||||
))
|
))
|
||||||
.arg(dry_run()),
|
.arg(dry_run()),
|
||||||
|
Command::new(CMD_CONFIGURE)
|
||||||
|
.about("Configure the current selected account")
|
||||||
|
.aliases(["config", "conf", "cfg"])
|
||||||
|
.arg(reset_flag()),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,3 +130,15 @@ Changes can be visualized with the RUST_LOG=trace environment variable.",
|
||||||
pub fn parse_dry_run_arg(m: &ArgMatches) -> bool {
|
pub fn parse_dry_run_arg(m: &ArgMatches) -> bool {
|
||||||
m.get_flag(ARG_DRY_RUN)
|
m.get_flag(ARG_DRY_RUN)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reset_flag() -> Arg {
|
||||||
|
Arg::new(ARG_RESET)
|
||||||
|
.help("Reset the configuration")
|
||||||
|
.short('r')
|
||||||
|
.long("reset")
|
||||||
|
.action(ArgAction::SetTrue)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_reset_flag(m: &ArgMatches) -> bool {
|
||||||
|
m.get_flag(ARG_RESET)
|
||||||
|
}
|
||||||
|
|
|
@ -6,16 +6,41 @@ use anyhow::Result;
|
||||||
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
|
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
|
||||||
use log::{info, trace};
|
use log::{info, trace};
|
||||||
use pimalaya_email::{
|
use pimalaya_email::{
|
||||||
folder::sync::Strategy as SyncFoldersStrategy, AccountConfig, Backend, BackendSyncBuilder,
|
folder::sync::Strategy as SyncFoldersStrategy, AccountConfig, Backend, BackendConfig,
|
||||||
BackendSyncProgressEvent,
|
BackendSyncBuilder, BackendSyncProgressEvent,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::DeserializedConfig,
|
config::{wizard::imap::configure_oauth2_client_secret, DeserializedConfig},
|
||||||
printer::{PrintTableOpts, Printer},
|
printer::{PrintTableOpts, Printer},
|
||||||
Accounts,
|
Accounts,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// Configure the current selected account
|
||||||
|
pub fn configure(
|
||||||
|
account_config: &AccountConfig,
|
||||||
|
backend_config: &BackendConfig,
|
||||||
|
reset: bool,
|
||||||
|
) -> Result<()> {
|
||||||
|
info!("entering the configure account handler");
|
||||||
|
match backend_config {
|
||||||
|
BackendConfig::None => (),
|
||||||
|
BackendConfig::Maildir(_) => (),
|
||||||
|
#[cfg(feature = "imap-backend")]
|
||||||
|
BackendConfig::Imap(imap_config) => {
|
||||||
|
imap_config.auth.configure(
|
||||||
|
&account_config.name,
|
||||||
|
reset,
|
||||||
|
configure_oauth2_client_secret,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
#[cfg(feature = "notmuch-backend")]
|
||||||
|
BackendConfig::Notmuch(config) => (),
|
||||||
|
};
|
||||||
|
println!("Account {} configured!", account_config.name);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Lists all accounts.
|
/// Lists all accounts.
|
||||||
pub fn list<'a, P: Printer>(
|
pub fn list<'a, P: Printer>(
|
||||||
max_width: Option<usize>,
|
max_width: Option<usize>,
|
||||||
|
|
|
@ -137,6 +137,9 @@ fn main() -> Result<()> {
|
||||||
backend.close()?;
|
backend.close()?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
Some(account::args::Cmd::Configure(reset)) => {
|
||||||
|
return account::handlers::configure(&account_config, &backend_config, reset);
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue