Class: TMail::Encoder

Inherits:
Object show all
Includes:
TextUtils
Defined in:
lib/gems/tmail-1.2.3.1/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.



224
225
226
227
228
229
230
231
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 224

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



206
207
208
209
210
211
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 206

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

Instance Method Details

#destObject



259
260
261
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 259

def dest
  @f
end

#encode_value(str) ⇒ Object



333
334
335
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 333

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

#header_body(str) ⇒ Object



286
287
288
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 286

def header_body( str )
  scanadd normalize_encoding(str)
end

#header_line(line) ⇒ Object

add



276
277
278
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 276

def header_line( line )
  scanadd line
end

#header_name(name) ⇒ Object



280
281
282
283
284
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 280

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



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 319

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



296
297
298
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 296

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

#meta(str) ⇒ Object



300
301
302
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 300

def meta( str )
  add_text str
end

#normalize_encoding(str) ⇒ Object



241
242
243
244
245
246
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 241

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

#phrase(str) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 308

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

#preserve_quotesObject



237
238
239
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 237

def preserve_quotes
  @preserve_quotes
end

#preserve_quotes=(bool) ⇒ Object



233
234
235
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 233

def preserve_quotes=( bool )
  @preserve_quotes
end

#puts(str = nil) ⇒ Object



263
264
265
266
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 263

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

#resetObject



248
249
250
251
252
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 248

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

#spaceObject Also known as: spc



290
291
292
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 290

def space
  add_lwsp ' '
end

#terminateObject



254
255
256
257
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 254

def terminate
  add_lwsp ''
  reset
end

#text(str) ⇒ Object



304
305
306
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 304

def text( str )
  scanadd normalize_encoding(str)
end

#write(str) ⇒ Object



268
269
270
# File 'lib/gems/tmail-1.2.3.1/lib/tmail/encode.rb', line 268

def write( str )
  @f << str
end