private static Stream GetStreamAttachmentStream(Attachment attachment)
        {
            StreamAttachmentBase streamAttachmentBase = attachment as StreamAttachmentBase;

            if (streamAttachmentBase == null)
            {
                throw new ArgumentNullException("stream", "Attachment is not a stream attachment");
            }
            OleAttachment oleAttachment = streamAttachmentBase as OleAttachment;
            Stream        stream;

            if (oleAttachment != null)
            {
                stream = oleAttachment.TryConvertToImage(ImageFormat.Jpeg);
                if (stream == null)
                {
                    stream = new MemoryStream();
                }
            }
            else
            {
                stream = streamAttachmentBase.GetContentStream(PropertyOpenMode.ReadOnly);
            }
            return(stream);
        }
        // Token: 0x0600071C RID: 1820 RVA: 0x00037B9C File Offset: 0x00035D9C
        private void ProcessAttachment(Attachment attachment, HttpContext httpContext, BlockStatus blockStatus)
        {
            if (attachment == null)
            {
                throw new ArgumentNullException("attachment");
            }
            OwaContext  owaContext  = OwaContext.Get(httpContext);
            UserContext userContext = owaContext.UserContext;
            Stream      stream      = null;

            try
            {
                StreamAttachmentBase streamAttachment = AttachmentUtility.GetStreamAttachment(attachment);
                if (streamAttachment == null)
                {
                    ExTraceGlobals.AttachmentHandlingTracer.TraceError((long)this.GetHashCode(), "AttachmentHandler.ProcessAttachment: attachment is not derived from AttachmentStream");
                }
                else
                {
                    AttachmentPolicy.Level attachmentLevel = AttachmentLevelLookup.GetAttachmentLevel(streamAttachment, userContext);
                    if (attachmentLevel == AttachmentPolicy.Level.Block)
                    {
                        Utilities.TransferToErrorPage(owaContext, LocalizedStrings.GetNonEncoded(-2000404449), LocalizedStrings.GetNonEncoded(-615885395));
                    }
                    else
                    {
                        string fileName = AttachmentUtility.CalculateAttachmentName(streamAttachment.DisplayName, streamAttachment.FileName);
                        string text     = streamAttachment.FileExtension;
                        if (text == null)
                        {
                            text = string.Empty;
                        }
                        string empty       = string.Empty;
                        bool   contentType = this.GetContentType(httpContext, streamAttachment, attachmentLevel, out empty);
                        bool   isInline    = this.GetIsInline(streamAttachment, attachmentLevel);
                        stream = AttachmentUtility.GetStream(streamAttachment);
                        if (stream == null)
                        {
                            ExTraceGlobals.AttachmentHandlingTracer.TraceError((long)this.GetHashCode(), "AttachmentHandler.ProcessAttachment: Image conversion of OLE attachment failure");
                        }
                        else
                        {
                            AttachmentHandler.SendDocumentContentToHttpStream(httpContext, stream, fileName, text, empty, isInline, streamAttachment.TextCharset, blockStatus, attachmentLevel, contentType);
                        }
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
        // Token: 0x0600071F RID: 1823 RVA: 0x00037D98 File Offset: 0x00035F98
        private bool GetContentType(HttpContext httpContext, StreamAttachmentBase streamAttachment, AttachmentPolicy.Level level, out string contentType)
        {
            contentType = AttachmentUtility.CalculateContentType(streamAttachment);
            bool flag = AttachmentUtility.DoNeedToFilterHtml(contentType, streamAttachment.FileExtension, level, OwaContext.Get(httpContext).UserContext);

            if (string.IsNullOrEmpty(contentType) && this.IsVoiceMailAttachment(httpContext))
            {
                contentType = "audio/x-ms-wma";
            }
            else if (flag)
            {
                AttachmentUtility.UpdateContentTypeForNeedToFilter(out contentType, streamAttachment.TextCharset);
            }
            return(flag);
        }
        // Token: 0x0600155B RID: 5467 RVA: 0x0007CE68 File Offset: 0x0007B068
        protected void CopyMessageContents(MessageItem source, MessageItem destination, bool copyOriginalRecipients, Item attachmentItem)
        {
            source.Load();
            string inReplyTo  = destination.InReplyTo;
            string references = destination.References;

            destination.Subject           = ((source.TryGetProperty(ItemSchema.SubjectPrefix) as string) ?? string.Empty) + ((source.TryGetProperty(ItemSchema.NormalizedSubject) as string) ?? string.Empty);
            destination.InReplyTo         = inReplyTo;
            destination.Importance        = source.Importance;
            destination.InternetMessageId = source.InternetMessageId;
            destination.Categories.Clear();
            destination.Categories.AddRange(source.Categories);
            destination.IconIndex = source.IconIndex;
            destination.IsRead    = source.IsRead;
            destination.IsReadReceiptRequested = source.IsReadReceiptRequested;
            destination.References             = references;
            if (copyOriginalRecipients)
            {
                this.originalRecipients = new HashSet <RecipientId>();
                foreach (Recipient recipient in destination.Recipients)
                {
                    this.originalRecipients.Add(recipient.Id);
                }
            }
            destination.Recipients.Clear();
            foreach (Recipient recipient2 in source.Recipients)
            {
                destination.Recipients.Add(recipient2.Participant, recipient2.RecipientItemType);
            }
            if (source.ReplyTo != null)
            {
                foreach (Participant item in source.ReplyTo)
                {
                    destination.ReplyTo.Add(item);
                }
            }
            RightsManagedMessageItem rightsManagedMessageItem = destination as RightsManagedMessageItem;
            AttachmentCollection     attachmentCollection     = (rightsManagedMessageItem != null) ? rightsManagedMessageItem.ProtectedAttachmentCollection : destination.AttachmentCollection;

            if (rightsManagedMessageItem == null && attachmentItem != null)
            {
                throw new InvalidOperationException("attachmentItem must be null for non-IRM messages");
            }
            if (this.ReplaceMime || attachmentItem != null)
            {
                if (rightsManagedMessageItem == null)
                {
                    Body.CopyBody(source, destination, GlobalSettings.DisableCharsetDetectionInCopyMessageContents);
                }
                else
                {
                    using (Stream stream = source.Body.OpenReadStream(new BodyReadConfiguration(source.Body.Format, source.Body.Charset)))
                    {
                        using (Stream stream2 = rightsManagedMessageItem.ProtectedBody.OpenWriteStream(new BodyWriteConfiguration(source.Body.Format, source.Body.Charset)))
                        {
                            Util.StreamHandler.CopyStreamData(stream, stream2);
                        }
                    }
                }
            }
            foreach (AttachmentHandle handle in source.AttachmentCollection)
            {
                using (Attachment attachment = source.AttachmentCollection.Open(handle))
                {
                    int num = 0;
                    if (attachment is StreamAttachment)
                    {
                        num = 1;
                    }
                    else if (attachment is OleAttachment)
                    {
                        num = 2;
                    }
                    if (num > 0)
                    {
                        Microsoft.Exchange.Data.Storage.AttachmentType type = (num == 1) ? Microsoft.Exchange.Data.Storage.AttachmentType.Stream : Microsoft.Exchange.Data.Storage.AttachmentType.Ole;
                        StreamAttachmentBase streamAttachmentBase           = (StreamAttachmentBase)attachment;
                        using (StreamAttachmentBase streamAttachmentBase2 = (StreamAttachmentBase)attachmentCollection.Create(type))
                        {
                            using (Stream contentStream = streamAttachmentBase2.GetContentStream())
                            {
                                using (Stream contentStream2 = streamAttachmentBase.GetContentStream())
                                {
                                    int    num2   = 4096;
                                    byte[] buffer = new byte[num2];
                                    int    count;
                                    while ((count = contentStream2.Read(buffer, 0, num2)) > 0)
                                    {
                                        contentStream.Write(buffer, 0, count);
                                    }
                                    streamAttachmentBase2.ContentType = streamAttachmentBase.ContentType;
                                    streamAttachmentBase2[AttachmentSchema.DisplayName] = streamAttachmentBase.DisplayName;
                                    streamAttachmentBase2.FileName = streamAttachmentBase.FileName;
                                    streamAttachmentBase2[AttachmentSchema.AttachContentId] = streamAttachmentBase[AttachmentSchema.AttachContentId];
                                    streamAttachmentBase2.IsInline = streamAttachmentBase.IsInline;
                                }
                            }
                            streamAttachmentBase2.Save();
                            continue;
                        }
                    }
                    ItemAttachment itemAttachment = (ItemAttachment)attachment;
                    using (Item item2 = itemAttachment.GetItem())
                    {
                        using (ItemAttachment itemAttachment2 = attachmentCollection.AddExistingItem(item2))
                        {
                            itemAttachment2.Save();
                        }
                    }
                }
            }
            if (attachmentItem != null)
            {
                using (ItemAttachment itemAttachment3 = attachmentCollection.AddExistingItem(attachmentItem))
                {
                    itemAttachment3.Save();
                }
            }
        }
        private static string GetAttachmentItself(Attachment attachment, Stream outStream, int offset, int count, out int total)
        {
            string text = string.Empty;

            total = 0;
            StreamAttachmentBase streamAttachmentBase = attachment as StreamAttachmentBase;

            if (streamAttachmentBase != null)
            {
                OleAttachment oleAttachment = streamAttachmentBase as OleAttachment;
                Stream        stream        = null;
                try
                {
                    if (oleAttachment != null)
                    {
                        stream = oleAttachment.TryConvertToImage(ImageFormat.Jpeg);
                        if (stream != null)
                        {
                            text = "image/jpeg";
                        }
                    }
                    if (stream == null)
                    {
                        stream = streamAttachmentBase.GetContentStream();
                    }
                    if (string.IsNullOrEmpty(text))
                    {
                        text = attachment.ContentType;
                    }
                    if (string.IsNullOrEmpty(text))
                    {
                        text = attachment.CalculatedContentType;
                    }
                    if (stream.Length > 0L)
                    {
                        if ((long)offset >= stream.Length)
                        {
                            throw new ArgumentOutOfRangeException("offset");
                        }
                        int num = (count == -1) ? ((int)stream.Length) : count;
                        if (num > GlobalSettings.MaxDocumentDataSize)
                        {
                            throw new DataTooLargeException(StatusCode.AttachmentIsTooLarge);
                        }
                        StreamHelper.CopyStream(stream, outStream, offset, num);
                        total = (int)stream.Length;
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                        stream = null;
                    }
                }
                return(text);
            }
            ItemAttachment itemAttachment = attachment as ItemAttachment;

            if (itemAttachment != null)
            {
                using (Item item = itemAttachment.GetItem(StoreObjectSchema.ContentConversionProperties))
                {
                    text = "message/rfc822";
                    OutboundConversionOptions outboundConversionOptions = AirSyncUtility.GetOutboundConversionOptions();
                    using (AirSyncStream airSyncStream = new AirSyncStream())
                    {
                        try
                        {
                            ItemConversion.ConvertItemToMime(item, airSyncStream, outboundConversionOptions);
                        }
                        catch (ConversionFailedException innerException)
                        {
                            throw new FormatException(string.Format(CultureInfo.InvariantCulture, "MIME conversion failed for Attachment {0}!", new object[]
                            {
                                attachment
                            }), innerException);
                        }
                        if (airSyncStream.Length > 0L)
                        {
                            if ((long)offset >= airSyncStream.Length)
                            {
                                throw new ArgumentOutOfRangeException("offset");
                            }
                            int num2 = (count == -1) ? ((int)airSyncStream.Length) : count;
                            if (num2 > GlobalSettings.MaxDocumentDataSize)
                            {
                                throw new DataTooLargeException(StatusCode.AttachmentIsTooLarge);
                            }
                            StreamHelper.CopyStream(airSyncStream, outStream, offset, num2);
                            total = (int)airSyncStream.Length;
                        }
                    }
                }
                return(text);
            }
            throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Attachment {0} is of invalid format!", new object[]
            {
                attachment
            }));
        }
        private bool TryCalculateIrmBodyTag(StoreDriverDeliveryEventArgsImpl argsImpl)
        {
            MessageItem messageItem = null;
            bool        result;

            try
            {
                OrganizationId organizationId = argsImpl.ADRecipientCache.OrganizationId;
                MessageItem    replayItem     = argsImpl.ReplayItem;
                using (Attachment attachment = this.TryOpenFirstAttachment(replayItem))
                {
                    StreamAttachmentBase streamAttachmentBase = attachment as StreamAttachmentBase;
                    if (streamAttachmentBase == null)
                    {
                        ConversationsProcessingAgent.tracer.TraceError((long)this.GetHashCode(), "message.rpmsg attachment is not of the correct type");
                        return(false);
                    }
                    using (Stream contentStream = streamAttachmentBase.GetContentStream(PropertyOpenMode.ReadOnly))
                    {
                        if (contentStream == null)
                        {
                            ConversationsProcessingAgent.tracer.TraceError((long)this.GetHashCode(), "The rights protected message is not properly formatted");
                            return(false);
                        }
                        using (DrmEmailMessageContainer drmEmailMessageContainer = new DrmEmailMessageContainer())
                        {
                            try
                            {
                                drmEmailMessageContainer.Load(contentStream, (object param0) => Streams.CreateTemporaryStorageStream());
                            }
                            catch (InvalidRpmsgFormatException)
                            {
                                ConversationsProcessingAgent.tracer.TraceError((long)this.GetHashCode(), "The rights protected message is not properly formatted");
                                return(false);
                            }
                            string text = null;
                            if (!argsImpl.MailItemDeliver.MbxTransportMailItem.ExtendedProperties.TryGetValue <string>("Microsoft.Exchange.RightsManagement.TransportDecryptionUL", out text) || string.IsNullOrEmpty(text))
                            {
                                ConversationsProcessingAgent.tracer.TraceError((long)this.GetHashCode(), "Failed to load the useLicense");
                                return(false);
                            }
                            try
                            {
                                Uri       licenseUri;
                                XmlNode[] array;
                                bool      flag;
                                RmsClientManager.GetLicensingUri(organizationId, drmEmailMessageContainer.PublishLicense, out licenseUri, out array, out flag);
                                RpMsgToMsgConverter     rpMsgToMsgConverter = new RpMsgToMsgConverter(drmEmailMessageContainer, organizationId, false);
                                RmsClientManagerContext context             = new RmsClientManagerContext(organizationId, RmsClientManagerContext.ContextId.MessageId, replayItem.InternetMessageId, argsImpl.ADRecipientCache, new RmsLatencyTracker(argsImpl.MailItemDeliver.MbxTransportMailItem.LatencyTracker), drmEmailMessageContainer.PublishLicense);
                                using (DisposableTenantLicensePair disposableTenantLicensePair = RmsClientManager.AcquireTenantLicenses(context, licenseUri))
                                {
                                    messageItem = rpMsgToMsgConverter.ConvertRpmsgToMsg(replayItem, text, disposableTenantLicensePair.EnablingPrincipalRac);
                                }
                            }
                            catch (RightsManagementException arg)
                            {
                                ConversationsProcessingAgent.tracer.TraceError <RightsManagementException>((long)this.GetHashCode(), "Conversion failed: {0}", arg);
                                return(false);
                            }
                            catch (InvalidRpmsgFormatException arg2)
                            {
                                ConversationsProcessingAgent.tracer.TraceError <InvalidRpmsgFormatException>((long)this.GetHashCode(), "Conversion failed: {0}", arg2);
                                return(false);
                            }
                            catch (ExchangeConfigurationException arg3)
                            {
                                ConversationsProcessingAgent.tracer.TraceError <ExchangeConfigurationException>((long)this.GetHashCode(), "Conversion failed: {0}", arg3);
                                return(false);
                            }
                            catch (AttachmentProtectionException arg4)
                            {
                                ConversationsProcessingAgent.tracer.TraceError <AttachmentProtectionException>((long)this.GetHashCode(), "Conversion failed: {0}", arg4);
                                return(false);
                            }
                        }
                    }
                }
                this.bodyTag = messageItem.Body.CalculateBodyTag(out this.latestMessageWordCount);
                if (messageItem.AttachmentCollection.Count == 0)
                {
                    this.forceAllAttachmentsHidden = true;
                }
                else
                {
                    foreach (AttachmentHandle attachmentHandle in messageItem.AttachmentCollection)
                    {
                        if (!attachmentHandle.IsInline)
                        {
                            break;
                        }
                        this.forceAllAttachmentsHidden = true;
                    }
                }
                result = true;
            }
            finally
            {
                if (messageItem != null)
                {
                    messageItem.Dispose();
                }
            }
            return(result);
        }
        // Token: 0x06000721 RID: 1825 RVA: 0x00037E14 File Offset: 0x00036014
        private bool GetIsInline(StreamAttachmentBase streamAttachment, AttachmentPolicy.Level level)
        {
            OleAttachment oleAttachment = streamAttachment as OleAttachment;

            return(level == AttachmentPolicy.Level.Allow && (streamAttachment.IsInline || null != oleAttachment));
        }