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