3 = General Purpose TMail Utilities
7 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
29 # with permission of Minero Aoki.
34 class SyntaxError < StandardError; end
37 def TMail.new_boundary
38 'mimepart_' + random_tag
41 def TMail.new_message_id( fqdn = nil )
42 fqdn ||= ::Socket.gethostname
43 "<#{random_tag()}@#{fqdn}.tmail>"
49 sprintf('%x%x_%x%x%d%x',
51 $$, Thread.current.object_id, @uniq, rand(255))
53 private_class_method :random_tag
58 # Defines characters per RFC that are OK for TOKENs, ATOMs, PHRASEs and CONTROL characters.
60 aspecial = '()<>[]:;.\\,"'
61 tspecial = '()<>[];:\\,"/?='
63 control = '\x00-\x1f\x7f-\xff'
65 ATOM_UNSAFE = /[#{Regexp.quote aspecial}#{control}#{lwsp}]/n
66 PHRASE_UNSAFE = /[#{Regexp.quote aspecial}#{control}]/n
67 TOKEN_UNSAFE = /[#{Regexp.quote tspecial}#{control}#{lwsp}]/n
68 CONTROL_CHAR = /[#{control}]/n
71 # Returns true if the string supplied is free from characters not allowed as an ATOM
72 not ATOM_UNSAFE === str
76 # If the string supplied has ATOM unsafe characters in it, will return the string quoted
77 # in double quotes, otherwise returns the string unmodified
78 (ATOM_UNSAFE === str) ? dquote(str) : str
81 def quote_phrase( str )
82 # If the string supplied has PHRASE unsafe characters in it, will return the string quoted
83 # in double quotes, otherwise returns the string unmodified
84 (PHRASE_UNSAFE === str) ? dquote(str) : str
87 def token_safe?( str )
88 # Returns true if the string supplied is free from characters not allowed as a TOKEN
89 not TOKEN_UNSAFE === str
92 def quote_token( str )
93 # If the string supplied has TOKEN unsafe characters in it, will return the string quoted
94 # in double quotes, otherwise returns the string unmodified
95 (TOKEN_UNSAFE === str) ? dquote(str) : str
99 # Wraps supplied string in double quotes unless it is already wrapped
100 # Returns double quoted string
101 unless str =~ /^".*?"$/
102 '"' + str.gsub(/["\\]/n) {|s| '\\' + s } + '"'
110 # Unwraps supplied string from inside double quotes
111 # Returns unquoted string
112 str =~ /^"(.*?)"$/ ? $1 : str
115 def join_domain( arr )
117 if /\A\[.*\]\z/ === i
134 'nst' => -(3 * 60 + 30),
172 def timezone_string_to_unixtime( str )
173 # Takes a time zone string from an EMail and converts it to Unix Time (seconds)
174 if m = /([\+\-])(\d\d?)(\d\d)/.match(str)
175 sec = (m[2].to_i * 60 + m[3].to_i) * 60
176 m[1] == '-' ? -sec : sec
178 min = ZONESTR_TABLE[str.downcase] or
179 raise SyntaxError, "wrong timezone format '#{str}'"
185 WDAY = %w( Sun Mon Tue Wed Thu Fri Sat TMailBUG )
186 MONTH = %w( TMailBUG Jan Feb Mar Apr May Jun
187 Jul Aug Sep Oct Nov Dec TMailBUG )
191 gmt = Time.at(tm.to_i)
193 offset = tm.to_i - Time.local(*gmt.to_a[0,6].reverse).to_i
195 # DO NOT USE strftime: setlocale() breaks it
196 sprintf '%s, %s %s %d %02d:%02d:%02d %+.2d%.2d',
197 WDAY[tm.wday], tm.mday, MONTH[tm.month],
198 tm.year, tm.hour, tm.min, tm.sec,
199 *(offset / 60).divmod(60)
203 MESSAGE_ID = /<[^\@>]+\@[^>\@]+>/
205 def message_id?( str )
210 MIME_ENCODED = /=\?[^\s?=]+\?[QB]\?[^\s?=]+\?=/i
212 def mime_encoded?( str )
217 def decode_params( hash )
220 hash.each do |key, value|
221 if m = /\*(?:(\d+)\*)?\z/.match(key)
222 ((encoded ||= {})[m.pre_match] ||= [])[(m[1] || 0).to_i] = value
224 new[key] = to_kcode(value)
228 encoded.each do |key, strings|
229 new[key] = decode_RFC2231(strings.join(''))
242 flag = NKF_FLAGS[$KCODE] or return str
246 RFC2231_ENCODED = /\A(?:iso-2022-jp|euc-jp|shift_jis|us-ascii)?'[a-z]*'/in
248 def decode_RFC2231( str )
249 m = RFC2231_ENCODED.match(str) or return str
251 NKF.nkf(NKF_FLAGS[$KCODE],
252 m.post_match.gsub(/%[\da-f]{2}/in) {|s| s[1,2].hex.chr })
254 m.post_match.gsub(/%[\da-f]{2}/in, "")
259 # Make sure the Content-Type boundary= parameter is quoted if it contains illegal characters
260 # (to ensure any special characters in the boundary text are escaped from the parser
261 # (such as = in MS Outlook's boundary text))
262 if @body =~ /^(.*)boundary=(.*)$/m
266 remainder =~ /^(.*)(;.*)$/m
270 boundary_text = remainder.chomp
272 if boundary_text =~ /[\/\?\=]/
273 boundary_text = "\"#{boundary_text}\"" unless boundary_text =~ /^".*?"$/
274 @body = "#{preamble}boundary=#{boundary_text}#{post}"