Esempio n. 1
0
        private void StartTimer(bool immediately = false)
        {
            if (_workTimer == null)
            {
                return;
            }

            _log.DebugFormat("Setup _workTimer to {0} seconds", _tasksConfig.CheckTimerInterval.TotalSeconds);
            if (immediately)
            {
                _workTimer.Change(0, Timeout.Infinite);
            }
            else
            {
                _workTimer.Change(_tasksConfig.CheckTimerInterval, _tasksConfig.CheckTimerInterval);
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }