Bläddra i källkod

Merge pull request #2 from rpdelaney/master

A couple small suggestions
Siddharth Dushantha 5 år sedan
förälder
incheckning
903879b11a
1 ändrade filer med 29 tillägg och 40 borttagningar
  1. 29 40
      tmpmail

+ 29 - 40
tmpmail

@@ -6,8 +6,7 @@
 #
 
 export LC_ALL=C
-export LC_CTYPE=C 
-export LANG=C
+export LC_CTYPE=C
 
 VERSION=1.0.3
 
@@ -25,7 +24,7 @@ RAW_TEXT=false
 TMPMAIL_DIR="/tmp/tmpmail/"
 
 # TMPMAIL_EMAIL_ADDRESS is where we store the temporary email address
-# that gets generated. This prevents the user from providing 
+# that gets generated. This prevents the user from providing
 # the email address everytime they run tmpmail
 TMPMAIL_EMAIL_ADDRESS="$TMPMAIL_DIR/email_address"
 TMPMAIL_HTML_EMAIL="$TMPMAIL_DIR/tmpmail.html"
@@ -52,9 +51,9 @@ generate_email_address(){
     # There are 2 ways which this function is called in this script.
     #  [1] The user wants to generate a new email and runs 'tmpmail --generate'
     #  [2] The user runs 'tmpmail' to check the inbox , but /tmp/tmpmail/email_address
-    #      is empty or nonexistant. Therefore a new email gets automatically 
-    #      generated before showing the inbox. But of course the inbox will 
-    #      be empty as the newly generated email address has not been 
+    #      is empty or nonexistant. Therefore a new email gets automatically
+    #      generated before showing the inbox. But of course the inbox will
+    #      be empty as the newly generated email address has not been
     #      sent any emails.
     #
     # When the function 'generate_email()' is called with the arguement
@@ -76,17 +75,12 @@ generate_email_address(){
     # giving us a text which only contains lower case letters form A to Z. We then take
     # the first 10 characters, which will be the username of the email address
     USERNAME=$(head /dev/urandom | tr -dc a-z | cut -c1-11)
-    
+
     # This is an array of the valid TLDS which 1secmail provides.
     TLDS=(com net org)
 
     # Randomly pick one of the TLDS mentiond above.
-    # This is done by first echoing all the TLDS into 'tr' which then
-    # replaces the white spaces with a new line. This is done because we use 
-    # 'sort' to put the TLDS in a random order, but 'sort' requires the TLDS to
-    # be on seperate lines. After the randomizing is done we take the first line 
-    # which is one of the TLDS
-    TLD=$(echo "${TLDS[@]}" | tr " " "\n" | sort -R | head -n 1)
+    TLD=${TLDS[$RANDOM % ${#TLDS[@]} ]}
 
     # Save the generated email address to the $TMPMAIL_EMAIL_ADDRESS file
     # so that it can be whenever 'tmpmail' is run
@@ -131,22 +125,22 @@ list_emails(){
 
     # This is where we store all of our emails, which is then
     # displayed using 'column'
-    INBOX=""
+    INBOX=()
 
     # This for loop goes through each mail that have been received.
     #
     # Since we need to go through all the data, we need to tell our for loop
     # to loop from 1 to X, where X is legnth of the $DATA which contains all
     # the emails.
-    # 
+    #
     # Normally to loop from 1 to 5, we would use shell expansion and write:
-    #   for index in {1..5}; do 
+    #   for index in {1..5}; do
     #       do_something
     #   done
     #
     # But we a minor issue. We dont know what the final number is, and we are not allowed
     # use to variables in shell expansions like this:
-    #   {1..$X} 
+    #   {1..$X}
     #
     # where $X is the length of the $DATA.
     #
@@ -162,29 +156,24 @@ list_emails(){
     #
     # We can then put those results into the foor loop
     for index in $(seq 1 "$DATA_LENGTH"); do
-	# Since arrays in JSON data start at 0, we must subtract
-	# the value of $index by 1 so that we dont miss one of the
-	# emails in the array
+    # Since arrays in JSON data start at 0, we must subtract
+    # the value of $index by 1 so that we dont miss one of the
+    # emails in the array
         MAIL_DATA=$(jq -r ".[$index-1]" <<< "$DATA")
 
         ID=$(jq -r ".id" <<< "$MAIL_DATA")
         FROM=$(jq -r ".from" <<< "$MAIL_DATA")
         SUBJECT=$(jq -r ".subject" <<< "$MAIL_DATA")
 
-	# The '||' are used as a divideder for 'column'. 'column' will use this divider as
-	# a point of reference to create the division. By default 'column' uses a blank space
-	# but that would not work in our case as the email subject could have multiple white spaces
-	# and 'column' would split the words that are seperated by white space, in different columns.
-	#
-	# Yes, there a double quote all by it self on the line under this one.
-	# This serves as a new line. So that the 'column' command works properly.
-        # I know I could have used '\n' but 'column' does not intepret it as a new line.
-        INBOX+="$ID ||$FROM ||$SUBJECT
-"
+    # The '||' are used as a divideder for 'column'. 'column' will use this divider as
+    # a point of reference to create the division. By default 'column' uses a blank space
+    # but that would not work in our case as the email subject could have multiple white spaces
+    # and 'column' would split the words that are seperated by white space, in different columns.
+        INBOX+=("$ID ||$FROM ||$SUBJECT")
     done
 
     # Show the emails cleanly
-    column -t -s "||" <<< "$INBOX" 
+    column -t -s "||" < <(printf '%s\n' "${INBOX[@]}")
 }
 
 
@@ -222,7 +211,7 @@ EOF
     # If the '--text' flag is used, then use 'w3m' to convert the HTML of
     # the email to pure text by removing all the HTML tags
     [ "$RAW_TEXT" = true ] && w3m -dump "$TMPMAIL_HTML_EMAIL" && exit
-    
+
     # Open up the HTML file using $BROWSER. By default,
     # this will be 'w3m'.
     $BROWSER "$TMPMAIL_HTML_EMAIL"
@@ -255,10 +244,10 @@ main(){
     # Create the $TMPMAIL_DIR directory and dont throw any errors
     # if it already exists
     mkdir -p "$TMPMAIL_DIR"
-    
+
     # Get the email address and save the value to the EMAIL_ADDRESS variable
     EMAIL_ADDRESS="$(get_email_address)"
-    
+
     # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
     # In this case, it takes the EMAIL_ADDRESS and removed everything after
     # the '@' symbol which gives us the username.
@@ -271,20 +260,20 @@ main(){
 
     # If no arguments are provided just the emails
     [[ $# -eq 0 ]] && list_emails && exit
-    
+
     while [[ "$1" ]]; do
         case "$1" in
             --help|-h) usage && exit ;;
             --generate|-g) generate_email_address true && exit;;
             --browser|-b) BROWSER="$2" ;;
-	    --text|-t) RAW_TEXT=true ;;
+            --text|-t) RAW_TEXT=true ;;
             --version) echo "$VERSION" && exit;;
             --recent|-r) view_recent_email && exit;;
-	    # If the user provides number as an argument,
-	    # assume its the ID of an email and try getting
-	    # the email that belongs to the ID
+            # If the user provides number as an argument,
+            # assume its the ID of an email and try getting
+            # the email that belongs to the ID
             *[0-9]*) view_email "$1" && exit;;
-	    -*) echo "error: option $1 does not exist"
+            -*) echo "error: option $1 does not exist"
         esac
         shift
     done