Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
l.pajak@smsapi.pl authored and l.pajak@smsapi.pl committed Dec 10, 2013
0 parents commit 0cec0b9
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Wysyłanie smsów z poziomu linuxowego terminala
===========

```bash
$ ./smsapi sms send --username LOGIN --password PASS 48xxxyyyzzz,48zzzyyyxxx "Hello world"
```

```bash
$ ./smsapi
Usage: smsapi sms send [OPTIONS] <to> <message>
Options:
--username <string>
--password <password>

--from <string> Sender name
--encoding <string> Message encoding (default:utf8)

-f, --fast <0|1> Fast

-n, --normalize <0|1> Normalize message
-v Verbose
```

plik konfiguracyjny ".smsapi.rc" może się znajdowac w katalogu roboczym lub w katalogo domowym użytkownika

5 changes: 5 additions & 0 deletions example.smsapi.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USERNAME=user
PASSWORD=pass
OPTIONS[encoding]=utf8
#OPTIONS[from]=
#OPTIONS[normalize]=1
146 changes: 146 additions & 0 deletions smsapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash

VERBOSE=0
USERNAME=""
PASSWORD=""
URI="https://ssl.smsapi.pl/api/"

help() {

echo "Usage: smsapi sms send [OPTIONS] <to> <message>"

echo "Options:
--username <string>
--password <password>
--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 ;;
"username") USERNAME="$2" ;;
"password") PASSWORD="$2" ;;
"uri") URI="$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="${URI}sms.do"
local params=()
params[${#params[@]}]=username=$(rawurlencode "$USERNAME")
params[${#params[@]}]=password=$(rawurlencode "$PASSWORD")
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
params[${#params[@]}]="from=${OPTIONS[from]}"
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}

RESULT=$(curl "$url" "$args" -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

0 comments on commit 0cec0b9

Please sign in to comment.