Class: Wordlist::CLI Private

Inherits:
Object
  • Object
show all
Defined in:
lib/wordlist/cli.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents the wordlist command's logic.

Since:

  • 1.0.0

Constant Summary collapse

PROGRAM_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The program name.

Since:

  • 1.0.0

"wordlist"
BUG_REPORT_URL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The URL to report bugs to.

Since:

  • 1.0.0

"https://github.com/postmodern/wordlist.rb/issues/new"
FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapping of --format option values and format: Symbols.

Since:

  • 1.0.0

{
  'txt'  => :txt,
  'gzip' => :gzip,
  'bzip2'=> :bzip2,
  'xz'   => :xz
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode: :read, format: nil, command: nil) ⇒ CLI

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the command.

Parameters:

  • mode (:read, :build) (defaults to: :read)
  • format (:txt, :gzip, :bzip2, :xz, nil) (defaults to: nil)
  • command (String, nil) (defaults to: nil)

Since:

  • 1.0.0



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/wordlist/cli.rb', line 81

def initialize(mode: :read, format: nil, command: nil)
  @option_parser = option_parser

  @mode    = mode
  @format  = format
  @command = command
  @output  = nil

  @operators = []
  @modifiers = []

  @builder_options = {}
end

Instance Attribute Details

#builder_optionsHash{Symbol => Object} (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Additional options for Builder#initialize.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 1.0.0



70
71
72
# File 'lib/wordlist/cli.rb', line 70

def builder_options
  @builder_options
end

#commandString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The command to run with each word from the wordlist.

Returns:

  • (String, nil)

Since:

  • 1.0.0



55
56
57
# File 'lib/wordlist/cli.rb', line 55

def command
  @command
end

#format:txt, ... (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The explicit wordlist format to use.

Returns:

  • (:txt, :gzip, :bzip2, :xz, nil)

Since:

  • 1.0.0



45
46
47
# File 'lib/wordlist/cli.rb', line 45

def format
  @format
end

#mode:build, :read (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Command mode (building or reading).

Returns:

  • (:build, :read)

Since:

  • 1.0.0



40
41
42
# File 'lib/wordlist/cli.rb', line 40

def mode
  @mode
end

#modifiersArray<(Symbol, ...)> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wordlist modifiers to apply.

Returns:

  • (Array<(Symbol, ...)>)

Since:

  • 1.0.0



65
66
67
# File 'lib/wordlist/cli.rb', line 65

def modifiers
  @modifiers
end

#operatorsArray<(Symbol, ...)> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wordlist operators to apply.

Returns:

  • (Array<(Symbol, ...)>)

Since:

  • 1.0.0



60
61
62
# File 'lib/wordlist/cli.rb', line 60

def operators
  @operators
end

#option_parserOptionParser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option parser.

Returns:

  • (OptionParser)

Since:

  • 1.0.0



35
36
37
# File 'lib/wordlist/cli.rb', line 35

def option_parser
  @option_parser
end

#outputString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The path to the output wordlist file.

Returns:

  • (String, nil)

Since:

  • 1.0.0



50
51
52
# File 'lib/wordlist/cli.rb', line 50

def output
  @output
end

Class Method Details

.run(argv = ARGV) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes and runs the command.

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments.

Returns:

  • (Integer)

    The exit status of the command.

Since:

  • 1.0.0



150
151
152
153
154
155
156
157
158
# File 'lib/wordlist/cli.rb', line 150

def self.run(argv=ARGV)
  new().run(argv)
rescue Interrupt
  # https://tldp.org/LDP/abs/html/exitcodes.html
  return 130
rescue Errno::EPIPE
  # STDOUT pipe broken
  return 0
end

Instance Method Details

#add_modifier(name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a modifier to be applied to the wordlist(s) later.

Parameters:

  • name (Symbol)

    The modifier method name.

  • args (Array<Object>)

    Additional arguments for the modifier.

Since:

  • 1.0.0



117
118
119
# File 'lib/wordlist/cli.rb', line 117

def add_modifier(name,*args)
  @modifiers << [name, args]
end

#add_operator(name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds an operator to be applied to the wordlist(s) later.

Parameters:

  • name (Symbol)

    The operator method name.

  • args (Array<Object>)

    Additional arguments for the operator.

Since:

  • 1.0.0



104
105
106
# File 'lib/wordlist/cli.rb', line 104

def add_operator(name,*args)
  @operators << [name, args]
end

#build_mode(argv) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wordlist building mode.

Parameters:

  • argv (Array<String>)

    Additional command-line arguments.

Since:

  • 1.0.0



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/wordlist/cli.rb', line 192

def build_mode(argv)
  builder = begin
              if @format
                Builder.open(@output, format: @format, **@builder_options)
              else
                Builder.open(@output, **@builder_options)
              end
            rescue UnknownFormat, CommandNotFound => error
              print_error(error.message)
              return -1
            end

  begin
    if argv.empty?
      $stdin.each_line do |line|
        builder.parse(line)
      end
    else
      argv.each do |file|
        builder.parse_file(file)
      end
    end
  ensure
    builder.close
  end

  return 0
end

#open_wordlist(path) ⇒ Wordlist::File

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens a wordlist file.

Parameters:

  • path (String)

    The path to the wordlist file.

Returns:

Since:

  • 1.0.0



130
131
132
133
134
135
136
137
138
139
# File 'lib/wordlist/cli.rb', line 130

def open_wordlist(path)
  if @format
    Wordlist::File.open(path, format: @format)
  else
    Wordlist::File.open(path)
  end
rescue WordlistNotFound, UnknownFormat => error
  print_error(error.message)
  exit -1
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a backtrace to stderr.

Parameters:

  • exception (Exception)

    The exception.

Since:

  • 1.0.0



449
450
451
452
453
454
455
456
# File 'lib/wordlist/cli.rb', line 449

def print_backtrace(exception)
  $stderr.puts "Oops! Looks like you've found a bug!"
  $stderr.puts "Please report the following text to: #{BUG_REPORT_URL}"
  $stderr.puts
  $stderr.puts "```"
  $stderr.puts "#{exception.full_message}"
  $stderr.puts "```"
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints an error message to stderr.

Parameters:

  • error (String)

    The error message.

Since:

  • 1.0.0



439
440
441
# File 'lib/wordlist/cli.rb', line 439

def print_error(error)
  $stderr.puts "#{PROGRAM_NAME}: #{error}"
end

#read_mode(argv) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wordlist reading mode.

Parameters:

  • argv (Array<String>)

    Additional command-line arguments.

Since:

  • 1.0.0



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/wordlist/cli.rb', line 227

def read_mode(argv)
  unless argv.length >= 1
    print_error "too few arguments given, requires at least one WORDLIST argument"
    print_error "usage: #{PROGRAM_NAME} [options] WORDLIST ..."
    return -1
  end

  # open the first wodlist
  wordlist = open_wordlist(argv.first)

  # append the additional wordlists
  argv[1..].each { |arg| wordlist += (open_wordlist(arg)) }

  # apply operators first
  @operators.each do |(operator,args)|
    wordlist = wordlist.send(operator,*args)
  end

  # then apply modifiers
  @modifiers.each do |(method,args)|
    wordlist = wordlist.send(method,*args)
  end

  begin
    if @command
      wordlist.each do |word|
        system(@command.gsub('{}',word))
      end
    else
      wordlist.each do |word|
        puts word
      end
    end
  rescue CommandNotFound => error
    print_error(error.message)
    return -1
  end

  return 0
end

#run(argv = ARGV) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the command.

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments.

Returns:

  • (Integer)

    The return status code.

Since:

  • 1.0.0



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/wordlist/cli.rb', line 169

def run(argv=ARGV)
  argv = begin
           @option_parser.parse(argv)
         rescue OptionParser::ParseError => error
           print_error(error.message)
           return -1
         end

  case @mode
  when :build then build_mode(argv)
  else             read_mode(argv)
  end
rescue => error
  print_backtrace(error)
  return -1
end