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.
781 782 783 784 785 786 787 788 789 |
# File 'lib/spf/model.rb', line 781 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.
771 772 773 |
# File 'lib/spf/model.rb', line 771 def errors @errors end |
#terms ⇒ Object (readonly)
Returns the value of attribute terms.
771 772 773 |
# File 'lib/spf/model.rb', line 771 def terms @terms end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
771 772 773 |
# File 'lib/spf/model.rb', line 771 def text @text end |
Class Method Details
.new_from_string(text, options = {}) ⇒ Object
791 792 793 794 795 796 |
# File 'lib/spf/model.rb', line 791 def self.new_from_string(text, = {}) [:text] = text record = new() record.parse return record end |
Instance Method Details
#error(exception) ⇒ Object
798 799 800 801 |
# File 'lib/spf/model.rb', line 798 def error(exception) raise exception if @raise_exceptions @errors << exception end |
#eval(server, request, want_result = true) ⇒ Object
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 |
# File 'lib/spf/model.rb', line 913 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
905 906 907 |
# File 'lib/spf/model.rb', line 905 def global_mod(mod_name) return @global_mods[mod_name] end |
#global_mods ⇒ Object
901 902 903 |
# File 'lib/spf/model.rb', line 901 def global_mods return @global_mods.values.sort {|a,b| a.precedence <=> b.precedence } end |
#ip_netblocks ⇒ Object
803 804 805 806 |
# File 'lib/spf/model.rb', line 803 def ip_netblocks @ip_netblocks.flatten! return @ip_netblocks end |
#parse ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
# File 'lib/spf/model.rb', line 808 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
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 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 |
# File 'lib/spf/model.rb', line 835 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::InvalidMech.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
827 828 829 830 831 832 833 |
# File 'lib/spf/model.rb', line 827 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
909 910 911 |
# File 'lib/spf/model.rb', line 909 def to_s return [version_tag, @terms, @global_mods].join(' ') end |