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,
  'zip'  => :zip,
  '7zip' => :"7zip"
}

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, :zip, :7zip, nil) (defaults to: nil)
  • command (String, nil) (defaults to: nil)

Since:

  • 1.0.0



83
84
85
86
87
88
89
90
91
92
93
94
95
# 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

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



72
73
74
# File 'lib/wordlist/cli.rb', line 72

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



57
58
59
# File 'lib/wordlist/cli.rb', line 57

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



47
48
49
# File 'lib/wordlist/cli.rb', line 47

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



42
43
44
# File 'lib/wordlist/cli.rb', line 42

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



67
68
69
# File 'lib/wordlist/cli.rb', line 67

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



62
63
64
# File 'lib/wordlist/cli.rb', line 62

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



37
38
39
# File 'lib/wordlist/cli.rb', line 37

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



52
53
54
# File 'lib/wordlist/cli.rb', line 52

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



152
153
154
155
156
157
158
159
160
# 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

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



119
120
121
# File 'lib/wordlist/cli.rb', line 119

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



106
107
108
# File 'lib/wordlist/cli.rb', line 106

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



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
220
221
# 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) ⇒ 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



132
133
134
135
136
137
138
139
140
141
# 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

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



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

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



441
442
443
# File 'lib/wordlist/cli.rb', line 441

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



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
267
268
# 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) ⇒ 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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 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