-
Notifications
You must be signed in to change notification settings - Fork 4
/
1secmail
executable file
·204 lines (187 loc) · 5.54 KB
/
1secmail
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
#
# A script to use 1secmail temp mail service in terminal
#
#/ Usage:
#/ ./1secmail [-u <inbox>|-i <id>|-r|-s|-t|-b]
#/
#/ Options:
#/ no option Randamly get an inbox
#/ -u <inbox> Optional, get an inbox by its mail address
#/ -i <id> Optional, show mail by its id
#/ -r Optional, show the most recent mail
#/ -s Optional, show available domains
#/ -t Optional, show plain text without using w3m
#/ -b Optional, show data URL with base64 encoded message
#/ -h | --help Display this help message
#/
#/ Examples:
#/ \e[32m- Generate a random inbox:\e[0m
#/ ~$ ./1secmail
#/
#/ \e[32m- Get mails in test@1secmail.com inbox:\e[0m
#/ ~$ ./1secmail \e[33m-u 'test@1secmail.com'\e[0m
#/
#/ \e[32m- Show mail 84113380 detail: \e[0m
#/ ~$ ./1secmail \e[33m-u 'test@1secmail.com' -i 84113380\e[0m
#/
#/ \e[32m- Show the most recent mail: \e[0m
#/ ~$ ./1secmail -u 'test@1secmail.com' \e[33m-r\e[0m
#/
#/ \e[32m- Show all available domains: \e[0m
#/ ~$ ./1secmail \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="http://1secmail.net/api/v1"
_INBOX_URL="$_HOST/?action=getMessages"
_MESSAGE_URL="$_HOST/?action=readMessage"
}
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 ":hrstbu:i:" opt; do
case $opt in
u)
_INBOX="$OPTARG"
;;
i)
_MESSAGE_UID="$OPTARG"
_FLAG_GET_MESSAGE=true
;;
r)
_FLAG_RECENT_MAIL=true
;;
s)
_FLAG_SHOW_DOMAIN=true
;;
t)
_FLAG_SHOW_TEXT=true
;;
b)
_FLAG_SHOW_B64=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
}
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
local login domain
login=$(awk -F '@' '{print $1}' <<< "$1")
domain=$(awk -F '@' '{print $2}' <<< "$1")
$_CURL -sSL "${_INBOX_URL}&login=${login}&domain=${domain}" | $_JQ .
}
get_recent_message() {
# Get the most recent message
# $1: address
local messages
messages="$(get_inbox "$_INBOX")"
if [[ -z "$messages" ]]; then
echo "Message not found"
exit 0
else
get_message "$_INBOX" "$($_JQ -r '.[0].id' <<< "$messages")"
fi
}
get_message() {
# Get message by id
# $1: address
# $2: id
local login domain message
login=$(awk -F '@' '{print $1}' <<< "$1")
domain=$(awk -F '@' '{print $2}' <<< "$1")
message="$($_CURL -sSL "${_MESSAGE_URL}&login=${login}&domain=${domain}&id=${2}")"
if [[ "$message" == "Message not found" ]]; then
echo "$message"
exit 0
else
if [[ -z $_W3M || -n "${_FLAG_SHOW_TEXT:-}" ]]; then
$_JQ -r '.htmlBody' <<< "$message"
elif [[ -n "${_FLAG_SHOW_B64:-}" ]]; then
echo "data:text/html;base64,$($_JQ -r ".htmlBody" <<< "$message" | base64)"
else
local from subject date body
from="$($_JQ -r ".from" <<< "$message")"
subject="$($_JQ -r ".subject" <<< "$message")"
date="$($_JQ -r ".date" <<< "$message")"
body="$($_JQ -r ".htmlBody" <<< "$message")"
$_W3M -T "text/html" <<< "<pre><b>Date:</b> ${date}<br/><b>From:</b> ${from}<br/><b>To:</b> ${1}<br/><b>Subject:</b> ${subject}</pre>${body}"
fi
fi
}
show_domain() {
# Show available domains
"$_CURL" -sS 'https://www.1secmail.com/api/v1/?action=getDomainList' | "$_JQ" -r '.[]'
}
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
if [[ -n "${_INBOX:-}" ]]; then
if [[ "${_FLAG_GET_MESSAGE:-}" == true ]]; then
get_message "$_INBOX" "${_MESSAGE_UID:-}"
elif [[ -n "${_FLAG_RECENT_MAIL:-}" ]]; then
get_recent_message "$_INBOX"
else
get_inbox "$_INBOX"
fi
fi
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi