class SurblClient

Public Class Methods

new() click to toggle source
# File lib/surbl_client.rb, line 159
def initialize
    @cache = [nil, nil]
end

Public Instance Methods

include?(domain) click to toggle source

Return true if base domain is listed in SURBL. Returns false otherwise.

# File lib/surbl_client.rb, line 233
def include?(domain)
    return lookup(domain) != nil
end
lookup(domain) click to toggle source

Extract base domain and check it against SURBL. Return [basedomain, lists] array, where basedomain is the base domain and lists is a list of strings indicating which blacklists the domain was found in. If there was no match, return false. If unsure (temporary error), return nil.

# File lib/surbl_client.rb, line 226
def lookup(domain)
    domain = _get_base_domain(domain)
    return _lookup_exact(domain)
end

Private Instance Methods

_get_base_domain(domain) click to toggle source
# File lib/surbl_client.rb, line 164
def _get_base_domain(domain)
    # Remove userinfo
    if domain.include? "@" then
        domain = domain[domain.index("@") + 1, domain.length]
    end
    # Remove port
    if domain.include? ":" then
        domain = domain[0, domain.index(":")]
    end
    # Choose the right "depth"...
    if @@two_level_tlds =~ domain then
        n = 3
    else
        n = 2
    end
    split_domain = domain.split(".")
    return split_domain[-n, split_domain.length].join(".")
end
_lookup_exact(domain) click to toggle source

Like 'lookup', but checks the exact domain name given. Not for direct use.

# File lib/surbl_client.rb, line 185
def _lookup_exact(domain)
    cached_domain = nil
    flags = @cache
    if cached_domain != domain then
        begin
            ip = Resolv.getaddress(domain + ".multi.surbl.org")
            flags = ip.split(".")[-1].to_i
        rescue Resolv::ResolvError => e
            if e.message == "no address for #{domain}" then
                # No record found
                flags = nil
                @cache = [domain, flags]
                return False
            else
                # Unhandled error, pass test for now
                return nil
            end
        rescue
            # Not sure if this can happen. Timeouts?
            return nil
        end
        @cache = [domain, flags]
    end
    if flags then
        matches = []
        @@flags.each do |n,s|
            matches.push(s) if flags & n != 0
        end
        return [domain, matches]
    else
        return False
    end
end