2018-04-13 00:04:02 +02:00
#!/usr/bin/env bash
2018-02-10 19:02:44 +01:00
2018-08-21 20:38:20 +02:00
[ " $( uname) " = = "Darwin" ] && os = ".macos"
2018-03-13 05:55:21 +01:00
2018-09-20 04:38:21 +02:00
muttdir = " $HOME /.config/mutt "
2018-08-26 10:34:31 +02:00
namere = " ^[a-z_][a-z0-9_-]* $"
emailre = ".+@.+\..+"
2018-02-10 07:47:29 +01:00
2018-04-11 00:15:26 +02:00
createMailboxes( ) { \
tmpdir = $( mktemp -d)
2018-10-14 19:02:40 +02:00
offlineimap --info -a " $1 " 2& > " $tmpdir " /log
2018-04-11 00:15:26 +02:00
sed -n '/^Folderlist/,/^Folderlist/p' " $tmpdir " /log |
grep "^ " | sed -e "s/\//./g;s/(.*//g;s/^ //g" > " $tmpdir " /lognew
while read box; do mkdir -p " $HOME /.mail/ $1 / $box " ; done <" $tmpdir " /lognew ; }
2018-03-16 20:10:16 +01:00
2018-07-24 03:59:38 +02:00
testSync( ) { ( crontab -l | grep mailsync.sh && removeSync) || addSync ; }
2018-03-06 07:38:22 +01:00
addSync( ) { min = $( dialog --inputbox "How many minutes should be between mail syncs?" 8 60 3>& 1 1>& 2 2>& 3 3>& -)
2018-09-20 04:38:21 +02:00
( crontab -l; echo " */ $min * * * * eval \"export $( egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$( pgrep -u $LOGNAME -x i3) /environ) \"; " $muttdir "/etc/mailsync.sh" ) | crontab - &&
2018-03-06 07:38:22 +01:00
dialog --msgbox "Cronjob successfully added. Remember you may need to restart or tell systemd/etc. to start your cron manager for this to take effect." 7 60 ; }
2018-07-24 03:59:38 +02:00
removeSync( ) { ( ( crontab -l | sed -e '/mailsync.sh/d' ) | crontab - >/dev/null) && dialog --msgbox "Cronjob successfully removed. To reactivate, select this option again." 6 60 ; }
2018-03-06 07:38:22 +01:00
2018-02-14 02:44:53 +01:00
changePassword( ) { \
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>& - )
2018-08-26 10:34:31 +02:00
while ! [ [ " ${ gpgemail } " = ~ ${ emailre } ] ] ; do
gpgemail = $( dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>& 1 1>& 2 2>& 3 3>& 1)
done
2018-02-14 02:44:53 +01:00
dialog --title "Luke's mutt/offlineIMAP password wizard" --passwordbox " Enter the new password for the \" $1 \" account. " 10 60 2> /tmp/$1
2018-10-14 19:02:40 +02:00
gpg2 -r " $gpgemail " --encrypt /tmp/$1 || ( 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-14 02:44:53 +01:00
shred -u /tmp/$1
2018-09-20 04:38:21 +02:00
mv /tmp/$1 .gpg " $muttdir " /credentials/
2018-03-17 17:27:50 +01:00
dialog --title "Finalizing your account." --infobox " The account \" $title \"'s password has been changed. Now attempting to configure mail directories...
This may take several seconds..." 10 70
2018-10-14 19:02:40 +02:00
createMailboxes " $title " || ( clear && exit )
detectMailboxes " $title "
dialog --title "Password changed." --msgbox " Your \" $fulladdr \" password has been changed. To start the download of your mail, you can manually run \`offlineimap -a $title \` in a terminal. The first sync may take some time depending on the amount of your mail. " 8 60 ; }
2018-02-14 02:44:53 +01:00
2018-09-20 04:38:21 +02:00
chooseDetect( ) { for x in $( grep "^accounts =" ~/.offlineimaprc | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' ) ; do detectMailboxes $x ; done && detectSuccess ; }
2018-02-14 22:58:02 +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
2018-09-20 04:38:21 +02:00
echo " macro index,pager g $1 \"<change-folder> $data <enter>\" \"Go to $2 .\" " >> " $muttdir " /accounts/$3 .muttrc
2018-02-13 20:15:43 +01:00
done ; }
2018-02-26 22:23:40 +01:00
gen_delim( ) { \
delim = "="
for i in ` seq $(( $1 - 1 )) `
do
delim = " $delim - "
done
echo $delim ; }
2018-02-13 20:15:43 +01:00
detectMailboxes( ) { \
find ~/.mail/$1 -maxdepth 1 -mindepth 1 -type d | sed -e "s/.*\///g;s/^/=/g" > /tmp/$1 _boxes
2018-09-20 04:38:21 +02:00
sidebar_width = $( sed -n -e '/^set sidebar_width/p' " $muttdir " /muttrc | awk -F'=' '{print $2}' )
2018-02-26 22:23:40 +01:00
delim = $( gen_delim $sidebar_width )
2018-09-20 04:38:21 +02:00
oneline = $( sed -e " s/^\| $/\"/g " /tmp/$1 _boxes | tr "\n" " " )
2018-02-26 22:23:40 +01:00
oneline = " = $1 $delim $oneline "
2018-09-20 04:38:21 +02:00
sed -i "/^mailboxes\|^set record\|^set postponed\|^set trash\|^set spoolfile/d" " $muttdir " /accounts/$1 .muttrc
echo mailboxes $oneline >> " $muttdir " /accounts/$1 .muttrc
sed -i "/^macro index,pager g/d" " $muttdir " /accounts/$1 .muttrc
2018-07-08 15:55:50 +02:00
grep -i /tmp/$1 _boxes -e inbox | sed 1q | formatShortcut i inbox $1
2018-02-21 16:12:46 +01:00
grep -i /tmp/$1 _boxes -e sent | sed 1q | formatShortcut s sent $1
grep -i /tmp/$1 _boxes -e draft | sed 1q | formatShortcut d drafts $1
grep -i /tmp/$1 _boxes -e trash | sed 1q | formatShortcut t trash $1
grep -i /tmp/$1 _boxes -e spam | sed 1q | formatShortcut S spam $1
grep -i /tmp/$1 _boxes -e archive | sed 1q | formatShortcut a archive $1
2018-07-08 15:55:50 +02:00
spoolfile = $( grep -i /tmp/$1 _boxes -e inbox | sed -e 's/=/+/g' | sed 1q)
2018-02-21 16:07:59 +01:00
record = $( grep -i /tmp/$1 _boxes -e sent | sed -e 's/=/+/g' | sed 1q)
postponed = $( grep -i /tmp/$1 _boxes -e draft | sed -e 's/=/+/g' | sed 1q)
2018-05-05 19:20:58 +02:00
trash = $( grep -i /tmp/$1 _boxes -e trash | sed -e 's/=/+/g' | sed 1q)
2018-09-20 04:38:21 +02:00
echo " set spoolfile = \" $spoolfile \" " >> " $muttdir " /accounts/$1 .muttrc
echo " set record = \" $record \" " >> " $muttdir " /accounts/$1 .muttrc
echo " set postponed = \" $postponed \" " >> " $muttdir " /accounts/$1 .muttrc
echo " set trash = \" $trash \" " >> " $muttdir " /accounts/$1 .muttrc ; }
2018-02-13 20:15:43 +01:00
2018-02-14 22:58:02 +01:00
# Get all accounts in ~/.offlineimaprc and load into variable `accounts`.
getAccounts( ) { \
2018-09-20 04:38:21 +02:00
grep "^accounts =" ~/.offlineimaprc | sed -e 's/accounts =\( \)//g;s/\(,\) /\n/g;' | nl --number-format= ln > /tmp/numbered
2018-02-12 01:58:16 +01:00
accounts = ( )
while read n s ; do
accounts += ( $n " $s " off)
2018-02-14 22:58:02 +01:00
done < /tmp/numbered ; }
2018-02-10 07:47:29 +01:00
2018-02-14 22:58:02 +01:00
# Yields a menu of available accounts.
inventory( ) { \
getAccounts
2018-02-14 23:01:20 +01:00
choices = $( dialog --separate-output --checklist "Select all desired email accounts with <SPACE>." 15 40 16 " ${ accounts [@] } " 2>& 1 >/dev/tty)
2018-02-12 01:58:16 +01:00
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
2018-09-20 04:38:21 +02:00
rm " $muttdir " /accounts/$1 .muttrc
rm " $muttdir " /credentials/$1 .gpg
rm -rf " $muttdir " /accounts/$1
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>& -)
2018-02-18 16:32:53 +01:00
smtp = $( dialog --inputbox "Insert the SMTP server for your email provider (excluding the port number)" 10 60 3>& 1 1>& 2 2>& 3 3>& - )
2018-02-12 02:06:31 +01:00
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-08-26 10:34:31 +02:00
while ! [ [ " ${ fulladdr } " = ~ ${ emailre } ] ] ; do
fulladdr = $( dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "That's not a valid email address. Please input the entire address." 10 60 3>& 1 1>& 2 2>& 3 3>& 1)
done
2018-02-10 07:47:29 +01:00
# Check to see if domain is in domain list
2018-09-20 04:38:21 +02:00
serverinfo = $( grep -w ^${ fulladdr ##*@ } " $muttdir " /autoconf/domains.csv)
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>& - )
2018-08-26 10:13:54 +02:00
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>& 1) || exit
while ! [ [ " ${ title } " = ~ ${ namere } ] ] ; do
title = $( dialog --no-cancel --title "Luke's mutt/offlineIMAP autoconfig" --inputbox "Account title not valid. Give a username beginning with a letter, with only lowercase letters, - or _." 10 60 3>& 1 1>& 2 2>& 3 3>& 1)
done
2018-03-22 16:23:46 +01:00
login = $( dialog --title "Luke's mutt/offlineIMAP autoconfig" --inputbox " If you have a username for the \" $title \" account which is different from your email address, enter it here. Otherwise leave this prompt blank. " 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
2018-02-18 16:53:26 +01:00
if [ [ -z " $login " ] ] ;
then
login = $fulladdr
fi
2018-02-10 07:47:29 +01:00
# 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;
2018-02-18 15:37:51 +01:00
s/\$ login/$login /g;
2018-02-10 07:47:29 +01:00
/$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-10-14 19:02:40 +02:00
grep "i[0-9]" " $muttdir /personal.muttrc " | awk '{print $3}' | sed -e 's/i//g' > /tmp/mutt_used
echo -e "1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9" > /tmp/mutt_all_possible
2018-02-11 00:00:14 +01:00
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-10-14 19:02:40 +02:00
shred -u " /tmp/ $title "
mv " /tmp/ $title .gpg " " $muttdir " /credentials/
2018-02-10 19:02:44 +01:00
2018-02-21 06:33:26 +01:00
# Adding directory structure for cache.
2018-10-14 19:02:40 +02:00
mkdir -p " $muttdir /accounts/ $title /cache/bodies "
2018-02-21 06:33:26 +01:00
2018-02-10 19:02:44 +01:00
# Creating the offlineimaprc if it doesn't exist already.
2018-09-20 04:38:21 +02:00
if [ ! -f ~/.offlineimaprc ] ; then cp " $muttdir " /autoconf/offlineimap_header" $os " ~/.offlineimaprc; fi
sed -e " $replacement " " $muttdir " /autoconf/offlineimap_profile" $os " >> ~/.offlineimaprc
2018-10-14 19:02:40 +02:00
mkdir -p " $HOME /.mail/ $title "
2018-02-10 19:02:44 +01:00
2018-08-21 20:38:20 +02:00
# Creating msmtprc if it doesn't exist already.
2018-09-20 04:38:21 +02:00
if [ ! -f ~/.msmtprc ] ; then cp " $muttdir " /autoconf/msmtprc_header ~/.msmtprc; fi
sed -e " $replacement " " $muttdir " /autoconf/msmtprc_profile >> ~/.msmtprc
2018-08-21 20:38:20 +02:00
2018-02-10 17:00:03 +01:00
# Add the mutt profile.
2018-10-14 19:02:40 +02:00
sed -e " $replacement " " $muttdir " /autoconf/mutt_profile > " $muttdir /accounts/ $title .muttrc "
2018-02-11 00:00:14 +01:00
# Add a numbered shortcut in the muttrc
2018-10-14 19:02:40 +02:00
echo " macro index,pager i $idnum '<sync-mailbox><enter-command>source \" $muttdir \"/accounts/ $title .muttrc<enter><change-folder>!<enter>;<check-stats>' " >> " $muttdir " /personal.muttrc
2018-02-11 00:00:14 +01:00
# Add to offlineimaprc sync list.
2018-10-14 19:02:40 +02:00
sed -i.bu " s/^accounts =.*[a-zA-Z] $/&, $title /g;s/^accounts =\\s* $/accounts = $title /g " ~/.offlineimaprc && rm ~/.offlineimaprc.bu
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-09-20 04:38:21 +02:00
grep " $muttdir " /personal.muttrc -e "^source .*accounts.*" >/dev/null && echo there || \
echo " source ${ muttdir } /accounts/ $title .muttrc " >> " $muttdir " /personal.muttrc
2018-02-17 06:56:13 +01:00
2018-03-16 20:10:16 +01:00
dialog --title "Finalizing your account." --infobox " The account \" $title \" has been added. Now attempting to configure mail directories...
2018-02-17 06:56:13 +01:00
2018-03-16 20:10:16 +01:00
This may take several seconds..." 10 70
2018-10-14 19:02:40 +02:00
createMailboxes " $title " || ( clear && exit )
detectMailboxes " $title "
dialog --title "Account added." --msgbox " Your \" $fulladdr \" account has been added. To start the download of your mail, you can manually run \`offlineimap -a $title \` in a terminal. The first sync may take some time depending on the amount of your mail. " 8 60 ; }
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.
2018-02-17 06:56:13 +01:00
chooseAdd( ) { \
2018-09-20 04:38:21 +02:00
mkdir -p " $muttdir " /credentials/ " $muttdir " /accounts/
2018-02-12 01:58:16 +01:00
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
2018-02-17 06:56:13 +01:00
dialog --title "Luke's mutt/offlineIMAP password wizard" --yesno "Would you like to add another email account?" 5 60 || break
2018-02-12 01:58:16 +01:00
addloop
done ; }
2018-10-14 19:02:40 +02:00
wipe ( ) { rm " $HOME /.offlineimaprc "
2018-02-12 01:58:16 +01:00
rm -rf " $muttdir " /accounts
2018-09-20 04:38:21 +02:00
rm -f " $muttdir " /credentials/*gpg
rm " $muttdir " /personal.muttrc ; }
2018-02-12 01:58:16 +01:00
2018-02-11 21:56:08 +01:00
while : ;
2018-02-12 01:58:16 +01:00
do
2018-02-14 23:19:15 +01:00
choice = $( dialog --title "Luke's mutt/offlineIMAP wizard" --nocancel \
2018-03-19 06:43:50 +01:00
--menu "What would you like to do?" 15 45 8 \
2018-02-12 01:58:16 +01:00
0 "List all email accounts configured." \
1 "Add an email account." \
2018-03-16 20:34:46 +01:00
2 "Enable/disable autosync." \
2018-03-19 06:43:50 +01:00
3 "Redetect mailboxes." \
4 "Change an account's password." \
5 "Remove an email account." \
6 "Remove all email accounts." \
7 "Exit this wizard." \
2018-02-14 06:43:16 +01:00
3>& 1 1>& 2 2>& 3 3>& 1 )
2018-02-12 01:58:16 +01:00
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;;
2018-02-17 06:56:13 +01:00
1) chooseAdd; ;
2018-10-04 22:04:27 +02:00
2) testSync; ;
2018-03-19 06:43:50 +01:00
3) detectWarning && chooseDetect; ;
2018-10-14 19:02:40 +02:00
4) inventory && for i in $userchoices ; do changePassword " $i " ; done ; ;
5) inventory && for i in $userchoices ; do removeAccount " $i " ; done ; ;
2018-03-19 06:43:50 +01:00
6) ( 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) ; ;
7) clear && break ; ;
2018-04-13 00:03:33 +02:00
*) echo "Unable to read response from dialog. Exiting." >& 2; exit 2
2018-02-12 01:58:16 +01:00
esac
2018-02-11 21:56:08 +01:00
done