Class: TMail::Encoder

Inherits:
Object show all
Includes:
TextUtils
Defined in:
lib/tmail/encode.rb

Overview

FIXME: This class can handle only (euc-jp/shift_jis -> iso-2022-jp).

Constant Summary collapse

BENCODE_DEBUG =
false
SPACER =
"\t"
MAX_LINE_LEN =
78
RFC_2822_MAX_LENGTH =
998
OPTIONS =
{
  'EUC'  => '-Ej -m0',
  'SJIS' => '-Sj -m0',
  'UTF8' => nil,      # FIXME
  'NONE' => nil
}

Constants included from TextUtils

TextUtils::ATOM_UNSAFE, TextUtils::CONTROL_CHAR, TextUtils::MESSAGE_ID, TextUtils::MIME_ENCODED, TextUtils::MONTH, TextUtils::NKF_FLAGS, TextUtils::PHRASE_UNSAFE, TextUtils::RFC2231_ENCODED, TextUtils::TOKEN_UNSAFE, TextUtils::WDAY, TextUtils::ZONESTR_TABLE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TextUtils

#atom_safe?, #decode_RFC2231, #decode_params, #join_domain, #message_id?, #mime_encoded?, #quote_atom, #quote_boundary, #quote_phrase, #quote_token, #time2str, #timezone_string_to_unixtime, #to_kcode, #token_safe?, #unquote

Constructor Details

#initialize(dest = nil, encoding = nil, eol = "\r\n", limit = nil) ⇒ Encoder

Returns a new instance of Encoder.



195
196
197
198
199
200
201
# File 'lib/tmail/encode.rb', line 195

def initialize( dest = nil, encoding = nil, eol = "\r\n", limit = nil )
  @f = StrategyInterface.create_dest(dest)
  @opt = OPTIONS[$KCODE]
  @eol = eol
  @preserve_quotes = true
  reset
end

Class Method Details

.encode(str) ⇒ Object



177
178
179
180
181
182
# File 'lib/tmail/encode.rb', line 177

def Encoder.encode( str )
  e = new()
  e.header_body str
  e.terminate
  e.dest.string
end

Instance Method Details

#destObject



229
230
231
# File 'lib/tmail/encode.rb', line 229

def dest
  @f
end

#encode_value(str) ⇒ Object



303
304
305
# File 'lib/tmail/encode.rb', line 303

def encode_value( str )
  str.gsub(TOKEN_UNSAFE) {|s| '%%%02x' % s[0] }
end

#header_body(str) ⇒ Object



256
257
258
# File 'lib/tmail/encode.rb', line 256

def header_body( str )
  scanadd normalize_encoding(str)
end

#header_line(line) ⇒ Object

add



246
247
248
# File 'lib/tmail/encode.rb', line 246

def header_line( line )
  scanadd line
end

#header_name(name) ⇒ Object



250
251
252
253
254
# File 'lib/tmail/encode.rb', line 250

def header_name( name )
  add_text name.split(/-/).map {|i| i.capitalize }.join('-')
  add_text ':'
  add_lwsp ' '
end

#kv_pair(k, v) ⇒ Object

FIXME: implement line folding



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/tmail/encode.rb', line 289

def kv_pair( k, v )
  return if v.nil?
  v = normalize_encoding(v)
  if token_safe?(v)
    add_text k + '=' + v
  elsif not CONTROL_CHAR === v
    add_text k + '=' + quote_token(v)
  else
    # apply RFC2231 encoding
    kv = k + '*=' + "iso-2022-jp'ja'" + encode_value(v)
    add_text kv
  end
end

#lwsp(str) ⇒ Object



266
267
268
# File 'lib/tmail/encode.rb', line 266

def lwsp( str )
  add_lwsp str.sub(/[\r\n]+[^\r\n]*\z/, '')
end

#meta(str) ⇒ Object



270
271
272
# File 'lib/tmail/encode.rb', line 270

def meta( str )
  add_text str
end

#normalize_encoding(str) ⇒ Object



211
212
213
214
215
216
# File 'lib/tmail/encode.rb', line 211

def normalize_encoding( str )
  if @opt
  then NKF.nkf(@opt, str)
  else str
  end
end

#phrase(str) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/tmail/encode.rb', line 278

def phrase( str )
  str = normalize_encoding(str)
  if CONTROL_CHAR === str
    scanadd str
  else
    add_text quote_phrase(str)
  end
end

#preserve_quotesObject



207
208
209
# File 'lib/tmail/encode.rb', line 207

def preserve_quotes
  @preserve_quotes
end

#preserve_quotes=(bool) ⇒ Object



203
204
205
# File 'lib/tmail/encode.rb', line 203

def preserve_quotes=( bool )
  @preserve_quotes
end

#puts(str = nil) ⇒ Object



233
234
235
236
# File 'lib/tmail/encode.rb', line 233

def puts( str = nil )
  @f << str if str
  @f << @eol
end

#resetObject



218
219
220
221
222
# File 'lib/tmail/encode.rb', line 218

def reset
  @text = ''
  @lwsp = ''
  @curlen = 0
end

#spaceObject Also known as: spc



260
261
262
# File 'lib/tmail/encode.rb', line 260

def space
  add_lwsp ' '
end

#terminateObject



224
225
226
227
# File 'lib/tmail/encode.rb', line 224

def terminate
  add_lwsp ''
  reset
end

#text(str) ⇒ Object



274
275
276
# File 'lib/tmail/encode.rb', line 274

def text( str )
  scanadd normalize_encoding(str)
end

#write(str) ⇒ Object



238
239
240
# File 'lib/tmail/encode.rb', line 238

def write( str )
  @f << str
end