diff --git a/mw b/mw index 4af2f5b..d28b4b7 100755 --- a/mw +++ b/mw @@ -11,8 +11,6 @@ gpgemail="$(cat "$creddir/gpgemail" 2>/dev/null)" # Get previously set gpg email tmpdir="$(mktemp -d)" GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command -mkdir -p "$maildir" "$creddir" "$bindir" - # Get certificate location depending on OS. Linux is elsewhere condition. case "$(uname)" in "Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;; @@ -142,12 +140,16 @@ EOF idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}') getpass "$title" getprofiles + mkdir -p "$accdir" mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp" [ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config" [ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config" echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config" echo "$msmtp_profile" >> "$HOME/.config/msmtp/config" echo "$mutt_profile" > "$accdir/$title.muttrc" + sed -i "s/^accounts =.*[a-zA-Z]$/&, $title/g;s/^accounts =\\s*$/accounts = $title/g" "$HOME/.config/offlineimap/config" + echo "macro index,pager i$idnum 'source \"$muttdir\"/accounts/$title.muttrc!;'" >> "$muttdir/personal.muttrc" + ! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc" trysync && finalize } @@ -159,10 +161,11 @@ getpass() { \ echo "$password" > "$tmpdir/$1" printf "Encrypting your password with %s..." "$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1" - print "DONE\\nShredding all memory of your password for safety's sake..." + printf "DONE\\nShredding all memory of your password for safety's sake..." unset password - shred -u "$tmpdir/$1" | rm -f "$tmpdir/$1" - mv "$tmpdir/$1.gpg" "$creddir" + shred -u "$tmpdir/$1" #| rm -f "$tmpdir/$1" + mkdir -p "$creddir" + mv "$tmpdir/$1.gpg" "$creddir/" printf "DONE.\\n" } @@ -174,6 +177,7 @@ askgpg() { \ read -r gpgemail done if "$GPG" -K | grep "<$gpgemail>" >/dev/null; then + mkdir -p "$creddir" echo "$gpgemail" > "$creddir/gpgemail" else printf "You do not appear to have a private key associated with %s.\\nPlease generate a GPG key pair by running \`%s --full-gen-key\` and rerun the wizard.\\n" "$gpgemail" "$GPG" @@ -196,73 +200,73 @@ gen_delim() { \ echo $delim ;} trysync() { \ - ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && exit 1 + ! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && return 1 printf "mutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\nPress enter to continue.\\n." stty -echo read wait stty echo - (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap) & + mkdir -p "$maildir" + (sleep 15; killall offlineimap; killall offlineimap; killall offlineimap)>/dev/null 2>&1 & offlineimap -qoa "$title" if ls -d "$maildir/$1/"* >/dev/null 2>&1; then - printf "\033[32mSync successful.\033[0m\\n"; exit + printf "\033[32mSync successful.\033[0m\\n"; return else - printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; exit 1 + printf "\033[31m\033[31mSync not successful.\033[0m Try running offlineimap manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n"; return 1 fi } finalize() { \ boxes="$(ls -d "$maildir/$title/"* 2>/dev/null | sed "s/.*\///;s/^/=/")" - [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && exit - echo "$boxes" > "$tmpdir/$title_boxes" + [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$title" && return + echo "$boxes" > "$tmpdir/title_boxes" printf "Setting up the mutt sidebar...\\n" sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')" delim="$(gen_delim "$sidebar_width")" printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n" - oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$title_boxes" | tr "\n" " ")" + oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")" oneline="=$title $delim $oneline" - spoolfile=$(grep -i "$tmpdir/$title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q) - record=$(grep -i "$tmpdir/$title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q) - postponed=$(grep -i "$tmpdir/$title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q) - trash=$(grep -i "$tmpdir/$title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q) - { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc" + spoolfile=$(grep -i "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q) + record=$(grep -i "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g' | sed 1q) + postponed=$(grep -i "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g' | sed 1q) + trash=$(grep -i "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g' | sed 1q) sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc" + { echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc" echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc" printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n" sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc" - grep -i "$tmpdir/$title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title" - grep -i "$tmpdir/$title_boxes" -e sent | sed 1q | formatShortcut s sent "$title" - grep -i "$tmpdir/$title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title" - grep -i "$tmpdir/$title_boxes" -e trash | sed 1q | formatShortcut t trash "$title" - grep -i "$tmpdir/$title_boxes" -e spam | sed 1q | formatShortcut S spam "$title" - grep -i "$tmpdir/$title_boxes" -e junk | sed 1q | formatShortcut j junk "$title" - grep -i "$tmpdir/$title_boxes" -e archive | sed 1q | formatShortcut a archive "$title" + grep -i "$tmpdir/title_boxes" -e inbox | sed 1q | formatShortcut i inbox "$title" + grep -i "$tmpdir/title_boxes" -e sent | sed 1q | formatShortcut s sent "$title" + grep -i "$tmpdir/title_boxes" -e draft | sed 1q | formatShortcut d drafts "$title" + grep -i "$tmpdir/title_boxes" -e trash | sed 1q | formatShortcut t trash "$title" + grep -i "$tmpdir/title_boxes" -e spam | sed 1q | formatShortcut S spam "$title" + grep -i "$tmpdir/title_boxes" -e junk | sed 1q | formatShortcut j junk "$title" + grep -i "$tmpdir/title_boxes" -e archive | sed 1q | formatShortcut a archive "$title" printf "All done.\\n" } -#wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;} +wipe () { rm -rf "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;} [ -z "$gpgemail" ] && askgpg pick() { \ - grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl > "$tmpdir/numbered" - cat "$tmpdir/numbered" - print "Select (an) account(s) to %s.\\n" "$1" + numbered="$(grep "^accounts *=" "$HOME/.config/offlineimap/config" | sed 's/accounts *= *//g;s/,/ /g;s/ \+/\n/g' | nl)"# > "$tmpdir/numbered" + printf "Select (an) account(s) to %s.\\n" "$1" + [ -z "$numbered" ] || return 1 + echo "$numbered" read -r input - title="$(cat "$tmpdir/numbered" | grep "$input" | awk '{print $2}')" - [ -z "$title" ] && printf "Invalid response." && exit 1 + title="$(echo "$numbered" | grep "$input" | awk '{print $2}')" + [ -z "$title" ] && printf "Invalid response." && return 1 [ ! -z "$2" ] && printf "Are you sure you want to %s the \`%s\` account?" "$1" "$title" && read -r input && echo "$input" | grep -i "y\(es\)*" >/dev/null } -removeAccount() { sed -ie " +delete() { sed -i " /Account $1]/,/Account/{//!d} /Account $1]/d s/ $1\(,\|$\)//g s/=$1\(,\|$\)/=/g s/,$//g " "$HOME/.config/offlineimap/config" - rm "$muttdir/accounts/$1.muttrc" - rm "$muttdir/credentials/$1.gpg" - rm -rf "$muttdir/accounts/$1" + rm -rf "$accdir/$1" "$creddir/$1.gpg" "$accdir/$1.muttrc" sed -i "/$1.muttrc/d" "$muttdir/personal.muttrc" # Delete from the line matching the account name, until the next account or empty line sed -i "/account $1/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config";}