diff options
author | nicholas a. evans <nicholas.evans@gmail.com> | 2021-05-05 13:15:52 -0400 |
---|---|---|
committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2021-05-06 15:20:35 +0900 |
commit | b0de2e7fe96bb689ac4ab6ccdd04fcf6a3b1d08e (patch) | |
tree | 29fbc09abaaef9a194dca3b6254615adf0a24612 /lib/net/imap | |
parent | deae61e939fee9e24bace3ee99334e841f663114 (diff) |
[ruby/net-imap] Move send_*_data into net/imap/command_data
Partially implements #10.
https://github.com/ruby/net-imap/commit/64d1080d63
Diffstat (limited to 'lib/net/imap')
-rw-r--r-- | lib/net/imap/command_data.rb | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/lib/net/imap/command_data.rb b/lib/net/imap/command_data.rb index 8dd36cd585..d52390c933 100644 --- a/lib/net/imap/command_data.rb +++ b/lib/net/imap/command_data.rb @@ -3,6 +3,115 @@ module Net class IMAP < Protocol + private + + def validate_data(data) + case data + when nil + when String + when Integer + NumValidator.ensure_number(data) + when Array + if data[0] == 'CHANGEDSINCE' + NumValidator.ensure_mod_sequence_value(data[1]) + else + data.each do |i| + validate_data(i) + end + end + when Time + when Symbol + else + data.validate + end + end + + def send_data(data, tag = nil) + case data + when nil + put_string("NIL") + when String + send_string_data(data, tag) + when Integer + send_number_data(data) + when Array + send_list_data(data, tag) + when Time + send_time_data(data) + when Symbol + send_symbol_data(data) + else + data.send_data(self, tag) + end + end + + def send_string_data(str, tag = nil) + case str + when "" + put_string('""') + when /[\x80-\xff\r\n]/n + # literal + send_literal(str, tag) + when /[(){ \x00-\x1f\x7f%*"\\]/n + # quoted string + send_quoted_string(str) + else + put_string(str) + end + end + + def send_quoted_string(str) + put_string('"' + str.gsub(/["\\]/n, "\\\\\\&") + '"') + end + + def send_literal(str, tag = nil) + synchronize do + put_string("{" + str.bytesize.to_s + "}" + CRLF) + @continued_command_tag = tag + @continuation_request_exception = nil + begin + @continuation_request_arrival.wait + e = @continuation_request_exception || @exception + raise e if e + put_string(str) + ensure + @continued_command_tag = nil + @continuation_request_exception = nil + end + end + end + + def send_number_data(num) + put_string(num.to_s) + end + + def send_list_data(list, tag = nil) + put_string("(") + first = true + list.each do |i| + if first + first = false + else + put_string(" ") + end + send_data(i, tag) + end + put_string(")") + end + + DATE_MONTH = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) + + 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 + + def send_symbol_data(symbol) + put_string("\\" + symbol.to_s) + end class RawData # :nodoc: def send_data(imap, tag) |