Class: Net::IMAP::ValidNonLiteralData

Inherits:
CommandData
  • Object
show all
Defined in:
lib/net/imap/command_data.rb

Overview

Represents IMAP text or quoted data, which share the same validations of decoded #data, and differ only in how they are formatted.

data may contain any 7-bit ASCII character except NULL, CR, or LF. Any multibyte UTF-8 character is also allowed when the connection supports UTF8: either UTF8=ACCEPT or IMAP4rev2 have been enabled, or the server supports only IMAP4rev2 and not earlier IMAP revisions, or the server advertises UTF8=ONLY.

NOTE: This does not verify whether the connection supports UTF-8, but that may change in future versions.

The string’s bytes must be valid ASCII or valid UTF-8. The string’s reported encoding is ignored, but the string is not transcoded.

Direct Known Subclasses

QuotedString, RawText

Instance Method Summary collapse

Methods inherited from CommandData

validate

Constructor Details

#initialize(data:) ⇒ ValidNonLiteralData

Returns a new instance of ValidNonLiteralData.



187
188
189
190
191
192
193
194
195
# File 'lib/net/imap/command_data.rb', line 187

def initialize(data:)
  data = String(data.to_str)
  unless data.encoding in Encoding::ASCII | Encoding::UTF_8
    data = data.dup.force_encoding(data.ascii_only? ? "ASCII" : "UTF-8")
  end
  data = -data
  super
  validate
end

Instance Method Details

#ascii_only?Boolean

Returns:

  • (Boolean)


209
# File 'lib/net/imap/command_data.rb', line 209

def ascii_only? = data.ascii_only?

#send_data(imap, tag = nil) ⇒ Object



211
# File 'lib/net/imap/command_data.rb', line 211

def send_data(imap, tag = nil) = imap.__send__(:put_string, formatted)

#validateObject



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/net/imap/command_data.rb', line 197

def validate
  if !(data.encoding in Encoding::ASCII | Encoding::UTF_8)
    raise DataFormatError, "must use ASCII or UTF-8 encoding"
  elsif !data.valid_encoding?
    raise DataFormatError, "invalid UTF-8 must be literal encoded"
  elsif data.include?("\0")
    raise DataFormatError, "NULL byte must be binary literal encoded"
  elsif /[\r\n]/.match?(data)
    raise DataFormatError, "CR and LF bytes must be literal encoded"
  end
end