Anthony Baldwin преди 5 години
родител
ревизия
1a3e9050c6
променени са 2 файла, в които са добавени 50 реда и са изтрити 31 реда
  1. 17 2
      README.md
  2. 33 29
      tmpmail

+ 17 - 2
README.md

@@ -7,6 +7,8 @@
 and receive emails to the temporary email address. It uses 1secmail's [API](https://www.1secmail.com/api/)
 to receive the emails.
 
+By default, emails will be generated by random unless the `--generate` argument is followed by the desired username.
+
 By default `w3m` is used to render the HTML emails on the terminal.
 But if you prefer another text based web browser or would rather view the email in a GUI web browser such as Firefox, simply
 use the `--browser` argument followed by the command needed to launch the web browser of your choice.
@@ -19,6 +21,7 @@ use the `--browser` argument followed by the command needed to launch the web br
 - `w3m`
 - `curl`
 - [`jq`](https://github.com/stedolan/jq)
+- `awk`
 
 ## Installation
 ### Install locally
@@ -40,7 +43,7 @@ $ yay -S tmpmail-git
 
 ## Usage
 ```console
-usage: tmpmail [-h] [--generate] [--text] [--browser BROWSER] [--recent] ID
+usage: tmpmail [-h] [--generate] CUSTOM [--text] [--browser BROWSER] [--recent] ID
 
 optional arguments:
 -h, --help           Show this help message
@@ -52,6 +55,18 @@ optional arguments:
 ```
 
 ### Examples
+Create random email
+```console
+$ tmpmail --generate
+xoithrjagpx@1secmail.net
+```
+
+Create custom email
+```console
+$ tmpmail --generate mycustomemail
+mycustomemail@1secmail.com
+```
+
 View the inbox
 ```console
 $ tmpmail
@@ -80,5 +95,5 @@ Subject: Test Email
 Hello World
 ```
 
-## Credits 
+## Credits
 This script is heavily inspired by Mitch Weaver's [`1secmail`](https://github.com/mitchweaver/bin/blob/master/application/1secmail) script

+ 33 - 29
tmpmail

@@ -5,7 +5,7 @@
 # Dependencies: jq, curl, w3m
 #
 
-export LC_CTYPE=C 
+export LC_CTYPE=C
 export LANG=C
 
 VERSION=1.0.1
@@ -24,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"
@@ -34,7 +34,7 @@ usage(){
     # Using 'cat << EOF' we can easily output a multiline text. This is much
     # better than using 'echo' for each line or using '\n' to create a new line.
     cat << EOF
-usage: tmpmail [-h] [--generate] [--browser BROWSER] [--recent] ID
+usage: tmpmail [-h] [--generate] CUSTOM [--browser BROWSER] [--recent] ID
 
 optional arguments:
 -h, --help           Show this help message
@@ -49,11 +49,12 @@ EOF
 
 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'
+    #  [1] The user wants to generate a new email and runs 'tmpmail --generate $OPTIONAL'
+    #      with or without the `$OPTIONAL` command.
     #  [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
@@ -63,27 +64,30 @@ generate_email_address(){
     # We need this variable so we can know whether or not we need to show the user
     # what the email was. <-- More about this can be found further down in this function.
     EXTERNALLY=${1:-false}
+    CUSTOM=${2:-false}
 
-    # Generate a random email address.
+    # Generate an email address.
+    #
     # This function is called whenever the user wants to generate a new email
-    # address by running 'tmpmail --generate' or when the user runs 'tmpmail'
+    # address by running 'tmpmail --generate $OPTIONAL' or when the user runs 'tmpmail'
     # but /tmp/tmpmail/email_address is empty or nonexistent.
     #
-    # We create a random username by taking the first 10 lines from /dev/random
-    # and delete all the characters which are *not* lower case letters from A to Z.
-    # So charcters such as dashes, periods, underscore, and numbers are all deleted,
-    # 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
+    # If a user does not pass $OPTIONAL, we create a random username by taking the first
+    # 10 lines from /dev/random and delete all the characters which are *not* lower case
+    # letters from A to Z. So charcters such as dashes, periods, underscore, and numbers are
+    # all deleted, 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)
-    
+    [ "$CUSTOM" != false ] && USERNAME=$CUSTOM
+
     # 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 
+    # 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 
+    # 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)
 
@@ -114,7 +118,7 @@ list_emails(){
     # List all the received emails in a nicely formatted order
     #
     # Fetch the email data using 1secmail's API
-    DATA=$(curl -sL "https://1secmail.$TLD/api/v1/?action=getMessages&login=$USERNAME&domain=1secmail.$TLD")
+    DATA=$(curl -skL "https://1secmail.$TLD/api/v1/?action=getMessages&login=$USERNAME&domain=1secmail.$TLD")
 
     # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
     # the email address has gotten any emails
@@ -137,15 +141,15 @@ list_emails(){
     # 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.
     #
@@ -183,7 +187,7 @@ list_emails(){
     done
 
     # Show the emails cleanly
-    column -t -s "||" <<< "$INBOX" 
+    column -t -s "||" <<< "$INBOX"
 }
 
 
@@ -193,7 +197,7 @@ view_email(){
     # The first argument provided to this function will be the ID of the email
     # that has been received
     EMAIL_ID="$1"
-    DATA=$(curl -sL "https://www.1secmail.$TLD/api/v1/?action=readMessage&login=$USERNAME&domain=1secmail.$TLD&id=$EMAIL_ID")
+    DATA=$(curl -skL "https://www.1secmail.$TLD/api/v1/?action=readMessage&login=$USERNAME&domain=1secmail.$TLD&id=$EMAIL_ID")
 
     # After the data is retrieved using the API, we have to check if we got any emails.
     # Luckly 1secmail's API is not complicated and returns 'Message not found' as plain text
@@ -221,7 +225,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"
@@ -243,7 +247,7 @@ view_recent_email(){
 
 main(){
     # Iterate of the array of dependencies and check if the user has them installed
-    dependencies=(jq w3m curl)
+    dependencies=(jq w3m curl awk)
     for dependency in "${dependencies[@]}"; do
         type -p "$dependency" &>/dev/null || {
         echo "error: Could not find '${dependency}', is it installed?" >&2
@@ -254,10 +258,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.
@@ -270,11 +274,11 @@ 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;;
+            --generate|-g) generate_email_address true $2  && exit;;
             --browser|-b) BROWSER="$2" ;;
 	    --text|-t) RAW_TEXT=true ;;
             --version) echo "$VERSION" && exit;;