mutt-wizard/mw

263 lines
11 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
creddir="$HOME/.local/share/muttwizard" # Location of encrypted credentials
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-02-21 17:29:21 +01:00
gpgemail="$(cat "$creddir/gpgemail" 2>&1)" # Get previously set gpg email address
2019-02-21 16:14:17 +01:00
tmpdir="$(mktemp -d)"
GPG="gpg"; command -v gpg >/dev/null || GPG="gpg2" # Ensure proper gpg command
mkdir -p "$maildir" "$creddir" "$bindir"
# 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() { \
offlineimap_header="[general]
accounts =
starttls = yes
ssl = true
pythonfile = $bindir/imappwd.py
"
offlineimap_profile="
[Account $title]
localrepository = $title-local
remoterepository = $title-remote
[Repository $title-remote]
auth_mechanisms = LOGIN
type = $type
remoteuser = $login
remotepasseval = mailpasswd(\"$title\")
remoteport = $iport
sslline = $sslcert
$ifgoogleline
[Repository $title-local]
type = Maildir
localfolders = $maildir/$title
"
msmtp_header="defaults
auth on
tls on
tls_trust_file $sslcert
logfile ~/.msmtp.log
"
msmtp_profile="
account $title
host $smtp
port $sport
from $login
user $login
passwordeval \"$GPG -d --quiet --for-your-eyes-only --no-tty $creddir/$title.gpg | sed -e '\$a\\'\"
"
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
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-02-21 17:29:21 +01:00
addaccount() { \
2019-02-21 18:07:28 +01:00
printf "Insert the \033[31memail address\033[0m that you want to autoconfigure for mutt/offlineIMAP\\n\\nEmail: "
2019-02-21 17:29:21 +01:00
read -r fulladdr
while ! echo "$fulladdr" | grep "$emailre" >/dev/null; do
2019-02-21 18:07:28 +01:00
printf "That is not a valid \033[31memail address\033[0m, please retype the desired email.\\n\\nEmail: "
2019-02-21 17:29:21 +01:00
read -r fulladdr
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-02-21 16:14:17 +01:00
serverinfo="$(grep "$domain" "$muttdir/domains.csv")"
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"
printf "Insert the IMAP server for your email provider (excluding the port number)\\n"
read -r imap
printf "What is your server's IMAP port number? (Usually something like 993)\\n"
read -r iport
printf "Insert the SMTP server for your email provider (excluding the port number)\\n"
read -r smtp
printf "What is your server's SMTP port number? (Usually 587 or 465)\\n"
read -r sport
2019-02-21 17:39:25 +01:00
printf "\\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
IFS=, read service imap iport smtp sport <<EOF
$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-02-21 18:07:28 +01:00
printf "\\nPress enter to continue.\\n"
stty -echo
read wait
stty echo
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-02-21 18:07:28 +01:00
printf "\033[31mTry again\033[0m. Pick a nickname that is one word only including lowercase letters and _ or -.\\n\tAccount name: "
2019-02-21 17:29:21 +01:00
read -r title
2019-02-21 16:14:17 +01:00
done
2019-02-21 18:07:28 +01:00
printf "If your account has a special username different from your address, insert it now. Otherwise leave this prompt totally blank.\\n\tLogin(?): "
2019-02-21 17:29:21 +01:00
read -r login
2019-02-21 16:14:17 +01:00
[ -z "$login" ] && login="$fulladdr"
2019-02-21 17:29:21 +01:00
if [ "$service" = "gmail.com" ]; then
type="Gmail"; ifgoogleline="folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']"
2019-02-21 18:07:28 +01:00
printf "\033[1mGoogle\033[0m mail account detected. Remember to check the README to make sure of Google-specific settings you must enable.\\n"
2019-02-21 17:29:21 +01:00
else
type="IMAP"; ifgoogleline="remotehost = $imap"
fi
2019-02-21 16:14:17 +01:00
grep "i[0-9]" "$muttdir/personal.muttrc" | 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-21 18:32:20 +01:00
getpass "$title"
2019-02-21 16:14:17 +01:00
getprofiles
mkdir -p "$HOME/.config/offlineimap/" "$HOME/.config/msmtp"
[ ! -f "$HOME/.config/offlineimap/config" ] && echo "$offlineimap_header" > "$HOME/.config/offlineimap/config"
[ ! -f "$HOME/.config/msmtp/config" ] && echo "$msmtp_header" > "$HOME/.config/msmtp/config"
echo "$offlineimap_profile" >> "$HOME/.config/offlineimap/config"
echo "$msmtp_profile" >> "$HOME/.config/msmtp/config"
echo "$mutt_profile" > "$accdir/$title.muttrc"
}
2019-02-21 16:29:50 +01:00
getpass() { \
2019-02-21 18:07:28 +01:00
printf "Now enter your password for the \"%s\" account. Don't worry, this will be encrypted and only you with your GPG key can view it.\\n\tPassword: " "$1"
2019-02-21 17:29:21 +01:00
stty -echo
read -r password
stty echo
echo "$password" > "$tmpdir/$1"
printf "Encrypting your password with %s..."
2019-02-21 16:29:50 +01:00
"$GPG" -r "$gpgemail" --encrypt "$tmpdir/$1"
2019-02-21 17:29:21 +01:00
print "DONE\\nShredding all memory of your password for safety's sake..."
unset password
shred -u "$tmpdir/$1" | rm -f "$tmpdir/$1"
2019-02-21 16:29:50 +01:00
mv "$tmpdir/$1.gpg" "$creddir"
2019-02-21 17:29:21 +01:00
printf "DONE.\\n"
2019-02-21 16:29:50 +01:00
}
2019-02-21 16:14:17 +01:00
askgpg() { \
2019-02-21 17:29:21 +01:00
printf "To safely encrypt passwords, mutt-wizard requires that you have a GPG public/private key pair.\\n\\nPlease input the email address of your GPG key pair below.\\nEmail: "
read -r gpgemail
2019-02-21 16:14:17 +01:00
while ! echo "$gpgemail" | grep "$emailre"; do
2019-02-21 17:29:21 +01:00
printf "That is not a valud email address. Please try again.\\nEmail: "
read -r gpgemail
2019-02-21 16:14:17 +01:00
done
if "$GPG" -K | grep "<$gpgemail>"; then
echo "$gpgemail" > "$creddir/gpgemail"
else
2019-02-21 17:29:21 +01:00
printf "You do not appear to have a private key associated with %s.\\nPlease generate a GPG key pair by running \`%s --full-gen-key\` and rerun the wizard.\\n" "$gpgemail" "$GPG"
exit 1
2019-02-21 16:14:17 +01:00
fi
}
formatShortcut() { \
while read data; do { echo "macro index,pager g$1 \"<change-folder>$data<enter>\" \"Go to $2.\" # autogenerated"
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 ;}
gen_delim() { \
delim="="
for i in $(seq $(( $1 - 1 )))
do
delim="$delim-"
done
echo $delim ;}
2019-02-21 18:32:20 +01:00
trysync() { \
! ping -q -c 1 1.1.1.1 > /dev/null && printf "No internet connection detected.\\nTry rerunning offlineimap manually when connection is established, then select the option to detect mailboxes and finalize installation.\\n" && exit 1
printf "mutt-wizard will run offlineimap briefly to (1) ensure that login details are functional and (2) allow offlineimap to tell us what mailboxes your email account has.\\nAfter around 15 seconds, mutt-wizard will kill the process and continue.\\nYou can run offlineimap manually to finish the mail sync later.\\nPress enter to continue.\\n."
stty -echo
read wait
stty echo
(sleep 15; killall offlineimap; killall offlineimap; killall offlineimap) &
offlineimap -qoa "$title"
exit 0
}
2019-02-21 16:14:17 +01:00
detectMailboxes() { \
2019-02-21 18:32:20 +01:00
boxes="$(ls -d "$maildir/$1/"* 2>/dev/null | sed "s/.*\///;s/^/=/")"
[ -z "$boxes" ] && printf "\033[31mNo local mailboxes have been detected for %s.\033[0m\\nThis means that offlineimap has not been successfully run.\\nRun offlineimap, and if it has an error, be sure to check your password and server settings manually if needbe.\\n" "$1" && exit
echo "$boxes" > "$tmpdir/$1_boxes"
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}')"
delim="$(gen_delim "$sidebar_width")"
2019-02-21 18:32:20 +01:00
printf "Setting default mailboxes for your Inbox, Sent, Drafts and Trash in mutt...\\n"
2019-02-21 16:14:17 +01:00
oneline="$(sed -e "s/^\|$/\"/g" "$tmpdir/$1_boxes" | tr "\n" " ")"
oneline="=$1 $delim $oneline"
2019-02-21 18:32:20 +01:00
spoolfile=$(grep -i "$tmpdir/$1_boxes" -e inbox | sed -e 's/=/+/g' | sed 1q)
record=$(grep -i "$tmpdir/$1_boxes" -e sent | sed -e 's/=/+/g' | sed 1q)
postponed=$(grep -i "$tmpdir/$1_boxes" -e draft | sed -e 's/=/+/g' | sed 1q)
trash=$(grep -i "$tmpdir/$1_boxes" -e trash | sed -e 's/=/+/g' | sed 1q)
{ echo "set spoolfile = \"$spoolfile\""; echo "set record = \"$record\""; echo "set postponed = \"$postponed\""; echo "set trash = \"$trash\""; } >> "$muttdir/accounts/$1.muttrc"
2019-02-21 16:14:17 +01:00
sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" "$muttdir/accounts/$1.muttrc"
echo mailboxes "$oneline" >> "$muttdir/accounts/$1.muttrc"
2019-02-21 18:32:20 +01:00
printf "Setting up your keyboard shortcuts for jumping between mailboxes...\\n"
2019-02-21 16:14:17 +01:00
sed -i "/# autogenerated/d" "$muttdir/accounts/$1.muttrc"
grep -i "$tmpdir/$1_boxes" -e inbox | sed 1q | formatShortcut i inbox "$1"
grep -i "$tmpdir/$1_boxes" -e sent | sed 1q | formatShortcut s sent "$1"
grep -i "$tmpdir/$1_boxes" -e draft | sed 1q | formatShortcut d drafts "$1"
grep -i "$tmpdir/$1_boxes" -e trash | sed 1q | formatShortcut t trash "$1"
grep -i "$tmpdir/$1_boxes" -e spam | sed 1q | formatShortcut S spam "$1"
grep -i "$tmpdir/$1_boxes" -e junk | sed 1q | formatShortcut j junk "$1"
grep -i "$tmpdir/$1_boxes" -e archive | sed 1q | formatShortcut a archive "$1"
2019-02-21 18:32:20 +01:00
printf "All done.\\n"
2019-02-21 16:14:17 +01:00
}
#wipe () { rm "$HOME/.config/offlineimap/config" "$accdir" "$creddir" "$muttdir/personal.muttrc" ;}
[ -z "$gpgemail" ] && askgpg
2019-02-21 18:07:28 +01:00
2019-02-21 17:39:25 +01:00
main() { \
while : ; do
2019-02-21 18:07:28 +01:00
printf "What would you like \033[32mmutt-wizard\033[0m to do?
\033[31m1 Add an email account\033[0m
2019-02-21 17:39:25 +01:00
2 Autodetect mailboxes
3 Change an account's password
0 Exit
Input a number to continue or press ctrl-c.\\n"
read -r choice
case "$choice" in
1) addaccount ;;
0) break ;;
*) printf "Invalid input.\\n"
esac
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"