Exemple #1
0
        static void Main(string[] args)
        {
            log.WriteDebugLog("Main() started");
            var             arguments = new UtilityArguments(args);
            string          Mailbox, User, URL, Key;
            SecureString    Password;
            bool            UseDefaultCredentials, IgnoreCertificateErrors, AllowRedirection;
            ExchangeHelper  EWSHelper  = new ExchangeHelper();
            ExchangeService EWSService = new ExchangeService();

            if (args.Length > 0)
            {
                log.WriteDebugLog("Arguments passed to executable");

                if (arguments.Help)
                {
                    DisplayHelp();
                    Environment.Exit(0);
                }

                // If using settings than load all settings
                if (arguments.UseSettings)
                {
                    MySettings Settings = new MySettings();

                    Key = Settings.Key;
                    if (Settings.User.Length > 0)
                    {
                        User = Settings.User;
                    }
                    else
                    {
                        User = "";
                    }

                    if (Settings.Password.Length > 0)
                    {
                        Password = SecureStringHelper.StringToSecureString(EncryptionHelper.DecryptString(Settings.Password, EncryptionHelper.Base64Decode(Key), 8));
                    }
                    else
                    {
                        Password = null;
                    }
                    if ((Settings.URL.StartsWith("http://")) || (Settings.URL.StartsWith("https://")))
                    {
                        URL = Settings.URL;
                    }
                    else
                    {
                        URL = "";
                    }
                    UseDefaultCredentials   = Settings.UseDefaultCredentials;
                    IgnoreCertificateErrors = Settings.IgnoreCertificateErrors;
                    AllowRedirection        = Settings.AllowRedirection;
                    Console.WriteLine("Settings loaded from settings file");
                    log.WriteDebugLog("Settings loaded from settings file");
                }
                else
                {
                    if (arguments.User.Length == 0 || arguments.Password.Length == 0)
                    {
                        UseDefaultCredentials = true;
                        log.WriteDebugLog("No user or passsword given. Using default credentials.");
                        User     = "";
                        Password = null;
                    }
                    else
                    {
                        UseDefaultCredentials = false;
                        User     = arguments.User;
                        Password = Password = SecureStringHelper.StringToSecureString(arguments.Password);
                        log.WriteDebugLog(string.Format("-user: {0}", arguments.User));
                        log.WriteDebugLog("-password: set");
                    }
                    IgnoreCertificateErrors = arguments.IgnoreCertificate;
                }

                Mailbox          = arguments.Mailbox;
                AllowRedirection = arguments.AllowRedirection;

                if (arguments.URL.Length > 0)
                {
                    URL = arguments.URL;
                }
                else
                {
                    URL = "";
                }

                if (string.IsNullOrEmpty(arguments.ImportFile) && string.IsNullOrEmpty(arguments.ExportFile))
                {
                    log.WriteErrorLog("No import or export file was given.");
                    DisplayHelp();
                    Environment.Exit(1);
                }

                if (arguments.URL.Length == 0)
                {
                    // Autodiscover
                    if (UseDefaultCredentials)
                    {
                        EWSService = EWSHelper.Service(UseDefaultCredentials, "", null, Mailbox, AllowRedirection, arguments.Impersonate, IgnoreCertificateErrors);
                    }
                    else
                    {
                        EWSService = EWSHelper.Service(UseDefaultCredentials, User, Password, Mailbox, AllowRedirection, arguments.Impersonate, IgnoreCertificateErrors);
                    }
                }
                else
                {
                    // URL
                    if (UseDefaultCredentials)
                    {
                        EWSService = EWSHelper.Service(UseDefaultCredentials, "", null, Mailbox, URL, arguments.Impersonate, IgnoreCertificateErrors);
                    }
                    else
                    {
                        EWSService = EWSHelper.Service(UseDefaultCredentials, User, Password, Mailbox, URL, arguments.Impersonate, IgnoreCertificateErrors);
                    }
                }

                if (EWSService != null)
                {
                    if (!(string.IsNullOrEmpty(arguments.ImportFile)))
                    {
                        int imported = CategoryHelper.Import(EWSService, arguments.ImportFile, arguments.ClearOnImport, arguments.Mailbox);
                        Console.WriteLine(string.Format("{0} categories imported", imported));
                        Environment.Exit(0);
                    }
                    else  // arguments.ExportFile should be non-null and not empty
                    {
                        int exported = CategoryHelper.Export(EWSService, arguments.ExportFile, arguments.Mailbox);
                        Console.WriteLine(string.Format("{0} categories exported", exported));
                        Environment.Exit(0);
                    }
                }
                else
                {
                    string errorMessage = "Error on creating the service. Check permissions and if the server is available.";
                    Console.WriteLine(errorMessage);
                    log.WriteErrorLog(errorMessage);
                    Environment.Exit(2);
                }
            }
            else
            {
                log.WriteDebugLog("Main() ended");
                var handle = GetConsoleWindow();
                ShowWindow(handle, SW_HIDE);
                StartForm();
            }
        }