internal static EmailMessage GenerateDecisionNotTakenNotification(RoutingAddress from, RoutingAddress recipient, string subject, string threadIndex, string threadTopic, string decisionMaker, bool?decision, ExDateTime?decisionTime, Header acceptLanguageHeader, Header contentLanguageHeader, CultureInfo fallbackCulture)
        {
            if (from.Equals(RoutingAddress.NullReversePath) || recipient.Equals(RoutingAddress.NullReversePath) || !from.IsValid || !recipient.IsValid)
            {
                return(null);
            }
            DsnHumanReadableWriter dsnHumanReadableWriter      = Components.DsnGenerator.DsnHumanReadableWriter;
            ApprovalInformation    decisionConflictInformation = dsnHumanReadableWriter.GetDecisionConflictInformation(subject, decisionMaker, decision, decisionTime, acceptLanguageHeader, contentLanguageHeader, fallbackCulture);
            EmailMessage           emailMessage = EmailMessage.Create(BodyFormat.Html, false, decisionConflictInformation.MessageCharset.Name);

            emailMessage.From = new EmailRecipient(string.Empty, from.ToString());
            emailMessage.To.Add(new EmailRecipient(string.Empty, recipient.ToString()));
            emailMessage.Subject = decisionConflictInformation.Subject;
            using (Stream contentWriteStream = emailMessage.Body.GetContentWriteStream())
            {
                dsnHumanReadableWriter.WriteHtmlModerationBody(contentWriteStream, decisionConflictInformation);
                contentWriteStream.Flush();
            }
            Header header = Header.Create("X-MS-Exchange-Organization-SCL");

            header.Value = "-1";
            emailMessage.RootPart.Headers.AppendChild(header);
            Header header2 = (TextHeader)Header.Create("Thread-Index");

            header2.Value = threadIndex;
            emailMessage.RootPart.Headers.AppendChild(header2);
            Header header3 = (TextHeader)Header.Create("Thread-Topic");

            header3.Value = threadTopic;
            emailMessage.RootPart.Headers.AppendChild(header3);
            return(emailMessage);
        }
        // Token: 0x06000037 RID: 55 RVA: 0x00003A78 File Offset: 0x00001C78
        private void SendExpiryNdrOofNotification(MessageItem messageItem, ApprovalInformation.ApprovalNotificationType notificationType)
        {
            ModeratedDLApplication.diag.TraceDebug((long)this.GetHashCode(), "Entering SendExpiryNdrOofNotification");
            if (!this.ShouldSendNotification(messageItem))
            {
                return;
            }
            RoutingAddress routingAddress;

            if (!this.TryGetOriginalSender(messageItem, out routingAddress))
            {
                return;
            }
            MailboxSession mailboxSession = (MailboxSession)messageItem.Session;

            messageItem.Load(ModeratedDLApplication.NotificationPropertiesFromInitMessage);
            string valueOrDefault = messageItem.GetValueOrDefault <string>(MessageItemSchema.AcceptLanguage, string.Empty);
            ICollection <string> moderatedRecipients = this.GetModeratedRecipients(messageItem, false);
            string valueOrDefault2 = messageItem.GetValueOrDefault <string>(ItemSchema.InternetReferences, string.Empty);
            DsnHumanReadableWriter defaultDsnHumanReadableWriter = DsnHumanReadableWriter.DefaultDsnHumanReadableWriter;
            StoreObjectId          defaultFolderId = mailboxSession.GetDefaultFolderId(DefaultFolderType.Outbox);

            using (MessageItem messageItem2 = MessageItem.Create(mailboxSession, defaultFolderId))
            {
                ApprovalInformation approvalInformation = null;
                approvalInformation  = defaultDsnHumanReadableWriter.GetMessageInModerationExpiredNdrOofInformation(notificationType, messageItem.Subject, moderatedRecipients, valueOrDefault2, valueOrDefault);
                messageItem2.Subject = approvalInformation.Subject;
                BodyWriteConfiguration configuration = new BodyWriteConfiguration(BodyFormat.TextHtml, approvalInformation.MessageCharset.Name);
                using (Stream stream = messageItem2.Body.OpenWriteStream(configuration))
                {
                    defaultDsnHumanReadableWriter.WriteHtmlModerationBody(stream, approvalInformation);
                }
                this.StampCommonNotificationProperties(messageItem2, messageItem, new RoutingAddress[]
                {
                    routingAddress
                }, valueOrDefault2, approvalInformation.Culture);
                string text;
                this.AttachOriginalMessageToNotification(messageItem, messageItem2, out text);
                messageItem2.ClassName = "IPM.Note.Microsoft.Approval.Reply";
                messageItem2.SendWithoutSavingMessage();
                ModeratedDLApplication.diag.TraceDebug((long)this.GetHashCode(), "Notification sent.");
            }
        }
        public override bool WriteSubjectAndBody(MessageItemApprovalRequest approvalRequest, CultureInfo cultureInfo, out CultureInfo cultureInfoWritten)
        {
            cultureInfoWritten = cultureInfo;
            ModerationApprovalRequestWriter.diag.TraceDebug <CultureInfo>((long)this.GetHashCode(), "Generating approval request. cultureInfo={0}", cultureInfo);
            DsnHumanReadableWriter defaultDsnHumanReadableWriter = DsnHumanReadableWriter.DefaultDsnHumanReadableWriter;

            this.CacheDataFromOriginalMessage();
            if (this.originalMessageStream == null)
            {
                ModerationApprovalRequestWriter.diag.TraceError <string>((long)this.GetHashCode(), "No original message to write approval request body.  Approval data: {0}", this.initiationMessage.ApprovalData);
                return(false);
            }
            bool flag = !this.hasInlineAttachment && !this.isOpaqueMessage;

            if (this.hasInlineAttachment)
            {
                ModerationApprovalRequestWriter.diag.TraceDebug <string>((long)this.GetHashCode(), "Not writing preview for message with approval data: {0}, because it has inline attachment", this.initiationMessage.ApprovalData);
            }
            ApprovalInformation approvalRequestMessageInformation = defaultDsnHumanReadableWriter.GetApprovalRequestMessageInformation(this.initiationMessage.Subject, this.originalSenderDisplayName, this.originalToFormattedLine, this.originalCcFormattedLine, this.moderatedRecipients, flag, cultureInfo);

            approvalRequest.MessageItem.Subject = approvalRequestMessageInformation.Subject;
            this.WriteApprovalRequestBody(approvalRequest.MessageItem.Body, defaultDsnHumanReadableWriter, approvalRequestMessageInformation, flag);
            return(true);
        }
        // Token: 0x0600003F RID: 63 RVA: 0x00004608 File Offset: 0x00002808
        private bool TryWriteNotificationWithAppendedComments(DsnHumanReadableWriter notificationWriter, MessageItem rejectItem, StreamAttachment commentAttachment, ApprovalInformation info)
        {
            bool     result             = true;
            string   htmlModerationBody = notificationWriter.GetHtmlModerationBody(info);
            Charset  textCharset        = commentAttachment.TextCharset;
            Encoding inputEncoding      = null;

            if (textCharset == null || !textCharset.TryGetEncoding(out inputEncoding))
            {
                return(false);
            }
            Charset charset = textCharset;

            if (!ModeratedDLApplication.IsEncodingMatch(info.Codepages, textCharset.CodePage))
            {
                charset = Charset.UTF8;
            }
            BodyWriteConfiguration configuration = new BodyWriteConfiguration(BodyFormat.TextHtml, charset.Name);

            using (Stream stream = rejectItem.Body.OpenWriteStream(configuration))
            {
                HtmlToHtml htmlToHtml = new HtmlToHtml();
                htmlToHtml.Header             = htmlModerationBody;
                htmlToHtml.HeaderFooterFormat = HeaderFooterFormat.Html;
                htmlToHtml.InputEncoding      = inputEncoding;
                htmlToHtml.OutputEncoding     = charset.GetEncoding();
                try
                {
                    using (Stream contentStream = commentAttachment.GetContentStream(PropertyOpenMode.ReadOnly))
                    {
                        htmlToHtml.Convert(contentStream, stream);
                        stream.Flush();
                    }
                }
                catch (ExchangeDataException arg)
                {
                    ModeratedDLApplication.diag.TraceDebug <ExchangeDataException>(0L, "Attaching comments failed with {0}", arg);
                    result = false;
                }
            }
            return(result);
        }
        // Token: 0x06000039 RID: 57 RVA: 0x00003EA4 File Offset: 0x000020A4
        private void SendRejectNotification(MessageItem messageItem)
        {
            ModeratedDLApplication.diag.TraceDebug((long)this.GetHashCode(), "Entering SendRejectNotification");
            if (!this.ShouldSendNotification(messageItem))
            {
                return;
            }
            RoutingAddress routingAddress;

            if (!this.TryGetOriginalSender(messageItem, out routingAddress))
            {
                return;
            }
            MailboxSession mailboxSession = (MailboxSession)messageItem.Session;

            messageItem.Load(ModeratedDLApplication.NotificationPropertiesFromInitMessage);
            string valueOrDefault = messageItem.GetValueOrDefault <string>(MessageItemSchema.AcceptLanguage, string.Empty);
            DsnHumanReadableWriter defaultDsnHumanReadableWriter = DsnHumanReadableWriter.DefaultDsnHumanReadableWriter;
            ICollection <string>   moderatedRecipients           = this.GetModeratedRecipients(messageItem, false);
            string        valueOrDefault2 = messageItem.GetValueOrDefault <string>(ItemSchema.InternetReferences, string.Empty);
            StoreObjectId defaultFolderId = mailboxSession.GetDefaultFolderId(DefaultFolderType.Outbox);

            using (MessageItem messageItem2 = MessageItem.Create(mailboxSession, defaultFolderId))
            {
                StreamAttachment streamAttachment = null;
                try
                {
                    bool flag = ModeratedDLApplication.TryGetCommentAttachment(messageItem, out streamAttachment) && streamAttachment.Size > 0L;
                    messageItem2.ClassName = "IPM.Note.Microsoft.Approval.Reply.Reject";
                    bool flag2 = true;
                    ApprovalInformation approvalInformation = null;
                    if (flag)
                    {
                        approvalInformation  = defaultDsnHumanReadableWriter.GetModerateRejectInformation(messageItem.Subject, moderatedRecipients, flag, valueOrDefault2, valueOrDefault);
                        messageItem2.Subject = approvalInformation.Subject;
                        flag2 = this.TryWriteNotificationWithAppendedComments(defaultDsnHumanReadableWriter, messageItem2, streamAttachment, approvalInformation);
                    }
                    if (!flag || !flag2)
                    {
                        approvalInformation  = defaultDsnHumanReadableWriter.GetModerateRejectInformation(messageItem.Subject, moderatedRecipients, false, valueOrDefault2, valueOrDefault);
                        messageItem2.Subject = approvalInformation.Subject;
                        BodyWriteConfiguration configuration = new BodyWriteConfiguration(BodyFormat.TextHtml, approvalInformation.MessageCharset.Name);
                        using (Stream stream = messageItem2.Body.OpenWriteStream(configuration))
                        {
                            defaultDsnHumanReadableWriter.WriteHtmlModerationBody(stream, approvalInformation);
                            stream.Flush();
                        }
                    }
                    if (flag && !flag2)
                    {
                        using (StreamAttachment streamAttachment2 = messageItem2.AttachmentCollection.Create(AttachmentType.Stream) as StreamAttachment)
                        {
                            streamAttachment2.FileName = defaultDsnHumanReadableWriter.GetModeratorsCommentFileName(approvalInformation.Culture);
                            using (Stream contentStream = streamAttachment2.GetContentStream())
                            {
                                using (Stream contentStream2 = streamAttachment.GetContentStream(PropertyOpenMode.ReadOnly))
                                {
                                    byte[] buffer = new byte[8192];
                                    ApprovalProcessor.CopyStream(contentStream2, contentStream, buffer);
                                }
                                contentStream.Flush();
                            }
                            streamAttachment2.Save();
                        }
                    }
                    this.StampCommonNotificationProperties(messageItem2, messageItem, new RoutingAddress[]
                    {
                        routingAddress
                    }, valueOrDefault2, approvalInformation.Culture);
                    messageItem2.SendWithoutSavingMessage();
                }
                finally
                {
                    if (streamAttachment != null)
                    {
                        streamAttachment.Dispose();
                    }
                }
            }
        }
        // Token: 0x06000038 RID: 56 RVA: 0x00003BFC File Offset: 0x00001DFC
        private void SendModeratorExpiryNotification(MessageItem messageItem, ApprovalInformation.ApprovalNotificationType notificationType)
        {
            ModeratedDLApplication.diag.TraceDebug((long)this.GetHashCode(), "Entering SendModeratorExpiryNotification");
            if (!this.ShouldSendNotification(messageItem))
            {
                return;
            }
            string valueOrDefault = messageItem.GetValueOrDefault <string>(MessageItemSchema.ApprovalAllowedDecisionMakers);

            RoutingAddress[] collection;
            if (!ApprovalUtils.TryGetDecisionMakers(valueOrDefault, out collection))
            {
                return;
            }
            MailboxSession mailboxSession = (MailboxSession)messageItem.Session;

            messageItem.Load(ModeratedDLApplication.NotificationPropertiesFromInitMessage);
            ICollection <string> moderatedRecipients = this.GetModeratedRecipients(messageItem, false);
            string valueOrDefault2 = messageItem.GetValueOrDefault <string>(ItemSchema.InternetReferences, string.Empty);
            int    value           = messageItem.GetValueAsNullable <int>(StoreObjectSchema.RetentionPeriod) ?? 2;
            Dictionary <CultureInfo, List <RoutingAddress> > dictionary = null;
            IRecipientSession      tenantOrRootOrgRecipientSession      = DirectorySessionFactory.Default.GetTenantOrRootOrgRecipientSession(false, ConsistencyMode.FullyConsistent, mailboxSession.GetADSessionSettings(), 587, "SendModeratorExpiryNotification", "f:\\15.00.1497\\sources\\dev\\Approval\\src\\Applications\\ModeratedDLApplication\\ModeratedDLApplication.cs");
            DsnHumanReadableWriter defaultDsnHumanReadableWriter        = DsnHumanReadableWriter.DefaultDsnHumanReadableWriter;
            StoreObjectId          defaultFolderId = mailboxSession.GetDefaultFolderId(DefaultFolderType.Outbox);

            if (ClientCultures.IsCultureSupportedForDsn(CultureInfo.CurrentCulture))
            {
                this.defaultFallBackCulture = CultureInfo.CurrentCulture;
            }
            else
            {
                this.defaultFallBackCulture = CultureInfo.GetCultureInfo("en-US");
            }
            if (ApprovalProcessor.TryGetCulturesForDecisionMakers(new List <RoutingAddress>(collection), tenantOrRootOrgRecipientSession, this.defaultFallBackCulture, out dictionary))
            {
                foreach (CultureInfo cultureInfo in dictionary.Keys)
                {
                    IList <RoutingAddress> list = dictionary[cultureInfo];
                    using (MessageItem messageItem2 = MessageItem.Create(mailboxSession, defaultFolderId))
                    {
                        ApprovalInformation approvalInformation = null;
                        string text;
                        this.AttachOriginalMessageToNotification(messageItem, messageItem2, out text);
                        if (string.IsNullOrEmpty(text))
                        {
                            RoutingAddress routingAddress;
                            if (!this.TryGetOriginalSender(messageItem, out routingAddress))
                            {
                                break;
                            }
                            text = routingAddress.ToString();
                        }
                        approvalInformation  = defaultDsnHumanReadableWriter.GetMessageInModerationModeratorExpiredInformation(notificationType, messageItem.Subject, moderatedRecipients, text, new int?(value), cultureInfo.Name, this.defaultFallBackCulture);
                        messageItem2.Subject = approvalInformation.Subject;
                        BodyWriteConfiguration configuration = new BodyWriteConfiguration(BodyFormat.TextHtml, approvalInformation.MessageCharset.Name);
                        using (Stream stream = messageItem2.Body.OpenWriteStream(configuration))
                        {
                            defaultDsnHumanReadableWriter.WriteHtmlModerationBody(stream, approvalInformation);
                        }
                        this.StampCommonNotificationProperties(messageItem2, messageItem, list, valueOrDefault2, approvalInformation.Culture);
                        messageItem2.ClassName = "IPM.Note.Microsoft.Approval.Reply.Reject";
                        messageItem2.SendWithoutSavingMessage();
                        ModeratedDLApplication.diag.TraceDebug <int, string>((long)this.GetHashCode(), "Expiry Notification sent for {0} decision makers, original message id '{1}'", list.Count, valueOrDefault2);
                    }
                }
            }
        }
        private bool TryWriteApprovalRequestBodyWithPreview(Body destiniationBody, DsnHumanReadableWriter approvalRequestWriter, ApprovalInformation info, Charset outputCharset)
        {
            if (this.originalEmailMessage == null)
            {
                return(false);
            }
            TextConverter textConverter;

            if (this.originalMesssageBodyFormat == Microsoft.Exchange.Data.Transport.Email.BodyFormat.Rtf)
            {
                textConverter = new RtfToHtml
                {
                    Header             = approvalRequestWriter.GetHtmlModerationBody(info),
                    HeaderFooterFormat = HeaderFooterFormat.Html,
                    OutputEncoding     = outputCharset.GetEncoding()
                };
            }
            else if (this.originalMesssageBodyFormat == Microsoft.Exchange.Data.Transport.Email.BodyFormat.Text)
            {
                textConverter = new TextToHtml
                {
                    Header             = approvalRequestWriter.GetHtmlModerationBody(info),
                    HeaderFooterFormat = HeaderFooterFormat.Html,
                    InputEncoding      = this.originalMessageBodyCharset.GetEncoding(),
                    OutputEncoding     = outputCharset.GetEncoding()
                };
            }
            else
            {
                if (this.originalMesssageBodyFormat != Microsoft.Exchange.Data.Transport.Email.BodyFormat.Html)
                {
                    return(false);
                }
                textConverter = new HtmlToHtml
                {
                    Header             = approvalRequestWriter.GetHtmlModerationBody(info),
                    HeaderFooterFormat = HeaderFooterFormat.Html,
                    InputEncoding      = this.originalMessageBodyCharset.GetEncoding(),
                    OutputEncoding     = outputCharset.GetEncoding()
                };
            }
            BodyWriteConfiguration configuration = new BodyWriteConfiguration(Microsoft.Exchange.Data.Storage.BodyFormat.TextHtml, outputCharset);

            using (Stream stream = destiniationBody.OpenWriteStream(configuration))
            {
                using (Stream contentReadStream = this.originalEmailMessage.Body.GetContentReadStream())
                {
                    try
                    {
                        textConverter.Convert(contentReadStream, stream);
                    }
                    catch (ExchangeDataException arg)
                    {
                        ModerationApprovalRequestWriter.diag.TraceDebug <ExchangeDataException>(0L, "Approval request with inline preview failed {0}", arg);
                        return(false);
                    }
                }
            }
            return(true);
        }
        private void WriteApprovalRequestBody(Body destiniationBody, DsnHumanReadableWriter writer, ApprovalInformation info, bool writePreview)
        {
            if (writePreview)
            {
                Charset outputCharset;
                bool    flag = !ModerationApprovalRequestWriter.CheckInputOutputCharsetMatch(info.Codepages, this.originalMessageBodyCharset, out outputCharset);
                if (!flag && this.TryWriteApprovalRequestBodyWithPreview(destiniationBody, writer, info, outputCharset))
                {
                    return;
                }
                ModerationApprovalRequestWriter.diag.TraceError <bool, string>((long)this.GetHashCode(), "Failed to write preview for message.  Falling back to no preview. Charset mismatch:{0}. Approval data:{1}.", flag, this.initiationMessage.ApprovalData);
                info = writer.GetApprovalRequestMessageInformation(this.initiationMessage.Subject, this.originalSenderDisplayName, this.originalToFormattedLine, this.originalCcFormattedLine, this.moderatedRecipients, false, info.Culture);
            }
            BodyWriteConfiguration configuration = new BodyWriteConfiguration(Microsoft.Exchange.Data.Storage.BodyFormat.TextHtml, info.MessageCharset.Name);

            using (Stream stream = destiniationBody.OpenWriteStream(configuration))
            {
                writer.WriteHtmlModerationBody(stream, info);
            }
        }