tmpmail 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #!/usr/bin/env sh
  2. #
  3. # by Siddharth Dushantha 2020
  4. #
  5. # Dependencies: jq, curl, w3m
  6. #
  7. version=1.2.1
  8. # By default 'tmpmail' uses 'w3m' as it's web browser to render
  9. # the HTML of the email
  10. browser="w3m"
  11. # The default command that will be used to copy the email address to
  12. # the user's clipboard when running 'tmpmail --copy'
  13. copy_to_clipboard_cmd="xclip -selection c"
  14. # If the value is set to 'true' tmpmail will convert the HTML email
  15. # to raw text and send that to stdout
  16. raw_text=false
  17. # Everything related to 'tmpmail' will be stored in /tmp/tmpmail
  18. # so that the old emails and email addresses get cleared after
  19. # restarting the computer
  20. tmpmail_dir="/tmp/tmpmail"
  21. # tmpmail_email_address is where we store the temporary email address
  22. # that gets generated. This prevents the user from providing
  23. # the email address everytime they run tmpmail
  24. tmpmail_email_address="$tmpmail_dir/email_address"
  25. # tmpmail.html is where the email gets stored.
  26. # Even though the file ends with a .html extension, the raw text version of
  27. # the email will also be stored in this file so that w3m and other browsers
  28. # are able to open this file
  29. tmpmail_html_email="$tmpmail_dir/tmpmail.html"
  30. # Default 1secmail API URL
  31. tmpmail_api_url="https://www.1secmail.com/api/v1/"
  32. usage() {
  33. # Using 'cat << EOF' we can easily output a multiline text. This is much
  34. # better than using 'echo' for each line or using '\n' to create a new line.
  35. cat <<EOF
  36. tmpmail
  37. tmpmail -h | --version
  38. tmpmail -g [ADDRESS]
  39. tmpmail [-t | -b BROWSER] -r | ID
  40. When called with no option and no argument, tmpmail lists the messages in
  41. the inbox and their numeric IDs. When called with one argument, tmpmail
  42. shows the email message with specified ID.
  43. -b, --browser BROWSER
  44. Specify BROWSER that is used to render the HTML of
  45. the email (default: w3m)
  46. --clipboard-cmd COMMAND
  47. Specify the COMMAND to use for copying the email address to your
  48. clipboard (default: xclip -selection c)
  49. -c, --copy
  50. Copy the email address to your clipboard
  51. -d, --domains
  52. Show list of available domains
  53. -g, --generate [ADDRESS]
  54. Generate a new email address, either the specified ADDRESS, or
  55. randomly create one
  56. -h, --help
  57. Show help
  58. -r, --recent
  59. View the most recent email message
  60. -t, --text
  61. View the email as raw text, where all the HTML tags are removed.
  62. Without this option, HTML is used.
  63. --version
  64. Show version
  65. EOF
  66. }
  67. get_list_of_domains() {
  68. # Getting domains list from 1secmail API
  69. data=$(curl -sL "$tmpmail_api_url?action=getDomainList")
  70. # Number of available domains
  71. data_length=$(printf %s "$data" | jq length)
  72. # If the length of the data we got is 0, that means the email address
  73. # has not received any emails yet.
  74. [ "$data_length" -eq 0 ] && echo "1secmail API error for getting domains list" && exit
  75. # Getting rid of quotes, braces and replace comma with space
  76. printf "%s" "$data" | tr -d "[|]|\"" | tr "," " "
  77. }
  78. show_list_of_domains() {
  79. # Convert the list of domains which are in a singal line, into multiple lines
  80. # with a dash in the beginning of each domain for a clean output
  81. domains=$(printf "%s" "$(get_list_of_domains)" | tr " " "\n" | sed "s/^/- /g")
  82. printf "List of available domains: \n%s\n" "$domains"
  83. }
  84. generate_email_address() {
  85. # There are 2 ways which this function is called in this script.
  86. # [1] The user wants to generate a new email and runs 'tmpmail --generate'
  87. # [2] The user runs 'tmpmail' to check the inbox , but /tmp/tmpmail/email_address
  88. # is empty or nonexistant. Therefore a new email gets automatically
  89. # generated before showing the inbox. But of course the inbox will
  90. # be empty as the newly generated email address has not been
  91. # sent any emails.
  92. #
  93. # When the function 'generate_email_address()' is called with the arguement
  94. # 'true', it means that the function was called because the user
  95. # ran 'tmpmail --generate'.
  96. #
  97. # We need this variable so we can know whether or not we need to show the user
  98. # what the email was. <-- More about this can be found further down in this function.
  99. externally=${1:-false}
  100. # This variable lets generate_email_address know if the user has provided a custom
  101. # email address which they want to use. custom is set to false if $2 has no value.
  102. custom=${2:-false}
  103. # Generate a random email address.
  104. # This function is called whenever the user wants to generate a new email
  105. # address by running 'tmpmail --generate' or when the user runs 'tmpmail'
  106. # but /tmp/tmpmail/email_address is empty or nonexistent.
  107. #
  108. # We create a random username by taking the first 10 lines from /dev/random
  109. # and delete all the characters which are *not* lower case letters from A to Z.
  110. # So charcters such as dashes, periods, underscore, and numbers are all deleted,
  111. # giving us a text which only contains lower case letters form A to Z. We then take
  112. # the first 10 characters, which will be the username of the email address
  113. username=$(head /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | cut -c1-11 | tr "[:upper:]" "[:lower:]")
  114. # Generate a regex for valif email adress by fetching the list of supported domains
  115. valid_email_address_regex=$(printf "[a-z0-9]+@%s" "$(get_list_of_domains | tr ' ' '|')")
  116. username_black_list_regex="(abuse|webmaster|contact|postmaster|hostmaster|admin)"
  117. username_black_list="- abuse\n- webmaster\n- contact\n- postmaster\n- hostmaster\n- admin"
  118. # Randomly pick one of the domains mentioned above.
  119. domain=$(printf "%b" "$domains" | tr " " "\n" | randomize | tail -1)
  120. email_address="$username@$domain"
  121. # If the user provided a custom email address then use that email address
  122. if [ "$custom" != false ]; then
  123. email_address=$custom
  124. # Check if the user is using username in the email address which appears
  125. # in the black list.
  126. if printf %b "$email_address" | grep -Eq "$username_black_list_regex"; then
  127. die "For security reasons, that username cannot be used. Here are the blacklisted usernames:\n$username_black_list"
  128. fi
  129. # Do a regex check to see if the email address provided by the user is a
  130. # valid email address
  131. if ! printf %b "$email_address" | grep -Eq "$valid_email_address_regex"; then
  132. die "Provided email is invalid. Must match $valid_email_address_regex"
  133. fi
  134. fi
  135. # Save the generated email address to the $tmpmail_email_address file
  136. # so that it can be whenever 'tmpmail' is run
  137. printf %s "$email_address" >"$tmpmail_email_address"
  138. # If this function was called because the user wanted to generate a new
  139. # email address, show them the email address
  140. [ "$externally" = true ] && cat "$tmpmail_email_address" && printf "\n"
  141. }
  142. get_email_address() {
  143. # This function is only called once and that is when this script
  144. # get executed. The output of this function gets stored in $email_address
  145. #
  146. # If the file that contains the email address is empty,
  147. # that means we do not have an email address, so generate one.
  148. [ ! -s "$tmpmail_email_address" ] && generate_email_address
  149. # Output the email address by getting the first line of $tmpmail_email
  150. head -n 1 "$tmpmail_email_address"
  151. }
  152. list_emails() {
  153. # List all the received emails in a nicely formatted order
  154. #
  155. # Fetch the email data using 1secmail's API
  156. data=$(curl -sL "$tmpmail_api_url?action=getMessages&login=$username&domain=$domain")
  157. # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
  158. # the email address has gotten any emails
  159. data_length=$(printf %s "$data" | jq length)
  160. # We are showing what email address is currently being used
  161. # in case the user has forgotten what the email address was.
  162. printf "[ Inbox for %s ]\n\n" "$email_address"
  163. # If the length of the data we got is 0, that means the email address
  164. # has not received any emails yet.
  165. [ "$data_length" -eq 0 ] && echo "No new mail" && exit
  166. # This is where we store all of our emails, which is then
  167. # displayed using 'column'
  168. inbox=""
  169. # Go through each mail that has been received
  170. index=1
  171. while [ $index -le "${data_length}" ]; do
  172. # Since arrays in JSON data start at 0, we must subtract
  173. # the value of $index by 1 so that we dont miss one of the
  174. # emails in the array
  175. mail_data=$(printf %s "$data" | jq -r ".[$index-1]")
  176. id=$(printf %s "$mail_data" | jq -r ".id")
  177. from=$(printf %s "$mail_data" | jq -r ".from")
  178. subject=$(printf %s "$mail_data" | jq -r ".subject")
  179. # The '||' are used as a divideder for 'column'. 'column' will use this divider as
  180. # a point of reference to create the division. By default 'column' uses a blank space
  181. # but that would not work in our case as the email subject could have multiple white spaces
  182. # and 'column' would split the words that are seperated by white space, in different columns.
  183. inbox="$inbox$id ||$from ||$subject\n"
  184. index=$((index + 1))
  185. done
  186. # Show the emails cleanly
  187. printf "%b" "$inbox" | column -t -s "||"
  188. }
  189. randomize() {
  190. # We could use 'shuf' and 'sort -R' but they are not a part of POSIX
  191. awk 'BEGIN {srand();} {print rand(), $0}' | \
  192. sort -n -k1 | cut -d' ' -f2
  193. }
  194. view_email() {
  195. # View an email by providing it's ID
  196. #
  197. # The first argument provided to this function will be the ID of the email
  198. # that has been received
  199. email_id="$1"
  200. data=$(curl -sL "$tmpmail_api_url?action=readMessage&login=$username&domain=$domain&id=$email_id")
  201. # After the data is retrieved using the API, we have to check if we got any emails.
  202. # Luckily 1secmail's API is not complicated and returns 'Message not found' as plain text
  203. # if our email address as not received any emails.
  204. # If we received the error message from the API just quit because there is nothing to do
  205. [ "$data" = "Message not found" ] && die "Message not found"
  206. # We pass the $data to 'jq' which extracts the values
  207. from=$(printf %s "$data" | jq -r ".from")
  208. subject=$(printf %s "$data" | jq -r ".subject")
  209. html_body=$(printf %s "$data" | jq -r ".htmlBody")
  210. attachments=$(printf %s "$data" | jq -r ".attachments | length")
  211. # If you get an email that is in pure text, the .htmlBody field will be empty and
  212. # we will need to get the content from .textBody instead
  213. [ -z "$html_body" ] && html_body="<pre>$(printf %s "$data" | jq -r ".textBody")</pre>"
  214. # Create the HTML with all the information that is relevant and then
  215. # assigning that HTML to the variable html_mail. This is the best method
  216. # to create a multiline variable
  217. html_mail=$(cat <<EOF
  218. <pre><b>To: </b>$email_address
  219. <b>From: </b>$from
  220. <b>Subject: </b>$subject</pre>
  221. $html_body
  222. EOF
  223. )
  224. if [ ! "$attachments" = "0" ]; then
  225. html_mail="$html_mail<br><b>[Attachments]</b><br>"
  226. index=1
  227. while [ "$index" -le "$attachments" ]; do
  228. filename=$(printf %s "$data" | jq -r ".attachments | .[$index-1] | .filename")
  229. link="$tmpmail_api_url?action=download&login=$username&domain=$domain&id=$email_id&file=$filename"
  230. html_link="<a href=$link download=$filename>$filename</a><br>"
  231. if [ "$raw_text" = true ]; then
  232. # The actual url is way too long and does not look so nice in STDOUT.
  233. # Therefore we will shortening it using is.gd so that it looks nicer.
  234. link=$(curl -s -F"url=$link" "https://is.gd/create.php?format=simple")
  235. html_mail="$html_mail$link [$filename]<br>"
  236. else
  237. html_mail="$html_mail$html_link"
  238. fi
  239. index=$((index + 1))
  240. done
  241. fi
  242. # Save the $html_mail into $tmpmail_html_email
  243. printf %s "$html_mail" >"$tmpmail_html_email"
  244. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  245. # the email to pure text by removing all the HTML tags
  246. [ "$raw_text" = true ] && w3m -dump "$tmpmail_html_email" && exit
  247. # Open up the HTML file using $browser. By default,
  248. # this will be 'w3m'.
  249. $browser "$tmpmail_html_email"
  250. }
  251. view_recent_email() {
  252. # View the most recent email.
  253. #
  254. # This is done by listing all the received email like you
  255. # normally see on the terminal when running 'tmpmail'.
  256. # We then grab the ID of the most recent
  257. # email, which the first line.
  258. mail_id=$(list_emails | head -3 | tail -1 | cut -d' ' -f 1)
  259. view_email "$mail_id"
  260. }
  261. copy_email_to_clipboard(){
  262. # Copy the email thats being used to the user's clipboard
  263. $copy_to_clipboard_cmd < $tmpmail_email_address
  264. }
  265. die() {
  266. # Print error message and exit
  267. #
  268. # The first argument provided to this function will be the error message.
  269. # Script will exit after printing the error message.
  270. printf "%b\n" "Error: $1" >&2
  271. exit 1
  272. }
  273. main() {
  274. # Iterate of the array of dependencies and check if the user has them installed.
  275. # We are checking if $browser is installed instead of checking for 'w3m'. By doing
  276. # this, it allows the user to not have to install 'w3m' if they are using another
  277. # browser to view the HTML.
  278. #
  279. # dep_missing allows us to keep track of how many dependencies the user is missing
  280. # and then print out the missing dependencies once the checking is done.
  281. dep_missing=""
  282. # The main command from $copy_to_clipboard_cmd
  283. # Example:
  284. # xclip -selection c
  285. # ├───┘
  286. # └ This part
  287. clipboard=${copy_to_clipboard_cmd%% *}
  288. for dependency in jq $browser $clipboard curl; do
  289. if ! command -v "$dependency" >/dev/null 2>&1; then
  290. # Append to our list of missing dependencies
  291. dep_missing="$dep_missing $dependency"
  292. fi
  293. done
  294. if [ "${#dep_missing}" -gt 0 ]; then
  295. printf %s "Could not find the following dependencies:$dep_missing"
  296. exit 1
  297. fi
  298. # Create the $tmpmail_dir directory and dont throw any errors
  299. # if it already exists
  300. mkdir -p "$tmpmail_dir"
  301. # Get the email address and save the value to the email_address variable
  302. email_address="$(get_email_address)"
  303. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  304. # In this case, it takes the email_address and removed everything after
  305. # the '@' symbol which gives us the username.
  306. username=${email_address%@*}
  307. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  308. # In this case, it takes the email_address and removes everything until the
  309. # period '.' which gives us the domain
  310. domain=${email_address#*@}
  311. # If no arguments are provided just the emails
  312. [ $# -eq 0 ] && list_emails && exit
  313. while [ "$1" ]; do
  314. case "$1" in
  315. --help | -h) usage && exit ;;
  316. --domains | -d) show_list_of_domains && exit ;;
  317. --generate | -g) generate_email_address true "$2" && exit ;;
  318. --clipboard-cmd) copy_to_clipboard_cmd="$2" ;;
  319. --copy | -c) copy_email_to_clipboard && exit ;;
  320. --browser | -b) browser="$2" ;;
  321. --text | -t) raw_text=true ;;
  322. --version) echo "$version" && exit ;;
  323. --recent | -r) view_recent_email && exit ;;
  324. *[0-9]*)
  325. # If the user provides number as an argument,
  326. # assume its the ID of an email and try getting
  327. # the email that belongs to the ID
  328. view_email "$1" && exit
  329. ;;
  330. -*) die "option '$1' does not exist" ;;
  331. esac
  332. shift
  333. done
  334. }
  335. main "$@"