class String

Public Instance Methods

blacklisted?(dx) click to toggle source

returns true if the IP is blacklisted; otherwise false examples: barracuda = 'b.barracudacentral.org'.blacklisted?(ip) spamhaus = 'zen.spamhaus.org'.blacklisted?(ip)

# File lib/net/extended_classes.rb, line 117
def blacklisted?(dx)
  domain = dx.split('.').reverse.join('.')+"."+self
  a = []
  Resolv::DNS.open do |dns|
    begin
      a = dns.getresources(domain, Resolv::DNS::Resource::IN::A)
    rescue Resolv::NXDomainError
      a=[]
    end
  end
  if a.size>0 then true else false end
end
dig_a() click to toggle source

returns list of IPV4 addresses, or nil (there should only be one IPV4 address)

# File lib/net/extended_classes.rb, line 70
def dig_a
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::A).collect { |r| r.address.to_s }
    if txts.empty? then nil else txts[0] end
  end
end
dig_aaaa() click to toggle source

returns list of IPV6 addresses, or nil (there should only be one IPV6 address)

# File lib/net/extended_classes.rb, line 79
def dig_aaaa
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::AAAA).collect { |r| r.address.to_s.downcase }
    if txts.empty? then nil else txts[0] end
  end
end
dig_dk() click to toggle source

returns a publibdomainkey, or nil (there should only be one DKIM public key)

# File lib/net/extended_classes.rb, line 97
def dig_dk
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::TXT).collect { |r| r.strings }
    if txts.empty? then nil else txts[0][0] end
  end
end
dig_mx() click to toggle source

returns list of MX names, or nil (there may be multiple MX names for a domain)

# File lib/net/extended_classes.rb, line 88
def dig_mx
  Resolv::DNS.open do |dns|
    txts = dns.getresources(self,Resolv::DNS::Resource::IN::MX).collect { |r| r.exchange.to_s }
    if txts.empty? then nil else txts end
  end
end
dig_ptr() click to toggle source

returns a reverse DNS hostname or nil

# File lib/net/extended_classes.rb, line 105
def dig_ptr
  begin
    Resolv.new.getname(self.downcase)
  rescue Resolv::ResolvError
    nil
  end
end
mta_live?(port) click to toggle source

opens a socket to the IP/port to see if there is an SMTP server there - returns “250 …” if the server is there, or times out in 5 seconds to prevent hanging the process

# File lib/net/extended_classes.rb, line 140
def mta_live?(port)
  tcp_socket = nil
  welcome = nil
  begin
    Timeout.timeout(LiveServerTestTimeout) do
      begin
        tcp_socket = TCPSocket.open(self,port)
      rescue Errno::ECONNREFUSED => e
        return "421 Service not available (port closed)"
      end
      begin
        welcome = tcp_socket.gets
        return welcome if welcome[1]!='2'
        tcp_socket.write("QUIT\r\n")
        line = tcp_socket.gets
        return line if line[1]!='2'
      ensure
        tcp_socket.close if tcp_socket
      end
    end
    return "250 #{welcome.chomp[4..-1]}"
  rescue SocketError => e
    return "421 Service not available (#{e.to_s})"
  rescue Timeout::Error => e
    return "421 Service not available (#{e.to_s})"
  end
end
utf8() click to toggle source

returns a UTF-8 encoded string – be carefule using this with email: email has to be received and transported with NO changes, except the addition of extra headers at the beginning (before any DKIM headers)

# File lib/net/extended_classes.rb, line 133
def utf8
  self.encode('UTF-8', 'binary', :invalid => :replace, :undef => :replace, :replace => '?')
end
validate_plain() { |username| ... } click to toggle source

this validates a password with the base64 plaintext in an AUTH command encoded -> AGNvY29AY3phcm1haWwuY29tAG15LXBhc3N3b3Jk => [“coco@example.com”, “my-password”] “my-password” –> {CRYPT}IwYH/ZXeR8vUM “AGNvY29AY3phcm1haWwuY29tAG15LXBhc3N3b3Jk”.validate_plain { “{CRYPT}IwYH/ZXeR8vUM” } => “coco@example.com”, true “AGNvY29AY3phcm1haWwuY29tAHh4LXBhc3N3b3Jk”.validate_plain { “{CRYPT}IwYH/ZXeR8vUM” } => “coco@example.com”, false

# File lib/net/extended_classes.rb, line 173
def validate_plain
  # decode and split up the username and password)
  username, password = Base64::decode64(self).split("\x00")[1..-1]
  return "", false if username.nil? || password.nil?
  passwd_hash = yield(username) # get the hash
  return "", false if passwd_hash.nil?
  m = passwd_hash.match(/^{(.*)}(.*)$/)
  return username, UnixCrypt.valid?(password, m[2])
end