Class: Mbox

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mbox/mail/metadata.rb,
lib/mbox/mail.rb,
lib/mbox/mbox.rb,
lib/mbox/mail/file.rb,
lib/mbox/mail/content.rb,
lib/mbox/mail/headers.rb,
lib/mbox/mail/headers/status.rb,
lib/mbox/mail/headers/content_type.rb

Overview

– Copyleft meh. [meh.doesntexist.org | [email protected]]

This file is part of ruby-mbox.

ruby-mbox is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ruby-mbox is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with ruby-mbox. If not, see <www.gnu.org/licenses/>. ++

Defined Under Namespace

Classes: Mail

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(what, options = {}) ⇒ Mbox

Returns a new instance of Mbox.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mbox/mbox.rb', line 51

def initialize (what, options = {})
	@input = if what.respond_to? :to_io
		what.to_io
	elsif what.is_a? String
		StringIO.new(what)
	else
		raise ArgumentError, 'I do not know what to do.'
	end

	@options = { separator: /^From [^\s]+ .{24}/ }.merge(options)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



49
50
51
# File 'lib/mbox/mbox.rb', line 49

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



48
49
50
# File 'lib/mbox/mbox.rb', line 48

def options
  @options
end

#pathObject

Returns the value of attribute path.



49
50
51
# File 'lib/mbox/mbox.rb', line 49

def path
  @path
end

Class Method Details

.finalizer(io) ⇒ Object



42
43
44
# File 'lib/mbox/mbox.rb', line 42

def self.finalizer (io)
	proc { io.close }
end

.open(path, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mbox/mbox.rb', line 25

def self.open (path, options = {})
	input = File.open(File.expand_path(path), 'r+:ASCII-8BIT')

	Mbox.new(input, options).tap {|mbox|
		mbox.path = path
		mbox.name = File.basename(path)

		if block_given?
			yield mbox

			mbox.close
		else
			ObjectSpace.define_finalizer mbox, finalizer(input)
		end
	}
end

Instance Method Details

#[](index, opts = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mbox/mbox.rb', line 97

def [] (index, opts = {})
	lock {
		seek index

		if @input.eof?
			raise IndexError, "#{index} is out of range"
		end

		Mail.parse(@input, options.merge(opts))
	}
end

#closeObject



63
64
65
# File 'lib/mbox/mbox.rb', line 63

def close
	@input.close
end

#each(opts = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/mbox/mbox.rb', line 87

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

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

#has_unread?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
# File 'lib/mbox/mbox.rb', line 157

def has_unread?
	each headers_only: true do |mail|
		return true if mail.unread?
	end

	false
end

#inspectObject



165
166
167
# File 'lib/mbox/mbox.rb', line 165

def inspect
	"#<Mbox:#{name} length=#{length}>"
end

#lengthObject Also known as: size



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mbox/mbox.rb', line 134

def length
	@input.seek(0)

	last   = ''
	length = 0

	lock {
		until @input.eof?
			line = @input.readline

			if line.match(options[:separator]) && last.chomp.empty?
				length += 1
			end

			last = line
		end
	}

	length
end

#lockObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mbox/mbox.rb', line 67

def lock
	if @input.respond_to? :flock
		@input.flock File::LOCK_SH
	end

	if block_given?
		begin
			yield self
		ensure
			unlock
		end
	end
end

#seek(to, whence = IO::SEEK_SET) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/mbox/mbox.rb', line 109

def seek (to, whence = IO::SEEK_SET)
	if whence == IO::SEEK_SET
		@input.seek 0
	end

	last   = ''
	index  = -1

	while line = @input.readline rescue nil
		if line.match(options[:separator]) && last.chomp.empty?
			index += 1

			if index >= to
				@input.seek(-line.length, IO::SEEK_CUR)

				break
			end
		end

		last = line
	end

	self
end

#unlockObject



81
82
83
84
85
# File 'lib/mbox/mbox.rb', line 81

def unlock
	if @input.respond_to? :flock
		@input.flock File::LOCK_UN
	end
end