commit 9662299d4e508f4c6706c7c6709730f06e60aa52
parent b6928fe4a6fd39f4b2478a9e43773bdebd600ac3
Author: Luke Smith <luke@lukesmith.xyz>
Date: Sat, 30 Jan 2021 16:12:11 -0500
bugfixes and simplification. fixes #517 #546 #600 #632
remove old vars like pass_prefix, muttdir
reorder mailbox detection to find inbox case and later delim
offline delim set to _ which should avoid most issues
Diffstat:
M | bin/mw | | | 91 | ++++++++++++++++++++++++++++++++++++------------------------------------------- |
1 file changed, 41 insertions(+), 50 deletions(-)
diff --git a/bin/mw b/bin/mw
@@ -1,19 +1,15 @@
#!/bin/sh
prefix="/usr/local"
-pass_prefix="mw-"
-muttdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt" # Main mutt config location
-accdir="$muttdir/accounts" # Directory for account settings
-maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail" # Location of mail storage
-emailre=".+@.+\..+" # Regex to confirm valid email address
+maildir="${XDG_DATA_HOME:-$HOME/.local/share}/mail"
muttshare="$prefix/share/mutt-wizard"
-mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
mwconfig="$muttshare/mutt-wizard.muttrc"
cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
-muttrc="$muttdir/muttrc"
+muttrc="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/muttrc"
+accdir="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/accounts"
msmtprc="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/config"
msmtplog="${XDG_CONFIG_HOME:-$HOME/.config}/msmtp/msmtp.log"
-marker="# mw-autogenerated"
+mbsyncrc="${MBSYNCRC:-$HOME/.mbsyncrc}"
alias mbsync='mbsync -c "$mbsyncrc"'
for x 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
@@ -25,10 +21,10 @@ checkbasics() { command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
[ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
"$GPG" --list-secret-keys "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" >/dev/null 2>&1 || {
echo "First run \`pass init <yourgpgemail>\` to set up a password archive."
- echo "(If you don't already have a GPG key pair, first run \`$GPG --full-gen-key\`.)"
+ echo "(If you don't already have a GPG key pair, first run \`$GPG --full-generate-key\`.)"
exit 1 ;} ;}
-getaccounts() { accounts="$(find -L "$accdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
+getaccounts() { accounts="$(find -L "$accdir" -type f 2>/dev/null | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc$//" | sort -n)" ;}
list() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;}
@@ -49,14 +45,14 @@ host $smtp
port ${sport:-587}
from $fulladdr
user $login
-passwordeval \"pass $pass_prefix$fulladdr\"
+passwordeval \"pass $fulladdr\"
$tlsline
"
mbsync_profile="IMAPStore $fulladdr-remote
Host $imap
Port ${iport:-993}
User $login
-PassCmd \"pass $pass_prefix$fulladdr\"
+PassCmd \"pass $fulladdr\"
AuthMechs LOGIN
SSLType ${imapssl:-IMAPS}
CertificateFile $sslcert
@@ -64,8 +60,8 @@ CertificateFile $sslcert
MaildirStore $fulladdr-local
Subfolders Verbatim
Path ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/
-Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/INBOX
-Flatten .
+Inbox ${XDG_DATA_HOME:-$HOME/.local/share}/mail/$fulladdr/${inbox:-INBOX}
+Flatten _
Channel $fulladdr
Expunge Both
@@ -111,7 +107,7 @@ set folder = \"imaps://$login@$imap:${iport:-993}\"
set imap_user = \"$login\"
set header_cache = $cachedir/$fulladdr/headers
set message_cachedir = $cachedir/$fulladdr/bodies
-set imap_pass = \"\`pass $pass_prefix$fulladdr\`\"
+set imap_pass = \"\`pass $fulladdr\`\"
set mbox_type = Maildir
set ssl_starttls = yes
@@ -159,7 +155,7 @@ delete() { if [ -z "${fulladdr+x}" ]; then
rm -rf "${cachedir:?}/${fulladdr:?}" "$accdir/"[1-9]"-$fulladdr.muttrc"
sed -ibu "/[0-9]-$fulladdr.muttrc/d" "$muttrc" ; rm -f "$muttrc"bu
sed -ibu "/account $fulladdr/,/^\(\s*$\|account\)/d" "$msmtprc"; rm -f "$msmtprc"bu
- pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
+ pass rm -f "$fulladdr" >/dev/null 2>&1
# Get rid of those multiple newlines because I don't know awk well enough to do it by default lol.
for file in "$msmtprc" "$mbsyncrc"; do
@@ -171,11 +167,11 @@ delete() { if [ -z "${fulladdr+x}" ]; then
askinfo() { \
[ -z "$fulladdr" ] && echo "Give the full email address to add:" &&
read -r fulladdr
- while ! echo "$fulladdr" | grep -qE "$emailre"; do
+ while ! echo "$fulladdr" | grep -qE ".+@.+\..+"; do
echo "\`$fulladdr\` is not a valid email address. Please retype the address:"
read -r fulladdr
done
- grep -q "Channel\s*$fulladdr$" "$mbsyncrc" &&
+ grep -q "Channel\s*$fulladdr$" "$mbsyncrc" 2>/dev/null &&
{ echo "$fulladdr has already been added" && exit 1 ;}
{ [ -z "$imap" ] || [ -z "$smtp" ] ;} && parsedomains
[ -z "$imap" ] && echo "Give your email server's IMAP address (excluding the port number):" &&
@@ -192,14 +188,11 @@ askinfo() { \
fi
}
-createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
- "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr"
- rm -f "$PASSWORD_STORE_DIR/$pass_prefix$fulladdr" ;}
-
-writeinfo() { mkdir -p "$muttdir" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
+createpass() { echo "$password" > "$PASSWORD_STORE_DIR/$fulladdr"
+ "$GPG" -qer "$(cat "$PASSWORD_STORE_DIR/.gpg-id")" "$PASSWORD_STORE_DIR/$fulladdr"
+ rm -f "$PASSWORD_STORE_DIR/$fulladdr" ;}
- # Get accounts and find the first missing account number (max. 9).
- getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "$x" || { export idnum="$x"; break ;}; done
+writeinfo() { mkdir -p "${muttrc%/*}" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_CONFIG_HOME:-$HOME/.config}/msmtp" "$maildir/$fulladdr"
# Configure msmtprc for sending mail.
[ ! -f "$msmtprc" ] && echo "$msmtp_header" > "$msmtprc"
@@ -213,37 +206,32 @@ writeinfo() { mkdir -p "$muttdir" "$accdir" "$cachedir/$fulladdr/bodies" "${XDG_
# Create a muttrc for viewing mail.
echo "$mutt_profile" > "$accdir/$idnum-$fulladdr.muttrc"
[ ! -f "$muttrc" ] && echo "# vim: filetype=neomuttrc" > "$muttrc"
- ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig $marker" >> "$muttrc"
- ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc $marker" >> "$muttrc"
- echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\" $marker" >> "$muttrc"
+ ! grep -q "^source.*mutt-wizard.muttrc" "$muttrc" && echo "source $mwconfig" >> "$muttrc"
+ ! grep "^source.*.muttrc" "$muttrc" | grep -qv "$mwconfig" && echo "source $accdir/$idnum-$fulladdr.muttrc" >> "$muttrc"
+ echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$idnum-$fulladdr.muttrc<enter><change-folder>!<enter>;<check-stats>' \"switch to $fulladdr\"" >> "$muttrc"
notmuchauto # Create a notmuch config file if not present already.
}
-getpass() { while : ; do pass rm -f "$pass_prefix$fulladdr" >/dev/null 2>&1
- pass insert "$pass_prefix$fulladdr" && break; done ;}
+getpass() { while : ; do pass rm -f "$fulladdr" >/dev/null 2>&1
+ pass insert "$fulladdr" && break; done ;}
formatShortcut() { toappend="$toappend
-macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\" $marker
-macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\" $marker
-macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\" $marker" >> "$accdir/$idnum-$fulladdr.muttrc" ;}
+macro index,pager g$1 \"<change-folder>=$3<enter>\" \"go to $2\"
+macro index,pager M$1 \";<save-message>=$3<enter>\" \"move mail to $2\"
+macro index,pager C$1 \";<copy-message>=$3<enter>\" \"copy mail to $2\"" ;}
setBox() { toappend="$toappend
-set $1 = \"+$2\" $marker" ;}
-
-getboxes() { [ -n "${force+x}" ] && mailboxes="INBOX
-Drafts
-Junk
-Trash
-Sent
-Archive" && return 0
- info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $pass_prefix$fulladdr)" --url "${protocol:-imaps}://$imap:${iport:-993}")"
- [ -z "$info" ] && echo "Log-on not successful." && return 1
- mailboxes="$(echo "$info" | sed "s/.*\" //;s/\"//g" | tr -d '')"
-}
+set $1 = \"+$2\"" ;}
-finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
- sed -ibu "/$marker/d" "$accdir/$idnum-$fulladdr.muttrc" ; rm -f "$accdir/$idnum-$fulladdr.muttrcbu"
+getboxes() { if [ -n "${force+x}" ]; then
+ mailboxes="$(printf "INBOX\\nDrafts\\nJunk\\nTrash\\nSent\\nArchive")"
+ else
+ info="$(curl --location-trusted -s -m 5 --user "$login:$(pass $fulladdr)" --url "${protocol:-imaps}://$imap:${iport:-993}")"
+ [ -z "$info" ] && echo "Log-on not successful." && return 1
+ mailboxes="$(echo "$info" | sed "s/.*\" //;s/\"//g" | tr -d '')"
+ fi
+ getaccounts; for x in $(seq 1 9); do echo "$accounts" | grep -q "^$x:" || { export idnum="$x"; break ;}; done
toappend="mailboxes $(echo "$mailboxes" | sed "s/^/\"=/;s/$/\"/" | paste -sd ' ' - )"
for x in $mailboxes; do
case "$x" in
@@ -253,9 +241,12 @@ finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur
*[Jj][Uu][Nn][Kk]*) formatShortcut j junk "$x" ;;
*[Aa][Rr][Cc][Hh][Ii][Vv][Ee]*) formatShortcut a archive "$x" ;;
*[Ss][Pp][Aa][Mm]*) formatShortcut S spam "$x" ;;
- *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" ;;
+ *[Ii][Nn][Bb][Oo][Xx]*) formatShortcut i inbox "$x"; setBox spoolfile "$x" inbox="$x" ;;
esac
done
+}
+
+finalize() { echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$fulladdr/{}/cur" "$maildir/$fulladdr/{}/tmp" "$maildir/$fulladdr/{}/new"
echo "$toappend" >> "$accdir/$idnum-$fulladdr.muttrc"
[ -z "${online+x}" ] && echo "$fulladdr (account #$idnum) added successfully."
command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview"
@@ -328,7 +319,7 @@ Options allowed with -a:
NOTE: Once at least one account is added, you can run
\`mbsync -a\` to begin downloading mail.
-To change an account's password, run \`pass edit ${pass_prefix}your@email.com\`.
+To change an account's password, run \`pass edit your@email.com\`.
EOF
}
@@ -365,7 +356,7 @@ esac done
case "$action" in
list) list ;;
- add) checkbasics && askinfo && getprofiles && writeinfo && getboxes && finalize || { delete ; exit 1 ;} ;;
+ add) checkbasics && askinfo && getboxes && getprofiles && writeinfo && finalize || { delete ; exit 1 ;} ;;
delete) delete ;;
sync) mailsync $fulladdr ;;
toggle) togglecron ;;