spidah-ruby_gntp 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +8 -0
- data/lib/ruby_gntp.rb +110 -27
- metadata +1 -1
data/ChangeLog
CHANGED
@@ -16,3 +16,11 @@
|
|
16
16
|
== Version 0.1.1 - 2009/4/17
|
17
17
|
* Add NOTE to example directory.
|
18
18
|
|
19
|
+
== Version 0.1.2 - 2009/08/15
|
20
|
+
* Enabled password authentication mainly for sending notifications to a network machine.
|
21
|
+
|
22
|
+
== Version 0.1.3 - 2009/08/19
|
23
|
+
* Added notification icon sending.
|
24
|
+
* Now sends out Origin-X headers.
|
25
|
+
* Use \r\n instead of \n in the header lines.
|
26
|
+
|
data/lib/ruby_gntp.rb
CHANGED
@@ -40,6 +40,9 @@ class GNTP
|
|
40
40
|
attr_reader :app_name, :target_host, :target_port, :password
|
41
41
|
attr_reader :message if $DEBUG
|
42
42
|
|
43
|
+
RUBY_GNTP_NAME = 'ruby_gntp'
|
44
|
+
RUBY_GNTP_VERSION = '0.1.3'
|
45
|
+
|
43
46
|
def initialize(app_name = 'Ruby/GNTP', host = 'localhost', password = '', port = 23053)
|
44
47
|
@app_name = app_name
|
45
48
|
@target_host = host
|
@@ -55,36 +58,37 @@ class GNTP
|
|
55
58
|
raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications
|
56
59
|
|
57
60
|
@app_icon = params[:app_icon]
|
61
|
+
@binaries = []
|
62
|
+
|
63
|
+
@message = register_header(@app_name, @app_icon)
|
64
|
+
@message << output_origin_headers
|
58
65
|
|
59
|
-
@message
|
60
|
-
|
61
|
-
Application-Name: #{@app_name}
|
62
|
-
Notifications-Count: #{@notifications.size}
|
63
|
-
EOF
|
64
|
-
@message << "Application-Icon: #{@app_icon}\n" if @app_icon
|
65
|
-
@message << "\n"
|
66
|
+
@message << "Notifications-Count: #{@notifications.size}\r\n"
|
67
|
+
@message << "\r\n"
|
66
68
|
|
67
69
|
@notifications.each do |notification|
|
68
70
|
name = notification[:name]
|
69
|
-
disp_name = notification[:disp_name]
|
71
|
+
disp_name = notification[:disp_name] || name
|
70
72
|
enabled = notification[:enabled] || true
|
71
73
|
icon = notification[:icon]
|
72
74
|
|
73
|
-
@message
|
74
|
-
Notification-
|
75
|
-
|
76
|
-
@message << "
|
77
|
-
@message << "Notification-Enabled: #{enabled}\n" if enabled
|
78
|
-
@message << "Notification-Icon: #{icon}\n" if icon
|
79
|
-
@message << "\n"
|
75
|
+
@message << "Notification-Name: #{name}\r\n"
|
76
|
+
@message << "Notification-Enabled: #{enabled ? 'True' : 'False'}\r\n"
|
77
|
+
@message << "Notification-Display-Name: #{disp_name}\r\n"
|
78
|
+
@message << "#{handle_icon(icon, 'Notification')}\r\n" if icon
|
80
79
|
end
|
81
80
|
|
81
|
+
@binaries.each {|binary|
|
82
|
+
@message << output_binary(binary)
|
83
|
+
}
|
84
|
+
|
85
|
+
@message << "\r\n"
|
86
|
+
|
82
87
|
unless (ret = send_and_recieve(@message))
|
83
88
|
raise "Register failed"
|
84
89
|
end
|
85
90
|
end
|
86
91
|
|
87
|
-
|
88
92
|
#
|
89
93
|
# notify
|
90
94
|
#
|
@@ -97,16 +101,16 @@ EOF
|
|
97
101
|
icon = params[:icon] || get_notification_icon(name)
|
98
102
|
sticky = params[:sticky]
|
99
103
|
|
100
|
-
@
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
@message << "\n"
|
104
|
+
@binaries = []
|
105
|
+
|
106
|
+
@message = notify_header(app_name, name, title, text, sticky, icon)
|
107
|
+
@message << output_origin_headers
|
108
|
+
|
109
|
+
@binaries.each {|binary|
|
110
|
+
@message << output_binary(binary)
|
111
|
+
}
|
112
|
+
|
113
|
+
@message << "\r\n"
|
110
114
|
|
111
115
|
unless (ret = send_and_recieve(@message))
|
112
116
|
raise "Notify failed"
|
@@ -133,7 +137,6 @@ EOF
|
|
133
137
|
# send and recieve
|
134
138
|
#
|
135
139
|
def send_and_recieve msg
|
136
|
-
msg.gsub!(/\n/, "\r\n")
|
137
140
|
print msg if $DEBUG
|
138
141
|
|
139
142
|
sock = TCPSocket.open(@target_host, @target_port)
|
@@ -159,6 +162,51 @@ EOF
|
|
159
162
|
return notification[:icon]
|
160
163
|
end
|
161
164
|
|
165
|
+
#
|
166
|
+
# outputs the registration header
|
167
|
+
#
|
168
|
+
def register_header(app_name, app_icon)
|
169
|
+
message = "#{get_gntp_header_start('REGISTER')}\r\n"
|
170
|
+
message << "Application-Name: #{app_name}\r\n"
|
171
|
+
message << "#{handle_icon(@app_icon, 'Application')}\r\n" if app_icon
|
172
|
+
end
|
173
|
+
|
174
|
+
#
|
175
|
+
# outputs the notification header
|
176
|
+
#
|
177
|
+
def notify_header(app_name, name, title, text, sticky, icon)
|
178
|
+
message = "#{get_gntp_header_start('NOTIFY')}\r\n"
|
179
|
+
message << "Application-Name: #{@app_name}\r\n"
|
180
|
+
message << "Notification-Name: #{name}\r\n"
|
181
|
+
message << "Notification-Title: #{title}\r\n"
|
182
|
+
message << "Notification-Text: #{text}\r\n" if text
|
183
|
+
message << "Notification-Sticky: #{sticky}\r\n" if sticky
|
184
|
+
message << "#{handle_icon(icon, 'Notification')}\r\n" if icon
|
185
|
+
end
|
186
|
+
|
187
|
+
def output_origin_headers
|
188
|
+
message = "Origin-Machine-Name: #{Socket.gethostname}\r\n"
|
189
|
+
message << "Origin-Software-Name: #{RUBY_GNTP_NAME}\r\n"
|
190
|
+
message << "Origin-Software-Version: #{RUBY_GNTP_VERSION}\r\n"
|
191
|
+
|
192
|
+
platformname, platformversion = '', ''
|
193
|
+
|
194
|
+
if ENV['OS']
|
195
|
+
ver = `ver`
|
196
|
+
if ver.index('[')
|
197
|
+
matches = ver.scan(/(.*)\[+(.*)\]+/)[0]
|
198
|
+
platformname, platformversion = matches[0], matches[1]
|
199
|
+
else
|
200
|
+
platformname, platformversion = 'Microsoft Windows', ver
|
201
|
+
end
|
202
|
+
else
|
203
|
+
platformname, platformversion = `uname -s`, `uname -r`
|
204
|
+
end
|
205
|
+
|
206
|
+
message << "Origin-Platform-Name: #{platformname.strip}\r\n"
|
207
|
+
message << "Origin-Platform-Version: #{platformversion.strip}\r\n"
|
208
|
+
end
|
209
|
+
|
162
210
|
#
|
163
211
|
# get start of the GNTP header
|
164
212
|
#
|
@@ -174,6 +222,41 @@ EOF
|
|
174
222
|
"GNTP/1.0 #{type} NONE MD5:#{keyhash}.#{salthash}"
|
175
223
|
end
|
176
224
|
end
|
225
|
+
|
226
|
+
#
|
227
|
+
# figure out how to handle the icon
|
228
|
+
# a URL icon just gets put into the header
|
229
|
+
# a file icon gets read and stored, ready to be appended to the end of the request
|
230
|
+
#
|
231
|
+
def handle_icon(icon, type)
|
232
|
+
if File.exists?(icon)
|
233
|
+
file = File.new(icon)
|
234
|
+
data = file.read
|
235
|
+
size = data.length
|
236
|
+
if size > 0
|
237
|
+
binary = {
|
238
|
+
:size => size,
|
239
|
+
:data => data,
|
240
|
+
:uniqueid => Digest::MD5.hexdigest(data)
|
241
|
+
}
|
242
|
+
@binaries << binary
|
243
|
+
"#{type}-Icon: x-growl-resource://#{binary[:uniqueid]}"
|
244
|
+
end
|
245
|
+
else
|
246
|
+
"#{type}-Icon: #{icon}"
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
#
|
251
|
+
# outputs any binary data to be sent
|
252
|
+
#
|
253
|
+
def output_binary(binary)
|
254
|
+
message = "\r\n"
|
255
|
+
message << "Identifier: #{binary[:uniqueid]}\r\n"
|
256
|
+
message << "Length: #{binary[:size]}\r\n"
|
257
|
+
message << "\r\n"
|
258
|
+
message << "#{binary[:data]}\r\n"
|
259
|
+
end
|
177
260
|
end
|
178
261
|
|
179
262
|
#----------------------------
|