# Script to handle automating/interacting with himalaya email server
# $BAT_PATH and $HIMALAYA_PATH are provided by .bashrc, where this
# file is sourced from
alias eml="$HIMALAYA_PATH list -s 10" # List emails in default inbox
alias ems="$HIMALAYA_PATH send" # Provide email via stdin and send
alias emwatch="watch -g -c -n 60 $HIMALAYA_PATH list -s 2" # List emails every 60 seconds, if something changes, stop watching
# Read email passed and page into bat/less
function emr() {
READER="/bin/less"
if [ ! -z $BAT_PATH ]; then
READER=$BAT_PATH
fi
$HIMALAYA_PATH read $1 | $READER
}
# Generate template for new email reply and open in either emacs
# or vim, depending on whether emacs server is running
function emw() {
if [ -z $1 ]; then
echo "Must provide emw an emailID"
return 1
fi
EMAIL_FILE=$(mktemp)
$HIMALAYA_PATH template reply $1 > $EMAIL_FILE
email_file_editor $EMAIL_FILE
email_sender $EMAIL_FILE
}
# Generate template for a new email, as above
function emn() {
EMAIL_FILE=$(mktemp)
$HIMALAYA_PATH template new > $EMAIL_FILE
email_file_editor $EMAIL_FILE
email_sender $EMAIL_FILE
}
function email_file_editor() {
EMAIL_FILE=$1
EMACS_SERVER_RUNNING=$(lsof -c emacs | grep server | wc -l)
if [ $EMACS_SERVER_RUNNING -gt 0 ]; then
emacsclient -c $EMAIL_FILE
else
$EDITOR $EMAIL_FILE
fi
}
function email_sender() {
EMAIL_FILE=$1
cat $EMAIL_FILE
echo -n "Do you want to send this email [y/n]: "
read USER_REPONSE
if [[ "$USER_REPONSE" == *"y"* ]]; then
echo "Sending email..."
cat -p $EMAIL_FILE | $HIMALAYA_PATH send
rm $EMAIL_FILE
else
echo "Aborting, email file at: $EMAIL_FILE"
fi
}
function clean_old_emails() {
EMAILS_MATCHING=$(grep Content /tmp/tmp.* | cut -d ":" -f 1 | wc -l)
echo -n "Delete $EMAILS_MATCHING old emails: [y/n]: "
read USER_REPONSE
if [[ "$USER_REPONSE" == *"y"* ]]; then
for file in $(grep Content /tmp/tmp.* | cut -d ":" -f 1); do rm $file; done
else
echo "Not deleting..."
fi
}