Example #1
0
        public void ExecutePostConnectionTask()
        {
            if (File.Exists(lastMailTimestampFilename))
            {
                DateTime lastTime      = new DateTime(Convert.ToInt64(File.ReadAllText(lastMailTimestampFilename)));
                TimeSpan sinceLastTime = DateTime.Now - lastTime;
                if (sinceLastTime.TotalMinutes < 11)
                {
                    return;
                }
            }


            AzusaContext context = AzusaContext.GetInstance();

            if (!context.Ini.ContainsKey(iniCategoryName))
            {
                return;
            }

            if (context.Ini[iniCategoryName].ContainsKey("enabled"))
            {
                bool isEnabled = Convert.ToInt32(context.Ini[iniCategoryName]["enabled"]) > 0;
                if (!isEnabled)
                {
                    return;
                }
            }

            bool   allcerts = Convert.ToInt32(context.Ini[iniCategoryName]["acceptAllCerts"]) > 0;
            string server   = context.Ini[iniCategoryName]["server"];
            int    port     = context.Ini[iniCategoryName].ContainsKey(portName) ? Convert.ToUInt16(context.Ini[iniCategoryName][portName]) : 143;
            bool   useSsl   = Convert.ToInt32(context.Ini[iniCategoryName]["ssl"]) > 0;
            string username = context.Ini[iniCategoryName]["username"];
            string password = context.Ini[iniCategoryName].ContainsKey(passwordName) ? context.Ini[iniCategoryName][passwordName] : "";

            if (string.IsNullOrEmpty(password))
            {
                password = PasswordManagement.Boundary.PasswordManagement.TryGetPassword(server, username);
            }

            if (string.IsNullOrEmpty(password))
            {
                context.Splash.Invoke((MethodInvoker) delegate { password = TextInputForm.PromptPassword(String.Format("Passwort für {0} auf {1}?", username, server), context.Splash); });
                if (string.IsNullOrEmpty(password))
                {
                    return;
                }
            }


            ImapClient client = new ImapClient(new MailProtocolLogger());

            if (allcerts)
            {
                client.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
            }
            client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            client.Connect(server, port, useSsl);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate(username, password);
            FolderNamespace     rootFolderNamespace = client.PersonalNamespaces[0];
            IList <IMailFolder> folders             = client.GetFolders(rootFolderNamespace);

            foreach (IMailFolder folder in folders)
            {
                Folder child = new Folder();
                child.id       = MakeId(folder);
                child.name     = folder.Name;
                child.parentId = MakeId(folder.ParentFolder);
                bool created = FolderService.CreateIfNotExists(child);
                CopyFolder(folder, child, !created, password);
            }
            client.Disconnect(true);

            File.WriteAllText(lastMailTimestampFilename, DateTime.Now.Ticks.ToString());
        }