Skip to content

Commit

Permalink
Merge pull request #4 from mc3/pgp_sig
Browse files Browse the repository at this point in the history
Pgp sig
  • Loading branch information
arnt committed Nov 27, 2015
2 parents f98b6d3 + 81e4b99 commit 850b035
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
10 changes: 10 additions & 0 deletions imap/handlers/fetch.cpp
Expand Up @@ -1345,11 +1345,21 @@ EString Fetch::bodyStructure( Multipart * m, bool extended )

Header * hdr = m->header();
ContentType * ct = hdr->contentType();

bool inMultipartSigned = false;

if ( ct && ct->type() == "multipart" ) {
if ( ct->subtype() == "signed" ) {
inMultipartSigned = true;
}
EStringList children;
List< Bodypart >::Iterator it( m->children() );
while ( it ) {
if ( inMultipartSigned ) {
++it; // skip next child-structure, as we appended it already raw
inMultipartSigned = false;
}
Header * h = it->header();
children.append( bodyStructure( it, extended ) );
++it;
}
Expand Down
16 changes: 14 additions & 2 deletions message/bodypart.cpp
Expand Up @@ -314,8 +314,10 @@ void Bodypart::parseMultipart( uint i, uint end,
const EString & divider,
bool digest,
List< Bodypart > * children,
Multipart * parent )
Multipart * parent,
bool pgpSigned )
{
bool isPgpSigned = pgpSigned;
uint start = 0;
bool last = false;
uint pn = 1;
Expand Down Expand Up @@ -348,6 +350,7 @@ void Bodypart::parseMultipart( uint i, uint end,
if ( rfc2822[j] == 10 )
j++;
if ( start > 0 ) {
uint sigstart = start; // remember original start
Header * h = Message::parseHeader( start, j,
rfc2822,
Header::Mime );
Expand All @@ -362,6 +365,15 @@ void Bodypart::parseMultipart( uint i, uint end,
if ( rfc2822[i-1] == 13 )
i--;
}

if ( isPgpSigned ) {
// store the complete to-be-signed part, incl. header(s), to keep the unchanged version
Bodypart * bpt = new Bodypart( 0, parent );
bpt->setData( rfc2822.mid(sigstart, i - sigstart) );
bpt->setNumBytes( i - sigstart );
children->append( bpt );
isPgpSigned = false;
}

Bodypart * bp =
parseBodypart( start, i, rfc2822, h, parent );
Expand Down Expand Up @@ -764,7 +776,7 @@ Bodypart * Bodypart::parseBodypart( uint start, uint end,
parseMultipart( start, end, rfc2822,
ct->parameter( "boundary" ),
ct->subtype() == "digest",
bp->children(), bp );
bp->children(), bp, false );
}
else if ( ct->type() == "message" && ct->subtype() == "rfc822" ) {
// There are sometimes blank lines before the message.
Expand Down
3 changes: 2 additions & 1 deletion message/bodypart.h
Expand Up @@ -56,7 +56,8 @@ class Bodypart

static void parseMultipart( uint, uint, const EString &,
const EString &, bool,
List< Bodypart > *, Multipart * );
List< Bodypart > *, Multipart *, bool );

private:
class BodypartData * d;
friend class Message;
Expand Down
2 changes: 1 addition & 1 deletion message/header.cpp
Expand Up @@ -1468,7 +1468,7 @@ void Header::repair( Multipart * p, const EString & body )
Bodypart::parseMultipart( 0, body.length(), body,
ct->parameter( "boundary" ),
false,
tmp->children(), tmp );
tmp->children(), tmp, false );
List<Bodypart>::Iterator i( tmp->children() );
Address * postmaster = 0;
while ( i && !postmaster ) {
Expand Down
5 changes: 4 additions & 1 deletion message/message.cpp
Expand Up @@ -91,11 +91,14 @@ void Message::parse( const EString & rfc2822 )
header()->repair( this, rfc2822.mid( i ) );

ContentType * ct = header()->contentType();
bool isPgpSigned = false;
if ( ct && ct->type() == "multipart" ) {
if ( ct->subtype() == "signed" )
isPgpSigned = true;
Bodypart::parseMultipart( i, rfc2822.length(), rfc2822,
ct->parameter( "boundary" ),
ct->subtype() == "digest",
children(), this );
children(), this, isPgpSigned );
}
else {
Bodypart * bp = Bodypart::parseBodypart( i, rfc2822.length(), rfc2822,
Expand Down
20 changes: 15 additions & 5 deletions message/multipart.cpp
Expand Up @@ -91,18 +91,28 @@ void Multipart::appendMultipart( EString &r, bool avoidUtf8 ) const
{
ContentType * ct = header()->contentType();
EString delim = ct->parameter( "boundary" );
bool isSigned = false;
if ( ct->subtype() == "signed" ) {
isSigned = true;
}
List<Bodypart>::Iterator it( children() );
r.append( "--" + delim );
while ( it ) {
r.append( crlf );

Bodypart * bp = it;
++it;

r.append( bp->header()->asText( avoidUtf8 ) );
r.append( crlf );
appendAnyPart( r, bp, ct, avoidUtf8 );


if ( isSigned ) {
++it; // skip next part, we append it raw
isSigned = false;
// just append our raw text, header is contained
appendAnyPart( r, bp, ct, avoidUtf8 );
} else {
r.append( bp->header()->asText( avoidUtf8 ) );
r.append( crlf );
appendAnyPart( r, bp, ct, avoidUtf8 );
}
r.append( crlf );
r.append( "--" );
r.append( delim );
Expand Down

0 comments on commit 850b035

Please sign in to comment.