Exemple #1
0
        public async Task ReadSubFolders(TreeNode rootFolder)
        {
            if (!(rootFolder.Tag is IMailFolder rootMailFolder))
            {
                return;
            }

            var serverFolders = await rootMailFolder.GetSubfoldersAsync();

            foreach (var serverFolder in serverFolders)
            {
                try
                {
                    if (!serverFolder.IsOpen)
                    {
                        await serverFolder.OpenAsync(FolderAccess.ReadWrite);
                    }
                }
                catch (Exception ex)
                {
                    NewLogMessage?.Invoke(this, $"{DateTime.Now}: ReadMails Exception: {ex.Message}.");
                }

                var nodeTree = await CreateOrUppdate(serverFolder);

                rootFolder.Nodes.Add(nodeTree);

                serverFolder.MessageFlagsChanged += ServerFolder_MessageFlagsChanged;
                serverFolder.CountChanged        += ServerFolder_CountChanged;
                serverFolder.Opened        += ServerFolder_Opened;
                serverFolder.UnreadChanged += ServerFolder_UnreadChanged;

                await ReadSubFolders(nodeTree);
            }
        }
Exemple #2
0
 private void OnClientDisconnected(object sender, DisconnectedEventArgs e)
 {
     ConnectionState(this, false);
     NewLogMessage?.Invoke(this, $"{DateTime.Now}: Disconected.");
     if (!e.IsRequested)
     {
         ReconnectAsync();
     }
 }
Exemple #3
0
 private void ServerFolder_Opened(object sender, EventArgs e)
 {
     if (sender is IMailFolder mailFolder)
     {
         NewLogMessage?.Invoke(this, $"{DateTime.Now}: {mailFolder.FullName} folder openned");
     }
     else
     {
         NewLogMessage?.Invoke(this, $"{DateTime.Now}: Uncnown folder openned");
     }
 }
Exemple #4
0
 private void ServerFolder_UnreadChanged(object sender, EventArgs e)
 {
     NewLogMessage?.Invoke(this, $"{DateTime.Now}: New Unread message.");
     if (sender is IMailFolder rootMailFolder)
     {
         UpdateForm(this, new Action(() =>
         {
             CreateOrUppdate(rootMailFolder).ContinueWith((t) => ReadMails(t.Result));
         }));
     }
 }
Exemple #5
0
        private void ServerFolder_MessageFlagsChanged(object sender, MessageFlagsChangedEventArgs e)
        {
            done?.Cancel();

            NewLogMessage?.Invoke(this, $"{DateTime.Now}: Message Number: {e.Index} flags changed.");
            if (sender is IMailFolder rootMailFolder)
            {
                UpdateForm(this, new Action(() =>
                {
                    CreateOrUppdate(rootMailFolder).ContinueWith((t) => ReadMails(t.Result));
                }));
            }
        }
Exemple #6
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            string s;

            if (formatter != null)
            {
                s = string.Format("Level:{0}\t\t{1}", logLevel, formatter(state, exception));
            }
            else
            {
                s = string.Format("Level:{0}\t\t{1}", logLevel, state?.ToString(), exception?.Message);
            }
            NewLogMessage?.Invoke(this, s);
        }
Exemple #7
0
        public async Task ReconnectAsync()
        {
            // TODO: SSL validation
            Client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

            try
            {
                await Client.ConnectAsync(host, port, options);

                await Client.AuthenticateAsync(Credentials);
            }
            catch (Exception ex)
            {
                NewLogMessage?.Invoke(this, $"{DateTime.Now}: {ex.Message}");
            }

            if (Client.Capabilities.HasFlag(ImapCapabilities.UTF8Accept))
            {
                await Client.EnableUTF8Async();
            }

            if (folders == null)
            {
                folders = new Dictionary <IMailFolder, TreeNode>();

                var folder = Client.GetFolder(Client.PersonalNamespaces[0]);

                rootNode = new TreeNode()
                {
                    Name = "root", Text = host, Tag = folder
                };

                folders.Add(folder, rootNode);

                Stopwatch stopWhatch = new Stopwatch();
                stopWhatch.Start();

                await ReadSubFolders(rootNode);

                stopWhatch.Stop();
                NewLogMessage?.Invoke(this, $"{DateTime.Now}: {host} folders readed by {stopWhatch.ElapsedMilliseconds} ms.");

                UpdateFoldersTree?.Invoke(this, rootNode);
            }
        }
Exemple #8
0
        public async Task WaitForNewMessagesAsync()
        {
            if (Client.Capabilities.HasFlag(ImapCapabilities.Idle))
            {
                NewLogMessage?.Invoke(this, $"{DateTime.Now}: Idle Mode.");
                using (done = new CancellationTokenSource(new TimeSpan(0, 9, 0)))
                    await Client.IdleAsync(done.Token, cancel.Token);
                NewLogMessage?.Invoke(this, $"{DateTime.Now}: Normal Mode.");
            }
            else
            {
                NewLogMessage?.Invoke(this, $"{DateTime.Now}: Idle Mode.");
                await Task.Delay(new TimeSpan(0, 1, 0), cancel.Token);

                await Client.NoOpAsync(cancel.Token);

                NewLogMessage?.Invoke(this, $"{DateTime.Now}: Normal Mode.");
            }
        }
Exemple #9
0
        public async Task ReadMails(TreeNode rootFolder)
        {
            if (!(rootFolder.Tag is IMailFolder rootMailFolder))
            {
                return;
            }

            Stopwatch stopWhatch = new Stopwatch();

            stopWhatch.Start();

            var mailsTask = rootMailFolder.FetchAsync(0, -1, SummaryItems);

            foreach (var message in await mailsTask)
            {
                TreeNode messageNode;

                if (!rootFolder.Nodes.ContainsKey(message.UniqueId.Id.ToString()))
                {
                    messageNode = new TreeNode {
                        Text = message.Envelope.Subject, Name = message.UniqueId.Id.ToString(), Tag = message
                    };

                    rootFolder.Nodes.Add(messageNode);
                }
                else
                {
                    messageNode = rootFolder.Nodes[message.UniqueId.Id.ToString()];
                }

                if (!message.Flags.Value.HasFlag(MessageFlags.Seen))
                {
                    messageNode.NodeFont = BoldFont;
                }
                else
                {
                    messageNode.NodeFont = RegularFont;
                }
            }

            stopWhatch.Stop();
            NewLogMessage?.Invoke(this, $"{DateTime.Now}: {rootMailFolder.Name} folders mail readed by {stopWhatch.ElapsedMilliseconds} ms.");
        }
Exemple #10
0
 /// <summary>
 /// Raises the <see cref="NewLogMessage"/> event.
 /// </summary>
 /// <param name="logMessage">the message to raise.</param>
 private void OnLog(LogMessage logMessage)
 {
     if (logMessage == null)
     {
         return;
     }
     try
     {
         using (Logger.SuppressFirstChanceExceptionLogMessages())
         {
             NewLogMessage?.Invoke(logMessage);
         }
     }
     catch (Exception)
     {
         //Swallow this exception
         //This is because if a subscriber throws an exception, creating a new log
         //might cause an infinite loop.
     }
 }
Exemple #11
0
 public void Status(string message)
 {
     NewLogMessage?.Invoke(this, new NewLogMessageEventArgs(message, LogType.Status));
 }
Exemple #12
0
 public void Error(string message)
 {
     NewLogMessage?.Invoke(this, new NewLogMessageEventArgs(message, LogType.Error));
 }
Exemple #13
0
 public void Warn(string message)
 {
     NewLogMessage?.Invoke(this, new NewLogMessageEventArgs(message, LogType.Warning));
 }
Exemple #14
0
 public void Success(string message)
 {
     NewLogMessage?.Invoke(this, new NewBuildLogMessageEventArgs(message, BuildLogType.Success));
 }
 /// <summary>
 ///     Add a new log message
 /// </summary>
 /// <param name="logLevel">The type of the message</param>
 /// <param name="message"></param>
 public static void Log(LogLevel logLevel, string message)
 {
     NewLogMessage?.Invoke(null, new NewLogMesssageEventArgs(message, logLevel));
 }
Exemple #16
0
 private void Client_MetadataChanged(object sender, MetadataChangedEventArgs e)
 {
     NewLogMessage?.Invoke(this, $"{DateTime.Now}: {e.Metadata.Value}");
 }
Exemple #17
0
 private void Client_Alert(object sender, AlertEventArgs e)
 {
     NewLogMessage?.Invoke(this, $"{DateTime.Now}: {e.Message}");
 }
Exemple #18
0
        private void Client_Connected(object sender, ConnectedEventArgs e)
        {
            NewLogMessage?.Invoke(this, $"{DateTime.Now}: Connected.");

            ConnectionState(this, true);
        }
Exemple #19
0
 public static void Log(string s)
 {
     NewLogMessage?.Invoke(@"[Debug Log] " + s);
 }
Exemple #20
0
 protected void OnNewLogMessage(object sender, LoggerEventArgs e)
 {
     NewLogMessage?.Invoke(sender, e);
 }