Exemplo n.º 1
0
        public static bool EditArticle(ref ProviderArticle anArticle, ProviderMember aMember, MessageInfo info)
        {
            _log.Info("Populating article from e-mail");
            bool returnValue = false;
            MailAddress senderAddress = new MailAddress(info.Envelope.Sender[0].Address);

            // Now parse the data from the e-mail
            EmailArticle anEmailArticle = new EmailArticle(info, _emailClient);

            if(anEmailArticle.IsHelp && !anEmailArticle.IsSubjectHelp)
            {
                // If we got the help command in the body but not in the subject then send the e-mail.
                // it's important that we check the subject help because otherwise we will send the help e-mail twice.
                _log.Info("Found the [help] command in the e-mail so respond with the help e-mail.");
                EmailManager.Instance.SendHelpEmail(senderAddress, aMember);
            }

            if (anEmailArticle.IsSubjectHelp)
            {
                // If we got help in the subject then ONLY send the help e-mail. Since the subject can't be an article title or id.
                _log.Info("This was a subject help request so send the help e-mail but do nothing else.");
                EmailManager.Instance.SendHelpEmail(senderAddress, aMember);
            }
            else
            {
                if(anEmailArticle.IsBlankEmail)
                {
                    _log.Info("E-mail was completely blank");
                }
                else if (anEmailArticle.Delete)
                {
                    if (anArticle.IsNew)
                    {
                        _log.Info("Attempted to delete a new article, which makes no sense so ignore this e-mail.");
                        MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                    senderAddress.Address,
                                                                    "insideword - article " + info.Envelope.Subject + " rejected",
                                                                    "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                    + "The delete command you sent us in an e-mail was ignored since you tried to delete an article that doesn't exist. Be sure that you used the correct article id number in the subject.");
                        aMailMessage.IsBodyHtml = true;
                        EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                    }
                    else
                    {
                        _log.Info("Deleting article.");
                        string title = anArticle.Title;
                        long id = anArticle.Id.Value;
                        string result;
                        if (anArticle.Delete())
                        {
                            result = "Successfully deleted article " + title + " with id " + id;
                        }
                        else
                        {
                            result = "Failed to delete article " + title + " with id " + id;
                        }
                        _log.Info(result);
                        MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                    senderAddress.Address,
                                                                    "insideword - article " + info.Envelope.Subject + " rejected",
                                                                    "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                    + result + "<br />");
                        aMailMessage.IsBodyHtml = true;
                        EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                        returnValue = false;
                    }
                }
                else
                {
                    if (anArticle.IsNew)
                    {
                        anArticle.CreateDate = DateTime.UtcNow;

                        // only update the member id when we first create the article otherwise an admin could end up taking control when editing it.
                        anArticle.MemberId = aMember.Id.Value;
                    }
                    anArticle.EditDate = DateTime.UtcNow;

                    if (anArticle.IsNew || !string.IsNullOrWhiteSpace(anEmailArticle.Title))
                    {
                        anArticle.Title = anEmailArticle.Title;
                    }

                    if (anEmailArticle.CategoryId.HasValue)
                    {
                        anArticle.AddCategory(anEmailArticle.CategoryId.Value);
                    }

                    if (anEmailArticle.Blurb != null)
                    {
                        if (string.IsNullOrWhiteSpace(anEmailArticle.Blurb))
                        {
                            anArticle.Blurb = null;
                        }
                        else
                        {
                            anArticle.Blurb = anEmailArticle.Blurb;
                        }
                    }

                    // don't overwrite unless we have some content.
                    if (!string.IsNullOrWhiteSpace(anEmailArticle.Text))
                    {
                        if (anArticle.IsNew || !anEmailArticle.Append)
                        {
                            anArticle.RawText = anEmailArticle.Text;
                        }
                        else
                        {
                            anArticle.RawText += anEmailArticle.Text;
                        }
                    }

                    if (anEmailArticle.IsPublished.HasValue)
                    {
                        // if the e-mail specifically set the publish status then set it in the article
                        anArticle.IsPublished = anEmailArticle.IsPublished.Value && anEmailArticle.CategoryId.HasValue;
                    }
                    else if (anArticle.IsNew)
                    {
                        // if the article is new but no status was set in the e-mail then set the status based on the following
                        anArticle.IsPublished = anEmailArticle.CategoryId.HasValue;
                    }
                    else
                    {
                        // don't change the status of the article
                    }
                    anArticle.Save();
                    returnValue = true;
                }

                if (anEmailArticle.IsReadArticle)
                {
                    if (anEmailArticle.ReadArticle.HasValue)
                    {
                        _log.Info("Request to send back article " + anEmailArticle.ReadArticle.Value);
                        try
                        {
                            ProviderArticle aDifferentArticle = new ProviderArticle(anEmailArticle.ReadArticle.Value);
                            if (aDifferentArticle.IsPublished || aMember.CanEdit(aDifferentArticle))
                            {
                                EmailManager.Instance.SendArticleEmail(senderAddress, anArticle, aMember);
                            }
                            else
                            {
                                throw new Exception("Member doesn't have the rights to request article " + anEmailArticle.ReadArticle.Value);
                            }
                        }
                        catch (Exception caughtException)
                        {
                            _log.Info("Failed to send back article " + anEmailArticle.ReadArticle.Value, caughtException);
                            MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                        senderAddress.Address,
                                                                        "insideword - article request " + anEmailArticle.ReadArticle.Value + " failed",
                                                                        "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                        + "Failed to return the article with id " + anEmailArticle.ReadArticle.Value + "<br />"
                                                                        + "Check to make sure the article id is a valid id.<br />");
                            aMailMessage.IsBodyHtml = true;
                            EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                        }
                    }
                    else
                    {
                        _log.Info("Sending back current article");
                        EmailManager.Instance.SendArticleEmail(senderAddress, anArticle, aMember);
                    }
                }
            }

            return returnValue;
        }
Exemplo n.º 2
0
        public static void ParseNewMail()
        {
            // Retrieve newest e-mails
            List<long> uids = _emailClient.SearchFlag(Flag.Unseen);
            if (uids.Count > 0)
            {
                _log.Info("Processing " + uids.Count + " unseen message(s).");

                ProviderMember aMember = new ProviderMember();

                foreach (MessageInfo info in _emailClient.GetMessageInfoByUID(uids))
                {
                    _log.Info("Processing: " + info.Envelope.Sender + " / " + info.Envelope.From + " / " + info.Envelope.Subject);

                    if (ValidEmail(info))
                    {
                        // E-mail header seems good so far.
                        // Fetch the owning member.
                        aMember = GetEmailOwner(info);

                        _log.Info("Checking account settings.");
                        if (aMember.IsBanned)
                        {
                            _log.Info("Member is banned. Ignoring e-mail.");
                            MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                        info.Envelope.From.ToString(),
                                                                        "insideword - article " + info.Envelope.Subject + " rejected",
                                                                        "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                      + "The article you sent to us through e-mail was rejected because your account was banned.");
                            aMailMessage.IsBodyHtml = true;
                            EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                        }
                        else
                        {
                            _log.Info("Member is good.");

                            long articleId = -1;
                            ProviderArticle anArticle = null;

                            // Try parsing the subject. If we successfully parse it as a long then this is an update to that article
                            if (long.TryParse(info.Envelope.Subject, out articleId))
                            {
                                _log.Info("Subject was number " + articleId + ", so attempt to load the article for editing purposes.");
                                if (ProviderArticle.Exists(articleId))
                                {
                                    // This article exists so load it
                                    anArticle = new ProviderArticle(articleId);
                                    _log.Info("Successfully loaded article " + articleId + " - " + anArticle.Title);
                                }
                                else
                                {
                                    _log.Info("Article with id " + articleId + " doesn't exist. Ignoring e-mail.");
                                    MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                                info.Envelope.From.ToString(),
                                                                                "insideword - article rejected",
                                                                                "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                                + "The article you sent to us through e-mail was rejected because you were trying to edit article "
                                                                                + articleId + ", which doesn't exist.");
                                    aMailMessage.IsBodyHtml = true;
                                    EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                                }
                            }
                            else
                            {
                                _log.Info("This is a new article.");
                                anArticle = new ProviderArticle();
                            }

                            if (anArticle != null)
                            {
                                _log.Info("Check member's edit rights for the given article.");
                                if (!aMember.CanEdit(anArticle))
                                {
                                    _log.Info("Member does not have the rights to edit this article. Ignoring e-mail.");
                                    MailMessage aMailMessage = new MailMessage("donotreply@insideword",
                                                                                info.Envelope.From.ToString(),
                                                                                "insideword - article rejected",
                                                                                "DO NOT REPLY TO THIS E-MAIL<br />"
                                                                              + "The article you sent to us through e-mail was rejected because you do not have the rights to edit it!");
                                    aMailMessage.IsBodyHtml = true;
                                    EmailManager.Instance.DefaultSmtp.Send(aMailMessage);
                                }
                                else if (EditArticle(ref anArticle, aMember, info))
                                {
                                    MailAddress address = new MailAddress(info.Envelope.From.ToString());
                                    EmailManager.Instance.SendEditArticleEmail(address, anArticle, aMember);
                                }
                            }
                        }
                    }

                    _emailClient.MarkMessageSeen(info.MessageNumber);
                    _log.Info("Marking message as seen");
                    _log.Info("Done processing: " + info.Envelope.Sender + " / " + info.Envelope.From + " / " + info.Envelope.Subject);
                }

                _ctxManager.DisposeCtx();
            }
        }