Browse Source

use [iCM]xy as mailbox shortcuts, list subfolders in mailboxes

pull/276/head
Roland Puntaier 5 years ago
parent
commit
87c646fa94
2 changed files with 48 additions and 43 deletions
  1. +46
    -41
      bin/mw
  2. +2
    -2
      test/test_mw.bats

+ 46
- 41
bin/mw View File

@@ -27,7 +27,7 @@ mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
# Main mutt config location # Main mutt config location
mwmuttdir="$mwconfigdir/mutt" mwmuttdir="$mwconfigdir/mutt"
# Directory for account settings # Directory for account settings
mwaccrcdir="$mwmuttdir/accounts"
mwaccmuttdir="$mwmuttdir/accounts"
# Location of mail storage # Location of mail storage
mwmaildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}" mwmaildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
# Regex to confirm valid email address # 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 ;} done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}


getaccounts() { 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() { mwlist() {
@@ -202,7 +202,7 @@ EOF
getpass getpass
fi fi
getprofiles getprofiles
mkdir -p "$mwmuttdir" "$mwaccrcdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}"
mkdir -p "$mwmuttdir" "$mwaccmuttdir" "$mwconfigdir/msmtp" "${mwmbsyncrc%/*}"
[ ! -f "$mwmsmtprc" ] && echo "$msmtp_header" > "$mwmsmtprc" [ ! -f "$mwmsmtprc" ] && echo "$msmtp_header" > "$mwmsmtprc"
echo "$msmtp_profile" >> "$mwmsmtprc" echo "$msmtp_profile" >> "$mwmsmtprc"
case "$mwservice" in case "$mwservice" in
@@ -212,14 +212,14 @@ EOF
# new idnum = first one missing # new idnum = first one missing
getaccounts getaccounts
for x in $(seq 1 9); do echo "$accounts" | grep "$x": >/dev/null 2>&1 || { export idnum="$x"; break ;}; done 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." [ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created."
! grep "source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc" ! grep "source.*mutt-wizard.muttrc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc"
if [ "$mwtype" = "offline" ]; then if [ "$mwtype" = "offline" ]; then
! grep "^macro .* gM .*" "$mwmuttrc" >/dev/null && echo "macro index gM '<shell-escape>mailsync -Va<enter>' \"sync all mail\" # mw-autogenerated" >> "$mwmuttrc" ! grep "^macro .* gM .*" "$mwmuttrc" >/dev/null && echo "macro index gM '<shell-escape>mailsync -Va<enter>' \"sync all mail\" # mw-autogenerated" >> "$mwmuttrc"
fi 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 return 0
} }


@@ -233,12 +233,6 @@ getpass() {
while : ; do pass rm -f "$mwpass" >/dev/null 2>&1 while : ; do pass rm -f "$mwpass" >/dev/null 2>&1
pass insert "$mwpass" && break; done ;} pass insert "$mwpass" && break; done ;}


formatShortcut() {
while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"go to $2\" # mw-autogenerated"
echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"move mail to $2\" # mw-autogenerated"
echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"copy mail to $2\" # mw-autogenerated"; } >> "$mwaccrc"
done
}


tryconnect() { tryconnect() {
if [ -z "$mailboxes" ]; then if [ -z "$mailboxes" ]; then
@@ -249,8 +243,19 @@ tryconnect() {
mailboxes="$($mbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1 mailboxes="$($mbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1
fi fi
if [ -n "$mailboxes" ]; then 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" 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 return 0
else 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" 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 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 '<sync-mailbox><enter-command>source %s<enter><change-folder>!<enter>;<check-stats>' $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" 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 return
fi 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" <<EOF
mwaccmutt="${email//[.@]/_}"
muttsync=$(printf '<sync-mailbox><enter-command>source %s<enter><change-folder>!<enter>;<check-stats>' $mwaccmuttrc)
cat >> "$mwaccmuttrc" <<EOF

set spoolfile = "$spoolfile" set spoolfile = "$spoolfile"
set record = "$record" set record = "$record"
set postponed = "$postponed" set postponed = "$postponed"
set trash = "$trash" set trash = "$trash"

folder-hook \$folder '$muttsync'
macro index,pager i$idnum '$muttsync' "switch to $email"

mailboxes =$mwaccmutt ===================== $(echo "$boxes" | sed -e "s/ //g;s/.*/\"=\0\"/g" | tr "\n" " ")
EOF EOF
echo "mailboxes =$mwaccmutt ===================== $(echo "$boxes" | sed -e "s/^\|$/\"/g" | tr "\n" " ")" >> "$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 <<EOF

macro index,pager i$boxi "<change-folder>=$eqbox<enter>" "go to $box"
macro index,pager M$boxi "<save-message>=$eqbox<enter>" "move mail to $box"
macro index,pager C$boxi "<copy-message>=$eqbox<enter>" "copy mail to $box"
EOF
done
if [ "$mwtype" = "offline" ]; then if [ "$mwtype" = "offline" ]; then
notmuchauto notmuchauto
printf "All done.\\n\033[33mYou can now run \`\033[32mmailsync [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwacc" 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() { mwdelete() {
sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc" sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
rm -rf "$mwacccachedir" 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 "/[0-9]-$mwacc.muttrc/d" "$mwmuttrc"
sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc" sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
} }
@@ -367,7 +372,7 @@ asktype() {


mwpurge() { mwpurge() {
confirm "delete all account data" || exit 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 pgrep cron >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null
sed -i "/\# mw-autogenerated/d" "$mwmuttrc" sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
echo "All configs and account settings have been purged." echo "All configs and account settings have been purged."


+ 2
- 2
test/test_mw.bats View File

@@ -46,7 +46,7 @@ teardown()
@test "add online" { @test "add online" {
export mwtype=online export mwtype=online
rm -rf mwtesttmp rm -rf mwtesttmp
export mailboxes="[Gmail]/Drafts"
export mailboxes="[Gmail]/INBOX"
run mwadd run mwadd
[ -f mwtesttmp/config/mutt/muttrc ] [ -f mwtesttmp/config/mutt/muttrc ]
[ -f mwtesttmp/config/mutt/accounts/1-$mwaddr.muttrc ] [ -f mwtesttmp/config/mutt/accounts/1-$mwaddr.muttrc ]
@@ -71,7 +71,7 @@ teardown()
#4 #4
@test "add offline successfully" { @test "add offline successfully" {
export mwtype=offline export mwtype=offline
export mailboxes="[Gmail]/Drafts"
export mailboxes="[Gmail]/INBOX"
rm -rf mwtesttmp rm -rf mwtesttmp
run mwadd run mwadd
[ -f mwtesttmp/config/mutt/muttrc ] [ -f mwtesttmp/config/mutt/muttrc ]


Loading…
Cancel
Save