2018-02-10 07:47:29 +01:00
#!/bin/bash
2018-02-10 19:02:44 +01:00
2018-02-10 07:52:08 +01:00
muttdir = " $HOME /.config/mutt/ "
2018-02-10 07:47:29 +01:00
mkdir -p ~/.config/mutt/credentials
# Email for GPG
youremail = $( \
dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your key pair. This is NOT necessarily the email you want to configure." 10 60 \
3>& 1 1>& 2 2>& 3 3>& - \
)
# Get email address
fulladdr = $( \
dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Insert your full email address." 10 60 \
3>& 1 1>& 2 2>& 3 3>& - \
)
# Check to see if domain is in domain list
2018-02-11 18:07:59 +01:00
serverinfo = $( cat " $muttdir " autoconf/domains.csv | grep -w ^${ fulladdr ##*@ } )
2018-02-10 19:02:44 +01:00
if [ -z " $serverinfo " ] ;
then
echo No suitable match. && exit
else
2018-02-10 07:47:29 +01:00
# Read in server data as variables
IFS = , read service imap iport smtp sport spoolfile postponed record <<EOF
$serverinfo
EOF
2018-02-10 19:02:44 +01:00
fi
2018-02-10 07:47:29 +01:00
realname = $( \
dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Enter the full name you'd like to be identified by on this email account." 10 60 \
3>& 1 1>& 2 2>& 3 3>& - \
)
title = $( \
dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Give a short, one-word name for this email account that will differentiate it from other email accounts." 10 60 \
3>& 1 1>& 2 2>& 3 3>& - \
)
# Sets the repo type and other variables for the sed regex.
if [ [ " $service " = = "gmail.com" ] ] ;
then
type = "Gmail"
delet = "remotehost"
else
type = "IMAP"
delet = "Gmail]\/"
fi
# The replacements
replacement = "
s/\$ realname/$realname /g;
s/\$ title/$title /g;
s/\$ fulladdr/$fulladdr /g;
s/\$ imap/$imap /g;
s/\$ iport/$iport /g;
s/\$ smtp/$smtp /g;
s/\$ sport/$sport /g;
s/\$ spoolfile/$spoolfile /g;
s/\$ postponed/$postponed /g;
s/\$ record/$record /g;
s/\$ type/$type /g;
/$delet /d"
2018-02-11 00:00:14 +01:00
# Gets the first unused shortcut number in the muttrc and puts it in $idnum.
2018-02-11 18:16:08 +01:00
cat " $muttdir " personal.muttrc | grep i[ 0-9] | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
2018-02-11 00:00:14 +01:00
echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9" > /tmp/mutt_all_possible
idnum = $( diff /tmp/mutt_all_possible /tmp/mutt_used | sed -n 2p | awk '{print $2}' )
2018-02-10 19:02:44 +01:00
2018-02-10 07:47:29 +01:00
addAccount( ) {
2018-02-11 18:42:26 +01:00
mkdir -p " $muttdir " accounts/
2018-02-10 19:02:44 +01:00
# First, adding the encrypted password.
dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox " Enter the password for the \" $title \" account. " 10 60 2> /tmp/$title
gpg -r $youremail --encrypt /tmp/$title
shred -u /tmp/$title && echo "Password encrypted and memory shredded."
mv /tmp/$title .gpg ~/.config/mutt/credentials/
# Creating the offlineimaprc if it doesn't exist already.
2018-02-10 17:00:03 +01:00
if [ ! -f ~/.offlineimaprc ] ; then cp " $muttdir " autoconf/offlineimap_header ~/.offlineimaprc; fi
cat " $muttdir " autoconf/offlineimap_profile | sed -e " $replacement " >> ~/.offlineimaprc
2018-02-10 19:02:44 +01:00
2018-02-10 17:00:03 +01:00
# Add the mutt profile.
cat " $muttdir " autoconf/mutt_profile | sed -e " $replacement " > " $muttdir " accounts/$title .muttrc
2018-02-11 00:00:14 +01:00
# Add a numbered shortcut in the muttrc
2018-02-11 18:16:08 +01:00
echo " macro index,pager i $idnum '<sync-mailbox><enter-command>source " $muttdir " accounts/ $title .muttrc<enter><change-folder>!<enter>' " >> " $muttdir " personal.muttrc
2018-02-11 00:00:14 +01:00
# Adding directory structure for cache.
mkdir -p " $muttdir " accounts/$title /cache/bodies " $muttdir " accounts/$title /cache/headers
# Add to offlineimaprc sync list.
sed -i " s/^accounts =.*[a-zA-Z] $/&, $title /g;s/^accounts = $/accounts = $title /g " ~/.offlineimaprc
2018-02-10 19:02:44 +01:00
2018-02-11 00:00:14 +01:00
# Makes account default if there is no default account.
2018-02-11 18:16:08 +01:00
grep " $muttdir " personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
echo " source ${ muttdir } accounts/ $title .muttrc " >> " $muttdir " personal.muttrc ; }
2018-02-10 07:47:29 +01:00
addAccount
2018-02-10 19:02:44 +01:00
clear