// ------------------------ CalcLineCode -------------------------------- // calc the LineCode of the mime message line. see the MimeLineCode enum. public static MimeLineCode CalcLineCode( string InLine, string InBdryText) { MimeLineCode code = MimeLineCode.None; // a part boundary line if ((InLine.Length > 2) && (InLine.Substring(0, 2) == "--") && (InBdryText != null)) { string beginBdry = "--" + InBdryText; string endBdry = beginBdry + "--"; if ((InLine.Length >= endBdry.Length) && (InLine.Substring(0, endBdry.Length) == endBdry)) { code = MimeLineCode.PartEndBdry; } else if ((InLine.Length >= beginBdry.Length) && (InLine.Substring(0, beginBdry.Length) == beginBdry)) { code = MimeLineCode.PartBdry; } } // check for the remaining line codes. if (code != MimeLineCode.None) { } else if (InLine.Length == 0) { code = MimeLineCode.None; } // a property line starts with non blank, then that word ends with ":" else if ((MimeCommon.CharIsWhitespace(InLine[0]) == false) && (ScanPropertyNameDelimChar(InLine).ResultChar == ':')) { code = MimeLineCode.Property; } else { code = MimeLineCode.Text; } return(code); }
// -------------------------- MessagePartSplitter ----------------------- // input is an array holding the unfolded lines of the message. // Build and return a list of MimeMessagePart objects. Each part object holds the // lines of a part of the message. // ( a part is either the header part or the boundary string delimited content parts. public static InMail.MimeMessagePartList MessagePartSplitter(string InStream) { InMail.MimeMessagePartList parts = new InMail.MimeMessagePartList( ); bool currentlyBetweenParts = false; StringStack bdryStack = new StringStack( ); int Ex = -1; string line = null; MimeMessagePart.PartInProgress pip = new MimeMessagePart.PartInProgress(MimePartCode.Top); while (true) { // advance in string. int lineBx = Ex + 1; if (lineBx >= InStream.Length) { break; } // the next line in the stream. recognize folds depending on if currently within // a part header or within the message lines of the part ( no folding there ) if (pip.CurrentlyAmoungPropertyLines == true) { IntStringPair rv = ScanEndUnfoldedLine(InStream, lineBx); Ex = rv.a; line = rv.b; } else { IntStringPair rv = ScanEndLine(InStream, lineBx); Ex = rv.a; line = rv.b; } // calc what kind of a line in the mime document we have here. MimeLineCode lc = MimeParser.CalcLineCode(line, bdryStack.GetTop( )); if ((lc == MimeLineCode.Property) && (pip.CurrentlyAmoungMessageLines == true)) { lc = MimeLineCode.Text; } switch (lc) { case MimeLineCode.Property: { StringPair propPair = MimeParser.SplitPropertyLine(line); // the content-type property. Could have an boundary="xxxxx" element. // Boundary strings in a mime document have scope. That is, within one // boundary ( section ) of the document, there can be sub boundaries // ( sub sections ) if (propPair.a.ToLower( ) == "content-type") { PartProperty.ContentType ct = MimeParser.ParseContentType(propPair.b); if ((ct.Boundary != null) && (ct.Boundary != "")) { bdryStack.Push(ct.Boundary); } } pip.AddLine(lineBx, line); break; } // part boundary line. load the lines of the current part and reset the line // counter of this new part. case MimeLineCode.PartBdry: if (pip.HasLines == true) { parts.AddNewPart(InStream, pip); } pip = new MimeMessagePart.PartInProgress(MimePartCode.Content); currentlyBetweenParts = false; break; case MimeLineCode.PartEndBdry: if (pip.HasLines == true) { parts.AddNewPart(InStream, pip); } pip = new MimeMessagePart.PartInProgress(MimePartCode.Content); if (bdryStack.IsNotEmpty) { bdryStack.Pop( ); } currentlyBetweenParts = true; break; // we have a line which is not a property line, not a boundary line. default: { // just starting out. discard the "+OK" response sent by the server immed // before the start of the message. if ((pip.PartCode == MimePartCode.Top) && (pip.PartIsJustStarting == true) && (line.Length >= 3) && (line.Substring(0, 3) == "+OK")) { } // currently handling property lines. else if (pip.CurrentlyAmoungPropertyLines == true) { // a blank line switches from property lines to message lines. if (line.Length == 0) { pip.CurrentlyAmoungPropertyLines = false; pip.CurrentlyAmoungMessageLines = true; } // what is this text line doing amoung the property lines ?? // for now, just ignore it. else { } } else if (currentlyBetweenParts == false) { pip.AddLine(lineBx, line); } break; } } } // load the lines of the final in progress part. if (pip.HasLines == true) { parts.AddNewPart(InStream, pip); } return(parts); }
// -------------------------- MessagePartSplitter ----------------------- // input is an array holding the unfolded lines of the message. // Build and return a list of MimeMessagePart objects. Each part object holds the // lines of a part of the message. // ( a part is either the header part or the boundary string delimited content parts. public static InMail.MimeMessagePartList MessagePartSplitter(string[] InLines) { InMail.MimeMessagePartList parts = new InMail.MimeMessagePartList( ); int curPartBx = -1; int curPartCx = 0; MimePartCode curPartCode = MimePartCode.Top; bool currentlyBetweenParts = false; bool partIsJustStarting = true; StringStack bdryStack = new StringStack( ); for (int Ix = 0; Ix < InLines.Length; ++Ix) { string line = InLines[Ix]; MimeLineCode lc = MimeParser.CalcLineCode(line, bdryStack.GetTop( )); switch (lc) { case MimeLineCode.Property: { StringPair propPair = MimeParser.SplitPropertyLine(line); // the content-type property. Could have an boundary="xxxxx" element. // Boundary strings in a mime document have scope. That is, within one // boundary ( section ) of the document, there can be sub boundaries // ( sub sections ) if (propPair.a.ToLower( ) == "content-type") { PartProperty.ContentType ct = MimeParser.ParseContentType(propPair.b); if ((ct.Boundary != null) && (ct.Boundary != "")) { bdryStack.Push(ct.Boundary); } } break; } // part boundary line. load the lines of the current part and reset the line // counter of this new part. case MimeLineCode.PartBdry: if (curPartBx != -1) { parts.AddNewPart(curPartCode) .LoadLines(InLines, curPartBx, curPartCx); } curPartCx = 0; curPartBx = -1; curPartCode = MimePartCode.Content; currentlyBetweenParts = false; partIsJustStarting = true; break; case MimeLineCode.PartEndBdry: if (curPartBx != -1) { parts.AddNewPart(curPartCode) .LoadLines(InLines, curPartBx, curPartCx); } curPartCx = 0; curPartBx = -1; if (bdryStack.IsNotEmpty) { bdryStack.Pop( ); } currentlyBetweenParts = true; break; default: break; } // add to range of lines in the current part. if ((currentlyBetweenParts == false) && (lc != MimeLineCode.PartBdry)) { if ((partIsJustStarting == true) && (line == "")) { } else { ++curPartCx; if (curPartBx == -1) { curPartBx = Ix; } } partIsJustStarting = false; } } // load the lines of the final in progress part. if (curPartBx != -1) { parts.AddNewPart(curPartCode) .LoadLines(InLines, curPartBx, curPartCx); } return(parts); }