Files
metasploit-gs/lib/rex/parser/ini.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
3.1 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
module Rex
module Parser
###
#
# This class parses the contents of an INI file.
#
###
class Ini < Hash
##
#
# Factories
#
##
2013-08-30 16:28:33 -05:00
#
# Creates a new class instance and reads in the contents of the supplied
# file path.
#
def self.from_file(path)
ini = Ini.new(path)
ini.from_file
return ini
end
2013-08-30 16:28:33 -05:00
#
# Creates a new class instance from the supplied string.
#
def self.from_s(str)
ini = Ini.new
ini.from_s(str)
return ini
end
2013-08-30 16:28:33 -05:00
#
# Initializes an ini instance and tries to read in the groups from the
# file if it exists.
#
def initialize(path = nil)
self.path = path
2013-08-30 16:28:33 -05:00
# Try to synchronize ourself with the file if we
# have one
begin
self.from_file if (self.path)
rescue
end
end
2013-08-30 16:28:33 -05:00
2015-11-20 15:17:15 -08:00
alias each_group each_key
2013-08-30 16:28:33 -05:00
#
# Adds a group of the supplied name if it doesn't already exist.
#
def add_group(name = 'global', reset = true)
self[name] = {} if (reset == true)
self[name] = {} if (!self[name])
2013-08-30 16:28:33 -05:00
return self[name]
end
2013-08-30 16:28:33 -05:00
#
# Checks to see if name is a valid group.
#
def group?(name)
return (self[name] != nil)
end
2013-08-30 16:28:33 -05:00
##
#
# Serializers
#
##
2013-08-30 16:28:33 -05:00
#
# Reads in the groups from the supplied file path or the instance's file
# path.
#
def from_file(fpath = nil)
fpath = path if (!fpath)
2013-08-30 16:28:33 -05:00
read_groups(fpath)
end
2013-08-30 16:28:33 -05:00
#
# Reads in the groups from the supplied string.
#
def from_s(str)
read_groups_string(str.split("\n"))
end
2013-08-30 16:28:33 -05:00
#
# Writes the group settings to a file.
#
def to_file(tpath = nil)
tpath = path if (!tpath)
2013-08-30 16:28:33 -05:00
f = File.new(tpath, "w")
f.write(to_s)
f.close
end
2013-08-30 16:28:33 -05:00
#
# Converts the groups to a string.
#
def to_s
str = ''
keys.sort.each { |k|
str << "[#{k}]\n"
2013-08-30 16:28:33 -05:00
self[k].each_pair { |var, val|
str << "#{var}=#{val}\n"
}
2013-08-30 16:28:33 -05:00
str << "\n";
}
2013-08-30 16:28:33 -05:00
return str
end
2013-08-30 16:28:33 -05:00
attr_reader :path
protected
#
# Reads in the groups and their attributes from the supplied file
# path or from the instance's file path if one was set.
#
2005-11-02 14:18:50 +00:00
def read_groups(fpath) # :nodoc:
if (!fpath)
raise ArgumentError, "No file path specified.",
caller
end
2013-08-30 16:28:33 -05:00
# Read in the contents of the file
lines = ::IO.readlines(fpath)
2013-08-30 16:28:33 -05:00
# Now read the contents from the supplied string
read_groups_string(lines)
end
2013-08-30 16:28:33 -05:00
#
# Reads groups from the supplied string
#
2005-11-02 14:18:50 +00:00
def read_groups_string(str) # :nodoc:
# Reset the groups hash
self.clear
2013-08-30 16:28:33 -05:00
# The active group
active_group = nil
2013-08-30 16:28:33 -05:00
# Walk each line initializing the groups
str.each { |line|
next if (line.match(/^;/))
2013-08-30 16:28:33 -05:00
# Eliminate cr/lf
line.gsub!(/(\n|\r)/, '')
2013-08-30 16:28:33 -05:00
# Is it a group [bob]?
if (md = line.match(/^\[(.+?)\]/))
2013-03-07 18:16:57 -06:00
active_group = md[1]
self[md[1]] = {}
# Is it a VAR=VAL?
elsif (md = line.match(/^(.+?)=(.*)$/))
if (active_group)
2016-07-23 00:26:18 -04:00
var, val = md[1], md[2]
# don't clobber datastore nils with ""
unless val.empty?
self[active_group][var] = val
end
end
end
}
end
2013-08-30 16:28:33 -05:00
2005-11-15 05:22:13 +00:00
attr_writer :path # :nodoc:
end
end
end