Class: Rex::Parser::Ini

Inherits:
Hash
  • Object
show all
Defined in:
lib/rex/parser/ini.rb

Overview

This class parses the contents of an INI file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Ini

Initializes an ini instance and tries to read in the groups from the file if it exists.


41
42
43
44
45
46
47
48
49
50
# File 'lib/rex/parser/ini.rb', line 41

def initialize(path = nil)
  self.path = path

  # Try to synchronize ourself with the file if we
  # have one
  begin
    self.from_file if (self.path)
  rescue
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path


123
124
125
# File 'lib/rex/parser/ini.rb', line 123

def path
  @path
end

Class Method Details

.from_file(path) ⇒ Object

Creates a new class instance and reads in the contents of the supplied file path.


22
23
24
25
26
# File 'lib/rex/parser/ini.rb', line 22

def self.from_file(path)
  ini = Ini.new(path)
  ini.from_file
  return ini
end

.from_s(str) ⇒ Object

Creates a new class instance from the supplied string.


31
32
33
34
35
# File 'lib/rex/parser/ini.rb', line 31

def self.from_s(str)
  ini = Ini.new
  ini.from_s(str)
  return ini
end

Instance Method Details

#add_group(name = 'global', reset = true) ⇒ Object

Adds a group of the supplied name if it doesn't already exist.


57
58
59
60
61
62
# File 'lib/rex/parser/ini.rb', line 57

def add_group(name = 'global', reset = true)
  self[name] = {} if (reset == true)
  self[name] = {} if (!self[name])

  return self[name]
end

#from_file(fpath = nil) ⇒ Object

Reads in the groups from the supplied file path or the instance's file path.


81
82
83
84
85
# File 'lib/rex/parser/ini.rb', line 81

def from_file(fpath = nil)
  fpath = path if (!fpath)

  read_groups(fpath)
end

#from_s(str) ⇒ Object

Reads in the groups from the supplied string.


90
91
92
# File 'lib/rex/parser/ini.rb', line 90

def from_s(str)
  read_groups_string(str.split("\n"))
end

#group?(name) ⇒ Boolean

Checks to see if name is a valid group.

Returns:

  • (Boolean)

67
68
69
# File 'lib/rex/parser/ini.rb', line 67

def group?(name)
  return (self[name] != nil)
end

#read_groups(fpath) ⇒ Object (protected)

Reads in the groups and their attributes from the supplied file path or from the instance's file path if one was set.


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rex/parser/ini.rb', line 131

def read_groups(fpath) # :nodoc:
  if (!fpath)
    raise ArgumentError, "No file path specified.",
      caller
  end

  # Read in the contents of the file
  lines = ::IO.readlines(fpath)

  # Now read the contents from the supplied string
  read_groups_string(lines)
end

#read_groups_string(str) ⇒ Object (protected)

Reads groups from the supplied string


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
# File 'lib/rex/parser/ini.rb', line 147

def read_groups_string(str) # :nodoc:
  # Reset the groups hash
  self.clear

  # The active group
  active_group = nil

  # Walk each line initializing the groups
  str.each { |line|
    next if (line.match(/^;/))

    # Eliminate cr/lf
    line.gsub!(/(\n|\r)/, '')

    # Is it a group [bob]?
    if (md = line.match(/^\[(.+?)\]/))
      active_group = md[1]
      self[md[1]]  = {}
    # Is it a VAR=VAL?
    elsif (md = line.match(/^(.+?)=(.*)$/))
      if (active_group)
        var, val = md[1], md[2]

        # don't clobber datastore nils with ""
        unless val.empty?
          self[active_group][var] = val
        end
      end
    end
  }
end

#to_file(tpath = nil) ⇒ Object

Writes the group settings to a file.


97
98
99
100
101
102
103
# File 'lib/rex/parser/ini.rb', line 97

def to_file(tpath = nil)
  tpath = path if (!tpath)

  f = File.new(tpath, "w")
  f.write(to_s)
  f.close
end

#to_sObject

Converts the groups to a string.


108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rex/parser/ini.rb', line 108

def to_s
  str = ''
  keys.sort.each { |k|
    str << "[#{k}]\n"

    self[k].each_pair { |var, val|
      str << "#{var}=#{val}\n"
    }

    str << "\n";
  }

  return str
end