Exemplo n.º 1
0
        public EmailFilter AddEmailFilter(string mbname, [FromBody] EmailFilterDto filterToAdd)
        {
            var filter = Mapper.Map<EmailFilter>(filterToAdd);
            ImapMailBoxConfiguration conf;

            if (filter.MoveToFolder == null)
            {
                filter.MoveToFolder = "";
            }

            if (filter.ForwardToAddress == null)
            {
                filter.ForwardToAddress = "";
            }

            using (var ctx = new MailModelContainer())
            {
                conf = ctx.ImapMailBoxConfigurations.FirstOrDefault(x => x.MailBoxName.Equals(mbname));

                if (conf == null) return null;

                conf.EmailFilters.Add(filter);

                ctx.SaveChanges();
            }

            Task.Factory.StartNew(() => InboxWatcher.ConfigureMailBox(conf));

            return filter;
        }
Exemplo n.º 2
0
        public IEnumerable<IEmail> GetEmails(string mailBoxName, bool fromtoday = false)
        {
            using (var ctx = new MailModelContainer())
            {
                var emails = ctx.Emails
                    .Where(x => x.ImapMailBoxConfigurationId == ctx.ImapMailBoxConfigurations.FirstOrDefault(y => y.MailBoxName.Equals(mailBoxName)).Id)
                    .Include(l => l.EmailLogs);

                if (fromtoday)
                    emails =
                        emails.Where(
                            x =>
                                x.TimeReceived.Year == DateTime.Now.Year && 
                                x.TimeReceived.Month == DateTime.Now.Month &&
                                x.TimeReceived.Day == DateTime.Now.Day);

                var emailDtos = new List<IEmail>();

                foreach (var email in emails.Take(500))
                {
                    emailDtos.Add(new EmailDto(email));
                }

                return emailDtos.OrderByDescending(x => x.Id);
            }
        }
 public IEnumerable<NotificationConfiguration> GetNotificationConfigurations(string mbname)
 {
     using (var ctx = new MailModelContainer())
     {
         var selectedNotifications = ctx.NotificationConfigurations.Where(x => x.ImapMailBoxConfiguration.MailBoxName.Equals(mbname)).ToList();
         selectedNotifications.ForEach(x => x.NotificationType = x.NotificationType.Split('.')[2]);
         return selectedNotifications;
     }
 }
 public IEnumerable<NotificationConfiguration> GetNotificationConfigurations()
 {
     using (var ctx = new MailModelContainer())
     {
         var selectedNotifications = ctx.NotificationConfigurations.ToList();
         selectedNotifications.ForEach(x => x.NotificationType = x.NotificationType.Split('.')[2]);
         return selectedNotifications;
     }
 }
Exemplo n.º 5
0
 public EmailFilter DeleteEmailFilter(string mbname, int id)
 {
     using (var ctx = new MailModelContainer())
     {
         var selection = ctx.EmailFilters.FirstOrDefault(x => x.Id == id);
         ctx.EmailFilters.Remove(selection);
         ctx.SaveChanges();
         return selection;
     }
 }
Exemplo n.º 6
0
        public EmailFilterer(IImapMailBox attachedMailBox)
        {
            _attachedMailBox = attachedMailBox;
            _attachedMailBox.NewMessageReceived += FilterOnMessageReceived;

            using (var ctx = new MailModelContainer())
            {
                _emailFilters.AddRange(ctx.EmailFilters.ToList());
            }
        }
Exemplo n.º 7
0
        public Email GetEmail(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                ctx.Configuration.ProxyCreationEnabled = false;
                var email = ctx.Emails.Include(x => x.EmailLogs).FirstOrDefault(x => x.Id == id);

                return email;
            }
        }
        public IClientConfiguration Get(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                ctx.Configuration.ProxyCreationEnabled = false;
                var selection = ctx.ImapMailBoxConfigurations.FirstOrDefault(x => x.Id == id);
                if (selection != null) return selection;

                return new ImapMailBoxConfiguration();
            }
        }
        public HttpResponseMessage Delete(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                var itemToDelete = ctx.NotificationConfigurations.Single(x => x.Id == id);
                ctx.NotificationConfigurations.Remove(itemToDelete);
                ctx.SaveChanges();
            }

            return new HttpResponseMessage(HttpStatusCode.OK);
        }
Exemplo n.º 10
0
        public int GetCountEmails(string mailBoxName)
        {
            using (var ctx = new MailModelContainer())
            {
                var selectedMailBox =
                    ctx.ImapMailBoxConfigurations.FirstOrDefault(x => x.MailBoxName.Equals(mailBoxName));

                if (selectedMailBox == null) return 0;

                return ctx.Emails.Count(x => x.ImapMailBoxConfigurationId == selectedMailBox.Id);
            }
        }
 public IEnumerable<IClientConfiguration> GetMailBoxConfigurations()
 {
     using (var ctx = new MailModelContainer())
     {
         var results = new List<IClientConfiguration>();
         foreach (var selection in ctx.ImapMailBoxConfigurations)
         {
             results.Add(new ClientConfigurationDto(selection));
         }
         return results;
     }
 }
        public HttpResponseMessage DeleteMailBox(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                var selection = ctx.ImapMailBoxConfigurations.First(x => x.Id == id);

                InboxWatcher.MailBoxes.Remove(selection.Id);

                ctx.ImapMailBoxConfigurations.Attach(selection);
                ctx.ImapMailBoxConfigurations.Remove(selection);
                ctx.SaveChanges();
            }

            return new HttpResponseMessage(HttpStatusCode.OK);
        }
Exemplo n.º 13
0
        public EmailFilter ChangeEmailFilter(string mbname, [FromBody] EmailFilterDto filterToAdd, int id)
        {
            var filter = Mapper.Map<EmailFilter>(filterToAdd);

            using (var ctx = new MailModelContainer())
            {
                var selection = ctx.EmailFilters.First(x => x.Id == id);

                selection.FilterName = filter.FilterName;
                selection.ForwardThis = filter.ForwardThis;
                selection.ForwardToAddress = filter.ForwardToAddress;
                selection.MoveToFolder = filter.MoveToFolder;
                selection.SentFromContains = filter.SentFromContains;
                selection.SubjectContains = filter.SubjectContains;

                ctx.SaveChanges();
                return selection;
            }
        }
Exemplo n.º 14
0
        public NotificationConfiguration UpdateNotificationConfiguration(Dictionary<string, object> data)
        {
            var notificationAction = GetNotificationInstance(data);
            var id = int.Parse(data.First(x => x.Key.Equals("Id")).Value.ToString());

            using (var ctx = new MailModelContainer())
            {
                var selection = ctx.NotificationConfigurations.Find(id);
                selection.ConfigurationXml = notificationAction.Serialize();
                selection.ImapMailBoxConfigurationId = int.Parse(data.First(x => x.Key.Equals("MailBoxId")).Value.ToString());

                ctx.Entry(selection).State = EntityState.Modified;
                ctx.SaveChanges();
                return selection;
            }
        }
Exemplo n.º 15
0
        public HttpResponseMessage TestNotification(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                var selectedConfig = ctx.NotificationConfigurations.First(x => x.Id == id);

                var t = Type.GetType(selectedConfig.NotificationType);
                if (t == null) return new HttpResponseMessage(HttpStatusCode.NotFound);

                var action = (INotificationAction)Activator.CreateInstance(t);
                action = action.DeSerialize(selectedConfig.ConfigurationXml);
                action.TestNotification();
            }

            return new HttpResponseMessage(HttpStatusCode.OK);
        }
Exemplo n.º 16
0
        public NotificationConfiguration PostNotification(Dictionary<string, object> data)
        {
            var notificationAction = GetNotificationInstance(data);

            var not = new NotificationConfiguration()
            {
                ConfigurationXml = notificationAction.Serialize(),
                NotificationType = notificationAction.GetType().FullName,
                ImapMailBoxConfigurationId =
                            int.Parse(data.First(x => x.Key.Equals("MailBoxId")).Value.ToString()),
            };

            //add a new record
            using (var ctx = new MailModelContainer())
            {
                ctx.NotificationConfigurations.Add(not);
                ctx.SaveChanges();
                var config = ctx.ImapMailBoxConfigurations.FirstOrDefault(x => x.Id == not.ImapMailBoxConfigurationId);
                Task.Factory.StartNew(() => InboxWatcher.ConfigureMailBox(config));
            }

            return not;
        }
Exemplo n.º 17
0
        public PagedResult Search(string mailBoxName, string search = "", string order = "asc", int limit = 0, int offset = 0, bool inQueue = false)
        {
            if (string.IsNullOrEmpty(search))
            {
                search = "";
            }

            search = search.ToLower();

            var results = new PagedResult();

            using (var ctx = new MailModelContainer())
            {
                var rows = ctx.Emails.Where(x => x.ImapMailBoxConfiguration.MailBoxName.Equals(mailBoxName)).Include(em => em.EmailLogs);

                if (!rows.Any()) return results;

                if (!string.IsNullOrEmpty(search))
                {
                    rows = rows.Where(x => x.Sender.ToLower().Contains(search) || 
                        x.Subject.ToLower().Contains(search) || 
                        x.BodyText.ToLower().Contains(search) || 
                        x.EmailLogs.Any(log => log.TakenBy.Contains(search) || log.Action.Contains(search)));
                }

                if (inQueue)
                {
                    rows = rows.Where(x => x.InQueue);
                }

                rows = !order.Equals("asc") ? rows.OrderByDescending(x => x.TimeReceived) : rows.OrderBy(x => x.TimeReceived);

                var rowCount = rows.Count();

                rows = limit != 0 ? rows.Skip(offset).Take(limit) : rows;

                results.rows = rows.ToList().Select(email => new EmailDto(email));
                results.total = rowCount;

                return results;
            }
        }
        public HttpResponseMessage ResetMailBox(string mbName)
        {
            using (var ctx = new MailModelContainer())
            {
                var selection = ctx.ImapMailBoxConfigurations.FirstOrDefault(x => x.MailBoxName.Equals(mbName));

                if (selection == null) return new HttpResponseMessage(HttpStatusCode.NotFound);

                Task.Factory.StartNew(async () => { await InboxWatcher.ConfigureMailBox(selection); });

                return new HttpResponseMessage(HttpStatusCode.OK);
            }
        }
Exemplo n.º 19
0
        private async Task<bool> FreshenMailBox()
        {
            if (Freshening) return false;
            
            Freshening = true;

            Trace.WriteLine($"{MailBoxName}: Freshening");

            var templist = EmailList;

            EmailList.Clear();

            try
            {
                foreach (var email in await _imapWorker.FreshenMailBox())
                {
                    EmailList.Add(email);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                Exceptions.Add(ex);
                EmailList = templist;
                Freshening = false;
                await _imapWorker.Setup().ConfigureAwait(false);
                return false;
            }

            using (var ctx = new MailModelContainer())
            {
                //LogEmailReceived returns false if email already exists in db => foreach email that wasn't previously received
                foreach (var email in EmailList)
                {
                    if (!await _mbLogger.LogEmailReceived(email))
                    {
                        await _mbLogger.LogEmailBackInQueue(email);
                        continue;
                    }

                    _notificationActions.ForEach(async x =>
                    {
                        var notify = x?.Notify(email, NotificationType.Received, MailBoxName);
                        if (notify != null)
                            await notify;
                    });
                    NewMessageReceived?.Invoke(email, EventArgs.Empty);

                    //set any email that is in the inbox to InQueue = true.
                    foreach(var em in ctx.Emails.Where(x => !x.InQueue && x.ImapMailBoxConfigurationId == _config.Id && x.EnvelopeID.Equals(email.Envelope.MessageId)))
                    {
                        em.InQueue = true;
                    }
                }

                //take care of any emails that may have been left as marked in queue from previous shutdown/disconnect
                foreach (var email in ctx.Emails.Where(email => email.InQueue && email.ImapMailBoxConfigurationId == _config.Id))
                {
                    if (!templist.Any(x => x.Envelope.MessageId.Equals(email.EnvelopeID)))
                    {
                        email.InQueue = false;
                    }
                }

                await ctx.SaveChangesAsync();
            }

            //set any email in db that is marked InQueue but not in current inbox as removed
            foreach (var summary in templist.Where(summary => !EmailList.Any(x => x.Envelope.MessageId.Equals(summary.Envelope.MessageId))))
            {
                await _mbLogger.LogEmailRemoved(summary);
            }
            
            Freshening = false;

            return true;
        }
        public IClientConfiguration Post(ClientConfigurationDto conf)
        {
            IClientConfiguration result;
            using (var ctx = new MailModelContainer())
            {
                result = ctx.ImapMailBoxConfigurations.Add(conf.GetMailBoxConfiguration());
                ctx.SaveChanges();
            }

            Task.Factory.StartNew(async () => { await InboxWatcher.ConfigureMailBox(result); });

            return new ClientConfigurationDto(result);
        }
Exemplo n.º 21
0
 public IEnumerable<EmailFilter> GetEmailFilters()
 {
     using (var ctx = new MailModelContainer())
     {
         return ctx.EmailFilters.ToList();
     }
 }
Exemplo n.º 22
0
 public IEnumerable<EmailFilter> GetMailBoxEmailFilters(string mbname)
 {
     using (var ctx = new MailModelContainer())
     {
         return ctx.EmailFilters.Where(x => x.ImapMailBoxConfiguration.MailBoxName.Equals(mbname)).ToList();
     }
 }
Exemplo n.º 23
0
        public HttpResponseMessage GetSingleNotificationScript(int id)
        {
            using (var ctx = new MailModelContainer())
            {
                if (!ctx.NotificationConfigurations.Any()) return new HttpResponseMessage(HttpStatusCode.NotFound);

                var notification = ctx.NotificationConfigurations.First(x => x.Id == id);

                var t = Type.GetType(notification.NotificationType);

                if (t == null) return new HttpResponseMessage(HttpStatusCode.NotFound);

                var action = (AbstractNotification)Activator.CreateInstance(t);
                var serializedNotification = action.DeSerialize(notification.ConfigurationXml);

                var response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StringContent(serializedNotification.GetConfigurationScript());
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/javascript");

                return response;
            }
        }
        public IClientConfiguration Put(ClientConfigurationDto conf)
        {
            IClientConfiguration selection;

            using (var ctx = new MailModelContainer())
            {
                selection = ctx.ImapMailBoxConfigurations.Find(conf.Id);
                ctx.Entry(selection).CurrentValues.SetValues(conf);
                ctx.SaveChanges();
            }

            InboxWatcher.MailBoxes.Remove(conf.Id);
            Task.Factory.StartNew(async () => { await InboxWatcher.ConfigureMailBox(conf); });
            return selection;
        }