Skip to content

Commit d3566cb

Browse files
committed
Fix resp_text to match RFC 3501
Specifically, "text" is allowed to begin with "[" or "=". Disallowing that was an older RFC2060 restriction. (RFC 2060 was written in 1996. RFC 3501 in 2003) :)
1 parent f7661a6 commit d3566cb

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

lib/net/imap.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,8 +1774,7 @@ def ensure_mod_sequence_value(num)
17741774
# Net::IMAP::ResponseText represents texts of responses.
17751775
# The text may be prefixed by the response code.
17761776
#
1777-
# resp_text ::= ["[" resp_text_code "]" SPACE] (text_mime2 / text)
1778-
# ;; text SHOULD NOT begin with "[" or "="
1777+
# resp_text ::= ["[" resp-text-code "]" SP] text
17791778
#
17801779
# ==== Fields:
17811780
#
@@ -3132,22 +3131,21 @@ def text
31323131
match(T_TEXT, lex_state: EXPR_TEXT).value
31333132
end
31343133

3134+
# resp-text = ["[" resp-text-code "]" SP] text
31353135
def resp_text
3136-
@lex_state = EXPR_RTEXT
3137-
token = lookahead
3138-
if token.symbol == T_LBRA
3136+
token = match(T_LBRA, T_TEXT, lex_state: EXPR_RTEXT)
3137+
case token.symbol
3138+
when T_LBRA
31393139
code = resp_text_code
3140-
else
3141-
code = nil
3140+
match(T_RBRA)
3141+
accept_space # violating RFC
3142+
ResponseText.new(code, text)
3143+
when T_TEXT
3144+
ResponseText.new(nil, token.value)
31423145
end
3143-
token = match(T_TEXT)
3144-
@lex_state = EXPR_BEG
3145-
return ResponseText.new(code, token.value)
31463146
end
31473147

31483148
def resp_text_code
3149-
@lex_state = EXPR_BEG
3150-
match(T_LBRA)
31513149
token = match(T_ATOM)
31523150
name = token.value.upcase
31533151
case name
@@ -3163,16 +3161,12 @@ def resp_text_code
31633161
token = lookahead
31643162
if token.symbol == T_SPACE
31653163
shift_token
3166-
@lex_state = EXPR_CTEXT
3167-
token = match(T_TEXT)
3168-
@lex_state = EXPR_BEG
3164+
token = match(T_TEXT, lex_state: EXPR_CTEXT)
31693165
result = ResponseCode.new(name, token.value)
31703166
else
31713167
result = ResponseCode.new(name, nil)
31723168
end
31733169
end
3174-
match(T_RBRA)
3175-
@lex_state = EXPR_RTEXT
31763170
return result
31773171
end
31783172

test/net/imap/test_imap_response_parser.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ def test_msg_att_modseq_data
301301
assert_equal(12345, response.data.attr["MODSEQ"])
302302
end
303303

304+
def test_msg_rfc3501_response_text_with_T_LBRA
305+
parser = Net::IMAP::ResponseParser.new
306+
response = parser.parse("RUBY0004 OK [READ-WRITE] [Gmail]/Sent Mail selected. (Success)\r\n")
307+
assert_equal("RUBY0004", response.tag)
308+
assert_equal("READ-WRITE", response.data.code.name)
309+
assert_equal("[Gmail]/Sent Mail selected. (Success)", response.data.text)
310+
end
311+
304312
def test_continuation_request_without_response_text
305313
parser = Net::IMAP::ResponseParser.new
306314
response = parser.parse("+\r\n")

0 commit comments

Comments
 (0)