Skip to content

Commit

Permalink
add basic support for xdg base directory specification
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Jul 7, 2024
1 parent 44b5b65 commit f8e6a2b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 86 deletions.
52 changes: 25 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Command-line Options:
prompt for cache encryption during oauth2 setup

-d, --confdir <DIR>
use a different directory than ~/.nmail
use a different directory than ~/.config/nmail

-e, --verbose
enable verbose logging
Expand Down Expand Up @@ -82,18 +82,15 @@ Command-line Options:

Configuration files:

~/.nmail/auth.conf
~/.config/nmail/auth.conf
configures custom oauth2 client id and secret

~/.nmail/main.conf
~/.config/nmail/main.conf
configures mail account and general settings

~/.nmail/ui.conf
~/.config/nmail/ui.conf
customizes UI settings

~/.nmail/secret.conf
stores saved passwords

Examples:

nmail -s gmail
Expand Down Expand Up @@ -236,8 +233,8 @@ file:

$ nmail

Then open the config file `~/.nmail/main.conf` in your favourite text editor
and fill out the required fields:
Then open the config file `~/.config/nmail/main.conf` in your favourite text
editor and fill out the required fields:

[email protected]
drafts=Drafts
Expand All @@ -251,7 +248,7 @@ and fill out the required fields:
trash=Trash
[email protected]

Full example of a config file `~/.nmail/main.conf`:
Full example of a config file `~/.config/nmail/main.conf`:

[email protected]
addressbook_encrypt=0
Expand Down Expand Up @@ -571,8 +568,8 @@ different config directories (and thus different email accounts), but it will
be just that - multiple instances - each in its own terminal. To facilitate
such usage one can set up aliases for accessing different accounts, e.g.:

alias gm='nmail -d ${HOME}/.nmail-gm' # gmail
alias hm='nmail -d ${HOME}/.nmail-hm' # hotmail
alias gm='nmail -d ${HOME}/.config/nmail-gm' # gmail
alias hm='nmail -d ${HOME}/.config/nmail-hm' # hotmail


Email Viewer
Expand Down Expand Up @@ -699,7 +696,7 @@ encrypted).
Storing the account password (`save_pass=1` in main.conf) is *not* secure.
While nmail encrypts the password, the key is trivial to determine from
the source code. Only store the password if measurements are taken to ensure
`~/.nmail/secret.conf` cannot by accessed by a third-party.
`~/.config/nmail/secret.conf` cannot by accessed by a third-party.


Configuration
Expand All @@ -708,8 +705,8 @@ Configuration
Aside from `main.conf` covered above, the following files can be used to
configure nmail.

~/.nmail/ui.conf
----------------
~/.config/nmail/ui.conf
-----------------------
This configuration file controls the UI aspects of nmail. Default configuration
file (platform-dependent defaults are left empty below):

Expand Down Expand Up @@ -1002,10 +999,11 @@ an email (default disabled).
### signature

Determines whether to suffix emails with a signature (default disabled). When
enabled, nmail will use `~/.nmail/signature.txt` if present, or otherwise use
`~/.signature` for signature plain text content. When composing markdown
formatted emails, nmail will use `~/.nmail/signature.html` if present, for the
html part, and otherwise simply convert the plain text signature to html.
enabled, nmail will use `~/.config/nmail/signature.txt` if present, or
otherwise use `~/.signature` for signature plain text content. When composing
markdown formatted emails, nmail will use `~/.config/nmail/signature.html` if
present, for the html part, and otherwise simply convert the plain text
signature to html.

Note: For **custom html** signature to work properly, the plain text signature
should not be present more than once in the composed message, thus a very short
Expand All @@ -1029,14 +1027,14 @@ Controls which character to indicate that an email is unread (default: `N`).
For a more graphical interface, an emoji such as `` can be used.


~/.nmail/colors.conf
--------------------
~/.config/nmail/colors.conf
---------------------------
This configuration file controls the configurable colors of nmail. For this
configuration to take effect, `colors_enabled=1` must be set in
`~/.nmail/ui.conf`.
`~/.config/nmail/ui.conf`.

Example color config files are provided in `/usr/local/share/nmail/themes`
and can be used by overwriting `~/.nmail/colors.conf`.
and can be used by overwriting `~/.config/nmail/colors.conf`.

### Htop style theme

Expand All @@ -1047,7 +1045,7 @@ nmail and htop.

To use this config:

cp /usr/local/share/nmail/themes/htop-style.conf ~/.nmail/colors.conf
cp /usr/local/share/nmail/themes/htop-style.conf ~/.config/nmail/colors.conf

### Manual configuration

Expand Down Expand Up @@ -1119,8 +1117,8 @@ Selected messages in message list view.
Top / title bar.


~/.nmail/auth.conf
------------------
~/.config/nmail/auth.conf
-------------------------
This configuration file allows users to set up custom OAuth 2.0 client id and
client secret. If not specified, nmail uses its own application id and secret.
Default configuration file:
Expand Down Expand Up @@ -1164,7 +1162,7 @@ to `d99kris at gmail dot com` with subject `nmail google oauth2 invite` from
the google account address you would like to be invited.

Alternatively a user may set up their own OAuth 2.0 application with Google
and configure `~/.nmail/auth.conf` accordingly.
and configure `~/.config/nmail/auth.conf` accordingly.


Accessing Email Cache using Other Email Clients
Expand Down
9 changes: 5 additions & 4 deletions doc/DEBUGGING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ If any issues are observed, try running nmail with verbose logging

nmail --verbose

and provide a copy of `~/.nmail/log.txt` when reporting a bug.
and provide a copy of `~/.config/nmail/log.txt` when reporting a bug.

If nmail **crashes** it's very useful to obtain a core dump and extract
information from it (such as the callstacks). See details below.
Expand All @@ -25,8 +25,8 @@ writable for all users by running:

sudo chmod og+w /cores

Then enable core dumps only for nmail by editing `~/.nmail/main.conf` and
setting:
Then enable core dumps only for nmail by editing `~/.config/nmail/main.conf`
and setting:

coredump_enabled=1

Expand Down Expand Up @@ -59,7 +59,8 @@ substantial amount of disk space:

Core Dumps - Linux
==================
Enable core dumps only for nmail by editing `~/.nmail/main.conf` and setting:
Enable core dumps only for nmail by editing `~/.config/nmail/main.conf` and
setting:

coredump_enabled=1

Expand Down
75 changes: 37 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main(int argc, char* argv[])
{
// Defaults
umask(S_IRWXG | S_IRWXO);
Util::SetApplicationDir(std::string(getenv("HOME")) + std::string("/.nmail"));
Util::SetApplicationDir(Util::GetDefaultApplicationDir());
Log::SetVerboseLevel(Log::INFO_LEVEL);
bool online = true;
bool changePass = false;
Expand Down Expand Up @@ -462,47 +462,46 @@ static void ShowHelp()
"Usage: nmail [OPTION]\n"
"\n"
"Options:\n"
" -c, --cache-encrypt prompt for cache encryption during oauth2 setup\n"
" -d, --confdir <DIR> use a different directory than ~/.nmail\n"
" -e, --verbose enable verbose logging\n"
" -ee, --extra-verbose enable extra verbose logging\n"
" -h, --help display this help and exit\n"
" -k, --keydump key code dump mode\n"
" -o, --offline run in offline mode\n"
" -p, --pass change password\n"
" -s, --setup <SERVICE> setup wizard for specified service, supported\n"
" services: gmail, gmail-oauth2, icloud, outlook,\n"
" outlook-oauth2\n"
" -v, --version output version information and exit\n"
" -x, --export <DIR> export cache to specified dir in Maildir format\n"
" -c, --cache-encrypt prompt for cache encryption during oauth2 setup\n"
" -d, --confdir <DIR> use a different directory than ~/.config/nmail\n"
" -e, --verbose enable verbose logging\n"
" -ee, --extra-verbose enable extra verbose logging\n"
" -h, --help display this help and exit\n"
" -k, --keydump key code dump mode\n"
" -o, --offline run in offline mode\n"
" -p, --pass change password\n"
" -s, --setup <SERVICE> setup wizard for specified service, supported\n"
" services: gmail, gmail-oauth2, icloud, outlook,\n"
" outlook-oauth2\n"
" -v, --version output version information and exit\n"
" -x, --export <DIR> export cache to specified dir in Maildir format\n"
"\n"
"Examples:\n"
" nmail -s gmail setup nmail for a gmail account\n"
" nmail running nmail without setup wizard will generate\n"
" default configuration files in ~/.nmail dir if\n"
" not present already, these can be manually edited\n"
" to configure nmail for email service providers not\n"
" supported by the built-in setup wizard (refer to\n"
" FILES section on ~/.nmail/main.conf for details)\n"
" nmail -s gmail setup nmail for a gmail account\n"
" nmail running nmail without setup wizard will generate\n"
" default configuration files in the nmail dir if\n"
" not present already, these can be edited to\n"
" configure nmail for email service providers not\n"
" supported by the built-in setup wizard (refer to\n"
" FILES section for details)\n"
"\n"
"Files:\n"
" ~/.nmail/auth.conf configures custom oauth2 client id and secret\n"
" ~/.nmail/main.conf configures mail account and general settings, for\n"
" full nmail functionality the following fields\n"
" shall be configured:\n"
" address (ex: [email protected]),\n"
" drafts (folder name, ex: Drafts),\n"
" imap_host (ex: imap.example.com),\n"
" imap_port (ex: 993),\n"
" inbox (folder name, ex: Inbox),\n"
" name (ex: Firstname Lastname),\n"
" sent (folder name, ex: Sent),\n"
" smtp_host (ex: smtp.example.com),\n"
" smtp_port (ex: 465 or 587),\n"
" trash (folder name, ex: Trash),\n"
" user (ex: [email protected] or example).\n"
" ~/.nmail/ui.conf customizes UI settings\n"
" ~/.nmail/secret.conf stores saved passwords\n"
" ~/.config/nmail/auth.conf configures custom oauth2 client id and secret\n"
" ~/.config/nmail/main.conf configures mail account and general settings,\n"
" for full functionality the following fields\n"
" shall be configured:\n"
" address (ex: [email protected]),\n"
" drafts (folder name, ex: Drafts),\n"
" imap_host (ex: imap.example.com),\n"
" imap_port (ex: 993),\n"
" inbox (folder name, ex: Inbox),\n"
" name (ex: Firstname Lastname),\n"
" sent (folder name, ex: Sent),\n"
" smtp_host (ex: smtp.example.com),\n"
" smtp_port (ex: 465 or 587),\n"
" trash (folder name, ex: Trash),\n"
" user (ex: [email protected] or example).\n"
" ~/.config/nmail/ui.conf customizes UI settings\n"
"\n"
"Report bugs at https://github.com/d99kris/nmail\n"
"\n";
Expand Down
25 changes: 11 additions & 14 deletions src/nmail.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NMAIL "1" "July 2024" "nmail 5.1.3" "User Commands"
.TH NMAIL "1" "July 2024" "nmail 5.1.4" "User Commands"
.SH NAME
nmail \- ncurses mail
.SH SYNOPSIS
Expand All @@ -14,7 +14,7 @@ alpine / pine, supporting IMAP and SMTP.
prompt for cache encryption during oauth2 setup
.TP
\fB\-d\fR, \fB\-\-confdir\fR <DIR>
use a different directory than ~/.nmail
use a different directory than ~/.config/nmail
.TP
\fB\-e\fR, \fB\-\-verbose\fR
enable verbose logging
Expand Down Expand Up @@ -46,12 +46,12 @@ output version information and exit
export cache to specified dir in Maildir format
.SH FILES
.TP
~/.nmail/auth.conf
~/.config/nmail/auth.conf
configures custom oauth2 client id and secret
.TP
~/.nmail/main.conf
configures mail account and general settings, for
full nmail functionality the following fields
~/.config/nmail/main.conf
configures mail account and general settings,
for full functionality the following fields
shall be configured:
address (ex: [email protected]),
drafts (folder name, ex: Drafts),
Expand All @@ -65,23 +65,20 @@ smtp_port (ex: 465 or 587),
trash (folder name, ex: Trash),
user (ex: [email protected] or example).
.TP
~/.nmail/ui.conf
~/.config/nmail/ui.conf
customizes UI settings
.TP
~/.nmail/secret.conf
stores saved passwords
.SH EXAMPLES
.TP
nmail \-s gmail
setup nmail for a gmail account
.TP
nmail
running nmail without setup wizard will generate
default configuration files in ~/.nmail dir if
not present already, these can be manually edited
to configure nmail for email service providers not
default configuration files in the nmail dir if
not present already, these can be edited to
configure nmail for email service providers not
supported by the built\-in setup wizard (refer to
FILES section on ~/.nmail/main.conf for details)
FILES section for details)
.SH AUTHOR
Written by Kristofer Berggren.
.SH "REPORTING BUGS"
Expand Down
4 changes: 2 additions & 2 deletions src/oauth2nmail
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def show_help():
print("")
print("Examples:")
print(" OAUTH2_TYPE=\"gmail-oauth2\" OAUTH2_CLIENT_ID=\"9\" OAUTH2_CLIENT_SECRET=\"j\" \\")
print(" OAUTH2_TOKEN_STORE=\"${HOME}/.nmail/oauth2.tokens\" oauth2nmail -g")
print(" OAUTH2_TOKEN_STORE=\"${HOME}/.config/nmail/oauth2.tokens\" oauth2nmail -g")
print("")
print(" OAUTH2_TYPE=\"gmail-oauth2\" OAUTH2_CLIENT_ID=\"9\" OAUTH2_CLIENT_SECRET=\"j\" \\")
print(" OAUTH2_TOKEN_STORE=\"${HOME}/.nmail/oauth2.tokens\" oauth2nmail -r")
print(" OAUTH2_TOKEN_STORE=\"${HOME}/.config/nmail/oauth2.tokens\" oauth2nmail -r")
print("")
print("Report bugs at https://github.com/d99kris/nmail")
print("")
Expand Down
22 changes: 22 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,3 +2295,25 @@ bool Util::IsIpAddress(const std::string& p_Str)

return rv;
}

std::string Util::GetDefaultApplicationDir()
{
char* homeEnv = getenv("HOME");
static const std::string homeDir = std::string((homeEnv != nullptr) ? homeEnv : "");

// Use old ~/.nmail if present, for backward compatibility
static const std::string legacyApplicationDir = homeDir + "/.nmail";
if (IsDir(legacyApplicationDir))
{
return legacyApplicationDir;
}

// Common value: export XDG_CONFIG_HOME="$HOME/.config"
char* xdgConfigHomeEnv = getenv("XDG_CONFIG_HOME");
static const std::string xdgConfigHomeDir = std::string((xdgConfigHomeEnv != nullptr) ? xdgConfigHomeEnv : "");
static const std::string configHomeDir = (!xdgConfigHomeDir.empty() ? xdgConfigHomeDir : (homeDir + "/.config"));

// Typically: ~/.config/nmail
static const std::string applicationDir = configHomeDir + "/nmail";
return applicationDir;
}
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class Util
static std::string GetOsArch();
static std::string ExtractString(const std::string& p_Str, const std::string& p_Prefix, const std::string& p_Suffix);
static bool IsIpAddress(const std::string& p_Str);
static std::string GetDefaultApplicationDir();

template<typename T>
static inline void Unused(const T& p_Arg)
Expand Down
2 changes: 1 addition & 1 deletion src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "version.h"

#define NMAIL_VERSION "5.1.3"
#define NMAIL_VERSION "5.1.4"

std::string Version::GetBuildOs()
{
Expand Down

0 comments on commit f8e6a2b

Please sign in to comment.