Пример #1
0
        public IActionResult EditTemplatePost(string id, MailTemplateModel model, string action)
        {
            memoryCache?.Remove("Template_" + id);

            if (action == "delete")
            {
                return(EditTemplateDelete(id));
            }
            else if (action == "send")
            {
                model.All = Request.Form.ContainsKey("All");
                return(EditTemplateSend(id, model.All));
            }

            try
            {
                model.Value.Name = model.Value.Name?.Trim();
                if (model.Value.Name.Length > 64)
                {
                    throw new ArgumentException(Resources.NameIsTooLong.FormatHtml(64));
                }
                if (!model.Value.GetListNameAndTemplateName(out string listName, out string templateName) ||
                    !MailTemplate.ValidateName(listName) ||
                    !MailTemplate.ValidateName(templateName))
                {
                    throw new ArgumentException(Resources.TemplateNameInvalidChars.FormatHtml(MailTemplate.FullNameSeparator));
                }
                using (MailDemonDatabase db = dbProvider.GetDatabase())
                {
                    if (db.Lists.FirstOrDefault(l => l.Name == listName) == null)
                    {
                        throw new ArgumentException(string.Format(Resources.ListNotFound, listName));
                    }
                    model.Value.LastModified = DateTime.UtcNow;
                    model.Value.Dirty        = true;
                    if (model.Value.Id == 0)
                    {
                        db.Templates.Add(model.Value);
                    }
                    else
                    {
                        db.Update(model.Value);
                    }
                    db.SaveChanges();
                }
                TempData["Message"] = Resources.Success;
                return(RedirectToAction(nameof(EditTemplate), new { id = model.Value.Name }));
            }
            catch (Exception ex)
            {
                MailDemonLog.Error(ex);
                model.Error   = true;
                model.Message = ex.Message;
                return(View(model));
            }
        }
Пример #2
0
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     do
     {
         DateTime dt = DateTime.UtcNow;
         using (MailDemonDatabase db = serviceProvider.GetService <MailDemonDatabase>())
         {
             db.Subscriptions.RemoveRange(db.Subscriptions.Where(r => r.Expires <= dt && r.UnsubscribeToken == null));
             db.SaveChanges();
         }
     }while (!(await stoppingToken.WaitHandle.WaitOneAsync(loopTimeSpan, stoppingToken)));
 }
Пример #3
0
        public IActionResult EditListPost(string id, MailListModel model, string action)
        {
            if (action == "delete")
            {
                return(EditListDelete(id));
            }

            try
            {
                model.Value.Name = model.Value.Name?.Trim();
                if (model.Value.Name.Length > 16)
                {
                    throw new ArgumentException(Resources.NameIsTooLong.FormatHtml(16));
                }
                else if (!model.Value.FromEmailAddress.TryParseEmailAddress(out _))
                {
                    throw new ArgumentException(Resources.EmailIsInvalid);
                }
                model.Value.Company = model.Value.Company?.Trim();
                model.Value.Website = model.Value.Website?.Trim();
                if (!MailTemplate.ValidateName(model.Value.Name))
                {
                    throw new ArgumentException(Resources.NameInvalidChars);
                }
                using (MailDemonDatabase db = dbProvider.GetDatabase())
                {
                    MailList existingList = db.Lists.FirstOrDefault(l => l.Name == model.Value.Name);
                    if (existingList != null && (existingList.Name != model.Value.Name || model.Value.Id == 0))
                    {
                        throw new ArgumentException(Resources.NameCannotChange);
                    }
                    if (model.Value.Id == 0)
                    {
                        db.Lists.Add(model.Value);
                    }
                    else
                    {
                        db.Update(model.Value);
                    }
                    db.SaveChanges();
                }
                TempData["Message"] = Resources.Success;
                return(RedirectToAction(nameof(EditList), new { id = model.Value.Name }));
            }
            catch (Exception ex)
            {
                MailDemonLog.Error(ex);
                model.Error   = true;
                model.Message = ex.Message;
                return(View(model));
            }
        }
Пример #4
0
        /// <summary>
        /// Unsubscribe from a mailing list
        /// </summary>
        /// <param name="db">DB</param>
        /// <param name="listName">List name</param>
        /// <param name="token">Unsubscribe token</param>
        /// <returns>True if unsubscribed, false if not</returns>
        public static bool UnsubscribeFromMailingList(this MailDemonDatabase db, string listName, string token)
        {
            MailListSubscription foundReg = db.Subscriptions.FirstOrDefault(r => r.UnsubscribeToken == token && r.ListName == listName && r.UnsubscribedDate == default);

            if (foundReg != null)
            {
                foundReg.UnsubscribedDate = DateTime.UtcNow;
                foundReg.SubscribeToken   = null;
                db.SaveChanges();
                return(true);
            }
            ;
            return(false);
        }
Пример #5
0
 public IActionResult Subscribers(string id, string action, long?subId)
 {
     if (action == "delete" && subId != null)
     {
         using MailDemonDatabase db = dbProvider.GetDatabase();
         MailListSubscription sub = db.Subscriptions.FirstOrDefault(s => s.Id == subId);
         if (sub != null)
         {
             db.Subscriptions.Remove(sub);
             db.SaveChanges();
         }
     }
     return(RedirectToAction(nameof(Subscribers), new { id }));
 }
Пример #6
0
        /// <summary>
        /// Confirm subscribe to a mailing list
        /// </summary>
        /// <param name="db">DB</param>
        /// <param name="listName">List name</param>
        /// <param name="token">Subscribe token</param>
        /// <returns>Registration or null if not found</returns>
        public static MailListSubscription ConfirmSubscribeToMailingList(this MailDemonDatabase db, string listName, string token)
        {
            MailListSubscription reg      = null;
            MailListSubscription foundReg = db.Subscriptions.FirstOrDefault(r => r.SubscribeToken == token);

            if (foundReg != null && foundReg.ListName == listName && foundReg.SubscribedDate == default && foundReg.SubscribeToken == token)
            {
                reg = foundReg;
                foundReg.Expires          = DateTime.MaxValue;
                foundReg.SubscribedDate   = DateTime.UtcNow;
                foundReg.UnsubscribedDate = default;
                foundReg.UnsubscribeToken = Guid.NewGuid().ToString("N");
                foundReg.MailList         = db.Lists.FirstOrDefault(l => l.Name == listName);
                foundReg.Result           = "New";
                db.SaveChanges();
            }
            return(reg);
        }
Пример #7
0
 private IActionResult EditTemplateDelete(string id)
 {
     try
     {
         using MailDemonDatabase db = dbProvider.GetDatabase();
         MailTemplate template = db.Templates.FirstOrDefault(t => t.Name == id);
         if (template != null)
         {
             db.Templates.Remove(template);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         MailDemonLog.Error(ex);
     }
     return(RedirectToAction(nameof(EditTemplate)));
 }
Пример #8
0
        private void InitializeDB(IApplicationBuilder app)
        {
            using var db = new MailDemonDatabase(Configuration);
            db.Initialize();

            // migrate away from litedb
            string migrationPath = Path.Combine(Directory.GetCurrentDirectory(), "MailDemon.db");

            if (File.Exists(migrationPath))
            {
                MailDemonLog.Warn("Migrating from old database {0}", migrationPath);
                var tran = db.Database.BeginTransaction();
                try
                {
                    using (FileStream fs = File.OpenRead(migrationPath))
                        using (LiteDB.LiteDatabase oldDb = new LiteDB.LiteDatabase(fs))
                        {
                            foreach (MailList list in oldDb.GetCollection <MailList>().FindAll())
                            {
                                db.Lists.Add(list);
                            }
                            foreach (MailTemplate template in oldDb.GetCollection <MailTemplate>().FindAll())
                            {
                                db.Templates.Add(template);
                            }
                            foreach (MailListSubscription sub in oldDb.GetCollection <MailListSubscription>().FindAll())
                            {
                                db.Subscriptions.Add(sub);
                            }
                            db.SaveChanges();
                            tran.Commit();
                            tran = null;
                            MailDemonLog.Warn("Migration success");
                        }
                    File.Delete(migrationPath);
                }
                finally
                {
                    tran?.Rollback();
                }
            }
        }
Пример #9
0
 private IActionResult EditListDelete(string id)
 {
     try
     {
         using MailDemonDatabase db = dbProvider.GetDatabase();
         MailList list = db.Lists.FirstOrDefault(l => l.Name == id);
         if (list != null)
         {
             db.Subscriptions.RemoveRange(db.Subscriptions.Where(r => r.ListName == id));
             db.Templates.RemoveRange(db.Templates.Where(t => t.Name.StartsWith(list.Name + MailTemplate.FullNameSeparator)));
             db.Lists.Remove(list);
             db.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         MailDemonLog.Error(ex);
     }
     return(RedirectToAction(nameof(EditList)));
 }
Пример #10
0
        /// <summary>
        /// Pre subscribe to a mailing list
        /// </summary>
        /// <param name="db">DB</param>
        /// <param name="reg">Registration, receives new registration if success</param>
        /// <returns>True if success, false if already subscribed</returns>
        public static bool PreSubscribeToMailingList(this MailDemonDatabase db, ref MailListSubscription reg)
        {
            bool   result = false;
            string token  = string.Empty;
            MailListSubscription final = reg;
            MailListSubscription dbReg = db.Subscriptions.FirstOrDefault(r => r.EmailAddress == final.EmailAddress && r.ListName == final.ListName);

            if (dbReg != null)
            {
                dbReg.Fields.Clear();
                foreach (var kv in final.Fields)
                {
                    dbReg.SetField(kv.Key, kv.Value);
                }
                dbReg.Error        = final.Error;
                dbReg.Message      = final.Message;
                dbReg.IPAddress    = final.IPAddress;
                dbReg.MailList     = final.MailList;
                dbReg.TemplateName = final.TemplateName;
                final = dbReg;
            }
            reg = final;
            if (reg.SubscribeToken == null)
            {
                reg.SubscribeToken = Guid.NewGuid().ToString("N");
                reg.Expires        = DateTime.UtcNow.AddHours(1.0);
                if (reg.Id == 0)
                {
                    db.Subscriptions.Add(reg);
                }
                else
                {
                    db.Update(reg);
                }
                result = true;
            }
            db.SaveChanges();
            return(result);
        }