Example #1
0
        private static async Task DoCommand(string command)
        {
			// Clean the input
			command = command.Trim();
			// Don't do anything if we have empty input
			if(command == "")
				return;

			// Split and handle command
            var args = SplitCommandLine(command).ToList();

            switch (args[0].ToLower())
            {
                case "help":
                    PrintHelp();
                    break;
                case "register":
                    {
                        
                        var service = ServiceManager.GetByName(args[1]);
                        ServiceManager.Register(service);
                        ServiceManager.RegisteredServicesDatabase.SaveAllRegistered();
                        Console.WriteLine(service + " registered");
                    }
                    break;
                case "startall":
                    {
                        foreach (var service in ServiceManager.Registered)
                        {
                            var unifiedService = service as UnifiedService;
                            if (unifiedService != null)
                            {
                                ServiceManager.StartUnified(unifiedService, null);
                            }
                            else
                            {
                                await ServiceManager.Start(service, true);
                            }
                            Console.WriteLine(service + " started");
                        }
                    }
                    break;
                case "stop":
                    {
                        var service = ServiceManager.GetByName(args[1]);
                        await ServiceManager.Abort(service);
                        Console.WriteLine(service + " stopped");
                    }
                    break;
                case "start":
                    {
                        var service = ServiceManager.GetByName(args[1]);
                        await ServiceManager.Start(service, true);
                        Console.WriteLine(service + " started");
                    }
                    break;
                case "send":
                    {
                        var service = ServiceManager.GetByName(args[1]);
                        var address = args[2];
                        var message = args[3];
                        var textBubble = new TextBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Outgoing, 
                            address, null, false, service, message);
                        await BubbleManager.Send(textBubble);
                        Console.WriteLine(textBubble + " sent");
                    }
                    break;
                case "deploy-unregister":
                    {
                        var pluginName = args[1];
                        var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                        if (deployment != null)
                        {
                            Settings.PluginDeployments.Remove(deployment);
                        }
                        MutableSettingsManager.Save(Settings);
                        Console.WriteLine("Removed.");
                    }
                    break;
                case "deploy-register":
                    {
                        var pluginName = args[1];
                        var path = args[2].ToLower();
                        if (Settings.PluginDeployments != null)
                        {
                            var hasDeployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower()) != null;
                            if (hasDeployment)
                            {
                                Console.WriteLine("Plugin has already been registered in deployment system.");
                                break;
                            }
                        }
                        if (Settings.PluginDeployments == null)
                        {
                            Settings.PluginDeployments = new List<TerminalSettings.PluginDeployment>();
                        }
                        Settings.PluginDeployments.Add(new TerminalSettings.PluginDeployment
                        {
                            Name = pluginName,
                            Path = path,
                        });
                        MutableSettingsManager.Save(Settings);
                        Console.WriteLine("Plugin registered!");
                    }
                    break;
                case "deploy-clean":
                    {
                        var pluginName = args[1];
                        var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                        if (deployment != null)
                        {
                            deployment.Assemblies = null;
                            MutableSettingsManager.Save(Settings);
                            Console.WriteLine("Cleaned assemblies.");
                        }
                        else
                        {
                            Console.WriteLine("Could not find plugin deployment: " + pluginName);
                        }
                    }
                    break;
                case "deploy":
                    {
                        var pluginName = args[1];
                        var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                        var oldAssemblies = deployment.Assemblies ?? new List<TerminalSettings.PluginDeployment.Assembly>();
                        var assembliesToDeploy = new List<TerminalSettings.PluginDeployment.Assembly>();
                        var newAssemblies = new List<TerminalSettings.PluginDeployment.Assembly>();
                        var pluginManifest = Path.Combine(deployment.Path, "PluginManifest.xml");
                        if (!File.Exists(pluginManifest))
                        {
                            Console.WriteLine("A plugin manifest file is needed!");
                            break;
                        }
                        foreach (var assemblyFile in Directory.EnumerateFiles(deployment.Path, "*.dll")
                            .Concat(new [] { pluginManifest }))
                        {
                            var assemblyFileName = Path.GetFileName(assemblyFile);
                            if (PlatformManager.AndroidLinkedAssemblies.Contains(assemblyFileName))
                                continue;

                            var lastModified = File.GetLastWriteTime(assemblyFile);
                            var newAssembly = new TerminalSettings.PluginDeployment.Assembly
                            {
                                Name = assemblyFileName,
                                Modified = lastModified
                            };
                            newAssemblies.Add(newAssembly);

                            var oldAssembly = oldAssemblies.FirstOrDefault(x => x.Name == assemblyFileName);
                            if (oldAssembly == null)
                            {
                                assembliesToDeploy.Add(newAssembly);
                            }
                            else if (oldAssembly.Modified != lastModified)
                            {
                                assembliesToDeploy.Add(newAssembly);
                            }
                        }
                        deployment.Assemblies = newAssemblies;
                        MutableSettingsManager.Save(Settings);
                        var instance = AndroidController.Instance;
                        var devices = instance.ConnectedDevices;
                        Device selectedDevice;
                        if (devices.Count > 1)
                        {
                            Console.WriteLine("Please pick a device:");
                            var counter = 0;
                            foreach (var device in devices)
                            {
                                Console.WriteLine(counter++ + ") " + device);
                            }
                            Console.Write("Selection: ");
                            var selection = int.Parse(Console.ReadLine().Trim());
                            selectedDevice = instance.GetConnectedDevice(devices[selection]);
                        }
                        else
                        {
                            selectedDevice = instance.GetConnectedDevice();
                        }
                        var remotePath = "/sdcard/Disa/plugins/" + deployment.Name;
                        if (selectedDevice.FileSystem.FileOrDirectory(remotePath) == ListingType.NONE)
                        {
                            selectedDevice.FileSystem.MakeDirectory(remotePath);
                        }

                        foreach (var assemblyToDeploy in assembliesToDeploy)
                        {
                            Console.WriteLine("Transferring " + assemblyToDeploy.Name + "...");
                            var remoteAssembly = remotePath + "/" + assemblyToDeploy.Name;
                            if (selectedDevice.FileSystem.FileOrDirectory(remoteAssembly) != ListingType.NONE)
                            {
                                selectedDevice.FileSystem.Delete(remoteAssembly);

                            }
                            selectedDevice.PushFile(Path.Combine(deployment.Path, assemblyToDeploy.Name), remoteAssembly);
                        }
                        Console.WriteLine("Plugin deployed! Restarting Disa...");
                        var cmd = Adb.FormAdbShellCommand(selectedDevice, false, "am", "force-stop", "com.disa");
                        Adb.ExecuteAdbCommand(cmd);
                        Task.Delay(250).Wait();
                        cmd = Adb.FormAdbShellCommand(selectedDevice, false, "monkey", "-p com.disa", "-c android.intent.category.LAUNCHER 1");
                        Adb.ExecuteAdbCommand(cmd);
                        Console.WriteLine("Disa restarted!");
                    }
                    break;
                case "deploy-print-dependencies":
                    {
                        var pluginName = args[1];
                        var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                        foreach (var assemblyFile in Directory.EnumerateFiles(deployment.Path, "*.dll"))
                        {
                            var assemblyFileName = Path.GetFileName(assemblyFile);
                            if (PlatformManager.AndroidLinkedAssemblies.Contains(assemblyFileName))
                                continue;
                            var module = ModuleDefinition.ReadModule(assemblyFile);
                            Console.WriteLine(assemblyFileName + ": ");
                            foreach (var referenceAssembly in module.AssemblyReferences)
                            {
                                Console.WriteLine("> " + referenceAssembly.FullName);
                            }  
                        }
                    }
                    break;
                default:
                    {
                        var service = ServiceManager.GetByName(args[0]);
                        if (service != null)
                        {
                            var terminal = service as ITerminal;
                            if (terminal != null)
                            {
                                try
                                {
                                    terminal.DoCommand(args.GetRange(1, args.Count - 1).ToArray());
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Error in processing a service terminal command: " + ex);
                                }
                            }

                        }           
                    }
                    break;
            }
        }
Example #2
0
        private static async void DoCommand(string command)
        {
            // Clean the input
            command = command.Trim();
            // Don't do anything if we have empty input
            if (command == "")
            {
                return;
            }

            // Split and handle command
            var args = SplitCommandLine(command).ToList();

            switch (args[0].ToLower())
            {
            case "help":
                PrintHelp();
                break;

            case "export-conversation":
                var bubbleGroupLocation = args[1];
                var jsonExportLocation  = args[2];
                BubbleGroupFactory.OutputBubblesInJsonFormat(bubbleGroupLocation, jsonExportLocation);
                break;

            case "register":
            {
                var service = ServiceManager.GetByName(args[1]);
                ServiceManager.Register(service);
                ServiceManager.RegisteredServicesDatabase.SaveAllRegistered();
                Console.WriteLine(service + " registered");
            }
            break;

            case "startall":
            {
                foreach (var service in ServiceManager.Registered)
                {
                    var unifiedService = service as UnifiedService;
                    if (unifiedService != null)
                    {
                        ServiceManager.StartUnified(unifiedService, null);
                    }
                    else
                    {
                        await ServiceManager.Start(service, true);
                    }
                    Console.WriteLine(service + " started");
                }
            }
            break;

            case "stop":
            {
                var service = ServiceManager.GetByName(args[1]);
                await ServiceManager.Abort(service);

                Console.WriteLine(service + " stopped");
            }
            break;

            case "start":
            {
                var service = ServiceManager.GetByName(args[1]);
                await ServiceManager.Start(service, true);

                Console.WriteLine(service + " started");
            }
            break;

            case "send":
            {
                var service    = ServiceManager.GetByName(args[1]);
                var address    = args[2];
                var message    = args[3];
                var textBubble = new TextBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Outgoing,
                                                address, null, false, service, message);
                await BubbleManager.Send(textBubble);

                Console.WriteLine(textBubble + " sent");
            }
            break;

            case "deploy-unregister":
            {
                var pluginName = args[1];
                var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                if (deployment != null)
                {
                    Settings.PluginDeployments.Remove(deployment);
                }
                MutableSettingsManager.Save(Settings);
                Console.WriteLine("Removed.");
            }
            break;

            case "deploy-register":
            {
                var pluginName = args[1];
                var path       = args[2].ToLower();
                if (Settings.PluginDeployments != null)
                {
                    var hasDeployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower()) != null;
                    if (hasDeployment)
                    {
                        Console.WriteLine("Plugin has already been registered in deployment system.");
                        break;
                    }
                }
                if (Settings.PluginDeployments == null)
                {
                    Settings.PluginDeployments = new List <TerminalSettings.PluginDeployment>();
                }
                Settings.PluginDeployments.Add(new TerminalSettings.PluginDeployment
                    {
                        Name = pluginName,
                        Path = path,
                    });
                MutableSettingsManager.Save(Settings);
                Console.WriteLine("Plugin registered!");
            }
            break;

            case "deploy-clean":
            {
                var pluginName = args[1];
                var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                if (deployment != null)
                {
                    deployment.Assemblies = null;
                    MutableSettingsManager.Save(Settings);
                    Console.WriteLine("Cleaned assemblies.");
                }
                else
                {
                    Console.WriteLine("Could not find plugin deployment: " + pluginName);
                }
            }
            break;

            case "deploy":
            {
                var pluginName         = args[1];
                var deployment         = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                var oldAssemblies      = deployment.Assemblies ?? new List <TerminalSettings.PluginDeployment.Assembly>();
                var assembliesToDeploy = new List <TerminalSettings.PluginDeployment.Assembly>();
                var newAssemblies      = new List <TerminalSettings.PluginDeployment.Assembly>();
                var pluginManifest     = Path.Combine(deployment.Path, "PluginManifest.xml");
                if (!File.Exists(pluginManifest))
                {
                    Console.WriteLine("A plugin manifest file is needed!");
                    break;
                }
                foreach (var assemblyFile in Directory.EnumerateFiles(deployment.Path, "*.dll")
                         .Concat(new [] { pluginManifest }))
                {
                    var assemblyFileName = Path.GetFileName(assemblyFile);
                    if (PlatformManager.AndroidLinkedAssemblies.Contains(assemblyFileName))
                    {
                        continue;
                    }

                    var lastModified = File.GetLastWriteTime(assemblyFile);
                    var newAssembly  = new TerminalSettings.PluginDeployment.Assembly
                    {
                        Name     = assemblyFileName,
                        Modified = lastModified
                    };
                    newAssemblies.Add(newAssembly);

                    var oldAssembly = oldAssemblies.FirstOrDefault(x => x.Name == assemblyFileName);
                    if (oldAssembly == null)
                    {
                        assembliesToDeploy.Add(newAssembly);
                    }
                    else if (oldAssembly.Modified != lastModified)
                    {
                        assembliesToDeploy.Add(newAssembly);
                    }
                }
                deployment.Assemblies = newAssemblies;
                MutableSettingsManager.Save(Settings);
                var    devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);
                Device selectedDevice;
                if (devices.Count > 1)
                {
                    Console.WriteLine("Please pick a device:");
                    var counter = 0;
                    foreach (var device in devices)
                    {
                        Console.WriteLine(counter++ + ") " + device.SerialNumber);
                    }
                    Console.Write("Selection: ");
                    var selection = int.Parse(Console.ReadLine().Trim());
                    selectedDevice = devices[selection];
                }
                else
                {
                    selectedDevice = devices.First();
                }
                var remotePath = "/sdcard/Disa/plugins/" + deployment.Name;
                if (!selectedDevice.FileSystem.Exists(remotePath))
                {
                    selectedDevice.FileSystem.MakeDirectory(remotePath);
                }
                foreach (var assemblyToDeploy in assembliesToDeploy)
                {
                    Console.WriteLine("Transferring " + assemblyToDeploy.Name + "...");
                    var remoteAssembly = remotePath + "/" + assemblyToDeploy.Name;
                    if (selectedDevice.FileSystem.Exists(remoteAssembly))
                    {
                        selectedDevice.FileSystem.Delete(remoteAssembly);
                    }
                    selectedDevice.SyncService.PushFile(Path.Combine(deployment.Path, assemblyToDeploy.Name),
                                                        remoteAssembly, new SyncServiceProgressMonitor());
                }
                Console.WriteLine("Plugin deployed! Restarting Disa...");
                selectedDevice.ExecuteShellCommand("am force-stop com.disa", new ShellOutputReceiver());
                Task.Delay(250).Wait();
                selectedDevice.ExecuteShellCommand("monkey -p com.disa -c android.intent.category.LAUNCHER 1", new ShellOutputReceiver());
                Console.WriteLine("Disa restarted!");
            }
            break;

            case "deploy-print-dependencies":
            {
                var pluginName = args[1];
                var deployment = Settings.PluginDeployments.FirstOrDefault(x => x.Name.ToLower() == pluginName.ToLower());
                foreach (var assemblyFile in Directory.EnumerateFiles(deployment.Path, "*.dll"))
                {
                    var assemblyFileName = Path.GetFileName(assemblyFile);
                    if (PlatformManager.AndroidLinkedAssemblies.Contains(assemblyFileName))
                    {
                        continue;
                    }
                    var module = ModuleDefinition.ReadModule(assemblyFile);
                    Console.WriteLine(assemblyFileName + ": ");
                    foreach (var referenceAssembly in module.AssemblyReferences)
                    {
                        Console.WriteLine("> " + referenceAssembly.FullName);
                    }
                }
            }
            break;

            default:
            {
                var service = ServiceManager.GetByName(args[0]);
                if (service != null)
                {
                    var terminal = service as ITerminal;
                    if (terminal != null)
                    {
                        try
                        {
                            terminal.DoCommand(args.GetRange(1, args.Count - 1).ToArray());
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error in processing a service terminal command: " + ex);
                        }
                    }
                }
            }
            break;
            }
        }