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?

···

--
Posted via http://www.ruby-forum.com/.

Heres an example message that triggers it.

Attachments:
http://www.ruby-forum.com/attachment/4953/message.txt

···

--
Posted via http://www.ruby-forum.com/.

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:in
`match'
  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:in
`media_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:in
`body_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:in
`body_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:in
`body_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:in
`numeric_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:in
`response'
  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:in
`get_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:in
`initialize'
  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:in
`initialize'
  from (irb):3:in `new'
  from (irb):3

···

--
Posted via http://www.ruby-forum.com/.