diff --git a/bin/mw b/bin/mw index ba5b8d7..b730a85 100755 --- a/bin/mw +++ b/bin/mw @@ -27,7 +27,7 @@ mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config} # Main mutt config location mwmuttdir="$mwconfigdir/mutt" # Directory for account settings -mwaccrcdir="$mwmuttdir/accounts" +mwaccmuttdir="$mwmuttdir/accounts" # Location of mail storage mwmaildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}" # Regex to confirm valid email address @@ -58,7 +58,7 @@ do done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;} getaccounts() { - accounts="$(find "$mwaccrcdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)" + accounts="$(find "$mwaccmuttdir" -type f | grep -o "[0-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)" } mwlist() { @@ -202,7 +202,7 @@ EOF getpass fi getprofiles - mkdir -p "$mwmuttdir" "$mwaccrcdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}" + mkdir -p "$mwmuttdir" "$mwaccmuttdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}" [ ! -f "$mwmsmtprc" ] && echo "$msmtp_header" > "$mwmsmtprc" echo "$msmtp_profile" >> "$mwmsmtprc" case "$mwservice" in @@ -212,14 +212,14 @@ EOF # new idnum = first one missing getaccounts for x in $(seq 1 9); do echo "$accounts" | grep "$x": >/dev/null 2>&1 || { export idnum="$x"; break ;}; done - mwaccrc="$mwaccrcdir/$idnum-$mwacc.muttrc" - echo "$mutt_profile" > "$mwaccrc" + mwaccmuttrc="$mwaccmuttdir/$idnum-$mwacc.muttrc" + echo "$mutt_profile" > "$mwaccmuttrc" [ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created." ! grep "source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc" if [ "$mwtype" = "offline" ]; then ! grep "^macro .* gM .*" "$mwmuttrc" >/dev/null && echo "macro index gM 'mailsync -Va' \"sync all mail\" # mw-autogenerated" >> "$mwmuttrc" fi - ! grep "^source.*$mwaccrc" "$mwmuttrc" >/dev/null && echo "source $mwaccrc # mw-autogenerated" >> "$mwmuttrc" + ! grep "^source.*$mwaccmuttrc" "$mwmuttrc" >/dev/null && echo "source $mwaccmuttrc # mw-autogenerated" >> "$mwmuttrc" return 0 } @@ -233,12 +233,6 @@ getpass() { while : ; do pass rm -f "$mwpass" >/dev/null 2>&1 pass insert "$mwpass" && break; done ;} -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"; } >> "$mwaccrc" - done -} tryconnect() { if [ -z "$mailboxes" ]; then @@ -249,8 +243,19 @@ tryconnect() { mailboxes="$($mbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1 fi if [ -n "$mailboxes" ]; then + spoolfile=$(echo "$mailboxes" | grep -i -m 1 inbox | sed -ne 's/.*/+\0/p') + [ -z "$spoolfile" ] && return 1 + + #make directories printf "\033[32mMailboxes detected.\033[0m\\n" - echo "$mailboxes" | xargs -I {} mkdir -p "$mwaccmaildir/{}" + echo "$mailboxes" | xargs -I {} mkdir -p "$mwaccmaildir/{}/"{cur,new,tmp} + record=$(echo "$mailboxes" | grep -i -m 1 sent | sed -ne 's/.*/+\0/p') + [ -z "$record" ] && mkdir -p "$mwaccmaildir/Sent/"{cur,new,tmp} && record="Sent" + postponed=$(echo "$mailboxes" | grep -i -m 1 draft | sed -ne 's/.*/+\0/p') + [ -z "$postponed" ] && mkdir -p "$mwaccmaildir/Drafts/"{cur,new,tmp} && postponed="Drafts" + trash=$(echo "$mailboxes" | grep -i -m 1 trash | sed -ne 's/.*/+\0/p') + [ -z "$trash" ] && mkdir -p "$mwaccmaildir/Trash/"{cur,new,tmp} && trash="Trash" + return 0 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" @@ -258,41 +263,41 @@ tryconnect() { fi } -finalize() { - sed -i "/# mw-autogenerated/d" "$mwaccrc" - sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$mwaccrc" - idnum=${mwaccrc%%-*} - idnum=${idnum##*/} - muttsync=$(printf 'source %s!;' $mwaccrc) - echo "macro index,pager i$idnum '$muttsync' \"switch to $mwaddr\" # mw-autogenerated" >> "$mwaccrc" - command -V urlview >/dev/null 2>&1 && [ ! -f "$HOME/.urlview" ] && echo "COMMAND \$BROWSER" > "$HOME/.urlview" - echo "folder-hook \$folder '$muttsync' # mw-autogenerated" >> "$mwaccrc" - boxes="$(find "$mwaccmaildir/" -mindepth 1 -maxdepth 1 | sed "s/\ /\\\ /g;s/^.*\//=/")" - if [ "$boxes" = "" ]; then +finalize() { # new mwaccmuttrc + idnum=${mwaccmuttrc%%-*} + idnum=${idnum##*/} + boxes="$(find "$mwaccmaildir/" -name cur | sed "s,$mwaccmaildir/,,g;s,/cur,,")" + if [[ "$boxes" =~ ^[[:space:]]*$ ]]; then 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" "$mwacc" return fi - printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n" - spoolfile=$(echo "$boxes" | grep -i -m 1 inbox | sed 's/=/+/g') - record=$(echo "$boxes" | grep -i -m 1 sent | sed 's/=/+/g') - postponed=$(echo "$boxes" | grep -i -m 1 draft | sed 's/=/+/g') - trash=$(echo "$boxes" | grep -i -m 1 trash | sed 's/=/+/g') - cat >> "$mwaccrc" <source %s!;' $mwaccmuttrc) + cat >> "$mwaccmuttrc" <> "$mwaccrc" - printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n" - echo "$boxes" | grep -i inbox | head -n 1 | formatShortcut i inbox - echo "$boxes" | grep -i sent | head -n 1 | formatShortcut s sent - echo "$boxes" | grep -i draft | head -n 1 | formatShortcut d drafts - echo "$boxes" | grep -i trash | head -n 1 | formatShortcut t trash - echo "$boxes" | grep -i spam | head -n 1 | formatShortcut S spam - echo "$boxes" | grep -i junk | head -n 1 | formatShortcut j junk - echo "$boxes" | grep -i archive | head -n 1 | formatShortcut a archive + for eqbox in $boxes; do + box=${eqbox##*/} + box=${box##*.} + boxi=${box::2} + boxi=${boxi,,} + cat >> $mwaccmuttrc <=$eqbox" "go to $box" +macro index,pager M$boxi "=$eqbox" "move mail to $box" +macro index,pager C$boxi "=$eqbox" "copy mail to $box" +EOF + done if [ "$mwtype" = "offline" ]; then notmuchauto printf "All done.\\n\033[33mYou can now run \`\033[32mmailsync [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwacc" @@ -323,7 +328,7 @@ mwpick() { mwdelete() { sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc" rm -rf "$mwacccachedir" - rm -rf "$mwaccrcdir/"[1-9]"-$mwacc.muttrc" + rm -rf "$mwaccmuttdir/"[1-9]"-$mwacc.muttrc" sed -i "/[0-9]-$mwacc.muttrc/d" "$mwmuttrc" sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc" } @@ -367,7 +372,7 @@ asktype() { mwpurge() { confirm "delete all account data" || exit - rm -rf "$mwmbsyncrc" "$mwaccrcdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}" "$mwcachedir" + rm -rf "$mwmbsyncrc" "$mwaccmuttdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}" "$mwcachedir" pgrep cron >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null sed -i "/\# mw-autogenerated/d" "$mwmuttrc" echo "All configs and account settings have been purged." diff --git a/test/test_mw.bats b/test/test_mw.bats index 3100cb0..0e8f043 100644 --- a/test/test_mw.bats +++ b/test/test_mw.bats @@ -46,7 +46,7 @@ teardown() @test "add online" { export mwtype=online rm -rf mwtesttmp - export mailboxes="[Gmail]/Drafts" + export mailboxes="[Gmail]/INBOX" run mwadd [ -f mwtesttmp/config/mutt/muttrc ] [ -f mwtesttmp/config/mutt/accounts/1-$mwaddr.muttrc ] @@ -71,7 +71,7 @@ teardown() #4 @test "add offline successfully" { export mwtype=offline - export mailboxes="[Gmail]/Drafts" + export mailboxes="[Gmail]/INBOX" rm -rf mwtesttmp run mwadd [ -f mwtesttmp/config/mutt/muttrc ]