Module: Wpxf::Options

Included in:
Module, Payload
Defined in:
lib/wpxf/core/options.rb

Overview

A mixin to provide option registering and datastore functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datastoreHash

Returns a hash containing the option values specified by the user.

Returns:

  • (Hash)

    a hash containing the option values specified by the user.



173
174
175
# File 'lib/wpxf/core/options.rb', line 173

def datastore
  @datastore
end

#optionsArray

Returns an array of Wpxf::Option objects used to configure the current module.

Returns:

  • (Array)

    an array of Wpxf::Option objects used to configure the current module.



170
171
172
# File 'lib/wpxf/core/options.rb', line 170

def options
  @options
end

Instance Method Details

#all_options_valid?Boolean

Returns true if all the required options are set.

Returns:

  • (Boolean)

    true if all the required options are set.



142
143
144
145
146
147
148
# File 'lib/wpxf/core/options.rb', line 142

def all_options_valid?
  options.each do |opt|
    return false unless opt.valid?(datastore[opt.name])
  end

  true
end

#get_option(name) ⇒ Option?

Find and return an Wpxf::Option by its registered name.

Parameters:

Returns:

  • (Option, nil)

    the matching option or nil if not found.



73
74
75
# File 'lib/wpxf/core/options.rb', line 73

def get_option(name)
  options.find { |o| o.name.eql?(name) }
end

#get_option_value(name) ⇒ Object

Get the value of a module option.

Parameters:

  • name

    the name of the option.

Returns:

  • the option value.



94
95
96
# File 'lib/wpxf/core/options.rb', line 94

def get_option_value(name)
  datastore[name]
end

#initializeObject

Initialize a new instance of Wpxf::Options.



15
16
17
18
19
20
# File 'lib/wpxf/core/options.rb', line 15

def initialize
  super

  self.options = []
  self.datastore = {}
end

#missing_optionsArray

Returns an array of missing option names that are required.

Returns:

  • (Array)

    an array of missing option names that are required.



160
161
162
163
164
165
166
# File 'lib/wpxf/core/options.rb', line 160

def missing_options
  opts = []
  options.each do |opt|
    opts.push(opt.name) unless !opt.required? || option_valid?(opt)
  end
  opts
end

#normalized_option_value(name) ⇒ Object

Get the normalized value of a module option.

Parameters:

  • name

    the name of the option.

Returns:

  • the option value.



101
102
103
104
# File 'lib/wpxf/core/options.rb', line 101

def normalized_option_value(name)
  option = get_option(name)
  return option.normalize(datastore[name]) unless option.nil?
end

#option_valid?(opt) ⇒ Boolean

Check if an option is valid.

Parameters:

Returns:

  • (Boolean)

    true if valid.



153
154
155
156
157
# File 'lib/wpxf/core/options.rb', line 153

def option_valid?(opt)
  return opt.valid?(datastore[opt.name]) if opt.is_a? Option

  get_option(opt).valid?(datastore[opt])
end

#option_value?(name) ⇒ Boolean

Returns true if the specified option has a value.

Parameters:

  • name

    the name of the option.

Returns:

  • (Boolean)

    true if the specified option has a value.



108
109
110
# File 'lib/wpxf/core/options.rb', line 108

def option_value?(name)
  !datastore[name].nil? && !datastore[name].empty?
end

#register_advanced_options(opts) ⇒ Void

Register an array of advanced Wpxf::Option.

Parameters:

Returns:

  • (Void)

    nothing.



51
52
53
54
55
56
57
# File 'lib/wpxf/core/options.rb', line 51

def register_advanced_options(opts)
  opts.each do |opt|
    opt.advanced = true
  end

  register_options(opts)
end

#register_evasion_options(opts) ⇒ Void

Register an array of evasion Wpxf::Option.

Parameters:

Returns:

  • (Void)

    nothing.



62
63
64
65
66
67
68
# File 'lib/wpxf/core/options.rb', line 62

def register_evasion_options(opts)
  opts.each do |opt|
    opt.evasion = true
  end

  register_options(opts)
end

#register_option(opt) ⇒ Void

Register an Wpxf::Option.

Parameters:

Returns:

  • (Void)

    nothing.



32
33
34
35
36
37
# File 'lib/wpxf/core/options.rb', line 32

def register_option(opt)
  raise 'payload is a reserved name' if opt.name.eql? 'payload'
  unregister_option(opt)
  options.push(opt)
  datastore[opt.name] = opt.default unless opt.default.nil?
end

#register_options(opts) ⇒ Void

Register an array of Wpxf::Option.

Parameters:

Returns:

  • (Void)

    nothing.



42
43
44
45
46
# File 'lib/wpxf/core/options.rb', line 42

def register_options(opts)
  opts.each do |opt|
    register_option(opt)
  end
end

#scoped_option_change(name, value) {|value| ... } ⇒ Nil

Temporarily change the value of an option and yield a block that uses the scoped value before resetting it back to the original value.

Parameters:

  • name (String)

    the name of the option.

  • value (Object)

    the scoped value.

Yield Parameters:

  • value (Object)

    the scoped value of the option.

Returns:

  • (Nil)

    nothing.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/wpxf/core/options.rb', line 118

def scoped_option_change(name, value)
  original_value = get_option_value(name)

  # Set the scoped option value and invoke the proc.
  set_option_value(name, value)
  yield(get_option_value(name))

  # Reset the option value back to the original.
  set_option_value(name, original_value)

  nil
end

#set_option_value(name, value) ⇒ String, Symbol

Set the value of a module option.

Parameters:

  • name

    the name of the option to set.

  • value

    the value to use.

Returns:

  • (String, Symbol)

    the normalized value, :invalid if the specified value is invalid or :not_found if the name is invalid.



82
83
84
85
86
87
88
89
# File 'lib/wpxf/core/options.rb', line 82

def set_option_value(name, value)
  opt = get_option(name)
  return :not_found unless opt
  return :invalid unless opt.valid?(value)

  datastore[name] = value
  opt.normalize(value)
end

#unregister_option(opt) ⇒ Void

Unregister an Wpxf::Option.

Parameters:

Returns:

  • (Void)

    nothing.



25
26
27
# File 'lib/wpxf/core/options.rb', line 25

def unregister_option(opt)
  options.delete_if { |o| o.name.eql?(opt.name) }
end

#unset_option(name) ⇒ Object

Unset an option or reset it back to its default value.

Parameters:

  • name (String)

    the name of the option to unset.



133
134
135
136
137
138
139
# File 'lib/wpxf/core/options.rb', line 133

def unset_option(name)
  opt = get_option(name)
  return unless opt

  datastore.delete(name)
  datastore[opt.name] = opt.default if opt.required?
end