tmpmail 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/env bash
  2. #
  3. # by Siddharth Dushantha 2020
  4. #
  5. # Dependencies: jq, curl, w3m
  6. #
  7. export LC_CTYPE=C
  8. export LANG=C
  9. VERSION=1.0.0
  10. # By default 'tmpmail' uses 'w3m' as it's web browser to render
  11. # the HTML of the email
  12. BROWSER="w3m"
  13. # If the value is set to 'true' tmpmail will convert the HTML email
  14. # to raw text and send that to stdout
  15. RAW_TEXT=false
  16. # Everything related to 'tmpmail' will be stored in /tmp/tmpmail
  17. # so that the old emails and email addresses get cleared after
  18. # restarting the computer
  19. TMPMAIL_DIR="/tmp/tmpmail/"
  20. # TMPMAIL_EMAIL_ADDRESS is where we store the temporary email address
  21. # that gets generated. This prevents the user from providing
  22. # the email address everytime they run tmpmail
  23. TMPMAIL_EMAIL_ADDRESS="$TMPMAIL_DIR/email_address"
  24. TMPMAIL_HTML_EMAIL="$TMPMAIL_DIR/tmpmail.html"
  25. usage(){
  26. # Using 'cat << EOF' we can easily output a multiline text. This is much
  27. # better than using 'echo' for each line or using '\n' to create a new line.
  28. cat << EOF
  29. usage: tmpmail [-h] [--generate] [--browser BROWSER] [--recent] ID
  30. optional arguments:
  31. -h, --help Show this help message
  32. --version Print version
  33. -g, --generate Generate a new email address
  34. -r, --recent View the most recent email
  35. -t, --text View the email as raw text, where all the HTML tags are removed
  36. -b, --browser Change the browser that is used to render the HTML of the email (default: w3m)
  37. EOF
  38. }
  39. generate_email_address(){
  40. # There are 2 ways which this function is called in this script.
  41. # [1] The user wants to generate a new email and runs 'tmpmail --generate'
  42. # [2] The user runs 'tmpmail' to check the inbox , but /tmp/tmpmail/email_address
  43. # is empty or nonexistant. Therefore a new email gets automatically
  44. # generated before showing the inbox. But of course the inbox will
  45. # be empty as the newly generated email address has not been
  46. # sent any emails.
  47. #
  48. # When the function 'generate_email()' is called with the arguement
  49. # 'true', it means that the function was called because the user
  50. # ran 'tmpmail --generate'.
  51. #
  52. # We need this variable so we can know whether or not we need to show the user
  53. # what the email was. <-- More about this can be found further down in this function.
  54. EXTERNALLY=${1:-false}
  55. # Generate a random email address.
  56. # This function is called whenever the user wants to generate a new email
  57. # address by running 'tmpmail --generate' or when the user runs 'tmpmail'
  58. # but /tmp/tmpmail/email_address is empty or nonexistent.
  59. #
  60. # We create a random username by taking the first 10 lines from /dev/random
  61. # and delete all the characters which are *not* lower case letters from A to Z.
  62. # So charcters such as dashes, periods, underscore, and numbers are all deleted,
  63. # giving us a text which only contains lower case letters form A to Z. We then take
  64. # the first 10 characters, which will be the username of the email address
  65. USERNAME=$(head /dev/urandom | tr -dc a-z | cut -c1-11)
  66. # This is an array of the valid TLDS which 1secmail provides.
  67. TLDS=(com net org)
  68. # Randomly pick one of the TLDS mentiond above.
  69. TLD=${TLDS[$RANDOM % ${#TLDS[@]} ]}
  70. # Save the generated email address to the $TMPMAIL_EMAIL_ADDRESS file
  71. # so that it can be whenever 'tmpmail' is run
  72. echo "$USERNAME@1secmail.$TLD" > "$TMPMAIL_EMAIL_ADDRESS"
  73. # If this function was called because the user wanted to generate a new
  74. # email address, show them the email address
  75. [ "$EXTERNALLY" = true ] && cat "$TMPMAIL_EMAIL_ADDRESS"
  76. }
  77. get_email_address(){
  78. # This function is only called once and that is when this script
  79. # get executed. The output of this function gets stored in $EMAIL_ADDRESS
  80. #
  81. # If the file that contains the email address is empty,
  82. # that means we do not have an email address, so generate one.
  83. [ ! -s "$TMPMAIL_EMAIL_ADDRESS" ] && generate_email_address
  84. # Output the email address by getting the first line of $TMPMAIL_EMAIL
  85. head -n 1 "$TMPMAIL_EMAIL_ADDRESS"
  86. }
  87. list_emails(){
  88. # List all the received emails in a nicely formatted order
  89. #
  90. # Fetch the email data using 1secmail's API
  91. DATA=$(curl -sL "http://1secmail.$TLD/api/v1/?action=getMessages&login=$USERNAME&domain=1secmail.$TLD")
  92. # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
  93. # the email address has gotten any emails
  94. DATA_LENGTH=$(jq length <<< "$DATA")
  95. # We are showing what email address is currently being used
  96. # in case the user has forgotten what the email address was.
  97. echo -e "[ Inbox for $EMAIL_ADDRESS ]\n"
  98. # If the length of the data we got is 0, that means the email address
  99. # has not received any emails yet.
  100. [ "$DATA_LENGTH" -eq 0 ] && echo "No new mail" && exit
  101. # This is where we store all of our emails, which is then
  102. # displayed using 'column'
  103. INBOX=()
  104. # This for loop goes through each mail that have been received.
  105. #
  106. # Since we need to go through all the data, we need to tell our for loop
  107. # to loop from 1 to X, where X is legnth of the $DATA which contains all
  108. # the emails.
  109. #
  110. # Normally to loop from 1 to 5, we would use shell expansion and write:
  111. # for index in {1..5}; do
  112. # do_something
  113. # done
  114. #
  115. # But we a minor issue. We dont know what the final number is, and we are not allowed
  116. # use to variables in shell expansions like this:
  117. # {1..$X}
  118. #
  119. # where $X is the length of the $DATA.
  120. #
  121. # To fix this issue, we can use 'seq' which will allow us to create a sequence
  122. # from X to Y.
  123. # Example:
  124. # $ seq 1 5
  125. # 1
  126. # 2
  127. # 3
  128. # 4
  129. # 5
  130. #
  131. # We can then put those results into the foor loop
  132. for index in $(seq 1 "$DATA_LENGTH"); do
  133. # Since arrays in JSON data start at 0, we must subtract
  134. # the value of $index by 1 so that we dont miss one of the
  135. # emails in the array
  136. MAIL_DATA=$(jq -r ".[$index-1]" <<< "$DATA")
  137. ID=$(jq -r ".id" <<< "$MAIL_DATA")
  138. FROM=$(jq -r ".from" <<< "$MAIL_DATA")
  139. SUBJECT=$(jq -r ".subject" <<< "$MAIL_DATA")
  140. # The '||' are used as a divideder for 'column'. 'column' will use this divider as
  141. # a point of reference to create the division. By default 'column' uses a blank space
  142. # but that would not work in our case as the email subject could have multiple white spaces
  143. # and 'column' would split the words that are seperated by white space, in different columns.
  144. INBOX+=("$ID ||$FROM ||$SUBJECT")
  145. done
  146. # Show the emails cleanly
  147. column -t -s "||" < <(printf '%s\n' "${INBOX[@]}")
  148. }
  149. view_email(){
  150. # View an email by providing it's ID
  151. #
  152. # The first argument provided to this function will be the ID of the email
  153. # that has been received
  154. EMAIL_ID="$1"
  155. DATA=$(curl -sL "http://www.1secmail.$TLD/api/v1/?action=readMessage&login=$USERNAME&domain=1secmail.$TLD&id=$EMAIL_ID")
  156. # After the data is retrieved using the API, we have to check if we got any emails.
  157. # Luckly 1secmail's API is not complicated and returns 'Message not found' as plain text
  158. # if our email address as not received any emails.
  159. # If we the error message from the API just quit because there is nothing to do
  160. [[ "$DATA" == "Message not found" ]] && echo "Message not found" && exit 1
  161. # We pass the $DATA to 'jq' which extracts the values
  162. FROM=$(jq -r ".from" <<< "$DATA")
  163. SUBJECT=$(jq -r ".subject" <<< "$DATA")
  164. HTML_BODY=$(jq -r ".htmlBody" <<< "$DATA")
  165. # Create the HTML with all the information that is relevant and then
  166. # assigning that HTML to the variable HTML_MAIL. This is the best method
  167. # to create a multiline variable
  168. read -r -d '' HTML_MAIL << EOF
  169. <pre><b>To: </b>$EMAIL_ADDRESS
  170. <b>From: </b>$FROM
  171. <b>Subject: </b>$SUBJECT</pre>
  172. $HTML_BODY
  173. EOF
  174. # Save the $HTML_MAIL into $TMPMAIL_HTML_EMAIL
  175. echo "$HTML_MAIL" > "$TMPMAIL_HTML_EMAIL"
  176. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  177. # the email to pure text by removing all the HTML tags
  178. [ "$RAW_TEXT" = true ] && w3m -dump "$TMPMAIL_HTML_EMAIL" && exit
  179. # Open up the HTML file using $BROWSER. By default,
  180. # this will be 'w3m'.
  181. $BROWSER "$TMPMAIL_HTML_EMAIL"
  182. }
  183. view_recent_email(){
  184. # View the most recent email.
  185. #
  186. # This is done by listing all the received email like you
  187. # normally see on the terminal when running 'tmpmail'.
  188. # We then use 'awk' to grab the ID of the most recent
  189. # email, which the first line.
  190. MAIL_ID=$(list_emails | awk 'NR==3{print $1}')
  191. view_email "$MAIL_ID"
  192. }
  193. main(){
  194. # Iterate of the array of dependencies and check if the user has them installed
  195. dependencies=(jq w3m curl)
  196. for dependency in "${dependencies[@]}"; do
  197. type -p "$dependency" &>/dev/null || {
  198. echo "error: Could not find '${dependency}', is it installed?" >&2
  199. exit 1
  200. }
  201. done
  202. # Create the $TMPMAIL_DIR directory and dont throw any errors
  203. # if it already exists
  204. mkdir -p "$TMPMAIL_DIR"
  205. # Get the email address and save the value to the EMAIL_ADDRESS variable
  206. EMAIL_ADDRESS="$(get_email_address)"
  207. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  208. # In this case, it takes the EMAIL_ADDRESS and removed everything after
  209. # the '@' symbol which gives us the username.
  210. USERNAME=${EMAIL_ADDRESS%@*}
  211. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  212. # In this case, it takes the EMAIL_ADDRESS and removes everything until the
  213. # period '.' which gives us the TLD
  214. TLD=${EMAIL_ADDRESS#*.}
  215. # If no arguments are provided just the emails
  216. [[ $# -eq 0 ]] && list_emails && exit
  217. while [[ "$1" ]]; do
  218. case "$1" in
  219. --help|-h) usage && exit ;;
  220. --generate|-g) generate_email_address true && exit;;
  221. --browser|-b) BROWSER="$2" ;;
  222. --text|-t) RAW_TEXT=true ;;
  223. --version) echo "$VERSION" && exit;;
  224. --recent|-r) view_recent_email && exit;;
  225. # If the user provides number as an argument,
  226. # assume its the ID of an email and try getting
  227. # the email that belongs to the ID
  228. *[0-9]*) view_email "$1" && exit;;
  229. -*) echo "error: option $1 does not exist"
  230. esac
  231. shift
  232. done
  233. }
  234. main "$@"