From e03654ed26a77fb4dd7eb8ab799a37ef06134f51 Mon Sep 17 00:00:00 2001 From: Bruce James Date: Mon, 16 Mar 2015 22:48:33 +0000 Subject: [PATCH 1/2] Added first and next to be able to trap parse errors on individual emails --- lib/mbox/mbox.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/mbox/mbox.rb b/lib/mbox/mbox.rb index 1cdc233..e9a8ea7 100644 --- a/lib/mbox/mbox.rb +++ b/lib/mbox/mbox.rb @@ -84,6 +84,18 @@ def unlock end end + def first(opts = {}) + @input.seek 0 + next(opts) + end + + def next(opts = {}) + lock { + mail = Mail.parse(@input, options.merge(opts)) + } + mail + end + def each (opts = {}) @input.seek 0 From a6d77e8065b02ceb9deccea7d5a702cd912dd49b Mon Sep 17 00:00:00 2001 From: Bruce James Date: Thu, 19 Mar 2015 09:50:47 +0000 Subject: [PATCH 2/2] Added extra error handling to trap parse errors. Errors are available as an array --- lib/mbox/mail.rb | 30 ++++++++++++++++++++++++------ lib/mbox/mbox.rb | 9 +++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/mbox/mail.rb b/lib/mbox/mail.rb index e965fa2..8b6ed21 100644 --- a/lib/mbox/mail.rb +++ b/lib/mbox/mail.rb @@ -28,6 +28,7 @@ def self.parse (input, options = {}) metadata = Mbox::Mail::Metadata.new headers = Mbox::Mail::Headers.new content = Mbox::Mail::Content.new(headers) + errors = [] next until input.eof? || (line = input.readline).match(options[:separator]) @@ -39,7 +40,11 @@ def self.parse (input, options = {}) until input.eof? || (line = input.readline).match(options[:separator]) break unless line.match(/^>+/) - metadata.parse_from line + begin + metadata.parse_from line + rescue Exception => e + errors << e + end end # headers parsing @@ -56,7 +61,11 @@ def self.parse (input, options = {}) metadata.parse_subject line end end until input.eof? || (line = input.readline).match(options[:separator]) - headers.parse(current) + begin + headers.parse(current) + rescue Exception => e + errors << e + end # content parsing current = '' @@ -67,7 +76,11 @@ def self.parse (input, options = {}) end unless options[:headers_only] - content.parse(current.chomp) + begin + content.parse(current.chomp) + rescue Exception => e + errors << e + end end # put the last separator back in its place @@ -75,17 +88,22 @@ def self.parse (input, options = {}) input.seek(-line.length, IO::SEEK_CUR) end - Mail.new(metadata, headers, content) + Mail.new(metadata, headers, content, errors) end - attr_reader :metadata, :headers, :content + attr_reader :metadata, :headers, :content, :errors - def initialize (metadata, headers, content) + def initialize (metadata, headers, content, errors) @metadata = metadata @headers = headers @content = content + @errors = errors end + def errors? + errors.size > 0 + end + def from metadata.from.first.name end diff --git a/lib/mbox/mbox.rb b/lib/mbox/mbox.rb index e9a8ea7..07647a0 100644 --- a/lib/mbox/mbox.rb +++ b/lib/mbox/mbox.rb @@ -84,16 +84,17 @@ def unlock end end - def first(opts = {}) + def reset() @input.seek 0 - next(opts) end - def next(opts = {}) + def has_next() + end + + def get_next(opts = {}) lock { mail = Mail.parse(@input, options.merge(opts)) } - mail end def each (opts = {})