mirror of
				https://github.com/Kopano-dev/kopano-ol-extension.git
				synced 2023-10-10 11:37:40 +00:00 
			
		
		
		
	[KOE-123] Added basic code to register an account on startup. Not working fully yet, just creates a store.
This commit is contained in:
		@@ -1,5 +1,7 @@
 | 
			
		||||
 | 
			
		||||
using Acacia;
 | 
			
		||||
using Acacia.Utils;
 | 
			
		||||
using Microsoft.Win32;
 | 
			
		||||
/// Copyright 2017 Kopano b.v.
 | 
			
		||||
/// 
 | 
			
		||||
/// This program is free software: you can redistribute it and/or modify
 | 
			
		||||
@@ -46,70 +48,85 @@ namespace OutlookRestarter
 | 
			
		||||
        {
 | 
			
		||||
            Logger.Instance.Debug(typeof(OutlookRestarter), "Restarting: {0}: {1}", BuildVersions.VERSION, string.Join(", ", args));
 | 
			
		||||
 | 
			
		||||
            string procPath = args[1];
 | 
			
		||||
            List<string> procArgs = args.Skip(2).ToList();
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                // Attempt waiting for the process to finish
 | 
			
		||||
                int procId = int.Parse(args[0]);
 | 
			
		||||
                Process proc = Process.GetProcessById(procId);
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Waiting for process to exit: {0}: {1}", procId, proc);
 | 
			
		||||
                bool finished = proc.WaitForExit(FINISH_WAIT_TIME);
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Waited for process to exit: {0}: {1}", procId, finished);
 | 
			
		||||
            }
 | 
			
		||||
            finally
 | 
			
		||||
            {
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Parsing arguments");
 | 
			
		||||
                List<string> useArgs = new List<string>();
 | 
			
		||||
                for (int i = 0; i < procArgs.Count; ++i)
 | 
			
		||||
                string procPath = args[1];
 | 
			
		||||
                List<string> procArgs = args.Skip(2).ToList();
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    if (procArgs[i] == "/cleankoe")
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Waiting1");
 | 
			
		||||
                    // Attempt waiting for the process to finish
 | 
			
		||||
                    int procId = int.Parse(args[0]);
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Waiting2");
 | 
			
		||||
                    Process proc = Process.GetProcessById(procId);
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Waiting3");
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Waiting for process to exit: {0}: {1}", procId, proc);
 | 
			
		||||
                    bool finished = proc.WaitForExit(FINISH_WAIT_TIME);
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Waited for process to exit: {0}: {1}", procId, finished);
 | 
			
		||||
                }
 | 
			
		||||
                finally
 | 
			
		||||
                {
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Parsing arguments");
 | 
			
		||||
                    List<string> useArgs = new List<string>();
 | 
			
		||||
                    for (int i = 0; i < procArgs.Count; ++i)
 | 
			
		||||
                    {
 | 
			
		||||
                        ++i;
 | 
			
		||||
                        string path = procArgs[i];
 | 
			
		||||
                        Logger.Instance.Debug(typeof(OutlookRestarter), "Request to remove store: {0}", path);
 | 
			
		||||
                        if (System.IO.Path.GetExtension(path) == ".ost")
 | 
			
		||||
                        if (procArgs[i] == "/cleankoe")
 | 
			
		||||
                        {
 | 
			
		||||
                            Logger.Instance.Info(typeof(OutlookRestarter), "Removing store: {0}", path);
 | 
			
		||||
 | 
			
		||||
                            for (int attempt = 0; attempt < DELETE_RETRIES; ++attempt)
 | 
			
		||||
                            {
 | 
			
		||||
                                // Delete it
 | 
			
		||||
                                try
 | 
			
		||||
                                {
 | 
			
		||||
                                    System.IO.File.Delete(path);
 | 
			
		||||
 | 
			
		||||
                                    // Success, done
 | 
			
		||||
                                    break;
 | 
			
		||||
                                }
 | 
			
		||||
                                catch (IOException e)
 | 
			
		||||
                                {
 | 
			
		||||
                                    Logger.Instance.Error(typeof(OutlookRestarter), "IOException removing store: {0}: on attempt {1}: {2}", path, attempt, e);
 | 
			
		||||
                                    // IO Exception. Wait a while and retry
 | 
			
		||||
                                    Thread.Sleep(DELETE_WAIT_TIME);
 | 
			
		||||
                                }
 | 
			
		||||
                                catch (Exception e)
 | 
			
		||||
                                {
 | 
			
		||||
                                    Logger.Instance.Error(typeof(OutlookRestarter), "Exception removing store: {0}: {1}", path, e);
 | 
			
		||||
                                    // This kind of exception will not be resolved by retrying
 | 
			
		||||
                                    break;
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                            ++i;
 | 
			
		||||
                            string path = procArgs[i];
 | 
			
		||||
                            HandleCleanKoe(path);
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            useArgs.Add(procArgs[i]);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    string argsString = string.Join(" ", useArgs);
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Parsed arguments: {0}", argsString);
 | 
			
		||||
                    // Start the process
 | 
			
		||||
                    Process process = new Process();
 | 
			
		||||
                    process.StartInfo = new ProcessStartInfo(procPath, argsString);
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Starting process: {0}", process);
 | 
			
		||||
                    process.Start();
 | 
			
		||||
                    Logger.Instance.Debug(typeof(OutlookRestarter), "Started process: {0}", process);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch(Exception e)
 | 
			
		||||
            {
 | 
			
		||||
                Logger.Instance.Fatal(typeof(OutlookRestarter), "Exception: {0}", e);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void HandleCleanKoe(string path)
 | 
			
		||||
        {
 | 
			
		||||
            Logger.Instance.Debug(typeof(OutlookRestarter), "Request to remove store: {0}", path);
 | 
			
		||||
            if (Path.GetExtension(path) == ".ost")
 | 
			
		||||
            {
 | 
			
		||||
                Logger.Instance.Info(typeof(OutlookRestarter), "Removing store: {0}", path);
 | 
			
		||||
 | 
			
		||||
                for (int attempt = 0; attempt < DELETE_RETRIES; ++attempt)
 | 
			
		||||
                {
 | 
			
		||||
                    // Delete it
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        useArgs.Add(procArgs[i]);
 | 
			
		||||
                        File.Delete(path);
 | 
			
		||||
 | 
			
		||||
                        // Success, done
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (IOException e)
 | 
			
		||||
                    {
 | 
			
		||||
                        Logger.Instance.Error(typeof(OutlookRestarter), "IOException removing store: {0}: on attempt {1}: {2}", path, attempt, e);
 | 
			
		||||
                        // IO Exception. Wait a while and retry
 | 
			
		||||
                        Thread.Sleep(DELETE_WAIT_TIME);
 | 
			
		||||
                    }
 | 
			
		||||
                    catch (Exception e)
 | 
			
		||||
                    {
 | 
			
		||||
                        Logger.Instance.Error(typeof(OutlookRestarter), "Exception removing store: {0}: {1}", path, e);
 | 
			
		||||
                        // This kind of exception will not be resolved by retrying
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                string argsString = string.Join(" ", useArgs);
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Parsed arguments: {0}", argsString);
 | 
			
		||||
                // Start the process
 | 
			
		||||
                Process process = new Process();
 | 
			
		||||
                process.StartInfo = new ProcessStartInfo(procPath, argsString);
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Starting process: {0}", process);
 | 
			
		||||
                process.Start();
 | 
			
		||||
                Logger.Instance.Debug(typeof(OutlookRestarter), "Started process: {0}", process);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user