lib/spf/model.rb in spf-0.0.3 vs lib/spf/model.rb in spf-0.0.4
- old
+ new
@@ -398,21 +398,21 @@
NAME = 'exists'
def parse_params
self.parse_domain_spec(true)
+ # Other method of denoting "potentially ~infinite" netblocks?
+ @ip_netblocks << nil
end
def params
return @domain_spec ? ':' + @domain_spec : nill
end
def match(server, request, want_result = true)
server.count_dns_interactive_term(request)
- # Other method of denoting "potentially ~infinite" netblocks?
- @ip_netblocks << nil
domain = self.domain(server, request)
packet = server.dns_lookup(domain, 'A')
rrs = (packet.answer or server.count_void_dns_lookup(request))
rrs.each do |rr|
return true if rr.type == 'A'
@@ -427,10 +427,11 @@
NAME = 'ip4'
def parse_params
self.parse_ipv4_network(true)
+ @ip_netblocks << @ip_network
end
def params
result = @ip_network.addr
if @ip_network.masklen != @default_ipv4_prefix_length
@@ -441,11 +442,10 @@
def match(server, request, want_result = true)
ip_network_v6 = IP::V4 === @ip_network ?
SPF::Util.ipv4_address_to_ipv6(@ip_network) :
@ip_network
- @ip_netblocks << @ip_network
return ip_network_v6.contains?(request.ip_address_v6)
end
end
@@ -453,32 +453,30 @@
NAME = 'ip6'
def parse_params
self.parse_ipv6_network(true)
+ @ip_netblocks << @ip_network
end
def params
params = ':' + @ip_network.short
params += '/' + @ip_network.masklen if
@ip_network.masklen != DEFAULT_IPV6_PREFIX_LENGTH
return params
end
def match(server, request, want_result = true)
- @ip_netblocks << @ip_network
return @ip_network.contains?(request.ip_address_v6)
end
end
class SPF::Mech::Include < SPF::Mech
NAME = 'include'
- attr_accessor :nested_record
-
def intitialize(options = {})
super(options)
@nested_record = nil
end
@@ -499,12 +497,10 @@
sub_request = request.new_sub_request({:authority_domain => authority_domain})
# Process sub-request:
result = server.process(sub_request)
- @nested_record = sub_request.record
-
# Translate result of sub-request (RFC 4408, 5.9):
return false unless want_result
return true if SPF::Result::Pass === result
@@ -519,10 +515,18 @@
SPF::Result::None === result
# Propagate any other results (including {Perm,Temp}Error) as-is:
raise result
end
+
+ def nested_record(server, request)
+ return @nested_record if @nested_record
+ authority_domain = self.domain(server, request)
+ sub_request = request.new_sub_request({:authority_domain => authority_domain})
+ return @nested_record = server.select_record(sub_request)
+ end
+
end
class SPF::Mech::MX < SPF::Mech
NAME = 'mx'
@@ -703,20 +707,20 @@
end
end
class SPF::Mod::Redirect < SPF::GlobalMod
- attr_reader :domain_spec, :included_record
+ attr_reader :domain_spec
NAME = 'redirect'
PRECEDENCE = 0.8
- def initialize(options = {})
+ def init(options = {})
super(options)
@nested_record = nil
end
-
+
def parse_params
self.parse_domain_spec(true)
end
def params
@@ -745,16 +749,24 @@
end
# Propagate any other results as-is:
result.throw
end
+
+ def nested_record(server, request)
+ return @nested_record if @nested_record
+ server.count_dns_interactive_term(request)
+ authority_domain = self.domain(server, request)
+ sub_request = request.new_sub_request({:authority_domain => authority_domain})
+ return @nested_record = server.select_record(sub_request)
+ end
end
end
class SPF::Record
- attr_reader :terms, :text, :errors, :ip_netblocks
+ attr_reader :terms, :text, :errors
RESULTS_BY_QUALIFIER = {
'' => :pass,
'+' => :pass,
'-' => :fail,
@@ -777,10 +789,14 @@
record = new(options)
record.parse
return record
end
+ def ip_netblocks
+ @ip_netblocks.flatten!
+ end
+
def parse
unless self.instance_variable_defined?(:@parse_text) and @parse_text
raise SPF::NothingToParseError.new('Nothing to parse for record')
end
self.parse_version_tag
@@ -791,13 +807,11 @@
rescue SPF::Error => e
term.errors << e if term
@errors << e
raise if @raise_exceptions
end
- @ip_netblocks << term.ip_netblocks if term
end
- @ip_netblocks.flatten!
#self.parse_end
end
def parse_version_tag
#@parse_text.sub!(self.version_tag_pattern, '')
@@ -828,10 +842,11 @@
mech_class = self.mech_classes[mech_name.to_sym]
unless mech_class
raise SPF::InvalidMech.new("Unknown mechanism type '#{mech_name}' in '#{@version_tag}' record")
end
term = mech = mech_class.new_from_string(mech_text)
+ @ip_netblocks << mech.ip_netblocks
@terms << mech
elsif (
@parse_text.sub!(/
^
(
@@ -875,11 +890,11 @@
def to_s
return [version_tag, @terms, @global_mods].join(' ')
end
- def eval(server, request)
+ def eval(server, request, want_result = true)
raise SPF::OptionRequiredError.new('SPF server object required for record evaluation') unless server
raise SPF::OptionRequiredError.new('Request object required for record evaluation') unless request
begin
@terms.each do |term|
if SPF::Mech === term
@@ -888,11 +903,11 @@
if mech.match(server, request, request.ip_address != nil)
result_name = RESULTS_BY_QUALIFIER[mech.qualifier]
result_class = server.result_class(result_name)
result = result_class.new([server, request, "Mechanism '#{term}' matched"])
mech.explain(server, request, result)
- raise result
+ raise result if want_result
end
elsif SPF::PositionalMod === term
# Term is a positional modifier.
mod = term
mod.process(server, request)
@@ -906,11 +921,11 @@
rescue SPF::Result => result
# Process global modifiers in ascending order of precedence:
@global_mods.each do |global_mod|
global_mod.process(server, request, result)
end
- raise result
+ raise result if want_result
end
end
class SPF::Record::V1 < SPF::Record
@@ -1044,5 +1059,6 @@
end
end
end
end
+# vim:sw=2 sts=2