1
0

tmpmail 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #!/usr/bin/env sh
  2. #
  3. # by Siddharth Dushantha 2020
  4. #
  5. # Dependencies: jq, curl, w3m
  6. #
  7. version=1.1.9
  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. valid_email_address_regex="[a-z0-9]+@(1secmail\.(com|net|org)|esiix.co|wwjmp.com|xojxe.com|yoggm.com)"
  88. username_black_list_regex="(abuse|webmaster|contact|postmaster|hostmaster|admin)"
  89. username_black_list="- abuse\n- webmaster\n- contact\n- postmaster\n- hostmaster\n- admin"
  90. domains="1secmail.com 1secmail.net 1secmail.org esiix.com wwjmp.com xojxe.com yoggm.com"
  91. # Randomly pick one of the domains mentiond above.
  92. domain=$(printf "%b" "$domains" | tr " " "\n" | randomize | tail -1)
  93. email_address="$username@$domain"
  94. # If the user provided a custom email address then use that email address
  95. if [ "$custom" != false ]; then
  96. email_address=$custom
  97. # Check if the user is using username in the email address which appears
  98. # in the black list.
  99. if printf %b "$email_address" | grep -Eq "$username_black_list_regex"; then
  100. print_error "For security reasons, that username cannot be used. Here are the blacklisted usernames:\n$username_black_list"
  101. fi
  102. # Do a regex check to see if the email address provided by the user is a
  103. # valid email address
  104. if ! printf %b "$email_address" | grep -Eq "$valid_email_address_regex"; then
  105. print_error "Provided email is invalid. Must match $valid_email_address_regex"
  106. fi
  107. fi
  108. # Save the generated email address to the $tmpmail_email_address file
  109. # so that it can be whenever 'tmpmail' is run
  110. printf %s "$email_address" >"$tmpmail_email_address"
  111. # If this function was called because the user wanted to generate a new
  112. # email address, show them the email address
  113. [ "$externally" = true ] && cat "$tmpmail_email_address" && printf "\n"
  114. }
  115. get_email_address() {
  116. # This function is only called once and that is when this script
  117. # get executed. The output of this function gets stored in $email_address
  118. #
  119. # If the file that contains the email address is empty,
  120. # that means we do not have an email address, so generate one.
  121. [ ! -s "$tmpmail_email_address" ] && generate_email_address
  122. # Output the email address by getting the first line of $tmpmail_email
  123. head -n 1 "$tmpmail_email_address"
  124. }
  125. list_emails() {
  126. # List all the received emails in a nicely formatted order
  127. #
  128. # Fetch the email data using 1secmail's API
  129. data=$(curl -sL "$tmpmail_api_url?action=getMessages&login=$username&domain=$domain")
  130. # Using 'jq' we get the length of the JSON data. From this we can determine whether or not
  131. # the email address has gotten any emails
  132. data_length=$(printf %s "$data" | jq length)
  133. # We are showing what email address is currently being used
  134. # in case the user has forgotten what the email address was.
  135. printf "[ Inbox for %s ]\n\n" "$email_address"
  136. # If the length of the data we got is 0, that means the email address
  137. # has not received any emails yet.
  138. [ "$data_length" -eq 0 ] && echo "No new mail" && exit
  139. # This is where we store all of our emails, which is then
  140. # displayed using 'column'
  141. inbox=""
  142. # Go through each mail that has been received
  143. index=1
  144. while [ $index -le "${data_length}" ]; do
  145. # Since arrays in JSON data start at 0, we must subtract
  146. # the value of $index by 1 so that we dont miss one of the
  147. # emails in the array
  148. mail_data=$(printf %s "$data" | jq -r ".[$index-1]")
  149. id=$(printf %s "$mail_data" | jq -r ".id")
  150. from=$(printf %s "$mail_data" | jq -r ".from")
  151. subject=$(printf %s "$mail_data" | jq -r ".subject")
  152. # The '||' are used as a divideder for 'column'. 'column' will use this divider as
  153. # a point of reference to create the division. By default 'column' uses a blank space
  154. # but that would not work in our case as the email subject could have multiple white spaces
  155. # and 'column' would split the words that are seperated by white space, in different columns.
  156. inbox="$inbox$id ||$from ||$subject\n"
  157. index=$((index + 1))
  158. done
  159. # Show the emails cleanly
  160. printf "%b" "$inbox" | column -t -s "||"
  161. }
  162. randomize() {
  163. # We could use 'shuf' and 'sort -R' but they are not a part of POSIX
  164. awk 'BEGIN {srand();} {print rand(), $0}' | \
  165. sort -n -k1 | cut -d' ' -f2
  166. }
  167. view_email() {
  168. # View an email by providing it's ID
  169. #
  170. # The first argument provided to this function will be the ID of the email
  171. # that has been received
  172. email_id="$1"
  173. data=$(curl -sL "$tmpmail_api_url?action=readMessage&login=$username&domain=$domain&id=$email_id")
  174. # After the data is retrieved using the API, we have to check if we got any emails.
  175. # Luckly 1secmail's API is not complicated and returns 'Message not found' as plain text
  176. # if our email address as not received any emails.
  177. # If we received the error message from the API just quit because there is nothing to do
  178. [ "$data" = "Message not found" ] && print_error "Message not found"
  179. # We pass the $data to 'jq' which extracts the values
  180. from=$(printf %s "$data" | jq -r ".from")
  181. subject=$(printf %s "$data" | jq -r ".subject")
  182. html_body=$(printf %s "$data" | jq -r ".htmlBody")
  183. attachments=$(printf %s "$data" | jq -r ".attachments | length")
  184. # If you get an email that is in pure text, the .htmlBody field will be empty and
  185. # we will need to get the content from .textBody instead
  186. [ -z "$html_body" ] && html_body="<pre>$(printf %s "$data" | jq -r ".textBody")</pre>"
  187. # Create the HTML with all the information that is relevant and then
  188. # assigning that HTML to the variable html_mail. This is the best method
  189. # to create a multiline variable
  190. html_mail=$(cat <<EOF
  191. <pre><b>To: </b>$email_address
  192. <b>From: </b>$from
  193. <b>Subject: </b>$subject</pre>
  194. $html_body
  195. EOF
  196. )
  197. if [ ! "$attachments" = "0" ]; then
  198. html_mail="$html_mail<br><b>[Attachments]</b><br>"
  199. index=1
  200. while [ "$index" -le "$attachments" ]; do
  201. filename=$(printf %s "$data" | jq -r ".attachments | .[$index-1] | .filename")
  202. link="$tmpmail_api_url?action=download&login=$username&domain=$domain&id=$email_id&file=$filename"
  203. html_link="<a href=$link download=$filename>$filename</a><br>"
  204. if [ "$raw_text" = true ]; then
  205. # The actual url is way too long and does not look so nice in STDOUT.
  206. # Therefore we will shortening it using is.gd so that it looks nicer.
  207. link=$(curl -s -F"url=$link" "https://is.gd/create.php?format=simple")
  208. html_mail="$html_mail$link [$filename]<br>"
  209. else
  210. html_mail="$html_mail$html_link"
  211. fi
  212. index=$((index + 1))
  213. done
  214. fi
  215. # Save the $html_mail into $tmpmail_html_email
  216. printf %s "$html_mail" >"$tmpmail_html_email"
  217. # If the '--text' flag is used, then use 'w3m' to convert the HTML of
  218. # the email to pure text by removing all the HTML tags
  219. [ "$raw_text" = true ] && w3m -dump "$tmpmail_html_email" && exit
  220. # Open up the HTML file using $browser. By default,
  221. # this will be 'w3m'.
  222. $browser "$tmpmail_html_email"
  223. }
  224. view_recent_email() {
  225. # View the most recent email.
  226. #
  227. # This is done by listing all the received email like you
  228. # normally see on the terminal when running 'tmpmail'.
  229. # We then grab the ID of the most recent
  230. # email, which the first line.
  231. mail_id=$(list_emails | head -3 | tail -1 | cut -d' ' -f 1)
  232. view_email "$mail_id"
  233. }
  234. print_error() {
  235. # Print error message
  236. #
  237. # The first argument provided to this function will be the error message.
  238. # Script will exit after printing the error message.
  239. printf "%b\n" "Error: $1" >&2
  240. exit 1
  241. }
  242. main() {
  243. # Iterate of the array of dependencies and check if the user has them installed.
  244. # We are checking if $browser is installed instead of checking for 'w3m'. By doing
  245. # this, it allows the user to not have to install 'w3m' if they are using another
  246. # browser to view the HTML
  247. for dependency in jq $browser curl; do
  248. if ! command -v "$dependency" >/dev/null 2>&1; then
  249. print_error "Could not find '$dependency', is it installed?"
  250. fi
  251. done
  252. # Create the $tmpmail_dir directory and dont throw any errors
  253. # if it already exists
  254. mkdir -p "$tmpmail_dir"
  255. # Get the email address and save the value to the email_address variable
  256. email_address="$(get_email_address)"
  257. # ${VAR#PATTERN} Removes shortest match of pattern from start of a string.
  258. # In this case, it takes the email_address and removed everything after
  259. # the '@' symbol which gives us the username.
  260. username=${email_address%@*}
  261. # ${VAR%PATTERN} Remove shortest match of pattern from end of a string.
  262. # In this case, it takes the email_address and removes everything until the
  263. # period '.' which gives us the domain
  264. domain=${email_address#*@}
  265. # If no arguments are provided just the emails
  266. [ $# -eq 0 ] && list_emails && exit
  267. while [ "$1" ]; do
  268. case "$1" in
  269. --help | -h) usage && exit ;;
  270. --generate | -g) generate_email_address true "$2" && exit ;;
  271. --browser | -b) browser="$2" ;;
  272. --text | -t) raw_text=true ;;
  273. --version) echo "$version" && exit ;;
  274. --recent | -r) view_recent_email && exit ;;
  275. *[0-9]*)
  276. # If the user provides number as an argument,
  277. # assume its the ID of an email and try getting
  278. # the email that belongs to the ID
  279. view_email "$1" && exit
  280. ;;
  281. -*) print_error "option '$1' does not exist" ;;
  282. esac
  283. shift
  284. done
  285. }
  286. main "$@"