Beispiel #1
0
        private IncomeSMS ChangeIncomeSMSStatus(IncomeSMS incomeSMS, IncomeSMS.IncomeSMSStatus Status, SMSContext context)
        {
            try
            {
                incomeSMS.Status = Status;
                context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
            }
            catch (Exception e)
            {
                logger.Error("Ошибка записи  в базу данных. Не удалось сохранить изменение статуса входящего сообщения.\r\nТекст ошибки:", e);
            }

            return incomeSMS;
        }
Beispiel #2
0
        public void ProcesingNewIncomeClientEmail(ImapX.Message[] emailMessages, List<Client> clients)
        {
            bool IsClientButNotAnswerFlag;

            using (SMSContext context = new SMSContext())
            {
                List<ImapX.Message> localUids = new List<ImapX.Message>();
                foreach (ImapX.Message message in emailMessages)
                {

                    IsClientButNotAnswerFlag = false;
                    if (message != null && message.From != null && !String.IsNullOrEmpty(message.From.Address))
                    {
                        Client client = clients.FirstOrDefault(x => x.Email.ToLower().Equals(message.From.Address.ToLower()));
                        if (client != null && ClientIsNotBlocked(client))
                        {
                            string SecretKey = message.Subject.Substring(message.Subject.IndexOf('№') + 1).Replace(" ", "").Replace("\u2192", "");
                            if (!String.IsNullOrEmpty(SecretKey) && message.Body != null)
                            {
                                IncomeSMS incomeSMS = GetIncomeSMS(SecretKey, client.ClientId, context);
                                if (incomeSMS != null)
                                {
                                    string text;

                                    string messageText = null;

                                    try
                                    {
                                        messageText = message.Body.Text;
                                    }
                                    catch
                                    {
                                        messageText = emailBox.GetTrubleMessageText(message.From.Address, message.Subject, (DateTime)message.Date);
                                    }

                                    if (messageText != null)
                                    {
                                        if (messageText.Contains("\r\n)\r\nIMAPX"))
                                            text = messageText.Substring(0, messageText.IndexOf("\r\n)\r\nIMAPX"));
                                        else
                                            text = messageText;
                                        if (text.Contains("\r\n"))
                                            text = text.Substring(0, text.IndexOf("\r\n"));

                                        IncomeClientEmail incomeClientEmail = new IncomeClientEmail()
                                        {

                                            ClientId = client.ClientId,
                                            DateTime = DateTime.Now,
                                            MessageId = incomeSMS.MessageId,
                                            IsSent = false,
                                            SenderEmail = client.Email,
                                            SecretKey = SecretKey,
                                            RecipientEmail = emailBox.Email,
                                            Text = text
                                        };
                                        incomeClientEmail = SaveIncomeClientEmail(incomeClientEmail, context);

                                        AddAnswer(incomeSMS.MessageId, incomeClientEmail.IncomeClientEmailId, Answer.AnswerSource.Email);

                                        if (incomeSMS.MessageType == IncomeSMS.Type.Email)
                                            emailBox.SendEmail(text, "Re:" + incomeSMS.ShortKey, incomeSMS.SenderAddress, SendIncomeClientEmailAnswerSetResult, incomeClientEmail);
                                        else
                                        {
                                            incomeClientEmail.IsSent = gsmModemList[incomeSMS.RecipientNumber].SendSMS(incomeSMS.SenderNumber, text);
                                            SaveIncomeClientEmail(incomeClientEmail, context);
                                        }

                                        localUids.Add(message);
                                    }
                                }
                                else
                                {
                                    IsClientButNotAnswerFlag = true;
                                }
                            }
                        }

                        if (client == null || IsClientButNotAnswerFlag)
                        {
                            string ShortKey = message.Subject.Replace(" ", "");
                            client = GetClientByShortKey(ShortKey, context);
                            if (client != null && ClientIsNotBlocked(client))
                            {
                                ServicePhone servicePhone = GetPhoneByClient(client, context);
                                if (servicePhone != null)
                                {
                                    string text;
                                    string messageText = null;

                                    try
                                    {
                                        messageText = message.Body.Text;
                                    }
                                    catch
                                    {
                                        messageText = emailBox.GetTrubleMessageText(message.From.Address, message.Subject, (DateTime)message.Date);
                                    }

                                    if (messageText != null)
                                    {

                                        if (messageText.Contains("\r\n)\r\nIMAPX"))
                                            text = messageText.Substring(0, messageText.IndexOf("\r\n)\r\nIMAPX"));
                                        else
                                            text = messageText;
                                        if (text.Contains("\r\n"))
                                            text = text.Substring(0, text.IndexOf("\r\n"));

                                        IncomeSMS incomeSMS = new IncomeSMS()
                                          {
                                              SenderAddress = message.From.Address.ToLower().Replace(" ", ""),
                                              RecipientNumber = servicePhone.PhoneNumber,
                                              Client = client,
                                              Text = text,
                                              DateTime = DateTime.Now,
                                              SecretKey = GetSecretKey(client),
                                              Status = IncomeSMS.IncomeSMSStatus.Checked,
                                              Category = "new",
                                              IsReaded = false,
                                              IsAnswered = false,
                                              SMSIsSend = false,
                                              ShortKey = ShortKey,
                                              MessageType = IncomeSMS.Type.Email
                                          };
                                        SaveIncomeSms(incomeSMS, context);
                                        localUids.Add(message);
                                    }

                                }
                            }
                        }

                    }
                }
                if (localUids.Count > 0) emailBox.SetSeen(localUids);
            }
        }
Beispiel #3
0
 private IncomeSMS SaveIncomeSMS(Client client, string text, string senderNumber, string recipienNumber, DateTime date, string ShortKey, SMSContext context)
 {
     try
     {
         IncomeSMS incomeSMS = new IncomeSMS()
         {
             SenderNumber = senderNumber,
             RecipientNumber = recipienNumber,
             Client = client,
             Text = text,
             DateTime = date,
             SecretKey = GetSecretKey(client),
             Status = IncomeSMS.IncomeSMSStatus.Received,
             Category = "new",
             IsReaded = false,
             IsAnswered = false,
             SMSIsSend = false,
             ShortKey = ShortKey,
             MessageType = IncomeSMS.Type.SMS
         };
         context.IncomeSMS.Add(incomeSMS);
         context.SaveChanges();
         return incomeSMS;
     }
     catch (Exception e)
     {
         logger.Error("Ошибка записи  в базу данных. Не удалось добавить  входящее сообщения.\r\nТекст ошибки:", e);
         return null;
     }
 }
Beispiel #4
0
        private IncomeSMS SaveIncomeSms(IncomeSMS incomeSMS, SMSContext context)
        {
            try
            {
                if (incomeSMS.MessageId == 0)
                    context.IncomeSMS.Add(incomeSMS);
                else
                    context.Entry(incomeSMS).State = System.Data.Entity.EntityState.Modified;

                context.SaveChanges();

            }
            catch (Exception e)
            {
                logger.Error("Ошибка чтения  из базы данных.Не удалось сохранить  входящее сообщения.\r\nТекст ошибки:", e);
            }

            return incomeSMS;
        }
Beispiel #5
0
        private IncomeClientSMS SaveIncomeClientSMS(ShortMessage message, ServicePhone ServicePhone, IncomeSMS incomeSMS, Client client, SMSContext context)
        {
            try
            {

                IncomeClientSMS incomeClientSMS = new IncomeClientSMS()
                {
                    Client = client,
                    ClientId = client.ClientId,
                    DateTime = message.SentDate,
                    Text = message.Text,
                    SenderNumber = client.PhoneNumber,
                    RecipientNumber = ServicePhone.PhoneNumber,
                    Status = IncomeClientSMS.IncomeClientSMSStatus.Received,
                    SecretKey = incomeSMS.SecretKey,
                    MessageId = incomeSMS.MessageId
                };

                context.IncomeClientSMS.Add(incomeClientSMS);
                context.SaveChanges();

                return incomeClientSMS;
            }
            catch (Exception e)
            {
                logger.Error("Ошибка записи  в базу данных. Не удалось добавить сообщение клиента с ответом.\r\nТекст ошибки:", e);
                return null;
            }
        }