Browse Source

integrate mailsync into mw

pull/276/head
Roland Puntaier 5 years ago
parent
commit
758f4b7c2c
9 changed files with 518 additions and 436 deletions
  1. +3
    -11
      README.md
  2. +0
    -80
      bin/mailsync
  3. +0
    -11
      bin/muttimage
  4. +495
    -315
      bin/mw
  5. +0
    -8
      bin/openfile
  6. +12
    -3
      mw.1
  7. +3
    -3
      share/mailcap
  8. +2
    -2
      test/dotests
  9. +3
    -3
      test/test_mw.bats

+ 3
- 11
README.md View File

@@ -26,15 +26,7 @@ sudo make install

User of Arch-based distros can also install mutt-wizard from the AUR as [mutt-wizard-git](https://aur.archlinux.org/packages/mutt-wizard-git/).

*NOTE:* If you have used an older version of mutt-wizard, especially when it used to use `offlineimap`, you need to remove your old configs.
Back up what's important and run:

```
rm -rf ~/.config/mutt ~/.msmtprc ~/.config/msmtp ~/.offlineimap ~/.offlineimaprc ~/.config/offlineimap ~/.mbsyncrc
```

The mutt-wizard is run with the command `mw`.
It also installs the `mailsync` command.
Once everything is setup, you'll use `neomutt` to access your mail.

- `mw add` -- add a new email account
@@ -63,8 +55,8 @@ If you get errors in `neomutt`, install the most recent version manually or manu
Although, `mw` will do this automatically, if you haven't set notmuch up before.
You can run it in mutt with `S`.
Run `notmuch new` to process new mail.
Although, the included `mailsync` script does this for you.
- `libnotify`/`libnotify-bin` - allows notifications when syncing mail with `mailsync`
Although, `mw sync` does this for you.
- `libnotify`/`libnotify-bin` - allows notifications when syncing mail with `mw sync`
- `abook` - a terminal-based address book.
Pressing tab while typing an address to send mail to will suggest contacts that are in your abook.
- A cron manager - if you want to enable the auto-sync feature.
@@ -96,7 +88,7 @@ To give you an example of the interface, here's an idea:
- `S` - search for a mail
- `gl` - limit by substring of subject
- `gL` - undo limit
- `gm / gM` - call mutt-wizard's mailsync for one / all mail accounts
- `gm / gM` - call mutt-wizard's `mw sync` for one / all mail accounts
- `^u` within input field / command line, will clear it, `^a` and `^e` go to beginning or end, `^g` aborts

Look into `/usr/share/mutt-wizard.muttrc` to see all bindings.


+ 0
- 80
bin/mailsync View File

@@ -1,80 +0,0 @@
#!/usr/bin/env sh
# Sync mail and give notification if there is new mail.

case "$(uname)" in
Linux) prefix="/usr" ;;
*) prefix="/usr/local" ;;
esac

export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
export DISPLAY=:0.0
if [[ -z $PASSWORD_STORE_DIR ]]; then
[ -d "$HOME/.local/share/password-store" ] && export PASSWORD_STORE_DIR="$HOME/.local/share/password-store"
fi
mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
mwmaildir="${MAILDIR:-$HOME/Mail}"
mwlastrun=${XDG_CACHE_HOME:-$HOME/.cache}/.mailsynclastrun
mwmbsyncrc="$mwconfigdir/isync/mbsyncrc"
mwmbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"

# Run only if user logged in (prevent cron errors)
pgrep -u "$USER" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
# Run only if not already running in other instance
pgrep -x mbsync >/dev/null && { echo "mbsync is running." ; exit ;}
pgrep -x notmuch >/dev/null && { echo "notmuch is running." ; exit ;}

# Checks for internet connection and set notification script.
ping -q -c 1 1.1.1.1 > /dev/null || { echo "No internet connection detected."; exit ;}
command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script."

# Settings are different for MacOS (Darwin) systems.
if [ "$(uname)" = "Darwin" ]; then
notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $1\"" && sleep 2 ;}
else
notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
fi

# Check account for new mail. Notify if there is new content.
syncandnotify() {
mwacc="$(echo "$mwaccount" | sed "s/.*\///")"
mwaccmaildir="$mwmaildir/$mwacc"
$mwmbsyncbin "$mwacc"
mwnew=$(find "$mwaccmaildir/INBOX/new/" "$mwaccmaildir/Inbox/new/" "$mwaccmaildir/inbox/new/" -type f -newer "$mwlastrun" 2> /dev/null)
newcount=$(echo "$mwnew" | sed '/^\s*$/d' | wc -l)
if [ "$newcount" -gt "0" ]; then
notify "$mwacc" "$newcount" &
for file in $mwnew; do
# Extract subject and sender from mail.
from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n-1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
notify-send "📧$from:" "$subject" &
done
fi
}

# Sync accounts passed as argument or all.
if [ "$#" -eq "0" ]; then
mwaccounts="$(awk '/^Group .*@.*/ {print $2}' "$mwmbsyncrc")"
mwaccounts+=" $(awk '/^Channel .*@.*/ {print $2}' "$mwmbsyncrc")"
else
mwaccounts=$*
fi

echo " 🔃" > /tmp/imapsyncicon_"$USER"
pkill -RTMIN+12 i3blocks >/dev/null 2>&1

# Parallelize multiple accounts
for mwaccount in $mwaccounts
do
syncandnotify &
done

wait
rm -f /tmp/imapsyncicon_"$USER"
pkill -RTMIN+12 i3blocks >/dev/null 2>&1

notmuch new 2>/dev/null

#Create a touch file that indicates the time of the last run of mailsync
touch "$mwlastrun"

+ 0
- 11
bin/muttimage View File

@@ -1,11 +0,0 @@
#!/bin/bash

#get image resolution
resolution=$(identify $1 | awk '{print $3}')
IFS='x' # x is set as delimiter
read -ra ADDR <<< "$resolution"
width=${ADDR[0]}
height=${ADDR[1]}

### Display Image / offset with mutt bar
echo -e "2;3;\n0;1;0;42;$((width));$((height));0;0;0;0;$1\n4;\n3;" | /usr/lib/w3m/w3mimgdisplay &

+ 495
- 315
bin/mw View File

@@ -1,80 +1,70 @@
#!/bin/sh

if [ "$#" -gt 1 ]; then
echo "To many arguments. You will be asked."
exit
fi

if [ -z "$prefix" ]; then
case "$(uname)" in
Linux) prefix="/usr" ;;
*) prefix="/usr/local" ;;
esac
case "$(uname)" in
Linux) prefix="/usr" ;;
*) prefix="/usr/local" ;;
esac
fi

command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
[ -z "$PASSWORD_STORE_DIR" ] && PASSWORD_STORE_DIR="$HOME/.password-store"
[ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
"$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`$GPG --full-gen-key\` first.\\n"
exit
}
! command -v "$prefix/bin/mbsync" >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
! command -v "$prefix/bin/msmtp" >/dev/null && printf "\`msmtp\` must be installed.\\n" && exit
! ( command -v mutt >/dev/null || command -v neomutt >/dev/null ) && printf "\`mutt\` must be installed.\\n" && exit

mwconfigdir=${XDG_CONFIG_HOME:-$HOME/.config}
# Main mutt config location
mwmuttdir="$mwconfigdir/mutt"
mwmuttrc="$mwmuttdir/muttrc"
# Directory for account settings
mwaccmuttdir="$mwmuttdir/accounts"
# Generated at every full sync
mwgenmuttaccs=$mwmuttdir/mw_generated.muttrc
# Location of mail storage
mwmaildir="${MAILDIR:-$HOME/mail}"
# Regex to confirm valid email address
mwemailre=".\+@.\+\\..\+"
mwshare="$prefix/share/mutt-wizard"
mwmuttrc="$mwmuttdir/muttrc"
mwmbsyncrc="$mwconfigdir/isync/mbsyncrc"
mwgetmaildir=$mwconfigdir/getmail #POP3
mwmsmtprc="$mwconfigdir/msmtp/config"
mwsharerc="$mwshare/mutt-wizard.muttrc"
mwcachedir="${XDG_CACHE_HOME:-$HOME/.cache}/mutt-wizard"
mwssltype="IMAPS"
mbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"
mwmbsyncbin="$prefix/bin/mbsync -c $mwmbsyncrc"
msmtpbin="$prefix/bin/msmtp"

mwtype=offline

_mwaddrmwtype(){
mwacc="$mwaddr" # let the user always just deal with his email
mwaccmaildir="$mwmaildir/$mwacc" # mail dir is $MAILDIR/email
mwacccachedir=$mwcachedir/${mwaddr//[.@]/_} # @ cannot stay because of mutt, . could
mwaccmuttrc="$mwaccmuttdir/$mwidnum-$mwacc.mwonofftype.$mwtype.muttrc"
mwpass=mutt-wizard-$mwaddr
[ -n "$mwidnum" ] && mwaccmuttrc="$mwaccmuttdir/$mwidnum-$mwacc.mwonofftype.$mwtype.muttrc"
mwacc="$mwaddr" # let the user always just deal with his email
mwaccmaildir="$mwmaildir/$mwaddr" # mail dir is $MAILDIR/email
mwacccachedir=$mwcachedir/${mwaddr//[.@]/_} # @ cannot stay because of mutt, . could
mwpass=mutt-wizard-$mwaddr
[ -n "$mwidnum" ] && mwaccmuttrc="$mwaccmuttdir/$mwidnum-$mwacc.mwonofftype.$mwtype.muttrc"
}

for mwcrt 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
[ -f "$mwcrt" ] && mwsslcert="$mwcrt" && break
done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && exit 1 ;}

_mwgetaccounts() {
mwaccounts="$(find "$mwaccmuttdir" -type f | grep -o "[1-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)"
_mwaccs() {
mwaccs="$(find "$mwaccmuttdir" -type f | grep -o "[1-9]-.*.muttrc" | sed "s/-/: /;s/\.muttrc//" | sort -n)"
}

_mwlist() {
_mwgetaccounts && [ -n "$mwaccounts" ] && echo "${mwaccounts//.mwonofftype./ }"
_mwaccs && [ -n "$mwaccs" ] && echo "${mwaccs//.mwonofftype./ }"
}

_mwadd() {
_mwasktype && _mwaskinfo && _mwtryconnect && _mwfinalize || _mwdelete
_mwcheckinternet && _mwasktype && _mwaskinfo && _mwtryconnect && _mwfinalize || _mwdelete
}

_mwgetprofiles() {
unset mwmsmtpheader mwmsmtpprofile mwmuttprofile mwmbsyncprofile
printf "Creating profiles for \`%s\`..." "$mwaddr"
unset mwmsmtpheader mwmsmtpprofile mwmbsyncprofile

for mwcrt 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
[ -f "$mwcrt" ] && mwsslcert="$mwcrt" && break
done || { echo "CA Certificate not found. Please install one or link it to /etc/ssl/certs/ca-certificates.crt" && return 1 ;}

printf "Creating profiles for \`%s\`..." "$mwaddr"
mwmsmtpheader="defaults
auth on
tls on
tls_trust_file $mwsslcert
logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
auth on
tls on
tls_trust_file $mwsslcert
logfile ${XDG_LOG_HOME:-$HOME}/msmtp.log
"
mwmsmtpprofile="account $mwacc
host $mwsmtp
@@ -85,8 +75,8 @@ passwordeval \"pass $mwpass\"
$mwstarttlsoff
"
mwmbsyncprofile="IMAPStore $mwacc-remote
Host $mwimap
Port $mwiport
Host $mwserver
Port $mwport
User $mwlogin
PassCmd \"pass $mwpass\"
SSLType $mwssltype
@@ -108,294 +98,246 @@ SyncState *
MaxMessages 0
# End profile
"

if [ "$mwtype" = "offline" ]; then
mwmuttprofile="# vim: filetype=neomuttrc
# muttrc file for account $mwaddr
set realname = \"$mwname\"
set from = \"$mwaddr\"
set sendmail = \"$msmtpbin -a $mwacc\"
alias me $mwname <$mwaddr>
set folder = \"$mwaccmaildir\"
set mbox_type = Maildir

macro index gm \"<shell-escape>mailsync $mwacc<enter>\" \"sync mail $mwaddr\"

unmailboxes *
"
else
mwmuttprofile="# vim: filetype=neomuttrc
# muttrc file for account $mwaddr
set realname = \"$mwname\"
set from = \"$mwaddr\"
set sendmail = \"$msmtpbin -a $mwacc\"
alias me $mwname <$mwaddr>
set folder = \"imaps://$mwaddr@$mwimap:$mwiport\"
set header_cache = \"$mwacccachedir\"
set message_cachedir = \$header_cache
set imap_user = \"$mwlogin\"
set imap_pass = \"\`pass $mwpass\`\"
account-hook \$folder 'set imap_user=\"$mwlogin\" imap_pass=\"\`pass $mwpass\`\"'

set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes

unmailboxes *
"
fi
printf "DONE.\\n"
printf "DONE.\\n"
}

_mwaskinfo() {
if [ -z "$mwaddr" ]; then
printf "Type the \033[31memail address\033[0m\\n\t\033[36m"
read -r mwaddr
printf "\033[0m"
while ! echo "$mwaddr" | grep "$mwemailre" >/dev/null; do
printf "That is not a valid \033[31memail address\033[0m, please retype\\n\t\033[36m"
read -r mwaddr
printf "\033[0m"
done
fi
mwdomain="$(echo "$mwaddr" | sed "s/.*@//")"
printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$mwdomain"
mwserverinfo="$(grep "^$mwdomain" "$mwshare/domains.csv" 2>/dev/null)"
if [ -z "$mwserverinfo" ]; then
printf "Your email domain is not known to mutt-wizard.\\nType in your settings.\\nUsually you find them by an internet search.\\n"
printf "Type the IMAP server (excluding the port number)\\n\033[36m\t"
read -r mwimap
printf "\033[0mIMAP port number (usually 993)\\n\033[36m\t"
read -r mwiport
printf "\033[0mSMTP server (excluding the port number)\\n\033[36m\t"
read -r mwsmtp
printf "\033[0mSMTP port number (usually 587 or 465)\\n\033[36m\t"
read -r mwsport
printf "\033[0m\\nIf you want, you can copy the line below and add it to the \`domains.csv\` file on Github, for others.\\n\\n%s,%s,%s,%s,%s\\n\\nBut be sure the setting works, first! ;-)\\n" "$mwdomain" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
else
IFS=, read -r mwservice mwimap mwiport mwsmtp mwsport <<EOF
if [ -z "$mwaddr" ]; then
printf "Type the \033[31memail address\033[0m\\n\t\033[36m"
read -r mwaddr
printf "\033[0m"
while ! echo "$mwaddr" | grep "$mwemailre" >/dev/null; do
printf "That is not a valid \033[31memail address\033[0m, please retype\\n\t\033[36m"
read -r mwaddr
printf "\033[0m"
done
fi
mwdomain="$(echo "$mwaddr" | sed "s/.*@//")"
printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$mwdomain"
mwserverinfo="$(grep "^$mwdomain" "$mwshare/domains.csv" 2>/dev/null)"
if [ -z "$mwserverinfo" ]; then
printf "Your email domain is not known to mutt-wizard.\\nType in your settings.\\nUsually you find them by an internet search.\\n"
printf "Type the IMAP/POP3 server (excluding the port number)\\n\033[36m\t"
read -r mwserver
printf "\033[0mIMAP port number (usually 993)\\n\033[36m\t"
read -r mwport
printf "\033[0mSMTP server (excluding the port number)\\n\033[36m\t"
read -r mwsmtp
printf "\033[0mSMTP port number (usually 587 or 465)\\n\033[36m\t"
read -r mwsport
printf "\033[0m\\nIf you want, you can copy the line below and add it to the \`domains.csv\` file on Github, for others.\\n\\n%s,%s,%s,%s,%s\\n\\nBut be sure the setting works, first! ;-)\\n" "$mwdomain" "$mwserver" "$mwport" "$mwsmtp" "$mwsport"
else
IFS=, read -r mwservice mwserver mwport mwsmtp mwsport <<EOF
$mwserverinfo
EOF
printf "\\n\033[3;33mCongrats!\033[0m Server info is known, so you don't need to look it up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\n" "$mwimap" "$mwiport" "$mwsmtp" "$mwsport"
case "$mwservice" in
gmail.com) printf "\033[31mGmail: \"less secure\" must be enabled before you continue.\\nDo it now, if you have not done it already:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
protonmail.ch|protonmail.com|pm.me) printf "\033[31mProtonmail: Users must install and configure Protonmail Bridge for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
esac
[ "$mwsport" = 465 ] && mwstarttlsoff="tls_starttls off"
fi
if [ -z "$mwname" ]; then
printf "Name to associate to email.\\n\t"
read -r mwname
fi
if [ -z "$mwlogin" ]; then
printf "Type your account username if different from your email address.\\n\033[34mFor most accounts you can probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
read -r mwlogin
printf "\033[0m"
[ -z "$mwlogin" ] && mwlogin="$mwaddr"
fi
# if the user has a pass entry he could provide it via mwpass
if [ -z "$mwpass" ]; then
mwpass=mutt-wizard-$mwaddr
_mwgetpass
fi
unset mwidnum
_mwaddrmwtype
_mwgetprofiles
mkdir -p "$mwmuttdir" "$mwaccmuttdir" "${mwmsmtprc%/*}" "${mwmbsyncrc%/*}"
if [ ! -f "$mwmsmtprc" ]; then
echo "$mwmsmtpheader" > "$mwmsmtprc"
else
sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
fi
echo "$mwmsmtpprofile" >> "$mwmsmtprc"
case "$mwservice" in
protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
esac
[ -f "$mwmbsyncrc" ] && sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
echo "$mwmbsyncprofile" >> "$mwmbsyncrc"
#mwaccmuttrc
mwaccmuttrc="$mwaccmuttdir/$(find "$mwaccmuttdir" -type f | grep -m 1 -o "[1-9]-$mwacc.mwonofftype.$mwtype.muttrc")"
if [[ ! -f "$mwaccmuttrc" ]]; then
# new mwidnum = first one missing
_mwgetaccounts
for mwx in $(seq 1 9); do echo "$mwaccounts" | grep "$mwx:" >/dev/null 2>&1 || { export mwidnum="$mwx"; break ;}; done
mwaccmuttrc="$mwaccmuttdir/$mwidnum-$mwacc.mwonofftype.$mwtype.muttrc"
else
mwidnum=${mwaccmuttrc%%-*}
mwidnum=${mwidnum##*/}
fi
echo "$mwmuttprofile" > "$mwaccmuttrc"
[ ! -f "$mwmuttrc" ] && echo "# vim: filetype=neomuttrc" > "$mwmuttrc" && echo "muttrc created."
! grep "source $mwsharerc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc"
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"
fi
! grep "^source.*$mwaccmuttrc" "$mwmuttrc" >/dev/null && echo "source $mwaccmuttrc # mw-autogenerated" >> "$mwmuttrc"
return 0
printf "\\n\033[3;33mCongrats!\033[0m Server info is known, so you don't need to look it up!\\n\t\033[1mIMAP server\033[0m: %s\\n\t\033[1mIMAP port\033[0m: %s\\n\t\033[1mSMTP server\033[0m: %s\\n\t\033[1mSMTP port\033[0m: %s\\n" "$mwserver" "$mwport" "$mwsmtp" "$mwsport"
case "$mwservice" in
gmail.com) printf "\033[31mGmail: \"less secure\" must be enabled before you continue.\\nDo it now, if you have not done it already:\\nhttps://support.google.com/accounts/answer/6010255\\n\033[0m" ;;
protonmail.ch|protonmail.com|pm.me) printf "\033[31mProtonmail: Users must install and configure Protonmail Bridge for the sync to work:\\nhttps://protonmail.com/bridge/\\n\033[0m" && ssltype="None" ;;
esac
[ "$mwsport" = 465 ] && mwstarttlsoff="tls_starttls off"
fi
if [ -z "$mwname" ]; then
printf "Name to associate to email.\\n\t"
read -r mwname
fi
if [ -z "$mwlogin" ]; then
printf "Type your account username if different from your email address.\\n\033[34mFor most accounts you can probably leave this blank.\033[0m\\n\tLogin(?): \033[36m"
read -r mwlogin
printf "\033[0m"
[ -z "$mwlogin" ] && mwlogin="$mwaddr"
fi
# if the user has a pass entry he could provide it via mwpass
if [ -z "$mwpass" ]; then
mwpass=mutt-wizard-$mwaddr
_mwgetpass
fi
_mwaddrmwtype
if [ "$mwtype" = "pop" ]; then
cat > "$mwgetmaildir/$mwaddr" << EOF
[retriever]
type = SimplePOP3SSLRetriever
server = $mwserver
username = $mwaddr
port = $mwport
password_command = ("pass", "$mwpass")

[destination]
type = Maildir
path = $mwaccmaildir/INBOX/

[options]
read_all = true
delete = true
message_log = ${XDG_LOG_HOME:-$HOME}/getmail.log

# vim: ft=conf
EOF
#from now on pop is lile offline
mwtype=offline
return 0
fi

_mwgetprofiles || return 1
mkdir -p "$mwmuttdir" "$mwaccmuttdir" "${mwmsmtprc%/*}" "${mwmbsyncrc%/*}"
if [ ! -f "$mwmsmtprc" ]; then
echo "$mwmsmtpheader" > "$mwmsmtprc"
else
sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
fi
echo "$mwmsmtpprofile" >> "$mwmsmtprc"
case "$mwservice" in
protonmail.ch|protonmail.com|pm.me) protonfinger || return 1 ;;
esac
[ -f "$mwmbsyncrc" ] && sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
echo "$mwmbsyncprofile" >> "$mwmbsyncrc"
[ ! -f "$mwmuttrc" ] && echo "# vim: filetype=muttrc" > "$mwmuttrc" && echo "muttrc created."
! grep "source $mwsharerc" "$mwmuttrc" >/dev/null && echo "source $mwsharerc # mw-autogenerated" >> "$mwmuttrc"
return 0
}

_mwprotonfinger() {
printf "Getting Protonmail bridge fingerprint...\\n"
mwfingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
sed -i "s/account $mwacc/&\ntls_trust_file\ntls_fingerprint $mwfingerprint/" "$mwmsmtprc"
printf "Getting Protonmail bridge fingerprint...\\n"
mwfingerprint="$($msmtpbin --serverinfo --host=127.0.0.1 --port=1025 --tls --tls-certcheck=off)" || return 1
sed -i "s/account $mwacc/&\ntls_trust_file\ntls_fingerprint $mwfingerprint/" "$mwmsmtprc"
}

_mwgetpass() {
while : ; do pass rm -f "$mwpass" >/dev/null 2>&1
pass insert "$mwpass" && break; done ;}
while : ; do pass rm -f "$mwpass" >/dev/null 2>&1
pass insert "$mwpass" && break; done ;}


_mwtryconnect() {
if [ ! -d "$mwaccmaildir" ]; then
mwaccmaildirWasThere="NO" # we need to remove again for "online"
mkdir -p "$mwaccmaildir"
fi
if [ -z "$mailboxes" ]; then
mailboxes="$($mbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1
fi
if [ -n "$mailboxes" ]; then
mwspoolfile=$(echo "$mailboxes" | grep -i -m 1 inbox | sed -ne 's/.*/+\0/p')
[ -z "$mwspoolfile" ] && return 1

#make directories
printf "\033[32mMailboxes detected.\033[0m\\n"
echo "$mailboxes" | xargs -I {} mkdir -p "$mwaccmaildir/{}/"{cur,new,tmp}
record=$(echo "$mailboxes" | grep -i -m 1 sent | sed -ne 's/.*/+\0/p')
[ -z "$mwrecord" ] && mkdir -p "$mwaccmaildir/Sent/"{cur,new,tmp} && record="Sent"
postponed=$(echo "$mailboxes" | grep -i -m 1 draft | sed -ne 's/.*/+\0/p')
[ -z "$mwpostponed" ] && mkdir -p "$mwaccmaildir/Drafts/"{cur,new,tmp} && postponed="Drafts"
trash=$(echo "$mailboxes" | grep -i -m 1 trash | sed -ne 's/.*/+\0/p')
[ -z "$mwtrash" ] && 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"
return 1
fi
if [ ! -d "$mwaccmaildir" ]; then
mwaccmaildirWasThere="NO" # we need to remove again for "online"
mkdir -p "$mwaccmaildir"
fi
if [ -z "$mwmailboxes" ]; then
if [ "$mwtype" = "pop" ]; then
mwgetmailbin --rcfile=$mwaddr --getmaildir=$mwgetmaildir && mwmailboxes="INBOX"
else
mwmailboxes="$($mwmbsyncbin -l $mwacc | sed 's/\//./')" >/dev/null 2>&1
fi
fi
if [ -n "$mwmailboxes" ]; then
mwspoolfile=$(echo "$mwmailboxes" | grep -i -m 1 inbox | sed -ne 's/.*/+\0/p')
[ -z "$mwspoolfile" ] && printf "\033[31m\033[31mNo spoolfile.\033[0m\\n" && return 1

printf "\033[32mMailboxes detected.\033[0m\\n"
echo "$mwmailboxes" | xargs -I {} mkdir -p "$mwaccmaildir/{}/"{cur,new,tmp}
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
}


_mwfinalize() { # new mwaccmuttrc
mwboxes="$(find "$mwaccmaildir/" -name cur | sed "s,$mwaccmaildir/,,g;s,/cur,,")"
if [[ "$mwboxes" =~ ^[[: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
mwaccmutt="${email//[.@]/_}"
muttsync=$(printf '<sync-mailbox><enter-command>source %s<enter><change-folder>!<enter>;<check-stats>' $mwaccmuttrc)
cat >> "$mwaccmuttrc" <<EOF

set spoolfile = "$mwspoolfile"
set record = "$mwrecord"
set postponed = "$mwpostponed"
set trash = "$mwtrash"

folder-hook \$folder '$muttsync'
macro index,pager i$mwidnum '$muttsync' "switch to $mwacc"
_genmwaccmuttrc
if [ "$mwtype" = "online" ]; then
cat >> "$mwaccmuttrc" <<EOF
set folder = "imaps://$mwaddr@$mwserver:$mwport"
set header_cache = "$mwacccachedir"
set message_cachedir = \$header_cache
set imap_user = "$mwlogin"
set imap_pass = "\`pass $mwpass\`"
account-hook \$folder 'set imap_user="$mwlogin" imap_pass="\`pass $mwpass\`"'
set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes

mailboxes =$mwaccmutt ===================== $(echo "$mwboxes" | sed -e "s/ //g;s/.*/\"=\0\"/g" | tr "\n" " ")
EOF
for mwabox in $mwboxes; do
mwbox=${mwabox##*/}
mwbox=${mwbox##*.}
mwboxi=${mwbox::2}
mwboxi=${mwboxi,,}
cat >> $mwaccmuttrc <<EOF

macro index,pager i$mwboxi "<change-folder>=$mwabox<enter>" "go to $mwbox"
macro index,pager M$mwboxi "<save-message>=$mwabox<enter>" "move mail to $mwbox"
macro index,pager C$mwboxi "<copy-message>=$mwabox<enter>" "copy mail to $mwbox"
EOF
done
if [ "$mwtype" = "offline" ]; then
_mwnotmuchauto
printf "All done.\\n\033[33mYou can now run \`\033[32mmailsync [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwacc"
else
mkdir -p "$mwacccachedir"
sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
[ "$mwaccmaildirWasThere" = "NO" ] && rm -rf "$mwaccmaildir"
fi
return 0
mkdir -p "$mwacccachedir"
sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
[ "$mwaccmaildirWasThere" = "NO" ] && rm -rf "$mwaccmaildir"
else
_mwnotmuchauto
printf "All done.\\n\033[33mYou can now run \`\033[32mmw [%s]\033[33m\` to sync your mail.\033[0m\\n" "$mwacc"
fi
}

_mwconfirm() {
printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r mwinput && ! echo "$mwinput" | grep -i "^y$\|^yes$" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
return 0 ;
printf "[y/N]: Do you want to %s?\\n\t" "$@" && read -r mwinput && ! echo "$mwinput" | 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"
_mwlist
[ -z "$mwpick" ] && read -r mwpick
mwidnum="$mwpick"
[ -z "$mwidnum" ] && return 1
mwaddr="$(echo "$mwaccounts" | grep "$mwidnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $2}')"
mwtype="$(echo "$mwaccounts" | grep "$mwidnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $3}')"
_mwaddrmwtype
[ -z "$mwacc" ] && printf "Invalid response." && return 1
return 0 ;
printf "Select an accounts to %s:\\n" "$1"
_mwlist
[ -z "$mwpick" ] && read -r mwpick
mwidnum="$mwpick"
[ -z "$mwidnum" ] && return 1
mwaddr="$(echo "$mwaccs" | grep "$mwidnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $2}')"
mwtype="$(echo "$mwaccs" | grep "$mwidnum:" | sed "s/\.mwonofftype\./ /" | awk '{print $3}')"
_mwaddrmwtype
[ -z "$mwacc" ] && printf "Invalid response." && return 1
return 0 ;
}

_mwdelete() {
rm -f "$mwaccmuttrc"
sed -i "/$mwaccmuttrc/d" "$mwmuttrc"
if [ "$mwtype" = "offline" ]; then
sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
else
rm -rf "$mwacccachedir"
fi
rm -f "$mwaccmuttrc"
rm -f "$mwgetmaildir/$mwaddr"
sed -i "/$mwaccmuttrc/d" "$mwmuttrc"
if [ "$mwtype" = "offline" ]; then
sed -i "/IMAPStore $mwacc-remote$/,/# End profile/d" "$mwmbsyncrc"
sed -i "/account $mwacc/,/^\(\s*$\|account\)/d" "$mwmsmtprc"
else
rm -rf "$mwacccachedir"
fi
}

_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"
[ -z "$mwcronremove" ] && read -r mwcronremove
printf "\033[0m"
echo "$mwcronremove" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null && echo "Mail sync turned off."
else
echo "How many minutes between each mail sync?"
printf "\033[36m\t"
[ -z $mwcronminutes ] && read -r mwcronminutes
printf "\033[0m"
while ! echo "$mwcronminutes" | grep "^[1-9]\+$" >/dev/null; do
printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
read -r mwcronminutes
printf "\033[0m"
done
(crontab -l; echo "*/$mwcronminutes * * * * 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 $mwcronminutes minutes. Be sure you have your cron manager running."
fi
! pgrep cron >/dev/null && echo "No cron manager running. Install/enable one and then select this option again." && return 1
if crontab -l | grep "mw sync" >/dev/null; then
echo "Active mail sync cronjob detected. Do you want to remove it?"
printf "\033[36m\t"
[ -z "$mwcronremove" ] && read -r mwcronremove
printf "\033[0m"
echo "$mwcronremove" | grep -i "^y\(es\)*$" >/dev/null && crontab -l | sed '/mw sync/d' | crontab - >/dev/null && echo "Mail sync turned off."
else
echo "How many minutes between each mail sync?"
printf "\033[36m\t"
[ -z $mwcronminutes ] && read -r mwcronminutes
printf "\033[0m"
while ! echo "$mwcronminutes" | grep "^[1-9]\+$" >/dev/null; do
printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
read -r mwcronminutes
printf "\033[0m"
done
(crontab -l; echo "*/$mwcronminutes * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus; export DISPLAY=:0; $(type mw | cut -d' ' -f3) sync") | crontab - &&
echo "Cronjob added. Mail will sync every $mwcronminutes minutes. Be sure you have your cron manager running."
fi
}

_mwasktype() {
if [ -z "$mwtype" ]; then
while : ; do
printf "[yes/no]: Local mail via mbsync? No: Mutt remotes (slower)\\n\t"
read -r mwoffline
case "$mwoffline" in
[Yy][Ee][Ss]) mwtype="offline" && break ;;
[Nn][Oo]) mwtype="online" && break ;;
*) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
esac
done
fi
if [ -z "$mwtype" ]; then
while : ; do
printf "[yes/no]: Local mail via mbsync? No: Mutt remotes (slower).\\n"
printf "[pop]: Local mail from POP3 using getmail.\\n\t"
read -r mwoffline
case "$mwoffline" in
[Yy][Ee][Ss]) mwtype="offline" && break ;;
[Nn][Oo]) mwtype="online" && break ;;
[Pp][Oo][Pp]) mwtype="pop" && break ;;
*) echo "Write out either yes or no completely. Try again or press ctrl-c to quit." ;;
esac
done
fi
}

_mwpurge() {
_mwconfirm "delete all account data" || exit
rm -rf "$mwaccmuttdir" "${mwmsmtprc%/*}" "${mwmbsyncrc%/*}" "$mwcachedir"
sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
pgrep cron >/dev/null && crontab -l | sed '/mailsync/d' | crontab - >/dev/null
echo "All configs and account settings have been purged."
_mwconfirm "delete all account data" || exit
rm -rf "$mwaccmuttdir/[1-9]-.*.muttrc" "${mwmsmtprc%/*}" "${mwmbsyncrc%/*}" "$mwcachedir" "$mwgenmuttaccs"
sed -i "/\# mw-autogenerated/d" "$mwmuttrc"
pgrep cron >/dev/null && crontab -l | sed '/mw sync/d' | crontab - >/dev/null
echo "All configs and account settings have been purged."
}

_mwnotmuchauto() {
[ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
[ -f "$NOTMUCH_CONFIG" ] && return 0
mwnmbasic="[database]
[ -z "$NOTMUCH_CONFIG" ] && NOTMUCH_CONFIG="$HOME/.notmuch-config"
[ -f "$NOTMUCH_CONFIG" ] && return 0
mkdir -p "${NOTMUCH_CONFIG%/*}"
cat > "$NOTMUCH_CONFIG" <<EOF
[database]
path=$mwmaildir
[user]
name=$mwname
@@ -409,34 +351,272 @@ exclude_tags=deleted;spam;
synchronize_flags=true
[crypto]
gpg_path=$GPG"
mkdir -p "${NOTMUCH_CONFIG%/*}"
echo "$mwnmbasic" > "$NOTMUCH_CONFIG"
EOF
}


#run after mbsync to create mutt settings for $mwaddr
function _genmwaccmuttrc()
{
mwaccmaildir="$mwmaildir/$mwaddr"
mwaccmuttrc="$mwaccmuttdir/$(find "$mwaccmuttdir" -type f | grep -m 1 -o "[1-9]-$mwacc.mwonofftype.$mwtype.muttrc")"
if [[ ! -f "$mwaccmuttrc" ]]; then
# new mwidnum = first one missing
mwaccs="$(find "$mwaccmuttdir" -type f | grep -o "[1-9]-.*.muttrc" | sort -n)"
for mwx in $(seq 1 9); do echo "$mwaccs" | grep "$mwx:" >/dev/null 2>&1 || { export mwidnum="$mwx"; break ;}; done
mwaccmuttrc="$mwaccmuttdir/$mwidnum-$mwacc.mwonofftype.$mwtype.muttrc"
else
mwidnum=${mwaccmuttrc%%-*}
mwidnum=${mwidnum##*/}
fi
! grep "source $mwaccmuttrc" "$mwgenmuttaccs" >/dev/null && echo "source $mwaccmuttrc" >> $mwgenmuttaccs
mwmailboxes="$(find "$mwaccmaildir/" -name cur | sed "s,$mwaccmaildir/,,g;s,/cur,,")"
[[ "$mwmailboxes" =~ ^[[:space:]]*$ ]] && echo "no mailboxes" && return
mwspoolfile=$(echo "$mwmailboxes" | grep -i -m 1 inbox | sed -ne 's/.*/+\0/p')
[[ "$mwspoolfile" =~ ^[[:space:]]*$ ]] && return

mwrecord=$(echo "$mwmailboxes" | grep -i -m 1 sent | sed -ne 's/.*/+\0/p')
[ -z "$mwrecord" ] && mkdir -p "$mwaccmaildir/Sent/"{cur,new,tmp} && mwrecord="Sent"
mwpostponed=$(echo "$mwmailboxes" | grep -i -m 1 draft | sed -ne 's/.*/+\0/p')
[ -z "$mwpostponed" ] && mkdir -p "$mwaccmaildir/Drafts/"{cur,new,tmp} && mwpostponed="Drafts"
mwtrash=$(echo "$mwmailboxes" | grep -i -m 1 trash | sed -ne 's/.*/+\0/p')
[ -z "$mwtrash" ] && mkdir -p "$mwaccmaildir/Trash/"{cur,new,tmp} && mwtrash="Trash"
mwaccmutt="${mwaddr//[.@]/_}"
mwmuttsync=$(printf '<sync-mailbox><enter-command>source %s<enter><change-folder>!<enter>;<check-stats>' $mwaccmuttrc)
cat > "$mwaccmuttrc" <<EOF
# vim: filetype=muttrc
# muttrc file for account $mwaddr
# generated by mw

set spoolfile = "$mwspoolfile"
set record = "$mwrecord"
set postponed = "$mwpostponed"
set trash = "$mwtrash"

unmailboxes *
mailboxes =$mwaccmutt ===================== $(echo "$mwmailboxes" | sed -e "s/ //g;s/.*/\"=\0\"/g" | tr "\n" " ")

macro index,pager i$mwidnum '$mwmuttsync' "switch to $mwaddr"

set from = "$mwaddr"
set sendmail = "/usr/bin/msmtp -a \$from"

EOF
[[ -n $realname ]] && cat >> "$mwaccmuttrc" <<EOF
set realname = "$mwname"
alias me $mwname <$mwaddr>
EOF

for eqbox in $mwmailboxes; 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 #can be online when called from mw
cat >> "$mwaccmuttrc" <<EOF

macro index gm "<shell-escape>mw $mwaddr<enter>" "sync mail $mwaddr"

set folder = "$mwaccmaildir"
folder-hook \$folder '$mwmuttsync'

EOF
fi
}

if [ "$(uname)" = "Darwin" ]; then
_mwnotify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $1\"" & ;}
else
if command -v notify-send >/dev/null; then
_mwnotify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." &;}
else
_mwnotify() { echo "mutt-wizard: 📬 $2 new mail(s) in \`$1\` account." ;}
fi

function _mwaddr()
{
mwaddr=$1
mwaddr=${mwaddr/#\~/$HOME}
mwaddr=${mwaddr#$mwmaildir/}
mwaddr=${mwaddr%%/*}
mkdir -p $mwmaildir/$mwaddr
}

function _mwgatheremails()
{
unset mwemails

! grep "source $mwgenmuttaccs" "$mwmuttrc" >/dev/null && echo "source $mwgenmuttaccs # mw-autogenerated" >> "$mwmuttrc"
cat > "$mwgenmuttaccs" <<EOF
# vim: filetype=muttrc
# generated by mw

macro index gM '<shell-escape>mw<enter>' "sync all mail"

EOF
if [[ -f $mwmbsyncrc ]]; then
for store in $(sed -ne 's/^Path\s*//p' $mwmbsyncrc); do
_mwaddr "$store"
mwemails+=" $mwaddr"
done
fi
if [[ -d $mwgetmaildir ]]; then
for gmrc in $mwgetmaildir/*; do
store=$(sed -ne "s/^path\s*=\s*//p" $gmrc)
store=${store/#\~/$HOME}
_mwaddr "$store"
mkdir -p "$store"/{cur,new,tmp}
mwemails+=" $mwaddr"
done
fi
}

_mwsyncandnotify() {
mwaccmaildir="$mwmaildir/$mwaddr"
if [[ -f "$mwgetmaildir/$mwaddr" ]]; then
$mwgetmailbin --rcfile=$mwaddr --getmaildir=$mwgetmaildir
else
$mwmbsyncbin $mwaddr
fi
mwlastrun="$mwaccmaildir/.mw.lastrun"
mwnew=$(find "$mwaccmaildir/INBOX/new/" "$mwaccmaildir/Inbox/new/" "$mwaccmaildir/inbox/new/" -type f -newer "$mwlastrun" 2> /dev/null)
newcount=$(echo "$mwnew" | sed '/^\s*$/d' | wc -l)
if [ "$newcount" -gt "0" ]; then
_mwnotify "$mwaddr" "$newcount"
for file in $mwnew; do
# Extract subject and sender from mail.
from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n-1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
_mwnotify "📧$from:" "$subject"
done
fi
touch "$mwlastrun"
}

function _mwsyncemails()
{
for mwaddr in $mwemails; do
_mwsyncandnotify
done
rm $XDG_CONFIG_HOME/getmail/oldmail-* &>/dev/null
echo notmuch+afew ...
notmuch new &>/dev/null
#notmuch also created afew MailMover target mailboxes
}

function _mwmuttemails()
{
for mwaddr in $mwemails; do
_genmwaccmuttrc
done
}

_mwcheckinternet()
{
# Checks for internet connection
if command -v systemctl >/dev/null; then
if ! systemctl --type service | grep net | grep active > /dev/null; then
echo "No internet connection."
return 1
fi
else
if ! ping -q -c 1 1.1.1.1 > /dev/null;
echo "No internet connection (ping failed)."
return 1
fi
fi
}


function _mwsync()
{
unset mwemails

# Run only if user logged in (prevent cron errors)
if ! pgrep -u "$USER" >/dev/null; then
echo "$USER not logged in; sync will not run."
return 1 ;
fi
# Run only if not already running in other instance
if pgrep -x mbsync >/dev/null; then
echo "mbsync is running."
return 1
fi
if pgrep -x notmuch >/dev/null; then
echo "notmuch is running."
return 1
fi

_mwcheckinternet || return 1

_mwaddr "$1"
mwemails="$mwaddr"
[[ $mwemails == "" ]] && _mwgatheremails && _mwsyncemails && _mwmuttemails && return 0
_mwsyncemails

return 0
}

trap 'echo -e "\033[0m\n"; exit' STOP INT ABRT KILL

if [ "${BASH_SOURCE[0]}" = "${0}" ]; then

if [ "$#" -gt 1 ]; then
echo "Command or email or no parameter (=sync all). See --help."
return 1
fi

command -V gpg >/dev/null 2>&1 && GPG="gpg" || GPG="gpg2"
[ -z "$PASSWORD_STORE_DIR" ] && PASSWORD_STORE_DIR="$HOME/.password-store"
[ -r "$PASSWORD_STORE_DIR/.gpg-id" ] &&
"$GPG" --list-secret-keys $(cat "$PASSWORD_STORE_DIR/.gpg-id") >/dev/null 2>&1 || {
printf "\`pass\` must be installed and initialized to encrypt passwords.\\nBe sure it is installed and run \`pass init <yourgpgemail>\`.\\nIf you don't have a GPG public private key pair, run \`$GPG --full-gen-key\` first.\\n"
exit
}
! command -v "$prefix/bin/mbsync" >/dev/null && printf "\`mbsync\` must be installed to run mutt-wizard.\\n" && exit
! command -v "$prefix/bin/msmtp" >/dev/null && printf "\`msmtp\` must be installed.\\n" && exit
! ( command -v mutt >/dev/null || command -v neomutt >/dev/null ) && printf "\`mutt\` must be installed.\\n" && exit


case "$1" in
ls) _mwlist ;;
add) _mwadd ;;
pass) _mwpick "change the password of" && _mwgetpass ;;
delete) _mwpick delete && _mwconfirm "delete the \`$mwacc\` profile" && _mwdelete ;;
purge) _mwpurge ;;
cron) _mwcron ;;
*) cat << EOF
mw: mutt-wizard, auto-configure email accounts for mutt
including downloadable mail with \`isync\`.

Allowed options:
add Add and autoconfigure an email address (9 max.)
ls List configured accounts
delete Pick an account to delete
purge Delete all accounts and settings
cron Enable or disable an autosync via cronjob
all else Print this message

NOTE: Once at least one account is added, you can run
\`mailsync -a\` to begin downloading mail.
ls) _mwlist ;;
add) _mwadd ;;
pass) _mwpick "change the password of" && _mwgetpass ;;
delete) _mwpick delete && _mwconfirm "delete the \`$mwacc\` profile" && _mwdelete ;;
purge) _mwpurge ;;
cron) _mwcron ;;
sync) _mwsync ;;
*@*) _mwsync $1 ;;
--help|-h) cat << EOF
mw: mutt-wizard, auto-configure and sync email accounts for mutt,
including mutt-independent sync of mail with \`isync\` or download of pop3 mail with \`getmail\`.

Commands:
add Add and autoconfigure an email address (9 max.)
ls List configured accounts
delete Pick an account to delete
purge Delete all accounts and settings
cron Enable or disable an autosync via cronjob
sync sync/download mail based on config in $mwmbsyncrc and $mwgetmaildir
--help|-h Print this message

With no parameter all configured emails are synced and all account muttrc's are created.
With a parameter containing a @, an mail is assumed and that account is synced.

Config requirements:

- mbsyncrc: "Path" ends in email
- getmail: config file name = email, "path" ends in email

EOF
esac

exit $?
fi

+ 0
- 8
bin/openfile View File

@@ -1,8 +0,0 @@
#!/bin/sh
# Helps open a file with xdg-open from mutt in a external program without weird side effects.
[ $(uname) = "Darwin" ] && opener="open" || opener="setsid xdg-open"
mkdir -p "/tmp/$USER-mutt-tmp"
file="/tmp/$USER-mutt-tmp/$(basename "$1")"
rm -f "$file"
cp "$1" "$file"
$opener "$file" >/dev/null 2>&1 &

+ 12
- 3
mw.1 View File

@@ -9,12 +9,20 @@ mw \- mutt-wizard \- autoconfigure email accounts for neomutt and isync
takes a user email account and sets up a terminal-based email interface with it for
.B neomutt.
This can include offline email with
.B isync/mbsync
.B mbsync
or
.B getmail
and configs for
.B msmtp
for sending mail, and passwords automatically encrypted and stored with
.B pass.
.SH COMMANDS
Without command parameter
.I sync
is assumed.
If a parameter contains
.I @
an email is assumed and synced.
.TP
.B add
configure an email account
@@ -36,8 +44,9 @@ settings.
.TP
.B cron
toggle a cronjob that will periodically sync mail with
.B mailsync.
Use mailsync instead of mbsync, also manually.
.TP
.B sync
syncs all emails.
.SH DETAILS
.TP
.B Mail location


+ 3
- 3
share/mailcap View File

@@ -1,7 +1,7 @@
text/plain; $EDITOR %s ;
text/html; openfile %s ; nametemplate=%s.html
text/html; mwopen %s ; nametemplate=%s.html
text/html; w3m -I %{charset} -T text/html; copiousoutput;
image/*; muttimage %s ; copiousoutput
image/*; mwimage %s ; copiousoutput
video/*; setsid mpv --quiet %s &; copiousoutput
application/pdf; openfile %s ;
application/pdf; mwopen %s ;
application/pgp-encrypted; gpg -d '%s'; copiousoutput;

+ 2
- 2
test/dotests View File

@@ -42,10 +42,10 @@ if [[ "$OK" == "OK" ]]; then

echo "= Test 2 ="
mwtype=offline mw add
mailsync
echo "=> In another window, start mutt and verify that mails are shown"
echo "=> Press i1, wait, press i2. Verify that mutt is fast again after i2."
echo "=> Press gm and gM, to start mailsync."
echo "=> Press gm and gM, to start mw sync."
echo "=> Anwer with Y and check that mbsyncrc, msmtp/config and mutt/accounts are gone."
mw purge



+ 3
- 3
test/test_mw.bats View File

@@ -24,7 +24,7 @@ setup()
export mwname="real name"
export mwaddr="full.addr@gmail.com"
export mwlogin="$mwaddr"
export mailboxes="[Gmail]/INBOX"
export mwmailboxes="[Gmail]/INBOX"
export mwshare=$PWD/../share
function pass() { return 0; }
export pass
@@ -55,7 +55,7 @@ teardown()

#3
@test "add offline unsuccessful" {
export mailboxes="[Gmail]/OTHER"
export mwmailboxes="[Gmail]/OTHER"
mwtype="offline" run _mwadd
[ -f mwtesttmp/config/mutt/muttrc ]
[ -d mwtesttmp/config/mutt/accounts ]
@@ -99,7 +99,7 @@ teardown()
mwcronminutes=99 run _mwcron
chkline="${lines[2]}"
[ "${chkline::14}" = "Cronjob added." ]
function crontab() { echo 'mailsync'; }
function crontab() { echo 'mw sync'; }
export crontab
mwcronremove=y run _mwcron
chkline="${lines[1]}"


Loading…
Cancel
Save