tmpmail 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/usr/bin/env sh
  2. #
  3. # by Siddharth Dushantha 2020
  4. #
  5. # Dependencies: jq, curl, w3m
  6. #
  7. VERSION=1.1.6
  8. # By default 'tmpmail' uses 'w3m' as it's web browser to render
  9. # the HTML of the email
  10. BROWSER="w3m"
  11. # If the value is set to 'true' tmpmail will convert the HTML email
  12. # to raw text and send that to stdout
  13. RAW_TEXT=false
  14. # Everything related to 'tmpmail' will be stored in /tmp/tmpmail
  15. # so that the old emails and email addresses get cleared after
  16. # restarting the computer
  17. TMPMAIL_DIR="/tmp/tmpmail"
  18. # TMPMAIL_EMAIL_ADDRESS is where we store the temporary email address
  19. # that gets generated. This prevents the user from providing
  20. # the email address everytime they run tmpmail
  21. TMPMAIL_EMAIL_ADDRESS="$TMPMAIL_DIR/email_address"
  22. # tmpmail.html is where the email gets stored.
  23. # Even though the file ends with a .html extension, the raw text version of
  24. # the email will also be stored in this file so that w3m and other browsers
  25. # are able to open this file
  26. TMPMAIL_HTML_EMAIL="$TMPMAIL_DIR/tmpmail.html"
  27. # Default 1secmail API URL
  28. TMPMAIL_API_URL="https://www.1secmail.com/api/v1/"
  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. tmpmail
  34. tmpmail -h | --version
  35. tmpmail -g [ADDRESS]
  36. tmpmail [-t | -b BROWSER] -r | ID
  37. When called with no option and no argument, tmpmail lists the messages in
  38. the inbox and their numeric IDs. When called with one argument, tmpmail
  39. shows the email message with specified ID.
  40. -b, --browser BROWSER
  41. Specify BROWSER (default: w3m) that is used to render the HTML of
  42. the email
  43. -g, --generate [ADDRESS]
  44. Generate a new email address, either the specified ADDRESS, or
  45. randomly create one
  46. -h, --help
  47. Show help
  48. -r, --recent
  49. View the most recent email message
  50. -t, --text
  51. View the email as raw text, where all the HTML tags are removed.
  52. Without this option, HTML is used.
  53. --version
  54. Show version
  55. EOF
  56. }
  57. generate_email_address() {
  58. # There are 2 ways which this function is called in this script.
  59. # [1] The user wants to generate a new email and runs 'tmpmail --generate'
  60. # [2] The user runs 'tmpmail' to check the inbox , but /tmp/tmpmail/email_address
  61. # is empty or nonexistant. Therefore a new email gets automatically
  62. # generated before showing the inbox. But of course the inbox will
  63. # be empty as the newly generated email address has not been
  64. # sent any emails.
  65. #
  66. # When the function 'generate_email_address()' is called with the arguement
  67. # 'true', it means that the function was called because the user
  68. # ran 'tmpmail --generate'.
  69. #
  70. # We need this variable so we can know whether or not we need to show the user
  71. # what the email was. <-- More about this can be found further down in this function.
  72. EXTERNALLY=${1:-false}
  73. # This variable lets generate_email_address know if the user has provided a custom
  74. # email address which they want to use. CUSTOM is set to false if $2 has no value.
  75. CUSTOM=${2:-false}
  76. # Generate a random email address.
  77. # This function is called whenever the user wants to generate a new email
  78. # address by running 'tmpmail --generate' or when the user runs 'tmpmail'
  79. # but /tmp/tmpmail/email_address is empty or nonexistent.
  80. #
  81. # We create a random username by taking the first 10 lines from /dev/random
  82. # and delete all the characters which are *not* lower case letters from A to Z.
  83. # So charcters such as dashes, periods, underscore, and numbers are all deleted,
  84. # giving us a text which only contains lower case letters form A to Z. We then take
  85. # the first 10 characters, which will be the username of the email address
  86. USERNAME=$(head /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | cut -c1-11 | tr "[:upper:]" "[:lower:]")
  87. DOMAINS="1secmail.com 1secmail.net 1secmail.org esiix.com wwjmp.com"
  88. # Randomly pick one of the domains mentiond above.
  89. DOMAIN=$(printf "%b" "$DOMAINS" | tr " " "\n" | randomize | tail -1)
  90. EMAIL_ADDRESS="$USERNAME@$DOMAIN"
  91. # If the user provided a custom email address then use that email address
  92. if [ "$CUSTOM" != false ]; then
  93. EMAIL_ADDRESS=$CUSTOM
  94. # Do a regex check to see if the email address provided by the user is a
  95. # valid email address
  96. REGEXP="[a-z0-9]+@(1secmail\.(com|net|org)|esiix.co|wwjmp.com)"
  97. if ! printf %b "$EMAIL_ADDRESS" | grep -Eq "$REGEXP"; then
  98. print_error "Provided email is invalid. Must match $REGEXP"
  99. fi
  100. fi
  101. # Save the generated email address to the $TMPMAIL_EMAIL_ADDRESS file
  102. # so that it can be whenever 'tmpmail' is run
  103. printf %s "$EMAIL_ADDRESS" >"$TMPMAIL_EMAIL_ADDRESS"
  104. # If this function was called because the user wanted to generate a new
  105. # email address, show them the email address
  106. [ "$EXTERNALLY" = true ] && cat "$TMPMAIL_EMAIL_ADDRESS"
  107. }
  108. get_email_address() {
  109. # This function is only called once and that is when this script
  110. # get executed. The output of this function gets stored in $EMAIL_ADDRESS
  111. #
  112. # If the file that contains the email address is empty,
  113. # that means we do not have an email address, so generate one.
  114. [ ! -s "$TMPMAIL_EMAIL_ADDRESS" ] && generate_email_address
  115. # Output the email address by getting the first line of $TMPMAIL_EMAIL
  116. head -n 1 "$TMPMAIL_EMAIL_ADDRESS"
  117. }
  118. list_emails() {
  119. # List all the received emails in a nicely formatted order
  120. #
  121. # Fetch the email data using 1secmail's API
  122. DATA=$(curl -sL "$TMPMAIL_API_URL?action=getMessages&login=$USERNAME&domain=$DOMAIN")
  123. # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
  124. # the email address has gotten any emails
  125. DATA_LENGTH=$(printf %s "$DATA" | jq length)
  126. # We are showing what email address is currently being used
  127. # in case the user has forgotten what the email address was.
  128. printf "[ Inbox for %s ]\n\n" "$EMAIL_ADDRESS"
  129. # If the length of the data we got is 0, that means the email address
  130. # has not received any emails yet.
  131. [ "$DATA_LENGTH" -eq 0 ] && echo "No new mail" && exit
  132. # This is where we store all of our emails, which is then
  133. # displayed using 'column'
  134. INBOX=""
  135. # Go through each mail that has been received
  136. index=1
  137. while [ $index -le "${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=$(printf %s "$DATA" | jq -r ".[$index-1]")
  142. ID=$(printf %s "$MAIL_DATA" | jq -r ".id")
  143. FROM=$(printf %s "$MAIL_DATA" | jq -r ".from")
  144. SUBJECT=$(printf %s "$MAIL_DATA" | jq -r ".subject")
  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. INBOX="$INBOX$ID ||$FROM ||$SUBJECT\n"
  150. index=$((index + 1))
  151. done
  152. # Show the emails cleanly
  153. printf "%b" "$INBOX" | column -t -s "||"
  154. }
  155. randomize() {
  156. # We could use 'shuf' and 'sort -R' but they are not a part of POSIX
  157. awk 'BEGIN {srand();} {print rand(), $0}' | \
  158. sort -n -k1 | cut -d' ' -f2
  159. }
  160. view_email() {
  161. # View an email by providing it's ID
  162. #
  163. # The first argument provided to this function will be the ID of the email
  164. # that has been received
  165. EMAIL_ID="$1"
  166. DATA=$(curl -sL "${TMPMAIL_API_URL}?action=readMessage&login=$USERNAME&domain=$DOMAIN&id=$EMAIL_ID")
  167. # After the data is retrieved using the API, we have to check if we got any emails.
  168. # Luckly 1secmail's API is not complicated and returns 'Message not found' as plain text
  169. # if our email address as not received any emails.
  170. # If we received the error message from the API just quit because there is nothing to do
  171. [ "$DATA" = "Message not found" ] && print_error "Message not found"
  172. # We pass the $DATA to 'jq' which extracts the values
  173. FROM=$(printf %s "$DATA" | jq -r ".from")
  174. SUBJECT=$(printf %s "$DATA" | jq -r ".subject")
  175. HTML_BODY=$(printf %s "$DATA" | jq -r ".htmlBody")
  176. ATTACHMENTS=$(printf %s "$DATA" | jq -r ".attachments | length")
  177. # If you get an email that is in pure text, the .htmlBody field will be empty and
  178. # we will need to get the content from .textBody instead
  179. [ -z "$HTML_BODY" ] && HTML_BODY="<pre>$(printf %s "$DATA" | jq -r ".textBody")</pre>"
  180. # Create the HTML with all the information that is relevant and then
  181. # assigning that HTML to the variable HTML_MAIL. This is the best method
  182. # to create a multiline variable
  183. HTML_MAIL=$(cat <<EOF
  184. <pre><b>To: </b>$EMAIL_ADDRESS
  185. <b>From: </b>$FROM
  186. <b>Subject: </b>$SUBJECT</pre>
  187. $HTML_BODY
  188. EOF
  189. )
  190. if [ ! "$ATTACHMENTS" = "0" ]; then
  191. HTML_MAIL="$HTML_MAIL<br><b>[Attachments]</b><br>"
  192. index=1
  193. while [ "$index" -le "$ATTACHMENTS" ]; do
  194. FILENAME=$(printf %s "$DATA" | jq -r ".attachments | .[$index-1] | .filename")
  195. LINK="$TMPMAIL_API_URL?action=download&login=$USERNAME&domain=$DOMAIN&id=$EMAIL_ID&file=$FILENAME"
  196. HTML_LINK="<a href=$LINK download=$FILENAME>$FILENAME</a><br>"
  197. if [ "$RAW_TEXT" = true ]; then
  198. # The actual url is way too long and does not look so nice in STDOUT.
  199. # Therefore we will shortening it using is.gd so that it looks nicer.
  200. LINK=$(curl -s -F"url=$LINK" "https://is.gd/create.php?format=simple")
  201. HTML_MAIL="$HTML_MAIL$LINK [$FILENAME]<br>"
  202. else
  203. HTML_MAIL="$HTML_MAIL$HTML_LINK"
  204. fi
  205. index=$((index + 1))
  206. done
  207. fi
  208. # Save the $HTML_MAIL into $TMPMAIL_HTML_EMAIL
  209. printf %s "$HTML_MAIL" >"$TMPMAIL_HTML_EMAIL"
  210. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  211. # the email to pure text by removing all the HTML tags
  212. [ "$RAW_TEXT" = true ] && w3m -dump "$TMPMAIL_HTML_EMAIL" && exit
  213. # Open up the HTML file using $BROWSER. By default,
  214. # this will be 'w3m'.
  215. $BROWSER "$TMPMAIL_HTML_EMAIL"
  216. }
  217. view_recent_email() {
  218. # View the most recent email.
  219. #
  220. # This is done by listing all the received email like you
  221. # normally see on the terminal when running 'tmpmail'.
  222. # We then grab the ID of the most recent
  223. # email, which the first line.
  224. MAIL_ID=$(list_emails | head -3 | tail -1 | cut -d' ' -f 1)
  225. view_email "$MAIL_ID"
  226. }
  227. print_error() {
  228. # Print error message
  229. #
  230. # The first argument provided to this function will be the error message.
  231. # Script will exit after printing the error message.
  232. printf "%s\n" "Error: $1" >&2
  233. exit 1
  234. }
  235. main() {
  236. # Iterate of the array of dependencies and check if the user has them installed.
  237. # We are checking if $BROWSER is installed instead of checking for 'w3m'. By doing
  238. # this, it allows the user to not have to install 'w3m' if they are using another
  239. # browser to view the HTML
  240. for dependency in jq $BROWSER curl; do
  241. if ! command -v "$dependency" >/dev/null 2>&1; then
  242. print_error "Could not find '$dependency', is it installed?"
  243. fi
  244. done
  245. # Create the $TMPMAIL_DIR directory and dont throw any errors
  246. # if it already exists
  247. mkdir -p "$TMPMAIL_DIR"
  248. # Get the email address and save the value to the EMAIL_ADDRESS variable
  249. EMAIL_ADDRESS="$(get_email_address)"
  250. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  251. # In this case, it takes the EMAIL_ADDRESS and removed everything after
  252. # the '@' symbol which gives us the username.
  253. USERNAME=${EMAIL_ADDRESS%@*}
  254. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  255. # In this case, it takes the EMAIL_ADDRESS and removes everything until the
  256. # period '.' which gives us the domain
  257. DOMAIN=${EMAIL_ADDRESS#*@}
  258. # If no arguments are provided just the emails
  259. [ $# -eq 0 ] && list_emails && exit
  260. while [ "$1" ]; do
  261. case "$1" in
  262. --help | -h) usage && exit ;;
  263. --generate | -g) generate_email_address true "$2" && exit ;;
  264. --browser | -b) BROWSER="$2" ;;
  265. --text | -t) RAW_TEXT=true ;;
  266. --version) echo "$VERSION" && exit ;;
  267. --recent | -r) view_recent_email && exit ;;
  268. *[0-9]*)
  269. # If the user provides number as an argument,
  270. # assume its the ID of an email and try getting
  271. # the email that belongs to the ID
  272. view_email "$1" && exit
  273. ;;
  274. -*) print_error "option '$1' does not exist" ;;
  275. esac
  276. shift
  277. done
  278. }
  279. main "$@"