Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/mbox/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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
Expand All @@ -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 = ''
Expand All @@ -67,25 +76,34 @@ 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
if !input.eof? && line
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
Expand Down
13 changes: 13 additions & 0 deletions lib/mbox/mbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ def unlock
end
end

def reset()
@input.seek 0
end

def has_next()
end

def get_next(opts = {})
lock {
mail = Mail.parse(@input, options.merge(opts))
}
end

def each (opts = {})
@input.seek 0

Expand Down