forked from gen-smtp/gen_smtp
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add parser for RFC 822-style address lists; encode all header values …
…with RFC2047.
- Loading branch information
Arjan Scherpenisse
committed
Feb 27, 2013
1 parent
b9f1196
commit 749b140
Showing
5 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ coverage/* | |
build | ||
*.xcodeproj | ||
.eunit/ | ||
ebin/gen_smtp.app | ||
ebin/gen_smtp.app | ||
src/smtp_rfc822_parse.erl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Nonterminals | ||
addresses | ||
address | ||
name | ||
names | ||
email. | ||
|
||
Terminals | ||
string | ||
',' '<' '>'. | ||
|
||
Rootsymbol | ||
addresses. | ||
|
||
Endsymbol | ||
'$end'. | ||
|
||
addresses -> address : ['$1']. | ||
addresses -> address ',' addresses : ['$1' | '$3']. | ||
addresses -> '$empty' : []. | ||
|
||
address -> email : {undefined, '$1'}. | ||
address -> '<' email '>' : {undefined, '$2'}. | ||
address -> names '<' email '>' : {lists:flatten('$1'), '$3'}. | ||
|
||
email -> string : element(3, '$1'). | ||
names -> name : '$1'. | ||
names -> name names : ['$1', " " | '$2']. | ||
name -> string : element(3, '$1'). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
-module(gen_smtp_util_test). | ||
|
||
-compile(export_all). | ||
|
||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
test_test() -> | ||
smtp_util:parse_rfc822_addresses("foo bar"). | ||
|
||
parse_rfc822_addresses_test_() -> | ||
[ | ||
{"Empty address list", | ||
fun() -> | ||
?assertEqual({ok, []}, smtp_util:parse_rfc822_addresses(<<>>)), | ||
?assertEqual({ok, []}, smtp_util:parse_rfc822_addresses(<<" ">>)), | ||
?assertEqual({ok, []}, smtp_util:parse_rfc822_addresses(<<" \r\n\t ">>)), | ||
?assertEqual({ok, []}, smtp_util:parse_rfc822_addresses(<<" | ||
">>)) | ||
end}, | ||
{"Single addresses", | ||
fun() -> | ||
?assertEqual({ok, [{undefined, "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"john@doe.com">>)), | ||
?assertEqual({ok, [{"Fræderik Hølljen", "me@example.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"Fræderik Hølljen <me@example.com>">>)), | ||
?assertEqual({ok, [{undefined, "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"<john@doe.com>">>)), | ||
?assertEqual({ok, [{"John", "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"John <john@doe.com>">>)), | ||
?assertEqual({ok, [{"John Doe", "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"John Doe <john@doe.com>">>)), | ||
?assertEqual({ok, [{"John Doe", "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"\"John Doe\" <john@doe.com>">>)), | ||
?assertEqual({ok, [{"John \"Mighty\" Doe", "john@doe.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"\"John \\\"Mighty\\\" Doe\" <john@doe.com>">>)) | ||
end}, | ||
{"Multiple addresses", | ||
fun() -> | ||
?assertEqual({ok, [{undefined, "a@a.com"}, {undefined, "b@b.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"a@a.com,b@b.com">>)), | ||
?assertEqual({ok, [{undefined, "a,a@a.com"}, {undefined, "b@b.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"<a,a@a.com>,b@b.com">>)), | ||
?assertEqual({ok, [{"Jan", "a,a@a.com"}, {undefined, "b@b.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"Jan <a,a@a.com>,b@b.com">>)), | ||
?assertEqual({ok, [{"Jan", "a,a@a.com"}, {"Berend Botje", "b@b.com"}]}, | ||
smtp_util:parse_rfc822_addresses(<<"Jan <a,a@a.com>,\"Berend Botje\" <b@b.com>">>)) | ||
end} | ||
]. | ||
|
||
combine_rfc822_addresses_test_() -> | ||
[ | ||
{"One address", | ||
fun() -> | ||
?assertEqual(<<"john@doe.com">>, | ||
smtp_util:combine_rfc822_addresses([{undefined, "john@doe.com"}])), | ||
?assertEqual(<<"John <john@doe.com>">>, | ||
smtp_util:combine_rfc822_addresses([{"John", "john@doe.com"}])), | ||
?assertEqual(<<"\"John \\\"Foo\" <john@doe.com>">>, | ||
smtp_util:combine_rfc822_addresses([{"John \"Foo", "john@doe.com"}])) | ||
end}, | ||
{"Multiple addresses", | ||
fun() -> | ||
?assertEqual(<<"john@doe.com, foo@bar.com">>, | ||
smtp_util:combine_rfc822_addresses([{undefined, "john@doe.com"}, {undefined, "foo@bar.com"}])), | ||
?assertEqual(<<"John <john@doe.com>, foo@bar.com">>, | ||
smtp_util:combine_rfc822_addresses([{"John", "john@doe.com"}, {undefined, "foo@bar.com"}])) | ||
end} | ||
]. | ||
|
||
rfc822_addresses_roundtrip_test() -> | ||
Addr = <<"Jan <a,a@a.com>, Berend Botje <b@b.com>">>, | ||
{ok, Parsed} = smtp_util:parse_rfc822_addresses(Addr), | ||
?assertEqual(Addr, smtp_util:combine_rfc822_addresses(Parsed)), | ||
ok. | ||
|
||
|