From 3a65f9ef4d81a941117f377ec1269143a3445865 Mon Sep 17 00:00:00 2001 From: Bob Potter Date: Sat, 29 Dec 2012 13:00:59 -0600 Subject: [PATCH] Extract comments from an address when it is part of a group --- lib/mail/parsers/rfc2822.treetop | 36 ++++++++++++------------- spec/mail/elements/address_list_spec.rb | 7 +++++ spec/mail/example_emails_spec.rb | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/mail/parsers/rfc2822.treetop b/lib/mail/parsers/rfc2822.treetop index fc437f6a9..3bcf437ef 100644 --- a/lib/mail/parsers/rfc2822.treetop +++ b/lib/mail/parsers/rfc2822.treetop @@ -222,7 +222,22 @@ module Mail end rule mailbox - name_addr / addr_spec + (name_addr / addr_spec) { + def dig_comments(comments, elements) + elements.each { |elem| + if elem.respond_to?(:comment) + comments << elem.comment + end + dig_comments(comments, elem.elements) if elem.elements + } + end + + def comments + comments = [] + dig_comments(comments, elements) + comments + end + } end rule address @@ -244,24 +259,7 @@ module Mail end } / - mailbox { - - def dig_comments(comments, elements) - elements.each { |elem| - if elem.respond_to?(:comment) - comments << elem.comment - end - dig_comments(comments, elem.elements) if elem.elements - } - end - - def comments - comments = [] - dig_comments(comments, elements) - comments - end - - } + mailbox end rule address_list diff --git a/spec/mail/elements/address_list_spec.rb b/spec/mail/elements/address_list_spec.rb index fc2bb380f..9d88c810c 100644 --- a/spec/mail/elements/address_list_spec.rb +++ b/spec/mail/elements/address_list_spec.rb @@ -75,6 +75,13 @@ a.addresses.map {|addr| addr.to_s }.should eq result end + it "should extract comments in addreses which are part of a group" do + parse_text = "group: jimmy ;"; + result = ["comment"] + a = Mail::AddressList.new(parse_text) + a.addresses.first.comments.should eq result + end + end describe "functionality" do diff --git a/spec/mail/example_emails_spec.rb b/spec/mail/example_emails_spec.rb index 6797423e0..7533bb288 100644 --- a/spec/mail/example_emails_spec.rb +++ b/spec/mail/example_emails_spec.rb @@ -187,7 +187,7 @@ it "should handle the rfc whitespace test email" do mail = Mail.read(fixture('emails', 'rfc2822', 'example10.eml')) mail.from.should eq ["pete(his account)@silly.test"] - mail.to.should eq ["c@(Chris's host.)public.example", "joe@example.org", "jdoe@one.test"] + mail.to.should eq ["c@public.example", "joe@example.org", "jdoe@one.test"] mail[:cc].group_names.should eq ['(Empty list)(start)Undisclosed recipients '] mail.date.should eq ::DateTime.parse('Thu, 13 Feb 1969 23:32 -0330') mail.message_id.should eq 'testabcd.1234@silly.test'