private async Task <bool> ZFetchToStream(cUID pUID, cSection pSection, eDecodingRequired pDecoding, string pTitle, Stream pStream) { frmProgress lProgress = null; try { lProgress = new frmProgress(pTitle); ZChildAdd(lProgress); await mClient.SelectedMailbox.UIDFetchAsync(pUID, pSection, pDecoding, pStream, new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment)); return(true); } catch (OperationCanceledException) { } catch (Exception ex) { if (!IsDisposed) { MessageBox.Show(this, $"problem when fetching\n{ex}"); } } finally { if (lProgress != null) { lProgress.Complete(); } } return(false); }
internal cSinglePartBody(string pType, string pSubType, cSection pSection, cBodyStructureParameters pParameters, string pContentId, cCulturedString pDescription, string pContentTransferEncoding, uint pSizeInBytes, cSinglePartExtensionData pExtensionData) : base(pType, pSubType, pSection) { Parameters = pParameters; ContentId = pContentId; Description = pDescription; ContentTransferEncoding = pContentTransferEncoding ?? throw new ArgumentNullException(nameof(pContentTransferEncoding)); if (ContentTransferEncoding.Equals("7BIT", StringComparison.InvariantCultureIgnoreCase)) { DecodingRequired = eDecodingRequired.none; } else if (ContentTransferEncoding.Equals("8BIT", StringComparison.InvariantCultureIgnoreCase)) { DecodingRequired = eDecodingRequired.none; } else if (ContentTransferEncoding.Equals("BINARY", StringComparison.InvariantCultureIgnoreCase)) { DecodingRequired = eDecodingRequired.none; } else if (ContentTransferEncoding.Equals("QUOTED-PRINTABLE", StringComparison.InvariantCultureIgnoreCase)) { DecodingRequired = eDecodingRequired.quotedprintable; } else if (ContentTransferEncoding.Equals("BASE64", StringComparison.InvariantCultureIgnoreCase)) { DecodingRequired = eDecodingRequired.base64; } else { DecodingRequired = eDecodingRequired.other; // note that rfc 2045 section 6.4 specifies that if 'unknown' then the part has to be treated as application/octet-stream } SizeInBytes = pSizeInBytes; ExtensionData = pExtensionData; }
internal Task FetchAsync(iMessageHandle pMessageHandle, cSection pSection, eDecodingRequired pDecoding, Stream pStream, cBodyFetchConfiguration pConfiguration) { // note: if it fails bytes could have been written to the stream var lContext = mRootContext.NewMethod(nameof(cIMAPClient), nameof(FetchAsync)); return(ZFetchBodyAsync(pMessageHandle, pSection, pDecoding, pStream, pConfiguration, lContext)); }
internal void Fetch(iMessageHandle pMessageHandle, cSection pSection, eDecodingRequired pDecoding, Stream pStream, cBodyFetchConfiguration pConfiguration) { // note: if it fails bytes could have been written to the stream var lContext = mRootContext.NewMethod(nameof(cIMAPClient), nameof(Fetch)); var lTask = ZFetchBodyAsync(pMessageHandle, pSection, pDecoding, pStream, pConfiguration, lContext); mSynchroniser.Wait(lTask, lContext); }
public frmMessageData(cUID pUID, cSection pSection, eDecodingRequired pDecoding, string pData) { mUID = pUID; mSection = pSection; mDecoding = pDecoding; mData = pData; InitializeComponent(); }
private async Task ZUIDFetchBodyAsync(iMailboxHandle pMailboxHandle, cUID pUID, cSection pSection, eDecodingRequired pDecoding, Stream pStream, cBodyFetchConfiguration pConfiguration, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cIMAPClient), nameof(ZUIDFetchBodyAsync), pMailboxHandle, pUID, pSection, pDecoding); if (mDisposed) { throw new ObjectDisposedException(nameof(cIMAPClient)); } var lSession = mSession; if (lSession == null || lSession.ConnectionState != eConnectionState.selected) { throw new InvalidOperationException(kInvalidOperationExceptionMessage.NotSelected); } if (pMailboxHandle == null) { throw new ArgumentNullException(nameof(pMailboxHandle)); } if (pUID == null) { throw new ArgumentNullException(nameof(pUID)); } if (pSection == null) { throw new ArgumentNullException(nameof(pSection)); } if (pStream == null) { throw new ArgumentNullException(nameof(pStream)); } if (!pStream.CanWrite) { throw new ArgumentOutOfRangeException(nameof(pStream)); } if (pConfiguration == null) { using (var lToken = mCancellationManager.GetToken(lContext)) { var lMC = new cMethodControl(mTimeout, lToken.CancellationToken); var lProgress = new cProgress(); var lWriteSizer = new cBatchSizer(mFetchBodyWriteConfiguration); await lSession.UIDFetchBodyAsync(lMC, pMailboxHandle, pUID, pSection, pDecoding, pStream, lProgress, lWriteSizer, lContext).ConfigureAwait(false); } } else { var lMC = new cMethodControl(pConfiguration.Timeout, pConfiguration.CancellationToken); var lProgress = new cProgress(mSynchroniser, pConfiguration.Increment); var lWriteSizer = new cBatchSizer(pConfiguration.Write ?? mFetchBodyWriteConfiguration); await lSession.UIDFetchBodyAsync(lMC, pMailboxHandle, pUID, pSection, pDecoding, pStream, lProgress, lWriteSizer, lContext).ConfigureAwait(false); } }
private bool ZFetchParameters(out cUID rUID, out cSection rSection, out eDecodingRequired rDecoding) { rUID = null; rSection = null; rDecoding = eDecodingRequired.other; if (!ValidateChildren(ValidationConstraints.Enabled)) { MessageBox.Show(this, "there are issues with the data entered"); return(false); } if (string.IsNullOrWhiteSpace(txtUID.Text)) { MessageBox.Show(this, "you must enter a UID"); return(false); } try { rUID = new cUID(uint.Parse(txtUIDValidity.Text), uint.Parse(txtUID.Text)); string lPart; if (string.IsNullOrWhiteSpace(txtPart.Text)) { lPart = null; } else { lPart = txtPart.Text.Trim(); } eSectionTextPart lTextPart; if (rdoAll.Checked) { lTextPart = eSectionTextPart.all; } else if (rdoHeader.Checked) { lTextPart = eSectionTextPart.header; } else if (rdoFields.Checked) { lTextPart = eSectionTextPart.headerfields; } else if (rdoFieldsNot.Checked) { lTextPart = eSectionTextPart.headerfieldsnot; } else if (rdoText.Checked) { lTextPart = eSectionTextPart.text; } else { lTextPart = eSectionTextPart.mime; } if (lTextPart == eSectionTextPart.headerfields || lTextPart == eSectionTextPart.headerfieldsnot) { if (string.IsNullOrWhiteSpace(txtFieldNames.Text)) { MessageBox.Show(this, "must enter some field names"); return(false); } if (!ZTryParseHeaderFieldNames(txtFieldNames.Text, out var lNames)) { MessageBox.Show(this, "must enter valid field names"); return(false); } rSection = new cSection(lPart, lNames, rdoFieldsNot.Checked); } else { rSection = new cSection(lPart, lTextPart); } if (rdoNone.Checked) { rDecoding = eDecodingRequired.none; } else if (rdoQuotedPrintable.Checked) { rDecoding = eDecodingRequired.quotedprintable; } else if (rdoQuotedPrintable.Checked) { rDecoding = eDecodingRequired.base64; } return(true); } catch (Exception e) { MessageBox.Show(this, $"there are issues with the data entered:\n{e.ToString()}"); return(false); } }
private async Task ZFetchBodyAsync(cMethodControl pMC, iMailboxHandle pMailboxHandle, cUID pUID, iMessageHandle pMessageHandle, cSection pSection, eDecodingRequired pDecoding, Stream pStream, cProgress pProgress, cBatchSizer pWriteSizer, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cSession), nameof(ZFetchBodyAsync), pMC, pMailboxHandle, pUID, pMessageHandle, pSection, pDecoding); if (pSection == null) { throw new ArgumentNullException(nameof(pSection)); } if (pStream == null) { throw new ArgumentNullException(nameof(pStream)); } if (pProgress == null) { throw new ArgumentNullException(nameof(pProgress)); } if (pWriteSizer == null) { throw new ArgumentNullException(nameof(pWriteSizer)); } if (!pStream.CanWrite) { throw new ArgumentOutOfRangeException(nameof(pStream)); } // work out if binary can/should be used or not bool lBinary = _Capabilities.Binary && pSection.TextPart == eSectionTextPart.all && pDecoding != eDecodingRequired.none; cDecoder lDecoder; if (lBinary || pDecoding == eDecodingRequired.none) { lDecoder = new cIdentityDecoder(pStream); } else if (pDecoding == eDecodingRequired.base64) { lDecoder = new cBase64Decoder(pStream); } else if (pDecoding == eDecodingRequired.quotedprintable) { lDecoder = new cQuotedPrintableDecoder(pStream); } else { throw new cContentTransferDecodingException("required decoding not supported", lContext); } uint lOrigin = 0; Stopwatch lStopwatch = new Stopwatch(); while (true) { int lLength = mFetchBodyReadSizer.Current; lStopwatch.Restart(); cBody lBody; if (pUID == null) { lBody = await ZFetchBodyAsync(pMC, pMessageHandle, lBinary, pSection, lOrigin, (uint)lLength, lContext).ConfigureAwait(false); } else { lBody = await ZUIDFetchBodyAsync(pMC, pMailboxHandle, pUID, lBinary, pSection, lOrigin, (uint)lLength, lContext).ConfigureAwait(false); } lStopwatch.Stop(); // store the time taken so the next fetch is a better size mFetchBodyReadSizer.AddSample(lBody.Bytes.Count, lStopwatch.ElapsedMilliseconds); uint lBodyOrigin = lBody.Origin ?? 0; // the body that we get may start before the place that we asked for int lOffset = (int)(lOrigin - lBodyOrigin); // write the bytes await lDecoder.WriteAsync(pMC, lBody.Bytes, lOffset, pWriteSizer, lContext).ConfigureAwait(false); // update progress pProgress.Increment(lBody.Bytes.Count - lOffset, lContext); // if the body we got was the whole body, we are done if (lBody.Origin == null) { break; } // if we got less bytes than asked for then we will assume that we are at the end if (lBody.Bytes.Count - lOffset < lLength) { break; } // set the start point for the next fetch lOrigin = lBodyOrigin + (uint)lBody.Bytes.Count; } await lDecoder.FlushAsync(pMC, pWriteSizer, lContext).ConfigureAwait(false); }
public Task UIDFetchBodyAsync(cMethodControl pMC, iMailboxHandle pMailboxHandle, cUID pUID, cSection pSection, eDecodingRequired pDecoding, Stream pStream, cProgress pProgress, cBatchSizer pWriteSizer, cTrace.cContext pParentContext) { var lContext = pParentContext.NewMethod(nameof(cSession), nameof(UIDFetchBodyAsync), pMC, pMailboxHandle, pUID, pSection, pDecoding); if (mDisposed) { throw new ObjectDisposedException(nameof(cSession)); } if (_ConnectionState != eConnectionState.selected) { throw new InvalidOperationException(kInvalidOperationExceptionMessage.NotSelected); } mMailboxCache.CheckIsSelectedMailbox(pMailboxHandle, pUID.UIDValidity); // to be repeated inside the select lock return(ZFetchBodyAsync(pMC, pMailboxHandle, pUID, null, pSection, pDecoding, pStream, pProgress, pWriteSizer, lContext)); }