himalaya_shell_ui.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Script to handle automating/interacting with himalaya email server
  2. # $BAT_PATH and $HIMALAYA_PATH are provided by .bashrc, where this
  3. # file is sourced from
  4. alias eml="$HIMALAYA_PATH list" # List emails in default inbox
  5. alias ems="$HIMALAYA_PATH send" # Provide email via stdin and send
  6. # Read email passed and page into bat/less
  7. function emr() {
  8. READER="/bin/less"
  9. if [ ! -z $BAT_PATH ]; then
  10. READER=$BAT_PATH
  11. fi
  12. $HIMALAYA_PATH read $1 | $READER
  13. }
  14. # Generate template for new email reply and open in either emacs
  15. # or vim, depending on whether emacs server is running
  16. function emw() {
  17. if [ -z $1 ]; then
  18. echo "Must provide emw an emailID"
  19. return 1
  20. fi
  21. EMAIL_FILE=$(mktemp)
  22. $HIMALAYA_PATH template reply $1 > $EMAIL_FILE
  23. email_file_editor $EMAIL_FILE
  24. email_sender $EMAIL_FILE
  25. }
  26. # Generate template for a new email, as above
  27. function emn() {
  28. EMAIL_FILE=$(mktemp)
  29. $HIMALAYA_PATH template new > $EMAIL_FILE
  30. email_file_editor $EMAIL_FILE
  31. email_sender $EMAIL_FILE
  32. }
  33. function email_file_editor() {
  34. EMAIL_FILE=$1
  35. EMACS_SERVER_RUNNING=$(lsof -c emacs | grep server | wc -l)
  36. if [ $EMACS_SERVER_RUNNING -gt 0 ]; then
  37. emacsclient -c $EMAIL_FILE
  38. else
  39. $EDITOR $EMAIL_FILE
  40. fi
  41. }
  42. function email_sender() {
  43. EMAIL_FILE=$1
  44. cat $EMAIL_FILE
  45. echo -n "Do you want to send this email [y/n]: "
  46. read USER_REPONSE
  47. if [[ "$USER_REPONSE" == *"y"* ]]; then
  48. echo "Sending email..."
  49. cat -p $EMAIL_FILE | $HIMALAYA_PATH send
  50. rm $EMAIL_FILE
  51. else
  52. echo "Aborting, email file at: $EMAIL_FILE"
  53. fi
  54. }
  55. function clean_old_emails() {
  56. EMAILS_MATCHING=$(grep Content /tmp/tmp.* | cut -d ":" -f 1 | wc -l)
  57. echo -n "Delete $EMAILS_MATCHING old emails: [y/n]: "
  58. read USER_REPONSE
  59. if [[ "$USER_REPONSE" == *"y"* ]]; then
  60. for file in $(grep Content /tmp/tmp.* | cut -d ":" -f 1); do rm $file; done
  61. else
  62. echo "Not deleting..."
  63. fi
  64. }