Example #1
0
        /// <summary>
        /// Performs a login operation on the main XMPP account, on the main XMPP account domain.
        /// </summary>
        /// <param name="UserName">User name</param>
        /// <param name="Password">Password</param>
        /// <param name="RemoteEndPoint">Remote End-Point.</param>
        /// <returns>If the login-operation was successful or not.</returns>
        public static async Task <LoginResult> DoMainXmppLogin(string UserName, string Password, string RemoteEndPoint)
        {
            if (xmppClient == null || xmppClient.UserName != UserName)
            {
                Log.Notice("Invalid login.", UserName, RemoteEndPoint, "LoginFailure", EventLevel.Minor);
                return(LoginResult.InvalidLogin);
            }

            ManualResetEvent Done  = new ManualResetEvent(false);
            ManualResetEvent Error = new ManualResetEvent(false);
            int    Result          = -1;
            string PasswordHash;
            string PasswordHashMethod;
            bool   Connected = false;

            using (XmppClient Client = new XmppClient(xmppClient.Host, xmppClient.Port, UserName, Password, "en", typeof(Gateway).Assembly))
            {
                Client.TrustServer     = xmppClient.TrustServer;
                Client.AllowCramMD5    = xmppClient.AllowCramMD5;
                Client.AllowDigestMD5  = xmppClient.AllowDigestMD5;
                Client.AllowPlain      = xmppClient.AllowPlain;
                Client.AllowScramSHA1  = xmppClient.AllowScramSHA1;
                Client.AllowEncryption = xmppClient.AllowEncryption;

                Client.OnStateChanged += (sender, NewState) =>
                {
                    switch (NewState)
                    {
                    case XmppState.StreamOpened:
                        Connected = true;
                        break;

                    case XmppState.Binding:
                        Done.Set();
                        break;

                    case XmppState.Error:
                        Error.Set();
                        break;
                    }
                };

                Client.Connect();

                await Task.Run(() => Result = WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 10000));

                PasswordHash       = Client.PasswordHash;
                PasswordHashMethod = Client.PasswordHashMethod;
            }

            if (Result != 0)
            {
                if (Connected)
                {
                    Log.Notice("Invalid login.", UserName, RemoteEndPoint, "LoginFailure", EventLevel.Minor);
                    return(LoginResult.InvalidLogin);
                }
                else
                {
                    if ((RemoteEndPoint.StartsWith("[::1]:") || RemoteEndPoint.StartsWith("127.0.0.1:")) &&
                        UserName == xmppConfiguration.Account && Password == xmppConfiguration.Password &&
                        string.IsNullOrEmpty(xmppConfiguration.PasswordType))
                    {
                        Log.Notice("Successful login. Connection to XMPP broker down. Credentials matched configuration and connection made from same machine.", UserName, RemoteEndPoint, "Login", EventLevel.Minor);
                        return(LoginResult.Successful);
                    }
                    else
                    {
                        Log.Notice("Unable to connect to XMPP broker.", UserName, RemoteEndPoint, "LoginFailure", EventLevel.Minor);
                        return(LoginResult.UnableToConnect);
                    }
                }
            }

            Log.Informational("Successful login.", UserName, RemoteEndPoint, "Login", EventLevel.Minor);

            if (xmppClient.State != XmppState.Connected &&
                (xmppClient.PasswordHash != PasswordHash || xmppClient.PasswordHashMethod != PasswordHashMethod))
            {
                Log.Notice("XMPP credentials updated.", UserName, RemoteEndPoint, "CredentialsUpdated", EventLevel.Minor);

                xmppClient.Reconnect(UserName, PasswordHash, PasswordHashMethod);

                xmppConfiguration.Account      = UserName;
                xmppConfiguration.Password     = PasswordHash;
                xmppConfiguration.PasswordType = PasswordHashMethod;

                xmppConfiguration.SaveSimpleXmppConfiguration(xmppConfigFileName);
            }

            return(LoginResult.Successful);
        }
Example #2
0
 internal static Task XmppCredentialsUpdated(string XmppConfigFileName, XmppCredentials Credentials)
 {
     SimpleXmppConfiguration.SaveSimpleXmppConfiguration(XmppConfigFileName, Credentials);
     return(Task.CompletedTask);
 }