示例#1
0
 public ExtendedMailBoxInfo(MailBox mailbox)
 {
     MailBox = mailbox;
 }
示例#2
0
        private void UploadToDocuments(Stream fileStream, string fileName, string contentType, MailBox mailbox, string httpContextScheme, ILogger log = null)
        {
            if (log == null)
            {
                log = new NullLogger();
            }

            try
            {
                var apiHelper      = new ApiHelper(httpContextScheme);
                var uploadedFileId = apiHelper.UploadToDocuments(fileStream, fileName, contentType, mailbox.EMailInFolder, true);

                log.Debug("ApiHelper.UploadToDocuments() -> uploadedFileId = {0}", uploadedFileId);
            }
            catch (ApiHelperException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound || ex.StatusCode == HttpStatusCode.Forbidden)
                {
                    log.Info("ApiHelper.UploadToDocuments() EMailIN folder '{0}' is unreachable. Try to unlink EMailIN...", mailbox.EMailInFolder);

                    SetMailboxEmailInFolder(mailbox.TenantId, mailbox.UserId, mailbox.MailBoxId, null);

                    mailbox.EMailInFolder = null;

                    CreateUploadToDocumentsFailureAlert(mailbox.TenantId, mailbox.UserId,
                                                        mailbox.MailBoxId,
                                                        (ex.StatusCode == HttpStatusCode.NotFound)
                                                            ? UploadToDocumentsErrorType
                                                        .FolderNotFound
                                                            : UploadToDocumentsErrorType
                                                        .AccessDenied);

                    throw;
                }

                log.Error("SaveEmailInData->ApiHelper.UploadToDocuments(fileName: '{0}', folderId: {1}) Exception:\r\n{2}\r\n",
                          fileName, mailbox.EMailInFolder, ex.ToString());
            }
        }
        public void SetMailboxProcessed(MailBox account, bool withError = false)
        {
            using (var db = GetDb())
            {
                Func <SqlUpdate> getBaseUpdate = () => new SqlUpdate(MailboxTable.Name)
                                                 .Where(MailboxTable.Columns.Tenant, account.TenantId)
                                                 .Where(MailboxTable.Columns.Id, account.MailBoxId)
                                                 .Set(MailboxTable.Columns.IsProcessed, false)
                                                 .Set(string.Format("{0} = UTC_TIMESTAMP()", MailboxTable.Columns.DateChecked))
                                                 .Set(string.Format("{0} = DATE_ADD(UTC_TIMESTAMP(), INTERVAL {1} SECOND)",
                                                                    MailboxTable.Columns.DateLoginDelayExpires,
                                                                    account.ServerLoginDelay));

                var updateAccountQuery = getBaseUpdate();

                if (account.AccessTokenRefreshed)
                {
                    updateAccountQuery
                    .Set(MailboxTable.Columns.OAuthToken, EncryptPassword(account.OAuthToken));
                }

                if (account.QuotaErrorChanged)
                {
                    if (account.QuotaError)
                    {
                        CreateQuotaErrorWarningAlert(account.TenantId, account.UserId, db);
                    }
                    else
                    {
                        var quotaAlerts = FindAlerts(db, account.TenantId, account.UserId, -1, AlertTypes.QuotaError);

                        if (quotaAlerts.Any())
                        {
                            DeleteAlerts(db, account.TenantId, account.UserId, quotaAlerts.Select(al => al.id).ToList());
                        }
                    }

                    updateAccountQuery
                    .Set(MailboxTable.Columns.QuotaError, account.QuotaError);
                }

                if (account.AuthErrorDate.HasValue)
                {
                    var difference = DateTime.UtcNow - account.AuthErrorDate.Value;

                    if (difference > AuthErrorDisableTimeout)
                    {
                        updateAccountQuery
                        .Set(MailboxTable.Columns.Enabled, false);

                        CreateAuthErrorDisableAlert(db, account.TenantId, account.UserId, account.MailBoxId);
                    }
                    else if (difference > AuthErrorWarningTimeout)
                    {
                        CreateAuthErrorWarningAlert(db, account.TenantId, account.UserId, account.MailBoxId);
                    }
                }
                else
                {
                    updateAccountQuery
                    .Set(MailboxTable.Columns.MsgCountLast, account.MessagesCount)
                    .Set(MailboxTable.Columns.SizeLast, account.Size);

                    if (account.Imap && account.ImapFolderChanged)
                    {
                        updateAccountQuery
                        .Where(MailboxTable.Columns.BeginDate, account.BeginDate)
                        .Set(MailboxTable.Columns.ImapIntervals, account.ImapIntervalsJson);

                        var result = db.ExecuteNonQuery(updateAccountQuery);

                        if (result == 0) // BeginDate has been changed
                        {
                            updateAccountQuery = getBaseUpdate();

                            if (account.QuotaErrorChanged)
                            {
                                updateAccountQuery
                                .Set(MailboxTable.Columns.QuotaError, account.QuotaError);
                            }

                            updateAccountQuery
                            .Set(MailboxTable.Columns.ImapIntervals, null);
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                db.ExecuteNonQuery(updateAccountQuery);
            }
        }
示例#4
0
        public void SetMailboxProcessed(MailBox account, bool withError = false)
        {
            using (var db = GetDb())
            {
                var utcTicksNow = DateTime.UtcNow;

                Func <SqlUpdate> getBaseUpdate = () => new SqlUpdate(MailboxTable.name)
                                                 .Where(MailboxTable.Columns.id_tenant, account.TenantId)
                                                 .Where(MailboxTable.Columns.id, account.MailBoxId)
                                                 .Set(MailboxTable.Columns.is_processed, false);

                var updateAccountQuery = getBaseUpdate();

                if (account.QuotaErrorChanged)
                {
                    if (account.QuotaError)
                    {
                        CreateQuotaErrorWarningAlert(db, account.TenantId, account.UserId);
                    }
                    else
                    {
                        var quotaAlerts = FindAlerts(db, account.TenantId, account.UserId, -1, AlertTypes.QuotaError);

                        if (quotaAlerts.Any())
                        {
                            DeleteAlerts(db, account.TenantId, account.UserId, quotaAlerts.Select(al => al.id).ToList());
                        }
                    }

                    updateAccountQuery
                    .Set(MailboxTable.Columns.quota_error, account.QuotaError);
                }

                if (account.AuthErrorDate.HasValue)
                {
                    updateAccountQuery
                    .Set(MailboxTable.Columns.date_login_delay_expires,
                         DateTime.UtcNow.Add(TimeSpan.FromSeconds(account.ServerLoginDelay)));

                    var difference = DateTime.UtcNow - account.AuthErrorDate.Value;

                    if (difference > AuthErrorDisableTimeout)
                    {
                        updateAccountQuery
                        .Set(MailboxTable.Columns.enabled, false);

                        CreateAuthErrorDisableAlert(db, account.TenantId, account.UserId, account.MailBoxId);
                    }
                    else if (difference > AuthErrorWarningTimeout)
                    {
                        CreateAuthErrorWarningAlert(db, account.TenantId, account.UserId, account.MailBoxId);
                    }
                }
                else
                {
                    updateAccountQuery
                    .Set(MailboxTable.Columns.msg_count_last, account.MessagesCount)
                    .Set(MailboxTable.Columns.size_last, account.Size);

                    if (account.Imap && account.ImapFolderChanged)
                    {
                        updateAccountQuery
                        .Where(MailboxTable.Columns.begin_date, account.BeginDate)
                        .Set(MailboxTable.Columns.imap_intervals, account.ImapIntervalsJson)
                        .Set(MailboxTable.Columns.date_checked, utcTicksNow);

                        var result = db.ExecuteNonQuery(updateAccountQuery);

                        if (result == 0) // BeginDate has been changed
                        {
                            updateAccountQuery = getBaseUpdate();

                            if (account.QuotaErrorChanged)
                            {
                                updateAccountQuery
                                .Set(MailboxTable.Columns.quota_error, account.QuotaError);
                            }

                            updateAccountQuery
                            .Set(MailboxTable.Columns.imap_intervals, "[]")
                            .Set(MailboxTable.Columns.date_checked, utcTicksNow);
                        }
                        else
                        {
                            return;
                        }
                    }
                }

                db.ExecuteNonQuery(updateAccountQuery);
            }
        }
示例#5
0
 // TODO: Implement new error proccessing
 public void MailboxProcessingError(MailBox account, Exception exception)
 {
     SetNextLoginDelayedFor(account, TimeSpan.FromSeconds(account.ServerLoginDelay));
 }
示例#6
0
        public void UploadIcsToCalendar(MailBox mailBox, int calendarId, string calendarEventUid, string calendarIcs,
                                        string calendarCharset, string calendarContentType, string calendarEventReceiveEmail, string httpContextScheme, ILogger log)
        {
            try
            {
                if (string.IsNullOrEmpty(calendarEventUid) ||
                    string.IsNullOrEmpty(calendarIcs) ||
                    calendarContentType != "text/calendar")
                {
                    return;
                }

                var calendar = MailUtil.ParseValidCalendar(calendarIcs, log);

                if (calendar == null)
                {
                    return;
                }

                var alienEvent = true;

                var organizer = calendar.Events[0].Organizer;

                if (organizer != null)
                {
                    var orgEmail = calendar.Events[0].Organizer.Value.ToString()
                                   .ToLowerInvariant()
                                   .Replace("mailto:", "");

                    if (orgEmail.Equals(calendarEventReceiveEmail))
                    {
                        alienEvent = false;
                    }
                }
                else
                {
                    throw new ArgumentException("calendarIcs.organizer is null");
                }

                if (alienEvent)
                {
                    if (calendar.Events[0].Attendees.Any(
                            a =>
                            a.Value.ToString()
                            .ToLowerInvariant()
                            .Replace("mailto:", "")
                            .Equals(calendarEventReceiveEmail)))
                    {
                        alienEvent = false;
                    }
                }

                if (alienEvent)
                {
                    return;
                }

                CoreContext.TenantManager.SetCurrentTenant(mailBox.TenantId);
                SecurityContext.AuthenticateMe(new Guid(mailBox.UserId));

                using (var ms = new MemoryStream(EncodingTools.GetEncodingByCodepageName(calendarCharset).GetBytes(calendarIcs)))
                {
                    var apiHelper = new ApiHelper(httpContextScheme);
                    apiHelper.UploadIcsToCalendar(calendarId, ms, "calendar.ics", calendarContentType);
                }
            }
            catch (Exception ex)
            {
                log.Error("UploadIcsToCalendar with \r\n" +
                          "calendarId: {0}\r\n" +
                          "calendarEventUid: '{1}'\r\n" +
                          "calendarIcs: '{2}'\r\n" +
                          "calendarCharset: '{3}'\r\n" +
                          "calendarContentType: '{4}'\r\n" +
                          "calendarEventReceiveEmail: '{5}'\r\n" +
                          "Exception:\r\n{6}\r\n",
                          calendarId, calendarEventUid, calendarIcs, calendarCharset, calendarContentType,
                          calendarEventReceiveEmail, ex.ToString());
            }
        }
示例#7
0
        private static SqlUpdate GetBaseUpdateAccountQueryOnMailboxProccessingComplete(MailBox account)
        {
            var utc_ticks_now = DateTime.UtcNow.Ticks;

            return(new SqlUpdate(MailboxTable.name)
                   .Where(MailboxTable.Columns.id, account.MailBoxId)
                   .Set(MailboxTable.Columns.is_processed, false)
                   .Set(MailboxTable.Columns.msg_count_last, account.MessagesCount)
                   .Set(MailboxTable.Columns.size_last, account.Size)
                   .Set(MailboxTable.Columns.time_checked, utc_ticks_now)); //Its needed for more uniform distribution in GetMailBoxForProccessing().
        }
示例#8
0
 public void OnAuthFailed(MailBox mailbox, string response_line)
 {
     SetDelayExpires(mailbox);
     mailBoxManager.SetAuthError(mailbox, true);
 }
示例#9
0
 public void OnAuthSucceed(MailBox mailbox)
 {
     SetDelayExpires(mailbox);
     mailBoxManager.SetAuthError(mailbox, false);
 }
示例#10
0
        private void SetDelayExpires(MailBox mailbox)
        {
            var expires = DateTime.UtcNow + TimeSpan.FromSeconds(mailbox.ServerLoginDelay);

            mailBoxManager.SetEmailLoginDelayExpires(mailbox.EMail.ToString(), expires);
        }
示例#11
0
 public void OnUpdateUidl(MailBox account, int message_id, string new_uidl)
 {
     mailBoxManager.UpdateMessageUidl(account.TenantId, account.UserId, message_id, new_uidl);
 }