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, #quote_unquoted_bencode, #quote_unquoted_name, #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.



229
230
231
232
233
234
235
236
# File 'lib/tmail/encode.rb', line 229

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

Class Method Details

.encode(str) ⇒ Object



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

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

Instance Method Details

#destObject



264
265
266
# File 'lib/tmail/encode.rb', line 264

def dest
  @f
end

#encode_value(str) ⇒ Object



342
343
344
# File 'lib/tmail/encode.rb', line 342

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

#header_body(str) ⇒ Object



291
292
293
# File 'lib/tmail/encode.rb', line 291

def header_body( str )
  scanadd normalize_encoding(str)
end

#header_line(line) ⇒ Object

add



281
282
283
# File 'lib/tmail/encode.rb', line 281

def header_line( line )
  scanadd line
end

#header_name(name) ⇒ Object



285
286
287
288
289
# File 'lib/tmail/encode.rb', line 285

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



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/tmail/encode.rb', line 328

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



301
302
303
# File 'lib/tmail/encode.rb', line 301

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

#meta(str) ⇒ Object



305
306
307
# File 'lib/tmail/encode.rb', line 305

def meta( str )
  add_text str
end

#normalize_encoding(str) ⇒ Object



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

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

#phrase(str) ⇒ Object



317
318
319
320
321
322
323
324
# File 'lib/tmail/encode.rb', line 317

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

#preserve_quotesObject



242
243
244
# File 'lib/tmail/encode.rb', line 242

def preserve_quotes
  @preserve_quotes
end

#preserve_quotes=(bool) ⇒ Object



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

def preserve_quotes=( bool )
  @preserve_quotes
end

#puts(str = nil) ⇒ Object



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

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

#puts_meta(str) ⇒ Object



309
310
311
# File 'lib/tmail/encode.rb', line 309

def puts_meta( str )
  add_text str + @eol + SPACER
end

#resetObject



253
254
255
256
257
# File 'lib/tmail/encode.rb', line 253

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

#spaceObject Also known as: spc



295
296
297
# File 'lib/tmail/encode.rb', line 295

def space
  add_lwsp ' '
end

#terminateObject



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

def terminate
  add_lwsp ''
  reset
end

#text(str) ⇒ Object



313
314
315
# File 'lib/tmail/encode.rb', line 313

def text( str )
  scanadd normalize_encoding(str)
end

#write(str) ⇒ Object



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

def write( str )
  @f << str
end