You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
746 lines
20 KiB
746 lines
20 KiB
15 years ago
|
/***************************************************************************
|
||
|
mimeheader.cc - description
|
||
|
-------------------
|
||
|
begin : Fri Oct 20 2000
|
||
|
copyright : (C) 2000 by Sven Carstens
|
||
|
email : s.carstens@gmx.de
|
||
|
***************************************************************************/
|
||
|
|
||
|
/***************************************************************************
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
* *
|
||
|
***************************************************************************/
|
||
|
|
||
|
#include "mimeheader.h"
|
||
|
#include "mimehdrline.h"
|
||
|
#include "mailheader.h"
|
||
|
#include "rfcdecoder.h"
|
||
|
|
||
15 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
// #include <iostream.h>
|
||
12 years ago
|
#include <tdeglobal.h>
|
||
15 years ago
|
#include <kinstance.h>
|
||
|
#include <kiconloader.h>
|
||
|
#include <kmimetype.h>
|
||
|
#include <kmimemagic.h>
|
||
|
#include <kmdcodec.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
mimeHeader::mimeHeader ():
|
||
|
typeList (17, false), dispositionList (17, false)
|
||
|
{
|
||
|
// Case insensitive hashes are killing us. Also are they too small?
|
||
|
originalHdrLines.setAutoDelete (true);
|
||
|
additionalHdrLines.setAutoDelete (false); // is also in original lines
|
||
|
nestedParts.setAutoDelete (true);
|
||
|
typeList.setAutoDelete (true);
|
||
|
dispositionList.setAutoDelete (true);
|
||
|
nestedMessage = NULL;
|
||
|
contentLength = 0;
|
||
|
contentType = "application/octet-stream";
|
||
|
}
|
||
|
|
||
|
mimeHeader::~mimeHeader ()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/*
|
||
15 years ago
|
TQPtrList<mimeHeader> mimeHeader::getAllParts()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPtrList<mimeHeader> retVal;
|
||
15 years ago
|
|
||
|
// caller is responsible for clearing
|
||
|
retVal.setAutoDelete( false );
|
||
|
nestedParts.setAutoDelete( false );
|
||
|
|
||
|
// shallow copy
|
||
|
retVal = nestedParts;
|
||
|
|
||
|
// can't have duplicate pointers
|
||
|
nestedParts.clear();
|
||
|
|
||
|
// restore initial state
|
||
|
nestedParts.setAutoDelete( true );
|
||
|
|
||
|
return retVal;
|
||
|
} */
|
||
|
|
||
|
void
|
||
|
mimeHeader::addHdrLine (mimeHdrLine * aHdrLine)
|
||
|
{
|
||
|
mimeHdrLine *addLine = new mimeHdrLine (aHdrLine);
|
||
|
if (addLine)
|
||
|
{
|
||
|
originalHdrLines.append (addLine);
|
||
13 years ago
|
if (tqstrnicmp (addLine->getLabel (), "Content-", 8))
|
||
15 years ago
|
{
|
||
|
additionalHdrLines.append (addLine);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int skip;
|
||
14 years ago
|
const char *aCStr = addLine->getValue ().data ();
|
||
15 years ago
|
TQDict < TQString > *aList = 0;
|
||
15 years ago
|
|
||
|
skip = mimeHdrLine::parseSeparator (';', aCStr);
|
||
|
if (skip > 0)
|
||
|
{
|
||
|
int cut = 0;
|
||
|
if (skip >= 2)
|
||
|
{
|
||
|
if (aCStr[skip - 1] == '\r')
|
||
|
cut++;
|
||
|
if (aCStr[skip - 1] == '\n')
|
||
|
cut++;
|
||
|
if (aCStr[skip - 2] == '\r')
|
||
|
cut++;
|
||
|
if (aCStr[skip - 1] == ';')
|
||
|
cut++;
|
||
|
}
|
||
15 years ago
|
TQCString mimeValue = TQCString (aCStr, skip - cut + 1); // cutting of one because of 0x00
|
||
15 years ago
|
|
||
|
|
||
13 years ago
|
if (!tqstricmp (addLine->getLabel (), "Content-Disposition"))
|
||
15 years ago
|
{
|
||
|
aList = &dispositionList;
|
||
|
_contentDisposition = mimeValue;
|
||
|
}
|
||
13 years ago
|
else if (!tqstricmp (addLine->getLabel (), "Content-Type"))
|
||
15 years ago
|
{
|
||
|
aList = &typeList;
|
||
|
contentType = mimeValue;
|
||
|
}
|
||
|
else
|
||
13 years ago
|
if (!tqstricmp (addLine->getLabel (), "Content-Transfer-Encoding"))
|
||
15 years ago
|
{
|
||
|
contentEncoding = mimeValue;
|
||
|
}
|
||
13 years ago
|
else if (!tqstricmp (addLine->getLabel (), "Content-ID"))
|
||
15 years ago
|
{
|
||
|
contentID = mimeValue;
|
||
|
}
|
||
13 years ago
|
else if (!tqstricmp (addLine->getLabel (), "Content-Description"))
|
||
15 years ago
|
{
|
||
|
_contentDescription = mimeValue;
|
||
|
}
|
||
13 years ago
|
else if (!tqstricmp (addLine->getLabel (), "Content-MD5"))
|
||
15 years ago
|
{
|
||
|
contentMD5 = mimeValue;
|
||
|
}
|
||
13 years ago
|
else if (!tqstricmp (addLine->getLabel (), "Content-Length"))
|
||
15 years ago
|
{
|
||
|
contentLength = mimeValue.toULong ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
additionalHdrLines.append (addLine);
|
||
|
}
|
||
|
// cout << addLine->getLabel().data() << ": '" << mimeValue.data() << "'" << endl;
|
||
|
|
||
|
aCStr += skip;
|
||
|
while ((skip = mimeHdrLine::parseSeparator (';', aCStr)))
|
||
|
{
|
||
|
if (skip > 0)
|
||
|
{
|
||
15 years ago
|
addParameter (TQCString (aCStr, skip).simplifyWhiteSpace(), aList);
|
||
15 years ago
|
// cout << "-- '" << aParm.data() << "'" << endl;
|
||
15 years ago
|
mimeValue = TQCString (addLine->getValue ().data (), skip);
|
||
15 years ago
|
aCStr += skip;
|
||
|
}
|
||
|
else
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::addParameter (const TQCString& aParameter, TQDict < TQString > *aList)
|
||
15 years ago
|
{
|
||
|
if ( !aList )
|
||
|
return;
|
||
|
|
||
15 years ago
|
TQString *aValue;
|
||
|
TQCString aLabel;
|
||
13 years ago
|
int pos = aParameter.find ('=');
|
||
15 years ago
|
// cout << aParameter.left(pos).data();
|
||
15 years ago
|
aValue = new TQString ();
|
||
15 years ago
|
aValue->setLatin1 (aParameter.right (aParameter.length () - pos - 1));
|
||
|
aLabel = aParameter.left (pos);
|
||
|
if ((*aValue)[0] == '"')
|
||
|
*aValue = aValue->mid (1, aValue->length () - 2);
|
||
|
|
||
|
aList->insert (aLabel, aValue);
|
||
|
// cout << "=" << aValue->data() << endl;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::getDispositionParm (const TQCString& aStr)
|
||
15 years ago
|
{
|
||
|
return getParameter (aStr, &dispositionList);
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::getTypeParm (const TQCString& aStr)
|
||
15 years ago
|
{
|
||
|
return getParameter (aStr, &typeList);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::setDispositionParm (const TQCString& aLabel, const TQString& aValue)
|
||
15 years ago
|
{
|
||
|
setParameter (aLabel, aValue, &dispositionList);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::setTypeParm (const TQCString& aLabel, const TQString& aValue)
|
||
15 years ago
|
{
|
||
|
setParameter (aLabel, aValue, &typeList);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQDictIterator < TQString > mimeHeader::getDispositionIterator ()
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQDictIterator < TQString > (dispositionList);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQDictIterator < TQString > mimeHeader::getTypeIterator ()
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQDictIterator < TQString > (typeList);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQPtrListIterator < mimeHdrLine > mimeHeader::getOriginalIterator ()
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQPtrListIterator < mimeHdrLine > (originalHdrLines);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQPtrListIterator < mimeHdrLine > mimeHeader::getAdditionalIterator ()
|
||
15 years ago
|
{
|
||
15 years ago
|
return TQPtrListIterator < mimeHdrLine > (additionalHdrLines);
|
||
15 years ago
|
}
|
||
|
|
||
|
void
|
||
|
mimeHeader::outputHeader (mimeIO & useIO)
|
||
|
{
|
||
|
if (!getDisposition ().isEmpty ())
|
||
|
{
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-Disposition: ")
|
||
15 years ago
|
+ getDisposition ()
|
||
|
+ outputParameter (&dispositionList));
|
||
|
}
|
||
|
|
||
|
if (!getType ().isEmpty ())
|
||
|
{
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-Type: ")
|
||
15 years ago
|
+ getType () + outputParameter (&typeList));
|
||
|
}
|
||
|
if (!getDescription ().isEmpty ())
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-Description: ") +
|
||
15 years ago
|
getDescription ());
|
||
|
if (!getID ().isEmpty ())
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-ID: ") + getID ());
|
||
15 years ago
|
if (!getMD5 ().isEmpty ())
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-MD5: ") + getMD5 ());
|
||
15 years ago
|
if (!getEncoding ().isEmpty ())
|
||
15 years ago
|
useIO.outputMimeLine (TQCString ("Content-Transfer-Encoding: ") +
|
||
15 years ago
|
getEncoding ());
|
||
|
|
||
15 years ago
|
TQPtrListIterator < mimeHdrLine > ait = getAdditionalIterator ();
|
||
15 years ago
|
while (ait.current ())
|
||
|
{
|
||
|
useIO.outputMimeLine (ait.current ()->getLabel () + ": " +
|
||
|
ait.current ()->getValue ());
|
||
|
++ait;
|
||
|
}
|
||
15 years ago
|
useIO.outputMimeLine (TQCString (""));
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::getParameter (const TQCString& aStr, TQDict < TQString > *aDict)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString retVal, *found;
|
||
15 years ago
|
if (aDict)
|
||
|
{
|
||
|
//see if it is a normal parameter
|
||
13 years ago
|
found = aDict->find (aStr);
|
||
15 years ago
|
if (!found)
|
||
|
{
|
||
|
//might be a continuated or encoded parameter
|
||
13 years ago
|
found = aDict->find (aStr + "*");
|
||
15 years ago
|
if (!found)
|
||
|
{
|
||
|
//continuated parameter
|
||
15 years ago
|
TQString decoded, encoded;
|
||
15 years ago
|
int part = 0;
|
||
|
|
||
|
do
|
||
|
{
|
||
15 years ago
|
TQCString search;
|
||
15 years ago
|
search.setNum (part);
|
||
|
search = aStr + "*" + search;
|
||
13 years ago
|
found = aDict->find (search);
|
||
15 years ago
|
if (!found)
|
||
|
{
|
||
13 years ago
|
found = aDict->find (search + "*");
|
||
15 years ago
|
if (found)
|
||
|
encoded += rfcDecoder::encodeRFC2231String (*found);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
encoded += *found;
|
||
|
}
|
||
|
part++;
|
||
|
}
|
||
|
while (found);
|
||
13 years ago
|
if (encoded.find ('\'') >= 0)
|
||
15 years ago
|
{
|
||
|
retVal = rfcDecoder::decodeRFC2231String (encoded.local8Bit ());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
retVal =
|
||
15 years ago
|
rfcDecoder::decodeRFC2231String (TQCString ("''") +
|
||
15 years ago
|
encoded.local8Bit ());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
//simple encoded parameter
|
||
|
retVal = rfcDecoder::decodeRFC2231String (found->local8Bit ());
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
retVal = *found;
|
||
|
}
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::setParameter (const TQCString& aLabel, const TQString& aValue,
|
||
|
TQDict < TQString > *aDict)
|
||
15 years ago
|
{
|
||
|
bool encoded = true;
|
||
|
uint vlen, llen;
|
||
15 years ago
|
TQString val = aValue;
|
||
15 years ago
|
|
||
|
if (aDict)
|
||
|
{
|
||
|
|
||
|
//see if it needs to get encoded
|
||
13 years ago
|
if (encoded && aLabel.find ('*') == -1)
|
||
15 years ago
|
{
|
||
|
val = rfcDecoder::encodeRFC2231String (aValue);
|
||
|
}
|
||
|
//kdDebug(7116) << "mimeHeader::setParameter() - val = '" << val << "'" << endl;
|
||
|
//see if it needs to be truncated
|
||
|
vlen = val.length();
|
||
|
llen = aLabel.length();
|
||
|
if (vlen + llen + 4 > 80 && llen < 80 - 8 - 2 )
|
||
|
{
|
||
|
const int limit = 80 - 8 - 2 - (int)llen;
|
||
|
// the -2 is there to allow extending the length of a part of val
|
||
|
// by 1 or 2 in order to prevent an encoded character from being
|
||
|
// split in half
|
||
|
int i = 0;
|
||
15 years ago
|
TQString shortValue;
|
||
|
TQCString shortLabel;
|
||
15 years ago
|
|
||
|
while (!val.isEmpty ())
|
||
|
{
|
||
|
int partLen; // the length of the next part of the value
|
||
|
if ( limit >= int(vlen) ) {
|
||
|
// the rest of the value fits completely into one continued header
|
||
|
partLen = vlen;
|
||
|
}
|
||
|
else {
|
||
|
partLen = limit;
|
||
|
// make sure that we don't split an encoded char in half
|
||
|
if ( val[partLen-1] == '%' ) {
|
||
|
partLen += 2;
|
||
|
}
|
||
|
else if ( partLen > 1 && val[partLen-2] == '%' ) {
|
||
|
partLen += 1;
|
||
|
}
|
||
|
// make sure partLen does not exceed vlen (could happen in case of
|
||
|
// an incomplete encoded char)
|
||
|
if ( partLen > int(vlen) ) {
|
||
|
partLen = vlen;
|
||
|
}
|
||
|
}
|
||
|
shortValue = val.left( partLen );
|
||
|
shortLabel.setNum (i);
|
||
|
shortLabel = aLabel + "*" + shortLabel;
|
||
|
val = val.right( vlen - partLen );
|
||
|
vlen = vlen - partLen;
|
||
|
if (encoded)
|
||
|
{
|
||
|
if (i == 0)
|
||
|
{
|
||
|
shortValue = "''" + shortValue;
|
||
|
}
|
||
|
shortLabel += "*";
|
||
|
}
|
||
|
//kdDebug(7116) << "mimeHeader::setParameter() - shortLabel = '" << shortLabel << "'" << endl;
|
||
|
//kdDebug(7116) << "mimeHeader::setParameter() - shortValue = '" << shortValue << "'" << endl;
|
||
|
//kdDebug(7116) << "mimeHeader::setParameter() - val = '" << val << "'" << endl;
|
||
15 years ago
|
aDict->insert (shortLabel, new TQString (shortValue));
|
||
15 years ago
|
i++;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
15 years ago
|
aDict->insert (aLabel, new TQString (val));
|
||
15 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
TQCString
|
||
15 years ago
|
mimeHeader::outputParameter (TQDict < TQString > *aDict)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString retVal;
|
||
15 years ago
|
if (aDict)
|
||
|
{
|
||
15 years ago
|
TQDictIterator < TQString > it (*aDict);
|
||
15 years ago
|
while (it.current ())
|
||
|
{
|
||
|
retVal += (";\n\t" + it.currentKey () + "=").latin1 ();
|
||
13 years ago
|
if (it.current ()->find (' ') > 0 || it.current ()->find (';') > 0)
|
||
15 years ago
|
{
|
||
|
retVal += '"' + it.current ()->utf8 () + '"';
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
retVal += it.current ()->utf8 ();
|
||
|
}
|
||
|
// << it.current()->utf8() << "'";
|
||
|
++it;
|
||
|
}
|
||
|
retVal += "\n";
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
mimeHeader::outputPart (mimeIO & useIO)
|
||
|
{
|
||
15 years ago
|
TQPtrListIterator < mimeHeader > nestedParts = getNestedIterator ();
|
||
|
TQCString boundary;
|
||
15 years ago
|
if (!getTypeParm ("boundary").isEmpty ())
|
||
|
boundary = getTypeParm ("boundary").latin1 ();
|
||
|
|
||
|
outputHeader (useIO);
|
||
|
if (!getPreBody ().isEmpty ())
|
||
|
useIO.outputMimeLine (getPreBody ());
|
||
|
if (getNestedMessage ())
|
||
|
getNestedMessage ()->outputPart (useIO);
|
||
|
while (nestedParts.current ())
|
||
|
{
|
||
|
if (!boundary.isEmpty ())
|
||
|
useIO.outputMimeLine ("--" + boundary);
|
||
|
nestedParts.current ()->outputPart (useIO);
|
||
|
++nestedParts;
|
||
|
}
|
||
|
if (!boundary.isEmpty ())
|
||
|
useIO.outputMimeLine ("--" + boundary + "--");
|
||
|
if (!getPostBody ().isEmpty ())
|
||
|
useIO.outputMimeLine (getPostBody ());
|
||
|
}
|
||
|
|
||
|
int
|
||
15 years ago
|
mimeHeader::parsePart (mimeIO & useIO, const TQString& boundary)
|
||
15 years ago
|
{
|
||
|
int retVal = 0;
|
||
|
bool mbox = false;
|
||
15 years ago
|
TQCString preNested, postNested;
|
||
15 years ago
|
mbox = parseHeader (useIO);
|
||
|
|
||
|
kdDebug(7116) << "mimeHeader::parsePart - parsing part '" << getType () << "'" << endl;
|
||
13 years ago
|
if (!tqstrnicmp (getType (), "Multipart", 9))
|
||
15 years ago
|
{
|
||
|
retVal = parseBody (useIO, preNested, getTypeParm ("boundary")); //this is a message in mime format stuff
|
||
|
setPreBody (preNested);
|
||
|
int localRetVal;
|
||
|
do
|
||
|
{
|
||
|
mimeHeader *aHeader = new mimeHeader;
|
||
|
|
||
|
// set default type for multipart/digest
|
||
13 years ago
|
if (!tqstrnicmp (getType (), "Multipart/Digest", 16))
|
||
15 years ago
|
aHeader->setType ("Message/RFC822");
|
||
|
|
||
|
localRetVal = aHeader->parsePart (useIO, getTypeParm ("boundary"));
|
||
|
addNestedPart (aHeader);
|
||
|
}
|
||
|
while (localRetVal); //get nested stuff
|
||
|
}
|
||
13 years ago
|
if (!tqstrnicmp (getType (), "Message/RFC822", 14))
|
||
15 years ago
|
{
|
||
|
mailHeader *msgHeader = new mailHeader;
|
||
|
retVal = msgHeader->parsePart (useIO, boundary);
|
||
|
setNestedMessage (msgHeader);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
retVal = parseBody (useIO, postNested, boundary, mbox); //just a simple part remaining
|
||
|
setPostBody (postNested);
|
||
|
}
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
int
|
||
15 years ago
|
mimeHeader::parseBody (mimeIO & useIO, TQCString & messageBody,
|
||
|
const TQString& boundary, bool mbox)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQCString inputStr;
|
||
|
TQCString buffer;
|
||
|
TQString partBoundary;
|
||
|
TQString partEnd;
|
||
15 years ago
|
int retVal = 0; //default is last part
|
||
|
|
||
|
if (!boundary.isEmpty ())
|
||
|
{
|
||
15 years ago
|
partBoundary = TQString ("--") + boundary;
|
||
|
partEnd = TQString ("--") + boundary + "--";
|
||
15 years ago
|
}
|
||
|
|
||
|
while (useIO.inputLine (inputStr))
|
||
|
{
|
||
|
//check for the end of all parts
|
||
|
if (!partEnd.isEmpty ()
|
||
13 years ago
|
&& !tqstrnicmp (inputStr, partEnd.latin1 (), partEnd.length () - 1))
|
||
15 years ago
|
{
|
||
|
retVal = 0; //end of these parts
|
||
|
break;
|
||
|
}
|
||
|
else if (!partBoundary.isEmpty ()
|
||
13 years ago
|
&& !tqstrnicmp (inputStr, partBoundary.latin1 (),
|
||
15 years ago
|
partBoundary.length () - 1))
|
||
|
{
|
||
|
retVal = 1; //continue with next part
|
||
|
break;
|
||
|
}
|
||
13 years ago
|
else if (mbox && inputStr.find ("From ") == 0)
|
||
15 years ago
|
{
|
||
|
retVal = 0; // end of mbox
|
||
|
break;
|
||
|
}
|
||
|
buffer += inputStr;
|
||
|
if (buffer.length () > 16384)
|
||
|
{
|
||
|
messageBody += buffer;
|
||
|
buffer = "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
messageBody += buffer;
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
mimeHeader::parseHeader (mimeIO & useIO)
|
||
|
{
|
||
|
bool mbox = false;
|
||
|
bool first = true;
|
||
|
mimeHdrLine my_line;
|
||
15 years ago
|
TQCString inputStr;
|
||
15 years ago
|
|
||
|
kdDebug(7116) << "mimeHeader::parseHeader - starting parsing" << endl;
|
||
|
while (useIO.inputLine (inputStr))
|
||
|
{
|
||
|
int appended;
|
||
13 years ago
|
if (inputStr.find ("From ") != 0 || !first)
|
||
15 years ago
|
{
|
||
|
first = false;
|
||
|
appended = my_line.appendStr (inputStr);
|
||
|
if (!appended)
|
||
|
{
|
||
|
addHdrLine (&my_line);
|
||
|
appended = my_line.setStr (inputStr);
|
||
|
}
|
||
|
if (appended <= 0)
|
||
|
break;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
mbox = true;
|
||
|
first = false;
|
||
|
}
|
||
|
inputStr = (const char *) NULL;
|
||
|
}
|
||
|
|
||
|
kdDebug(7116) << "mimeHeader::parseHeader - finished parsing" << endl;
|
||
|
return mbox;
|
||
|
}
|
||
|
|
||
|
mimeHeader *
|
||
15 years ago
|
mimeHeader::bodyPart (const TQString & _str)
|
||
15 years ago
|
{
|
||
|
// see if it is nested a little deeper
|
||
13 years ago
|
int pt = _str.find('.');
|
||
15 years ago
|
if (pt != -1)
|
||
|
{
|
||
15 years ago
|
TQString tempStr = _str;
|
||
15 years ago
|
mimeHeader *tempPart;
|
||
|
|
||
|
tempStr = _str.right (_str.length () - pt - 1);
|
||
|
if (nestedMessage)
|
||
|
{
|
||
|
kdDebug(7116) << "mimeHeader::bodyPart - recursing message" << endl;
|
||
|
tempPart = nestedMessage->nestedParts.at (_str.left(pt).toULong() - 1);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
kdDebug(7116) << "mimeHeader::bodyPart - recursing mixed" << endl;
|
||
|
tempPart = nestedParts.at (_str.left(pt).toULong() - 1);
|
||
|
}
|
||
|
if (tempPart)
|
||
|
tempPart = tempPart->bodyPart (tempStr);
|
||
|
return tempPart;
|
||
|
}
|
||
|
|
||
|
kdDebug(7116) << "mimeHeader::bodyPart - returning part " << _str << endl;
|
||
|
// or pick just the plain part
|
||
|
if (nestedMessage)
|
||
|
{
|
||
|
kdDebug(7116) << "mimeHeader::bodyPart - message" << endl;
|
||
|
return nestedMessage->nestedParts.at (_str.toULong () - 1);
|
||
|
}
|
||
|
kdDebug(7116) << "mimeHeader::bodyPart - mixed" << endl;
|
||
|
return nestedParts.at (_str.toULong () - 1);
|
||
|
}
|
||
|
|
||
15 years ago
|
void mimeHeader::serialize(TQDataStream& stream)
|
||
15 years ago
|
{
|
||
|
int nestedcount = nestedParts.count();
|
||
|
if (nestedParts.isEmpty() && nestedMessage)
|
||
|
nestedcount = 1;
|
||
15 years ago
|
stream << nestedcount << contentType << TQString (getTypeParm ("name")) << _contentDescription
|
||
15 years ago
|
<< _contentDisposition << contentEncoding << contentLength << partSpecifier;
|
||
|
// serialize nested message
|
||
|
if (nestedMessage)
|
||
|
nestedMessage->serialize(stream);
|
||
|
|
||
|
// serialize nested parts
|
||
|
if (!nestedParts.isEmpty())
|
||
|
{
|
||
15 years ago
|
TQPtrListIterator < mimeHeader > it(nestedParts);
|
||
15 years ago
|
mimeHeader* part;
|
||
|
while ( (part = it.current()) != 0 )
|
||
|
{
|
||
|
++it;
|
||
|
part->serialize(stream);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#ifdef KMAIL_COMPATIBLE
|
||
|
// compatibility subroutines
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::bodyDecoded ()
|
||
|
{
|
||
|
kdDebug(7116) << "mimeHeader::bodyDecoded" << endl;
|
||
15 years ago
|
TQByteArray temp;
|
||
15 years ago
|
|
||
|
temp = bodyDecodedBinary ();
|
||
13 years ago
|
return TQString::fromLatin1 (temp.data (), temp.count ());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQByteArray
|
||
15 years ago
|
mimeHeader::bodyDecodedBinary ()
|
||
|
{
|
||
15 years ago
|
TQByteArray retVal;
|
||
15 years ago
|
|
||
13 years ago
|
if (contentEncoding.find ("quoted-printable", 0, false) == 0)
|
||
15 years ago
|
retVal = KCodecs::quotedPrintableDecode(postMultipartBody);
|
||
13 years ago
|
else if (contentEncoding.find ("base64", 0, false) == 0)
|
||
15 years ago
|
KCodecs::base64Decode(postMultipartBody, retVal);
|
||
|
else retVal = postMultipartBody;
|
||
|
|
||
|
kdDebug(7116) << "mimeHeader::bodyDecodedBinary - size is " << retVal.size () << endl;
|
||
|
return retVal;
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::setBodyEncodedBinary (const TQByteArray & _arr)
|
||
15 years ago
|
{
|
||
|
setBodyEncoded (_arr);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
mimeHeader::setBodyEncoded (const TQByteArray & _arr)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQByteArray setVal;
|
||
15 years ago
|
|
||
|
kdDebug(7116) << "mimeHeader::setBodyEncoded - in size " << _arr.size () << endl;
|
||
13 years ago
|
if (contentEncoding.find ("quoted-printable", 0, false) == 0)
|
||
15 years ago
|
setVal = KCodecs::quotedPrintableEncode(_arr);
|
||
13 years ago
|
else if (contentEncoding.find ("base64", 0, false) == 0)
|
||
15 years ago
|
KCodecs::base64Encode(_arr, setVal);
|
||
|
else
|
||
|
setVal.duplicate (_arr);
|
||
|
kdDebug(7116) << "mimeHeader::setBodyEncoded - out size " << setVal.size () << endl;
|
||
|
|
||
|
postMultipartBody.duplicate (setVal);
|
||
|
kdDebug(7116) << "mimeHeader::setBodyEncoded - out size " << postMultipartBody.size () << endl;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::iconName ()
|
||
|
{
|
||
15 years ago
|
TQString fileName;
|
||
15 years ago
|
|
||
|
// FIXME: bug? Why throw away this data?
|
||
|
fileName =
|
||
14 years ago
|
KMimeType::mimeType (contentType.lower ())->icon (TQString(), false);
|
||
15 years ago
|
fileName =
|
||
12 years ago
|
TDEGlobal::instance ()->iconLoader ()->iconPath (fileName, TDEIcon::Desktop);
|
||
15 years ago
|
// if (fileName.isEmpty())
|
||
12 years ago
|
// fileName = TDEGlobal::instance()->iconLoader()->iconPath( "unknown", TDEIcon::Desktop );
|
||
15 years ago
|
return fileName;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
mimeHeader::setNestedMessage (mailHeader * inPart, bool destroy)
|
||
|
{
|
||
|
// if(nestedMessage && destroy) delete nestedMessage;
|
||
|
nestedMessage = inPart;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::headerAsString ()
|
||
|
{
|
||
14 years ago
|
mimeIOTQString myIO;
|
||
15 years ago
|
|
||
|
outputHeader (myIO);
|
||
|
return myIO.getString ();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString
|
||
15 years ago
|
mimeHeader::magicSetType (bool aAutoDecode)
|
||
|
{
|
||
15 years ago
|
TQString mimetype;
|
||
|
TQByteArray body;
|
||
15 years ago
|
KMimeMagicResult *result;
|
||
|
|
||
|
KMimeMagic::self ()->setFollowLinks (TRUE); // is it necessary ?
|
||
|
|
||
|
if (aAutoDecode)
|
||
|
body = bodyDecodedBinary ();
|
||
|
else
|
||
|
body = postMultipartBody;
|
||
|
|
||
|
result = KMimeMagic::self ()->findBufferType (body);
|
||
|
mimetype = result->mimeType ();
|
||
|
contentType = mimetype;
|
||
|
return mimetype;
|
||
|
}
|
||
|
#endif
|