Class: Host

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

Class Method Summary collapse

Class Method Details

.add(hostname, hashtag = hostname.sub(/^www./,'').gsub('-','').gsub('.','dot'), banlist: nil, block: false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/blockhosts.rb', line 21

def self.add(hostname, 
             hashtag=hostname.sub(/^www./,'').gsub('-','').gsub('.','dot'),
             banlist: nil, block: false)

  b = block ? '' : '#'
  
  s = if banlist then

    hashtag = hostname.sub(/^#/,'')
    list, _ = RXFHelper.read(banlist)
    list.lines.map {|hostx| "#{b}#{@ip} #{hostx.chomp} ##{hashtag}" }\
        .join("\n")

  else
    "#{b}#{@ip} #{hostname} ##{hashtag}"
  end

  open(@file, 'a') { |f| f.puts s } unless File.read(@file).include? hostname
end

.block(hashtag) ⇒ Object



76
# File 'lib/blockhosts.rb', line 76

def self.block(hashtag)   self.disable(hashtag)  end

.disable(hashtag) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/blockhosts.rb', line 41

def self.disable(hashtag)   

  modify() do |line|
    line.gsub(/^#([^#]+##{hashtag.sub(/^#/,'')}[^$]+)/,'\1')
  end

end

.enable(hashtag) ⇒ Object



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

def self.enable(hashtag)

  modify() do |line|
    
    if line[0] == '#' then
      line
    else
      line.gsub(/^([^^]+##{hashtag.sub(/^#/,'')}[^$]+$)/,'#\1')
    end
    
  end

end

.exists?(host) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/blockhosts.rb', line 63

def self.exists?(host)
  s = File.read(@file)
  s.lines.grep(/\s#{host}\s/).any?
end

.export(hashtag, filename = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/blockhosts.rb', line 68

def self.export(hashtag, filename=nil)
  
  s = self.view(hashtag)
   
  filename ? FileX.write(filename, s) : s
   
end

.modifyObject



79
80
81
82
83
84
85
86
87
# File 'lib/blockhosts.rb', line 79

def self.modify()
 
  return unless block_given?

  s = File.read(@file)
  s2 = s.lines.map {|x| yield(x)}.join
  File.write(@file, s2) unless s == s2

end

.rm(hashtag) ⇒ Object



89
90
91
92
93
# File 'lib/blockhosts.rb', line 89

def self.rm(hashtag)   

  modify() {|line| line =~ /##{hashtag}/ ? '' : line }

end

.unblock(hashtag) ⇒ Object



77
# File 'lib/blockhosts.rb', line 77

def self.unblock(hashtag) self.enable(hashtag)   end

.view(hashtag) ⇒ Object



95
96
97
98
99
100
# File 'lib/blockhosts.rb', line 95

def self.view(hashtag)
  
  File.read(@file).lines.select {|x| x =~ /##{hashtag}/}\
       .map {|x| x[/(?<=#{@ip} )[^ ]+/]}.join("\n")    
  
end