Example #1
0
        private static MailboxState GetMailboxState(MailBoxData mailbox, ILog log)
        {
            try
            {
                log.Debug("GetMailBoxState()");

                var engine = new EngineFactory(-1);
                var status = engine.MailboxEngine.GetMailboxStatus(new СoncreteUserMailboxExp(mailbox.MailBoxId, mailbox.TenantId, mailbox.UserId));

                if (mailbox.BeginDate != status.BeginDate)
                {
                    mailbox.BeginDateChanged = true;
                    mailbox.BeginDate        = status.BeginDate;

                    return(MailboxState.DateChanged);
                }

                if (status.IsRemoved)
                {
                    return(MailboxState.Deleted);
                }

                if (!status.Enabled)
                {
                    return(MailboxState.Disabled);
                }
            }
            catch (Exception exGetMbInfo)
            {
                log.InfoFormat("GetMailBoxState(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                               mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, exGetMbInfo.Message);
            }

            return(MailboxState.NoChanges);
        }
Example #2
0
        private List <MailSieveFilterData> GetFilters(EngineFactory factory, ILog log)
        {
            var user = factory.UserId;

            if (string.IsNullOrEmpty(user))
            {
                return(new List <MailSieveFilterData>());
            }

            try
            {
                if (Filters.ContainsKey(user))
                {
                    return(Filters[user]);
                }

                var filters = factory.FilterEngine.GetList();

                Filters.TryAdd(user, filters);

                return(filters);
            }
            catch (Exception ex)
            {
                log.Error("GetFilters failed", ex);
            }

            return(new List <MailSieveFilterData>());
        }
Example #3
0
        private bool IsCrmAvailable(MailBoxData mailbox, ILog log)
        {
            bool crmAvailable;

            lock (_locker)
            {
                if (_userCrmAvailabeDictionary.TryGetValue(mailbox.UserId, out crmAvailable))
                {
                    return(crmAvailable);
                }

                crmAvailable = mailbox.IsCrmAvailable(_tasksConfig.DefaultApiSchema, log);
                _userCrmAvailabeDictionary.GetOrAdd(mailbox.UserId, crmAvailable);
            }

            return(crmAvailable);
        }
Example #4
0
        private void NotifySignalrIfNeed(MailBoxData mailbox, ILog log)
        {
            if (!_tasksConfig.EnableSignalr)
            {
                log.Debug("Skip NotifySignalrIfNeed: EnableSignalr == false");

                return;
            }

            var now = DateTime.UtcNow;

            try
            {
                if (mailbox.LastSignalrNotify.HasValue &&
                    !((now - mailbox.LastSignalrNotify.Value).TotalSeconds > SIGNALR_WAIT_SECONDS))
                {
                    mailbox.LastSignalrNotifySkipped = true;

                    log.InfoFormat(
                        "Skip NotifySignalrIfNeed: last notification has occurend less then {0} seconds ago",
                        SIGNALR_WAIT_SECONDS);

                    return;
                }

                if (_signalrWorker == null)
                {
                    throw new NullReferenceException("_signalrWorker");
                }

                _signalrWorker.AddMailbox(mailbox);

                log.InfoFormat("NotifySignalrIfNeed(UserId = {0} TenantId = {1}) has been succeeded",
                               mailbox.UserId, mailbox.TenantId);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("NotifySignalrIfNeed(UserId = {0} TenantId = {1}) Exception: {2}", mailbox.UserId,
                                mailbox.TenantId, ex.ToString());
            }

            mailbox.LastSignalrNotify        = now;
            mailbox.LastSignalrNotifySkipped = false;
        }
Example #5
0
        private static void SetMailboxAuthError(MailBoxData mailbox, ILog log)
        {
            try
            {
                if (mailbox.AuthErrorDate.HasValue)
                {
                    return;
                }

                mailbox.AuthErrorDate = DateTime.UtcNow;

                var engine = new EngineFactory(mailbox.TenantId);
                engine.MailboxEngine.SetMaiboxAuthError(mailbox.MailBoxId, mailbox.AuthErrorDate.Value);
            }
            catch (Exception ex)
            {
                log.ErrorFormat(
                    "CreateTasks->SetMailboxAuthError(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message);
            }
        }
Example #6
0
        private void CloseMailClient(MailClient client, MailBoxData mailbox, ILog log)
        {
            if (client == null)
            {
                return;
            }

            try
            {
                client.Authenticated -= ClientOnAuthenticated;
                client.GetMessage    -= ClientOnGetMessage;

                client.Cancel();
                client.Dispose();
            }
            catch (Exception ex)
            {
                log.ErrorFormat(
                    "CloseMailClient(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message);
            }
        }
Example #7
0
        private void DoOptionalOperations(MailMessageData message, MimeMessage mimeMessage, MailBoxData mailbox, MailFolder folder, ILog log)
        {
            var factory = new EngineFactory(mailbox.TenantId, mailbox.UserId, log);

            var tagIds = new List <int>();

            if (folder.Tags.Any())
            {
                log.Debug("DoOptionalOperations->GetOrCreateTags()");

                tagIds = factory.TagEngine.GetOrCreateTags(mailbox.TenantId, mailbox.UserId, folder.Tags);
            }

            log.Debug("DoOptionalOperations->IsCrmAvailable()");

            if (IsCrmAvailable(mailbox, log))
            {
                log.Debug("DoOptionalOperations->GetCrmTags()");

                var crmTagIds = factory.TagEngine.GetCrmTags(message.FromEmail);

                if (crmTagIds.Any())
                {
                    if (tagIds == null)
                    {
                        tagIds = new List <int>();
                    }

                    tagIds.AddRange(crmTagIds.Select(t => t.Id));
                }
            }

            if (tagIds.Any())
            {
                if (message.TagIds == null || !message.TagIds.Any())
                {
                    message.TagIds = tagIds;
                }
                else
                {
                    message.TagIds.AddRange(tagIds);
                }

                message.TagIds = message.TagIds.Distinct().ToList();
            }

            log.Debug("DoOptionalOperations->AddMessageToIndex()");

            var wrapper = message.ToMailWrapper(mailbox.TenantId, new Guid(mailbox.UserId));

            factory.IndexEngine.Add(wrapper);

            foreach (var tagId in tagIds)
            {
                try
                {
                    log.DebugFormat("DoOptionalOperations->SetMessagesTag(tagId: {0})", tagId);

                    factory.TagEngine.SetMessagesTag(new List <int> {
                        message.Id
                    }, tagId);
                }
                catch (Exception e)
                {
                    log.ErrorFormat(
                        "SetMessagesTag(tenant={0}, userId='{1}', messageId={2}, tagid = {3}) Exception:\r\n{4}\r\n",
                        mailbox.TenantId, mailbox.UserId, message.Id, e.ToString(),
                        tagIds != null ? string.Join(",", tagIds) : "null");
                }
            }

            log.Debug("DoOptionalOperations->AddRelationshipEventForLinkedAccounts()");

            factory.CrmLinkEngine.AddRelationshipEventForLinkedAccounts(mailbox, message, _tasksConfig.DefaultApiSchema);

            log.Debug("DoOptionalOperations->SaveEmailInData()");

            factory.EmailInEngine.SaveEmailInData(mailbox, message, _tasksConfig.DefaultApiSchema);

            log.Debug("DoOptionalOperations->SendAutoreply()");

            factory.AutoreplyEngine.SendAutoreply(mailbox, message, _tasksConfig.DefaultApiSchema, log);

            log.Debug("DoOptionalOperations->UploadIcsToCalendar()");

            factory
            .CalendarEngine
            .UploadIcsToCalendar(mailbox, message.CalendarId, message.CalendarUid, message.CalendarEventIcs,
                                 message.CalendarEventCharset, message.CalendarEventMimeType, mailbox.EMail.Address,
                                 _tasksConfig.DefaultApiSchema);

            if (_tasksConfig.SaveOriginalMessage)
            {
                log.Debug("DoOptionalOperations->StoreMailEml()");
                StoreMailEml(mailbox.TenantId, mailbox.UserId, message.StreamId, mimeMessage, log);
            }

            log.Debug("DoOptionalOperations->ApplyFilters()");

            var filters = GetFilters(factory, log);

            factory.FilterEngine.ApplyFilters(message, mailbox, folder, filters);

            log.Debug("DoOptionalOperations->NotifySignalrIfNeed()");

            NotifySignalrIfNeed(mailbox, log);
        }
Example #8
0
        public AggregatorService(Options options)
        {
            ServiceName  = ASC_MAIL_COLLECTION_SERVICE_NAME;
            EventLog.Log = "Application";

            // These Flags set whether or not to handle that specific
            // type of event. Set to true if you need it, false otherwise.
            CanHandlePowerEvent         = false;
            CanHandleSessionChangeEvent = false;
            CanPauseAndContinue         = false;
            CanShutdown = true;
            CanStop     = true;
            try
            {
                _log = LogManager.GetLogger("ASC.Mail.MainThread");

                _logStat = LogManager.GetLogger("ASC.Mail.Stat");

                _tasksConfig = TasksConfig.FromConfig;

                var mailSettings = new MailQueueItemSettings();

                _tasksConfig.DefaultFolders = mailSettings.DefaultFolders;

                _tasksConfig.ImapFlags = mailSettings.ImapFlags;

                _tasksConfig.SkipImapFlags = mailSettings.SkipImapFlags;

                _tasksConfig.SpecialDomainFolders = mailSettings.SpecialDomainFolders;

                if (options.OnlyUsers != null)
                {
                    _tasksConfig.WorkOnUsersOnly.AddRange(options.OnlyUsers.ToList());
                }

                if (options.NoMessagesLimit)
                {
                    _tasksConfig.MaxMessagesPerSession = -1;
                }

                _taskSecondsLifetime =
                    TimeSpan.FromSeconds(ConfigurationManager.AppSettings["mail.task-process-lifetime-seconds"] != null
                        ? Convert.ToInt32(ConfigurationManager.AppSettings["mail.task-process-lifetime-seconds"])
                        : 300);

                _queueManager = new QueueManager(_tasksConfig, _log);

                _resetEvent = new ManualResetEvent(false);

                _cancelTokenSource = new CancellationTokenSource();

                _taskFactory = new TaskFactory();

                _tsTaskStateCheckInterval = ConfigurationManager.AppSettings["mail.task-check-state-seconds"] != null
                        ? TimeSpan.FromSeconds(
                    Convert.ToInt32(ConfigurationManager.AppSettings["mail.task-check-state-seconds"]))
                        : TimeSpan.FromSeconds(30);

                if (_tasksConfig.EnableSignalr)
                {
                    _signalrWorker = new SignalrWorker();
                }

                _workTimer = new Timer(workTimer_Elapsed, _cancelTokenSource.Token, Timeout.Infinite, Timeout.Infinite);

                Filters = new ConcurrentDictionary <string, List <MailSieveFilterData> >();

                _log.Info("Service is ready.");
            }
            catch (Exception ex)
            {
                _log.FatalFormat("CollectorService error under construct: {0}", ex.ToString());
            }
        }
Example #9
0
        private MailClient CreateMailClient(MailBoxData mailbox, ILog log, CancellationToken cancelToken)
        {
            MailClient client = null;

            var connectError = false;
            var stopClient   = false;

            Stopwatch watch = null;

            if (_tasksConfig.CollectStatistics)
            {
                watch = new Stopwatch();
                watch.Start();
            }

            try
            {
                client = new MailClient(mailbox, cancelToken, _tasksConfig.TcpTimeout,
                                        mailbox.IsTeamlab || _tasksConfig.SslCertificateErrorsPermit, _tasksConfig.ProtocolLogPath, log, true);

                log.DebugFormat("MailClient.LoginImapPop(Tenant = {0}, MailboxId = {1} Address = '{2}')",
                                mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail);

                if (!mailbox.Imap)
                {
                    client.FuncGetPop3NewMessagesIDs =
                        uidls => MessageEngine.GetPop3NewMessagesIDs(mailbox, uidls, _tasksConfig.ChunkOfPop3Uidl);
                }

                client.Authenticated += ClientOnAuthenticated;

                client.LoginImapPop();
            }
            catch (System.TimeoutException exTimeout)
            {
                log.WarnFormat(
                    "[TIMEOUT] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, exTimeout.ToString());

                connectError = true;
                stopClient   = true;
            }
            catch (OperationCanceledException)
            {
                log.InfoFormat(
                    "[CANCEL] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail);
                stopClient = true;
            }
            catch (AuthenticationException authEx)
            {
                log.ErrorFormat(
                    "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, authEx.ToString());

                connectError = true;
                stopClient   = true;
            }
            catch (WebException webEx)
            {
                log.ErrorFormat(
                    "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, webEx.ToString());

                connectError = true;
                stopClient   = true;
            }
            catch (Exception ex)
            {
                log.ErrorFormat(
                    "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail,
                    ex is ImapProtocolException || ex is Pop3ProtocolException ? ex.Message : ex.ToString());

                stopClient = true;
            }
            finally
            {
                if (connectError)
                {
                    SetMailboxAuthError(mailbox, log);
                }

                if (stopClient)
                {
                    CloseMailClient(client, mailbox, log);
                }

                if (_tasksConfig.CollectStatistics && watch != null)
                {
                    watch.Stop();

                    LogStat(CONNECT_MAILBOX, mailbox, watch.Elapsed, connectError);
                }
            }

            return(client);
        }
Example #10
0
        public string StoreMailEml(int tenant, string user, string streamId, MimeMessage message, ILog log)
        {
            if (message == null)
            {
                return(string.Empty);
            }

            // Using id_user as domain in S3 Storage - allows not to add quota to tenant.
            var savePath = MailStoragePathCombiner.GetEmlKey(user, streamId);
            var storage  = MailDataStore.GetDataStore(tenant);

            try
            {
                using (var stream = new MemoryStream())
                {
                    message.WriteTo(stream);

                    var res = storage.Save(savePath, stream, MailStoragePathCombiner.EML_FILE_NAME).ToString();

                    log.InfoFormat("StoreMailEml() tenant='{0}', user_id='{1}', save_eml_path='{2}' Result: {3}", tenant, user, savePath, res);

                    return(res);
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("StoreMailEml Exception: {0}", ex.ToString());
            }

            return(string.Empty);
        }