IMAP: unexpected token LPAR (expected QUOTED or LITERAL)

I’m writing an IMAP parser that uses BODYSTRUCTURE to fetch only the
text (not attachments) part of an email. On a few messages, ruby errors
out on imap.fetch id, “BODYSTRUCTURE”, with
“Net::IMAP::ResponseParseError: unexpected token LPAR (expected QUOTED
or LITERAL)”. I would just rescue it, but it breaks the connection as
well, raising “OpenSSL::SSL::SSLError: SSL_write:: unexpected record”
for any further fetches.

The only two other topics on this I have found have patches, but for
earlier versions of ruby, which I have been unable to adapt.
http://rubyforge.org/tracker/index.php?func=detail&aid=3983&group_id=426&atid=1698
https://www.ruby-forum.com/topic/87177

Help?

Heres an example message that triggers it.

Heres the full trace, with Net::IMAP.debug = true

C: RUBY0003 FETCH 3398 bodystructure
S: * 3398 FETCH (BODYSTRUCTURE ((“ALTERNATIVE” (“BOUNDARY”
“==alternative_xad5934455aeex”) NIL NIL)(“TEXT” “HTML” (“CHARSET”
“iso-8859-1” “NAME” “seti_letter.html”) NIL NIL “7BIT” 6777 171 NIL NIL
NIL) “ALTERNATIVE” (“BOUNDARY” “==alternative_xad5934455aeex”) NIL NIL))
@str: “* 3398 FETCH (BODYSTRUCTURE ((“ALTERNATIVE” (“BOUNDARY”
“==alternative_xad5934455aeex”) NIL NIL)(“TEXT” “HTML”
(“CHARSET” “iso-8859-1” “NAME” “seti_letter.html”) NIL NIL
“7BIT” 6777 171 NIL NIL NIL) “ALTERNATIVE” (“BOUNDARY”
“==alternative_xad5934455aeex”) NIL NIL))\r\n”
@pos: 45
@lex_state: EXPR_DATA
@token.symbol: LPAR
@token.value: “(”
Net::IMAP::ResponseParseError: unexpected token LPAR (expected QUOTED or
LITERAL)
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:3156:in
parse_error' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:3008:inmatch’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2956:in
case_insensitive_string' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2286:inmedia_type’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2219:in
body_type_basic' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2214:inbody_type_1part’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2198:in
body' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2273:inbody_type_mpart’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2196:in
body' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2173:inbody_data’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2085:in
msg_att' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:2055:innumeric_response’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1997:in
response_untagged' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1977:inresponse’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1903:in
parse' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:1028:inget_response’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:936:in
receive_responses' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:926:ininitialize’
from
/Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:925:in
start' from /Users/shea/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/1.8/net/imap.rb:925:ininitialize’
from (irb):3:in `new’
from (irb):3