Class: SPF::Record
- Inherits:
-
Object
- Object
- SPF::Record
- Defined in:
- lib/spf/model.rb,
lib/spf/model.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_QUALIFIER =
'+'
- RESULTS_BY_QUALIFIER =
{ '' => :pass, '+' => :pass, '-' => :fail, '~' => :softfail, '?' => :neutral }
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#terms ⇒ Object
readonly
Returns the value of attribute terms.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
- #error(exception) ⇒ Object
- #eval(server, request, want_result = true) ⇒ Object
- #global_mod(mod_name) ⇒ Object
- #global_mods ⇒ Object
-
#initialize(options) ⇒ Record
constructor
A new instance of Record.
- #ip_netblocks ⇒ Object
- #parse ⇒ Object
- #parse_term ⇒ Object
- #parse_version_tag ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ Record
Returns a new instance of Record.
836 837 838 839 840 841 842 843 844 |
# File 'lib/spf/model.rb', line 836 def initialize() super() @parse_text = (@text = [:text] if not self.instance_variable_defined?(:@parse_text)).dup @terms ||= [] @global_mods ||= {} @errors = [] @ip_netblocks = [] @raise_exceptions = .has_key?(:raise_exceptions) ? [:raise_exceptions] : true end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
826 827 828 |
# File 'lib/spf/model.rb', line 826 def errors @errors end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
826 827 828 |
# File 'lib/spf/model.rb', line 826 def terms @terms end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
826 827 828 |
# File 'lib/spf/model.rb', line 826 def text @text end |
Class Method Details
.new_from_string(text, options = {}) ⇒ Object
846 847 848 849 850 851 |
# File 'lib/spf/model.rb', line 846 def self.new_from_string(text, = {}) [:text] = text record = new() record.parse return record end |
Instance Method Details
#error(exception) ⇒ Object
853 854 855 856 |
# File 'lib/spf/model.rb', line 853 def error(exception) raise exception if @raise_exceptions @errors << exception end |
#eval(server, request, want_result = true) ⇒ Object
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/spf/model.rb', line 975 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 # Term is a mechanism. mech = term 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 if want_result end elsif SPF::PositionalMod === term # Term is a positional modifier. mod = term mod.process(server, request) elsif SPF::UnknownMod === term # Term is an unknown modifier. Ignore it (RFC 4408, 6/3). else # Invalid term object encountered: error(SPF::UnexpectedTermObjectError.new("Unexpected term object '#{term}' encountered.")) end end server.throw_result(:neutral_by_default, request, 'Default neutral result due to no mechanism matches') 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 if want_result end end |
#global_mod(mod_name) ⇒ Object
967 968 969 |
# File 'lib/spf/model.rb', line 967 def global_mod(mod_name) return @global_mods[mod_name] end |
#global_mods ⇒ Object
963 964 965 |
# File 'lib/spf/model.rb', line 963 def global_mods return @global_mods.values.sort {|a,b| a.precedence <=> b.precedence } end |
#ip_netblocks ⇒ Object
858 859 860 861 |
# File 'lib/spf/model.rb', line 858 def ip_netblocks @ip_netblocks.flatten! return @ip_netblocks end |
#parse ⇒ Object
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
# File 'lib/spf/model.rb', line 863 def parse unless self.instance_variable_defined?(:@parse_text) and @parse_text error(SPF::NothingToParseError.new('Nothing to parse for record')) return end self.parse_version_tag while @parse_text.length > 0 term = nil begin term = self.parse_term rescue SPF::Error => e term.errors << e if term @errors << e raise if @raise_exceptions return if SPF::JunkInRecordError === e or SPF::JunkInTermError === e end end end |
#parse_term ⇒ Object
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 |
# File 'lib/spf/model.rb', line 890 def parse_term regex = / ^ ( #{SPF::Mech::QUALIFIER_PATTERN}? (#{SPF::Mech::NAME_PATTERN}) [^\x20]* ) (?: \x20+ | $ ) /x term = nil if @parse_text.sub!(regex, '') and $& # Looks like a mechanism: mech_text = $1 mech_name = $2.downcase mech_class = self.mech_classes[mech_name.to_sym] exception = nil unless mech_class exception = SPF::InvalidMechError.new("Unknown mechanism type '#{mech_name}' in '#{self.version_tag}' record") error(exception) mech_class = SPF::Mech end term = mech = mech_class.new_from_string(mech_text, {:raise_exceptions => @raise_exceptions}) term.errors << exception if exception @ip_netblocks << mech.ip_netblocks if mech.ip_netblocks @terms << mech if mech_class == SPF::Mech raise SPF::InvalidMechError.new("Unknown mechanism type '#{mech_name}' in '#{self.version_tag}' record") end elsif ( @parse_text.sub!(/ ^ ( (#{SPF::Mod::NAME_PATTERN}) = [^\x20]* ) (?: \x20+ | $ ) /x, '') and $& ) # Looks like a modifier: mod_text = $1 mod_name = $2.downcase mod_class = self.class::MOD_CLASSES[mod_name.to_sym] || SPF::UnknownMod if mod_class # Known modifier. term = mod = mod_class.new_from_string(mod_text, {:raise_exceptions => @raise_exceptions}) if SPF::GlobalMod === mod # Global modifier. if @global_mods[mod_name] raise SPF::DuplicateGlobalMod.new("Duplicate global modifier '#{mod_name}' encountered") end @global_mods[mod_name] = mod elsif SPF::PositionalMod === mod # Positional modifier, queue normally: @terms << mod end end else token_text = @parse_text.sub(/\s.*/, '') hint = nil if token_text =~ /#{SPF::Term::IPV4_ADDRESS_PATTERN}/x hint = 'missing ip4: before IPv4 address?' elsif token_text =~ /#{SPF::Term::IPV6_ADDRESS_PATTERN}/x hint = 'missing ip6: before IPv6 address?' end raise SPF::JunkInRecordError.new("Junk encountered in record '#{@text}'", @text, @parse_text, hint) end @errors.concat(term.errors) return term end |
#parse_version_tag ⇒ Object
882 883 884 885 886 887 888 |
# File 'lib/spf/model.rb', line 882 def parse_version_tag @parse_text.sub!(/^#{self.version_tag_pattern}\s*/ix, '') unless $1 raise SPF::InvalidRecordVersionError.new( "Not a '#{self.version_tag}' record: '#{@text}'") end end |
#to_s ⇒ Object
971 972 973 |
# File 'lib/spf/model.rb', line 971 def to_s return [version_tag, @terms, @global_mods].join(' ') end |