Ejemplo n.º 1
0
 public void CloseWithComment(string comment)
 {
     SupportDatabase.CloseWithComment(this.Identity, comment);
 }
Ejemplo n.º 2
0
        public static void NotifyBouncingEmails()
        {
            Regex regex = new Regex(@"(?<email>[a-z_0-9\.\+\-]+\@[a-z_0-9\.\-]+)",
                                    RegexOptions.Compiled | RegexOptions.IgnoreCase);

            string  SMSBouncingEmail = ("" + Persistence.Key["SMSBouncingEmail"]).ToUpper().Trim();
            Regex   reIsNo           = new Regex(@"(NO)|(N)|(NEJ)", RegexOptions.IgnoreCase);
            Boolean dontSendSMS      = reIsNo.IsMatch(SMSBouncingEmail);
            Boolean holdSMS          = SMSBouncingEmail == "HOLD";
            int     lastCaseHandled  = 0;

            if (PhoneMessageTransmitter.CheckServiceStatus() == false)
            {
                dontSendSMS = true;
            }

            SupportCase[] cases = null;
            while (lastCaseHandled == 0 || (cases != null && cases.Length > 0))
            {
                cases = SupportDatabase.GetUndeliverableCases(lastCaseHandled, 100);

                if (cases.Length == 0)  // very very important bugfix
                {
                    break;
                }

                foreach (SupportCase @case in cases)
                {
                    lastCaseHandled = @case.Identity;
                    bool attemptRecovery = true;

                    if (@case.Title.ToLower().Contains("ditt medlemskap har"))
                    {
                        // "Ditt medlemskap har gått ut"

                        attemptRecovery = false;
                    }

                    if (@case.Title.ToLower().Contains("press"))
                    {
                        // Press release

                        attemptRecovery = false;
                    }

                    if (attemptRecovery)
                    {
                        string body = GetFirstEventText(@case.Identity);

                        // Strip the mail header

                        int bodySeparator = body.IndexOf("\r\n\r\n");

                        if (bodySeparator > 0)
                        {
                            body = body.Substring(bodySeparator + 4);
                        }
                        else
                        {
                            body = string.Empty;
                        }

                        Match match = regex.Match(body);
                        if (match.Success)
                        {
                            string email  = match.Groups["email"].Value;
                            People people = People.FromEmail(email);

                            if (people.Count > 2)
                            {
                                CloseWithComment(@case.Identity,
                                                 "Bounced message closed. More than one person matches the email address. Both marked unreachable.");

                                foreach (Person person in people)
                                {
                                    person.MailUnreachable = true;
                                }
                            }

                            else if (people.Count < 1)
                            {
                                CloseWithComment(@case.Identity,
                                                 "Bounced message closed. No person in the database matches the email address.");
                            }

                            else
                            {
                                // When we get here, there is exactly one person matching in the database

                                Person person = people[0];

                                bool hasActiveMemberships = false;

                                Memberships memberships = person.GetMemberships();

                                foreach (Membership membership in memberships)
                                {
                                    if (membership.Active)
                                    {
                                        hasActiveMemberships = true;
                                    }
                                }

                                if (hasActiveMemberships)
                                {
                                    // Attempt to contact by SMS.
                                    if (!holdSMS)
                                    {
                                        if (person.Phone.Trim().Length > 2)
                                        {
                                            if (dontSendSMS)
                                            {
                                                CloseWithComment(@case.Identity,
                                                                 "The person at phone# " + person.Phone + ", " + person.Name +
                                                                 " (#" + person.Identity.ToString() +
                                                                 "), was not contacted due to that SMS notification is currently turned off. Bounced message closed.");
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    person.SendPhoneMessage("Piratpartiet: den mailadress vi har till dig (" +
                                                                            person.Mail +
                                                                            ") studsar. Kontakta [email protected] med ny adress.");
                                                    CloseWithComment(@case.Identity,
                                                                     "Successfully notified the member at phone# " + person.Phone +
                                                                     ", " + person.Name + " (#" + person.Identity.ToString() +
                                                                     "), about the bounced email using an SMS message. Case closed.");
                                                }
                                                catch (Exception)
                                                {
                                                    CloseWithComment(@case.Identity,
                                                                     "The person at phone# " + person.Phone + ", " + person.Name +
                                                                     " (#" + person.Identity.ToString() +
                                                                     "), could not be reached over SMS. This member has been marked unreachable. Bounced message closed.");
                                                    person.MailUnreachable = true;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            CloseWithComment(@case.Identity,
                                                             person.Name + " (#" + person.Identity.ToString() +
                                                             ") does not have a listed phone number. This member has been marked unreachable. Bounced message closed.");
                                            person.MailUnreachable = true;
                                        }
                                    }
                                }
                                else
                                {
                                    CloseWithComment(@case.Identity,
                                                     "The person at phone# " + person.Phone + ", " + person.Name + " (#" +
                                                     person.Identity.ToString() +
                                                     ") has no active memberships. Bounced message closed.");
                                }
                            }
                        }
                        else
                        {
                            CloseWithComment(@case.Identity, "Bounced message closed. No email address could be located.");
                        }
                    }
                    else
                    {
                        CloseWithComment(@case.Identity, "Bounced message closed without attempt of recovery.");
                    }
                }
            }
        }