Class: EventMachine::IMAP::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/em-imap/formatter.rb

Defined Under Namespace

Classes: Literal

Constant Summary collapse

DATE_MONTH =
%w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Formatter

Returns a new instance of Formatter.


22
23
24
# File 'lib/em-imap/formatter.rb', line 22

def initialize(&block)
  @block = block
end

Class Method Details

.format(data, &block) ⇒ Object

Format the data to be sent into strings and literals, and call the block for each token to be sent.

NOTE: The block is responsible for handling any network-level concerns, such as sending literals only with permission.

Parameters:

  • data

    The data to format,

  • &block

    The callback, which will be called with a number of strings and EM::IMAP::Formatter::Literal instances.


18
19
20
# File 'lib/em-imap/formatter.rb', line 18

def self.format(data, &block)
  new(&block).send_data(data)
end

Instance Method Details

#put_string(str) ⇒ Object


26
27
28
# File 'lib/em-imap/formatter.rb', line 26

def put_string(str)
  @block.call str
end

#send_command(cmd) ⇒ Object


57
58
59
60
61
62
63
64
65
66
# File 'lib/em-imap/formatter.rb', line 57

def send_command(cmd)
  put_string cmd.tag
  put_string " "
  put_string cmd.cmd
  cmd.args.each do |i|
    put_string " "
    send_data(i)
  end
  put_string "\r\n"
end

#send_data(data) ⇒ Object

The remainder of the code in this file is directly from Net::IMAP. Copyright (C) 2000 Shugo Maeda <[email protected]>


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/em-imap/formatter.rb', line 36

def send_data(data)
  case data
  when nil
    put_string("NIL")
  when String
    send_string_data(data)
  when Integer
    send_number_data(data)
  when Array
    send_list_data(data)
  when Time
    send_time_data(data)
  when Symbol
    send_symbol_data(data)
  when EM::IMAP::Command
    send_command(data)
  else
    data.send_data(self)
  end
end

#send_list_data(list) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/em-imap/formatter.rb', line 94

def send_list_data(list)
  put_string("(")
  first = true
  list.each do |i|
    if first
      first = false
    else
      put_string(" ")
    end
    send_data(i)
  end
  put_string(")")
end

#send_literal(str) ⇒ Object


30
31
32
# File 'lib/em-imap/formatter.rb', line 30

def send_literal(str)
  @block.call Literal.new(str)
end

#send_number_data(num) ⇒ Object


87
88
89
90
91
92
# File 'lib/em-imap/formatter.rb', line 87

def send_number_data(num)
  if num < 0 || num >= 4294967296
    raise Net::IMAP::DataFormatError, num.to_s
  end
  put_string(num.to_s)
end

#send_quoted_string(str) ⇒ Object


83
84
85
# File 'lib/em-imap/formatter.rb', line 83

def send_quoted_string(str)
  put_string('"' + str.gsub(/["\\]/n, "\\\\\\&") + '"')
end

#send_string_data(str) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/em-imap/formatter.rb', line 68

def send_string_data(str)
  case str
  when ""
    put_string('""')
  when /[\x80-\xff\r\n]/n    # literal

    send_literal(str)
  when /[(){ \x00-\x1f\x7f%*"\\]/n    # quoted string

    send_quoted_string(str)
  else
    put_string(str)
  end
end

#send_symbol_data(symbol) ⇒ Object


118
119
120
# File 'lib/em-imap/formatter.rb', line 118

def send_symbol_data(symbol)
  put_string("\\" + symbol.to_s)
end

#send_time_data(time) ⇒ Object


110
111
112
113
114
115
116
# File 'lib/em-imap/formatter.rb', line 110

def send_time_data(time)
  t = time.dup.gmtime
  s = format('"%2d-%3s-%4d %02d:%02d:%02d +0000"',
             t.day, DATE_MONTH[t.month - 1], t.year,
             t.hour, t.min, t.sec)
  put_string(s)
end