From f480e147313d105a58a4d8c1216e2cf6a909d972 Mon Sep 17 00:00:00 2001 From: Daniel Dallos Date: Sat, 30 Sep 2017 22:59:03 +0200 Subject: [PATCH 1/4] fixed e-mail regex issue --- pushbullet | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pushbullet b/pushbullet index ec3d21f..120cf89 100755 --- a/pushbullet +++ b/pushbullet @@ -545,7 +545,7 @@ push) info "Sending to All Devices" json="$json}" # $2 must be a contact/an email address if it contains an @. - elif expr "$2" : '*@*' > /dev/null ; then + elif expr "$2" : '.*@.*' > /dev/null ; then info "Sending to email address $2" json="$json,\"email\":\"$2\"}" # since it's an email we are also creating a chat From 847288b46e6de8524be236af39acd8c4bf0f11df Mon Sep 17 00:00:00 2001 From: danesz Date: Sun, 1 Oct 2017 21:29:41 +0200 Subject: [PATCH 2/4] added support for multiple devices --- pushbullet | 214 ++++++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 102 deletions(-) diff --git a/pushbullet b/pushbullet index 120cf89..7b549d2 100755 --- a/pushbullet +++ b/pushbullet @@ -20,6 +20,8 @@ ERR_PB_OTHER=213 CURLOPTS="--silent" +MULTIPLE_DEVICE_DELIMITER = ":" + info() { if [ -z "${QUIET}" ]; then echo "$@" @@ -448,126 +450,134 @@ push) printUsage fi + IFS="$MULTIPLE_DEVICE_DELIMITER" read -r -a devices_array <<< "$2" + # get device listing curlres=$(getDevices) devices=$(echo "$curlres" | tr '{' '\n' | tr ',' '\n' | grep \"nickname\" | cut -d'"' -f4) idens=$(echo "$curlres" | tr '{' '\n' | grep active\"\:true | tr ',' '\n' | grep iden | cut -d'"' -f4) - lineNum=$(echo "$devices" | grep -i -n "$2" | cut -d: -f1) - # check the lenght of $lineNum, if it returns more than one entry then the search was not unique enough - lineLength=$(echo $lineNum | wc -w) - if [ $lineLength -gt '1' ]; then - err "The given device name is not unique enough. Please check 'pushbullet list'" - exit ${ERR_PB_DEVICE} - fi + # if there is only 1 device as a parameter, it will be still parsed to devices_array[0] + for one_device in "${devices_array[@]}" + do - dev_id=$(echo "$idens" | sed -n $lineNum'p') - dev_name=$(echo "$devices" | sed -n $lineNum'p') + lineNum=$(echo "$devices" | grep -i -n "$one_device" | cut -d: -f1) - title="$4" - body="" - if [ ! -t 0 ]; then - # we have something on stdin - body=$(cat) - # remove unprintable characters, or pushbullet API fails - body=$(echo "$body"|tr -dc '[:print:]\n'|tr '"' "'") - fi - - if expr "$4" : 'https\?://*' > /dev/null ; then - body=${body:-$5} - url="$4" - elif expr "$5" : 'https\?://*' > /dev/null ; then - body=${body:-$6} - url="$5" - else - body=${body:-$5} - url="$6" - fi + # check the lenght of $lineNum, if it returns more than one entry then the search was not unique enough + lineLength=$(echo $lineNum | wc -w) + if [ $lineLength -gt '1' ]; then + err "The given device name is not unique enough. Please check 'pushbullet list'" + exit ${ERR_PB_DEVICE} + fi - # replace newlines with an escape sequence - body=$(printf '%s\n' "$body" | sed 's/\n/\\n/g') + dev_id=$(echo "$idens" | sed -n $lineNum'p') + dev_name=$(echo "$devices" | sed -n $lineNum'p') - case $3 in - note) - type=note - json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\"" - ;; - link) - type=link - if expr "$url" : 'https\?://*' > /dev/null ; then - err "Error: A valid link has to start with http:// or https://" - exit ${ERR_FMT} + title="$4" + body="" + if [ ! -t 0 ]; then + # we have something on stdin + body=$(cat) + # remove unprintable characters, or pushbullet API fails + body=$(echo "$body"|tr -dc '[:print:]\n'|tr '"' "'") fi - json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"url\":\"$url\"" - ;; - file) - file=$4 - if [ -d $file ]; then - info "Given file is actually a folder, compressing it first" - archivename="$(date +%s)" - tar cfz /tmp/"$archivename.tar.gz" "$file" - file=/tmp/"$archivename.tar.gz" + + if expr "$4" : 'https\?://*' > /dev/null ; then + body=${body:-$5} + url="$4" + elif expr "$5" : 'https\?://*' > /dev/null ; then + body=${body:-$6} + url="$5" + else + body=${body:-$5} + url="$6" fi - if [ -z $file ] || [ ! -f $file ]; then - err "Error: no valid file to push was specified" - exit ${ERR_NOFILE} + + # replace newlines with an escape sequence + body=$(printf '%s\n' "$body" | sed 's/\n/\\n/g') + + case $3 in + note) + type=note + json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\"" + ;; + link) + type=link + if expr "$url" : 'https\?://*' > /dev/null ; then + err "Error: A valid link has to start with http:// or https://" + exit ${ERR_FMT} + fi + json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"url\":\"$url\"" + ;; + file) + file=$4 + if [ -d $file ]; then + info "Given file is actually a folder, compressing it first" + archivename="$(date +%s)" + tar cfz /tmp/"$archivename.tar.gz" "$file" + file=/tmp/"$archivename.tar.gz" + fi + if [ -z $file ] || [ ! -f $file ]; then + err "Error: no valid file to push was specified" + exit ${ERR_NOFILE} + fi + # Api docs: https://docs.pushbullet.com/v2/upload-request/ + mimetype=$(file -i -b "$file") + set +e + curlres=$(curl $CURLOPTS --header "Access-Token: $PB_API_KEY" \ + --header "Content-Type: application/json" \ + --data-binary "{\"file_name\":\"$file\",\"file_type\":\"${mimetype%:*}\"}" \ + --request POST \ + "$API_URL/upload-request") + curlretcode=$? + checkCurlReturnCode "$curlretcode" + curlres2=$(curl $CURLOPTS --include --request POST \ + $(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep upload_url | awk -F\" '{print $(NF-1)}') \ + -F file=@"$file") + curlretcode=$? + set -e + checkCurlReturnCode "$curlretcode" + + type=file + file_name=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_name |awk -F\" '{print $(NF-1)}') + file_type=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_type |awk -F\" '{print $(NF-1)}') + file_url=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_url |awk -F\" '{print $(NF-1)}') + json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"file_name\":\"$file_name\",\"file_type\":\"$file_type\",\"file_url\":\"$file_url\"" + ;; + *) + printUsage + ;; + esac + + if [ "$one_device" = "all" ]; then + info "Sending to All Devices" + json="$json}" + # $one_device must be a contact/an email address if it contains an @. + elif expr "$one_device" : '.*@.*' > /dev/null ; then + info "Sending to email address $one_device" + json="$json,\"email\":\"$one_device\"}" + # since it's an email we are also creating a chat + $0 chat create "$one_device" + # $one_device must be a channel_tag if $lineNum is empty. + elif [ -z "$lineNum" ]; then + info "Sending to channel $one_device" + json="$json,\"channel_tag\":\"$one_device\"}" + # in all other cases $one_device must be the identifier of a device. + else + info "Sending to device $dev_name" + json="$json,\"device_iden\":\"$dev_id\"}" fi - # Api docs: https://docs.pushbullet.com/v2/upload-request/ - mimetype=$(file -i -b "$file") set +e curlres=$(curl $CURLOPTS --header "Access-Token: $PB_API_KEY" \ - --header "Content-Type: application/json" \ - --data-binary "{\"file_name\":\"$file\",\"file_type\":\"${mimetype%:*}\"}" \ + --header "Content-type: application/json" \ + --data-binary "$json" \ --request POST \ - "$API_URL/upload-request") - curlretcode=$? - checkCurlReturnCode "$curlretcode" - curlres2=$(curl $CURLOPTS --include --request POST \ - $(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep upload_url | awk -F\" '{print $(NF-1)}') \ - -F file=@"$file") + "$API_URL/pushes") curlretcode=$? set -e - checkCurlReturnCode "$curlretcode" - - type=file - file_name=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_name |awk -F\" '{print $(NF-1)}') - file_type=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_type |awk -F\" '{print $(NF-1)}') - file_url=$(echo "$curlres" | "$PROGDIR"/JSON.sh -b | grep file_url |awk -F\" '{print $(NF-1)}') - json="{\"type\":\"$type\",\"title\":\"$title\",\"body\":\"$body\",\"file_name\":\"$file_name\",\"file_type\":\"$file_type\",\"file_url\":\"$file_url\"" - ;; - *) - printUsage - ;; - esac - - if [ "$2" = "all" ]; then - info "Sending to All Devices" - json="$json}" - # $2 must be a contact/an email address if it contains an @. - elif expr "$2" : '.*@.*' > /dev/null ; then - info "Sending to email address $2" - json="$json,\"email\":\"$2\"}" - # since it's an email we are also creating a chat - $0 chat create "$2" - # $2 must be a channel_tag if $lineNum is empty. - elif [ -z "$lineNum" ]; then - info "Sending to channel $2" - json="$json,\"channel_tag\":\"$2\"}" - # in all other cases $2 must be the identifier of a device. - else - info "Sending to device $dev_name" - json="$json,\"device_iden\":\"$dev_id\"}" - fi - set +e - curlres=$(curl $CURLOPTS --header "Access-Token: $PB_API_KEY" \ - --header "Content-type: application/json" \ - --data-binary "$json" \ - --request POST \ - "$API_URL/pushes") - curlretcode=$? - set -e - checkCurlOutput "$curlres" "$curlretcode" + checkCurlOutput "$curlres" "$curlretcode" + done ;; setup) CLIENT_ID=RP56dyRen86HaaLnXBevnrDTHT8fTcr6 From 81e77b2a3f58ad3d6b497837fa33fef96b57c53a Mon Sep 17 00:00:00 2001 From: Daniel Dallos Date: Mon, 2 Oct 2017 15:38:15 +0200 Subject: [PATCH 3/4] fixed sh while loop and params --- pushbullet | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pushbullet b/pushbullet index 7b549d2..194221f 100755 --- a/pushbullet +++ b/pushbullet @@ -20,8 +20,6 @@ ERR_PB_OTHER=213 CURLOPTS="--silent" -MULTIPLE_DEVICE_DELIMITER = ":" - info() { if [ -z "${QUIET}" ]; then echo "$@" @@ -450,8 +448,6 @@ push) printUsage fi - IFS="$MULTIPLE_DEVICE_DELIMITER" read -r -a devices_array <<< "$2" - # get device listing curlres=$(getDevices) @@ -459,9 +455,8 @@ push) idens=$(echo "$curlres" | tr '{' '\n' | grep active\"\:true | tr ',' '\n' | grep iden | cut -d'"' -f4) # if there is only 1 device as a parameter, it will be still parsed to devices_array[0] - for one_device in "${devices_array[@]}" + for one_device in $2 do - lineNum=$(echo "$devices" | grep -i -n "$one_device" | cut -d: -f1) # check the lenght of $lineNum, if it returns more than one entry then the search was not unique enough From 17ed9d242b61f0bdd4d75678a03f90095715d330 Mon Sep 17 00:00:00 2001 From: Daniel Dallos Date: Mon, 2 Oct 2017 21:25:57 +0200 Subject: [PATCH 4/4] modified comments --- pushbullet | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pushbullet b/pushbullet index 194221f..f7fa9ae 100755 --- a/pushbullet +++ b/pushbullet @@ -73,6 +73,7 @@ list - List all devices and contacts in your PushBullet account. (does not requi additional parameters) push - Push data to a device or contact. (the device name can simply be a unique part of the name that \"list\" returns) + To send pushes to multiple devices, you can declare them like this (between quotes): "device1 device2" pushes active - List your 'active' pushes (pushes that haven't been deleted). Format is: iden:type:title:modified pushes recent - List all of your 'active' pushes since the last run. @@ -454,7 +455,7 @@ push) devices=$(echo "$curlres" | tr '{' '\n' | tr ',' '\n' | grep \"nickname\" | cut -d'"' -f4) idens=$(echo "$curlres" | tr '{' '\n' | grep active\"\:true | tr ',' '\n' | grep iden | cut -d'"' -f4) - # if there is only 1 device as a parameter, it will be still parsed to devices_array[0] + # if there is only 1 device as a parameter, it will be still parsed to one_device for one_device in $2 do lineNum=$(echo "$devices" | grep -i -n "$one_device" | cut -d: -f1)