Exemple #1
0
 private void UnprotectAllAttachments()
 {
     if (!(this.decodedItem.TryGetProperty(MessageItemSchema.DRMServerLicense) is string))
     {
         return;
     }
     foreach (AttachmentHandle handle in this.decodedItem.AttachmentCollection)
     {
         using (Attachment attachment = this.decodedItem.AttachmentCollection.Open(handle))
         {
             Stream           stream           = null;
             StreamAttachment streamAttachment = attachment as StreamAttachment;
             if (StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream))
             {
                 using (stream)
                 {
                     using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
                     {
                         Util.StreamHandler.CopyStreamData(stream, contentStream);
                     }
                 }
                 attachment.Save();
             }
         }
     }
 }
Exemple #2
0
        private static void CopyProtectableData(Body sourceBody, AttachmentCollection sourceAttachmentCollection, MessageItem targetItem)
        {
            if (sourceBody.IsBodyDefined)
            {
                using (Stream stream = sourceBody.OpenReadStream(new BodyReadConfiguration(sourceBody.Format, sourceBody.RawCharset.Name)))
                {
                    using (Stream stream2 = targetItem.Body.OpenWriteStream(new BodyWriteConfiguration(sourceBody.Format, sourceBody.RawCharset)))
                    {
                        Util.StreamHandler.CopyStreamData(stream, stream2);
                    }
                    goto IL_6D;
                }
            }
            targetItem.DeleteProperties(Body.BodyProps);
IL_6D:
            targetItem.AttachmentCollection.RemoveAll();
            foreach (AttachmentHandle handle in sourceAttachmentCollection)
            {
                using (Attachment attachment = sourceAttachmentCollection.Open(handle))
                {
                    using (Attachment attachment2 = attachment.CreateCopy(targetItem.AttachmentCollection, new BodyFormat?(targetItem.Body.Format)))
                    {
                        attachment2.Save();
                    }
                }
            }
        }
Exemple #3
0
 internal void SaveAttachment()
 {
     this.CheckDisposed("SaveAttachment");
     if (this.attachMethod == null)
     {
         this.ResetAttachMethod(1);
     }
     using (DisposeGuard disposeGuard = default(DisposeGuard))
     {
         disposeGuard.Add <CoreAttachment>(this.coreAttachment);
         using (Attachment attachment = AttachmentCollection.CreateTypedAttachment(this.coreAttachment, null))
         {
             attachment.SaveFlags |= PropertyBagSaveFlags.IgnoreMapiComputedErrors;
             attachment.Save();
         }
         disposeGuard.Success();
     }
 }
 internal static void CloneAttachmentCollection(ICoreItem sourceItem, ICoreItem destinationItem)
 {
     foreach (AttachmentHandle handle in sourceItem.AttachmentCollection)
     {
         using (CoreAttachment coreAttachment = sourceItem.AttachmentCollection.Open(handle, CoreObjectSchema.AllPropertiesOnStore))
         {
             using (CoreAttachment coreAttachment2 = destinationItem.AttachmentCollection.CreateCopy(coreAttachment))
             {
                 using (Attachment attachment = AttachmentCollection.CreateTypedAttachment(coreAttachment2, new AttachmentType?(coreAttachment2.AttachmentType)))
                 {
                     attachment.SaveFlags |= (coreAttachment.PropertyBag.SaveFlags | PropertyBagSaveFlags.IgnoreMapiComputedErrors);
                     attachment.Save();
                 }
             }
         }
     }
     destinationItem.AttachmentCollection.IsClonedFromAnExistingAttachmentCollection = (sourceItem.AttachmentCollection.Count > 0 && !sourceItem.AttachmentCollection.IsDirty && sourceItem.AttachmentCollection.Count == destinationItem.AttachmentCollection.Count);
 }
Exemple #5
0
 public void UnprotectAttachment(AttachmentId attachmentId)
 {
     using (Attachment attachment = this.ProtectedAttachmentCollection.Open(attachmentId))
     {
         StreamAttachment streamAttachment = attachment as StreamAttachment;
         if (streamAttachment == null)
         {
             throw new ObjectNotFoundException(ServerStrings.MapiCannotOpenAttachment);
         }
         using (Stream stream = StreamAttachment.OpenRestrictedContent(streamAttachment, this.orgId))
         {
             using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
             {
                 Util.StreamHandler.CopyStreamData(stream, contentStream);
             }
         }
         attachment.Save();
     }
 }
        protected void CopyAttachments(BodyConversionCallbacks callbacks, AttachmentCollection sourceCollection, AttachmentCollection targetCollection, bool copyInlinesOnly, bool targetIsPlainText, InboundConversionOptions optionsForSmime)
        {
            ReadOnlyCollection <AttachmentLink> readOnlyCollection = null;
            bool flag = true;

            if (callbacks.HtmlCallback != null && callbacks.HtmlCallback.AttachmentListInitialized)
            {
                readOnlyCollection = callbacks.HtmlCallback.AttachmentLinks;
                flag = (flag && callbacks.HtmlCallback.ClearInlineOnUnmarkedAttachments);
            }
            if (callbacks.RtfCallback != null && callbacks.RtfCallback.AttachmentListInitialized)
            {
                readOnlyCollection = callbacks.RtfCallback.AttachmentLinks;
                flag = (flag && callbacks.RtfCallback.ClearInlineOnUnmarkedAttachments);
            }
            if (readOnlyCollection == null)
            {
                if (copyInlinesOnly)
                {
                    return;
                }
                using (IEnumerator <AttachmentHandle> enumerator = sourceCollection.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        AttachmentHandle handle = enumerator.Current;
                        using (Attachment attachment = sourceCollection.Open(handle, null))
                        {
                            if (!attachment.IsCalendarException)
                            {
                                using (Attachment attachment2 = attachment.CreateCopy(targetCollection, new BodyFormat?(this.parameters.TargetFormat)))
                                {
                                    attachment2.IsInline = false;
                                    attachment2.Save();
                                }
                            }
                        }
                    }
                    return;
                }
            }
            foreach (AttachmentLink attachmentLink in readOnlyCollection)
            {
                if (!copyInlinesOnly || attachmentLink.IsInline(flag))
                {
                    using (Attachment attachment3 = sourceCollection.Open(attachmentLink.AttachmentId, null))
                    {
                        if (!copyInlinesOnly || !(attachment3 is ReferenceAttachment))
                        {
                            using (Attachment attachment4 = attachment3.CreateCopy(targetCollection, new BodyFormat?(this.parameters.TargetFormat)))
                            {
                                attachmentLink.MakeAttachmentChanges(attachment4, flag);
                                if (targetIsPlainText)
                                {
                                    attachment4.IsInline = false;
                                }
                                attachment4.Save();
                            }
                        }
                    }
                }
            }
        }
Exemple #7
0
        public virtual bool SaveChanges()
        {
            if (this.AttachmentCollection == null)
            {
                throw new InvalidOperationException("Target item not specified; callback cannot invoke attachment-specific methods");
            }
            bool result = false;

            if (this.attachmentLinks != null)
            {
                using (IEnumerator <AttachmentLink> enumerator = this.attachmentLinks.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        AttachmentLink attachmentLink = enumerator.Current;
                        using (CoreAttachment coreAttachment = this.AttachmentCollection.Open(attachmentLink.AttachmentId))
                        {
                            using (Attachment attachment = Microsoft.Exchange.Data.Storage.AttachmentCollection.CreateTypedAttachment(coreAttachment, null))
                            {
                                if (this.removeUnlinkedAttachments && !attachmentLink.IsInline(this.requireMarkInline) && attachment.IsInline)
                                {
                                    attachment.Dispose();
                                    this.AttachmentCollection.Remove(attachmentLink.AttachmentId);
                                }
                                else
                                {
                                    if (attachmentLink.NeedsConversionToImage && attachment.AttachmentType == AttachmentType.Ole)
                                    {
                                        OleAttachment oleAttachment = attachment as OleAttachment;
                                        if (oleAttachment == null)
                                        {
                                            continue;
                                        }
                                        using (Attachment attachment2 = oleAttachment.ConvertToImageAttachment(this.AttachmentCollection, ImageFormat.Jpeg))
                                        {
                                            result = true;
                                            attachmentLink.MakeAttachmentChanges(attachment2, this.requireMarkInline);
                                            attachment2.Save();
                                            continue;
                                        }
                                    }
                                    if (attachmentLink.MakeAttachmentChanges(attachment, this.requireMarkInline))
                                    {
                                        result = true;
                                        attachment.Save();
                                    }
                                }
                            }
                        }
                    }
                    return(result);
                }
            }
            if (this.requireMarkInline)
            {
                List <AttachmentId> list = null;
                foreach (AttachmentHandle handle in this.AttachmentCollection)
                {
                    using (CoreAttachment coreAttachment2 = this.AttachmentCollection.Open(handle))
                    {
                        if (coreAttachment2.IsInline)
                        {
                            if (this.removeUnlinkedAttachments)
                            {
                                if (list == null)
                                {
                                    list = new List <AttachmentId>();
                                }
                                list.Add(coreAttachment2.Id);
                            }
                            else
                            {
                                coreAttachment2.IsInline = false;
                                result = true;
                                coreAttachment2.Save();
                            }
                        }
                    }
                }
                if (list != null)
                {
                    foreach (AttachmentId id in list)
                    {
                        this.AttachmentCollection.Remove(id);
                    }
                }
            }
            return(result);
        }
Exemple #8
0
        private void DecryptMsg(MessageItem rightsProtectedMessage, string useLicense, SafeRightsManagementHandle decryptorHandle)
        {
            if (decryptorHandle == null)
            {
                throw new ArgumentNullException("decryptorHandle");
            }
            if (decryptorHandle.IsInvalid)
            {
                throw new ArgumentException("decryptorHandle");
            }
            this.originalItem = rightsProtectedMessage;
            DrmEmailMessage drmEmailMessage = null;
            bool            flag            = false;

            try
            {
                ExTraceGlobals.RightsManagementTracer.TraceDebug((long)this.GetHashCode(), "Found a use license for server. Decrypting message");
                DrmEmailMessageBinding messageBinding = new DrmEmailMessageBinding(this.drmMsgContainer.PublishLicense, decryptorHandle);
                this.drmMsgContainer.Bind(messageBinding, new CreateStreamCallbackDelegate(this.BodyStreamCallback), new CreateStreamCallbackDelegate(this.AttachmentsStreamCallback));
                drmEmailMessage = this.drmMsgContainer.EmailMessage;
                this.SaveAndCloseCurrentAttachment();
                this.decryptedItem.SafeSetProperty(InternalSchema.DRMServerLicense, useLicense);
                this.decryptedItem.SafeSetProperty(InternalSchema.DrmPublishLicense, this.drmMsgContainer.PublishLicense);
                if (drmEmailMessage.Attachments.Count > 0)
                {
                    ExTraceGlobals.RightsManagementTracer.TraceDebug <int>((long)this.GetHashCode(), "Number of attachments in the rights protected message : {0}", drmEmailMessage.Attachments.Count);
                    int num = 0;
                    foreach (DrmEmailAttachment drmEmailAttachment in drmEmailMessage.Attachments)
                    {
                        using (Attachment attachment = this.decryptedItem.AttachmentCollection.Open(this.messageAttachmentIds[num++], null))
                        {
                            attachment.FileName = drmEmailAttachment.FileName;
                            attachment[InternalSchema.DisplayName] = drmEmailAttachment.DisplayName;
                            attachment.ContentId = drmEmailAttachment.ContentId;
                            attachment[InternalSchema.AttachContentLocation] = drmEmailAttachment.ContentLocation;
                            attachment[InternalSchema.AttachMhtmlFlags]      = drmEmailAttachment.AttachFlags;
                            attachment[InternalSchema.AttachCalendarHidden]  = drmEmailAttachment.AttachHidden;
                            if (drmEmailAttachment.AttachHidden)
                            {
                                attachment.IsInline = true;
                            }
                            if (drmEmailMessage.BodyFormat == BodyFormat.Rtf)
                            {
                                attachment.RenderingPosition = (int)drmEmailAttachment.CharacterPosition;
                                if (attachment.AttachmentType == AttachmentType.EmbeddedMessage || attachment.AttachmentType == AttachmentType.Stream)
                                {
                                    attachment[InternalSchema.AttachRendering] = drmEmailAttachment.AttachRendering;
                                }
                            }
                            if (this.decryptAttachments)
                            {
                                Stream           stream           = null;
                                StreamAttachment streamAttachment = attachment as StreamAttachment;
                                if (streamAttachment != null && StreamAttachment.TryOpenRestrictedContent(streamAttachment, this.orgId, out stream))
                                {
                                    using (stream)
                                    {
                                        using (Stream contentStream = streamAttachment.GetContentStream(PropertyOpenMode.Create))
                                        {
                                            Util.StreamHandler.CopyStreamData(stream, contentStream);
                                        }
                                    }
                                }
                            }
                            attachment.Save();
                        }
                    }
                }
                drmEmailMessage.Close();
                drmEmailMessage = null;
                if (this.originalItem != null)
                {
                    PersistablePropertyBag.CopyProperty(this.originalItem.PropertyBag, InternalSchema.TransportMessageHeaders, this.decryptedItem.PropertyBag);
                    this.decryptedItem.Recipients.CopyRecipientsFrom(this.originalItem.Recipients);
                }
                this.decryptedItem.Save(SaveMode.NoConflictResolution);
                this.decryptedItem.Load(InternalSchema.ContentConversionProperties);
                flag = true;
            }
            finally
            {
                if (drmEmailMessage != null)
                {
                    drmEmailMessage.Close();
                    drmEmailMessage = null;
                }
                if (!flag && this.decryptedItem != null)
                {
                    this.decryptedItem.Dispose();
                    this.decryptedItem = null;
                }
            }
        }