Beispiel #1
0
        public Task<Command> SetAccountAsync(Account account, CancellationToken cancellationToken)
        {
            var accountCommand = new Command
            {
                Method = CommandMethod.Set,
                Resource = account
            };

            return Channel.ProcessCommandAsync(accountCommand, cancellationToken);
        }
Beispiel #2
0
        private static async Task StartAsClientAsync(Uri uri)
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var client = new ConsoleClient(uri);

            string identityInput = null;
            Identity identity;

            while (!Identity.TryParse(identityInput, out identity))
            {
                System.Console.Write("Enter the identity (name@domain): ");
                identityInput = System.Console.ReadLine();
            }

            System.Console.Write("Use (P)assword or (C)ertificate? ");
            var authenticationScheme = System.Console.ReadLine();

            X509Certificate2 clientCertificate = null;
            string password = null;
            bool accountExists = false;

            if (authenticationScheme.Equals("c", StringComparison.OrdinalIgnoreCase))
            {
                // Finds the certificate for the identity
                var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadOnly);

                var certificates = store.Certificates.Find(X509FindType.FindBySubjectName, identity.ToString(), true);

                if (certificates.Count > 0)
                {
                    clientCertificate = certificates[0];
                    accountExists = true;
                }
                else
                {
                    System.Console.Write(string.Format("Client certificate not found for '{0}'", identity.ToString()));
                    password = System.Console.ReadLine();
                }
            }
            else
            {
                System.Console.Write("Enter the password: "******"New (N) or Existing (E) account (Enter for E): ");
                var createAccount = System.Console.ReadLine();

                if (createAccount.ToUpper().Equals("N"))
                {
                    System.Console.WriteLine("Connecting in Guest mode...");

                    if (await client.ConnectAsGuestAsync(identity.Domain, CancellationToken.None))
                    {
                        System.Console.WriteLine("Connected. Creating the account...");

                        // Creates the account
                        var account = new Account()
                        {
                            Password = password.ToBase64()
                        };

                        var createAccountCommand = new Command()
                        {
                            From = new Node()
                            {
                                Name = identity.Name,
                                Domain = identity.Domain
                            },
                            Pp = client.Channel.LocalNode,
                            Method = CommandMethod.Set,
                            Resource = new Account()
                            {
                                Password = password.ToBase64()
                            }
                        };
                        
                        var createAccountCommandResult = await client.Channel.ProcessCommandAsync(createAccountCommand, cancellationTokenSource.Token);

                        if (createAccountCommandResult.Status == CommandStatus.Success)
                        {
                            System.Console.WriteLine("Account created. Closing the session...");

                            await client.Channel.SendFinishingSessionAsync();
                            client.Dispose();

                            client = new ConsoleClient(uri);
                            accountExists = true;
                        }
                        else
                        {
                            System.Console.WriteLine("Account creation failed: {0} - Reason: {1}", createAccountCommandResult.Status, createAccountCommandResult.Reason != null ? createAccountCommandResult.Reason.Description : "None");
                        }
                    }
                    else
                    {
                        System.Console.Write("Could not connect as a guest.");
                    }

                }
                else
                {
                    accountExists = true;
                }
            }


            System.Console.WriteLine("Connecting...");

            if (accountExists &&
                ((password != null && await client.ConnectWithPasswordAsync(identity, password, cancellationTokenSource.Token)) ||
                (clientCertificate != null && await client.ConnectWithCertificateAsync(identity, clientCertificate, cancellationTokenSource.Token))))
            {
                System.Console.WriteLine("Connected.");

                var listenMessagesTask = client.ReceiveMessagesAsync(cancellationTokenSource.Token);
                listenMessagesTask
                    .ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            System.Console.WriteLine("Exception: {0}", t.Exception);
                        }
                    });

                var listenNotificationsTask = client.ReceiveNotificationsAsync(cancellationTokenSource.Token);
                listenNotificationsTask
                    .ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            System.Console.WriteLine("Exception: {0}", t.Exception);
                        }
                    });

                System.Console.WriteLine("Setting the receipts...");

                var receiptCommandResult = await client.SetReceiptsAsync(cancellationTokenSource.Token);

                System.Console.WriteLine("Receipt result: {0} - Reason: {1}", receiptCommandResult.Status, receiptCommandResult.Reason != null ? receiptCommandResult.Reason.Description : "None");

                System.Console.WriteLine("Getting account information...");

                var accountCommandResult = await client.GetAccountAsync(cancellationTokenSource.Token);

                System.Console.WriteLine("Account result: {0} - Reason: {1}", accountCommandResult.Status, accountCommandResult.Reason != null ? accountCommandResult.Reason.Description : "None");

                System.Console.WriteLine("Getting the roster...");

                var rosterCommandResult = await client.GetContactsAsync(cancellationTokenSource.Token);

                if (rosterCommandResult.Status == CommandStatus.Success)
                {
                    System.Console.WriteLine("Contacts:");

                    var roster = rosterCommandResult.Resource as DocumentCollection;
                    foreach (Contact contact in roster.Items)
                    {
                        System.Console.WriteLine("- {0}", contact.Identity);
                    }
                }
                else
                {
                    System.Console.WriteLine("Account result: {0} - Reason: {1}", rosterCommandResult.Status, rosterCommandResult.Reason != null ? rosterCommandResult.Reason.Description : "None");
                }

                if (accountCommandResult.Status == CommandStatus.Failure &&
                    accountCommandResult.Reason.Code == ReasonCodes.COMMAND_RESOURCE_NOT_FOUND)
                {
                    System.Console.WriteLine("Setting account information...");

                    var account = new Account()
                    {
                        Email = "*****@*****.**",
                        City = "Albuquerque",
                        AllowAnonymousSender = false,
                        InboxSize = 100
                    };

                    accountCommandResult = await client.SetAccountAsync(account, cancellationTokenSource.Token);
                    System.Console.WriteLine("Account result: {0} - Reason: {1}", accountCommandResult.Status, accountCommandResult.Reason != null ? accountCommandResult.Reason.Description : "None");
                }

                System.Console.WriteLine("Setting presence...");

                var presence = new Presence()
                {
                    Status = PresenceStatus.Available,
                    Message = "I'm here!",
                    RoutingRule = RoutingRule.IdentityByDistance
                };

                var presenceCommandResult = await client.SetPresenceAsync(presence, cancellationTokenSource.Token);
                System.Console.WriteLine("Presence result: {0} - Reason: {1}", presenceCommandResult.Status, presenceCommandResult.Reason != null ? presenceCommandResult.Reason.Description : "None");

                System.Console.WriteLine("Client started. Type EXIT to quit.");

                while (true)
                {
                    System.Console.Write("Destination: ");

                    var toInput = System.Console.ReadLine();

                    if (toInput.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
                    {
                        break;
                    }
                    else if (toInput.StartsWith("add"))
                    {
                        var commandIdentity = toInput.Split(' ');

                        Identity contactIdentity;

                        if (commandIdentity.Length > 1 &&
                            Identity.TryParse(commandIdentity[1], out contactIdentity))
                        {
                            System.Console.WriteLine("Adding contact...");

                            var contact = new Contact()
                            {
                                Identity = contactIdentity
                            };

                            var contactCommandResult = await client.SetContactsAsync(contact, cancellationTokenSource.Token);

                            System.Console.WriteLine("Contact result: {0} - Reason: {1}", contactCommandResult.Status, contactCommandResult.Reason != null ? contactCommandResult.Reason.Description : "None");
                        }
                        else
                        {
                            System.Console.WriteLine("Invalid command. Try 'add [email protected]'.");
                        }
                    }
                    else
                    {
                        Node to = null;
                        if (Node.TryParse(toInput, out to))
                        {
                            System.Console.Write("Message: ");

                            var messageText = System.Console.ReadLine();
                            await client.SendTextMessageAsync(to, messageText);
                        }
                    }
                }

                await client.DisconnectAsync(CancellationToken.None);
                cancellationTokenSource.Cancel();
            }
            else
            {
                System.Console.Write("Could not connect. ");
            }
            System.Console.Write("Press any key to exit.");
            System.Console.ReadLine();
        }
Beispiel #3
0
        public async Task LoginAsync()
        {
            IClientChannel client = null;

            ITraceWriter traceWriter = null;

            if (ShowTraceWindow)
            {
                traceWriter = Owner.TraceViewModel;

                base.MessengerInstance.Send<OpenWindowMessage>(
                    new OpenWindowMessage()
                    {
                        WindowName = "Trace",
                        DataContext = Owner.TraceViewModel
                    });
            }

            IsBusy = true;
            this.ErrorMessage = string.Empty;

            try
            {
                var cancellationToken = _loginTimeout.ToCancellationToken();

                var transport = new TcpTransport(traceWriter: traceWriter);
                await transport.OpenAsync(_serverAddressUri, cancellationToken);

                client = new ClientChannel(
                    transport, 
                    _sendTimeout,
                    fillEnvelopeRecipients: true,
                    autoReplyPings: true,
                    autoNotifyReceipt: true);

                if (RegisterUser)
                {
                    var guestSessionResult = await client.EstablishSessionAsync(
                        compressionOptions => compressionOptions.First(),
                        encryptionOptions => SessionEncryption.TLS,
                        new Identity() { Name = Guid.NewGuid().ToString(), Domain = _userNameNode.Domain },
                        (schemeOptions, roundtrip) => new GuestAuthentication(),
                        null,
                        cancellationToken
                        );

                    if (guestSessionResult.State == SessionState.Established)
                    {
                        // Creates the account
                        var account = new Account()
                        {
                            Password = this.Password.ToBase64()
                        };

                        await client.SetResourceAsync<Account>(
                            LimeUri.Parse(UriTemplates.ACCOUNT),
                            account, 
                            _userNameNode, 
                            cancellationToken);

                        await client.SendFinishingSessionAsync();
                        await client.ReceiveFinishedSessionAsync(cancellationToken);

                        client.DisposeIfDisposable();

                        transport = new TcpTransport(traceWriter: traceWriter);
                        await transport.OpenAsync(_serverAddressUri, cancellationToken);
                        client = new ClientChannel(
                            transport,
                            _sendTimeout,
                            fillEnvelopeRecipients: true,
                            autoReplyPings: true,
                            autoNotifyReceipt: true);

                    }
                    else if (guestSessionResult.Reason != null)
                    {
                        this.ErrorMessage = guestSessionResult.Reason.Description;
                    }
                    else
                    {
                        this.ErrorMessage = "Could not establish a guest session with the server";
                    }
                }

                var authentication = new PlainAuthentication();
                authentication.SetToBase64Password(this.Password);

                var sessionResult = await client.EstablishSessionAsync(
                    compressionOptions => compressionOptions.First(),
                    encryptionOptions => SessionEncryption.TLS,
                    new Identity() { Name = _userNameNode.Name, Domain = _userNameNode.Domain },
                    (schemeOptions, roundtrip) => authentication,
                    _userNameNode.Instance,
                    cancellationToken);
                
                if (sessionResult.State == SessionState.Established)
                {
                    var rosterViewModel = new RosterViewModel(client, this);
                    base.Owner.ContentViewModel = rosterViewModel;
                }
                else if (sessionResult.Reason != null)
                {
                    this.ErrorMessage = sessionResult.Reason.Description;
                }
                else
                {
                    this.ErrorMessage = "Could not connect to the server";
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                client.DisposeIfDisposable();
            }
            finally
            {
                IsBusy = false;
            }            
        }