openfire_admin 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/README.md +1 -3
- data/lib/openfire_admin/admin_client.rb +24 -0
- data/lib/openfire_admin/client.rb +63 -0
- data/lib/openfire_admin/http_client.rb +2 -1
- data/lib/openfire_admin/plugin.rb +132 -0
- data/lib/openfire_admin/property_map.rb +76 -0
- data/lib/openfire_admin/response_exception.rb +2 -0
- data/lib/openfire_admin/system_cache.rb +39 -0
- data/lib/openfire_admin/user_admin.rb +32 -0
- data/lib/openfire_admin/version.rb +1 -1
- data/lib/openfire_admin.rb +4 -308
- data/openfire_admin.gemspec +3 -0
- data/spec/fixtures/plugin-admin.jsp +584 -0
- data/spec/fixtures/server-properties.jsp +483 -0
- data/spec/fixtures/server-properties_password.jsp +640 -0
- data/spec/fixtures/system-cache.jsp +1315 -0
- data/spec/fixtures/system-cache_success.jsp +1327 -0
- data/spec/fixtures/versions.xml +32 -0
- data/spec/openfire_admin_spec.rb +198 -0
- data/spec/spec_helper.rb +91 -0
- metadata +46 -6
data/lib/openfire_admin.rb
CHANGED
@@ -1,315 +1,11 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
require 'net/http'
|
3
|
-
require "nokogiri"
|
4
|
-
require 'open-uri'
|
5
2
|
require 'openfire_admin/http_client'
|
6
3
|
require 'openfire_admin/response_exception'
|
4
|
+
require 'openfire_admin/system_cache'
|
5
|
+
require 'openfire_admin/user_admin'
|
6
|
+
require 'openfire_admin/client'
|
7
7
|
|
8
8
|
# openfire admin operator
|
9
|
-
|
9
|
+
module OpenfireAdmin
|
10
10
|
NBSP = "\302\240" # Nokogiri::HTML(" ").text
|
11
|
-
attr_reader :client
|
12
|
-
|
13
|
-
# pure admin console client
|
14
|
-
class AdminClient
|
15
|
-
attr_reader :http
|
16
|
-
def initialize(loginurl)
|
17
|
-
@http = HttpClient.new(URI.parse(loginurl))
|
18
|
-
end
|
19
|
-
def login(username, pass)
|
20
|
-
@http.post( "/login.jsp" , {
|
21
|
-
"login"=> "true",
|
22
|
-
"password"=>pass,
|
23
|
-
"url"=>"/index.jsp",
|
24
|
-
"username"=>username}) do |res|
|
25
|
-
raise ResponceException.new("can't login",res) unless res.code == "302"
|
26
|
-
end
|
27
|
-
end
|
28
|
-
def remove_property(name)
|
29
|
-
@http.post('/server-properties.jsp', 'propName' => name, 'del'=>'true') do |res|
|
30
|
-
raise ResponceException.new("cant save",res) if res.code != "302" or res['location'] !~ /deletesuccess=true$/
|
31
|
-
end
|
32
|
-
end
|
33
|
-
def set_property(name, value)
|
34
|
-
@http.post('/server-properties.jsp', 'propName' => name, 'propValue'=>value.to_s, 'save'=>'Save Property') do |res|
|
35
|
-
raise ResponceException.new("cant save",res) if res.code != "302"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
def get_property(name)
|
39
|
-
@http.post("/server-properties.jsp", "edit"=>"true", "propName"=>name) do |res|
|
40
|
-
ta = Nokogiri::HTML(res.body).at('textarea[name=propValue]')
|
41
|
-
raise ResponceException.new("not found textarea",res) unless ta
|
42
|
-
ta.content.to_s
|
43
|
-
end
|
44
|
-
end
|
45
|
-
def get_properties
|
46
|
-
ret = {}
|
47
|
-
@http.get("/server-properties.jsp") do |res|
|
48
|
-
raise ResponceException.new("can't read",res) unless res.code== "200"
|
49
|
-
doc = Nokogiri::HTML(res.body)
|
50
|
-
doc.search('//h1/parent::node()//table/tbody/tr[@class=""]').each do |tr|
|
51
|
-
v = tr.at('td[2] span')[:title]
|
52
|
-
v = "" if v == NBSP
|
53
|
-
ret[tr.at('td span')[:title]]= v
|
54
|
-
end
|
55
|
-
end
|
56
|
-
ret
|
57
|
-
end
|
58
|
-
def get_installed_plugins
|
59
|
-
@http.get("/plugin-admin.jsp") do |res|
|
60
|
-
doc =Nokogiri::HTML(res.body)
|
61
|
-
doc.at('h1').parent.at('table').search('tbody tr[valign=top]').map do |tr|
|
62
|
-
img = tr.at('a[href*="reloadplugin="]')
|
63
|
-
if img
|
64
|
-
{
|
65
|
-
:key => img[:href].match(/\?reloadplugin=([^"'&>]*)/)[1],
|
66
|
-
:name => tr.search('td')[1].content.gsub(NBSP,' ').strip,
|
67
|
-
:description => tr.search('td')[3].content.strip,
|
68
|
-
:version => tr.search('td')[4].content.strip
|
69
|
-
}
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
def create_user(username,password,name,email,isadmin=false)
|
75
|
-
params = {
|
76
|
-
"create"=>"Create User",
|
77
|
-
"email"=> email,
|
78
|
-
"name"=> name,
|
79
|
-
"password"=> password,
|
80
|
-
"passwordConfirm"=>password,
|
81
|
-
"username"=>username }
|
82
|
-
params['isadmin'] = "on" if isadmin
|
83
|
-
@http.post("/user-create.jsp", params ) do |res|
|
84
|
-
raise ResponceException.new("can't create user #{username}",res ) unless res.code == "302" and res["location"] =~ /success=true/
|
85
|
-
end
|
86
|
-
end
|
87
|
-
def delete_user(username)
|
88
|
-
@http.get("/user-delete.jsp?username=#{username}&delete=Delete+User") do |res|
|
89
|
-
raise ResponceException.new("can't delete user #{username}",res ) unless res.code == "302" and res["location"] =~ /deletesuccess=true/
|
90
|
-
end
|
91
|
-
end
|
92
|
-
def user_exists?(username)
|
93
|
-
@http.get("/user-password.jsp?username=#{username}") do |res|
|
94
|
-
res.code == "200"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
def install_plugin(url)
|
98
|
-
@http.post("/dwr/exec/downloader.installPlugin.dwr",
|
99
|
-
"callCount"=>"1",
|
100
|
-
"c0-scriptName"=>"downloader",
|
101
|
-
"c0-methodName"=>"installPlugin",
|
102
|
-
"c0-id"=>"0",
|
103
|
-
"c0-param0"=>"string:#{url}",
|
104
|
-
"c0-param1"=>"string:14867746",
|
105
|
-
"xml"=>"true" ) do |res|
|
106
|
-
raise ResponceException.new("plugin install fail",res) unless res.code == "200" and res.body =~ /s0\.successfull=/
|
107
|
-
end
|
108
|
-
end
|
109
|
-
def reload_plugin(key)
|
110
|
-
@http.get("/plugin-admin.jsp?reloadplugin=#{key}") do |res|
|
111
|
-
raise ResponceException.new("cant reload",res) if res.code != "302" or res['location'] !~ /reloadsuccess=true/
|
112
|
-
end
|
113
|
-
end
|
114
|
-
def uninstall_plugin(key)
|
115
|
-
@http.get("/plugin-admin.jsp?deleteplugin=#{key}") do |res|
|
116
|
-
raise ResponceException.new("cant delete",res) if res.code != "302" or res['location'] !~ /deletesuccess=true/
|
117
|
-
end
|
118
|
-
end
|
119
|
-
def system_cache
|
120
|
-
@http.get("/system-cache.jsp") do |res|
|
121
|
-
Nokogiri::HTML(res.body).search('input[type=checkbox][name=cacheID]').map{|i|
|
122
|
-
{
|
123
|
-
:cacheID => i[:value],
|
124
|
-
:name => i.ancestors("tr").first.search("td td")[1].content.strip
|
125
|
-
}
|
126
|
-
}
|
127
|
-
end
|
128
|
-
end
|
129
|
-
def system_cache_clear(cacheID)
|
130
|
-
@http.post("/system-cache.jsp","cacheID"=>cacheID, "clear"=>"Clear") do |res|
|
131
|
-
! Nokogiri::HTML(res.body).at("div[class='jive-success']").nil?
|
132
|
-
end
|
133
|
-
end
|
134
|
-
def setup_mode?
|
135
|
-
@http.get("/login.jsp") do |res|
|
136
|
-
res.code == "302" and res["location"] =~ %r"/setup/"
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
def setup_mode?
|
141
|
-
@client.setup_mode?
|
142
|
-
end
|
143
|
-
def setup_wizard
|
144
|
-
require File.join(File.dirname(__FILE__),"openfire_admin/setup_wizard")
|
145
|
-
SetupWizard.new(@client.http)
|
146
|
-
end
|
147
|
-
|
148
|
-
def initialize(loginurl="http://localhost:9090")
|
149
|
-
@client = AdminClient.new(loginurl)
|
150
|
-
end
|
151
|
-
def logined?
|
152
|
-
@logined
|
153
|
-
end
|
154
|
-
|
155
|
-
def login(username, password)
|
156
|
-
@client.login(username, password)
|
157
|
-
@logined = true
|
158
|
-
end
|
159
|
-
|
160
|
-
# System property map
|
161
|
-
class PropertyMap
|
162
|
-
def initialize(client)
|
163
|
-
@client = client
|
164
|
-
reload
|
165
|
-
end
|
166
|
-
|
167
|
-
def inspect
|
168
|
-
@cache.inspect
|
169
|
-
end
|
170
|
-
|
171
|
-
# get system property
|
172
|
-
def []( name )
|
173
|
-
v = @cache[name]
|
174
|
-
v = @client.get_property(name) if v.nil? and @cache.has_key?(name)
|
175
|
-
v
|
176
|
-
end
|
177
|
-
|
178
|
-
# reload cache
|
179
|
-
def reload
|
180
|
-
@cache = @client.get_properties
|
181
|
-
self
|
182
|
-
end
|
183
|
-
|
184
|
-
# remove property
|
185
|
-
def remove(name)
|
186
|
-
@client.remove_property(name)
|
187
|
-
end
|
188
|
-
|
189
|
-
# set/add property
|
190
|
-
def []=(name,value)
|
191
|
-
if value.nil?
|
192
|
-
remove(name)
|
193
|
-
else
|
194
|
-
@client.set_property(name, value)
|
195
|
-
@cache[name]=value
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
# get properties
|
201
|
-
def system_properties
|
202
|
-
PropertyMap.new(@client)
|
203
|
-
end
|
204
|
-
|
205
|
-
# cache control. this instance can clear cache.
|
206
|
-
class SystemCache
|
207
|
-
attr_reader :cacheID, :name
|
208
|
-
def initialize(client, cacheID, name)
|
209
|
-
@client = client
|
210
|
-
@cacheID = cacheID
|
211
|
-
@name = name
|
212
|
-
end
|
213
|
-
def to_s
|
214
|
-
"#<#{self.class} (#{@cacheID})#{name.inspect}>"
|
215
|
-
end
|
216
|
-
# clear cache
|
217
|
-
def clear
|
218
|
-
@client.system_cache_clear( @cacheID )
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
# return SystemCache array
|
223
|
-
def system_cache
|
224
|
-
@client.system_cache.map{|c| SystemCache.new( @client, c[:cacheID], c[:name] )}
|
225
|
-
end
|
226
|
-
|
227
|
-
# plugin abstract class
|
228
|
-
class Plugin
|
229
|
-
attr_accessor :name, :description, :version
|
230
|
-
attr_reader :key
|
231
|
-
def initialize(client, key)
|
232
|
-
@client = client
|
233
|
-
@key = key.downcase
|
234
|
-
end
|
235
|
-
def inspect
|
236
|
-
to_s
|
237
|
-
end
|
238
|
-
def to_s
|
239
|
-
"#<#{self.class} #{key} #{version} (#{name.inspect} #{description.inspect})>"
|
240
|
-
end
|
241
|
-
def eql?(obj)
|
242
|
-
case obj
|
243
|
-
when Plugin
|
244
|
-
self.key == obj.key
|
245
|
-
when String
|
246
|
-
self.key == obj.downcase
|
247
|
-
else
|
248
|
-
false
|
249
|
-
end
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
# installed plugin. this instance can uninstall and reload
|
254
|
-
class InstalledPlugin < Plugin
|
255
|
-
# reload plugin
|
256
|
-
def reload
|
257
|
-
@client.reload_plugin(key)
|
258
|
-
end
|
259
|
-
|
260
|
-
# uninstall plugin
|
261
|
-
def uninstall
|
262
|
-
@client.uninstall_plugin(key)
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
# available plugin. this can install
|
267
|
-
class AvailablePlugin < Plugin
|
268
|
-
attr_accessor :url
|
269
|
-
|
270
|
-
# install plugin
|
271
|
-
def install
|
272
|
-
@client.install_plugin(url)
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
# plugin list array. this can find plugin by key.
|
277
|
-
class PluginList < Array
|
278
|
-
def [](name)
|
279
|
-
if name.is_a?(String)
|
280
|
-
self.find{|p| p.eql? name }
|
281
|
-
else
|
282
|
-
super
|
283
|
-
end
|
284
|
-
end
|
285
|
-
end
|
286
|
-
|
287
|
-
# plugins list array of available plugins.
|
288
|
-
# if you need not installed plugins, ( self.available_plugins - self.install_plugin )
|
289
|
-
def available_plugins
|
290
|
-
ret = PluginList.new
|
291
|
-
doc = Nokogiri::XML(open("http://www.igniterealtime.org/projects/openfire/versions.jsp").read)
|
292
|
-
doc.search('plugin').each do |tr|
|
293
|
-
p = AvailablePlugin.new(@client, tr[:url].match(/\/([^\.\/]+)\.[^\/.]+$/)[1])
|
294
|
-
p.name = tr[:name]
|
295
|
-
p.description = tr[:description]
|
296
|
-
p.version = tr[:latest]
|
297
|
-
p.url = tr[:url]
|
298
|
-
ret << p
|
299
|
-
end
|
300
|
-
ret
|
301
|
-
end
|
302
|
-
|
303
|
-
# plugins list array of installed plugins
|
304
|
-
def installed_plugins
|
305
|
-
ret = PluginList.new
|
306
|
-
@client.get_installed_plugins.each{|p|
|
307
|
-
r = InstalledPlugin.new(@client, p[:key])
|
308
|
-
r.name = p[:name]
|
309
|
-
r.description = p[:description]
|
310
|
-
r.version = p[:version]
|
311
|
-
ret << r
|
312
|
-
}
|
313
|
-
ret
|
314
|
-
end
|
315
11
|
end
|
data/openfire_admin.gemspec
CHANGED