Beispiel #1
0
        static void Main(string[] args)
        {
main:
            logger = new Logger(new ConsoleLoggerOutput());
            WriteLine("Chatovatko client at your service!");
            try {
                config        = new ConsoleClientDatabaseConfig();
                initializator = new DBInitializator(config, logger);
                initializator.DBEnsureCreated();

                settingsLoader = new SettingsLoader(config, logger);

                if (settingsLoader.Exists())
                {
                    Log("Settings exists and will be loaded.");
                    settings = settingsLoader.GetSettingsCapsula();
                }
                else
                {
                    Log("Settings doesn't exist.");
                }

                bool running = true;
                while (running)
                {
                    try
                    {
                        String   command      = ReadLine().Trim();
                        String[] commandParts = command.Split(' ');

                        switch (commandParts[0])
                        {
                        case "init":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }

                            if (settings != null)
                            {
                                WriteLine("Chatovatko is initialized already.");
                                break;
                            }

                            bool?  newUser       = null;
                            string userName      = null;
                            string serverAddress = commandParts[2];
                            switch (commandParts[1])
                            {
                            case "new":
                                newUser = true;
                                break;

                            case "login":
                                newUser = false;
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            if (newUser != null)
                            {
                                X509Certificate2 clientCert;
                                if (newUser == true)
                                {
                                    WriteLine("Your certificate is being created. Please, be patient.");
                                    clientCert = X509Certificate2Generator.GenerateCACertificate(logger);

                                    WriteLine("Your certificate has been generated. Enter path to save it: [default: ~/.chatovatko/mykey.p12]");
                                    string path = ReadLine();
                                    if (path.Equals(""))
                                    {
                                        path = $"{Utils.GetConfigDirectory()}/mykey.p12";
                                    }
                                    X509Certificate2Utils.ExportToPkcs12File(clientCert, path);
                                    WriteLine("----------------------------------------------------------------");

                                    WriteLine("Enter your new unique username:"******"Enter path to your certificate please: [default: ~/.chatovatko/mykey.p12]");
                                    string path = ReadLine();
                                    if (path.Equals(""))
                                    {
                                        path = $"{Utils.GetConfigDirectory()}/mykey.p12";
                                    }

                                    WriteLine("If you are logining to this server first time, it is nessary to enter you new unique username:"******"Do you trust this server (y/n):");

                                string pushed = ReadLine();
                                if (!pushed.Equals("y"))
                                {
                                    break;
                                }

                                IConnectionVerificator verificator = new ConnectionVerificator(logger, info.PublicKey);
                                connection = new Connection(logger, verificator, serverAddress, clientCert, config, userName);
                                connection.Connect();

                                Log("Saving settings.");
                                settingsLoader.Create(clientCert, connection.UserId, connection.UserName, info.Name, serverAddress, info.PublicKey, (int)connection.ClientId);
                                settings = settingsLoader.GetSettingsCapsula();

                                Log("Self-trustification begin.");
                                connection.TrustContact(connection.UserId);
                                Log("Self-trustification done.");

                                Log("Updating.");
                                connection.Pull();
                                connection.Push();
                                Log("Updating done.");
                            }
                            break;

                        case "connect":
                            CreateOpenedConnection(true);
                            break;

                        case "disconnect":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            connection.Disconnect();
                            break;

                        case "push":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            connection.Push();
                            break;

                        case "pull":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            connection.Pull();
                            break;

                        case "delete":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "database":
                                initializator.DBDelete();
                                WriteLine();

                                running        = false;
                                config         = null;
                                initializator  = null;
                                settingsLoader = null;
                                settings       = null;
                                connection     = null;
                                goto main;

                            case "message":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                DeleteMessage(Int32.Parse(commandParts[2]));
                                break;

                            case "thread":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                DeleteThread(Int32.Parse(commandParts[2]));
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "download":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "info":
                                WriteServerInfo(commandParts[2]);
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "ls":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "users":
                                WriteUsers();
                                break;

                            case "threads":
                                WriteThreads();
                                break;

                            case "messages":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                WriteMessages(Int32.Parse(commandParts[2]));
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "post":
                            if (commandParts.Length < 4)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "thread":
                                PostThread(Int32.Parse(commandParts[2]), BuildFromRest(commandParts, 3));
                                break;

                            case "message":
                                PostMessage(Int32.Parse(commandParts[2]), commandParts[3]);
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "rename":
                            if (commandParts.Length < 4)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "thread":
                                RenameThread(Int32.Parse(commandParts[2]), BuildFromRest(commandParts, 3));
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "trust":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            VerifyConnectionOpened(true);
                            connection.TrustContact(Int32.Parse(commandParts[1]));
                            break;

                        case "untrust":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            VerifyConnectionOpened(true);
                            connection.UntrustContact(Int32.Parse(commandParts[1]));
                            break;

                        case "generate":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "X509Certificate2":
                                X509Certificate2 cert = X509Certificate2Generator.GenerateCACertificate(logger);
                                WriteLine(X509Certificate2Utils.ExportToBase64(cert));

                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "aesTrial":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            AesTrial();
                            break;

                        case "exit":
                        case "quit":
                            running = false;
                            break;

                        case "--":
                        case "":
                        case "#":
                            break;

                        case "status":
                            WriteStatus(settings.Settings, config);
                            break;

                        default:
                            WriteSyntaxError(commandParts[0]);
                            break;
                        }
                    }
                    catch (ChatovatkoException ex)
                    {
                        logger.Log("Program", "Core", "The command has failed.", true);
                        logger.LogException(ex);
                    }
                    catch (Exception ex)
                    {
                        logger.LogException(ex, "Program", "Core", "The command has failed.");
                    }
                }

                //Config config;

                //Connection.Connect();
            }
            catch (Exception ex)
            {
                logger.Log("Program", "Core", String.Format("The client has crashed. Exception:\n{0}\n{1}", ex.Message, ex.StackTrace), true);
            }
            finally
            {
                logger.Close();
                logger = null;
            }
        }