Пример #1
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;
            }
        }
Пример #2
0
        public Task FetchChannelBubbleGroup(Contact.ID[] contactIds, Action <BubbleGroup> result)
        {
            return(Task.Factory.StartNew(() =>
            {
                // Sanity check, we are currently only processing collections passed in with a cardinality of 1
                if (contactIds.Length != 1)
                {
                    result(null);
                    return;
                }

                foreach (var chat in _dialogs.GetAllChats())
                {
                    var name = TelegramUtils.GetChatTitle(chat);
                    var upgraded = TelegramUtils.GetChatUpgraded(chat);
                    if (upgraded)
                    {
                        continue;
                    }
                    var left = TelegramUtils.GetChatLeft(chat);
                    if (left)
                    {
                        continue;
                    }
                    var kicked = TelegramUtils.GetChatKicked(chat);
                    if (kicked)
                    {
                        continue;
                    }
                    var isChannel = chat is Channel;
                    if (isChannel)
                    {
                        var channel = chat as Channel;
                        if (channel.Broadcast == null)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    var channelAddress = TelegramUtils.GetChatId(chat);
                    if (BubbleGroupComparer(contactIds[0].Id, channelAddress))
                    {
                        var newBubble = new NewBubble(
                            time: Time.GetNowUnixTimestamp(),
                            direction: Bubble.BubbleDirection.Outgoing,
                            address: channelAddress,
                            participantAddress: null,
                            party: true,
                            service: this);
                        newBubble.ExtendedParty = true;

                        var newGroup = BubbleGroupFactory.AddNewIfNotExist(newBubble);
                        if (newGroup == null)
                        {
                            newGroup = BubbleGroupManager.FindWithAddress(this, channelAddress);
                        }

                        result(newGroup);

                        return;
                    }
                }

                // OK, we didn't get a channel locally, we must be trying to join a public channel
                using (var client = new FullClientDisposable(this))
                {
                    // A NewChannel flow will stuff away a Contact in the ContactId.Tag field
                    var telegramPartyContact = contactIds[0].Tag as TelegramPartyContact;
                    if (telegramPartyContact == null)
                    {
                        result(null);
                        return;
                    }

                    // Go for the public channel join
                    var response = TelegramUtils.RunSynchronously(client.Client.Methods.ChannelsJoinChannelAsync(new ChannelsJoinChannelArgs
                    {
                        Channel = new InputChannel
                        {
                            ChannelId = uint.Parse(contactIds[0].Id),
                            AccessHash = telegramPartyContact.AccessHash
                        }
                    }));

                    // Process the result and add in the new public channel to our local set of groups
                    var updates = response as Updates;
                    if (updates != null)
                    {
                        SendToResponseDispatcher(updates, client.Client);
                        _dialogs.AddChats(updates.Chats);
                        var chat = TelegramUtils.GetChatFromUpdate(updates);

                        var channelAddress = TelegramUtils.GetChatId(chat);

                        var newBubble = new NewBubble(
                            time: Time.GetNowUnixTimestamp(),
                            direction: Bubble.BubbleDirection.Outgoing,
                            address: channelAddress,
                            participantAddress: null,
                            party: true,
                            service: this);
                        newBubble.ExtendedParty = true;

                        var newGroup = BubbleGroupFactory.AddNew(newBubble);

                        result(newGroup);
                    }
                    else
                    {
                        result(null);
                    }
                }
            }));
        }