class HostsE

Public Class Methods

new(filename='hostse.txt', sps_host: '127.0.0.1', sps_port: '59053', hostname: Socket.gethostname, topic: 'dnslookup/' + hostname, debug: false) click to toggle source
# File lib/blockhosts.rb, line 107
def initialize(filename='hostse.txt', sps_host: '127.0.0.1',
      sps_port: '59053', hostname: Socket.gethostname, 
      topic: 'dnslookup/' + hostname, debug: false)

  @sps_host, @sps_port, @topic, @debug = sps_host, sps_port, topic, debug
  @filename = filename
  @entries = FileX.read(filename).lines

end

Public Instance Methods

subscribe(topic=@topic) click to toggle source
# File lib/blockhosts.rb, line 117
def subscribe(topic=@topic)

  sps = SPSSub.new(host: @sps_host, port: @sps_port)

  sps.subscribe(topic: topic + ' or reload' ) do |host, topic|

    if topic == 'reload' then
      @entries = FileX.read(@filename).lines 
      puts 'reloaded ' if @debug
      next
    end
    
    puts 'host: ' + host.inspect if @debug      
    
    @entries.each do |line|

      pattern, hashtag = line.split(/\s+#/)
      puts 'pattern: ' + pattern.inspect if @debug

      if host =~ /#{pattern.gsub('*','.*')}/ then
        puts 'adding host' if @debug
        Host.add(host, hashtag, block: true) unless Host.exists? host

      end
    end

  end

end