Class: BetterCap::Factories::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/factories/parser.rb

Overview

Factory class responsible for listing, parsing and creating BetterCap::Parsers object instances.

Constant Summary collapse

@@path =
File.dirname(__FILE__) + '/../sniffer/parsers/'

Class Method Summary collapse

Class Method Details

.availableObject

Return a list of available parsers.


25
26
27
28
29
30
31
32
33
# File 'lib/bettercap/factories/parser.rb', line 25

def available
  avail = []
  Dir.foreach( @@path ) do |file|
    if file =~ /.rb/ and file != 'base.rb' and file != 'custom.rb'
      avail << file.gsub('.rb','').upcase
    end
  end
  avail
end

.from_cmdline(v) ⇒ Object

Parse the v command line argument and return a list of parser names. Will raise BetterCap::Error if one or more parser names are not valid.

Raises:


37
38
39
40
41
42
43
44
45
46
# File 'lib/bettercap/factories/parser.rb', line 37

def from_cmdline(v)
  raise BetterCap::Error, "No parser names provided" if v.nil?

  avail = available
  list = v.split(',').collect(&:strip).collect(&:upcase).reject{ |c| c.empty? }
  list.each do |parser|
      raise BetterCap::Error, "Invalid parser name '#{parser}'." unless avail.include?(parser) or parser == '*'
  end
  list
end

.load_by_names(parsers) ⇒ Object

Return a list of BetterCap::Parsers instances by their parsers names.


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bettercap/factories/parser.rb', line 49

def load_by_names(parsers)
  loaded = []
  Dir.foreach( @@path ) do |file|
    cname = file.gsub('.rb','').upcase
    if file =~ /.rb/ and file != 'base.rb' and file != 'custom.rb' and ( parsers.include?(cname) or parsers == ['*'] )
      Logger.debug "Loading parser #{cname} ..."

      require_relative "#{@@path}#{file}"

      loaded << BetterCap::Loader.load("BetterCap::Parsers::#{cname.capitalize}").new
    end
  end
  loaded
end

.load_custom(expression) ⇒ Object

Load and return an instance of the BetterCap::Parsers::Custom parser given the expression Regex object.


66
67
68
69
# File 'lib/bettercap/factories/parser.rb', line 66

def load_custom(expression)
  require_relative "#{@@path}custom.rb"
  [ BetterCap::Parsers::Custom.new(expression) ]
end