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 CHANGED
@@ -3,6 +3,8 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ *.swp
7
+ .rspec
6
8
  Gemfile.lock
7
9
  InstalledFiles
8
10
  _yardoc
data/README.md CHANGED
@@ -27,9 +27,7 @@ Or install it yourself as:
27
27
 
28
28
  ## TODO
29
29
 
30
- 1. write test.
31
- 2. refactoring code
32
- 3. write comment, and document.
30
+ 1. write comment, and document.
33
31
 
34
32
  ## Contributing
35
33
 
@@ -0,0 +1,24 @@
1
+ require 'openfire_admin/http_client'
2
+ require 'openfire_admin/response_exception'
3
+
4
+ module OpenfireAdmin
5
+ # pure admin console client
6
+ class AdminClient
7
+ def initialize(loginurl)
8
+ @http = HttpClient.new(URI.parse(loginurl))
9
+ end
10
+ def post(path, form_data, &proc); @http.post(path, form_data, &proc); end
11
+ def get(path, &proc); @http.get(path, &proc); end
12
+
13
+ # login
14
+ def login(username, pass)
15
+ post( "/login.jsp" , {
16
+ "login"=> "true",
17
+ "password"=>pass,
18
+ "url"=>"/index.jsp",
19
+ "username"=>username}) do |res|
20
+ raise ResponceException.new("can't login",res) unless res.code == "302"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+ require 'open-uri'
2
+ require 'openfire_admin/admin_client'
3
+ require 'openfire_admin/setup_wizard'
4
+ require 'openfire_admin/plugin'
5
+ require 'openfire_admin/property_map'
6
+
7
+ module OpenfireAdmin
8
+ def self.new(loginurl="http://localhost:9090")
9
+ Client.new(loginurl)
10
+ end
11
+
12
+ # HighLevel client
13
+ class Client
14
+ attr_reader :client
15
+ # constructor
16
+ def initialize(loginurl="http://localhost:9090")
17
+ @client = AdminClient.new(loginurl)
18
+ end
19
+
20
+ def setup_mode?
21
+ @client.get("/login.jsp") do |res|
22
+ res.code == "302" and res["location"] =~ %r"/setup/"
23
+ end
24
+ end
25
+
26
+ def setup_wizard
27
+ SetupWizard.new(@client)
28
+ end
29
+
30
+ def logined?
31
+ @logined
32
+ end
33
+
34
+ def login(username, password)
35
+ @client.login(username, password)
36
+ @logined = true
37
+ end
38
+
39
+ # get properties
40
+ def system_properties
41
+ PropertyMap.new(@client)
42
+ end
43
+
44
+ # return SystemCache array
45
+ def system_cache
46
+ @client.system_cache.map{|c| SystemCache.new( @client, c[:cacheID], c[:name] )}
47
+ end
48
+
49
+ # plugins list array of available plugins.
50
+ # if you need not installed plugins, ( self.available_plugins - self.install_plugin )
51
+ def available_plugins
52
+ PluginList.availables(@client)
53
+ end
54
+
55
+ # plugins list array of installed plugins
56
+ def installed_plugins
57
+ PluginList.installed(@client)
58
+ end
59
+ def users
60
+ UserAdmin.new(@client)
61
+ end
62
+ end
63
+ end
@@ -1,3 +1,4 @@
1
+ require 'net/http'
1
2
  module OpenfireAdmin
2
3
  # http client ( cookie support )
3
4
  class HttpClient
@@ -35,7 +36,7 @@ module OpenfireAdmin
35
36
  def post(path, form_data)
36
37
  req = Net::HTTP::Post.new(path)
37
38
  req.set_form_data(form_data)
38
- request(req){|res| yield res }
39
+ request(req){|res| yield res }
39
40
  end
40
41
 
41
42
  # get path
@@ -0,0 +1,132 @@
1
+ require 'nokogiri'
2
+ require 'openfire_admin/admin_client'
3
+
4
+ module OpenfireAdmin
5
+ class AdminClient
6
+ def get_installed_plugins
7
+ get("/plugin-admin.jsp") do |res|
8
+ doc =Nokogiri::HTML(res.body)
9
+ doc.at('h1').parent.at('table').search('tbody tr[valign=top]').map do |tr|
10
+ img = tr.at('a[href*="reloadplugin="]')
11
+ if img
12
+ {
13
+ :key => img[:href].match(/\?reloadplugin=([^"'&>]*)/)[1],
14
+ :name => tr.search('td')[1].content.gsub(NBSP,' ').strip,
15
+ :description => tr.search('td')[3].content.strip,
16
+ :version => tr.search('td')[4].content.strip
17
+ }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ def install_plugin(url)
23
+ post("/dwr/exec/downloader.installPlugin.dwr",
24
+ "callCount"=>"1",
25
+ "c0-scriptName"=>"downloader",
26
+ "c0-methodName"=>"installPlugin",
27
+ "c0-id"=>"0",
28
+ "c0-param0"=>"string:#{url}",
29
+ "c0-param1"=>"string:14867746",
30
+ "xml"=>"true" ) do |res|
31
+ raise ResponceException.new("plugin install fail",res) unless res.code == "200" and res.body =~ /s0\.successfull=/
32
+ end
33
+ end
34
+ def reload_plugin(key)
35
+ get("/plugin-admin.jsp?reloadplugin=#{key}") do |res|
36
+ raise ResponceException.new("cant reload",res) if res.code != "302" or res['location'] !~ /reloadsuccess=true/
37
+ end
38
+ end
39
+ def uninstall_plugin(key)
40
+ get("/plugin-admin.jsp?deleteplugin=#{key}") do |res|
41
+ raise ResponceException.new("cant delete",res) if res.code != "302" or res['location'] !~ /deletesuccess=true/
42
+ end
43
+ end
44
+ end
45
+ # plugin abstract class
46
+ class Plugin
47
+ attr_accessor :name, :description, :version
48
+ attr_reader :key
49
+ def initialize(client, key)
50
+ @client = client
51
+ @key = key.downcase
52
+ end
53
+ def inspect
54
+ to_s
55
+ end
56
+ def to_s
57
+ "#<#{self.class} #{key} #{version} (#{name.inspect} #{description.inspect})>"
58
+ end
59
+ def eql?(obj)
60
+ case obj
61
+ when Plugin
62
+ self.key == obj.key
63
+ when String
64
+ self.key == obj.downcase
65
+ else
66
+ false
67
+ end
68
+ end
69
+ end
70
+
71
+ # installed plugin. this instance can uninstall and reload
72
+ class InstalledPlugin < Plugin
73
+ # reload plugin
74
+ def reload
75
+ @client.reload_plugin(key)
76
+ end
77
+
78
+ # uninstall plugin
79
+ def uninstall
80
+ @client.uninstall_plugin(key)
81
+ end
82
+ end
83
+
84
+ # available plugin. this can install
85
+ class AvailablePlugin < Plugin
86
+ attr_accessor :url
87
+
88
+ # install plugin
89
+ def install
90
+ @client.install_plugin(url)
91
+ end
92
+
93
+ end
94
+
95
+ # plugin list array. this can find plugin by key.
96
+ class PluginList < Array
97
+ def [](name)
98
+ if name.is_a?(String)
99
+ self.find{|p| p.eql? name }
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ def self.installed(client)
106
+ ret = PluginList.new
107
+ client.get_installed_plugins.each{|p|
108
+ r = InstalledPlugin.new(client, p[:key])
109
+ r.name = p[:name]
110
+ r.description = p[:description]
111
+ r.version = p[:version]
112
+ ret << r
113
+ }
114
+ ret
115
+ end
116
+ PLUGIN_LIST_URL= "http://www.igniterealtime.org/projects/openfire/versions.jsp"
117
+ def self.availables(client, xml=nil)
118
+ xml = open(PLUGIN_LIST_URL).read unless xml
119
+ ret = PluginList.new
120
+ doc = Nokogiri::XML(xml)
121
+ doc.search('plugin').each do |tr|
122
+ p = AvailablePlugin.new(client, tr[:url].match(/\/([^\.\/]+)\.[^\/.]+$/)[1])
123
+ p.name = tr[:name]
124
+ p.description = tr[:description]
125
+ p.version = tr[:latest]
126
+ p.url = tr[:url]
127
+ ret << p
128
+ end
129
+ ret
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,76 @@
1
+ require 'nokogiri'
2
+ require 'openfire_admin/admin_client'
3
+
4
+ module OpenfireAdmin
5
+ class AdminClient
6
+ def remove_property(name)
7
+ post('/server-properties.jsp', 'propName' => name, 'del'=>'true') do |res|
8
+ raise ResponceException.new("cant save",res) if res.code != "302" or res['location'] !~ /deletesuccess=true$/
9
+ end
10
+ end
11
+ def set_property(name, value)
12
+ post('/server-properties.jsp', 'propName' => name, 'propValue'=>value.to_s, 'save'=>'Save Property') do |res|
13
+ raise ResponceException.new("cant save",res) if res.code != "302"
14
+ end
15
+ end
16
+ def get_property(name)
17
+ post("/server-properties.jsp", "edit"=>"true", "propName"=>name) do |res|
18
+ ta = Nokogiri::HTML(res.body).at('textarea[name=propValue]')
19
+ raise ResponceException.new("not found textarea",res) unless ta
20
+ ta.content.to_s
21
+ end
22
+ end
23
+ def get_properties
24
+ ret = {}
25
+ get("/server-properties.jsp") do |res|
26
+ raise ResponceException.new("can't read",res) unless res.code== "200"
27
+ doc = Nokogiri::HTML(res.body)
28
+ doc.search('//h1/parent::node()//table/tbody/tr[@class=""]').each do |tr|
29
+ v = tr.at('td[2] span')[:title]
30
+ v = "" if v == NBSP
31
+ ret[tr.at('td span')[:title]]= v
32
+ end
33
+ end
34
+ ret
35
+ end
36
+ end
37
+ # System property map
38
+ class PropertyMap
39
+ def initialize(client)
40
+ @client = client
41
+ reload
42
+ end
43
+
44
+ def inspect
45
+ @cache.inspect
46
+ end
47
+
48
+ # get system property
49
+ def []( name )
50
+ v = @cache[name]
51
+ v = @client.get_property(name) if v.nil? and @cache.has_key?(name)
52
+ v
53
+ end
54
+
55
+ # reload cache
56
+ def reload
57
+ @cache = @client.get_properties
58
+ self
59
+ end
60
+
61
+ # remove property
62
+ def remove(name)
63
+ @client.remove_property(name)
64
+ end
65
+
66
+ # set/add property
67
+ def []=(name,value)
68
+ if value.nil?
69
+ remove(name)
70
+ else
71
+ @client.set_property(name, value)
72
+ @cache[name]=value
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,3 +1,5 @@
1
+ require 'nokogiri'
2
+ require 'net/http'
1
3
  # openfire admin operator
2
4
  module OpenfireAdmin
3
5
  # unexpected response found exception
@@ -0,0 +1,39 @@
1
+ require 'nokogiri'
2
+ require 'openfire_admin/admin_client.rb'
3
+
4
+ module OpenfireAdmin
5
+ # extend for system cache
6
+ class AdminClient
7
+ def system_cache
8
+ get("/system-cache.jsp") do |res|
9
+ Nokogiri::HTML(res.body).search('input[type=checkbox][name=cacheID]').map{|i|
10
+ {
11
+ :cacheID => i[:value],
12
+ :name => i.ancestors("tr").first.search("td td")[1].content.strip
13
+ }
14
+ }
15
+ end
16
+ end
17
+ def system_cache_clear(cacheID)
18
+ post("/system-cache.jsp","cacheID"=>cacheID, "clear"=>"Clear") do |res|
19
+ ! Nokogiri::HTML(res.body).at("div[class='jive-success']").nil?
20
+ end
21
+ end
22
+ end
23
+ # cache control. this instance can clear cache.
24
+ class SystemCache
25
+ attr_reader :cacheID, :name
26
+ def initialize(client, cacheID, name)
27
+ @client = client
28
+ @cacheID = cacheID
29
+ @name = name
30
+ end
31
+ def to_s
32
+ "#<#{self.class} (#{@cacheID})#{name.inspect}>"
33
+ end
34
+ # clear cache
35
+ def clear
36
+ @client.system_cache_clear( @cacheID )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ require 'openfire_admin/http_client.rb'
2
+
3
+ module OpenfireAdmin
4
+ class UserAdmin
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+ def create(username,password,name,email,isadmin=false)
9
+ params = {
10
+ "create"=>"Create User",
11
+ "email"=> email,
12
+ "name"=> name,
13
+ "password"=> password,
14
+ "passwordConfirm"=>password,
15
+ "username"=>username }
16
+ params['isadmin'] = "on" if isadmin
17
+ @client.post("/user-create.jsp", params ) do |res|
18
+ raise ResponceException.new("can't create user #{username}",res ) unless res.code == "302" and res["location"] =~ /success=true/
19
+ end
20
+ end
21
+ def delete(username)
22
+ @client.get("/user-delete.jsp?username=#{username}&delete=Delete+User") do |res|
23
+ raise ResponceException.new("can't delete user #{username}",res ) unless res.code == "302" and res["location"] =~ /deletesuccess=true/
24
+ end
25
+ end
26
+ def exists?(username)
27
+ @client.get("/user-password.jsp?username=#{username}") do |res|
28
+ res.code == "200"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module OpenfireAdmin
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end