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
799 800 801 802 803 804 805 806 807 |
# File 'lib/spf/model.rb', line 799 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.
789 790 791 |
# File 'lib/spf/model.rb', line 789 def errors @errors end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
789 790 791 |
# File 'lib/spf/model.rb', line 789 def terms @terms end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
789 790 791 |
# File 'lib/spf/model.rb', line 789 def text @text end |
Class Method Details
.new_from_string(text, options = {}) ⇒ Object
809 810 811 812 813 814 |
# File 'lib/spf/model.rb', line 809 def self.new_from_string(text, = {}) [:text] = text record = new() record.parse return record end |
Instance Method Details
#error(exception) ⇒ Object
816 817 818 819 |
# File 'lib/spf/model.rb', line 816 def error(exception) raise exception if @raise_exceptions @errors << exception end |
#eval(server, request, want_result = true) ⇒ Object
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 962 963 964 |
# File 'lib/spf/model.rb', line 931 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 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
923 924 925 |
# File 'lib/spf/model.rb', line 923 def global_mod(mod_name) return @global_mods[mod_name] end |
#global_mods ⇒ Object
919 920 921 |
# File 'lib/spf/model.rb', line 919 def global_mods return @global_mods.values.sort {|a,b| a.precedence <=> b.precedence } end |
#ip_netblocks ⇒ Object
821 822 823 824 |
# File 'lib/spf/model.rb', line 821 def ip_netblocks @ip_netblocks.flatten! return @ip_netblocks end |
#parse ⇒ Object
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/spf/model.rb', line 826 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 end end end |
#parse_term ⇒ Object
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 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 |
# File 'lib/spf/model.rb', line 853 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 '#{@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 '#{@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 raise SPF::JunkInRecordError.new("Junk encountered in record '#{@text}'") end @errors.concat(term.errors) return term end |
#parse_version_tag ⇒ Object
845 846 847 848 849 850 851 |
# File 'lib/spf/model.rb', line 845 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
927 928 929 |
# File 'lib/spf/model.rb', line 927 def to_s return [version_tag, @terms, @global_mods].join(' ') end |