Class: Mapi::Message

Inherits:
Item
  • Object
show all
Defined in:
lib/mapi/base.rb,
lib/mapi/convert.rb,
lib/mapi/convert/note-mime.rb,
lib/mapi/convert/note-tmail.rb,
lib/mapi/convert/contact.rb

Overview

i refer to it as a message (as does mapi), although perhaps Item is better, as its a more general concept than a message, as used in Pst files. though maybe i’ll switch to using Mapi::Object as the base class there.

IMessage essentially, but there’s also stuff like IMAPIFolder etc. so, for this to form basis for PST Item, it’d need to be more general.

Direct Known Subclasses

Msg, Pst::Item

Defined Under Namespace

Classes: Post, VcardConverter

Constant Summary collapse

CONVERSION_MAP =
{
	'text/x-vcard'   => [:to_vcard, 'vcf'],
	'message/rfc822' => [:to_mime, 'eml'],
	'text/plain'     => [:to_post, 'txt']
	# ...
}

Instance Attribute Summary

Attributes inherited from Item

#properties

Instance Method Summary collapse

Methods inherited from Item

#initialize

Constructor Details

This class inherits a constructor from Mapi::Item

Instance Method Details

#attachmentsObject

these 2 collections should be provided by our subclasses

Raises:

  • (NotImplementedError)


87
88
89
# File 'lib/mapi/base.rb', line 87

def attachments
	raise NotImplementedError
end

#body_to_mimeObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mapi/convert/note-mime.rb', line 146

def body_to_mime
	# to create the body
	# should have some options about serializing rtf. and possibly options to check the rtf
	# for rtf2html conversion, stripping those html tags or other similar stuff. maybe want to
	# ignore it in the cases where it is generated from incoming html. but keep it if it was the
	# source for html and plaintext.
	if props.body_rtf or props.body_html
		# should plain come first?
		mime = Mime.new "Content-Type: multipart/alternative\r\n\r\n"
		# its actually possible for plain body to be empty, but the others not.
		# if i can get an html version, then maybe a callout to lynx can be made...
		mime.parts << Mime.new("Content-Type: text/plain\r\n\r\n" + props.body) if props.body
		# this may be automatically unwrapped from the rtf if the rtf includes the html
		mime.parts << Mime.new("Content-Type: text/html\r\n\r\n"  + props.body_html) if props.body_html
		# temporarily disabled the rtf. its just showing up as an attachment anyway.
		#mime.parts << Mime.new("Content-Type: text/rtf\r\n\r\n"   + props.body_rtf)  if props.body_rtf
		# its thus currently possible to get no body at all if the only body is rtf. that is not
		# really acceptable FIXME
		mime
	else
		# check no header case. content type? etc?. not sure if my Mime class will accept
		Log.debug "taking that other path"
		# body can be nil, hence the to_s
		Mime.new "Content-Type: text/plain\r\n\r\n" + props.body.to_s
	end
end

#body_to_tmailObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mapi/convert/note-tmail.rb', line 156

def body_to_tmail
	# to create the body
	# should have some options about serializing rtf. and possibly options to check the rtf
	# for rtf2html conversion, stripping those html tags or other similar stuff. maybe want to
	# ignore it in the cases where it is generated from incoming html. but keep it if it was the
	# source for html and plaintext.
	if props.body_rtf or props.body_html
		# should plain come first?
		part = TMail::Mail.new
		# its actually possible for plain body to be empty, but the others not.
		# if i can get an html version, then maybe a callout to lynx can be made...
		part.parts << TMail::Mail.parse("Content-Type: text/plain\r\n\r\n" + props.body) if props.body
		# this may be automatically unwrapped from the rtf if the rtf includes the html
		part.parts << TMail::Mail.parse("Content-Type: text/html\r\n\r\n"  + props.body_html) if props.body_html
		# temporarily disabled the rtf. its just showing up as an attachment anyway.
		#mime.parts << Mime.new("Content-Type: text/rtf\r\n\r\n"   + props.body_rtf)  if props.body_rtf
		# its thus currently possible to get no body at all if the only body is rtf. that is not
		# really acceptable FIXME
		part['Content-Type'] = 'multipart/alternative'
		part
	else
		# check no header case. content type? etc?. not sure if my Mime class will accept
		Log.debug "taking that other path"
		# body can be nil, hence the to_s
		TMail::Mail.parse "Content-Type: text/plain\r\n\r\n" + props.body.to_s
	end
end

#convertObject



34
35
36
37
38
39
40
# File 'lib/mapi/convert.rb', line 34

def convert
	type = mime_type
	unless pair = CONVERSION_MAP[type]
		raise 'unable to convert message with mime type - %p' % type
	end
	send pair.first
end

#headersObject



23
24
25
# File 'lib/mapi/convert/note-mime.rb', line 23

def headers
	mime.headers
end

#inspectObject



95
96
97
98
99
100
101
102
# File 'lib/mapi/base.rb', line 95

def inspect
	str = %w[message_class from to subject].map do |key|
		" #{key}=#{props.send(key).inspect}"
	end.compact.join
	str << " recipients=#{recipients.inspect}"
	str << " attachments=#{attachments.inspect}"
	"#<#{self.class.to_s[/\w+$/]}#{str}>"
end

#mimeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mapi/convert/note-mime.rb', line 9

def mime
	return @mime if @mime
	# if these headers exist at all, they can be helpful. we may however get a
	# application/ms-tnef mime root, which means there will be little other than
	# headers. we may get nothing.
	# and other times, when received from external, we get the full cigar, boundaries
	# etc and all.
	# sometimes its multipart, with no boundaries. that throws an error. so we'll be more
	# forgiving here
	@mime = Mime.new props.transport_message_headers.to_s, true
	populate_headers
	@mime
end

#mime_typeObject

get the mime type of the message.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mapi/convert.rb', line 17

def mime_type
	case props.message_class #.downcase <- have a feeling i saw other cased versions
	when 'IPM.Contact'
		# apparently "text/directory; profile=vcard" is what you're supposed to use
		'text/x-vcard'
	when 'IPM.Note'
		'message/rfc822'
	when 'IPM.Post'
		'text/plain'
	when 'IPM.StickyNote'
		'text/plain' # hmmm....
	else
		Mapi::Log.warn 'unknown message_class - %p' % props.message_class
		nil
	end
end

#populate_headersObject

copy data from msg properties storage to standard mime. headers i’ve now seen it where the existing headers had heaps on stuff, and the msg#props had practically nothing. think it was because it was a tnef - msg conversion done by exchange.



30
31
32
33
34
35
36
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mapi/convert/note-mime.rb', line 30

def populate_headers
	# construct a From value
	# should this kind of thing only be done when headers don't exist already? maybe not. if its
	# sent, then modified and saved, the headers could be wrong?
	# hmmm. i just had an example where a mail is sent, from an internal user, but it has transport
	# headers, i think because one recipient was external. the only place the senders email address
	# exists is in the transport headers. so its maybe not good to overwrite from.
	# recipients however usually have smtp address available.
	# maybe we'll do it for all addresses that are smtp? (is that equivalent to 
	# sender_email_address !~ /^\//
	name, email = props.sender_name, props.sender_email_address
	if props.sender_addrtype == 'SMTP'
		headers['From'] = if name and email and name != email
			[%{"#{name}" <#{email}>}]
		else
			[email || name]
		end
	elsif !headers.has_key?('From')
		# some messages were never sent, so that sender stuff isn't filled out. need to find another
		# way to get something
		# what about marking whether we thing the email was sent or not? or draft?
		# for partition into an eventual Inbox, Sent, Draft mbox set?
		# i've now seen cases where this stuff is missing, but exists in transport message headers,
		# so maybe i should inhibit this in that case.
		if email
			# disabling this warning for now
			#Log.warn "* no smtp sender email address available (only X.400). creating fake one"
			# this is crap. though i've specially picked the logic so that it generates the correct
			# email addresses in my case (for my organisation).
			# this user stuff will give valid email i think, based on alias.
			user = name ? name.sub(/(.*), (.*)/, "\\2.\\1") : email[/\w+$/].downcase
			domain = (email[%r{^/O=([^/]+)}i, 1].downcase + '.com' rescue email)
			headers['From'] = [name ? %{"#{name}" <#{user}@#{domain}>} : "<#{user}@#{domain}>" ]
		elsif name
			# we only have a name? thats screwed up.
			# disabling this warning for now
			#Log.warn "* no smtp sender email address available (only name). creating fake one"
			headers['From'] = [%{"#{name}"}]
		else
			# disabling this warning for now
			#Log.warn "* no sender email address available at all. FIXME"
		end
	# else we leave the transport message header version
	end

	# for all of this stuff, i'm assigning in utf8 strings.
	# thats ok i suppose, maybe i can say its the job of the mime class to handle that.
	# but a lot of the headers are overloaded in different ways. plain string, many strings
	# other stuff. what happens to a person who has a " in their name etc etc. encoded words
	# i suppose. but that then happens before assignment. and can't be automatically undone
	# until the header is decomposed into recipients.
	recips_by_type = recipients.group_by { |r| r.type }
	# i want to the the types in a specific order.
	[:to, :cc, :bcc].each do |type|
		# for maximal (probably pointless) fidelity, we try to sort recipients by the
		# numerical part of the ole name
		recips = recips_by_type[type] || []
		recips = (recips.sort_by { |r| r.obj.name[/\d{8}$/].hex } rescue recips)
		# switched to using , for separation, not ;. see issue #4
		# recips.empty? is strange. i wouldn't have thought it possible, but it was right?
		headers[type.to_s.sub(/^(.)/) { $1.upcase }] = [recips.join(', ')] unless recips.empty?
	end
	headers['Subject'] = [props.subject] if props.subject

	# fill in a date value. by default, we won't mess with existing value hear
	if !headers.has_key?('Date')
		# we want to get a received date, as i understand it.
		# use this preference order, or pull the most recent?
		keys = %w[message_delivery_time client_submit_time last_modification_time creation_time]
		time = keys.each { |key| break time if time = props.send(key) }
		time = nil unless Date === time

		# now convert and store
		# this is a little funky. not sure about time zone stuff either?
		# actually seems ok. maybe its always UTC and interpreted anyway. or can be timezoneless.
		# i have no timezone info anyway.
		# in gmail, i see stuff like 15 Jan 2007 00:48:19 -0000, and it displays as 11:48.
		# can also add .localtime here if desired. but that feels wrong.
		headers['Date'] = [Time.iso8601(time.to_s).rfc2822] if time
	end

	# some very simplistic mapping between internet message headers and the
	# mapi properties
	# any of these could be causing duplicates due to case issues. the hack in #to_mime
	# just stops re-duplication at that point. need to move some smarts into the mime
	# code to handle it.
	mapi_header_map = [
		[:internet_message_id, 'Message-ID'],
		[:in_reply_to_id, 'In-Reply-To'],
		# don't set these values if they're equal to the defaults anyway
		[:importance, 'Importance', proc { |val| val.to_s == '1' ? nil : val }],
		[:priority, 'Priority', proc { |val| val.to_s == '1' ? nil : val }],
		[:sensitivity, 'Sensitivity', proc { |val| val.to_s == '0' ? nil : val }],
		# yeah?
		[:conversation_topic, 'Thread-Topic'],
		# not sure of the distinction here
		# :originator_delivery_report_requested ??
		[:read_receipt_requested, 'Disposition-Notification-To', proc { |val| from }]
	]
	mapi_header_map.each do |mapi, mime, *f|
		next unless q = val = props.send(mapi) or headers.has_key?(mime)
		next if f[0] and !(val = f[0].call(val))
		headers[mime] = [val.to_s]
	end
end

#recipientsObject

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/mapi/base.rb', line 91

def recipients
	raise NotImplementedError
end

#to_mimeObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mapi/convert/note-mime.rb', line 173

def to_mime
	# intended to be used for IPM.note, which is the email type. can use it for others if desired,
	# YMMV
	Log.warn "to_mime used on a #{props.message_class}" unless props.message_class == 'IPM.Note'
	# we always have a body
	mime = body = body_to_mime

	# If we have attachments, we take the current mime root (body), and make it the first child
	# of a new tree that will contain body and attachments.
	unless attachments.empty?
		mime = Mime.new "Content-Type: multipart/mixed\r\n\r\n"
		mime.parts << body
		# i don't know any better way to do this. need multipart/related for inline images
		# referenced by cid: urls to work, but don't want to use it otherwise...
		related = false
		attachments.each do |attach|
			part = attach.to_mime
			related = true if part.headers.has_key?('Content-ID') or part.headers.has_key?('Content-Location')
			mime.parts << part
		end
		mime.headers['Content-Type'] = ['multipart/related'] if related
	end

	# at this point, mime is either
	# - a single text/plain, consisting of the body ('taking that other path' above. rare)
	# - a multipart/alternative, consiting of a few bodies (plain and html body. common)
	# - a multipart/mixed, consisting of 1 of the above 2 types of bodies, and attachments.
	# we add this standard preamble if its multipart
	# FIXME preamble.replace, and body.replace both suck.
	# preamble= is doable. body= wasn't being done because body will get rewritten from parts
	# if multipart, and is only there readonly. can do that, or do a reparse...
	# The way i do this means that only the first preamble will say it, not preambles of nested
	# multipart chunks.
	mime.preamble.replace "This is a multi-part message in MIME format.\r\n" if mime.multipart?

	# now that we have a root, we can mix in all our headers
	headers.each do |key, vals|
		# don't overwrite the content-type, encoding style stuff
		next if mime.headers.has_key? key
		# some new temporary hacks
		next if key =~ /content-type/i and vals[0] =~ /base64/
		next if mime.headers.keys.map(&:downcase).include? key.downcase
		mime.headers[key] += vals
	end
	# just a stupid hack to make the content-type header last
	mime.headers['Content-Type'] = mime.headers.delete 'Content-Type'

	mime
end

#to_postObject



56
57
58
# File 'lib/mapi/convert.rb', line 56

def to_post
	Post.new self
end

#to_tmailObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mapi/convert/note-tmail.rb', line 184

def to_tmail
	# intended to be used for IPM.note, which is the email type. can use it for others if desired,
	# YMMV
	Log.warn "to_mime used on a #{props.message_class}" unless props.message_class == 'IPM.Note'
	# we always have a body
	mail = body = body_to_tmail

	# If we have attachments, we take the current mime root (body), and make it the first child
	# of a new tree that will contain body and attachments.
	unless attachments.empty?
		raise NotImplementedError
		mime = Mime.new "Content-Type: multipart/mixed\r\n\r\n"
		mime.parts << body
		# i don't know any better way to do this. need multipart/related for inline images
		# referenced by cid: urls to work, but don't want to use it otherwise...
		related = false
		attachments.each do |attach|
			part = attach.to_mime
			related = true if part.headers.has_key?('Content-ID') or part.headers.has_key?('Content-Location')
			mime.parts << part
		end
		mime.headers['Content-Type'] = ['multipart/related'] if related
	end

	# at this point, mime is either
	# - a single text/plain, consisting of the body ('taking that other path' above. rare)
	# - a multipart/alternative, consiting of a few bodies (plain and html body. common)
	# - a multipart/mixed, consisting of 1 of the above 2 types of bodies, and attachments.
	# we add this standard preamble if its multipart
	# FIXME preamble.replace, and body.replace both suck.
	# preamble= is doable. body= wasn't being done because body will get rewritten from parts
	# if multipart, and is only there readonly. can do that, or do a reparse...
	# The way i do this means that only the first preamble will say it, not preambles of nested
	# multipart chunks.
	mail.quoted_body = "This is a multi-part message in MIME format.\r\n" if mail.multipart?

	# now that we have a root, we can mix in all our headers
	headers.each do |key, vals|
		# don't overwrite the content-type, encoding style stuff
		next if mail[key]
		# some new temporary hacks
		next if key =~ /content-type/i and vals[0] =~ /base64/
		#next if mime.headers.keys.map(&:downcase).include? key.downcase
		mail[key] = vals.first
	end
	# just a stupid hack to make the content-type header last
	#mime.headers['Content-Type'] = mime.headers.delete 'Content-Type'

	mail
end

#to_vcardObject



134
135
136
137
138
139
# File 'lib/mapi/convert/contact.rb', line 134

def to_vcard
	#p props.raw.reject { |key, value| key.guid.inspect !~ /00062004-0000-0000-c000-000000000046/ }.
	#	map { |key, value| [key.to_sym, value] }.reject { |a, b| b.respond_to? :read }
	#y props.to_h.reject { |a, b| b.respond_to? :read }
	VcardConverter.new(self).convert
end

#typeObject

redundant?



137
138
139
# File 'lib/mapi/convert/note-mime.rb', line 137

def type
	props.message_class[/IPM\.(.*)/, 1].downcase rescue nil
end