Class: HostsE

Inherits:
Object
  • Object
show all
Includes:
RXFHelperModule
Defined in:
lib/blockhosts.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'hostse.txt', sps_host: '127.0.0.1', sps_port: '59053', hostname: Socket.gethostname, topic: 'dnslookup/' + hostname, debug: false) ⇒ HostsE

Returns a new instance of HostsE.



107
108
109
110
111
112
113
114
115
# 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

Instance Method Details

#subscribe(topic = @topic) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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