-
Notifications
You must be signed in to change notification settings - Fork 6
/
smsapi
executable file
·114 lines (114 loc) · 2.65 KB
/
smsapi
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
#!/bin/bash
VERBOSE=0
OAUTH=""
SMSAPI_PL="https://api.smsapi.pl/"
SMSAPI_COM="https://api.smsapi.com/"
help() {
echo "Usage: smsapi sms send [OPTIONS] <to> <message>"
echo "Options:
--service <SMSAPI_PL or SMSAPI_COM>
--oauth <OAuth token>
--from <string> Sender name
--encoding <string> Message encoding (default:utf8)
-f, --fast <0|1> Fast
-n, --normalize <0|1> Normalize message
-v Verbose"
exit 0;
}
ARGS=( )
declare -A OPTIONS
if [ -f ./.smsapi.rc ]; then
source ./.smsapi.rc
elif [ -f ~/.smsapi.rc ]; then
source ~/.smsapi.rc
fi
setOption() {
case $1 in
"f") OPTIONS[fast]="$2" ;;
"n") OPTIONS[normalize]="$2" ;;
*) OPTIONS[$1]="$2";
esac
}
setParam() {
case $1 in
"v") VERBOSE=$2 ;;
"oauth") OAUTH="$2" ;;
"service") SERVICE="$2" ;;
*) OPTIONS[$1]="$2";
esac
}
while [ ! -z "$1" ]; do
if [[ $1 =~ ^-([a-zA-Z0-9])$ ]]; then
setParam ${BASH_REMATCH[1]} 1
elif [[ $1 =~ ^--([a-z0-9A-Z_\-]+)=(.*)$ ]]; then
setParam ${BASH_REMATCH[1]} "${BASH_REMATCH[2]}"
elif [[ $1 =~ ^--([a-z0-9A-Z_\-]+)$ ]]; then
shift
setParam ${BASH_REMATCH[1]} "$1"
elif [[ $1 =~ ^--$ ]]; then
shift
ARGS[${#ARGS[@]}]="$1"
else
ARGS[${#ARGS[@]}]="$1"
fi
shift
done
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9]) o="${c}" ;;
*) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
verbose() {
if [ $VERBOSE == 1 ]; then
echo "$@" 1>&2
fi
}
sms_send() {
local url="${!SERVICE}sms.do"
local params=()
params[${#params[@]}]=to=$(rawurlencode "$1")
params[${#params[@]}]=message=$(rawurlencode "$2")
if [ ! -z ${OPTIONS[normalize]} ] && [ ${OPTIONS[normalize]} -eq 1 ]; then
params[${#params[@]}]="normalize=1"
fi
if [ ! -z ${OPTIONS[encoding]} ]; then
params[${#params[@]}]="encoding=${OPTIONS[encoding]}"
else
params[${#params[@]}]="encoding=utf8"
fi
if [ ! -z "${OPTIONS[from]}" ]; then
local fromName=$(rawurlencode "${OPTIONS[from]}")
params[${#params[@]}]="from=${fromName}"
fi
if [ ! -z ${OPTIONS[fast]} ] && [ ${OPTIONS[fast]} -eq 1 ]; then
params[${#params[@]}]="fast=1"
fi
verbose "${params[@]}"
local data=$( printf "&%s" "${params[@]}" )
data=${data:1}
token=${OAUTH}
RESULT=$(curl "$url" -A "smsapi/bash-client" --header "Authorization: Bearer $token" -d "$data" -s -S 2>&2)
echo $RESULT
}
if [ ${#ARGS[@]} -lt 2 ]; then help; fi
COMMAND="${ARGS[0]}"
ACTION="${ARGS[1]}"
ARGS=("${ARGS[@]:2}")
if [ "$COMMAND" == "sms" ]; then
if [ "$ACTION" == "send" ]; then
sms_send "${ARGS[0]}" "${ARGS[1]}"
else
help "$COMMAND"
fi
else
help
fi