FIXME: This class can handle only (euc-jp/shift_jis -> iso-2022-jp).
Methods
- add_lwsp
- add_text
- add_with_encode
- concat_A_S
- concat_E
- dest
- do_encode
- encode
- encode_value
- extract_A
- extract_J
- extract_S
- flush
- fold
- header_body
- header_line
- header_name
- kv_pair
- lwsp
- max_bytes
- meta
- new
- normalize_encoding
- phrase
- puts
- reset
- restsize
- scanadd
- space
- spc
- terminate
- text
- write
Included Modules
Constants
| BENCODE_DEBUG | = | false unless defined?(BENCODE_DEBUG) |
| SPACER | = | "\t" |
| MAX_LINE_LEN | = | 70 |
| OPTIONS | = | { 'EUC' => '-Ej -m0', 'SJIS' => '-Sj -m0', 'UTF8' => nil, # FIXME 'NONE' => nil |
| METHOD_ID | = | { ?j => :extract_J, ?e => :extract_E, ?a => :extract_A, ?s => :extract_S |
Public Class methods
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 153
153: def Encoder.encode( str )
154: e = new()
155: e.header_body str
156: e.terminate
157: e.dest.string
158: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 170
170: def initialize( dest = nil, encoding = nil, eol = "\r\n", limit = nil )
171: @f = StrategyInterface.create_dest(dest)
172: @opt = OPTIONS[$KCODE]
173: @eol = eol
174: reset
175: end
Public Instance methods
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 195
195: def dest
196: @f
197: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 268
268: def encode_value( str )
269: str.gsub(TOKEN_UNSAFE) {|s| '%%%02x' % s[0] }
270: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 222
222: def header_body( str )
223: scanadd normalize_encoding(str)
224: end
add
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 212
212: def header_line( line )
213: scanadd line
214: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 216
216: def header_name( name )
217: add_text name.split(/-/).map {|i| i.capitalize }.join('-')
218: add_text ':'
219: add_lwsp ' '
220: end
FIXME: implement line folding
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 255
255: def kv_pair( k, v )
256: v = normalize_encoding(v)
257: if token_safe?(v)
258: add_text k + '=' + v
259: elsif not CONTROL_CHAR === v
260: add_text k + '=' + quote_token(v)
261: else
262: # apply RFC2231 encoding
263: kv = k + '*=' + "iso-2022-jp'ja'" + encode_value(v)
264: add_text kv
265: end
266: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 232
232: def lwsp( str )
233: add_lwsp str.sub(/[\r\n]+[^\r\n]*\z/, '')
234: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 236
236: def meta( str )
237: add_text str
238: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 177
177: def normalize_encoding( str )
178: if @opt
179: then NKF.nkf(@opt, str)
180: else str
181: end
182: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 244
244: def phrase( str )
245: str = normalize_encoding(str)
246: if CONTROL_CHAR === str
247: scanadd str
248: else
249: add_text quote_phrase(str)
250: end
251: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 199
199: def puts( str = nil )
200: @f << str if str
201: @f << @eol
202: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 184
184: def reset
185: @text = ''
186: @lwsp = ''
187: @curlen = 0
188: end
This method is also aliased as
spc
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 226
226: def space
227: add_lwsp ' '
228: end
Alias for space
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 190
190: def terminate
191: add_lwsp ''
192: reset
193: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 240
240: def text( str )
241: scanadd normalize_encoding(str)
242: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 204
204: def write( str )
205: @f << str
206: end
Private Instance methods
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 416
416: def add_lwsp( lwsp )
417: # puts '---- lwsp -------------------------------------'
418: # puts "+ #{lwsp.inspect}"
419: fold if restsize() <= 0
420: flush
421: @lwsp = lwsp
422: end
free length buffer
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 405
405: def add_text( str )
406: @text << str
407: # puts '---- text -------------------------------------'
408: # puts "+ #{str.inspect}"
409: # puts "txt >>>#{@text.inspect}<<<"
410: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 412
412: def add_with_encode( str )
413: @text << "=?iso-2022-jp?B?#{Base64.encode(str)}?="
414: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 335
335: def concat_A_S( types, strs )
336: i = 0
337: types.each_byte do |t|
338: case t
339: when ?a then add_text strs[i]
340: when ?s then add_lwsp strs[i]
341: else
342: raise "TMail FATAL: unknown flag: #{t.chr}"
343: end
344: i += 1
345: end
346: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 355
355: def concat_E( types, strs )
356: if BENCODE_DEBUG
357: puts '---- concat_E'
358: puts "types=#{types.split(//).join(' ')}"
359: puts "strs =#{strs.inspect}"
360: end
361:
362: flush() unless @text.empty?
363:
364: chunk = ''
365: strs.each_with_index do |s,i|
366: mid = METHOD_ID[types[i]]
367: until s.empty?
368: unless c = __send__(mid, chunk.size, s)
369: add_with_encode chunk unless chunk.empty?
370: flush
371: chunk = ''
372: fold
373: c = __send__(mid, 0, s)
374: raise 'TMail FATAL: extract fail' unless c
375: end
376: chunk << c
377: end
378: end
379: add_with_encode chunk unless chunk.empty?
380: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 304
304: def do_encode( types, strs )
305: #
306: # result : (A|E)(S(A|E))*
307: # E : W(SW)*
308: # W : (J|A)+ but must contain J # (J|A)*J(J|A)*
309: # A : <<A character string not to be encoded>>
310: # J : <<A character string to be encoded>>
311: # S : <<LWSP>>
312: #
313: # An encoding unit is `E'.
314: # Input (parameter `types') is (J|A)(J|A|S)*(J|A)
315: #
316: if BENCODE_DEBUG
317: puts
318: puts '-- do_encode ------------'
319: puts types.split(//).join(' ')
320: p strs
321: end
322:
323: e = /[ja]*j[ja]*(?:s[ja]*j[ja]*)*/
324:
325: while m = e.match(types)
326: pre = m.pre_match
327: concat_A_S pre, strs[0, pre.size] unless pre.empty?
328: concat_E m[0], strs[m.begin(0) ... m.end(0)]
329: types = m.post_match
330: strs.slice! 0, m.end(0)
331: end
332: concat_A_S types, strs
333: end
This method is also aliased as
extract_S
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 389
389: def extract_A( chunksize, str )
390: size = max_bytes(chunksize, str.size)
391: return nil if size <= 0
392: str.slice!(0, size)
393: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 382
382: def extract_J( chunksize, str )
383: size = max_bytes(chunksize, str.size) - 6
384: size = (size % 2 == 0) ? (size) : (size - 1)
385: return nil if size <= 0
386: "\e$B#{str.slice!(0, size)}\e(B"
387: end
Alias for extract_A
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 424
424: def flush
425: # puts '---- flush ----'
426: # puts "spc >>>#{@lwsp.inspect}<<<"
427: # puts "txt >>>#{@text.inspect}<<<"
428: @f << @lwsp << @text
429: @curlen += (@lwsp.size + @text.size)
430: @text = ''
431: @lwsp = ''
432: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 434
434: def fold
435: # puts '---- fold ----'
436: @f << @eol
437: @curlen = 0
438: @lwsp = SPACER
439: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 397
397: def max_bytes( chunksize, ssize )
398: (restsize() - '=?iso-2022-jp?B??='.size) / 4 * 3 - chunksize
399: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 441
441: def restsize
442: MAX_LINE_LEN - (@curlen + @lwsp.size + @text.size)
443: end
[ show source ]
# File lib/action_mailer/vendor/tmail/encode.rb, line 274
274: def scanadd( str, force = false )
275: types = ''
276: strs = []
277:
278: until str.empty?
279: if m = /\A[^\e\t\r\n ]+/.match(str)
280: types << (force ? 'j' : 'a')
281: strs.push m[0]
282:
283: elsif m = /\A[\t\r\n ]+/.match(str)
284: types << 's'
285: strs.push m[0]
286:
287: elsif m = /\A\e../.match(str)
288: esc = m[0]
289: str = m.post_match
290: if esc != "\e(B" and m = /\A[^\e]+/.match(str)
291: types << 'j'
292: strs.push m[0]
293: end
294:
295: else
296: raise 'TMail FATAL: encoder scan fail'
297: end
298: str = m.post_match
299: end
300:
301: do_encode types, strs
302: end