class Wordlist::CLI

Represents the ‘wordlist` command’s logic.

@api private

@since 1.0.0

Constants

BUG_REPORT_URL

The URL to report bugs to.

FORMATS

Mapping of ‘–format` option values and `format:` Symbols.

PROGRAM_NAME

The program name.

Attributes

builder_options[R]

Additional options for {Builder#initialize}.

@return [Hash{Symbol => Object}]

command[R]

The command to run with each word from the wordlist.

@return [String, nil]

format[R]

The explicit wordlist format to use.

@return [:txt, :gzip, :bzip2, :xz, nil]

mode[R]

Command mode (building or reading).

@return [:build, :read]

modifiers[R]

Wordlist modifiers to apply.

@return [Array<(Symbol, …)>]

operators[R]

Wordlist operators to apply.

@return [Array<(Symbol, …)>]

option_parser[R]

The command’s option parser.

@return [OptionParser]

output[R]

The path to the output wordlist file.

@return [String, nil]

Public Class Methods

new(mode: :read, format: nil, command: nil) click to toggle source

Initializes the command.

@param [:read, :build] mode

@param [:txt, :gzip, :bzip2, :xz, :zip, :7zip, nil] format

@param [String, nil] command

# File lib/wordlist/cli.rb, line 83
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
run(argv=ARGV) click to toggle source

Initializes and runs the command.

@param [Array<String>] argv

Command-line arguments.

@return [Integer]

The exit status of the command.
# File lib/wordlist/cli.rb, line 152
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

Public Instance Methods

add_modifier(name,*args) click to toggle source

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

@param [Symbol] name

The modifier method name.

@param [Array<Object>] args

Additional arguments for the modifier.
# File lib/wordlist/cli.rb, line 119
def add_modifier(name,*args)
  @modifiers << [name, args]
end
add_operator(name,*args) click to toggle source

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

@param [Symbol] name

The operator method name.

@param [Array<Object>] args

Additional arguments for the operator.
# File lib/wordlist/cli.rb, line 106
def add_operator(name,*args)
  @operators << [name, args]
end
build_mode(argv) click to toggle source

Wordlist building mode.

@param [Array<String>] argv

Additional command-line arguments.
# File lib/wordlist/cli.rb, line 194
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) click to toggle source

Opens a wordlist file.

@param [String] path

The path to the wordlist file.

@return [Wordlist::File]

The opened wordlist.
# File lib/wordlist/cli.rb, line 132
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
print_backtrace(exception) click to toggle source

Prints a backtrace to stderr.

@param [Exception] exception

The exception.
print_error(error) click to toggle source

Prints an error message to stderr.

@param [String] error

The error message.
read_mode(argv) click to toggle source

Wordlist reading mode.

@param [Array<String>] argv

Additional command-line arguments.
# File lib/wordlist/cli.rb, line 229
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) click to toggle source

Runs the command.

@param [Array<String>] argv

Command-line arguments.

@return [Integer]

The return status code.
# File lib/wordlist/cli.rb, line 171
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