Class: Hermeneutics::Cli::ImapTools::UTF7

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/cli/imap/utf7imap.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ UTF7

Returns a new instance of UTF7.



38
39
40
# File 'lib/hermeneutics/cli/imap/utf7imap.rb', line 38

def initialize txt
  @txt = txt
end

Class Method Details

.decode(txt) ⇒ Object



32
33
34
# File 'lib/hermeneutics/cli/imap/utf7imap.rb', line 32

def decode txt
  (new txt).decode
end

.encode(str) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hermeneutics/cli/imap/utf7imap.rb', line 17

def encode str
  e = str.gsub /&|([^ -~]+)/ do
    if $1 then
      b64 = [($1.encode Encoding::UTF_16BE)].pack "m0"
      b64.slice! %r/=+\z/
      b64.tr! "/", ","
    end
    "&#{b64}-"
  end
  if e.empty? or e =~ %r/[ "]/ then
    e = %Q["#{e.gsub /(["\\])/ do "\\#$1" end}"]
  end
  new e
end

Instance Method Details

#decodeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hermeneutics/cli/imap/utf7imap.rb', line 44

def decode
  t = @txt
  if t =~ /\A"(.*)"\z/ then
    t = $1
    t.gsub! /\\(.)/ do $1 end
  end
  t.gsub /&(.*?)-/ do
    if $1.empty? then
      "&"
    else
      r = $1
      r.tr! ",", "/"
      f = -r.length % 4
      if f.nonzero? then
        r << "=" * f
      end
      r, = r.unpack "m"
      r.force_encoding Encoding::UTF_16BE
      r.encode! Encoding.default_external
      r
    end
  end
end

#to_sObject



42
# File 'lib/hermeneutics/cli/imap/utf7imap.rb', line 42

def to_s ; @txt ; end