Class: Mapi::Mime

Inherits:
Object
  • Object
show all
Defined in:
lib/mime.rb

Constant Summary collapse

Hash =
begin
	require 'orderedhash'
	OrderedHash
rescue LoadError
	Hash
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, ignore_body = false) ⇒ Mime

Create a Mime object using str as an initial serialization, which must contain headers and a body (even if empty). Needs work.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mime.rb', line 37

def initialize str, ignore_body=false
	headers, @body = $~[1..-1] if str[/(.*?\r?\n)(?:\r?\n(.*))?\Z/m]

	@headers = Hash.new { |hash, key| hash[key] = [] }
	@body ||= ''
	headers.to_s.scan(/^\S+:\s*.*(?:\n\t.*)*/).each do |header|
		@headers[header[/(\S+):/, 1]] << header[/\S+:\s*(.*)/m, 1].gsub(/\s+/m, ' ').strip # this is kind of wrong
	end

	# don't have to have content type i suppose
	@content_type, attrs = nil, {}
	if content_type = @headers['Content-Type'][0]
		@content_type, attrs = Mime.split_header content_type
	end

	return if ignore_body

	if multipart?
		if body.empty?
			@preamble = ''
			@epilogue = ''
			@parts = []
		else
			# we need to split the message at the boundary
			boundary = attrs['boundary'] or raise "no boundary for multipart message"

			# splitting the body:
			parts = body.split(/--#{Regexp.quote boundary}/m)
			unless parts[-1] =~ /^--/; warn "bad multipart boundary (missing trailing --)"
			else parts[-1][0..1] = ''
			end
			parts.each_with_index do |part, i|
				part =~ /^(\r?\n)?(.*?)(\r?\n)?\Z/m
				part.replace $2
				warn "bad multipart boundary" if (1...parts.length-1) === i and !($1 && $3)
			end
			@preamble = parts.shift
			@epilogue = parts.pop
			@parts = parts.map { |part| Mime.new part }
		end
	end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



33
34
35
# File 'lib/mime.rb', line 33

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



33
34
35
# File 'lib/mime.rb', line 33

def content_type
  @content_type
end

#epilogueObject (readonly)

Returns the value of attribute epilogue.



33
34
35
# File 'lib/mime.rb', line 33

def epilogue
  @epilogue
end

#headersObject (readonly)

Returns the value of attribute headers.



33
34
35
# File 'lib/mime.rb', line 33

def headers
  @headers
end

#partsObject (readonly)

Returns the value of attribute parts.



33
34
35
# File 'lib/mime.rb', line 33

def parts
  @parts
end

#preambleObject (readonly)

Returns the value of attribute preamble.



33
34
35
# File 'lib/mime.rb', line 33

def preamble
  @preamble
end

Class Method Details

.make_boundary(i, extra_obj = Mime) ⇒ Object

i is some value that should be unique for all multipart boundaries for a given message



140
141
142
# File 'lib/mime.rb', line 140

def self.make_boundary i, extra_obj = Mime
	"----_=_NextPart_#{'%03d' % i}_#{'%08x' % extra_obj.object_id}.#{'%08x' % Time.now}"
end

.split_header(header) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mime.rb', line 122

def self.split_header header
	# FIXME: haven't read standard. not sure what its supposed to do with " in the name, or if other
	# escapes are allowed. can't test on windows as " isn't allowed anyway. can be fixed with more
	# accurate parser later.
	# maybe move to some sort of Header class. but not all headers should be of it i suppose.
	# at least add a join_header then, taking name and {}. for use in Mime#to_s (for boundary
	# rewrite), and Attachment#to_mime, among others...
	attrs = {}
	header.scan(/;\s*([^\s=]+)\s*=\s*("[^"]*"|[^\s;]*)\s*/m).each do |key, value|
		if attrs[key]; warn "ignoring duplicate header attribute #{key.inspect}"
		else attrs[key] = value[/^"/] ? value[1..-2] : value
		end
	end

	[header[/^[^;]+/].strip, attrs]
end

Instance Method Details

#inspectObject



84
85
86
87
# File 'lib/mime.rb', line 84

def inspect
	# add some extra here.
	"#<Mime content_type=#{@content_type.inspect}>"
end

#multipart?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/mime.rb', line 80

def multipart?
	@content_type && @content_type =~ /^multipart/ ? true : false
end

#to_s(opts = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mime.rb', line 104

def to_s opts={}
	opts = {:boundary_counter => 0}.merge opts
	if multipart?
		boundary = Mime.make_boundary opts[:boundary_counter] += 1, self
		@body = [preamble, parts.map { |part| "\r\n" + part.to_s(opts) + "\r\n" }, "--\r\n" + epilogue].
			flatten.join("\r\n--" + boundary)
		content_type, attrs = Mime.split_header @headers['Content-Type'][0]
		attrs['boundary'] = boundary
		@headers['Content-Type'] = [([content_type] + attrs.map { |key, val| %{#{key}="#{val}"} }).join('; ')]
	end

	str = ''
	@headers.each do |key, vals|
		vals.each { |val| str << "#{key}: #{val}\r\n" }
	end
	str << "\r\n" + @body
end

#to_treeObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mime.rb', line 89

def to_tree
	if multipart?
		str = "- #{inspect}\n"
		parts.each_with_index do |part, i|
			last = i == parts.length - 1
			part.to_tree.split(/\n/).each_with_index do |line, j|
				str << "  #{last ? (j == 0 ? "\\" : ' ') : '|'}" + line + "\n"
			end
		end
		str
	else
		"- #{inspect}\n"
	end
end