himalaya_shell_ui.sh 2.0 KB

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