Просмотр исходного кода

Get list of available domains direct from API instead of hardcoded list

aincube 3 лет назад
Родитель
Сommit
51074a09fb
1 измененных файлов с 24 добавлено и 1 удалено
  1. 24 1
      tmpmail

+ 24 - 1
tmpmail

@@ -59,6 +59,8 @@ shows the email message with specified ID.
         clipboard (default: xclip -selection c)
 -c, --copy
         Copy the email address to your clipboard
+-d, --domains
+        Show list of available domains
 -g, --generate [ADDRESS]
         Generate a new email address, either the specified ADDRESS, or
         randomly create one
@@ -74,6 +76,24 @@ shows the email message with specified ID.
 EOF
 }
 
+get_list_of_domains() {
+    # Getting domains list from 1secmail API
+    data=$(curl -sL "$tmpmail_api_url?action=getDomainList") 
+    # Number of available domains
+    data_length=$(printf %s "$data" | jq length)
+
+    # If the length of the data we got is 0, that means the email address
+    # has not received any emails yet.
+    [ "$data_length" -eq 0 ] && echo "1secmail API error for getting domains list" && exit
+    # Getting rid of quotes, braces and replace comma with space
+    domains=$(printf "${data//[\"\[\]]/}" | tr "," " ")
+}
+
+show_list_of_domains() {
+    get_list_of_domains
+    printf "List of available domains: \n%s\n" "${domains//\ /$'\n'}"
+}
+
 generate_email_address() {
     # There are 2 ways which this function is called in this script.
     #  [1] The user wants to generate a new email and runs 'tmpmail --generate'
@@ -110,7 +130,9 @@ generate_email_address() {
     valid_email_address_regex="[a-z0-9]+@(1secmail\.(com|net|org)|esiix.co|wwjmp.com|xojxe.com|yoggm.com)"
     username_black_list_regex="(abuse|webmaster|contact|postmaster|hostmaster|admin)"
     username_black_list="- abuse\n- webmaster\n- contact\n- postmaster\n- hostmaster\n- admin"
-    domains="1secmail.com 1secmail.net 1secmail.org esiix.com wwjmp.com xojxe.com yoggm.com" 
+
+    # Getting list of available domains into $domains variable
+    get_list_of_domains
 
     # Randomly pick one of the domains mentioned above.
     domain=$(printf "%b" "$domains" | tr " " "\n" | randomize | tail -1)
@@ -356,6 +378,7 @@ main() {
     while [ "$1" ]; do
         case "$1" in
             --help | -h) usage && exit ;;
+            --domains | -d) show_list_of_domains && exit ;;
             --generate | -g) generate_email_address true "$2" && exit ;;
             --clipboard-cmd) copy_to_clipboard_cmd="$2" ;;
             --copy | -c) copy_email_to_clipboard && exit ;;