📄 msgpartc.c
字号:
while ( lastChild->next ) lastChild = lastChild->next; } return lastChild;} // End BestAlternative/*----------------------------------------------------------------------- * Method to return best text alternative for displaying inline. */MsgPartC*MsgPartC::BestTextAlternative() const{ if ( !IsAlternative() || !child ) return NULL; MsgPartC *lastChild = NULL; MsgPartC *cp = child;// Loop through children Boolean done = False; while ( !done && cp ) {// Check the recognized types if ( cp->IsInlineText() && (cp->IsPlainText() || cp->IsRichText() || cp->IsEnriched()) ) { lastChild = cp; cp = cp->next; } else { done = True; } } // End for each child// If there are no known types, use the first one if (!lastChild ) lastChild = child; return lastChild;} // End BestTextAlternative/*----------------------------------------------------------------------- * Method to return a good label for the body part that can be called by * a const object. */voidMsgPartC::GetLabel(StringC& str) const{#if 0 if ( partNum.size() > 0 ) { str = partNum; str += ' '; } else#endif str.Clear();//// Try the description// StringC desc; GetHeaderValue("Content-Description", desc); if ( desc.size() > 0 && !desc.Equals("default", IGNORE_CASE) ) { str += desc; return; }//// Try the name parameter// ParamC *param = Param(NAME_S, accParams); if ( param ) { str += param->val; return; } param = Param(NAME_S, conParams); if ( param ) { str += param->val; return; }//// If this is a message/rfc822 type, look for a subject// if ( Is822() ) { if ( subject ) { subject->GetValueText(str); return; } else if ( childMsg ) { StringC sub; childMsg->GetSubjectText(sub); str += sub; return; } }//// Try the content-disposition header// param = Param(FILENAME_S); if ( param ) { str += param->val; return; } param = Param(NAME_S, disParams); if ( param ) { str += param->val; return; }//// If the name is still blank, use the type.// str += conStr; return;} // End GetLabel/*----------------------------------------------------------------------- * Method to return a good label for the body part that can be called by * a non-const object. */voidMsgPartC::GetLabel(StringC& str){#if 0 if ( partNum.size() > 0 ) { str = partNum; str += ' '; } else#endif str.Clear();//// Try the description// StringC desc; GetHeaderValue("Content-Description", desc); if ( desc.size() > 0 && !desc.Equals("default", IGNORE_CASE) ) { str += desc; return; }//// Try the name parameter// ParamC *param = Param(NAME_S, accParams); if ( param ) { str += param->val; return; } param = Param(NAME_S, conParams); if ( param ) { str += param->val; return; }//// If this is a message/rfc822 type, look for a subject// if ( Is822() ) { if ( subject ) subject->GetValueText(str); else { FileMsgC *msg = ChildMsg(); StringC sub; msg->GetSubjectText(sub); str += sub; } return; }//// Try the content-disposition header// param = Param(FILENAME_S); if ( param ) { str += param->val; return; } param = Param(NAME_S, disParams); if ( param ) { str += param->val; return; }//// If the name is still blank, use the type.// str += conStr; return;} // End GetLabel/*----------------------------------------------------------------------- * Method to return a file name for this part */voidMsgPartC::GetFileName(StringC& file) const{ if ( !IsMail() && dataFile.size() > 0 ) { file += dataFile; return; }//// Try the name parameter// ParamC *param = Param(NAME_S, accParams); if ( param ) { file += param->val; return; } param = Param(NAME_S, conParams); if ( param ) { file += param->val; return; }//// Try the content-disposition header// param = Param(FILENAME_S); if ( param ) { file += param->val; return; } param = Param(NAME_S, disParams); if ( param ) { file += param->val; return; }//// Try the description// StringC desc; GetDescription(desc); if ( !desc.Equals("default", IGNORE_CASE) ) { file += desc; return; }//// If the name is still blank, use the message file.// if ( msgFile.size() > 0 ) { file += msgFile; return; }//// If the name is still blank, punt.// file += "Unknown"; return;} // End GetFileName/*----------------------------------------------------------------------- * Method to return value of content-description header */voidMsgPartC::GetDescription(StringC& str) const{ HeaderValC *val = HeaderValue("Content-Description"); if ( val ) val->GetValueText(str);}/*----------------------------------------------------------------------- * Method to return value of content-disposition header */voidMsgPartC::GetDisposition(StringC& str) const{ HeaderValC *val = HeaderValue("Content-Disposition"); if ( val ) val->GetValueText(str);}/*----------------------------------------------------------------------- * Method to return whether the content-disposition headers says "attachment" */BooleanMsgPartC::IsAttachment() const{ StringC disp; GetHeaderValue("Content-Disposition", disp); return disp.StartsWith("attachment", IGNORE_CASE);}/*--------------------------------------------------------------- * Method to return the text as it appears in the mail message. */BooleanMsgPartC::GetText(StringC& text, FILE *fp, Boolean getHead, Boolean getExtHead, Boolean getBody, Boolean restoreFroms) const{ if ( getHead && getBody ) getExtHead = True; else if ( !IsExternal() ) getExtHead = False; if ( !getBody ) restoreFroms = False; if ( msgFile.size() == 0 ) { if ( parentMsg && parentMsg->IsImap() ) return parentMsg->GetPartText(this, text, getHead, getExtHead, getBody); else return False; }//// Figure out how much to get// int off = 0; int len = 0; if ( getHead && getBody ) { // Assume getExtHead off = offset; len = bytes; } else if ( getHead && getExtHead ) { off = offset; len = headBytes+1+extBytes; } else if ( getExtHead && getBody ) { off = extOffset; len = extBytes+extBlank+bodyBytes; } else if ( getHead ) { off = offset; len = headBytes; } else if ( getExtHead ) { off = extOffset; len = extBytes; } else if ( getBody ) { off = bodyOffset; len = bodyBytes; }//// Perform the get// return GetText(text, fp, off, len, restoreFroms);} // End GetText/*------------------------------------------------------------------------- * Return the text for the specified offset and length */BooleanMsgPartC::GetText(StringC& text, FILE *ifp, int off, int len, Boolean restoreFroms) const{ Boolean closeInput = (ifp == NULL); if ( !ifp ) { ifp = fopen(msgFile, "r"); if ( !ifp ) { StringC errmsg("Could not open file: \""); errmsg += msgFile; errmsg += "\".\n"; errmsg += SystemErrorMessage(errno); halApp->PopupMessage(errmsg); return False; } } fseek(ifp, off, SEEK_SET); Boolean error = False; if ( restoreFroms ) { RegexC *fromPat = UnixFolderC::fromPat; int remaining = len; char buffer[BUFLEN]; buffer[0] = 0; while ( remaining > 0 ) {//// Read a line from the source file// if ( !fgets(buffer, BUFLEN-1, ifp) ) { StringC errmsg("Could not read file: \""); errmsg += msgFile; errmsg += "\".\n"; errmsg += SystemErrorMessage(errno); halApp->PopupMessage(errmsg); if ( closeInput ) fclose(ifp); return False; }//// See if it needs restoring// if ( buffer[0] == '>' ) { CharC bufStr = buffer; if ( bufStr.StartsWith(">From ") && (!fromPat || fromPat->match(bufStr, 1)) ) { bufStr.CutBeg(1); } text += bufStr; } else text += buffer; remaining -= strlen(buffer); } // End while bytes remain to be copied } // End if Froms may need restoring else error = !text.AppendFile(ifp, len); if ( closeInput ) fclose(ifp); return !error;} // End GetText/*----------------------------------------------------------------------- * Method to return the contents of the data file associated with this part. */BooleanMsgPartC::GetData(StringC& text, FILE *fp){//// If it's external, read the part file. It is an error if this file does// not already exist.// if ( IsExternal() ) { if ( dataFile.size() == 0 ) return GetText(text, fp); if ( !text.AppendFile(dataFile) ) { StringC lab; GetLabel(lab); StringC errmsg("Could not read file: "); errmsg += dataFile; errmsg += "\nfor part \""; errmsg += lab; errmsg += "\"\n"; errmsg += SystemErrorMessage(errno); halApp->PopupMessage(errmsg); return False; } return True; } // End if part is external//// If it's encoded, decode it and read the resulting file.// if ( IsEncoded() ) { if ( dataFile.size() == 0 && !Decode(fp) ) return GetText(text, fp); if ( !text.AppendFile(dataFile) ) { StringC lab; GetLabel(lab); StringC errmsg("Could not read decoded body file: "); errmsg += dataFile; errmsg += "\nfor part \""; errmsg += lab; errmsg += "\"\n"; errmsg += SystemErrorMessage(errno); halApp->PopupMessage(errmsg); return False; } return True; } // End if part is encoded//// If it's not encoded or external, return the body text// return GetText(text, fp, False, False, True, parentMsg && parentMsg->HasSafeFroms());} // End GetData/*----------------------------------------------------------------------- * Method to decode this body part to a file */BooleanMsgPartC::Decode(FILE *fp){ if ( !IsEncoded() || dataFile.size() > 0 ) return True;//// Create an output file// char *ofile = tempnam(NULL, "dec."); dataFile = ofile; if ( conType == CT_HTML ) { // Some brain-damaged MUAs generating HTML mails omit the encapsulating // <HTML></HTML> tags so we'll have to tell the browser to display this // as a valid HTML doc via the proper file extension. dataFile += ".html"; } delDataFile = True; free(ofile);//// If there's no message file, we have to get the text.// Boolean error = False; if ( msgFile.size() == 0 ) { StringC text; if ( bodyBytes > 0 ) text.reSize(bodyBytes); GetText(text, /*fp*/NULL, /*getHead*/False, /*getExtHead*/False, /*getBody*/True, /*restoreFroms*/False); if ( IsBase64() ) { MailcapC *mcap = MailcapEntry(this); error = !Text64ToFile(text, dataFile, mcap ? mcap->isText : False, NULL); } else if ( IsQuoted() ) { error = !TextQPToFile(text, dataFile, NULL); } else if ( IsUUencoded() ) { error = !TextUUToFile(text, dataFile); } // If none of the above, silently ignore the encoding // and hope for the best. }//// Get the decoded text// else { if ( IsBase64() ) { MailcapC *mcap = MailcapEntry(this); error = !File64ToFile(msgFile, dataFile, mcap ? mcap->isText : False, fp, NULL, bodyOffset, bodyBytes); } else error = !FileQPToFile(msgFile, dataFile, fp, NULL, bodyOffset, bodyBytes); } return !error;} // End Decode/*--------------------------------------------------------------- * Method to read and process the part headers */BooleanMsgPartC::ScanHead(FILE *fp){ if ( headScanned ) return True; Boolean closeFile = (fp == NULL); if ( !fp ) { fp = fopen(msgFile, "r"); if ( !fp ) { StringC errmsg("Could not open file: \""); errmsg += msgFile; errmsg += "\".\n"; errmsg += SystemErrorMessage(errno); halApp->PopupMessage(errmsg); return False; } }//// Move to start of headers// fseek(fp, offset, SEEK_SET); headLines = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -