public void TestBodyPartBasic() { var uri = new Uri("https://www.nationalgeographic.com/travel/contests/photographer-of-the-year-2018/wallpapers/week-9-nature/2/"); const string expected = "(\"image\" \"jpeg\" (\"name\" \"wallpaper.jpg\") \"id@localhost\" \"A majestic supercell storm approaching a house in Kansas, 2016.\" \"base64\" 0 \"8criUiOQmpfifOuOmYFtEQ==\" (\"attachment\" (\"filename\" \"wallpaper.jpg\")) (\"en\" \"fr\") \"https://www.nationalgeographic.com/travel/contests/photographer-of-the-year-2018/wallpapers/week-9-nature/2/\")"; BodyPartBasic basic, parsed; BodyPart body; basic = new BodyPartBasic { ContentType = new ContentType("image", "jpeg") { Name = "wallpaper.jpg" }, ContentId = "id@localhost", ContentMd5 = "8criUiOQmpfifOuOmYFtEQ==", ContentLanguage = new string[] { "en", "fr" }, ContentLocation = uri, ContentDescription = "A majestic supercell storm approaching a house in Kansas, 2016.", ContentDisposition = new ContentDisposition(ContentDisposition.Attachment) { FileName = "wallpaper.jpg" }, ContentTransferEncoding = "base64" }; Assert.IsTrue(basic.IsAttachment); Assert.AreEqual("wallpaper.jpg", basic.FileName); Assert.AreEqual(expected, basic.ToString()); Assert.IsTrue(BodyPart.TryParse(expected, out body)); Assert.IsInstanceOf <BodyPartBasic> (body); parsed = (BodyPartBasic)body; Assert.AreEqual(expected, parsed.ToString()); }
static BodyPartBasic CreateBasic(string type, string subtype, string partSpecifier, bool attachment) { var basic = new BodyPartBasic { ContentType = CreateContentType(type, subtype, partSpecifier) }; basic.ContentDisposition = new ContentDisposition(attachment ? ContentDisposition.Attachment : ContentDisposition.Inline); return(basic); }
public void TestBasicFunctionality() { var body = new BodyPartBasic { PartSpecifier = "1.2.3.4" }; AnnotationEntry entry; entry = new AnnotationEntry("/comment"); Assert.AreEqual("/comment", entry.Entry, "Entry"); Assert.IsNull(entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Both, entry.Scope, "Scope"); entry = new AnnotationEntry("/comment", AnnotationScope.Private); Assert.AreEqual("/comment.priv", entry.Entry, "Entry"); Assert.IsNull(entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Private, entry.Scope, "Scope"); entry = new AnnotationEntry("/comment", AnnotationScope.Shared); Assert.AreEqual("/comment.shared", entry.Entry, "Entry"); Assert.IsNull(entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Shared, entry.Scope, "Scope"); entry = new AnnotationEntry("1.2.3.4", "/comment"); Assert.AreEqual("/1.2.3.4/comment", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Both, entry.Scope, "Scope"); entry = new AnnotationEntry("1.2.3.4", "/comment", AnnotationScope.Private); Assert.AreEqual("/1.2.3.4/comment.priv", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Private, entry.Scope, "Scope"); entry = new AnnotationEntry("1.2.3.4", "/comment", AnnotationScope.Shared); Assert.AreEqual("/1.2.3.4/comment.shared", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Shared, entry.Scope, "Scope"); entry = new AnnotationEntry(body, "/comment"); Assert.AreEqual("/1.2.3.4/comment", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Both, entry.Scope, "Scope"); entry = new AnnotationEntry(body, "/comment", AnnotationScope.Private); Assert.AreEqual("/1.2.3.4/comment.priv", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Private, entry.Scope, "Scope"); entry = new AnnotationEntry(body, "/comment", AnnotationScope.Shared); Assert.AreEqual("/1.2.3.4/comment.shared", entry.Entry, "Entry"); Assert.AreEqual("1.2.3.4", entry.PartSpecifier, "PartSpecifier"); Assert.AreEqual("/comment", entry.Path, "Path"); Assert.AreEqual(AnnotationScope.Shared, entry.Scope, "Scope"); }
/// <summary> /// Visit the basic MIME body part. /// </summary> /// <remarks> /// Visits the basic MIME body part. /// </remarks> /// <param name="entity">The basic MIME body part.</param> protected internal virtual void VisitBodyPartBasic(BodyPartBasic entity) { VisitBodyPart(entity); }
public static BodyPart ParseBody (ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken (cancellationToken); if (token.Type == ImapTokenType.Nil) return null; if (token.Type != ImapTokenType.OpenParen) throw ImapEngine.UnexpectedToken (token, false); token = engine.PeekToken (cancellationToken); if (token.Type == ImapTokenType.OpenParen) return ParseMultipart (engine, path, null, cancellationToken); ContentType type; string value; if (!ParseContentType (engine, cancellationToken, out type, out value)) { // GMail breakage... yay! What we have is a nested multipart with // the same boundary as its parent. return ParseMultipart (engine, path, value, cancellationToken); } var id = ReadNStringToken (engine, false, cancellationToken); var desc = ReadNStringToken (engine, true, cancellationToken); // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers... var enc = ReadNStringToken (engine, false, cancellationToken); var octets = ReadNumber (engine, cancellationToken); BodyPartBasic body; if (type.Matches ("message", "rfc822")) { var mesg = new BodyPartMessage (); // Note: GMail (and potentially other IMAP servers) will send body-part-basic // expressions instead of body-part-msg expressions when they encounter // message/rfc822 MIME parts that are illegally encoded using base64 (or // quoted-printable?). According to rfc3501, IMAP servers are REQUIRED to // send body-part-msg expressions for message/rfc822 parts, however, it is // understandable why GMail (and other IMAP servers?) do what they do in this // particular case. // // For examples, see issue #32 and issue #59. // // The workaround is to check for the expected '(' signifying an envelope token. // If we do not get an '(', then we are likely looking at the Content-MD5 token // which gets handled below. token = engine.PeekToken (cancellationToken); if (token.Type == ImapTokenType.OpenParen) { mesg.Envelope = ParseEnvelope (engine, cancellationToken); mesg.Body = ParseBody (engine, path, cancellationToken); mesg.Lines = ReadNumber (engine, cancellationToken); } body = mesg; } else if (type.Matches ("text", "*")) { var text = new BodyPartText (); text.Lines = ReadNumber (engine, cancellationToken); body = text; } else { body = new BodyPartBasic (); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLanguage (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ParseContentLocation (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) SkipBodyExtensions (engine, cancellationToken); // read the ')' token = engine.ReadToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) throw ImapEngine.UnexpectedToken (token, false); return body; }
public void TestBodyPartCollection() { var text = new BodyPartText { ContentType = new ContentType("text", "plain"), ContentLocation = new Uri("body", UriKind.Relative) }; var image1 = new BodyPartBasic { ContentType = new ContentType("image", "jpeg"), ContentLocation = new Uri("http://localhost/image1.jpg") }; var image2 = new BodyPartBasic { ContentType = new ContentType("image", "jpeg"), ContentId = "image2@localhost" }; var list = new BodyPartCollection(); var parts = new BodyPart[3]; int i = 0; Assert.Throws <ArgumentNullException> (() => list.Add(null)); Assert.Throws <ArgumentNullException> (() => list.Remove(null)); Assert.Throws <ArgumentNullException> (() => list.Contains(null)); Assert.Throws <ArgumentNullException> (() => list.IndexOf(null)); Assert.Throws <ArgumentNullException> (() => list.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException> (() => list.CopyTo(parts, -1)); Assert.Throws <ArgumentOutOfRangeException> (() => { var x = list[0]; }); Assert.IsFalse(list.IsReadOnly); Assert.AreEqual(0, list.Count); list.Add(text); Assert.AreEqual(1, list.Count); Assert.IsTrue(list.Contains(text)); Assert.IsFalse(list.Contains(image1)); Assert.AreEqual(0, list.IndexOf(new Uri("body", UriKind.Relative))); Assert.AreEqual(-1, list.IndexOf(new Uri("http://localhost/image1.jpg"))); Assert.AreEqual(-1, list.IndexOf(new Uri("cid:image2@localhost"))); Assert.AreEqual(text, list[0]); list.Add(image1); Assert.AreEqual(2, list.Count); Assert.IsTrue(list.Contains(text)); Assert.IsTrue(list.Contains(image1)); Assert.AreEqual(0, list.IndexOf(new Uri("body", UriKind.Relative))); Assert.AreEqual(1, list.IndexOf(new Uri("http://localhost/image1.jpg"))); Assert.AreEqual(-1, list.IndexOf(new Uri("cid:image2@localhost"))); Assert.AreEqual(text, list[0]); Assert.AreEqual(image1, list[1]); Assert.IsTrue(list.Remove(text)); Assert.AreEqual(1, list.Count); Assert.IsFalse(list.Contains(text)); Assert.IsTrue(list.Contains(image1)); Assert.AreEqual(-1, list.IndexOf(new Uri("body", UriKind.Relative))); Assert.AreEqual(0, list.IndexOf(new Uri("http://localhost/image1.jpg"))); Assert.AreEqual(-1, list.IndexOf(new Uri("cid:image2@localhost"))); Assert.AreEqual(image1, list[0]); list.Clear(); Assert.AreEqual(0, list.Count); list.Add(text); list.Add(image1); list.Add(image2); list.CopyTo(parts, 0); Assert.AreEqual(0, list.IndexOf(new Uri("body", UriKind.Relative))); Assert.AreEqual(1, list.IndexOf(new Uri("http://localhost/image1.jpg"))); Assert.AreEqual(2, list.IndexOf(new Uri("cid:image2@localhost"))); foreach (var part in list) { Assert.AreEqual(parts[i++], part); } i = 0; foreach (var part in (IEnumerable)list) { Assert.AreEqual(parts[i++], part); } }
public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken(cancellationToken); if (token.Type == ImapTokenType.Nil) { return(null); } if (token.Type != ImapTokenType.OpenParen) { throw ImapEngine.UnexpectedToken(token, false); } token = engine.PeekToken(cancellationToken); if (token.Type == ImapTokenType.OpenParen) { return(ParseMultipart(engine, path, cancellationToken)); } var type = ParseContentType(engine, cancellationToken); var id = ReadNStringToken(engine, false, cancellationToken); var desc = ReadNStringToken(engine, true, cancellationToken); var enc = ReadStringToken(engine, cancellationToken); var octets = ReadNumber(engine, cancellationToken); BodyPartBasic body; if (type.Matches("message", "rfc822")) { var mesg = new BodyPartMessage(); mesg.Envelope = ParseEnvelope(engine, cancellationToken); mesg.Body = ParseBody(engine, path, cancellationToken); mesg.Lines = ReadNumber(engine, cancellationToken); body = mesg; } else if (type.Matches("text", "*")) { var text = new BodyPartText(); text.Lines = ReadNumber(engine, cancellationToken); body = text; } else { body = new BodyPartBasic(); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLocation(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ReadNStringToken(engine, false, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { SkipBodyExtensions(engine, cancellationToken); } // read the ')' token = engine.ReadToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { throw ImapEngine.UnexpectedToken(token, false); } return(body); }
public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken (cancellationToken); if (token.Type == ImapTokenType.Nil) return null; if (token.Type != ImapTokenType.OpenParen) throw ImapEngine.UnexpectedToken (token, false); token = engine.PeekToken (cancellationToken); if (token.Type == ImapTokenType.OpenParen) return ParseMultipart (engine, path, cancellationToken); var type = ParseContentType (engine, cancellationToken); var id = ReadNStringToken (engine, false, cancellationToken); var desc = ReadNStringToken (engine, true, cancellationToken); var enc = ReadStringToken (engine, cancellationToken); var octets = ReadNumber (engine, cancellationToken); BodyPartBasic body; if (type.Matches ("message", "rfc822")) { var mesg = new BodyPartMessage (); mesg.Envelope = ParseEnvelope (engine, cancellationToken); mesg.Body = ParseBody (engine, path, cancellationToken); mesg.Lines = ReadNumber (engine, cancellationToken); body = mesg; } else if (type.Matches ("text", "*")) { var text = new BodyPartText (); text.Lines = ReadNumber (engine, cancellationToken); body = text; } else { body = new BodyPartBasic (); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLocation (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ReadNStringToken (engine, false, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) SkipBodyExtensions (engine, cancellationToken); // read the ')' token = engine.ReadToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) throw ImapEngine.UnexpectedToken (token, false); return body; }
/// <summary> /// Encargado de Cargar PDF a documento Electrónico /// </summary> /// <param name="docCargado"></param> /// <param name="respuestaCargaPDF"></param> /// <param name="summary"></param> /// <param name="attachment"></param> /// <param name="mime"></param> private void CargarPDF(DocumentoElectronico docCargado, out SVCDocumentosElectronicos.MensajeWCFOfBoolean respuestaCargaPDF, IMessageSummary summary, BodyPartBasic attachment) { MimePart mime = null; this.logger.LogWriter.Write(new LogEntry() { Message = String.Format("Se encuentra PDF adjunto para documento N°:{0}", docCargado.Id.ToString()), Categories = new List <string> { "General" }, Priority = 1, ProcessName = Logger.PROCESS_NAME }); mime = (MimePart)inbox.GetBodyPart(summary.UniqueId, attachment); using (MemoryStream stream = new MemoryStream()) { mime.Content.DecodeTo(stream); stream.Position = 0; respuestaCargaPDF = documentClient.CargarPDFCompra(docCargado.Id, stream.ToArray(), "pdf", -2); if (respuestaCargaPDF.CodigoError != Logger.CODIGO_EXITOSO) { this.logger.LogWriter.Write(new LogEntry() { Message = String.Format("Imposible cargar PDF adjunto para documento N°:{0}, error:{1}", docCargado.Id.ToString(), respuestaCargaPDF.MensajeError), Categories = new List <string> { "General" }, Priority = 1, ProcessName = Logger.PROCESS_NAME }); } else { this.logger.LogWriter.Write(new LogEntry() { Message = String.Format("PDF adjunto cargado para documento N°:{0}", docCargado.Id.ToString()), Categories = new List <string> { "General" }, Priority = 1, ProcessName = Logger.PROCESS_NAME }); } } }
/// <summary> /// Get NIT from XML Adjunto /// </summary> /// <param name="nit"></param> /// <param name="doc"></param> /// <param name="manager"></param> /// <param name="navigator"></param> /// <param name="query"></param> /// <param name="summary"></param> /// <param name="attachment"></param> /// <param name="mime"></param> private void GetNit(out string nit, out XPathDocument doc, IMessageSummary summary, BodyPartBasic attachment) { XmlNamespaceManager manager = null; XPathNavigator navigator = null; XPathExpression query = null; MimePart mime = null; mime = (MimePart)inbox.GetBodyPart(summary.UniqueId, attachment); using (MemoryStream stream = new MemoryStream()) { mime.Content.DecodeTo(stream); stream.Position = 0; doc = new XPathDocument(stream); } navigator = doc.CreateNavigator(); query = navigator.Compile("//fe:AccountingCustomerParty/fe:Party/cac:PartyIdentification/cbc:ID"); manager = new XmlNamespaceManager(navigator.NameTable); manager.AddNamespace("fe", "http://www.dian.gov.co/contratos/facturaelectronica/v1"); manager.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"); manager.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"); query.SetContext(manager); nit = navigator.SelectSingleNode(query).Value; }
public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken(cancellationToken); if (token.Type == ImapTokenType.Nil) { return(null); } if (token.Type != ImapTokenType.OpenParen) { throw ImapEngine.UnexpectedToken(token, false); } token = engine.PeekToken(cancellationToken); if (token.Type == ImapTokenType.OpenParen) { return(ParseMultipart(engine, path, cancellationToken)); } var type = ParseContentType(engine, cancellationToken); var id = ReadNStringToken(engine, false, cancellationToken); var desc = ReadNStringToken(engine, true, cancellationToken); // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers... var enc = ReadNStringToken(engine, false, cancellationToken); var octets = ReadNumber(engine, cancellationToken); BodyPartBasic body; if (type.Matches("message", "rfc822")) { var mesg = new BodyPartMessage(); // Note: GMail's support for message/rfc822 body parts is broken. Essentially, // GMail treats message/rfc822 parts as if they were basic body parts. // // For examples, see issue #32 and issue #59. // // The workaround is to check for the expected '(' signifying an envelope token. // If we do not get an '(', then we are likely looking at the Content-MD5 token // which gets handled below. token = engine.PeekToken(cancellationToken); if (!engine.IsGMail || token.Type == ImapTokenType.OpenParen) { mesg.Envelope = ParseEnvelope(engine, cancellationToken); mesg.Body = ParseBody(engine, path, cancellationToken); mesg.Lines = ReadNumber(engine, cancellationToken); } body = mesg; } else if (type.Matches("text", "*")) { var text = new BodyPartText(); text.Lines = ReadNumber(engine, cancellationToken); body = text; } else { body = new BodyPartBasic(); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLanguage(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ParseContentLocation(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { SkipBodyExtensions(engine, cancellationToken); } // read the ')' token = engine.ReadToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { throw ImapEngine.UnexpectedToken(token, false); } return(body); }
public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken (cancellationToken); if (token.Type == ImapTokenType.Nil) return null; if (token.Type != ImapTokenType.OpenParen) throw ImapEngine.UnexpectedToken (token, false); token = engine.PeekToken (cancellationToken); if (token.Type == ImapTokenType.OpenParen) return ParseMultipart (engine, path, cancellationToken); var type = ParseContentType (engine, cancellationToken); var id = ReadNStringToken (engine, false, cancellationToken); var desc = ReadNStringToken (engine, true, cancellationToken); // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers... var enc = ReadNStringToken (engine, false, cancellationToken); var octets = ReadNumber (engine, cancellationToken); BodyPartBasic body; if (type.Matches ("message", "rfc822")) { var mesg = new BodyPartMessage (); // Note: GMail's support for message/rfc822 body parts is broken. Essentially, // GMail treats message/rfc822 parts as if they were basic body parts. // // For examples, see issue #32 and issue #59. // // The workaround is to check for the expected '(' signifying an envelope token. // If we do not get an '(', then we are likely looking at the Content-MD5 token // which gets handled below. token = engine.PeekToken (cancellationToken); if (!engine.IsGMail || token.Type == ImapTokenType.OpenParen) { mesg.Envelope = ParseEnvelope (engine, cancellationToken); mesg.Body = ParseBody (engine, path, cancellationToken); mesg.Lines = ReadNumber (engine, cancellationToken); } body = mesg; } else if (type.Matches ("text", "*")) { var text = new BodyPartText (); text.Lines = ReadNumber (engine, cancellationToken); body = text; } else { body = new BodyPartBasic (); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken (engine, false, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLanguage (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ParseContentLocation (engine, cancellationToken); token = engine.PeekToken (cancellationToken); } if (token.Type != ImapTokenType.CloseParen) SkipBodyExtensions (engine, cancellationToken); // read the ')' token = engine.ReadToken (cancellationToken); if (token.Type != ImapTokenType.CloseParen) throw ImapEngine.UnexpectedToken (token, false); return body; }
public SaveResult saveAttachment(IMailFolder folder, IMessageSummary summary, BodyPartBasic attachment) { var result = SaveResult.Error; var entity = folder.GetBodyPart(summary.UniqueId, attachment); if (entity is MimePart) { var part = (MimePart)entity; var log = logAttachment(summary.UniqueId, part.FileName, part.ContentType); if (isIgnoreableAttachment(part.ContentType, part.FileName)) { log("Ignoring attachment"); result = SaveResult.Ignore; } else if (IsMimeTypeAcceptable(part.ContentType)) { log("MIME type ok"); result = downloader.downloadAttachment( part.Content, this.fileNamer(folder)(part)(summary) ); } else { log("MIME type bad"); } } return(result); }
/// <summary> /// Visit the basic MIME body part. /// </summary> /// <remarks> /// Visits the basic MIME body part. /// </remarks> /// <param name="entity">The basic MIME body part.</param> protected internal virtual void VisitBodyPartBasic (BodyPartBasic entity) { VisitBodyPart (entity); }
public static BodyPart ParseBody(ImapEngine engine, string path, CancellationToken cancellationToken) { var token = engine.ReadToken(cancellationToken); if (token.Type == ImapTokenType.Nil) { return(null); } if (token.Type != ImapTokenType.OpenParen) { throw ImapEngine.UnexpectedToken(token, false); } token = engine.PeekToken(cancellationToken); if (token.Type == ImapTokenType.OpenParen) { return(ParseMultipart(engine, path, cancellationToken)); } var type = ParseContentType(engine, cancellationToken); var id = ReadNStringToken(engine, false, cancellationToken); var desc = ReadNStringToken(engine, true, cancellationToken); // Note: technically, body-fld-enc, is not allowed to be NIL, but we need to deal with broken servers... var enc = ReadNStringToken(engine, false, cancellationToken); var octets = ReadNumber(engine, cancellationToken); BodyPartBasic body; if (type.Matches("message", "rfc822")) { var mesg = new BodyPartMessage(); // Note: GMail (and potentially other IMAP servers) will send body-part-basic // expressions instead of body-part-msg expressions when they encounter // message/rfc822 MIME parts that are illegally encoded using base64 (or // quoted-printable?). According to rfc3501, IMAP servers are REQUIRED to // send body-part-msg expressions for message/rfc822 parts, however, it is // understandable why GMail (and other IMAP servers?) do what they do in this // particular case. // // For examples, see issue #32 and issue #59. // // The workaround is to check for the expected '(' signifying an envelope token. // If we do not get an '(', then we are likely looking at the Content-MD5 token // which gets handled below. token = engine.PeekToken(cancellationToken); if (token.Type == ImapTokenType.OpenParen) { mesg.Envelope = ParseEnvelope(engine, cancellationToken); mesg.Body = ParseBody(engine, path, cancellationToken); mesg.Lines = ReadNumber(engine, cancellationToken); } body = mesg; } else if (type.Matches("text", "*")) { var text = new BodyPartText(); text.Lines = ReadNumber(engine, cancellationToken); body = text; } else { body = new BodyPartBasic(); } body.ContentTransferEncoding = enc; body.ContentDescription = desc; body.PartSpecifier = path; body.ContentType = type; body.ContentId = id; body.Octets = octets; // if we are parsing a BODYSTRUCTURE, we may get some more tokens before the ')' token = engine.PeekToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { body.ContentMd5 = ReadNStringToken(engine, false, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentDisposition = ParseContentDisposition(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLanguage = ParseContentLanguage(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { body.ContentLocation = ParseContentLocation(engine, cancellationToken); token = engine.PeekToken(cancellationToken); } if (token.Type != ImapTokenType.CloseParen) { SkipBodyExtensions(engine, cancellationToken); } // read the ')' token = engine.ReadToken(cancellationToken); if (token.Type != ImapTokenType.CloseParen) { throw ImapEngine.UnexpectedToken(token, false); } return(body); }