social_stream-presence 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,301 @@
1
+ #!/usr/bin/env ruby
2
+ #Rest Api Client Script
3
+ #Version: 13-12-2011
4
+ #@author Aldo
5
+
6
+
7
+ require 'logger'
8
+ require 'rest_client'
9
+ require 'openssl'
10
+ require 'digest/md5'
11
+
12
+
13
+ path = "/var/log/ejabberd/scripts.log"
14
+ file = File.open(path, File::WRONLY | File::APPEND | File::CREAT)
15
+ file.sync = true
16
+ $logger = Logger.new(file)
17
+ $logger.level = Logger::DEBUG
18
+
19
+ def getOption(option)
20
+ File.open('/etc/ejabberd/ssconfig.cfg', 'r') do |f1|
21
+ while line = f1.gets
22
+ line = line.gsub(/\n/,'')
23
+ if line.match(/^#/)
24
+ #Comments
25
+ elsif line.match(/^#{option}/)
26
+ return line.gsub(/#{option}/,'')
27
+ end
28
+ end
29
+ end
30
+ return "Undefined"
31
+ end
32
+
33
+ #Constants
34
+ $secure_rest_api = getOption("secure_rest_api=")
35
+ $pass = getOption("ejabberd_password=")
36
+ $scripts_path = getOption("scripts_path=")
37
+ $script_title = "Rest Api Client"
38
+
39
+
40
+ def log(title,text)
41
+ $logger.info title + ": " + text
42
+ end
43
+
44
+ def getMethodName
45
+ caller[0] =~ /`(.*?)'/
46
+ $1
47
+ end
48
+
49
+
50
+ #####################
51
+ ##### Example #####
52
+ #####################
53
+ #def myHook(param1,param2)
54
+ # log(getMethodName,"(My message: #{param1},#{param2})")
55
+ # url = "http://" + getOption("web_domain=") + "/xmpp/hookRoute"
56
+
57
+ # params = {}
58
+ # encrypted_params = {}
59
+ # #Add params to sent in clear
60
+ # params[:param1_in_server]=param1
61
+ # #Add params to sent cipher
62
+ # encrypted_params[:param2_in_server]=param2
63
+
64
+ # return [getMethodName,generic_api_call(url,params,encrypted_params)]
65
+ #end
66
+
67
+ def setConnection(username)
68
+ log($script_title,"#{getMethodName}(#{username})")
69
+ url = "http://" + getOption("web_domain=") + "/xmpp/setConnection"
70
+
71
+ params = {}
72
+ encrypted_params = {}
73
+ encrypted_params[:name]=username
74
+
75
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
76
+ end
77
+
78
+
79
+ def unsetConnection(username)
80
+ log($script_title,"#{getMethodName}(#{username})")
81
+ url = "http://" + getOption("web_domain=") + "/xmpp/unsetConnection"
82
+
83
+ params = {}
84
+ encrypted_params = {}
85
+ encrypted_params[:name]=username
86
+
87
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
88
+ end
89
+
90
+
91
+ def setPresence(username,status)
92
+ log($script_title,"#{getMethodName}(#{username},#{status})")
93
+ url = "http://" + getOption("web_domain=") + "/xmpp/setPresence"
94
+
95
+ params = {}
96
+ encrypted_params = {}
97
+ encrypted_params[:name]=username
98
+ encrypted_params[:status]=status
99
+
100
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
101
+ end
102
+
103
+
104
+ def unsetPresence(username)
105
+ log($script_title,"#{getMethodName}(#{username})")
106
+ url = "http://" + getOption("web_domain=") + "/xmpp/unsetPresence"
107
+
108
+ params = {}
109
+ encrypted_params = {}
110
+ encrypted_params[:name]=username
111
+
112
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
113
+ end
114
+
115
+
116
+ def resetConnection()
117
+ log($script_title,"Call #{getMethodName}()")
118
+ url = "http://" + getOption("web_domain=") + "/xmpp/resetConnection"
119
+
120
+ params = {}
121
+ encrypted_params = {}
122
+
123
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
124
+ end
125
+
126
+
127
+ def synchronize()
128
+ log($script_title,"Call #{getMethodName}()")
129
+ url = "http://" + getOption("web_domain=") + "/xmpp/synchronizePresence"
130
+
131
+
132
+ #Get connected users using Emanagement
133
+ users = []
134
+ command = $scripts_path + "/emanagement getConnectedUsers"
135
+ output = %x[#{command}]
136
+ sessions = output.split("\n")
137
+
138
+ sessions.each do |session|
139
+ users << session.split("@")[0]
140
+ end
141
+ #Finish
142
+
143
+
144
+ #In this cases users always will be sent in clear (not cipher)
145
+ #(Too much data to cipher)
146
+ #Anyway, we must to build the hash to pass the authentication
147
+ params = {}
148
+ encrypted_params = {}
149
+ params[:name]=users.join(",")
150
+
151
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
152
+ end
153
+
154
+
155
+
156
+ #Params must include the password and all the parameters to be sent in clear.
157
+ #Anyway, If "secure_rest_api" is disable, encrypted_params will be send in clear.
158
+ def generic_api_call(url,params,encrypted_params)
159
+
160
+ begin
161
+ unless params
162
+ params = {}
163
+ end
164
+
165
+ unless encrypted_params
166
+ encrypted_params = {}
167
+ end
168
+
169
+ params[:password]=$pass
170
+
171
+ response=sendHttpRequest(url,params,encrypted_params)
172
+ puts response.body
173
+
174
+ if response.body.include?("Ok")
175
+ return true
176
+ else
177
+ return false
178
+ end
179
+
180
+ rescue => e
181
+ log($script_title,"#{e.class.name}: #{e.message}")
182
+ puts("#{e.class.name}: #{e.message}")
183
+ return false
184
+ end
185
+ end
186
+
187
+
188
+ #Send HTTP Request to Social Stream Presence API
189
+ def sendHttpRequest(url,params,encrypted_params)
190
+
191
+ unless params[:password]
192
+ return "params[:password] required in sendHttpRequest";
193
+ end
194
+
195
+
196
+ if $secure_rest_api == "true"
197
+ xmpp_private_key_path = $scripts_path + "/rsa_keys/xmpp_rsa_key_private.pem";
198
+ web_public_key_path = $scripts_path + "/rsa_keys/web_rsa_key_public.pem";
199
+ xmpp_private_key = OpenSSL::PKey::RSA.new(File.read(xmpp_private_key_path))
200
+ web_public_key = OpenSSL::PKey::RSA.new(File.read(web_public_key_path))
201
+
202
+ request_params = {};
203
+
204
+ #Copy non encypted params
205
+ params.each do |key,value|
206
+ unless key.to_s == "password"
207
+ request_params[key] = value
208
+ end
209
+ end
210
+
211
+ #Include encrypted params
212
+ if encrypted_params and encrypted_params.empty? == false
213
+ request_params[:encrypted_params] = web_public_key.public_encrypt(encrypted_params.to_s)
214
+ end
215
+
216
+ #Generating stamp
217
+ #1 Get constant password
218
+ password = params[:password];
219
+ #2 Generating timestamp
220
+ timestamp = Time.now.utc.to_s
221
+ #3 Calculating Hash
222
+ hash = calculateHash(request_params)
223
+
224
+ #Add cipher stamp to the request
225
+ request_params[:password] = xmpp_private_key.private_encrypt(password+"#####"+timestamp+"#####"+hash)
226
+
227
+ #Replace previous params
228
+ params = request_params
229
+ else
230
+ #Non secure mode: send encrypted params in clear
231
+ if encrypted_params
232
+ encrypted_params.each do |key,value|
233
+ params[key] = value
234
+ end
235
+ end
236
+ end
237
+
238
+ response = RestClient.get url, :params => params
239
+ return response
240
+ end
241
+
242
+
243
+ def calculateHash(request_params)
244
+ unless request_params
245
+ request_params = {};
246
+ end
247
+
248
+ hash = "";
249
+ request_params.each do |key,value|
250
+ hash = hash + key.to_s + value.to_s
251
+ end
252
+ return Digest::MD5.hexdigest(hash)
253
+ end
254
+
255
+
256
+
257
+
258
+ def invokeApiCall(method,args)
259
+ length = args.length;
260
+ case length
261
+ when 0
262
+ return send(method)
263
+ when 1
264
+ return send(method,args[0])
265
+ when 2
266
+ return send(method,args[0],args[1])
267
+ when 3
268
+ return send(method,args[0],args[1],args[2])
269
+ when 4
270
+ return send(method,args[0],args[1],args[2],args[3])
271
+ else
272
+ return send(method,args)
273
+ end
274
+ end
275
+
276
+
277
+ #Main Program
278
+
279
+ begin
280
+
281
+ args = []
282
+ method = ARGV[0];
283
+ ARGV.each do |arg|
284
+ args.push(arg)
285
+ end
286
+ args.shift;
287
+
288
+ if (response = invokeApiCall(method, args) and response[1])
289
+ puts $script_title + ": #{response[0]} [OK]"
290
+ log( $script_title , "#{response[0]} [OK]" )
291
+ else
292
+ puts $script_title + ": #{response[0]} [FAIL]"
293
+ log( $script_title , "#{response[0]} [FAIL]" )
294
+ end
295
+
296
+ rescue => e
297
+ log($script_title,"#{e.class.name}: #{e.message}")
298
+ puts("#{e.class.name}: #{e.message}")
299
+ exit 1
300
+ end
301
+
@@ -1,9 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
- #Presence Script
2
+ #Rest Api Client Script
3
+ #Version: 13-12-2011
3
4
  #@author Aldo
4
5
 
6
+
5
7
  require 'logger'
6
8
  require 'rest_client'
9
+ require 'openssl'
10
+ require 'digest/md5'
11
+
7
12
 
8
13
  path = "/var/log/ejabberd/scripts.log"
9
14
  file = File.open(path, File::WRONLY | File::APPEND | File::CREAT)
@@ -25,35 +30,268 @@ def getOption(option)
25
30
  return "Undefined"
26
31
  end
27
32
 
28
- $url = "http://" + getOption("web_domain=") + "/xmpp/synchronizePresence"
33
+ #Constants
34
+ $secure_rest_api = getOption("secure_rest_api=")
29
35
  $pass = getOption("ejabberd_password=")
30
36
  $scripts_path = getOption("scripts_path=")
37
+ $script_title = "Synchronize Presence script"
38
+
39
+
40
+ def log(title,text)
41
+ $logger.info title + ": " + text
42
+ end
43
+
44
+ def getMethodName
45
+ caller[0] =~ /`(.*?)'/
46
+ $1
47
+ end
48
+
49
+
50
+ #####################
51
+ ##### Example #####
52
+ #####################
53
+ #def myHook(param1,param2)
54
+ # log(getMethodName,"(My message: #{param1},#{param2})")
55
+ # url = "http://" + getOption("web_domain=") + "/xmpp/hookRoute"
31
56
 
57
+ # params = {}
58
+ # encrypted_params = {}
59
+ # #Add params to sent in clear
60
+ # params[:param1_in_server]=param1
61
+ # #Add params to sent cipher
62
+ # encrypted_params[:param2_in_server]=param2
32
63
 
33
- def log(text)
34
- $logger.info "Synchronize Presence Script: " + text
64
+ # return [getMethodName,generic_api_call(url,params,encrypted_params)]
65
+ #end
66
+
67
+ def setConnection(username)
68
+ log($script_title,"#{getMethodName}(#{username})")
69
+ url = "http://" + getOption("web_domain=") + "/xmpp/setConnection"
70
+
71
+ params = {}
72
+ encrypted_params = {}
73
+ encrypted_params[:name]=username
74
+
75
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
35
76
  end
36
77
 
78
+
79
+ def unsetConnection(username)
80
+ log($script_title,"#{getMethodName}(#{username})")
81
+ url = "http://" + getOption("web_domain=") + "/xmpp/unsetConnection"
82
+
83
+ params = {}
84
+ encrypted_params = {}
85
+ encrypted_params[:name]=username
86
+
87
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
88
+ end
89
+
90
+
91
+ def setPresence(username,status)
92
+ log($script_title,"#{getMethodName}(#{username},#{status})")
93
+ url = "http://" + getOption("web_domain=") + "/xmpp/setPresence"
94
+
95
+ params = {}
96
+ encrypted_params = {}
97
+ encrypted_params[:name]=username
98
+ encrypted_params[:status]=status
99
+
100
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
101
+ end
102
+
103
+
104
+ def unsetPresence(username)
105
+ log($script_title,"#{getMethodName}(#{username})")
106
+ url = "http://" + getOption("web_domain=") + "/xmpp/unsetPresence"
107
+
108
+ params = {}
109
+ encrypted_params = {}
110
+ encrypted_params[:name]=username
111
+
112
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
113
+ end
114
+
115
+
116
+ def resetConnection()
117
+ log($script_title,"Call #{getMethodName}()")
118
+ url = "http://" + getOption("web_domain=") + "/xmpp/resetConnection"
119
+
120
+ params = {}
121
+ encrypted_params = {}
122
+
123
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
124
+ end
125
+
126
+
37
127
  def synchronize()
38
- log("Start Synchronize")
39
-
40
- users = []
41
- command = $scripts_path + "/emanagement getConnectedUsers"
42
- output = %x[#{command}]
43
- sessions = output.split("\n")
44
-
45
- sessions.each do |session|
46
- users << session.split("@")[0]
128
+ log($script_title,"Call #{getMethodName}()")
129
+ url = "http://" + getOption("web_domain=") + "/xmpp/synchronizePresence"
130
+
131
+
132
+ #Get connected users using Emanagement
133
+ users = []
134
+ command = $scripts_path + "/emanagement getConnectedUsers"
135
+ output = %x[#{command}]
136
+ sessions = output.split("\n")
137
+
138
+ sessions.each do |session|
139
+ users << session.split("@")[0]
140
+ end
141
+ #Finish
142
+
143
+
144
+ #In this cases users always will be sent in clear (not cipher)
145
+ #(Too much data to cipher)
146
+ #Anyway, we must to build the hash to pass the authentication
147
+ params = {}
148
+ encrypted_params = {}
149
+ params[:name]=users.join(",")
150
+
151
+ return [getMethodName,generic_api_call(url,params,encrypted_params)]
152
+ end
153
+
154
+
155
+
156
+ #Params must include the password and all the parameters to be sent in clear.
157
+ #Anyway, If "secure_rest_api" is disable, encrypted_params will be send in clear.
158
+ def generic_api_call(url,params,encrypted_params)
159
+
160
+ begin
161
+ unless params
162
+ params = {}
163
+ end
164
+
165
+ unless encrypted_params
166
+ encrypted_params = {}
167
+ end
168
+
169
+ params[:password]=$pass
170
+
171
+ response=sendHttpRequest(url,params,encrypted_params)
172
+ puts response.body
173
+
174
+ if response.body.include?("Ok")
175
+ return true
176
+ else
177
+ return false
178
+ end
179
+
180
+ rescue => e
181
+ log($script_title,"#{e.class.name}: #{e.message}")
182
+ puts("#{e.class.name}: #{e.message}")
183
+ return false
47
184
  end
185
+ end
186
+
187
+
188
+ #Send HTTP Request to Social Stream Presence API
189
+ def sendHttpRequest(url,params,encrypted_params)
190
+
191
+ unless params[:password]
192
+ return "params[:password] required in sendHttpRequest";
193
+ end
48
194
 
49
- RestClient.post($url, :name => users, :password => $pass)
50
- return true
51
195
 
52
- rescue RestClient::Exception
53
- log("RestClient::Exception")
196
+ if $secure_rest_api == "true"
197
+ xmpp_private_key_path = $scripts_path + "/rsa_keys/xmpp_rsa_key_private.pem";
198
+ web_public_key_path = $scripts_path + "/rsa_keys/web_rsa_key_public.pem";
199
+ xmpp_private_key = OpenSSL::PKey::RSA.new(File.read(xmpp_private_key_path))
200
+ web_public_key = OpenSSL::PKey::RSA.new(File.read(web_public_key_path))
201
+
202
+ request_params = {};
203
+
204
+ #Copy non encypted params
205
+ params.each do |key,value|
206
+ unless key.to_s == "password"
207
+ request_params[key] = value
208
+ end
209
+ end
210
+
211
+ #Include encrypted params
212
+ if encrypted_params and encrypted_params.empty? == false
213
+ request_params[:encrypted_params] = web_public_key.public_encrypt(encrypted_params.to_s)
214
+ end
215
+
216
+ #Generating stamp
217
+ #1 Get constant password
218
+ password = params[:password];
219
+ #2 Generating timestamp
220
+ timestamp = Time.now.utc.to_s
221
+ #3 Calculating Hash
222
+ hash = calculateHash(request_params)
223
+
224
+ #Add cipher stamp to the request
225
+ request_params[:password] = xmpp_private_key.private_encrypt(password+"#####"+timestamp+"#####"+hash)
226
+
227
+ #Replace previous params
228
+ params = request_params
229
+ else
230
+ #Non secure mode: send encrypted params in clear
231
+ if encrypted_params
232
+ encrypted_params.each do |key,value|
233
+ params[key] = value
234
+ end
235
+ end
236
+ end
237
+
238
+ response = RestClient.get url, :params => params
239
+ return response
54
240
  end
55
241
 
56
242
 
57
- synchronize()
243
+ def calculateHash(request_params)
244
+ unless request_params
245
+ request_params = {};
246
+ end
247
+
248
+ hash = "";
249
+ request_params.each do |key,value|
250
+ hash = hash + key.to_s + value.to_s
251
+ end
252
+ return Digest::MD5.hexdigest(hash)
253
+ end
254
+
255
+
256
+
58
257
 
258
+ def invokeApiCall(method,args)
259
+ length = args.length;
260
+ case length
261
+ when 0
262
+ return send(method)
263
+ when 1
264
+ return send(method,args[0])
265
+ when 2
266
+ return send(method,args[0],args[1])
267
+ when 3
268
+ return send(method,args[0],args[1],args[2])
269
+ when 4
270
+ return send(method,args[0],args[1],args[2],args[3])
271
+ else
272
+ return send(method,args)
273
+ end
274
+ end
275
+
276
+
277
+ #Main Program
278
+
279
+ begin
280
+
281
+ args = []
282
+ method = "synchronize"
283
+
284
+ if (response = invokeApiCall(method, args) and response[1])
285
+ puts $script_title + ": #{response[0]} [OK]"
286
+ log( $script_title , "#{response[0]} [OK]" )
287
+ else
288
+ puts $script_title + ": #{response[0]} [FAIL]"
289
+ log( $script_title , "#{response[0]} [FAIL]" )
290
+ end
291
+
292
+ rescue => e
293
+ log($script_title,"#{e.class.name}: #{e.message}")
294
+ puts("#{e.class.name}: #{e.message}")
295
+ exit 1
296
+ end
59
297