def message_to_chunks m, encrypted=false, sibling_types=[]
    if m.multipart?
      chunks =
        case m.header.content_type.downcase
        when "multipart/signed"
          multipart_signed_to_chunks m
        when "multipart/encrypted"
          multipart_encrypted_to_chunks m
        end

      unless chunks
        sibling_types = m.body.map { |p| p.header.content_type }
        chunks = m.body.map { |p| message_to_chunks p, encrypted, sibling_types }.flatten.compact
      end

      chunks
    elsif m.header.content_type && m.header.content_type.downcase == "message/rfc822"
      encoding = m.header["Content-Transfer-Encoding"]
      if m.body
        body =
        case encoding
        when "base64"
          m.body.unpack("m")[0]
        when "quoted-printable"
          m.body.unpack("M")[0]
        when "7bit", "8bit", nil
          m.body
        else
          raise RMail::EncodingUnsupportedError, encoding.inspect
        end
        body = body.normalize_whitespace
        payload = RMail::Parser.read(body)
        from = payload.header.from.first ? payload.header.from.first.format : ""
        to = payload.header.to.map { |p| p.format }.join(", ")
        cc = payload.header.cc.map { |p| p.format }.join(", ")
        subj = decode_header_field(payload.header.subject) || DEFAULT_SUBJECT
        subj = Message.normalize_subj(subj.gsub(/\s+/, " ").gsub(/\s+$/, ""))
        msgdate = payload.header.date
        from_person = from ? Person.from_address(decode_header_field(from)) : nil
        to_people = to ? Person.from_address_list(decode_header_field(to)) : nil
        cc_people = cc ? Person.from_address_list(decode_header_field(cc)) : nil
        [Chunk::EnclosedMessage.new(from_person, to_people, cc_people, msgdate, subj)] + message_to_chunks(payload, encrypted)
      else
        debug "no body for message/rfc822 enclosure; skipping"
        []
      end
    elsif m.header.content_type && m.header.content_type.downcase == "application/pgp" && m.body
      ## apparently some versions of Thunderbird generate encryped email that
      ## does not follow RFC3156, e.g. messages with X-Enigmail-Version: 0.95.0
      ## they have no MIME multipart and just set the body content type to
      ## application/pgp. this handles that.
      ##
      ## TODO 1: unduplicate code between here and
      ##         multipart_encrypted_to_chunks
      ## TODO 2: this only tries to decrypt. it cannot handle inline PGP
      notice, sig, decryptedm = CryptoManager.decrypt m.body
      if decryptedm # managed to decrypt
        children = message_to_chunks decryptedm, true
        [notice, sig].compact + children
      else
        ## try inline pgp signed
      	chunks = inline_gpg_to_chunks m.body, $encoding, (m.charset || $encoding)
        if chunks
          chunks
        else
          [notice]
        end
      end
    else
      filename =
        ## first, paw through the headers looking for a filename.
        ## RFC 2183 (Content-Disposition) specifies that disposition-parms are
        ## separated by ";". So, we match everything up to " and ; (if present).
        if m.header["Content-Disposition"] && m.header["Content-Disposition"] =~ /filename="?(.*?[^\\])("|;|\z)/m
          $1
        elsif m.header["Content-Type"] && m.header["Content-Type"] =~ /name="?(.*?[^\\])("|;|\z)/im
          $1

        ## haven't found one, but it's a non-text message. fake
        ## it.
        ##
        ## TODO: make this less lame.
        elsif m.header["Content-Type"] && m.header["Content-Type"] !~ /^text\/plain/i
          extension =
            case m.header["Content-Type"]
            when /text\/html/ then "html"
            when /image\/(.*)/ then $1
            end

          ["sup-attachment-#{Time.now.to_i}-#{rand 10000}", extension].join(".")
        end

      ## if there's a filename, we'll treat it as an attachment.
      if filename
        ## filename could be 2047 encoded
        filename = Rfc2047.decode_to $encoding, filename
        # add this to the attachments list if its not a generated html
        # attachment (should we allow images with generated names?).
        # Lowercase the filename because searches are easier that way
        @attachments.push filename.downcase unless filename =~ /^sup-attachment-/
        add_label :attachment unless filename =~ /^sup-attachment-/
        content_type = (m.header.content_type || "application/unknown").downcase # sometimes RubyMail gives us nil
        [Chunk::Attachment.new(content_type, filename, m, sibling_types)]

      ## otherwise, it's body text
      else
        ## Decode the body, charset conversion will follow either in
        ## inline_gpg_to_chunks (for inline GPG signed messages) or
        ## a few lines below (messages without inline GPG)
        body = m.body ? m.decode : ""

        ## Check for inline-PGP
        chunks = inline_gpg_to_chunks body, $encoding, (m.charset || $encoding)
        return chunks if chunks

        if m.body
          ## if there's no charset, use the current encoding as the charset.
          ## this ensures that the body is normalized to avoid non-displayable
          ## characters
          body = m.decode.transcode($encoding, m.charset)
        else
          body = ""
        end

        text_to_chunks(body.normalize_whitespace.split("\n"), encrypted)
      end
    end
  end
def message_to_chunks m, encrypted=false, sibling_types=[]
    if m.multipart?
      chunks =
        case m.header.content_type.downcase
        when "multipart/signed"
          multipart_signed_to_chunks m
        when "multipart/encrypted"
          multipart_encrypted_to_chunks m
        end

      unless chunks
        sibling_types = m.body.map { |p| p.header.content_type }
        chunks = m.body.map { |p| message_to_chunks p, encrypted, sibling_types }.flatten.compact
      end

      chunks
    elsif m.header.content_type && m.header.content_type.downcase == "message/rfc822"
      encoding = m.header["Content-Transfer-Encoding"]
      if m.body
        body =
        case encoding
        when "base64"
          m.body.unpack("m")[0]
        when "quoted-printable"
          m.body.unpack("M")[0]
        when "7bit", "8bit", nil
          m.body
        else
          raise RMail::EncodingUnsupportedError, encoding.inspect
        end
        body = body.normalize_whitespace
        payload = RMail::Parser.read(body)
        from = payload.header.from.first ? payload.header.from.first.format : ""
        to = payload.header.to.map { |p| p.format }.join(", ")
        cc = payload.header.cc.map { |p| p.format }.join(", ")
        subj = decode_header_field(payload.header.subject) || DEFAULT_SUBJECT
        subj = Message.normalize_subj(subj.gsub(/\s+/, " ").gsub(/\s+$/, ""))
        msgdate = payload.header.date
        from_person = from ? Person.from_address(decode_header_field(from)) : nil
        to_people = to ? Person.from_address_list(decode_header_field(to)) : nil
        cc_people = cc ? Person.from_address_list(decode_header_field(cc)) : nil
        [Chunk::EnclosedMessage.new(from_person, to_people, cc_people, msgdate, subj)] + message_to_chunks(payload, encrypted)
      else
        debug "no body for message/rfc822 enclosure; skipping"
        []
      end
    elsif m.header.content_type && m.header.content_type.downcase == "application/pgp" && m.body
      ## apparently some versions of Thunderbird generate encryped email that
      ## does not follow RFC3156, e.g. messages with X-Enigmail-Version: 0.95.0
      ## they have no MIME multipart and just set the body content type to
      ## application/pgp. this handles that.
      ##
      ## TODO 1: unduplicate code between here and
      ##         multipart_encrypted_to_chunks
      ## TODO 2: this only tries to decrypt. it cannot handle inline PGP
      notice, sig, decryptedm = CryptoManager.decrypt m.body
      if decryptedm # managed to decrypt
        children = message_to_chunks decryptedm, true
        [notice, sig].compact + children
      else
        ## try inline pgp signed
      	chunks = inline_gpg_to_chunks m.body, $encoding, (m.charset || $encoding)
        if chunks
          chunks
        else
          [notice]
        end
      end
    else
      filename =
        ## first, paw through the headers looking for a filename.
        ## RFC 2183 (Content-Disposition) specifies that disposition-parms are
        ## separated by ";". So, we match everything up to " and ; (if present).
        if m.header["Content-Disposition"] && m.header["Content-Disposition"] =~ /filename="?(.*?[^\\])("|;|\z)/m
          $1
        elsif m.header["Content-Type"] && m.header["Content-Type"] =~ /name="?(.*?[^\\])("|;|\z)/im
          $1

        ## haven't found one, but it's a non-text message. fake
        ## it.
        ##
        ## TODO: make this less lame.
        elsif m.header["Content-Type"] && m.header["Content-Type"] !~ /^text\/plain/i
          extension =
            case m.header["Content-Type"]
            when /text\/html/ then "html"
            when /image\/(.*)/ then $1
            end

          ["sup-attachment-#{Time.now.to_i}-#{rand 10000}", extension].join(".")
        end

      ## if there's a filename, we'll treat it as an attachment.
      if filename
        ## filename could be 2047 encoded
        filename = Rfc2047.decode_to $encoding, filename
        # add this to the attachments list if its not a generated html
        # attachment (should we allow images with generated names?).
        # Lowercase the filename because searches are easier that way
        @attachments.push filename.downcase unless filename =~ /^sup-attachment-/
        add_label :attachment unless filename =~ /^sup-attachment-/
        content_type = (m.header.content_type || "application/unknown").downcase # sometimes RubyMail gives us nil
        [Chunk::Attachment.new(content_type, filename, m, sibling_types)]

      ## otherwise, it's body text
      else
        ## Decode the body, charset conversion will follow either in
        ## inline_gpg_to_chunks (for inline GPG signed messages) or
        ## a few lines below (messages without inline GPG)
        body = m.body ? m.decode : ""

        ## Check for inline-PGP
        chunks = inline_gpg_to_chunks body, $encoding, (m.charset || $encoding)
        return chunks if chunks

        if m.body
          ## if there's no charset, use the current encoding as the charset.
          ## this ensures that the body is normalized to avoid non-displayable
          ## characters
          body = m.decode.transcode($encoding, m.charset)
        else
          body = ""
        end

        text_to_chunks(body.normalize_whitespace.split("\n"), encrypted)
      end
    end
  end
takes a RMail::Message, breaks it into Chunk:: classes.