mailmessage.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,042 行 · 第 1/3 页
CPP
1,042 行
}
else {
// Body
if(body.getCharset())
ret += CT_CHARSET; ret += body.getCharset(); ret += NL;
if( body.getEncoding() )
ret += ENCODING; ret += body.getEncoding();
// end of headers
ret += NL;
ret += NL;
ret += body.getContent(); ret += NL;
}
LOG.debug("MailMessage::format END");
return stringdup(ret.c_str());
}
int MailMessage::parse(const char *rfc2822, size_t len) {
StringBuffer s(rfc2822, len);
int rc;
LOG.debug("MailMessage::parse START");
size_t hdrlen = getHeadersLen(s, newline);
StringBuffer headers = s.substr(0, hdrlen);
StringBuffer rfcbody;
rc = parseHeaders(headers);
if(rc)
return rc;
if(contentType.ifind(MULTIPART) != StringBuffer::npos) {
// Multipart message
rfcbody = s.substr(hdrlen);
rc= parseBodyParts(rfcbody);
}
else {
// go to the beginning of the body
hdrlen = hdrlen + strlen(newline) + strlen(newline);
rfcbody = s.substr(hdrlen);
body.setMimeType(contentType);
// FIXME: handle all encodings, not only quoted-printable
if( strcmp(body.getEncoding(), "quoted-printable") == 0 ) {
char *decoded = qp_decode( rfcbody );
body.setContent ( decoded );
delete [] decoded;
}
else if ( strcmp(body.getEncoding(), "base64") == 0 ) {
char *decoded = NULL;
size_t len = 0;
rc = uudecode( rfcbody, &decoded, &len ) ;
if( !rc ) {
body.setContent ( decoded );
delete [] decoded;
}
}
else body.setContent(rfcbody);
}
LOG.debug("MailMessage::parse END");
return rc;
}
//---------------------------------------------------------- Private Methods
StringBuffer convertImportance(StringBuffer data) {
StringBuffer ret("3");
if (data == IMP_HIGH) {
ret = "1";
} else if (data == IMP_LOW) {
ret = "5";
}
return ret;
}
StringBuffer convertXPriority(StringBuffer data) {
StringBuffer ret("3");
if (data.ifind("1") == 0 || data.ifind("2") == 0) {
ret = "1";
} else if (data.ifind("4") == 0 || data.ifind("5") == 0) {
ret = "5";
}
return ret;
}
int MailMessage::parseHeaders(StringBuffer &rfcHeaders) {
ArrayList lines;
const StringBuffer *line;
StringBuffer strReceived;
bool receivedExtracted = false;
LOG.debug("parseHeaders START");
// Join header parts using \t or 8 blank
StringBuffer joinlinetab("\t");
rfcHeaders.replaceAll(joinlinetab, " ");
StringBuffer joinlinespaces(newline);
joinlinespaces+=" "; // 8 blanks
rfcHeaders.replaceAll(joinlinespaces, " ");
rfcHeaders.split(lines, newline);
// importance is set to "0" by default. Then there isn't anything modification
// in the header, at the end of the loop the importance will be set to "3", default normal
importance = "0";
for ( line=(StringBuffer *)lines.front();
line;
line=(StringBuffer *)lines.next() ) {
if( *line == "\r" )
break;
// The first empty line marks the end of the header section
if( line->empty() ){
break;
}
// Process the headers
if( line->ifind(TO) == 0 ){
to = MailMessage::decodeHeader(line->substr(TO_LEN));
}
else if( line->ifind(FROM) == 0 ) {
from = MailMessage::decodeHeader(line->substr(FROM_LEN));
}
else if( line->ifind(CC) == 0 ) {
cc = MailMessage::decodeHeader(line->substr(CC_LEN));
}
else if( line->ifind(BCC) == 0 ) {
bcc = MailMessage::decodeHeader(line->substr(BCC_LEN));
}
else if ( line->ifind(DATE) == 0 ) {
//subjectParsing = false;
if( date.parseRfc822(line->substr(DATE_LEN)) ) {
LOG.error("Error parsing date");
return 500;
}
}
else if( line->ifind(SUBJECT) == 0 ) {
subject = MailMessage::decodeHeader(line->substr(SUBJECT_LEN));
LOG.debug("SUBJECT: %s", subject.c_str());
}
else if( line->ifind(ENCODING) == 0 ) { // it is here for single part only
body.setEncoding(line->substr(ENCODING_LEN));
}
else if(line->ifind(MIMEVERS) == 0 ) {
mimeVersion = line->substr(MIMEVERS_LEN);
}
else if(line->ifind(MESSAGEID) == 0 ) {
messageId = line->substr(MESSAGEID_LEN);
}
else if(line->ifind(IMPORTANCE) == 0 ) {
StringBuffer data = line->substr(IMPORTANCE_LEN);
data.lowerCase();
importance = convertImportance(data);
}
else if(line->ifind(X_PRIORITY) == 0 ) {
if (importance == "0") {
StringBuffer data = line->substr(X_PRIORITY_LEN);
data.lowerCase();
importance = convertXPriority(data);
}
}
else if( line->ifind(MIMETYPE) == 0 ) {
StringBuffer sb = getTokenValue(line, MIMETYPE);
if (sb.length() > 0)
contentType = sb;
sb.reset();
sb = getTokenValue(line, "boundary=", false);
if (sb.length() > 0) {
boundary = sb;
} else {
body.setCharset(getTokenValue(line, CT_CHARSET));
}
/*
size_t len = line->find(";") - MIMETYPE_LEN ;
contentType = line->substr(MIMETYPE_LEN, len);
// Save boundary for multipart
size_t begin = line->ifind("boundary=");
size_t end = StringBuffer::npos;
if( begin != StringBuffer::npos ) {
begin += strlen("boundary=\"");
end = line->find("\"", begin) ;
boundary = line->substr( begin, end-begin );
}
else {
begin=line->ifind(CT_CHARSET);
if( begin != StringBuffer::npos ) {
begin += strlen(CT_CHARSET);
size_t end = begin;
size_t quote = line->find("\"", begin);
if (quote != StringBuffer::npos){
begin = quote + 1;
end = line->find("\"", begin) ;
}
else {
end = line->find(";", begin) ;
if (end == StringBuffer::npos) {
end = line->find(" ", begin);
}
}
body.setCharset( line->substr( begin, end-begin ) );
}
}
*/
}
else if(line->ifind(RECEIVED) == 0) {
if (!receivedExtracted) {
strReceived = line->substr(line->rfind(";") );
if (!strReceived.empty()) {
received.parseRfc822(strReceived.substr(2));
receivedExtracted = true;
}
/*
while (!strReceived.empty()) {
if (received.parseRfc822(strReceived.substr(2)) == 0) {
receivedExtracted = true;
break;
} else {
StringBuffer s(line->substr(line->rfind(strReceived.c_str())));
strReceived = line->substr(s.rfind(";"));
}
}
*/
}
}
else {
headers.add(*(StringBuffer *)line);
}
}
// If received was not found, copy send date
if( received == BasicTime() ){
received = date;
}
// if the importance is never set we put the importance to 3, normal
if (importance == "0") {
importance = "3";
}
LOG.debug("parseHeaders END");
// FIXME: should check for mandatory headers before return 0
return 0;
}
int MailMessage::parseBodyParts(StringBuffer &rfcBody) {
BodyPart part;
// The boundary is the one defined in the headers preceded by
// a newline and two hypens
StringBuffer bound("\n--");
bound += boundary;
LOG.debug("parseBodyParts START");
size_t nextBoundary = rfcBody.find(bound);
getBodyPart(rfcBody, bound, body, nextBoundary, false);
if (contentType.ifind("multipart/alternative") == StringBuffer::npos) {
// If it's not multipart/alternative, get the other parts
while( getBodyPart(rfcBody, bound, part, nextBoundary, true) ) {
// some problem in the attachment?
if( part.getContent() ) {
attachments.add(part);
}
else LOG.error("Empty content in attachment.");
part = BodyPart();
}
}
LOG.debug("parseBodyParts END");
return 0;
}
// Return true if the instance is empty (a valid MailMessage must have a valid date that is not
// the default 1/1/1970)
bool MailMessage::empty() {
return ( this->getDate() == BasicTime() );
}
ArrayElement* MailMessage::clone() {
return new MailMessage(*this);
}
void MailMessage::setHeaders(const char* chExtraHeaders)
{
if(chExtraHeaders){
StringBuffer extraHeaders(chExtraHeaders);
ArrayList lines;
extraHeaders.split(headers, "\n");
}
}
/**
* The result must be deleted by caller
*/
const char* MailMessage::getHeaders()
{
if( headers.size() ) {
StringBuffer buff;
buff.join(headers, "\n");
char* strHeaders = stringdup(buff.c_str(), buff.length() -1);
return strHeaders;
}
else return 0;
}
bool MailMessage::operator==(MailMessage& that){
return (
this->to == that.to &&
this->from == that.from &&
this->cc == that.cc &&
this->bcc == that.bcc &&
this->subject == that.subject &&
this->date == that.date &&
this->received == that.received &&
this->contentType == that.contentType &&
this->boundary == that.boundary &&
this->mimeVersion == that.mimeVersion &&
this->messageId == that.messageId
);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?