Esempio n. 1
0
        /// <summary>
        /// Creates the specified message.
        /// </summary>
        /// <param name="EMailRouterPop3BoxId">The E mail router POP3 box id.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public static int Create(int EMailRouterPop3BoxId, Pop3Message message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            EMailMessageRow newRow = new EMailMessageRow();

            newRow.EMailRouterPop3BoxId = EMailRouterPop3BoxId;
            //newRow.Created = DateTime.UtcNow;
            newRow.From = EMailMessage.GetSenderEmail(message);

            //if (message.Sender != null)
            //    newRow.From = EMailMessage.ExtractFirstEmail(message.Sender.Email);
            //else
            //    newRow.From = EMailMessage.ExtractFirstEmail(message.From.Email);

            newRow.To = EMailMessage.ExtractFirstEmail(message.To);

            newRow.Subject = message.Subject == null ? string.Empty : message.Subject;

            newRow.EmlMessage = GetPop3MessageBytes(message);

            newRow.Update();

            return(newRow.PrimaryKeyId);
        }
Esempio n. 2
0
        //		public static string[] GetExternalSendersByIncidentId(int IncidentId)
        //		{
        //			return EMailMessageRow.GetExternalSendersByIncidentId(IncidentId);
        //		}

        /// <summary>
        /// Logs the on creator.
        /// </summary>
        /// <param name="DefaultCreatorId">The default creator id.</param>
        /// <param name="msg">The MSG.</param>
        /// <returns></returns>
        internal static UserLight LogOnCreator(int DefaultCreatorId, Pop3Message msg)
        {
            if (Security.CurrentUser != null)
            {
                return(Security.CurrentUser);
            }

            int creatorId = DefaultCreatorId;

            int emailUser = DBUser.GetUserByEmail(EMailMessage.GetSenderEmail(msg), false);

            if (emailUser > 0)
            {
                // OZ: 2007-01-26: Fix problem: Mediachase.IBN.Business.AccessDeniedException: Access denied.
                //   at Mediachase.IBN.Business.Incident.CreateFromEmail
                using (IDataReader reader = DBUser.GetUserInfo(emailUser))
                {
                    if (reader.Read())
                    {
                        if (!(bool)reader["IsActive"])
                        {
                            emailUser = -1;
                        }
                    }
                    else
                    {
                        emailUser = -1;
                    }
                }
                //
            }

            if (emailUser > 0)
            {
                creatorId = emailUser;
            }

            UserLight creator = UserLight.Load(creatorId);

            return(Security.SetCurrentUser(creator));
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EMailMessageInfo"/> class.
        /// </summary>
        /// <param name="EMailMessageId">The E mail message id.</param>
        protected EMailMessageInfo(int EMailMessageId)
        {
            _EMailMessageId = EMailMessageId;

            // Load Message Row
            EMailMessageRow row = new EMailMessageRow(EMailMessageId);

            ///////////////// Test
            // E:\Util\BlobDataExtractor\output24700.eml
            //System.IO.StreamReader st = new System.IO.StreamReader(@"D:\EMailMessage_72848.eml", System.Text.Encoding.Default);
            //string strmsg = st.ReadToEnd();
            //byte[] buffer = System.Text.Encoding.Default.GetBytes(strmsg);
            //System.IO.MemoryStream memStream = new System.IO.MemoryStream(buffer, 0, buffer.Length, true, true);
            ///////////////

            _created = Database.DBCommon.GetLocalDate(Security.CurrentUser.TimeZoneId, row.Created);

            MemoryStream memStream = new MemoryStream(row.EmlMessage.Length);

            memStream.Write(row.EmlMessage, 0, row.EmlMessage.Length);
            memStream.Position = 0;

            Pop3Message message = new Pop3Message(memStream);

            // Load Headers
            _headers.Add(message.Headers);

            // Extract Html Body
            _htmlBody = ConvertBody(message);

            // Extract Attachments
            _attachments = (AttachmentInfo[])ExtractAttachments(message.MimeEntries).ToArray(typeof(AttachmentInfo));

            _senderEmail = EMailMessage.GetSenderEmail(message);
            _senderName  = EMailMessage.GetSenderName(message);

            _emailBoxId = row.EMailRouterPop3BoxId;
        }
Esempio n. 4
0
        /// <summary>
        /// Checks the specified email box.
        /// </summary>
        /// <param name="emailBox">The email box.</param>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public static EMailMessageAntiSpamRuleRusult Check(EMailRouterPop3Box emailBox, Pop3Message message)
        {
            // 2007-01-09: Check Empty From and Sender
            if (message.Sender == null && message.From == null || message.To == null)
            {
                return(EMailMessageAntiSpamRuleRusult.Deny);
            }

            EMailMessageAntiSpamRule[] antiSpamRules = EMailMessageAntiSpamRule.List();

            // Load Rules
            //EMailMessageAntiSpamRule[] antiSpamRules = _antiSpamRules;

            // Check Rules

            foreach (EMailMessageAntiSpamRule rule in antiSpamRules)
            {
                if (((EMailMessageAntiSpamRuleType)rule.RuleType) != EMailMessageAntiSpamRuleType.Service)
                {
                    string KeyValue = GetStringFromEmailByKey(message, rule.Key);

                    if (KeyValue != null)
                    {
                        switch ((EMailMessageAntiSpamRuleType)rule.RuleType)
                        {
                        case EMailMessageAntiSpamRuleType.Contains:
                            foreach (string ContainsSubStr in rule.Value.Split(';'))
                            {
                                string TrimContainsSubStr = ContainsSubStr.Trim();
                                if (TrimContainsSubStr != string.Empty)
                                {
                                    if (TrimContainsSubStr.IndexOfAny(new char[] { '*', '?' }) != -1)
                                    {
                                        if (Pattern.Match(KeyValue, TrimContainsSubStr))
                                        {
                                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                                        }
                                    }
                                    else
                                    {
                                        if (KeyValue.IndexOf(TrimContainsSubStr) != -1)
                                        {
                                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                                        }
                                    }
                                }
                            }
                            break;

                        case EMailMessageAntiSpamRuleType.IsEqual:
                            if (string.Compare(KeyValue, rule.Value, true) == 0)
                            {
                                return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                            }
                            break;

                        case EMailMessageAntiSpamRuleType.RegexMatch:
                            Match match = Regex.Match(KeyValue, rule.Value);
                            if ((match.Success && (match.Index == 0)) && (match.Length == KeyValue.Length))
                            {
                                return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                            }
                            break;
                        }
                    }
                }
                else
                {
                    string FromEmail = EMailMessage.GetSenderEmail(message);

                    switch (rule.Key)
                    {
                    case BlackListServiceName:
                        if (BlackListItem.Contains(FromEmail))
                        {
                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                        }
                        break;

                    case WhiteListServiceName:
                        if (WhiteListItem.Contains(FromEmail))
                        {
                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                        }
                        break;

                    case TicketServiceName:
                        if (message.Subject != null && TicketUidUtil.LoadFromString(message.Subject) != string.Empty)
                        {
                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                        }
                        break;

                    case IncidentBoxRulesServiceName:
                        // Step 1. Get Incident Info
                        IEMailIncidentMapping mappingHandler = EMailIncidentMappingHandler.LoadHandler(emailBox.Settings.SelectedHandlerId);
                        IncidentInfo          incidentInfo   = mappingHandler.Create(emailBox, message);

                        // Step 2. Evaluate IncidentBoxRules
                        IncidentBox box = IncidentBoxRule.Evaluate(incidentInfo, false);
                        if (box != null)
                        {
                            return(rule.Accept ? EMailMessageAntiSpamRuleRusult.Accept : EMailMessageAntiSpamRuleRusult.Deny);
                        }
                        break;
                    }
                }
            }

            return(EMailMessageAntiSpamRuleRusult.Pending);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds to incident message.
        /// </summary>
        /// <param name="IsInternal">if set to <c>true</c> [is internal].</param>
        /// <param name="IncidentId">The incident id.</param>
        /// <param name="EMailMessageId">The E mail message id.</param>
        /// <param name="msg">The MSG.</param>
        /// <returns></returns>
        public static int AddToIncidentMessage(bool IsInternal, int IncidentId, int EMailMessageId, Pop3Message msg)
        {
            IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId));

            if (IsInternal)
            {
                // 2007-02-15: OZ: New Messagew Addon
                Issue2.SetNewMessage(IncidentId, false);
                //

                // TODO: If Responsible is Group, set current user as responsible

                return(AddInternalEMail2Incident(IncidentId, EMailMessageId, EMailMessage.GetSenderName(msg), EMailMessage.GetSenderEmail(msg)));
            }
            else
            {
                // 2007-02-15: OZ: New Messagew Addon
                Issue2.SetNewMessage(IncidentId, true);
                //

                return(AddExternalEMail2Incident(IncidentId, EMailMessageId, EMailMessage.GetSenderName(msg), EMailMessage.GetSenderEmail(msg)));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Copies to incident.
        /// </summary>
        /// <param name="emailMessageId">The email message id.</param>
        /// <param name="incidentId">The incident id.</param>
        public static void CopyToIncident(int emailMessageId, int incidentId)
        {
            using (DbTransaction tran = DbTransaction.Begin())
            {
                // Remove from pending
                PendingEMailMessageRow.DeleteByEMailMessageId(emailMessageId);

                // Load Pop3 Message
                EMailMessageInfo   msgInfo  = EMailMessageInfo.Load(emailMessageId);
                EMailRouterPop3Box emailBox = EMailRouterPop3Box.Load(msgInfo.EMailRouterPop3BoxId);

                Pop3Message msg = EMailMessage.GetPop3Message(emailMessageId);

                // Add User to White Lits
                string SenderEmail = EMailMessage.GetSenderEmail(msg);
                //if(!WhiteListItem.Contains(SenderEmail))

                if (PortalConfig.UseAntiSpamFilter && PortalConfig.AutoFillWhiteList)
                {
                    WhiteListItem.Create(SenderEmail);
                }


                int creatorId, issueBoxId;
                using (IDataReader reader = Incident.GetIncident(incidentId))
                {
                    reader.Read();

                    creatorId  = (int)reader["CreatorId"];
                    issueBoxId = (int)reader["IncidentBoxId"];
                }

                int       stateId, managerId, responsibleId;
                bool      isResposibleGroup;
                ArrayList users = new ArrayList();
                Issue2.GetIssueBoxSettings(issueBoxId, out stateId, out managerId, out responsibleId, out isResposibleGroup, users);

                UserLight prevUser = LogOnCreator(creatorId, msg);

                int ThreadNodeId = AddToIncidentMessage(emailBox.IsInternal, incidentId, emailMessageId, msg);

                if (ProcessXIbnHeaders(incidentId, ThreadNodeId, msg))
                {
                    ArrayList excludeUsers = EMailRouterOutputMessage.Send(incidentId, emailBox, msg);

                    // O.R. [2008-09-09]: Exclude inactive users
                    if (responsibleId > 0 && User.GetUserActivity(responsibleId) != User.UserActivity.Active)
                    {
                        responsibleId = -1;
                    }
                    ArrayList activeUsers = new ArrayList();
                    foreach (int userId in users)
                    {
                        if (User.GetUserActivity(userId) == User.UserActivity.Active)
                        {
                            activeUsers.Add(userId);
                        }
                    }
                    //

                    Issue2.SendAlertsForNewIssue(incidentId, managerId, responsibleId, activeUsers, excludeUsers);
                }

                tran.Commit();
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Approves the pending.
        /// </summary>
        /// <param name="EMailMessageId">The E mail message id.</param>
        public static void ApprovePending(int EMailMessageId)
        {
            // Stop Double Approve
            if (!PendingEMailMessageRow.Contains(EMailMessageId))
            {
                return;
            }

            using (DbTransaction tran = DbTransaction.Begin())
            {
                // Remove from pending
                PendingEMailMessageRow.DeleteByEMailMessageId(EMailMessageId);

                // Load Pop3 Message
                EMailMessageInfo   msgInfo  = EMailMessageInfo.Load(EMailMessageId);
                EMailRouterPop3Box emailBox = EMailRouterPop3Box.Load(msgInfo.EMailRouterPop3BoxId);

                Pop3Message msg = EMailMessage.GetPop3Message(EMailMessageId);

                // Add User to White Lits
                string SenderEmail = EMailMessage.GetSenderEmail(msg);
                //if(!WhiteListItem.Contains(SenderEmail))

                if (PortalConfig.UseAntiSpamFilter && PortalConfig.AutoFillWhiteList)
                {
                    WhiteListItem.Create(SenderEmail);
                }

                string TicketUID = TicketUidUtil.LoadFromString(msg.Subject == null ? string.Empty : msg.Subject);

                if (TicketUID == string.Empty)
                {
                    int IncidentId = CreateNewIncident(EMailMessageId, emailBox, msg);

                    // Automaticaly by Incident.CreateFromEmail
                    //EMailRouterOutputMessage.Send(IncidentId, emailBox, msg);
                }
                else
                {
                    // Assign By Ticket
                    int IncidentId = FindIncidentByTicket(TicketUID);

                    if (IncidentId != -1)
                    {
                        int creatorId, issueBoxId;
                        using (IDataReader reader = Incident.GetIncident(IncidentId))
                        {
                            reader.Read();

                            creatorId  = (int)reader["CreatorId"];
                            issueBoxId = (int)reader["IncidentBoxId"];
                        }

                        int       stateId, managerId, responsibleId;
                        bool      isResposibleGroup;
                        ArrayList users = new ArrayList();
                        Issue2.GetIssueBoxSettings(issueBoxId, out stateId, out managerId, out responsibleId, out isResposibleGroup, users);

                        UserLight prevUser = LogOnCreator(creatorId, msg);

                        int ThreadNodeId = AddToIncidentMessage(emailBox.IsInternal, IncidentId, EMailMessageId, msg);

                        if (ProcessXIbnHeaders(IncidentId, ThreadNodeId, msg))
                        {
                            ArrayList excludeUsers = EMailRouterOutputMessage.Send(IncidentId, emailBox, msg);

                            // O.R. [2008-09-09]: Exclude inactive users
                            if (responsibleId > 0 && User.GetUserActivity(responsibleId) != User.UserActivity.Active)
                            {
                                responsibleId = -1;
                            }
                            ArrayList activeUsers = new ArrayList();
                            foreach (int userId in users)
                            {
                                if (User.GetUserActivity(userId) == User.UserActivity.Active)
                                {
                                    activeUsers.Add(userId);
                                }
                            }
                            //

                            Issue2.SendAlertsForNewIssue(IncidentId, managerId, responsibleId, activeUsers, excludeUsers);
                        }
                    }
                    else
                    {
                        IncidentId = CreateNewIncident(EMailMessageId, emailBox, msg);

                        // Automaticaly by Incident.CreateFromEmail
                        //EMailRouterOutputMessage.Send(IncidentId, emailBox, msg);
                    }
                }
                tran.Commit();
            }
        }
        public IncidentInfo Create(EMailRouterPop3Box box, Mediachase.Net.Mail.Pop3Message msg)
        {
            IncidentInfo retVal = new IncidentInfo();

            retVal.CreatorId = box.Settings.DefaultEMailIncidentMappingBlock.DefaultCreator;

            retVal.EMailBox = box.EMailRouterPop3BoxId;

            retVal.Title = msg.Subject == null?string.Empty:msg.Subject;

            if (retVal.Title == string.Empty)
            {
                // Set Default Inicdent Title if subject is empty
                string SenderID = EMailMessage.GetSenderName(msg);
                if (SenderID == string.Empty)
                {
                    SenderID = EMailMessage.GetSenderEmail(msg);
                }
                retVal.Title = string.Format("{0} ({1})", SenderID, DateTime.Now.ToString("d"));
            }

            // OZ: 2008-07-30 Added description defenition rules
            if (box.Settings.DefaultEMailIncidentMappingBlock.DescriptionId == -1)
            {
                retVal.Description = string.Empty;
                retVal.EMailBody   = EMailMessageInfo.ExtractTextFromHtml(msg);
            }
            else
            {
                retVal.Description = EMailMessageInfo.ExtractTextFromHtml(msg);
                retVal.EMailBody   = retVal.Description;
            }
            //

            int priorityId = GetPriorityId(box, msg);

            if (priorityId < 0)             // <0 From Email
            {
                retVal.PriorityId = 500;

                string importance = msg.Importance;
                if (importance != null)
                {
                    switch (importance.ToLower())
                    {
                    case "low":
                        retVal.PriorityId = 0;
                        break;

                    case "normal":
                        retVal.PriorityId = 500;
                        break;

                    case "high":
                        retVal.PriorityId = 750;
                        break;
                    }
                }
            }
            else
            {
                retVal.PriorityId = priorityId;
            }


            retVal.MailSenderEmail = EMailMessage.GetSenderEmail(msg);

            retVal.SeverityId = GetSeverityId(box, msg);
            retVal.TypeId     = GetTypeId(box, msg);
            retVal.GeneralCategories.AddRange(GetGeneralCategories(box, msg));
            retVal.IncidentCategories.AddRange(GetIncidentCategories(box, msg));

            // OZ: 2007-01-11
            retVal.ProjectId     = box.Settings.DefaultEMailIncidentMappingBlock.ProjectId;
            retVal.IncidentBoxId = box.Settings.DefaultEMailIncidentMappingBlock.IncidentBoxId;

            // OZ: 2007-01-22
            retVal.OrgUid     = box.Settings.DefaultEMailIncidentMappingBlock.OrgUid;
            retVal.ContactUid = box.Settings.DefaultEMailIncidentMappingBlock.ContactUid;

            return(retVal);
        }
        private void OnNewMessage(object sender, EventArgs e)
        {
            if (e is NewPop3MessageEventArgs && sender is EMailRouterPop3BoxMessageLoader)
            {
                EMailRouterPop3Box internalPop3Box = EMailRouterPop3Box.ListInternal();
                if (internalPop3Box != null && !internalPop3Box.Activity.IsActive)
                {
                    internalPop3Box = null;
                }

                UserLight prevUser = Security.CurrentUser;

                Alerts2.SendToCurrentUser = true;

                try
                {
                    using (DbTransaction tran = DbTransaction.Begin())
                    {
                        EMailRouterPop3BoxMessageLoader loader = (EMailRouterPop3BoxMessageLoader)sender;

                        Pop3Message msg = ((NewPop3MessageEventArgs)e).Message;

                        string TicketUID = TicketUidUtil.LoadFromString(msg.Subject == null ? string.Empty : msg.Subject);

                        EMailMessageAntiSpamRuleRusult result = EMailMessageAntiSpamRuleRusult.Deny;

                        if (loader.Owner.IsInternal)
                        {
                            int UserId = DBUser.GetUserByEmail(EMailMessage.GetSenderEmail(msg), false);
                            if (TicketUID != string.Empty && UserId > 0)
                            {
                                result = EMailMessageAntiSpamRuleRusult.Accept;
                            }
                        }
                        else
                        {
                            if (PortalConfig.UseAntiSpamFilter)
                            {
                                result = EMailMessageAntiSpamRule.Check(loader.Owner, msg);
                            }
                            else
                            {
                                result = EMailMessageAntiSpamRuleRusult.Accept;
                            }
                        }

                        int EMailMessageId = -1;

                        // OZ: [2007-01-29] EMailMessageLog
                        if (EmailLogSettings != null)
                        {
                            string from = string.Empty;

                            if (msg.Headers["Reply-To"] != null)
                            {
                                from = msg.Headers["Reply-To"];
                            }
                            //else if (msg.Headers["Sender"] != null)
                            //{
                            //    from = msg.Headers["Sender"];
                            //}
                            else if (msg.Headers["From"] != null)
                            {
                                from = msg.Headers["From"];
                            }

                            EMailMessageLog.Add(from,
                                                msg.Headers["To"] != null ? msg.Headers["To"] : string.Empty,
                                                msg.Headers["Subject"] != null ? msg.Headers["Subject"] : string.Empty,
                                                loader.Owner.EMailRouterPop3BoxId,
                                                result);
                        }
                        //

                        switch (result)
                        {
                        case EMailMessageAntiSpamRuleRusult.Pending:
                            // Register Email Message
                            EMailMessageId = RegisterEmail(loader.Owner.EMailRouterPop3BoxId, msg);
                            // Add to pending email table
                            EMailMessage.MarkAsPendingMessage(EMailMessageId);
                            break;

                        case EMailMessageAntiSpamRuleRusult.Accept:
                            // Register Email Message
                            EMailMessageId = RegisterEmail(loader.Owner.EMailRouterPop3BoxId, msg);


                            if (TicketUID == string.Empty)
                            {
                                int IncidentId = EMailMessage.CreateNewIncident(EMailMessageId, loader.Owner, msg);
                            }
                            else
                            {
                                // Assign By Ticket
                                int IncidentId = FindIncidentByTicket(TicketUID);

                                if (IncidentId != -1)
                                {
                                    int creatorId, issueBoxId;
                                    using (IDataReader reader = DBIncident.GetIncident(IncidentId, 0, 1))
                                    {
                                        reader.Read();

                                        creatorId  = (int)reader["CreatorId"];
                                        issueBoxId = (int)reader["IncidentBoxId"];
                                    }

                                    int       stateId, managerId, responsibleId;
                                    bool      isResposibleGroup;
                                    ArrayList users = new ArrayList();

                                    Issue2.GetIssueBoxSettings(issueBoxId, out stateId, out managerId, out responsibleId, out isResposibleGroup, users);

                                    EMailMessage.LogOnCreator(creatorId, msg);

                                    int ThreadNodeId = EMailMessage.AddToIncidentMessage(loader.Owner.IsInternal, IncidentId, EMailMessageId, msg);

                                    if (EMailMessage.ProcessXIbnHeaders(IncidentId, ThreadNodeId, msg))
                                    {
                                        try
                                        {
                                            ArrayList excludeUsers = EMailRouterOutputMessage.Send(IncidentId, loader.Owner, msg);
                                            SystemEvents.AddSystemEvents(SystemEventTypes.Issue_Updated_Forum_MessageAdded, IncidentId, -1, excludeUsers);
                                        }
                                        catch (Exception ex)
                                        {
                                            System.Diagnostics.Trace.WriteLine(ex);
                                            Log.WriteError(ex.ToString());
                                        }
                                    }
                                }
                                else
                                {
                                    // Add to pending email table
                                    if (!loader.Owner.IsInternal)
                                    {
                                        EMailMessage.MarkAsPendingMessage(EMailMessageId);
                                    }
                                }
                            }
                            break;

                        case EMailMessageAntiSpamRuleRusult.Deny:
                            // Do nothing
                            break;
                        }

                        tran.Commit();
                    }
                }
                finally
                {
                    Alerts2.SendToCurrentUser = false;

                    if (Security.CurrentUser != prevUser)
                    {
                        Security.SetCurrentUser(prevUser);
                    }
                }
            }
        }