Skip to content

Instantly share code, notes, and snippets.

@milesrout
Created March 28, 2022 22:34
Show Gist options
  • Save milesrout/c4b0ed463b0a366870a41edb09645b69 to your computer and use it in GitHub Desktop.
Save milesrout/c4b0ed463b0a366870a41edb09645b69 to your computer and use it in GitHub Desktop.
mbsync_std_account() {
local IFS=$' \t\n'
local id=$1
local host=$2
local addr=$3
local user=$addr
local -a mailboxes=( "${@:4}" )
if [[ "$id" == "@" ]]; then
id="${3//[@\.]/-}"
fi
read -d '' imap_base <<HERE
IMAPAccount ${id}
Host ${host}
User ${user}
PassCmd "pass 'Email/$addr'"
SSLType IMAPS
CertificateFile /etc/ssl/certs/ca-certificates.crt
IMAPStore ${id}-remote
Account ${id}
MaildirStore ${id}-local
SubFolders Verbatim
Path ~/mail/${id}/
Inbox ~/mail/${id}/INBOX
Channel ${id}-inbox
Far :${id}-remote:
Near :${id}-local:
Patterns INBOX
HERE
local imap_mailboxes
declare -a imap_mailboxes=( "$imap_base"$'\n' )
for mailbox in "${mailboxes[@]}"; do
readarray -d '=' <<<$mailbox
local mblocal="${MAPFILE[0]%=}"
local mbremote="${MAPFILE[1]%$'\n'}"
if [[ ! "$mblocal" =~ ^[a-zA-Z0-9_]+$ ]]; then
mblocal="\"$mblocal\""
fi
if [[ ! "$mbremote" =~ ^[a-zA-Z0-9_]+$ ]]; then
mbremote="\"$mbremote\""
fi
read -d '' imap_mailbox <<HERE
Channel ${id}-${mblocal}
Far :${id}-remote:${mbremote}
Near :${id}-local:${mblocal}
HERE
imap_mailboxes+=( "$imap_mailbox"$'\n' )
done
local imap_channels
declare -a imap_channels=()
for mailbox in "${mailboxes[@]}"; do
readarray -d '=' <<<$mailbox
local mblocal="${MAPFILE[0]%=}"
read -d '' imap_channel <<HERE
Channel ${id}-${mblocal}
HERE
imap_channels+=( "$imap_channel" )
done
local IFS=$'\n'
echo
echo '##########################################################'
echo
echo "${imap_mailboxes[*]}"
echo "Group ${id}"
echo "Channel ${id}-inbox"
echo "${imap_channels[*]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment