|
@@ -0,0 +1,144 @@
|
|
1
|
+#!/bin/bash
|
|
2
|
+#########################################################################################
|
|
3
|
+### androidNotifty.sh #
|
|
4
|
+### Script for sending notifications using "Notify My Androids" API #
|
|
5
|
+### Author : Markp1989@gmail.com Version: 25JULY2011 #
|
|
6
|
+#########################################################################################
|
|
7
|
+## Requirements: curl #
|
|
8
|
+#########################################################################################
|
|
9
|
+## Usage: androidNotify.sh "Application Name" "Event Name" "Some Details" [priority] #
|
|
10
|
+#########################################################################################
|
|
11
|
+## API Documentation: https://www.notifymyandroid.com/api.jsp #
|
|
12
|
+#########################################################################################
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+loadConf()
|
|
16
|
+{
|
|
17
|
+ if [ -e "$1" ]
|
|
18
|
+ then
|
|
19
|
+ echo "[ info ] Found a config file: $1"
|
|
20
|
+ . "$1"
|
|
21
|
+ fi
|
|
22
|
+}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+#####start user configurable settings####
|
|
26
|
+APIkey="0" ## API Key must me set here, go to http://www.notifymyandroid.com/ to get one
|
|
27
|
+limit=5 # how many times to attempt to run curl, can help on unstable networks.
|
|
28
|
+pinghost="google.fr" ##host to ping before running anything to verify that the internet is up.
|
|
29
|
+#####end user configurable settings######
|
|
30
|
+
|
|
31
|
+loadConf /etc/nma.conf
|
|
32
|
+loadConf ~/.nma.conf
|
|
33
|
+
|
|
34
|
+##renaming parameters to make rest of the code more readable.
|
|
35
|
+command=$0
|
|
36
|
+application=$1
|
|
37
|
+event=$2
|
|
38
|
+description=$3
|
|
39
|
+priority=$4
|
|
40
|
+
|
|
41
|
+##the urls that are used to send the notification##
|
|
42
|
+baseurl="https://www.notifymyandroid.com/publicapi"
|
|
43
|
+verifyurl="$baseurl/verify"
|
|
44
|
+posturl="$baseurl/notify"
|
|
45
|
+
|
|
46
|
+##choosing a unique temp file based on the time (date,month,hour,minute,nano second),to avoid concurent usage interfering with each other
|
|
47
|
+notifyout=/tmp/androidNotify$(date '+%d%m%Y%H%M%S%N')
|
|
48
|
+
|
|
49
|
+usage="Usage: $command Application Event Description [priority]"
|
|
50
|
+##exit functions so i dont have to keep writing the same exit messages for every fail.
|
|
51
|
+function error_exit {
|
|
52
|
+##this function is used to exit the program in the event of an error.
|
|
53
|
+ printf '%s\n\t%s\n\t\t%s\n' "[ Error ] Notification not sent:" "$errormessage" "$usage"
|
|
54
|
+printf '\n%s\n' "$application , $event , $description, priority=$priority" >> $notifyout ##adding info used to tmp file.
|
|
55
|
+ exit 1
|
|
56
|
+
|
|
57
|
+}
|
|
58
|
+function clean_exit {
|
|
59
|
+ printf '%s\n' "[ info ] Notification sent"
|
|
60
|
+rm "$notifyout" ##removing output file when notification was sent okay
|
|
61
|
+ exit 0
|
|
62
|
+}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+function pre_check {
|
|
66
|
+##this function checks that curl is installed, and that an internet connection is available
|
|
67
|
+which curl >/dev/null || { errormessage="curl is required but it's not installed, exiting."; error_exit; }
|
|
68
|
+##checking that the machine can connect to the internet by pinging the host defined in $pinghost
|
|
69
|
+ping -c 1 -w 10 "$pinghost" >/dev/null || { errormessage="internet connection is required but not connected, exiting."; error_exit; }
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+function input_check {
|
|
73
|
+##this section checks that the parameters are an acceptable length if any of these tests fail then the program exits.
|
|
74
|
+##the API will send an error back if the data sent is invalid, but no point in knowingly sending incorrect information.
|
|
75
|
+#API key must be 48 characters long, just because the APIkey is the right length doesnt mean its valid,the API will complain if it is invalid.
|
|
76
|
+if [[ "${#APIkey}" -ne "48" ]]; then
|
|
77
|
+errormessage="APIkey must be 48 characters long, you gave me ${#APIkey}"
|
|
78
|
+ error_exit
|
|
79
|
+#application must be between 1 and 256 characters long
|
|
80
|
+elif [[ "${#application}" -gt "256" || "${#application}" -lt "1" ]]; then
|
|
81
|
+ errormessage="[ error ] the application parameter is invalid or missing"
|
|
82
|
+error_exit
|
|
83
|
+#event must be between 1 and 1000 characters long
|
|
84
|
+elif [[ "${#event}" -gt "1000" || "${#event}" -lt "1" ]]; then
|
|
85
|
+ errormessage="[ error ] the event parameter is invalid or missing"
|
|
86
|
+error_exit
|
|
87
|
+#description must be between 1 and 1000 characters long
|
|
88
|
+elif [[ "${#description}" -gt "1000" || "${#description}" -lt "1" ]]; then
|
|
89
|
+errormessage="[ error ] the description parameter is invalid or missing"
|
|
90
|
+error_exit
|
|
91
|
+##priority is expected to be between -2 and 2, if other numbers are given than default(0) is used.
|
|
92
|
+fi
|
|
93
|
+
|
|
94
|
+if [ -z $priority ]; then
|
|
95
|
+printf '%s\n' "priority is not set , must be between -2 and 2, using 0 instead"
|
|
96
|
+ priority=0
|
|
97
|
+elif [[ "$priority" -gt "2" || $priority -lt "-2" ]]; then
|
|
98
|
+printf '%s\n' "priority $priority is invalid, must be between -2 and 2, using 0 instead"
|
|
99
|
+priority=0
|
|
100
|
+fi
|
|
101
|
+
|
|
102
|
+if [[ -n "$5" ]]; then
|
|
103
|
+errormessage="[ error ] too many parameters have been provided:"
|
|
104
|
+error_exit
|
|
105
|
+fi
|
|
106
|
+
|
|
107
|
+}
|
|
108
|
+
|
|
109
|
+function send_notification {
|
|
110
|
+##sending the notification using curl
|
|
111
|
+#if curl failes to complete then it will try again, untill the "limit" is met,or curl runs ok.
|
|
112
|
+complete=0
|
|
113
|
+tries=0
|
|
114
|
+while [[ $complete -lt 1 && $tries -lt $limit ]]
|
|
115
|
+do
|
|
116
|
+#if curl is already running that we wait up to a minute before proceding
|
|
117
|
+if [ "$(pidof curl)" ]
|
|
118
|
+then
|
|
119
|
+ printf '%s\n' "another instance of curl is running, waiting up to 1 minute before trying."
|
|
120
|
+ sleep $((RANDOM % 60)) ##waiting up to 1 minute as running multiple instances of curl at a time was causing some to fail on my machine.
|
|
121
|
+fi
|
|
122
|
+curl --silent --data-ascii "apikey=$APIkey" --data-ascii "application=$application" --data-ascii "event=$event" --data-asci "description=$description" --data-asci "priority=$priority" $posturl -o $notifyout && complete=1
|
|
123
|
+tries=$tries+1
|
|
124
|
+done
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+function check_notification {
|
|
128
|
+##checking that the notification was sent ok.
|
|
129
|
+##api returns message 200 if the notification was sent
|
|
130
|
+if grep -q 200 $notifyout; then
|
|
131
|
+clean_exit
|
|
132
|
+else
|
|
133
|
+##getting the error reported by the API, this may break if the API changes its output, will change as soon as I can find a commandline xml parser
|
|
134
|
+errormessage="$(cut -f4 -d'>' $notifyout | cut -f1 -d'<')"
|
|
135
|
+
|
|
136
|
+error_exit
|
|
137
|
+fi
|
|
138
|
+}
|
|
139
|
+
|
|
140
|
+##running the functions
|
|
141
|
+pre_check
|
|
142
|
+input_check
|
|
143
|
+send_notification
|
|
144
|
+check_notification
|