tmpmail 15 KB

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