3
# Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
5
# Permission is hereby granted, free of charge, to any person obtaining
6
# a copy of this software and associated documentation files (the
7
# "Software"), to deal in the Software without restriction, including
8
# without limitation the rights to use, copy, modify, merge, publish,
9
# distribute, sublicense, and/or sell copies of the Software, and to
10
# permit persons to whom the Software is furnished to do so, subject to
11
# the following conditions:
13
# The above copyright notice and this permission notice shall be
14
# included in all copies or substantial portions of the Software.
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
# Note: Originally licensed under LGPL v2+. Using MIT license for Rails
25
# with permission of Minero Aoki.
28
# = TMail - The EMail Swiss Army Knife for Ruby
30
# The TMail library provides you with a very complete way to handle and manipulate EMails
31
# from within your Ruby programs.
33
# Used as the backbone for email handling by the Ruby on Rails and Nitro web frameworks as
34
# well as a bunch of other Ruby apps including the Ruby-Talk mailing list to newsgroup email
35
# gateway, it is a proven and reliable email handler that won't let you down.
37
# Originally created by Minero Aoki, TMail has been recently picked up by Mikel Lindsaar and
38
# is being actively maintained. Numerous backlogged bug fixes have been applied as well as
39
# Ruby 1.9 compatibility and a swath of documentation to boot.
41
# TMail allows you to treat an email totally as an object and allow you to get on with your
42
# own programming without having to worry about crafting the perfect email address validation
43
# parser, or assembling an email from all it's component parts.
45
# TMail handles the most complex part of the email - the header. It generates and parses
46
# headers and provides you with instant access to their innards through simple and logically
47
# named accessor and setter methods.
49
# TMail also provides a wrapper to Net/SMTP as well as Unix Mailbox handling methods to
50
# directly read emails from your unix mailbox, parse them and use them.
52
# Following is the comprehensive list of methods to access TMail::Mail objects. You can also
53
# check out TMail::Mail, TMail::Address and TMail::Headers for other lists.
56
# Provides an exception to throw on errors in Syntax within TMail's parsers
57
class SyntaxError < StandardError; end
59
# Provides a new email boundary to separate parts of the email. This is a random
60
# string based off the current time, so should be fairly unique.
65
# #=> "mimepart_47bf656968207_25a8fbb80114"
67
# #=> "mimepart_47bf66051de4_25a8fbb80240"
68
def TMail.new_boundary
69
'mimepart_' + random_tag
72
# Provides a new email message ID. You can use this to generate unique email message
73
# id's for your email so you can track them.
75
# Optionally takes a fully qualified domain name (default to the current hostname
76
# returned by Socket.gethostname) that will be appended to the message ID.
80
# email.message_id = TMail.new_message_id
81
# #=> "<47bf66845380e_25a8fbb80332@baci.local.tmail>"
83
# #=> "Message-Id: <47bf668b633f1_25a8fbb80475@baci.local.tmail>\n\n"
84
# email.message_id = TMail.new_message_id("lindsaar.net")
85
# #=> "<47bf668b633f1_25a8fbb80475@lindsaar.net.tmail>"
87
# #=> "Message-Id: <47bf668b633f1_25a8fbb80475@lindsaar.net.tmail>\n\n"
88
def TMail.new_message_id( fqdn = nil )
89
fqdn ||= ::Socket.gethostname
90
"<#{random_tag()}@#{fqdn}.tmail>"
94
def TMail.random_tag #:nodoc:
97
sprintf('%x%x_%x%x%d%x',
99
$$, Thread.current.object_id, @uniq, rand(255))
101
private_class_method :random_tag
107
# Text Utils provides a namespace to define TOKENs, ATOMs, PHRASEs and CONTROL characters that
108
# are OK per RFC 2822.
110
# It also provides methods you can call to determine if a string is safe
113
aspecial = %Q|()<>[]:;.\\,"|
114
tspecial = %Q|()<>[];:\\,"/?=|
116
control = %Q|\x00-\x1f\x7f-\xff|
118
CONTROL_CHAR = /[#{control}]/n
119
ATOM_UNSAFE = /[#{Regexp.quote aspecial}#{control}#{lwsp}]/n
120
PHRASE_UNSAFE = /[#{Regexp.quote aspecial}#{control}]/n
121
TOKEN_UNSAFE = /[#{Regexp.quote tspecial}#{control}#{lwsp}]/n
123
# Returns true if the string supplied is free from characters not allowed as an ATOM
124
def atom_safe?( str )
125
not ATOM_UNSAFE === str
128
# If the string supplied has ATOM unsafe characters in it, will return the string quoted
129
# in double quotes, otherwise returns the string unmodified
130
def quote_atom( str )
131
(ATOM_UNSAFE === str) ? dquote(str) : str
134
# If the string supplied has PHRASE unsafe characters in it, will return the string quoted
135
# in double quotes, otherwise returns the string unmodified
136
def quote_phrase( str )
137
(PHRASE_UNSAFE === str) ? dquote(str) : str
140
# Returns true if the string supplied is free from characters not allowed as a TOKEN
141
def token_safe?( str )
142
not TOKEN_UNSAFE === str
145
# If the string supplied has TOKEN unsafe characters in it, will return the string quoted
146
# in double quotes, otherwise returns the string unmodified
147
def quote_token( str )
148
(TOKEN_UNSAFE === str) ? dquote(str) : str
151
# Wraps supplied string in double quotes unless it is already wrapped
152
# Returns double quoted string
153
def dquote( str ) #:nodoc:
154
unless str =~ /^".*?"$/
155
'"' + str.gsub(/["\\]/n) {|s| '\\' + s } + '"'
162
# Unwraps supplied string from inside double quotes
163
# Returns unquoted string
165
str =~ /^"(.*?)"$/m ? $1 : str
168
# Provides a method to join a domain name by it's parts and also makes it
169
# ATOM safe by quoting it as needed
170
def join_domain( arr )
172
if /\A\[.*\]\z/ === i
189
'nst' => -(3 * 60 + 30),
228
# Takes a time zone string from an EMail and converts it to Unix Time (seconds)
229
def timezone_string_to_unixtime( str )
230
if m = /([\+\-])(\d\d?)(\d\d)/.match(str)
231
sec = (m[2].to_i * 60 + m[3].to_i) * 60
232
m[1] == '-' ? -sec : sec
234
min = ZONESTR_TABLE[str.downcase] or
235
raise SyntaxError, "wrong timezone format '#{str}'"
241
WDAY = %w( Sun Mon Tue Wed Thu Fri Sat TMailBUG )
242
MONTH = %w( TMailBUG Jan Feb Mar Apr May Jun
243
Jul Aug Sep Oct Nov Dec TMailBUG )
247
gmt = Time.at(tm.to_i)
249
offset = tm.to_i - Time.local(*gmt.to_a[0,6].reverse).to_i
251
# DO NOT USE strftime: setlocale() breaks it
252
sprintf '%s, %s %s %d %02d:%02d:%02d %+.2d%.2d',
253
WDAY[tm.wday], tm.mday, MONTH[tm.month],
254
tm.year, tm.hour, tm.min, tm.sec,
255
*(offset / 60).divmod(60)
259
MESSAGE_ID = /<[^\@>]+\@[^>]+>/
261
def message_id?( str )
266
MIME_ENCODED = /=\?[^\s?=]+\?[QB]\?[^\s?=]+\?=/i
268
def mime_encoded?( str )
273
def decode_params( hash )
276
hash.each do |key, value|
277
if m = /\*(?:(\d+)\*)?\z/.match(key)
278
((encoded ||= {})[m.pre_match] ||= [])[(m[1] || 0).to_i] = value
280
new[key] = to_kcode(value)
284
encoded.each do |key, strings|
285
new[key] = decode_RFC2231(strings.join(''))
298
flag = NKF_FLAGS[TMail.KCODE] or return str
302
RFC2231_ENCODED = /\A(?:iso-2022-jp|euc-jp|shift_jis|us-ascii)?'[a-z]*'/in
304
def decode_RFC2231( str )
305
m = RFC2231_ENCODED.match(str) or return str
307
to_kcode(m.post_match.gsub(/%[\da-f]{2}/in) {|s| s[1,2].hex.chr })
309
m.post_match.gsub(/%[\da-f]{2}/in, "")
314
# Make sure the Content-Type boundary= parameter is quoted if it contains illegal characters
315
# (to ensure any special characters in the boundary text are escaped from the parser
316
# (such as = in MS Outlook's boundary text))
317
if @body =~ /^(.*)boundary=(.*)$/m
321
remainder =~ /^(.*?)(;.*)$/m
325
boundary_text = remainder.chomp
327
if boundary_text =~ /[\/\?\=]/
328
boundary_text = "\"#{boundary_text}\"" unless boundary_text =~ /^".*?"$/
329
@body = "#{preamble}boundary=#{boundary_text}#{post}"
334
# AppleMail generates illegal character contained Content-Type parameter like:
335
# name==?ISO-2022-JP?B?...=?=
336
# so quote. (This case is only value fits in one line.)
337
def quote_unquoted_bencode
338
@body = @body.gsub(%r"(;\s+[-a-z]+=)(=\?.+?)([;\r\n ]|\z)"m) {
339
head, should_quoted, tail = $~.captures
341
# should_quoted: "=?ISO-2022-JP?B?...=?="
343
head << quote_token(should_quoted) << tail
347
# AppleMail generates name=filename attributes in the content type that
348
# contain spaces. Need to handle this so the TMail Parser can.
349
def quote_unquoted_name
350
@body = @body.gsub(%r|(name=)([\w\s.]+)(.*)|m) {
351
head, should_quoted, tail = $~.captures
353
# should_quoted: "=?ISO-2022-JP?B?...=?="
354
head << quote_token(should_quoted) << tail