mutt-wizard/mw

334 lines
15 KiB
Plaintext
Raw Normal View History

2019-02-21 16:14:17 +01:00
#!/bin/sh
muttdir="$HOME/.config/mutt" # Main mutt config location
accdir="$muttdir/accounts" # Directory for account settings
maildir="$HOME/.local/share/mail" # Location of mail storage
bindir="$HOME/.config/mutt/bin" # Location of scripts run by mutt or the wizard
namere="^[a-z_][a-z0-9_-]*$" # Regex to ensure viable username
emailre=".\+@.\+\\..\+" # Regex to confirm valid email address
2019-04-11 19:39:47 +02:00
mbsyncrc="$HOME/.mbsyncrc"
2019-02-22 23:27:12 +01:00
[ ! -f "$muttdir/domains.csv" ] || [ ! -d "$bindir" ] && printf "Read the README. Be sure to put the repo in the right place before running.\\n" && exit 1
2019-02-21 16:14:17 +01:00
tmpdir="$(mktemp -d)"
# Get certificate location depending on OS. Linux is elsewhere condition.
case "$(uname)" in
"Darwin") sslcert="/usr/local/etc/openssl/cert.pem" ;;
*) sslcert="/etc/ssl/certs/ca-certificates.crt" ;;
esac
getprofiles() { \
2019-04-11 19:39:47 +02:00
unset msmtp_header msmtp_profile mutt_profile mbsync_profile
2019-02-22 18:25:37 +01:00
printf "Creating profiles for \`%s\`..." "$title"
2019-03-25 19:11:44 +01:00
msmtp_header="defaults
auth on
tls on
tls_trust_file $sslcert
logfile ~/.config/msmtp/msmtp.log
"
msmtp_profile="
account $title
host $smtp
port $sport
from $login
user $login
2019-04-19 23:47:20 +02:00
passwordeval \"pass mutt-wizard-$title\"
2019-03-25 19:11:44 +01:00
"
2019-04-16 17:19:24 +02:00
mbsync_profile="IMAPAccount $title
Host $imap
User $login
Port $iport
2019-04-19 23:47:20 +02:00
PassCmd \"pass mutt-wizard-$title\"
2019-04-16 17:19:24 +02:00
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt
IMAPStore $title-remote
Account $title
MaildirStore $title-local
Subfolders Verbatim
Path ~/.local/share/mail/$title/
Inbox ~/.local/share/mail/$title/INBOX
Channel $title
2019-04-16 19:10:52 +02:00
Expunge Both
2019-04-16 17:19:24 +02:00
Master :$title-remote:
Slave :$title-local:
Patterns * !\"[Gmail]/All Mail\"
Create Both
SyncState *
# End profile"
2019-03-25 19:11:44 +01:00
if [ "$accounttype" = "offline" ]; then
mutt_profile="# vim: filetype=neomuttrc
# muttrc file for account $title
set realname = \"$realname\"
set from = \"$fulladdr\"
set sendmail = \"/usr/bin/msmtp -a $title\"
set folder = \"$maildir/$title\"
set header_cache = $accdir/$title/cache/headers
set message_cachedir = $accdir/$title/cache/bodies
set certificate_file = $accdir/$title/certificates
2019-04-19 23:47:20 +02:00
set imap_pass = \`pass mutt-wizard-$title\`
set smtp_pass = \`pass mutt-wizard-$title\`
2019-03-25 19:11:44 +01:00
alias me $realname <$fulladdr>
set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes
bind index,pager gg noop
bind index,pager g noop
bind index,pager M noop
bind index,pager C noop
bind index gg first-entry
unmailboxes *
2019-03-30 17:53:02 +01:00
set imap_check_subscribed
2019-03-25 19:11:44 +01:00
"
else
2019-02-21 16:14:17 +01:00
mutt_profile="# vim: filetype=neomuttrc
# muttrc file for account $title
set realname = \"$realname\"
set from = \"$fulladdr\"
set sendmail = \"/usr/bin/msmtp -a $title\"
2019-04-16 17:42:30 +02:00
set folder = \"imaps://$fulladdr@$imap:$iport\"
set imap_user = \"$login\"
2019-02-21 16:14:17 +01:00
set header_cache = $accdir/$title/cache/headers
set message_cachedir = $accdir/$title/cache/bodies
set certificate_file = $accdir/$title/certificates
source \"$bindir/getmuttpass $title |\"
alias me $realname <$fulladdr>
set mbox_type = Maildir
set ssl_starttls = yes
set ssl_force_tls = yes
bind index,pager gg noop
bind index,pager g noop
bind index,pager M noop
bind index,pager C noop
bind index gg first-entry
unmailboxes *
"
2019-03-25 19:11:44 +01:00
fi
2019-02-22 14:15:54 +01:00
printf "DONE.\\n"
2019-02-21 16:14:17 +01:00
}
2019-02-21 17:29:21 +01:00
addaccount() { \
2019-04-15 21:14:03 +02:00
printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/mbsync\\n\\nEmail: \033[36m\t"
2019-02-21 17:29:21 +01:00
read -r fulladdr
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-21 17:29:21 +01:00
while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
2019-04-15 21:14:03 +02:00
printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: \033[36m\t"
2019-02-21 17:29:21 +01:00
read -r fulladdr
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-21 16:14:17 +01:00
done
domain="$(echo "$fulladdr" | sed "s/.*@//")"
2019-02-21 18:07:28 +01:00
printf "\\nSearching for \033[32m%s\033[0m in \033[34m\`domains.csv\`\033[0m..." "$domain"
2019-04-17 21:43:18 +02:00
serverinfo="$(grep "$domain" "$muttdir/domains.csv" 2>/dev/null)"
2019-02-21 16:14:17 +01:00
if [ -z "$serverinfo" ]; then
2019-02-21 17:29:21 +01:00
printf "Your email domain is not in mutt-wizard's database yet.\\nmutt-wizard will still autoconfigure everything, but you will have to manually type in your service's IMAP and SMTP server information.\\nYou can usually quickly find this by internet searching for it.\\n"
2019-04-15 21:14:03 +02:00
printf "Insert the IMAP server for your email provider (excluding the port number)\\n\033[36m\t"
2019-02-21 17:29:21 +01:00
read -r imap
2019-04-15 21:14:03 +02:00
printf "\033[0mWhat is your server's IMAP port number? (Usually something like 993)\\n\033[36m\t"
2019-02-21 17:29:21 +01:00
read -r iport
2019-04-15 21:14:03 +02:00
printf "\033[0mInsert the SMTP server for your email provider (excluding the port number)\\n\033[36m\t"
2019-02-21 17:29:21 +01:00
read -r smtp
2019-04-15 21:14:03 +02:00
printf "\033[0mWhat is your server's SMTP port number? (Usually 587 or 465)\\n\033[36m\t"
2019-02-21 17:29:21 +01:00
read -r sport
2019-04-15 21:14:03 +02:00
printf "\033[0m\\nGreat! If you want to be helpful, copy the line below and you can add it to the \`domains.csv\` file on Github.\\nThis will make things easier for others who use your email provider.\\n\\n%s,%s,%s,%s,%s\\n\\nAlthough be sure to test to see if these settings work first! ;-)\\n" "$domain" "$imap" "$iport" "$smtp" "$sport"
2019-02-21 16:14:17 +01:00
else
2019-02-22 18:25:37 +01:00
IFS=, read -r service imap iport smtp sport <<EOF
2019-02-21 16:14:17 +01:00
$serverinfo
EOF
2019-02-21 18:07:28 +01:00
printf "\\n\033[3;33mCongrats!\033[0m Server info has automatically be found, so you won't have to look anything 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\\nThis data will be used by the wizard.\\n" "$imap" "$iport" "$smtp" "$sport"
2019-02-21 16:14:17 +01:00
fi
2019-03-25 19:11:44 +01:00
cont
2019-02-21 18:07:28 +01:00
printf "Enter the \033[35mfull name\033[0m you want to be identified by on this account.\\n\tReal name: "
2019-02-21 17:29:21 +01:00
read -r realname
2019-02-21 18:07:28 +01:00
printf "Enter a short, \033[36mone-word identifier\033[0m for this email account that will distinguish them from any other accounts you add.\\n\tAccount name: "
2019-02-21 17:29:21 +01:00
read -r title
2019-02-21 16:14:17 +01:00
while ! echo "$title" | grep "$namere" >/dev/null; do
2019-04-15 21:14:03 +02:00
printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: \033[36m\t"
2019-02-21 17:29:21 +01:00
read -r title
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-21 16:14:17 +01:00
done
2019-04-15 21:14:03 +02:00
printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): \033[36m\t"
2019-02-21 17:29:21 +01:00
read -r login
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-21 16:14:17 +01:00
[ -z "$login" ] && login="$fulladdr"
2019-04-17 21:43:18 +02:00
grep "i[0-9]" "$muttdir/personal.muttrc" 2>/dev/null | awk '{print $3}' | sed -e 's/i//g' > "$tmpdir/mutt_used"
2019-02-21 18:41:31 +01:00
seq 1 9 > "$tmpdir/mutt_all"
idnum=$(diff "$tmpdir/mutt_all" "$tmpdir/mutt_used" | sed -n 2p | awk '{print $2}')
2019-02-22 19:14:38 +01:00
getpass
2019-02-21 16:14:17 +01:00
getprofiles
2019-03-25 19:11:44 +01:00
mkdir -p "$accdir/$title/cache/bodies" "$HOME/.config/msmtp"
2019-02-21 16:14:17 +01:00
[ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
echo "$mutt_profile" > "$accdir/$title.muttrc"
echo "$mbsync_profile" >> "$mbsyncrc"
2019-03-31 21:34:01 +02:00
echo "macro index,pager i$idnum '<sync-mailbox><enter-command>source $accdir/$title.muttrc<enter><change-folder>!<enter>;<check-stats>'" >> "$muttdir/personal.muttrc"
2019-02-22 13:40:43 +01:00
! grep "^source.*.muttrc" "$muttdir/personal.muttrc" >/dev/null && echo "source $accdir/$title.muttrc" >> "$muttdir/personal.muttrc"
2019-04-16 17:42:30 +02:00
trysync && finalize
2019-04-17 21:42:00 +02:00
[ "$accounttype" = "offline" ] || sed -i "/IMAPAccount $title$/,/# End profile/d" "$mbsyncrc"
2019-02-21 16:14:17 +01:00
}
2019-04-19 23:47:20 +02:00
getpass() { pass rm -f "mutt-wizard-$title" 2>/dev/null ; pass insert "mutt-wizard-$title" ;}
2019-02-21 16:14:17 +01:00
formatShortcut() { \
2019-02-22 18:25:37 +01:00
while read -r data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
2019-02-21 16:14:17 +01:00
echo "macro index,pager M$1 \"<save-message>$data<enter>\" \"Move mail to $2.\" # autogenerated"
echo "macro index,pager C$1 \"<copy-message>$data<enter>\" \"Copy mail to $2.\" # autogenerated"; } >> "$muttdir/accounts/$3.muttrc"
done ;}
2019-02-21 18:32:20 +01:00
trysync() { \
2019-04-12 05:12:19 +02:00
! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning \`mbsync %s\` manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" "$title" && return 1
2019-04-11 19:39:47 +02:00
printf "\033[32mYou must have an internet connection to continue.\033[0m\\nmutt-wizard will run mbsync briefly to (1) ensure that login details are functional and (2) allow mbsync to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run mbsync manually to finish the mail sync later.\\n"
2019-03-25 19:11:44 +01:00
cont
2019-04-11 23:50:28 +02:00
mkdir -p "$maildir/$title"
2019-04-12 05:12:19 +02:00
if mailboxes="$(mbsync -l "$title")" >/dev/null 2>&1; then
printf "\033[32mMailboxes detected.\033[0m\\n"
echo "$mailboxes" | xargs -I {} mkdir -p "$maildir/$title/{}"
2019-02-21 18:41:48 +01:00
else
2019-04-12 05:12:19 +02:00
printf "\033[31m\033[31mSync not successful.\033[0m Try running \`mbsync %s\` manually after double-checking your password and server settings.\\nThen select to finalize the account.\\n" "$title"; return 1
2019-02-21 18:41:48 +01:00
fi
2019-02-21 18:32:20 +01:00
}
2019-02-21 18:41:48 +01:00
finalize() { \
2019-02-22 18:25:37 +01:00
boxes="$(du -a "$maildir/$title/"* -d 0 | sed "s/^.*\//=/")"
2019-04-11 19:39:47 +02:00
[ -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" "$title" && return
2019-02-22 13:40:43 +01:00
echo "$boxes" > "$tmpdir/title_boxes"
2019-02-21 18:32:20 +01:00
printf "Setting up the mutt sidebar...\\n"
2019-02-21 16:14:17 +01:00
sidebar_width="$(sed -n -e '/^set sidebar_width/p' "$muttdir/muttrc" | awk -F'=' '{print $2}')"
2019-02-22 19:14:38 +01:00
delim="$(head -c "$sidebar_width" < /dev/zero | tr '\0' =)"
2019-02-21 18:32:20 +01:00
printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
2019-02-22 13:40:43 +01:00
oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/title_boxes" | tr "\n" " ")"
2019-02-22 03:31:53 +01:00
oneline="=$title $delim $oneline"
2019-04-16 17:08:43 +02:00
spoolfile=$(grep -i -m 1 "$tmpdir/title_boxes" -e inbox | sed -e 's/=/+/g')
record=$(grep -i -m 1 "$tmpdir/title_boxes" -e sent | sed -e 's/=/+/g')
postponed=$(grep -i -m 1 "$tmpdir/title_boxes" -e draft | sed -e 's/=/+/g')
trash=$(grep -i -m 1 "$tmpdir/title_boxes" -e trash | sed -e 's/=/+/g')
2019-02-22 03:31:53 +01:00
sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$title.muttrc"
2019-02-22 13:40:43 +01:00
{ echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$title.muttrc"
2019-02-22 03:31:53 +01:00
echo mailboxes "$oneline" >> "$muttdir/accounts/$title.muttrc"
2019-02-21 18:32:20 +01:00
printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
2019-02-22 03:31:53 +01:00
sed -i "/# autogenerated/d" "$muttdir/accounts/$title.muttrc"
2019-04-16 17:08:43 +02:00
grep -i -m 1 inbox "$tmpdir/title_boxes" | formatShortcut i inbox "$title"
grep -i -m 1 sent "$tmpdir/title_boxes" | formatShortcut s sent "$title"
grep -i -m 1 draft "$tmpdir/title_boxes" | formatShortcut d drafts "$title"
grep -i -m 1 trash "$tmpdir/title_boxes" | formatShortcut t trash "$title"
grep -i -m 1 spam "$tmpdir/title_boxes" | formatShortcut S spam "$title"
grep -i -m 1 junk "$tmpdir/title_boxes" | formatShortcut j junk "$title"
grep -i -m 1 archive "$tmpdir/title_boxes" | formatShortcut a archive "$title"
2019-02-21 18:32:20 +01:00
printf "All done.\\n"
2019-02-21 16:14:17 +01:00
}
2019-04-12 05:12:19 +02:00
confirm() { printf "Are you \033[31;1mreally\033[0m sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
printf "Are you really, really sure you want to %s?\\n\t" "$@" && read -r input && ! echo "$input" | grep -i "y\(es\)*" >/dev/null && printf "That doesn't seem like a yes to me.\\n\\n" && return 1
return 0 ;}
2019-02-21 16:14:17 +01:00
2019-04-16 17:08:43 +02:00
pick() { numbered="$(ls $accdir/*muttrc | sed "s/.*\///;s/\..*//" | nl)"
2019-04-12 05:12:19 +02:00
[ "$(echo "$numbered" | wc -w)" = 0 ] && printf "No accounts to choose from.\\n" && return 1
2019-02-22 19:14:38 +01:00
printf "Select an accounts to %s:\\n" "$1"
2019-02-22 13:40:43 +01:00
echo "$numbered"
2019-02-22 19:14:38 +01:00
printf "\033[36m\t"
2019-02-22 01:42:02 +01:00
read -r input
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-22 18:25:37 +01:00
[ -z "$input" ] && return 1
2019-02-22 13:40:43 +01:00
title="$(echo "$numbered" | grep "$input" | awk '{print $2}')"
[ -z "$title" ] && printf "Invalid response." && return 1
2019-04-12 05:12:19 +02:00
return 0 ;}
2019-02-22 01:42:02 +01:00
2019-04-16 17:09:50 +02:00
delete() { sed -i "/IMAPAccount $title$/,/# End profile/d" "$mbsyncrc"
2019-04-19 23:47:20 +02:00
rm -rf "${accdir:?}/${title:?}" "$accdir/$title.muttrc"
2019-02-22 19:14:38 +01:00
sed -i "/$title.muttrc/d" "$muttdir/personal.muttrc"
2019-04-12 05:12:19 +02:00
sed -i "/account $title/,/^\(\s*$\|account\)/d" "$HOME/.config/msmtp/config"
2019-04-16 17:09:50 +02:00
#confirm "delete the local mail repository as well?" && rm -rf "${maildir:?}/${title:?}"
2019-04-12 05:12:19 +02:00
}
2019-02-21 18:07:28 +01:00
2019-03-25 19:11:44 +01:00
choosecron() { ! pgrep crond >/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"
read -r rmyn
printf "\033[0m"
echo "$rmyn" | 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"
read -r minnum
printf "\033[0m"
while ! echo "$minnum" | grep "^[0-9]\+$" >/dev/null; do
2019-04-15 21:14:03 +02:00
printf "That doesn't look like a number. How many minutes between each mail sync?\\n\033[36m\t"
2019-03-25 19:11:44 +01:00
read -r minnum
printf "\033[0m"
done
(crontab -l; echo "*/$minnum * * * * export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus; export DISPLAY=:0; . \$HOME/.profile; $muttdir/bin/mailsync -f INBOX") | crontab - &&
echo "Cronjob added. Mail will sync every $minnum minutes. Be sure you have your cron manager running."
fi
}
2019-04-12 05:12:19 +02:00
cont() { printf "Press Enter to continue.\\n"
2019-03-25 19:11:44 +01:00
stty -echo
read -r null
2019-04-12 05:12:19 +02:00
stty echo ; }
2019-03-25 19:11:44 +01:00
addtype() { \
2019-04-12 05:12:19 +02:00
! command -v mbsync >/dev/null && printf "You must have the \`mbsync\` command to add an account.\\nIt syncs mail and detects remote mailboxes.\\nYou can typically get it by installing \`isync\`.\\n" && return 1
2019-03-25 19:11:44 +01:00
while : ; do
2019-04-11 19:39:47 +02:00
echo "Do you want to keep your mail for this account offline with mbsync? [yes/no]"
2019-03-25 19:11:44 +01:00
read -r offnot
case "$offnot" in
[Yy]es) accounttype="offline" && break ;;
[Nn]o) accounttype="online" && break ;;
*) echo "I don't know what you mean. Try again or press ctrl-c to quit." ;;
esac
done
addaccount
}
2019-02-21 17:39:25 +01:00
main() { \
while : ; do
2019-04-19 23:47:20 +02:00
[ ! -f "$HOME/.password-store/.gpg-id" ] && 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
printf "Welcome to:\\n\033[34m"
2019-03-25 19:11:44 +01:00
cat << "EOF"
_ __ ___ _ _| |_| |_ __ _(_)______ _ _ __ __| |
| '_ ` _ \| | | | __| __|___\ \ /\ / / |_ / _` | '__/ _` |
| | | | | | |_| | |_| ||_____\ V V /| |/ / (_| | | | (_| |
|_| |_| |_|\__,_|\__|\__| \_/\_/ |_/___\__,_|_| \__,_|
EOF
printf "\033[0mMade and maintained by Luke Smith <https://lukesmith.xyz>
What would you like \033[32mmutt-wizard\033[0m to do?
2019-04-15 21:14:03 +02:00
1 Add an email account
2 Change an account's password
3 Remove an account
2019-04-16 20:46:02 +02:00
4 Change GPG key pair used for encryption
5 Delete all account data
6 Enable/disable autosync
2019-02-21 17:39:25 +01:00
0 Exit
Input a number to continue or press ctrl-c.\\n"
2019-02-22 19:14:38 +01:00
printf "\033[36m\t"
2019-02-21 17:39:25 +01:00
read -r choice
2019-02-22 19:14:38 +01:00
printf "\033[0m"
2019-02-21 17:39:25 +01:00
case "$choice" in
2019-03-25 19:11:44 +01:00
1) addtype ;;
2) pick "change the password of" && confirm "change the password of" && getpass ;;
3) pick delete && confirm "delete the \`$title\` profile" && delete ;;
2019-04-16 20:46:02 +02:00
4) askgpg ;;
2019-04-19 23:47:20 +02:00
5) confirm "delete all account data" && rm -ri "$mbsyncrc" "$accdir" "$muttdir/personal.muttrc" && echo "All configs and account settings have been purged." ;;
2019-04-16 20:46:02 +02:00
6) choosecron ;;
2019-02-21 17:39:25 +01:00
0) break ;;
*) printf "Invalid input.\\n"
esac
2019-03-25 19:11:44 +01:00
cont
2019-02-21 16:14:17 +01:00
done
2019-02-21 17:39:25 +01:00
}
main
2019-02-21 16:14:17 +01:00
rm -rf "$tmpdir"