Class: Host

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

Class Method Summary collapse

Class Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/blockhosts.rb', line 19

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

  s = if banlist then

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

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

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

.block(hashtag) ⇒ Object



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

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

.disable(hashtag) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/blockhosts.rb', line 35

def self.disable(hashtag)   

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

end

.enable(hashtag) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/blockhosts.rb', line 43

def self.enable(hashtag)

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

end

.modifyObject



60
61
62
63
64
65
66
67
68
# File 'lib/blockhosts.rb', line 60

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



70
71
72
73
74
75
76
# File 'lib/blockhosts.rb', line 70

def self.rm(hashtag)   

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

end

.unblock(hashtag) ⇒ Object



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

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