/
mail.rb
572 lines (480 loc) · 16.9 KB
/
mail.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
135
136
137
138
139
140
141
142
143
144
145
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
172
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# -*- coding: utf-8 -*-
require 'mail'
module Mail
# encoding patch
if self.const_defined?(:Ruby19)
Ruby19.class_eval do
def self.b_value_decode(str)
match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
if match
encoding = match[1]
str = self.decode_base64(match[2])
str.force_encoding(fix_encoding(encoding))
end
# if str contains some emoticon, the following line raises Encoding error
str.encode("utf-8", :invalid => :replace, :replace => "") rescue Jpmobile::Util.ascii_8bit(str)
end
# change encoding
def self.b_value_encode(str, encoding)
str = Jpmobile::Util.encode(str, encoding)
[Ruby19.encode_base64(str), encoding]
end
end
elsif self.const_defined?(:Ruby18)
Ruby18.class_eval do
def self.b_value_decode(str)
match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
if match
encoding = match[1]
str = Ruby18.decode_base64(match[2])
end
str
end
# change encoding
def self.b_value_encode(str, encoding)
str = Jpmobile::Util.encode(str, encoding)
[Encodings::Base64.encode(str), encoding]
end
end
end
class Message
attr_accessor :mobile
def mobile=(m)
if @mobile = m
@charset = m.mail_charset(@charset)
if @body
@body.charset = @charset
@body.mobile = m
end
end
end
def encoded_with_jpmobile
if @mobile
header['subject'].mobile = @mobile if header['subject']
header['from'].mobile = @mobile if header['from']
header['to'].mobile = @mobile if header['to']
self.charset = @mobile.mail_charset(@charset)
ready_to_send!
self.body.charset = @charset
self.body.mobile = @mobile
self.header['Content-Transfer-Encoding'] = @mobile.content_transfer_encoding(self.header)
if @mobile.decorated?
unless self.content_type.match(/image\//)
self.header['Content-ID'] = nil
end
unless self.header['Content-Type'].sub_type == 'mixed'
self.header['Date'] = nil
self.header['Mime-Version'] = nil
end
end
buffer = header.encoded
buffer << "\r\n"
buffer = @mobile.utf8_to_mail_encode(buffer)
buffer << body.encoded(content_transfer_encoding)
buffer
else
encoded_without_jpmobile
end
end
def parse_message_with_jpmobile
header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}/m, 2)
self.header = header_part
@body_part_jpmobile = body_part
convert_encoding_jpmobile
body_part = @body_part_jpmobile
self.body = body_part
end
def init_with_string(string)
# convert to ASCII-8BIT for ascii incompatible encodings
s = Jpmobile::Util.ascii_8bit(string)
self.raw_source = s
set_envelope_header
parse_message
@separate_parts = multipart?
end
def process_body_raw_with_jpmobile
process_body_raw_without_jpmobile
if @mobile
@body.charset = @charset
@body.mobile = @mobile
if has_content_transfer_encoding? and
["base64", "quoted-printable"].include?(content_transfer_encoding) and
["text"].include?(@mobile_main_type)
@body.decode_transfer_encoding
end
if @body.multipart?
@body.parts.each do |p|
p.charset = @charset
p.mobile = @mobile
end
end
end
end
def body_lazy_with_jpmobile(value, index)
body_lazy_without_jpmobile(value, index)
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
alias_method :parse_message_without_jpmobile, :parse_message
alias_method :parse_message, :parse_message_with_jpmobile
alias_method :process_body_raw_without_jpmobile, :process_body_raw
alias_method :process_body_raw, :process_body_raw_with_jpmobile
alias_method :body_lazy_without_jpmobile, :body_lazy
alias_method :body_lazy, :body_lazy_with_jpmobile
# -- docomo
# multipart/mixed
# |- multipart/related
# | |- multipart/alternative
# | | |- text/plain
# | | |- text/html
# | |- image/xxxx (インライン画像)
# |- image/xxxx (添付画像)
# -- au
# multipart/mixed
# |- multipart/alternative
# | |- text/plain
# | |- text/html
# |- image/xxxx (インライン画像)
# |- image/xxxx (添付画像)
# -- normal
# multipart/mixed
# |- multipart/alternative
# | |- text/plain
# | |- text/html
# | |- image/xxxx (インライン画像)
# |- image/xxxx (添付画像)
def rearrange!
if @mobile and @mobile.decoratable?
@mobile.decorated = true
text_body_part = find_part_by_content_type("text/plain").first
html_body_part = find_part_by_content_type("text/html").first
html_body_part.transport_encoding = 'quoted-printable' if html_body_part
inline_images = []
attached_files = []
attachments.each do |p|
if p.content_type.match(/^image\//) and p.content_disposition.match(/^inline/)
if p.header['Content-Type'].parameters['filename']
p.header['Content-Type'].parameters['name'] = p.header['Content-Type'].parameters['filename'].to_s
end
inline_images << p
elsif p.content_disposition
attached_files << p
end
end
alternative_part = Mail::Part.new{content_type 'multipart/alternative'}
alternative_part.add_part(text_body_part) if text_body_part
alternative_part.add_part(html_body_part) if html_body_part
if @mobile.require_related_part?
related_part = Mail::Part.new{content_type 'multipart/related'}
related_part.add_part(alternative_part)
inline_images.each do |inline_image|
related_part.add_part(inline_image)
end
inline_images.clear
else
related_part = alternative_part
end
unless self.header['Content-Type'].sub_type == 'mixed'
self.header['Content-Type'] = self.content_type.gsub(/#{self.header['Content-Type'].sub_type}/, 'mixed')
end
self.parts.clear
self.body = nil
self.add_part(related_part)
inline_images.each do |inline_image|
self.add_part(inline_image)
end
attached_files.each do |attached_file|
self.add_part(attached_file)
end
end
end
def find_part_by_content_type(content_type)
finded_parts = []
self.parts.each do |part|
if part.multipart?
finded_parts << part.find_part_by_content_type(content_type)
elsif part.content_type.match(/^#{content_type}/)
finded_parts << part
end
end
finded_parts.flatten
end
private
def convert_encoding_jpmobile
# decide mobile carrier
if self.header[:from]
mobile_class = Jpmobile::Email.detect_from_mail_header(self.header[:from].value)
@mobile ||= mobile_class.new(nil, nil) if mobile_class
end
# override charset
if self.header[:content_type]
content_type_charset = Jpmobile::Util.extract_charset(self.header[:content_type].value)
unless content_type_charset.blank?
@charset = content_type_charset
self.header[:content_type].parameters[:charset] = @charset
@mobile_main_type = self.header[:content_type].main_type
end
if !Jpmobile::Email.convertable?(self.header[:content_type].value) and content_type_charset.blank?
@charset = ''
end
end
# convert header(s)
if self.header[:subject]
subject_charset = Jpmobile::Util.extract_charset(self.header[:subject].value)
# override subject encoding if @charset is blank
@charset = subject_charset if !subject_charset.blank? # and @charset.blank?
self.header[:subject].charset = subject_charset unless subject_charset.blank?
if @mobile
v = @mobile.to_mail_internal(
Encodings.value_decode(self.header[:subject].value), subject_charset)
if @charset == subject_charset and @mobile.mail_charset != @charset
self.header[:subject].value = Jpmobile::Util.force_encode(v, @charset, Jpmobile::Util::UTF8)
else
self.header[:subject].value = Jpmobile::Util.force_encode(v, @mobile.mail_charset(@charset), Jpmobile::Util::UTF8)
end
end
end
if @body_part_jpmobile and @mobile and !@charset.blank?
@body_part_jpmobile = @mobile.decode_transfer_encoding(@body_part_jpmobile, @charset)
end
end
end
class Part
def init_with_string(string)
self.raw_source = string
set_envelope_header
parse_message
@separate_parts = multipart?
end
def parse_message_with_jpmobile
header_part, body_part = raw_source.split(/#{CRLF}#{WSP}*#{CRLF}/m, 2)
if header_part =~ HEADER_LINE
self.header = header_part
else
self.header = "Content-Type: text/plain\r\n"
end
@body_part_jpmobile = body_part
convert_encoding_jpmobile
body_part = @body_part_jpmobile
self.body = body_part
end
alias_method :parse_message_without_jpmobile, :parse_message
alias_method :parse_message, :parse_message_with_jpmobile
end
class Body
attr_accessor :mobile
# convert encoding
def encoded_with_jpmobile(transfer_encoding = '8bit')
if @mobile and !multipart?
if @mobile.to_mail_body_encoded?(@raw_source)
@raw_source
elsif Jpmobile::Util.ascii_8bit?(@raw_source)
enc = Mail::Encodings::get_encoding(get_best_encoding(transfer_encoding))
Jpmobile::Util.force_encode(enc.encode(@raw_source), nil, @charset)
else
if transfer_encoding == 'quoted-printable'
# [str].pack("M").gsub(/\n/, "\r\n")
Jpmobile::Util.force_encode([@mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))].pack("M").gsub(/\n/, "\r\n"), Jpmobile::Util::BINARY, @charset)
# @mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))
else
@mobile.to_mail_body(Jpmobile::Util.force_encode(@raw_source, @charset, Jpmobile::Util::UTF8))
end
end
else
encoded_without_jpmobile(transfer_encoding)
end
end
def decoded_with_jpmobile
decoded_without_jpmobile
end
# fix charset
def set_charset_with_jpmobile
@charset ||= only_us_ascii? ? 'US-ASCII' : nil
end
def mobile=(m)
@mobile = m
if self.multipart? and @mobile
self.parts.each do |part|
part.charset = @charset
part.mobile = @mobile
part.body.charset = @charset
part.body.mobile = @mobile
end
end
end
def decode_transfer_encoding
_raw_source = Encodings.get_encoding(encoding).decode(@raw_source)
unless Jpmobile::Util.extract_charset(_raw_source) == @charset
@charset = Jpmobile::Util.extract_charset(_raw_source)
end
_raw_source = Jpmobile::Util.set_encoding(_raw_source, @charset)
@raw_source = @mobile.decode_transfer_encoding(_raw_source, @charset)
end
def preamble_with_jpmobile
if @mobile
Jpmobile::Util.encode(@preamble, @charset)
else
preamble_without_jpmobile
end
end
def epilogue_with_jpmobile
if @mobile
Jpmobile::Util.encode(@epilogue, @charset)
else
epilogue_without_jpmobile
end
end
def crlf_boundary_with_jpmobile
if @mobile
Jpmobile::Util.encode(crlf_boundary_without_jpmobile, @charset)
else
crlf_boundary_without_jpmobile
end
end
def end_boundary_with_jpmobile
if @mobile
Jpmobile::Util.encode(end_boundary_without_jpmobile, @charset)
else
end_boundary_without_jpmobile
end
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
alias_method :decoded_without_jpmobile, :decoded
alias_method :decoded, :decoded_with_jpmobile
alias_method :set_charset_without_jpmobile, :set_charset
alias_method :set_charset, :set_charset_with_jpmobile
alias_method :preamble_without_jpmobile, :preamble
alias_method :preamble, :preamble_with_jpmobile
alias_method :crlf_boundary_without_jpmobile, :crlf_boundary
alias_method :crlf_boundary, :crlf_boundary_with_jpmobile
alias_method :end_boundary_without_jpmobile, :end_boundary
alias_method :end_boundary, :end_boundary_with_jpmobile
alias_method :epilogue_without_jpmobile, :epilogue
alias_method :epilogue, :epilogue_with_jpmobile
end
class UnstructuredField
attr_accessor :mobile
end
# for subject
class SubjectField < UnstructuredField
def encoded_with_jpmobile
if @mobile
if @mobile.to_mail_subject_encoded?(value)
"#{name}: #{value}\r\n"
else
# convert encoding
"#{name}: " + @mobile.to_mail_subject(value) + "\r\n"
end
else
encoded_without_jpmobile
end
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
end
class StructuredField
attr_accessor :mobile
end
class FromField < StructuredField
def initialize_with_jpmobile(value = nil, charset = 'utf-8')
@jpmobile_raw_text = value
initialize_without_jpmobile(value, charset)
end
alias_method :initialize_without_jpmobile, :initialize
alias_method :initialize, :initialize_with_jpmobile
def mobile=(m)
if @mobile = m
self.charset = @mobile.mail_charset(@charset)
self.value = @jpmobile_raw_text
self.parse
end
end
def encoded_with_jpmobile
if @mobile
self.charset = @mobile.mail_charset(@charset)
end
encoded_without_jpmobile
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
end
class ToField < StructuredField
def initialize_with_jpmobile(value = nil, charset = 'utf-8')
@jpmobile_raw_text = value
initialize_without_jpmobile(value, charset)
end
alias_method :initialize_without_jpmobile, :initialize
alias_method :initialize, :initialize_with_jpmobile
def mobile=(m)
if @mobile = m
self.charset = @mobile.mail_charset(@charset)
self.value = @jpmobile_raw_text
self.parse
end
end
def encoded_with_jpmobile
if @mobile
self.charset = @mobile.mail_charset(@charset)
end
encoded_without_jpmobile
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
end
class Address
def encoded_with_jpmobile
encoded_without_jpmobile
end
alias_method :encoded_without_jpmobile, :encoded
alias_method :encoded, :encoded_with_jpmobile
end
class ContentTypeElement # :nodoc:
def initialize_with_jpmobile(string)
if m = string.match(/\A(.*?)(name|filename)=("|')(.+)("|')(.*?)\z/) and
m[4].each_byte.detect { |b| (b == 0 || b > 127)}
name = [m[4]].pack('m').strip
string = "#{m[1]}#{m[2]}=#{m[3]}#{name}#{m[5]}#{m[6]}"
end
initialize_without_jpmobile(string)
end
alias_method :initialize_without_jpmobile, :initialize
alias_method :initialize, :initialize_with_jpmobile
end
class ContentDispositionElement # :nodoc:
def initialize_with_jpmobile(string)
if m = string.match(/\A(.*?)(name|filename)=("|')(.+)("|')(.*?)\z/) and
m[4].each_byte.detect { |b| (b == 0 || b > 127)}
name = [m[4]].pack('m').strip
string = "#{m[1]}#{m[2]}=#{m[3]}#{name}#{m[5]}#{m[6]}"
end
initialize_without_jpmobile(string)
end
alias_method :initialize_without_jpmobile, :initialize
alias_method :initialize, :initialize_with_jpmobile
end
class ContentLocationElement # :nodoc:
def initialize_with_jpmobile(string)
if m = string.match(/\A(.*?)(name|filename)=("|')(.+)("|')(.*?)\z/) and
m[4].each_byte.detect { |b| (b == 0 || b > 127)}
name = [m[4]].pack('m').strip
string = "#{m[1]}#{m[2]}=#{m[3]}#{name}#{m[5]}#{m[6]}"
end
initialize_without_jpmobile(string)
end
alias_method :initialize_without_jpmobile, :initialize
alias_method :initialize, :initialize_with_jpmobile
end
class Sendmail
def Sendmail.call(path, arguments, destinations, mail)
encoded_mail = mail.encoded
if Jpmobile::Util.jis?(encoded_mail)
encoded_mail = Jpmobile::Util.ascii_8bit(encoded_mail)
end
IO.popen("#{path} #{arguments} #{destinations}", "w+") do |io|
io.puts encoded_mail.gsub(/\r\r\n/, "\n").to_lf
io.flush
end
end
end
end