diff --git a/.gitignore b/.gitignore index 8573c25..e2338cc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ personal.muttrc *.gpg *.pyc accounts/ -*mailsynclastrun +*mw.lastrun diff --git a/README.rst b/README.rst index a124712..e1b53e8 100644 --- a/README.rst +++ b/README.rst @@ -37,6 +37,19 @@ DESCRIPTION **mw** configures **mbsync**, **getmail**, **msmtp** and **mutt** in one go. **mutt** stands for **mutt** or **neomutt**. +**mw** asks the user for the information it needs. +They cannot be provided as parameters to the command. +It is still possible to script **mw** by defining variables. +See the commands section. + +*mutt-wizard.muttrc* is linked in your *muttrc*. +Have this line there, if you prefer your own settings:: + + # source /usr/share/mutt-wizard/mutt-wizard.muttrc + +You will need to keep the binding of *i,g,C,M* to *noop*, though, +because of the generated bindings in the account muttrc. + COMMANDS ======== @@ -48,6 +61,7 @@ If the parameter contains @ an email is assumed and synced. Add new email First you decide, whether you want to configure + - **mbsync**, **getmail**, **msmtp** to sync emails independently from **mutt** using **mw**, - or just **mutt**. For an IMAP server **mutt** becomes very slow. @@ -59,6 +73,12 @@ If the parameter contains @ an email is assumed and synced. If you are lucky, your email server is there. Else **mw** will prompt you. + Add an email without questions, e.g.:: + + a=x.y@gmail.com mwtype=offline mwaddr=$a mwlogin=$a mwpass=mutt-wizard-$a mwserverinfo="gmail.com,imap.gmail.com,993,smtp.gmail.com,587" mwname="your name" mw add + + *mwserverinfo* can be omitted, if the email domain is in the accompanied *domains.csv*. + *ls|list* List all email accounts configured by mutt-wizard @@ -67,6 +87,10 @@ If the parameter contains @ an email is assumed and synced. Remove the configuration files for an already configured email + Remove without questions the email whose generated muttrc starts with 1:: + + mwpick=1 mw rm <</dev/null 2>&1 + pass insert "$mwpass" && break; done ; } -_mwadd() { - type gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2" - [ -z "$PASSWORD_STORE_DIR" ] && PASSWORD_STORE_DIR="$HOME/.password-store" - [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] && - "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || { - printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init \`.\\n" - printf "If you don't have a GPG public private key pair, run \`$GPG --full-gen-key\` first.\\n" - return 1 - } - _mwcheckcert && _mwcheckinternet && _mwasktype && _mwaskinfo && _mwprofiles && _mwtryconnect && _mwfinalize || _mwdelete +_mwcheckcert() { + for mwcrt in "/etc/ssl/certs/ca-certificates.crt" "/etc/pki/tls/certs/ca-bundle.crt" "/etc/ssl/ca-bundle.pem" \ + "/etc/pki/tls/cacert.pem" "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem" "/etc/ssl/cert.pem" "/usr/local/share/ca-certificates/" + do + [ -f "$mwcrt" ] && mwsslcert="$mwcrt" && break + done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && return 1 ;} } -_mwgetpass() { - while : ; do pass rm -f "$mwpass" >/dev/null 2>&1 - pass insert "$mwpass" && break; done ; +_mwcheckinternet() { + # Checks for internet connection + if type systemctl >/dev/null; then + if ! systemctl --type service | grep net | grep active > /dev/null; then + echo "No internet connection." + return 1 + fi + else + if ! ping -q -c 1 1.1.1.1 > /dev/null; then + echo "No internet connection (ping failed)." + return 1 + fi + fi +} + +_mwasktype() { + if [ -z "$mwtype" ]; then + while : ; do + printf "Do you want to sync your mails using the command 'mw' outside mutt? [yes/no]\\n" + printf "'no' makes Neomutt slow for IMAP accounts, so probably you want 'yes'.\\n\t" + read -r mwoffline + case "$mwoffline" in + [Yy][Ee][Ss]) mwtype="offline" && break ;; + [Nn][Oo]) mwtype="online" && break ;; + *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;; + esac + done + fi + if [ "$mwtype" == "offline" ]; then + ! type "$prefix/bin/msmtp" >/dev/null && printf "\033[31m\`msmtp\` must be installed. You can do that afterwards.\\n\033[0m" + else + ! ( type mutt >/dev/null || type neomutt >/dev/null ) && printf "\033[31m\`mutt\` must be installed. You can do that afterwards.\\n\033[0m" + fi + + return 0 } _mwaskinfo() { @@ -78,8 +107,10 @@ _mwaskinfo() { done fi mwdomain="$(echo "$mwaddr" | sed "s/.*@//")" - printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$mwdomain" - mwserverinfo="$(grep "^$mwdomain" "$mwshare/domains.csv" 2>/dev/null)" + if [ -z "$mwserverinfo" ]; then + printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$mwdomain" + mwserverinfo="$(grep "^$mwdomain" "$mwshare/domains.csv" 2>/dev/null)" + fi if [ -z "$mwserverinfo" ]; then printf "Your email domain is not known to $_MW_.\\nType in your settings.\\n" printf "Usually you find them by an internet search.\\n" @@ -273,6 +304,28 @@ _mwtryconnect() { fi } +_mwnotmuchauto() { + [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config" + [ -f "$NOTMUCH_CONFIG" ] && return 0 + mkdir -p "${NOTMUCH_CONFIG%/*}" + cat > "$NOTMUCH_CONFIG" </dev/null + rm -f "$mwaccmuttrc" &>/dev/null + if [ "$mwtype" = "offline" ]; then # not online + sed -i "/${mwaccmuttrc//\//\\\/}/d" "$mwgenmuttaccs" &>/dev/null + sed -i "/account $mwaddr/,/^\(\s*$\|account\)/d" "$mwmsmtprc" &>/dev/null + if [ -f "$mwgetmaildir/$mwaddr" ]; then + rm -f "$mwgetmaildir/$mwaddr" &>/dev/null + else + sed -i "/IMAPStore $mwaddr-remote$/,/# End $_MW_ profile/d" "$mwmbsyncrc" &>/dev/null + fi + else + sed -i "/${mwaccmuttrc//\//\\\/}/d" "$mwmuttrc" &>/dev/null + rm -rf "$mwacccachedir" &>/dev/null + fi +} + +_mwadd() { + type gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2" + [ -z "$PASSWORD_STORE_DIR" ] && PASSWORD_STORE_DIR="$HOME/.password-store" + [ -r "$PASSWORD_STORE_DIR/.gpg-id" ] && + "$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || { + printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init \`.\\n" + printf "If you don't have a GPG public private key pair, run \`$GPG --full-gen-key\` first.\\n" + return 1 + } + _mwcheckcert && _mwcheckinternet && _mwasktype && _mwaskinfo && _mwprofiles && _mwtryconnect && _mwfinalize || _mwdelete +} + _mwconfirm() { printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r mwinput && ! echo "$mwinput" | grep -i "^y$\|^yes$" >/dev/null \ && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 return 0 ; } +_mwlist() { + _mwaddrs && [ -n "$mwaddrs" ] && echo "${mwaddrs//.mwonofftype./ }" +} + _mwpick() { printf "Select an accounts to %s:\\n" "$1" _mwlist @@ -311,68 +397,34 @@ _mwpick() { return 0 ; } -_mwdelete() { - [ "$mwaccmaildirWasThere" = "NO" ] && rm -rf "$mwaccmaildir" &>/dev/null - rm -f "$mwaccmuttrc" &>/dev/null - if [ "$mwtype" = "offline" ]; then # not online - sed -i "/${mwaccmuttrc//\//\\\/}/d" "$mwgenmuttaccs" &>/dev/null - sed -i "/account $mwaddr/,/^\(\s*$\|account\)/d" "$mwmsmtprc" &>/dev/null - if [ -f "$mwgetmaildir/$mwaddr" ]; then - rm -f "$mwgetmaildir/$mwaddr" &>/dev/null - else - sed -i "/IMAPStore $mwaddr-remote$/,/# End $_MW_ profile/d" "$mwmbsyncrc" &>/dev/null - fi - else - sed -i "/${mwaccmuttrc//\//\\\/}/d" "$mwmuttrc" &>/dev/null - rm -rf "$mwacccachedir" &>/dev/null - fi -} - _mwcron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1 if crontab -l | grep "mw sync" >/dev/null; then - echo "Active mail sync cronjob detected. Do you want to remove it?" - printf "\033[36m\t" - [ -z "$mwcronremove" ] && read -r mwcronremove - printf "\033[0m" + if [ -z $mwcronminutes ]; then + echo "Active mail sync cronjob detected. Do you want to remove it?" + printf "\033[36m\t" + [ -z $mwcronremove ] && read -r mwcronremove + printf "\033[0m" + else # remove to add again + mwcronremove="yes" + fi echo "$mwcronremove" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mw sync/d' | crontab - >/dev/null \ - && echo "Mail sync turned off." - else + && echo "mw cronjob removed." + [ -z $mwcronminutes ] && return + fi + if [ -z $mwcronminutes ]; then echo "How many minutes between each mail sync?" printf "\033[36m\t" [ -z $mwcronminutes ] && read -r mwcronminutes printf "\033[0m" - while ! echo "$mwcronminutes" | grep "^[1-9]\+$" >/dev/null; do - printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t" - read -r mwcronminutes - printf "\033[0m" - done - (crontab -l; echo "*/$mwcronminutes * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mw | cut -d' ' -f3) sync") \ - | crontab - && \ - echo "Cronjob added. Mail will sync every $mwcronminutes minutes. Be sure you have your cron manager running." - fi -} - -_mwasktype() { - if [ -z "$mwtype" ]; then - while : ; do - printf "Do you want to sync your mails using the command 'mw' outside mutt? [yes/no]\\n" - printf "'no' makes Neomutt slow for IMAP accounts, so probably you want 'yes'.\\n\t" - read -r mwoffline - case "$mwoffline" in - [Yy][Ee][Ss]) mwtype="offline" && break ;; - [Nn][Oo]) mwtype="online" && break ;; - *) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;; - esac - done - fi - if [ "$mwtype" == "offline" ]; then - ! type "$prefix/bin/msmtp" >/dev/null && printf "\033[31m\`msmtp\` must be installed. You can do that afterwards.\\n\033[0m" - else - ! ( type mutt >/dev/null || type neomutt >/dev/null ) && printf "\033[31m\`mutt\` must be installed. You can do that afterwards.\\n\033[0m" fi - - return 0 + while ! echo "$mwcronminutes" | grep "^[1-9]\+$" >/dev/null; do + printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t" + read -r mwcronminutes + printf "\033[0m" + done + (crontab -l; echo "*/$mwcronminutes * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mw | cut -d' ' -f3) sync") \ + | crontab - && echo "mw cronjob added. Mail will sync every $mwcronminutes minutes. Be sure you have your cron manager running." } _mwpurge() { @@ -392,28 +444,6 @@ _mwpurge() { echo "All configs and account settings have been purged." } -_mwnotmuchauto() { - [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config" - [ -f "$NOTMUCH_CONFIG" ] && return 0 - mkdir -p "${NOTMUCH_CONFIG%/*}" - cat > "$NOTMUCH_CONFIG" </dev/null; then - if ! systemctl --type service | grep net | grep active > /dev/null; then - echo "No internet connection." - return 1 - fi - else - if ! ping -q -c 1 1.1.1.1 > /dev/null; then - echo "No internet connection (ping failed)." - return 1 - fi - fi -} - _mwsync() { unset mwemails mwtype="offline" diff --git a/mw.1 b/mw.1 index 85035af..7dcded3 100644 --- a/mw.1 +++ b/mw.1 @@ -50,6 +50,24 @@ Sync email without mutt. \f[B]mw\f[R] configures \f[B]mbsync\f[R], \f[B]getmail\f[R], \f[B]msmtp\f[R] and \f[B]mutt\f[R] in one go. \f[B]mutt\f[R] stands for \f[B]mutt\f[R] or \f[B]neomutt\f[R]. +.PP +\f[B]mw\f[R] asks the user for the information it needs. +They cannot be provided as parameters to the command. +It is still possible to script \f[B]mw\f[R] by defining variables. +See the commands section. +.PP +\f[I]mutt-wizard.muttrc\f[R] is linked in your \f[I]muttrc\f[R]. +Have this line there, if you prefer your own settings: +.IP +.nf +\f[C] +# source /usr/share/mutt-wizard/mutt-wizard.muttrc +\f[R] +.fi +.PP +You will need to keep the binding of \f[I]i,g,C,M\f[R] to +\f[I]noop\f[R], though, because of the generated bindings in the account +muttrc. .SH COMMANDS .PP Without command parameter \f[B]sync\f[R] is assumed. @@ -60,9 +78,12 @@ If the parameter contains \[at] an email is assumed and synced. .PP Add new email .PP -First you decide, whether you want to configure - \f[B]mbsync\f[R], -\f[B]getmail\f[R], \f[B]msmtp\f[R] to sync emails independently from -\f[B]mutt\f[R] using \f[B]mw\f[R], - or just \f[B]mutt\f[R]. +First you decide, whether you want to configure +.IP \[bu] 2 +\f[B]mbsync\f[R], \f[B]getmail\f[R], \f[B]msmtp\f[R] to sync emails +independently from \f[B]mutt\f[R] using \f[B]mw\f[R], +.IP \[bu] 2 +or just \f[B]mutt\f[R]. For an IMAP server \f[B]mutt\f[R] becomes very slow. So normally you enter \f[B]yes\f[R] here. .PP @@ -72,6 +93,17 @@ So normally you enter \f[B]yes\f[R] here. ports. If you are lucky, your email server is there. Else \f[B]mw\f[R] will prompt you. +.PP +Add an email without questions, e.g.: +.IP +.nf +\f[C] +a=x.y\[at]gmail.com mwtype=offline mwaddr=$a mwlogin=$a mwpass=mutt-wizard-$a mwserverinfo=\[dq]gmail.com,imap.gmail.com,993,smtp.gmail.com,587\[dq] mwname=\[dq]your name\[dq] mw add +\f[R] +.fi +.PP +\f[I]mwserverinfo\f[R] can be omitted, if the email domain is in the +accompanied \f[I]domains.csv\f[R]. .RE .PP \f[I]ls|list\f[R] @@ -84,6 +116,14 @@ List all email accounts configured by mutt-wizard .RS .PP Remove the configuration files for an already configured email +.PP +Remove without questions the email whose generated muttrc starts with 1: +.IP +.nf +\f[C] +mwpick=1 mw rm <<