email_address_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.swp
2
+ pkg/*
3
+ *.rbc
data/LICENSE ADDED
@@ -0,0 +1 @@
1
+
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # EmailAddressValidator #
2
+
3
+ Implementation of RFCs 2822 and 822 for email address validation, and 1123 for domain validation.
4
+
5
+ ## Description ##
6
+
7
+ Parsing email addresses is not easy, and most regex based approaches deviate from the RFCs. This library is based off the actual grammars in the RFCs, allowing it to achieve greater accuracy.
8
+
9
+ This may mean that this library is more permissive than you desire, as the RFCs support syntax that many will find undesirable. To accomodate this, there are a few options users can set to achieve more practical results.
10
+
11
+ The two man things to know are that:
12
+
13
+ 1. What most people desire from a validator is to match only the addr_spec portion of the grammars, as this keeps certain weird addresses, such as RFC groups excluded.
14
+ 2. RFCs 822/2822 do not require valid domains, essentially requiring little more than dotted strings. This library provides additional RFC-1123 Parsing to ensure that a valid domain has been passed in.
15
+
16
+ ## Examples ##
17
+
18
+ Validate only the addr_spec portion of an address as per RFC-2822. Additionally, validate the domain of the address as per RFC-1123. This is what most people probably want:
19
+
20
+ EmailAddressValidator.validate_addr('example@example.net',true)
21
+
22
+ Validate against the full grammar for RFC-2822, without checking the domain.
23
+
24
+ EmailAddressValidator.validate('example@example.net', false)
25
+
26
+ Validate against the addr_spec portion of RFC-822
27
+
28
+ EmailAddressValidator.validate_822_addr('example@example.net')
29
+
30
+ Validate against the full grammar for RFC-822
31
+
32
+ EmailAddressValidator.validate_822('example@example.net')
33
+
34
+ Validate a domain per RFC-1123
35
+
36
+ EmailAddressValidator.validate_domain('example.net')
37
+
38
+ ## Additional notes on the RFCs ##
39
+
40
+ RFC 2822 removes a lot of the cruft that 822 carries with it, unless you have a good reason, you likely want to stay away from RFC 822.
41
+
42
+ A few fun things came up researching this library:
43
+
44
+ * RFCs 2822/822 do not validate domains properly.
45
+ * RFCs 2822/822 support groups, multiple labeled lists of addresses such as `MyGroup: "John Higgins" <john@example.net>, mark mark@example.net;`
46
+ * RFC 822 supports routes, a sequence of mailservers the message is supposed to travel, such as `test@mymailserver@othermailserver.com`
47
+ * RFCs 2822/822 support double quoted strings as the local part of an address, with crazy chars in them, such as `"my@funky$address"@example.net`
48
+ * RFCs 2822/822 support phrases before angle bracketed addresses so the entirety of the string `"Test" <test@example.net>` is valid. This is why you probably only want to validate the addr_spec portion.
49
+
50
+ ## Further Reading ##
51
+
52
+ [RFC-2822](http://www.ietf.org/rfc/rfc2822.txt)
53
+
54
+ [RFC-822](http://www.ietf.org/rfc/rfc0822.txt)
55
+
56
+ [RFC-1123](http://www.ietf.org/rfc/rfc1123.txt)
57
+
58
+ ## Authors ##
59
+
60
+ Evan Phoenix / [evanphx](http://github.com/evanphx)
61
+
62
+ Andrew Cholakian / [andrewvc](http://github.com/andrewvc)
63
+
64
+ ## License ##
65
+
66
+ (The MIT License) FIXME (different license?)
67
+
68
+ Copyright (c) 2011 FIXME (author's name)
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/rdoctask'
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task :default => :spec
9
+
10
+ desc "Rebuild the parsers"
11
+ task "parser" do
12
+ sh "kpeg -s -o lib/email_address_validator/rfc822-parser.rb -f grammars/rfc822.kpeg"
13
+ sh "kpeg -s -o lib/email_address_validator/rfc2822-parser.rb -f grammars/rfc2822.kpeg"
14
+ sh "kpeg -s -o lib/email_address_validator/domain-parser.rb -f grammars/domain.kpeg"
15
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "email_address_validator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "email_address_validator"
7
+ s.version = EmailAddressValidator::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Evan Phoenix", "Andrew Cholakian"]
10
+ s.email = ["andrew@andrewvc.com"]
11
+ s.homepage = "https://github.com/andrewvc/rfc-822"
12
+ s.summary = %q{RFC 2822/822 Email Address Parsing.}
13
+ s.description = %q{RFC Compliant Email Address Parsing using the KPEG grammars.}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.md"
17
+ ]
18
+
19
+ s.rubyforge_project = "email_address_validator"
20
+
21
+ s.add_development_dependency "rspec", ">= 2.4.0"
22
+ s.add_development_dependency "kpeg", ">= 0.7.0"
23
+
24
+ s.files = `git ls-files`.split("\n")
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+ end
29
+
@@ -0,0 +1,28 @@
1
+ %% name = EmailAddressValidator::DomainParser
2
+
3
+ domain = < subdomain > &{ text.size < 255 }
4
+
5
+ subdomain = subdomain "." label
6
+ | label
7
+
8
+ # See http://tools.ietf.org/html/rfc1123#section-2.1.
9
+ # It allows for a digit to start a label.
10
+
11
+ label = let-dig < let-dig-hyp* >
12
+ &{ text.size < 63 && (text.size == 0 || text[-1] != ?-) }
13
+
14
+ let-dig-hyp = let-dig | "-"
15
+
16
+ let-dig = letter | digit
17
+
18
+ # <letter> ::= any one of the 52 alphabetic characters A through Z in
19
+ # upper case and a through z in lower case
20
+
21
+ letter = /[A-Za-z]/
22
+
23
+ # <digit> ::= any one of the ten digits 0 through 9
24
+
25
+ digit = /[0-9]/
26
+
27
+ root = domain !.
28
+
@@ -0,0 +1,152 @@
1
+ %% name = EmailAddressValidator::RFC2822Parser
2
+
3
+ %% { attr_accessor :validate_domain }
4
+
5
+ d(num) = <.> &{ text[0] == num }
6
+ d_btw(start,fin) = <.> &{ t = text[0]; t >= start && t <= fin }
7
+
8
+ WSP = " " | d(9)
9
+
10
+ LF = /\x0A/
11
+ CR = /\x0D/
12
+
13
+ CRLF = CR LF
14
+
15
+ ALPHA = /[A-Za-z]/
16
+
17
+ DIGIT = /[0-9]/
18
+
19
+ NO-WS-CTL = d_btw(1,8)
20
+ | d(11)
21
+ | d(12)
22
+ | d_btw(14,31)
23
+ | d(127)
24
+
25
+ text = d_btw(1,9)
26
+ | d(11)
27
+ | d(12)
28
+ | d_btw(14,127)
29
+ | obs-text
30
+
31
+ quoted-pair = "\\" text
32
+ | obs-qp
33
+
34
+ FWS = (WSP* CRLF)? WSP+
35
+ | obs-FWS
36
+
37
+ ctext = NO-WS-CTL
38
+ | d_btw(33,39)
39
+ | d_btw(42,91)
40
+ | d_btw(93,126)
41
+
42
+ ccontent = ctext | quoted-pair | comment
43
+
44
+ comment = "(" (FWS? ccontent)* FWS? ")"
45
+
46
+ CFWS = (FWS? comment)* ((FWS? comment) | FWS)
47
+
48
+ atext = ALPHA | DIGIT
49
+ | "!" | "#"
50
+ | "$" | "%"
51
+ | "&" | "'"
52
+ | "*" | "+"
53
+ | "-" | "/"
54
+ | "=" | "?"
55
+ | "^" | "_"
56
+ | "`" | "{"
57
+ | "|" | "}"
58
+ | "~"
59
+
60
+ atom = CFWS? atext+ CFWS?
61
+
62
+ dot-atom = CFWS? dot-atom-text CFWS?
63
+
64
+ dot-atom-text = atext+ ("." atext+)*
65
+
66
+ qtext = NO-WS-CTL
67
+ | d(33)
68
+ | d_btw(35,91)
69
+ | d_btw(93,126)
70
+
71
+ qcontent = qtext | quoted-pair
72
+
73
+ quoted-string = CFWS? "\"" (FWS? qcontent)* FWS? "\"" CFWS?
74
+
75
+ word = atom | quoted-string
76
+
77
+ phrase = word+ | obs-phrase
78
+
79
+ utext = NO-WS-CTL
80
+ | d_btw(33,126)
81
+ | obs-utext
82
+
83
+ unstructured = (FWS? utext)* FWS?
84
+
85
+ address = mailbox | group
86
+
87
+ mailbox = name-addr | addr-spec
88
+
89
+ name-addr = display-name? angle-addr
90
+
91
+ angle-addr = CFWS? "<" addr-spec ">" CFWS?
92
+ | obs-angle-addr
93
+
94
+ group = display-name ":" (mailbox-list | CFWS)? ";" CFWS?
95
+
96
+ display-name = phrase
97
+
98
+ mailbox-list = mailbox ("," mailbox)*
99
+ | obs-mbox-list
100
+
101
+ address-list = address ("," address)*
102
+ | obs-addr-list
103
+
104
+ addr-spec = local-part "@" domain
105
+
106
+ local-part = dot-atom | quoted-string | obs-local-part
107
+
108
+ domain = < dot-atom >
109
+ &{ @validate_domain ? EmailAddressValidator::DomainParser.new(text).parse : true }
110
+ | domain-literal
111
+ | < obs-domain >
112
+ &{ @validate_domain ? EmailAddressValidator::DomainParser.new(text).parse : true }
113
+
114
+ domain-literal = CFWS? "[" (FWS? dcontent)* FWS? "]" CFWS?
115
+
116
+ dcontent = dtext | quoted-pair
117
+
118
+ dtext = NO-WS-CTL
119
+ | d_btw(33,90)
120
+ | d_btw(94,126)
121
+
122
+ obs-qp = "\\" d_btw(0,127)
123
+
124
+ obs-text = LF* CR* (obs-char LF* CR*)*
125
+
126
+ obs-char = d_btw(0,9) | d(11)
127
+ | d(12) | d_btw(14,127)
128
+
129
+ obs-utext = obs-text
130
+
131
+ obs-phrase = word (word | "." | CFWS)*
132
+
133
+ obs-phrase-list = phrase
134
+ | (phrase? CFWS? "," CFWS?)+ phrase?
135
+
136
+ obs-FWS = WSP+ (CRLF WSP+)*
137
+
138
+ obs-angle-addr = CFWS? "<" obs-route? addr-spec ">" CFWS?
139
+
140
+ obs-route = CFWS? obs-domain-list ":" CFWS?
141
+
142
+ obs-domain-list = "@" domain ((CFWS | ",")* CFWS? "@" domain)*
143
+
144
+ obs-local-part = word ("." word)*
145
+
146
+ obs-domain = atom ("." atom)*
147
+
148
+ obs-mbox-list = (address? CFWS? "," CFWS?)+ address?
149
+
150
+ root = address !.
151
+
152
+ only_addr_spec = addr-spec !.
@@ -0,0 +1,76 @@
1
+ %% name = EmailAddressValidator::RFC822Parser
2
+
3
+ %% { attr_accessor :validate_domain }
4
+
5
+ HTAB = /\x09/
6
+ LF = /\x0A/
7
+ CR = /\x0D/
8
+ SPACE = " "
9
+ - = SPACE*
10
+ AT = "@"
11
+
12
+ LWSP_char = SPACE | HTAB
13
+ CHAR = /[\x00-\x7f]/
14
+ CTL = /[\x00-\x1f\x7f]/
15
+
16
+ special = /[\]()<>@,;:\\".\[]/
17
+
18
+ CRLF = CR LF
19
+
20
+ linear_white_space = (CRLF? LWSP_char)+
21
+
22
+ atom = /[^\]\x00-\x20 \x7F\x80-\xFF()<>@,;:\\".\[]+/
23
+
24
+ ctext = /[^)\\\x0D\x80-\xFF(]+/
25
+ | linear_white_space
26
+
27
+
28
+ dtext = /[^\]\\\x0D\x80-\xFF\[]+/
29
+ | linear_white_space
30
+
31
+ qtext = /[^"\\\x0D\x80-\xFF]+/
32
+ | linear_white_space
33
+
34
+
35
+ quoted_pair = "\\" CHAR
36
+
37
+ quoted_string = "\"" (qtext | quoted_pair)* "\""
38
+
39
+ domain_literal = "[" (dtext | quoted_pair)* "]"
40
+
41
+ comment = "(" (ctext | quoted_pair | comment)* ")"
42
+
43
+ ocms = comment*
44
+
45
+ word = atom | quoted_string
46
+
47
+ phrase = (word -)+
48
+
49
+ valid = ocms address ocms
50
+
51
+ address = mailbox | group
52
+
53
+ group = phrase ocms ":" ocms mailbox (ocms "," ocms mailbox)* ocms ";"
54
+
55
+ mailbox = addr_spec
56
+ | phrase - ocms - angle_addr
57
+
58
+ angle_addr = "<" ocms route? ocms addr_spec ">"
59
+
60
+ route = (AT ocms domain)+ ":"
61
+
62
+ addr_spec = local_part ocms "@" ocms domain
63
+
64
+ local_part = word ocms ("." ocms word)*
65
+
66
+ domain = domain_literal
67
+ | < sub_domain ocms ("." ocms sub_domain)* >
68
+ &{ @validate_domain ? EmailAddressValidator::DomainParser.new(text).parse : true }
69
+
70
+ sub_domain = domain_ref | domain_literal
71
+
72
+ domain_ref = atom
73
+
74
+ root = valid !.
75
+
76
+ only_addr_spec = addr_spec !.
@@ -0,0 +1,103 @@
1
+ module EmailAddressValidator
2
+ # :stopdoc:
3
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
4
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
5
+ VERSION = ::File.read(PATH + 'version.txt').strip
6
+ # :startdoc:
7
+
8
+ # Returns the library path for the module. If any arguments are given,
9
+ # they will be joined to the end of the libray path using
10
+ # <tt>File.join</tt>.
11
+ #
12
+ def self.libpath( *args )
13
+ rv = args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
14
+ if block_given?
15
+ begin
16
+ $LOAD_PATH.unshift LIBPATH
17
+ rv = yield
18
+ ensure
19
+ $LOAD_PATH.shift
20
+ end
21
+ end
22
+ return rv
23
+ end
24
+
25
+ # Returns the lpath for the module. If any arguments are given,
26
+ # they will be joined to the end of the path using
27
+ # <tt>File.join</tt>.
28
+ #
29
+ def self.path( *args )
30
+ rv = args.empty? ? PATH : ::File.join(PATH, args.flatten)
31
+ if block_given?
32
+ begin
33
+ $LOAD_PATH.unshift PATH
34
+ rv = yield
35
+ ensure
36
+ $LOAD_PATH.shift
37
+ end
38
+ end
39
+ return rv
40
+ end
41
+
42
+ # Utility method used to require all files ending in .rb that lie in the
43
+ # directory below this file that has the same name as the filename passed
44
+ # in. Optionally, a specific _directory_ name can be passed in such that
45
+ # the _filename_ does not have to be equivalent to the directory.
46
+ #
47
+ def self.require_all_libs_relative_to( fname, dir = nil )
48
+ dir ||= ::File.basename(fname, '.*')
49
+ search_me = ::File.expand_path(
50
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
51
+
52
+ Dir.glob(search_me).sort.each {|rb| require rb}
53
+ end
54
+
55
+ # Shorthand for +EmailAddressParser.validate_2822_addr
56
+ def self.validate_addr(addr, validate_domain=false); self.validate_2822; end
57
+
58
+ # Validates +addr+ against the addr_spec portion of RFC 2822.
59
+ # This is what most people actually want out of an email validator
60
+ # You very well may want to set validate_domain to true as well,
61
+ # as RFC2822 doesn't explicitly require valid domains
62
+ def self.validate_2822_addr(addr, validate_domain=false)
63
+ parser = RFC2822Parser.new(addr, "only_addr_spec")
64
+ parser.validate_domain = validate_domain
65
+ parser.parse
66
+ end
67
+
68
+ # Shorthand for +EmailAddressParser.validate_2822
69
+ def self.validate(addr, validate_domain=false); self.validate_2822; end
70
+
71
+ # Validates an email address according to RFC 2822
72
+ # This validates addresses against the full spec, which
73
+ # may not be what you want.
74
+ def self.validate_2822(addr, validate_domain=false)
75
+ parser = RFC2822Parser.new(addr)
76
+ parser.validate_domain = validate_domain
77
+ parser.parse
78
+ end
79
+
80
+ # Validates legacy address according to RFC 822, the original
81
+ # email grammar.
82
+ def self.validate_822(addr, validate_domain=false)
83
+ parser = RFC822Parser.new(addr)
84
+ parser.validate_domain = validate_domain
85
+ parser.parse
86
+ end
87
+
88
+ # Validates only the addr_spec portion an address according to RFC 822
89
+ def self.validate_822_addr(addr, validate_domain=false)
90
+ parser = RFC822Parser.new(addr, "only_addr_spec")
91
+ parser.validate_domain = validate_domain
92
+ parser.parse
93
+ end
94
+
95
+ # Validates a domain name
96
+ def self.validate_domain(domain)
97
+ parser = DomainParser.new(addr)
98
+ parser.parse
99
+ end
100
+
101
+ end
102
+
103
+ EmailAddressValidator.require_all_libs_relative_to(__FILE__)