-
Notifications
You must be signed in to change notification settings - Fork 1
/
getnada
executable file
·179 lines (161 loc) · 4.37 KB
/
getnada
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
#
# A script to use getnada temp mail service in terminal
#
#/ Usage:
#/ ./getnada [-u <inbox>|-i <uid>|-d <uid>|-s|-t]
#/
#/ Options:
#/ no option Optional, randamly get an inbox
#/ -u <inbox> Optional, get an inbox by its mail address
#/ -i <uid> Optional, show mail by its uid
#/ -d <uid> Optional, delete mail by its uid
#/ -s Optional, show available domains
#/ -t Optional, show plain text without using w3m
#/ -h | --help Display this help message
#/
#/ Examples:
#/ \e[32m- Generate a random inbox:\e[0m
#/ $ ./getnada
#/
#/ \e[32m- Get mails in test@getnada.com:\e[0m
#/ $ ./getnada \e[33m-u 'test@getnada.com'\e[0m
#/
#/ \e[32m- Show mail uUa4V5Hjmkqf9O detail: \e[0m
#/ $ ./getnada \e[33m-i uUa4V5Hjmkqf9O\e[0m
#/
#/ \e[32m- Delete mail uUa4V5Hjmkqf9O: \e[0m
#/ $ ./getnada \e[33m-d uUa4V5Hjmkqf9O\e[0m
#/
#/ \e[32m- Show all available domains: \e[0m
#/ $ ./getnada \e[33m-s\e[0m
set -e
set -u
usage() {
# Display usage message
printf "\n%b\n" "$(grep '^#/' "$0" | cut -c4-)" && exit 0
}
set_var() {
# Declare variables
_HOST="https://getnada.com/api/v1"
_INBOX_URL="$_HOST/inboxes"
_MESSAGE_URL="$_HOST/messages/html"
_DOMAIN_URL="$_HOST/domains"
}
set_command() {
# Declare commands
_CURL="$(command -v curl)" || command_not_found "curl" "https://curl.haxx.se/download.html"
_JQ="$(command -v jq)" || command_not_found "jq" "https://stedolan.github.io/jq/"
_W3M="$(command -v w3m)" || true
_FAKER="$(command -v faker-cli)" || true
}
set_args() {
# Declare arguments
expr "$*" : ".*--help" > /dev/null && usage
while getopts ":hstu:i:d:" opt; do
case $opt in
u)
_INBOX="$OPTARG"
;;
i)
_MESSAGE_UID="$OPTARG"
_FLAG_GET_MESSAGE=true
;;
d)
_MESSAGE_UID="$OPTARG"
_FLAG_DELETE_MESSAGE=true
;;
s)
_FLAG_SHOW_DOMAIN=true
;;
t)
_FLAG_SHOW_TEXT=true
;;
h)
usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
}
command_not_found() {
# Show command not found message
# $1: command name
# $2: installation URL
printf "%b\n" '\033[31m'"$1"'\033[0m command not found!'
[[ -n "${2:-}" ]] && printf "%b\n" 'Install from \033[31m'"$2"'\033[0m'
exit 1
}
jq_format() {
# Show result formated by jq
# $1: response
if $_JQ -e . >/dev/null 2>&1 <<<"$1"; then
echo "$1" | $_JQ .
else
echo "$1"
fi
}
fake_username () {
# Create a fake user
if [[ -z "$_FAKER" ]]; then
tr -dc 'a-z0-9' < /dev/urandom \
| head -c9
else
sed -E 's/"//g' <<< "$($_FAKER -n firstName).$($_FAKER -n lastName)" \
| tr '[:upper:]' '[:lower:]'
fi
}
get_inbox() {
# Get inbox by mailbox address
# $1: address
jq_format "$($_CURL -sSX GET "$_INBOX_URL/$1")"
}
get_message() {
# Get message by uid
# $1: uid
if [[ -z $_W3M || -n "${_FLAG_SHOW_TEXT:-}" ]]; then
$_CURL -sSX GET "$_MESSAGE_URL/$1"
else
$_W3M -T "text/html" <<< "$($_CURL -sSX GET "$_MESSAGE_URL/$1")"
fi
}
delete_message() {
# Delete message by uid
# $1: uid
jq_format "$($_CURL -sSX DELETE "$_MESSAGE_URL/$1")"
}
show_domain() {
# Show available domains
$_CURL -sSX GET "$_DOMAIN_URL" | $_JQ -r '.[].name'
}
show_random_domain() {
# Show available domains in a random order
show_domain | shuf
}
get_random_inbox() {
# Get a randam inbox
local u d
u=$(fake_username)
d=$(show_random_domain | tail -1)
get_inbox "$u@$d"
echo "$u@$d"
}
main() {
set_args "$@"
set_command
set_var
if [[ -z "$*" ]]; then
get_random_inbox
else
[[ -n "${_FLAG_SHOW_DOMAIN:-}" ]] && show_domain
[[ -n "${_INBOX:-}" ]] && get_inbox "$_INBOX"
[[ "${_FLAG_GET_MESSAGE:-}" == true ]] && get_message "${_MESSAGE_UID:-}"
[[ "${_FLAG_DELETE_MESSAGE:-}" == true ]] && delete_message "${_MESSAGE_UID:-}" && get_message "${_MESSAGE_UID:-}"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi