tmpmail 11 KB

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