/
netgrowlnotify.py
executable file
·266 lines (209 loc) · 8.48 KB
/
netgrowlnotify.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
"""
netgrowlnotify V0.1
Copyright (c) Jason Penney, 2009
"""
import socket
import os
import os.path
import sys
import time
from optparse import OptionParser
VERSION=0.1
def sendMessage(message, addr, socket_type=socket.SOCK_DGRAM, response_size=None):
response = None
s = socket.socket(socket.AF_INET,socket_type)
if socket_type == socket.SOCK_STREAM:
s.connect(addr)
s.sendto(message, addr)
if response_size != None:
response = s.recv(response_size)
s.close()
return response
def sendGNTPMessage(message,addr):
import gntp
response = gntp.parse_gntp(sendMessage(message.encode(), addr, socket.SOCK_STREAM,1024))
if isinstance(response, gntp.GNTPOK):
return response
raise Exception('invalid response\n', response)
def gntpnotify(options):
try:
import gntp
except Exception as e:
sys.stderr.write("Unable to load 'gntp' module\n")
sys.stderr.write(e)
sys.exit(1)
if options.port == None:
options.port = 23053
addr = (options.host, options.port)
#register
r = gntp.GNTPRegister()
r.add_header('Application-Name',options.name)
r.add_notification(options.identifier,True)
if options.password:
r.set_password(options.password)
response = sendGNTPMessage(r,addr)
p = gntp.GNTPNotice()
p.add_header('Application-Name',options.name)
p.add_header('Notification-Name',options.identifier)
p.add_header('Notification-Title',options.title)
p.add_header('Origin-Software-Name',os.path.basename(sys.argv[0]))
p.add_header('Origin-Software-Version',VERSION)
if options.password:
p.set_password(options.password)
if options.sticky:
p.add_header('Notification-Sticky',options.sticky)
if options.priority:
p.add_header('Notification-Priority',options.priority)
if options.message:
options.message = options.message.replace("\n","\\n")
p.add_header('Notification-Text',options.message)
sendGNTPMessage(p,addr)
def prowlnotify(options):
try:
import prowlpy
except Exception as e:
sys.stderr.write("Unable to load 'prowlpy' module\n")
sys.stderr.write(e)
sys.exit(1)
if options.prowl_key == "" and options.password != "":
options.prowl_key = options.password
if options.prowl_key == "":
if options.prowl_keyfile == "":
try:
options.prowl_keyfile = os.path.join(os.path.expanduser("~"),
".prowlkey")
except:
pass
if options.prowl_keyfile != "":
keyfile = None
try:
keyfile = open(options.prowl_keyfile,'r')
except:
pass
if keyfile:
options.prowl_key = keyfile.readline().rstrip()
if options.prowl_key == "":
sys.stderr.write("please provide Prowl API key using --prowl-key or --prowl-keyfile\n")
sys.exit(3)
application = ""
if options.name != os.path.basename(sys.argv[0]):
application = options.name
if options.identifier != "":
application = application + ": "
if options.identifier != "":
application = application + options.identifier
if application == "":
application=options.name
p = prowlpy.Prowl(options.prowl_key)
p.post(application=application,
event=options.title,
description=options.message,
priority=options.priority)
def netgrowlnotify(options):
try:
import netgrowl
except Exception as e:
sys.stderr.write("Unable to load 'netgrowl' module\n")
sys.stderr.write(e)
sys.exit(1)
if options.port == None:
options.port=netgrowl.GROWL_UDP_PORT
addr = (options.host, options.port)
r = netgrowl.GrowlRegistrationPacket(application=options.name,
password=options.password)
r.addNotification(options.identifier)
sendMessage(r.payload(),addr)
p = netgrowl.GrowlNotificationPacket(application=options.name,
notification=options.identifier,
title=options.title,
description=options.message,
priority=options.priority,
sticky=options.sticky,
password=options.password)
sendMessage(p.payload(),addr)
def parse_time(stime):
ftime = time.strftime('%Y %j ',time.localtime()) + stime.replace(' ','')
ret = None
try:
ret = time.strptime(ftime,'%Y %j %I:%M%p')
except:
ret = None
pass
if ret == None:
try:
ret = time.strptime(ftime, '%Y %j %H:%M')
except:
ret = None
pass
if ret == None:
raise Exception("unable to parse time: " + stime)
return ret
if __name__ == "__main__":
usage = "Usage: %prog [options] [title]"
parser = OptionParser(usage=usage, version='%s %s' % ('%prog', VERSION))
parser.add_option("-n", "--name", dest="name",
help="Set the name of the application that sends the notification",
type="string", default=os.path.basename(sys.argv[0]))
parser.add_option("-s", "--sticky", dest="sticky",
help="Make the notification sticky",action="store_true",
default=False)
parser.add_option("-m", "--message", dest="message",
help="Sets the message to be used instead of using stdin",
type="string", default="")
parser.add_option("-p", "--priority", dest="priority",
help="Specify an int or named key (default is 0)",
type="int", default=0)
parser.add_option("-d", "--identifier", dest="identifier",
help="Specify a notification identifier (used for coalescing)",
type="string", default="")
parser.add_option("-H", "--host", dest="host",
help="Specify a hostname to which to send a remote notification.",
type="string", default="localhost")
parser.add_option("-P", "--password", dest="password",
help="Password used for remote notifications.",
type="string", default="")
parser.add_option("", "--port", dest="port",
help="Port number for UDP notifications.",
type="int", default=None)
parser.add_option("-t", "--title", dest="title",
help="Does nothing. Any text following will be treated as the",
type="string", default="")
# prowl support
parser.add_option("","--prowl",action="store_true", default=False,
help="Send notification to Prowl instead of Growl")
parser.add_option("","--prowl-key",type="string",dest="prowl_key",
default="",
help="API key used for Prowl notifications")
parser.add_option("","--prowl-keyfile", type="string", dest="prowl_keyfile",
help="Path to file containing Prowl API key",
default="")
# gntp support
parser.add_option("-g","--gntp",action="store_true",default=False,
help="Send notification via GNTP (default is to use UDP)")
# time window support
parser.add_option("","--time-start", default=None, type="string",
dest="time_start")
parser.add_option("","--time-end", default=None, type="string",
dest="time_end")
(options,args) = parser.parse_args()
options.title = options.title + " " + " ".join(args)
if options.message:
options.message = options.message.replace("\\n","\n")
if options.time_start != None or options.time_end != None:
now = time.localtime()
if options.time_start != None:
mintime = parse_time(options.time_start)
if now < mintime:
sys.exit(0)
if options.time_end != None:
maxtime = parse_time(options.time_end)
if now > maxtime:
print "too late"
sys.exit(0)
if options.prowl:
prowlnotify(options)
elif options.gntp:
gntpnotify(options)
else:
netgrowlnotify(options)