-
Notifications
You must be signed in to change notification settings - Fork 0
/
bail
executable file
·151 lines (130 loc) · 4.04 KB
/
bail
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
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------------------------------------------------------
# CONFIG
# -----------------------------------------------------------------------------
__VERSION="0.1"
__NAME="Template App"
__FILE=$(basename "$0")
__CACHE=${XDG_STATE_HOME:=$HOME/.config/state}/bail
readonly __VERSION __FILE __NAME __CACHE
mkdir -p "$__CACHE"
current_account="$__CACHE"/account
touch "$current_account"
url="https://api.mail.tm"
readonly url
password='D~%8W_UE'
custom_password=0
# -----------------------------------------------------------------------------
# PRIVATE interfaces
# -----------------------------------------------------------------------------
function _get_domain {
domain=$(curl -s "$url/domains" | jq -r '."hydra:member"[0].domain')
echo "$domain"
}
function _random {
openssl rand -hex "${1:-5}"
}
function _gen_password {
if [ "$custom_password" == 0 ]; then
custom_password=1
if command -v pwgen &> /dev/null; then
password=$(pwgen -Cns 20 1)
else
password=$(_random 10)
fi
fi
}
# -----------------------------------------------------------------------------
# PUBLIC interfaces
# -----------------------------------------------------------------------------
function create { # Create a new account, create [prefix]
__parse_options "$@"
_gen_password
domain=$(_get_domain)
if [[ -n $* ]]; then
address=$*"@$domain"
else
address=$(_random)"@$domain"
fi
data='{ "address":"'$address'", "password":"'$password'" }'
account=$(curl -s "$url/accounts" \
-H 'Content-Type: application/json' \
-d "$data")
echo "$account" | jq '.+= {"password":"'"$password"'"}' > "$__CACHE"/account
echo "created"
jq ".address" "$current_account"
}
function me { # List all created acounts
address=$(jq -r ".address" < "$current_account")
pass=$(jq -r ".password" < "$current_account")
token=$(_token "$address" "$pass")
curl -s "$url/me" -H "Authorization:Bearer $token" | jq
}
function delete { # Delete an account
__parse_options "$@"
id=$(jq -r '.id' < "$current_account")
address=$(jq -r ".address" < "$current_account")
pass=$(jq -r ".password" < "$current_account")
token=$(_token "$address" "$pass")
curl -s -XDELETE "$url/accounts/$id" -H "Authorization:Bearer $token"
rm "$current_account"
echo "deleted current account"
}
function _token {
curl -s "$url/token" \
-H 'Content-Type: application/json' \
-d '{"address":"'"$1"'", "password":"'"$2"'"}' \
| jq -r ".token"
}
function messages { # get messages
address=$(jq -r ".address" < "$current_account")
pass=$(jq -r ".password" < "$current_account")
token=$(_token "$address" "$pass")
messages=$(curl -s "$url/messages" -H "Authorization:Bearer $token")
opts=$(echo "$messages" | jq -r '."hydra:member"[] | .from.name +"<"+ .from.address +">√"+ .subject' | column -t -s "√")
mapfile -t opts <<< "$opts"
c=0
for i in "${opts[@]}"; do
echo -e "$c\t$i"
c=$((c + 1))
done
echo -n "pick a number: "
read -r picked
msg=$(echo "$messages" | jq -r '."hydra:member"['"$picked"'] .id')
message=$(curl -s "$url/messages/$msg" -H "Authorization:Bearer $token")
echo "from:"
echo "$message" | jq -r '.from.name + "<" + .from.address + ">"'
echo ""
echo "subject:"
echo "$message" | jq -r '.subject'
echo ""
echo "message:"
echo -e "$(echo "$message" | jq -r '.text')"
}
# -----------------------------------------------------------------------------
function help { # Display this helpful help information
local functions
functions=$(grep -E "^function [^_]*?$" "$0" | sed -E "s/ *{ *# */#/g" | column -t -s '#' | sed -E "s/^function / /g" | sort)
echo "$__NAME v $__VERSION"
echo "Usage: $__FILE <task> [args]"
echo ""
echo "Tasks:"
echo "$functions"
echo "args:"
echo " -p define custom password"
echo ""
}
function __parse_options {
while getopts "p:" o; do
case "$o" in
p)
password=${OPTARG:-$password}
custom_password=1
;;
*) ;;
esac
done
shift $((OPTIND - 1))
}
"${@:-help}"