tmpmail 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. # This is done by first echoing all the TLDS into 'tr' which then
  70. # replaces the white spaces with a new line. This is done because we use
  71. # 'sort' to put the TLDS in a random order, but 'sort' requires the TLDS to
  72. # be on seperate lines. After the randomizing is done we take the first line
  73. # which is one of the TLDS
  74. TLD=$(echo "${TLDS[@]}" | tr " " "\n" | sort -R | head -n 1)
  75. # Save the generated email address to the $TMPMAIL_EMAIL_ADDRESS file
  76. # so that it can be whenever 'tmpmail' is run
  77. echo "$USERNAME@1secmail.$TLD" > "$TMPMAIL_EMAIL_ADDRESS"
  78. # If this function was called because the user wanted to generate a new
  79. # email address, show them the email address
  80. [ "$EXTERNALLY" = true ] && cat "$TMPMAIL_EMAIL_ADDRESS"
  81. }
  82. get_email_address(){
  83. # This function is only called once and that is when this script
  84. # get executed. The output of this function gets stored in $EMAIL_ADDRESS
  85. #
  86. # If the file that contains the email address is empty,
  87. # that means we do not have an email address, so generate one.
  88. [ ! -s "$TMPMAIL_EMAIL_ADDRESS" ] && generate_email_address
  89. # Output the email address by getting the first line of $TMPMAIL_EMAIL
  90. head -n 1 "$TMPMAIL_EMAIL_ADDRESS"
  91. }
  92. list_emails(){
  93. # List all the received emails in a nicely formatted order
  94. #
  95. # Fetch the email data using 1secmail's API
  96. DATA=$(curl -sL "https://1secmail.$TLD/api/v1/?action=getMessages&login=$USERNAME&domain=1secmail.$TLD")
  97. # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
  98. # the email address has gotten any emails
  99. DATA_LENGTH=$(jq length <<< "$DATA")
  100. # We are showing what email address is currently being used
  101. # in case the user has forgotten what the email address was.
  102. echo -e "[ Inbox for $EMAIL_ADDRESS ]\n"
  103. # If the length of the data we got is 0, that means the email address
  104. # has not received any emails yet.
  105. [ "$DATA_LENGTH" -eq 0 ] && echo "No new mail" && exit
  106. # This is where we store all of our emails, which is then
  107. # displayed using 'column'
  108. INBOX=""
  109. # This for loop goes through each mail that have been received.
  110. #
  111. # Since we need to go through all the data, we need to tell our for loop
  112. # to loop from 1 to X, where X is legnth of the $DATA which contains all
  113. # the emails.
  114. #
  115. # Normally to loop from 1 to 5, we would use shell expansion and write:
  116. # for index in {1..5}; do
  117. # do_something
  118. # done
  119. #
  120. # But we a minor issue. We dont know what the final number is, and we are not allowed
  121. # use to variables in shell expansions like this:
  122. # {1..$X}
  123. #
  124. # where $X is the length of the $DATA.
  125. #
  126. # To fix this issue, we can use 'seq' which will allow us to create a sequence
  127. # from X to Y.
  128. # Example:
  129. # $ seq 1 5
  130. # 1
  131. # 2
  132. # 3
  133. # 4
  134. # 5
  135. #
  136. # We can then put those results into the foor loop
  137. for index in $(seq 1 "$DATA_LENGTH"); do
  138. # Since arrays in JSON data start at 0, we must subtract
  139. # the value of $index by 1 so that we dont miss one of the
  140. # emails in the array
  141. MAIL_DATA=$(jq -r ".[$index-1]" <<< "$DATA")
  142. ID=$(jq -r ".id" <<< "$MAIL_DATA")
  143. FROM=$(jq -r ".from" <<< "$MAIL_DATA")
  144. SUBJECT=$(jq -r ".subject" <<< "$MAIL_DATA")
  145. # The '||' are used as a divideder for 'column'. 'column' will use this divider as
  146. # a point of reference to create the division. By default 'column' uses a blank space
  147. # but that would not work in our case as the email subject could have multiple white spaces
  148. # and 'column' would split the words that are seperated by white space, in different columns.
  149. #
  150. # Yes, there a double quote all by it self on the line under this one.
  151. # This serves as a new line. So that the 'column' command works properly.
  152. # I know I could have used '\n' but 'column' does not intepret it as a new line.
  153. INBOX+="$ID ||$FROM ||$SUBJECT
  154. "
  155. done
  156. # Show the emails cleanly
  157. column -t -s "||" <<< "$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.$TLD/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. # Create the HTML with all the information that is relevant and then
  176. # assigning that HTML to the variable HTML_MAIL. This is the best method
  177. # to create a multiline variable
  178. read -r -d '' HTML_MAIL << EOF
  179. <pre><b>To: </b>$EMAIL_ADDRESS
  180. <b>From: </b>$FROM
  181. <b>Subject: </b>$SUBJECT</pre>
  182. $HTML_BODY
  183. EOF
  184. # Save the $HTML_MAIL into $TMPMAIL_HTML_EMAIL
  185. echo "$HTML_MAIL" > "$TMPMAIL_HTML_EMAIL"
  186. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  187. # the email to pure text by removing all the HTML tags
  188. [ "$RAW_TEXT" = true ] && w3m -dump "$TMPMAIL_HTML_EMAIL" && exit
  189. # Open up the HTML file using $BROWSER. By default,
  190. # this will be 'w3m'.
  191. $BROWSER "$TMPMAIL_HTML_EMAIL"
  192. }
  193. view_recent_email(){
  194. # View the most recent email.
  195. #
  196. # This is done by listing all the received email like you
  197. # normally see on the terminal when running 'tmpmail'.
  198. # We then use 'awk' to grab the ID of the most recent
  199. # email, which the first line.
  200. MAIL_ID=$(list_emails | awk 'NR==3{print $1}')
  201. view_email "$MAIL_ID"
  202. }
  203. main(){
  204. # Iterate of the array of dependencies and check if the user has them installed
  205. dependencies=(jq w3m curl)
  206. for dependency in "${dependencies[@]}"; do
  207. type -p "$dependency" &>/dev/null || {
  208. echo "error: Could not find '${dependency}', is it installed?" >&2
  209. exit 1
  210. }
  211. done
  212. # Create the $TMPMAIL_DIR directory and dont throw any errors
  213. # if it already exists
  214. mkdir -p "$TMPMAIL_DIR"
  215. # Get the email address and save the value to the EMAIL_ADDRESS variable
  216. EMAIL_ADDRESS="$(get_email_address)"
  217. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  218. # In this case, it takes the EMAIL_ADDRESS and removed everything after
  219. # the '@' symbol which gives us the username.
  220. USERNAME=${EMAIL_ADDRESS%@*}
  221. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  222. # In this case, it takes the EMAIL_ADDRESS and removes everything until the
  223. # period '.' which gives us the TLD
  224. TLD=${EMAIL_ADDRESS#*.}
  225. # If no arguments are provided just the emails
  226. [[ $# -eq 0 ]] && list_emails && exit
  227. while [[ "$1" ]]; do
  228. case "$1" in
  229. --help|-h) usage && exit ;;
  230. --generate|-g) generate_email_address true && exit;;
  231. --browser|-b) BROWSER="$2" ;;
  232. --text|-t) RAW_TEXT=true ;;
  233. --version) echo "$VERSION" && exit;;
  234. --recent|-r) view_recent_email && exit;;
  235. # If the user provides number as an argument,
  236. # assume its the ID of an email and try getting
  237. # the email that belongs to the ID
  238. *[0-9]*) view_email "$1" && exit;;
  239. -*) echo "error: option $1 does not exist"
  240. esac
  241. shift
  242. done
  243. }
  244. main "$@"