tmpmail 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/usr/bin/env sh
  2. #
  3. # by Siddharth Dushantha 2020
  4. #
  5. # Dependencies: jq, curl, w3m
  6. #
  7. VERSION=1.1.5
  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
  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. # This for loop goes through each mail that have been received.
  136. #
  137. # Since we need to go through all the data, we need to tell our for loop
  138. # to loop from 1 to X, where X is legnth of the $DATA which contains all
  139. # the emails.
  140. #
  141. # Normally to loop from 1 to 5, we would use shell expansion and write:
  142. # for index in {1..5}; do
  143. # do_something
  144. # done
  145. #
  146. # But we a minor issue. We dont know what the final number is, and we are not allowed
  147. # use to variables in shell expansions like this:
  148. # {1..$X}
  149. #
  150. # where $X is the length of the $DATA.
  151. #
  152. # To fix this issue, we can use 'seq' which will allow us to create a sequence
  153. # from X to Y.
  154. # Example:
  155. # $ seq 1 5
  156. # 1
  157. # 2
  158. # 3
  159. # 4
  160. # 5
  161. #
  162. # We can then put those results into the foor loop
  163. index=1
  164. while [ $index -le "${DATA_LENGTH}" ]; do
  165. # Since arrays in JSON data start at 0, we must subtract
  166. # the value of $index by 1 so that we dont miss one of the
  167. # emails in the array
  168. MAIL_DATA=$(printf %s "$DATA" | jq -r ".[$index-1]")
  169. ID=$(printf %s "$MAIL_DATA" | jq -r ".id")
  170. FROM=$(printf %s "$MAIL_DATA" | jq -r ".from")
  171. SUBJECT=$(printf %s "$MAIL_DATA" | jq -r ".subject")
  172. # The '||' are used as a divideder for 'column'. 'column' will use this divider as
  173. # a point of reference to create the division. By default 'column' uses a blank space
  174. # but that would not work in our case as the email subject could have multiple white spaces
  175. # and 'column' would split the words that are seperated by white space, in different columns.
  176. INBOX="$INBOX$ID ||$FROM ||$SUBJECT\n"
  177. index=$(( index + 1 ))
  178. done
  179. # Show the emails cleanly
  180. printf "%b" "$INBOX" | column -t -s "||"
  181. }
  182. # 'shuf' is not part of POSIX, neither is 'sort -R'
  183. randomize() {
  184. awk 'BEGIN {srand();} {print rand(), $0}' | \
  185. sort -n -k1 | cut -d' ' -f2
  186. }
  187. view_email() {
  188. # View an email by providing it's ID
  189. #
  190. # The first argument provided to this function will be the ID of the email
  191. # that has been received
  192. EMAIL_ID="$1"
  193. DATA=$(curl -sL "${TMPMAIL_API_URL}?action=readMessage&login=$USERNAME&domain=$DOMAIN&id=$EMAIL_ID")
  194. # After the data is retrieved using the API, we have to check if we got any emails.
  195. # Luckly 1secmail's API is not complicated and returns 'Message not found' as plain text
  196. # if our email address as not received any emails.
  197. # If we received the error message from the API just quit because there is nothing to do
  198. [ "$DATA" = "Message not found" ] && print_error "Message not found"
  199. # We pass the $DATA to 'jq' which extracts the values
  200. FROM=$(printf %s "$DATA" | jq -r ".from")
  201. SUBJECT=$(printf %s "$DATA" | jq -r ".subject")
  202. HTML_BODY=$(printf %s "$DATA" | jq -r ".htmlBody")
  203. ATTACHMENTS=$(printf %s "$DATA" | jq -r ".attachments | length")
  204. # If you get an email that is in pure text, the .htmlBody field will be empty and
  205. # we will need to get the content from .textBody instead
  206. [ -z "$HTML_BODY" ] && HTML_BODY="<pre>$(printf %s "$DATA" | jq -r ".textBody")</pre>"
  207. # Create the HTML with all the information that is relevant and then
  208. # assigning that HTML to the variable HTML_MAIL. This is the best method
  209. # to create a multiline variable
  210. HTML_MAIL=$(cat <<EOF
  211. <pre><b>To: </b>$EMAIL_ADDRESS
  212. <b>From: </b>$FROM
  213. <b>Subject: </b>$SUBJECT</pre>
  214. $HTML_BODY
  215. EOF
  216. )
  217. if [ ! "$ATTACHMENTS" = "0" ]; then
  218. HTML_MAIL="$HTML_MAIL<br><b>[Attachments]</b><br>"
  219. index=1
  220. while [ "$index" -le "$ATTACHMENTS" ]; do
  221. FILENAME=$(printf %s "$DATA" | jq -r ".attachments | .[$index-1] | .filename")
  222. LINK="$TMPMAIL_API_URL?action=download&login=$USERNAME&domain=$DOMAIN&id=$EMAIL_ID&file=$FILENAME"
  223. HTML_LINK="<a href=$LINK download=$FILENAME>$FILENAME</a><br>"
  224. if [ "$RAW_TEXT" = true ]; then
  225. # The actual url is way too long and does not look so nice in STDOUT.
  226. # Therefore we will shortening it using is.gd so that it looks nicer.
  227. LINK=$(curl -s -F"url=$LINK" "https://is.gd/create.php?format=simple")
  228. HTML_MAIL="$HTML_MAIL$LINK [$FILENAME]<br>"
  229. else
  230. HTML_MAIL="$HTML_MAIL$HTML_LINK"
  231. fi
  232. index=$(( index + 1 ))
  233. done
  234. fi
  235. # Save the $HTML_MAIL into $TMPMAIL_HTML_EMAIL
  236. printf %s "$HTML_MAIL" >"$TMPMAIL_HTML_EMAIL"
  237. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  238. # the email to pure text by removing all the HTML tags
  239. [ "$RAW_TEXT" = true ] && w3m -dump "$TMPMAIL_HTML_EMAIL" && exit
  240. # Open up the HTML file using $BROWSER. By default,
  241. # this will be 'w3m'.
  242. $BROWSER "$TMPMAIL_HTML_EMAIL"
  243. }
  244. view_recent_email() {
  245. # View the most recent email.
  246. #
  247. # This is done by listing all the received email like you
  248. # normally see on the terminal when running 'tmpmail'.
  249. # We then grab the ID of the most recent
  250. # email, which the first line.
  251. MAIL_ID=$(list_emails | head -3 | tail -1 | cut -d' ' -f 1)
  252. view_email "$MAIL_ID"
  253. }
  254. print_error() {
  255. # Print error message
  256. #
  257. # The first argument provided to this function will be the error message.
  258. # Script will exit after printing the error message.
  259. printf "%s\n" "Error: $1" >&2
  260. exit 1
  261. }
  262. main() {
  263. # Iterate of the array of dependencies and check if the user has them installed.
  264. # We are checking if $BROWSER is installed instead of checking for 'w3m'. By doing
  265. # this, it allows the user to not have to install 'w3m' if they are using another
  266. # browser to view the HTML
  267. for dependency in jq $BROWSER curl; do
  268. if ! command -v "$dependency" >/dev/null 2>&1; then
  269. print_error "Could not find '$dependency', is it installed?"
  270. fi
  271. done
  272. # Create the $TMPMAIL_DIR directory and dont throw any errors
  273. # if it already exists
  274. mkdir -p "$TMPMAIL_DIR"
  275. # Get the email address and save the value to the EMAIL_ADDRESS variable
  276. EMAIL_ADDRESS="$(get_email_address)"
  277. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  278. # In this case, it takes the EMAIL_ADDRESS and removed everything after
  279. # the '@' symbol which gives us the username.
  280. USERNAME=${EMAIL_ADDRESS%@*}
  281. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  282. # In this case, it takes the EMAIL_ADDRESS and removes everything until the
  283. # period '.' which gives us the domain
  284. DOMAIN=${EMAIL_ADDRESS#*@}
  285. # If no arguments are provided just the emails
  286. [ $# -eq 0 ] && list_emails && exit
  287. while [ "$1" ]; do
  288. case "$1" in
  289. --help | -h) usage && exit ;;
  290. --generate | -g) generate_email_address true "$2" && exit ;;
  291. --browser | -b) BROWSER="$2" ;;
  292. --text | -t) RAW_TEXT=true ;;
  293. --version) echo "$VERSION" && exit ;;
  294. --recent | -r) view_recent_email && exit ;;
  295. *[0-9]*)
  296. # If the user provides number as an argument,
  297. # assume its the ID of an email and try getting
  298. # the email that belongs to the ID
  299. view_email "$1" && exit
  300. ;;
  301. -*) print_error "option '$1' does not exist" ;;
  302. esac
  303. shift
  304. done
  305. }
  306. main "$@"