From 2649f5d506b41d66dfddef9b1931945b98334896 Mon Sep 17 00:00:00 2001 From: Roland Puntaier Date: Tue, 27 Aug 2019 23:24:28 +0200 Subject: [PATCH] further fixes and refactoring --- bin/mw | 70 ++++++++++++++++++++++++++++++----------------- test/test_mw.bats | 2 ++ 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/bin/mw b/bin/mw index bf1d5bf..6724330 100755 --- a/bin/mw +++ b/bin/mw @@ -51,13 +51,19 @@ do [ -f "$x" ] && sslcert="$x" && break done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;} -getaccounts() { accounts="$(find "$mwaccdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\..*//" | sort -n)" ;} +getaccounts() { + accounts="$(find "$mwaccdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)" +} -mwlist() { getaccounts && [ -n "$accounts" ] && echo "$accounts" ;} +mwlist() { + getaccounts && [ -n "$accounts" ] && echo "$accounts" +} -mwadd() { asktype && askinfo && tryconnect && finalize || mwdelete ;} +mwadd() { + asktype && askinfo && tryconnect && finalize || mwdelete +} -getprofiles() { \ +getprofiles() { unset msmtp_header msmtp_profile mutt_profile mbsync_profile printf "Creating profiles for \`%s\`..." "$mwaccount" msmtp_header="defaults @@ -136,7 +142,7 @@ fi printf "DONE.\\n" } -askinfo() { \ +askinfo() { if [ -z $mwaddr ]; then printf "Type the \033[31memail address\033[0m\\n\t\033[36m" read -r mwaddr @@ -207,21 +213,24 @@ EOF return 0 } -protonfinger() { printf "Getting Protonmail bridge fingerprint...\\n" +protonfinger() { + printf "Getting Protonmail bridge fingerprint...\\n" fingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1 sed -i "s/account $mwaccount/&\ntls_trust_file\ntls_fingerprint $fingerprint/" "$mwmsmtprc" } -getpass() { while : ; do pass rm -f "$mwpass" >/dev/null 2>&1 +getpass() { + while : ; do pass rm -f "$mwpass" >/dev/null 2>&1 pass insert "$mwpass" && break; done ;} -formatShortcut() { \ +formatShortcut() { while read -r data; do { echo "macro index,pager g$1 \"$data\" \"go to $2\" # mw-autogenerated" echo "macro index,pager M$1 \"$data\" \"move mail to $2\" # mw-autogenerated" echo "macro index,pager C$1 \"$data\" \"copy mail to $2\" # mw-autogenerated"; } >> "$mwaccdir/$idnum-$mwaccount.muttrc" - done ;} + done +} -tryconnect() { \ +tryconnect() { if [ -z "$mailboxes" ]; then mkdir -p $mwmaildir/$mwaccount mailboxes="$($mbsyncbin -l $mwaccount | sed 's/\//./')" >/dev/null 2>&1 @@ -235,9 +244,10 @@ tryconnect() { \ else printf "\033[31m\033[31mLog-on not successful.\033[0m\\nIt seems that either you inputted the wrong password or server settings, or there are other requirements for your account out of the control of mutt-wizard.\\n" return 1 - fi ;} + fi +} -finalize() { \ +finalize() { boxes="$(find "$mwmaildir/$mwaccount/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")" [ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that mbsync has not been successfully run.\\nRun mbsync, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$mwaccount" && return printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n" @@ -262,24 +272,32 @@ finalize() { \ return 0 } -confirm() { printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 - return 0 ;} +confirm() { + printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1 + return 0 ; +} -mwpick() { printf "Select an accounts to %s:\\n" "$1" +mwpick() { + printf "Select an accounts to %s:\\n" "$1" mwlist read -r input [ -z "$input" ] && return 1 - mwaccount="$(echo "$accounts" | grep "$input" | awk '{print $2}')" + mwaddr="$(echo "$accounts" | grep "$input": | awk '{print $2}')" + mwaccount=$mwaddr [ -z "$mwaccount" ] && printf "Invalid response." && return 1 - return 0 ;} + return 0 ; +} -mwdelete() { sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc" - rm -rf "${mwcachedir:?}/${mwaccount:?}" "$mwaccdir/"[1-9]"-$mwaccount.muttrc" +mwdelete() { + sed -i "/IMAPStore $mwaccount-remote$/,/# End profile/d" "$mwmbsyncrc" + rm -rf "${mwcachedir:?}/${mwaccount:?}" + rm -rf "$mwaccdir/"[1-9]"-$mwaccount.muttrc" sed -i "/[0-9]-$mwaccount.muttrc/d" "$mwmuttrc" sed -i "/account $mwaccount/,/^\(\s*$\|account\)/d" "$mwmsmtprc" - } +} -mwcron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1 +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 mailsync >/dev/null; then echo "Active mail sync cronjob detected. Do you want to remove it?" printf "\033[36m\t" @@ -298,9 +316,10 @@ mwcron() { ! pgrep cron >/dev/null && echo "No cron manager running. Install/ena done (crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mailsync | cut -d' ' -f3)") | crontab - && echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running." - fi ;} + fi +} -asktype() { \ +asktype() { if [ -z "$mwtype" ]; then while : ; do printf "[yes/no]: Local mail via mbsync? No: Mutt remotes (slower)\\n\t" @@ -322,7 +341,7 @@ mwpurge() { echo "All configs and account settings have been purged." } -notmuchauto() { \ +notmuchauto() { [ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config" [ -f "$NOTMUCH_CONFIG" ] && return 0 nmbasic="[database] @@ -340,7 +359,8 @@ synchronize_flags=true [crypto] gpg_path=$GPG" mkdir -p ${NOTMUCH_CONFIG%/*} - echo "$nmbasic" > "$NOTMUCH_CONFIG" ;} + echo "$nmbasic" > "$NOTMUCH_CONFIG" +} trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL diff --git a/test/test_mw.bats b/test/test_mw.bats index 68d71f5..3100cb0 100644 --- a/test/test_mw.bats +++ b/test/test_mw.bats @@ -81,5 +81,7 @@ teardown() cat mwtesttmp/config/isync/mbsyncrc | sed -ne '/^\s*\w/p' [ ! "$(cat mwtesttmp/config/isync/mbsyncrc | sed -ne '/^\s*\w/p')" = "" ] [ ! "$(cat mwtesttmp/config/msmtp/config | sed -ne '/^account/p')" = "" ] + run mwlist + [ "$(echo $lines | awk '{print $2}')" = "$mwaddr" ] }