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
2018-02-13 20:50:49 +01:00
detectWarning( ) { \
dialog --title "Mailbox detect requirement" --yesno " In order for the mailbox detection system to work, you must have
1) already have added the email account with this wizard, and
2) already have run offlineimap at least once to synchronize your mail.
This detection system only works if you already have begun your first sync. If you have, press 'yes' to continue ." 12 70 ;}
detectSuccess( ) { dialog --title "Mailbox detect complete." --msgbox " The script has now smartly detected your different mailboxes and has enabled them in the sidebar and given you keyboard shortcuts as below:
gi for move to the Inbox.
gs for move to Sent Mail.
gd for move to Drafts
gS for move to Spam
ga for move to the Archive.
gj for move to Junk
gt for move to Trash
These shortcuts will only work if your email system does have that particular folder ( i.e. if your email system has a Junk folder, but not a Trash folder, 'gt' will not work, etc." 20 60 ;}
2018-02-13 20:15:43 +01:00
formatShortcut( ) { \
while read data; do
echo " macro index,pager g $1 \"<change-folder> $data <enter>\" \"Go to $2 .\" " >> " $muttdir " accounts/$3 .muttrc
done ; }
detectMailboxes( ) { \
find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1 _boxes
oneline = $( cat /tmp/$1 _boxes | tr "\n" " " )
2018-02-13 20:50:49 +01:00
sed -i "/^mailboxes\|^set spoolfile\|^set record\|^set postponed/d" " $muttdir " accounts/$1 .muttrc
2018-02-13 20:15:43 +01:00
echo mailboxes $oneline >> " $muttdir " accounts/$1 .muttrc
sed -i "/^macro index,pager g/d" " $muttdir " accounts/$1 .muttrc
2018-02-13 21:35:44 +01:00
grep -vi /tmp/$1 _boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail" | sort -n | sed 1q | formatShortcut i inbox $1
2018-02-13 20:15:43 +01:00
grep -i /tmp/$1 _boxes -e sent | formatShortcut s sent $1
2018-02-13 20:50:49 +01:00
grep -i /tmp/$1 _boxes -e draft | formatShortcut d drafts $1
2018-02-13 20:15:43 +01:00
grep -i /tmp/$1 _boxes -e trash | formatShortcut t trash $1
grep -i /tmp/$1 _boxes -e spam | formatShortcut S spam $1
2018-02-13 20:50:49 +01:00
grep -i /tmp/$1 _boxes -e archive | formatShortcut a archive $1
2018-02-13 21:35:44 +01:00
spoolfile = $( grep -vi /tmp/$1 _boxes -e "trash\|drafts\|sent\|trash\|spam\|junk\|archive\|chat\|old\|new\|gmail" | sort -n | sed 1q | sed -e 's/=/+/g' )
2018-02-13 20:50:49 +01:00
record = $( grep -i /tmp/$1 _boxes -e sent | sed -e 's/=/+/g' )
postponed = $( grep -i /tmp/$1 _boxes -e draft | sed -e 's/=/+/g' )
echo " set spoolfile = \" $spoolfile \" " >> " $muttdir " accounts/$1 .muttrc
echo " set record = \" $record \" " >> " $muttdir " accounts/$1 .muttrc
echo " set postponed = \" $postponed \" " >> " $muttdir " accounts/$1 .muttrc ; }
2018-02-13 20:15:43 +01:00
2018-02-12 01:58:16 +01:00
# Sees what accounts have been generated bny the wizard
# by checking ~/.offlineimap and yields a menu of them.
inventory( ) { \
cat ~/.offlineimaprc | grep "^accounts =" | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format= ln > /tmp/numbered
accounts = ( )
while read n s ; do
accounts += ( $n " $s " off)
done < /tmp/numbered
2018-02-10 07:47:29 +01:00
2018-02-12 01:58:16 +01:00
choices = $( dialog --separate-output --checklist "Choose an email account." 22 76 16 " ${ accounts [@] } " 2>& 1 >/dev/tty)
if [ -z " $choices " ] ;
then
clear
else
userchoices = $( IFS = "|" ; keys = " ${ choices [*] } " ; keys = " ${ keys //|/ \\ | } " ; grep -w " ${ keys } " /tmp/numbered | awk '{print $2}' )
fi ; }
removeAccount( ) { sed -ie "
/Account $1 ] /,/Account/{ //!d}
/Account $1 ] /d
s/ $1 \( ,\| $\) //g
s/= $1 \( ,\| $\) /= /g
s/,$//g
" ~/.offlineimaprc
rm " $muttdir " accounts/$1 .muttrc
rm " $muttdir " credentials/$1 .gpg
rm -rf " $muttdir " accounts/$1
2018-02-12 02:42:57 +01:00
sed -i '/$1.muttrc/d' " $muttdir " personal.muttrc ; }
2018-02-12 01:58:16 +01:00
2018-02-12 02:06:31 +01:00
manual( ) { \
imap = $( dialog --inputbox "Insert the IMAP server for your email provider (excluding the port number)" 10 60 3>& 1 1>& 2 2>& 3 3>& - )
iport = $( dialog --inputbox "What is your server's IMAP port number? (Usually 993)" 10 60 3>& 1 1>& 2 2>& 3 3>& -)
smtpserver = $( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>& 1 1>& 2 2>& 3 3>& - )
sport = $( dialog --inputbox "What is your server's SMTP port number? (Usually 587 or 465)" 10 60 3>& 1 1>& 2 2>& 3 3>& - ) ; }
2018-02-12 01:58:16 +01:00
addloop( ) { fulladdr = $( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Insert the full email address for the account you want to configure." 10 60 3>& 1 1>& 2 2>& 3 3>& - )
2018-02-10 07:47:29 +01:00
# 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
2018-02-12 02:06:31 +01:00
manual
2018-02-10 19:02:44 +01:00
else
2018-02-10 07:47:29 +01:00
# Read in server data as variables
2018-02-13 21:10:05 +01:00
IFS = , read service imap iport smtp sport <<EOF
2018-02-10 07:47:29 +01:00
$serverinfo
EOF
2018-02-10 19:02:44 +01:00
fi
2018-02-11 21:56:08 +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>& - )
2018-02-10 07:47:29 +01:00
# 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/\$ 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-11 21:56:08 +01:00
addAccount \
; }
2018-02-10 19:02:44 +01:00
2018-02-10 07:47:29 +01:00
addAccount( ) {
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
2018-02-13 21:00:48 +01:00
gpg2 -r $gpgemail --encrypt /tmp/$title || ( dialog --title "GPG decryption failed." --msgbox "GPG decryption failed. This is either because you do not have a GPG key pair or because your distro uses GPG1 and you thus need to symlink /usr/bin/gpg2 to /usr/bin/gpg." 7 60 && break )
2018-02-12 01:58:16 +01:00
shred -u /tmp/$title
2018-02-10 19:02:44 +01:00
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.
2018-02-12 00:41:06 +01:00
mkdir -p " $muttdir " accounts/$title /cache/bodies
2018-02-11 00:00:14 +01:00
# 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
2018-02-12 01:58:16 +01:00
# This is run when a user chooses to add an account.
addChosen( ) { \
mkdir -p " $muttdir " credentials/ " $muttdir " accounts/
gpgemail = $( dialog --title "Luke's mutt/offlineIMAP password wizard" --inputbox "Insert the email address with which you originally created your GPG key pair. This is NOT necessarily the email you want to configure." 10 60 3>& 1 1>& 2 2>& 3 3>& - )
addloop
while : ;
do
dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 10 60 || break
addloop
done ; }
wipe ( ) { rm $HOME /.offlineimaprc
rm -rf " $muttdir " /accounts
rm -f " $muttdir " credentials/*gpg
rm " $muttdir " personal.muttrc ; }
2018-02-11 21:56:08 +01:00
while : ;
2018-02-12 01:58:16 +01:00
do
choice = $( dialog --title "Luke's mutt/offlineIMAP wizard" \
2018-02-13 20:15:43 +01:00
--menu "What would you like to do?" 13 45 6 \
2018-02-12 01:58:16 +01:00
0 "List all email accounts configured." \
1 "Add an email account." \
2018-02-13 20:15:43 +01:00
2 "Auto-detect mailboxes for an account." \
3 "Remove an email account." \
4 "Remove all email accounts." \
5 "Exit this wizard." \
2018-02-12 01:58:16 +01:00
3>& 1 1>& 2 2>& 3 3>& 1 )
case $choice in
0) dialog --title "Accounts detected" --msgbox " The following accounts have been detected:
$( grep ~/.offlineimaprc -e "^accounts =" | sed 's/accounts =//g' )
" 6 60;;
1) addChosen; ;
2018-02-13 20:50:49 +01:00
2) detectWarning && inventory && for i in $userchoices ; do detectMailboxes $i ; done && detectSuccess; ;
2018-02-13 20:15:43 +01:00
3) inventory && for i in $userchoices ; do removeAccount $i ; done ; ;
4) ( dialog --defaultno --title "Wipe all custom neomutt/offlineIMAP settings?" --yesno "Would you like to wipe all of the mutt/offlineIMAP settings generated by the system?" 6 60 && wipe) ; ;
5) clear && break
2018-02-12 01:58:16 +01:00
esac
2018-02-11 21:56:08 +01:00
done