void ServerThread::readyRead()
{
        if(!isInitialized)
    {
        initialize();
        return;
    }

    QDataStream input(s);
    s->waitForReadyRead();
    MessageEnvelop e;
    try
    {
        input >> e;
    }
    catch(MessageException e)
    {
        qDebug() << "Hackers are strong "
                 << "But I'm stronger";
        return;
        //sendError("Telnet Cannot kill me");
    }

    switch (e.getRequestType())
    {
    case SEND_LOGIN_TO_SERVER:
    case PING:
    case OK:
        return;
    case REQUEST_CALL_TO_CLIENT_FROM_SERVER:
        qDebug() << "Connection init";
        return requestCall(e.getName());
    case REQUEST_CLIENT_LIST_FROM_SERVER:
        return sendError("The List will be sent M'kay");
    case SEND_DENIED_RESPONSE_TO_COMMUNICATION:
        sendConnectionDenied();
        return;
    case SEND_SUCCESS_RESPONSE_TO_COMMUNICATION:
        qDebug() << "Connection Granted";
        ConnectionGranted(e.getPassword());
        return;

    case SEND_LOGOUT_REQUEST:
        logout();
        return;
    case END_OF_CALL_TO_CLIENT:
    case ERROR_SERVER_RESPONSE:
        return sendError("Only me can communicate on port 666 MUHEHEHE");
    default:
        return sendError("Unrecognized messsage type M'kay");
    }


}
void ServerThread::registerNewClient(MessageEnvelop &e)
{
    if(database->existsUser(e.getName().toStdString()))
    {
        sendError("The Username already exists M'kay, pick another one M'kay");
        emit error(this->s->error());
        return;
    }
    std::ifstream rand("/dev/urandom",std::ios::binary);
    char * newSalt = new char[8];
    rand.read(newSalt, 8);
    rand.close();
    char * corrSalt = getAscii85(newSalt, 8);
    delete[] newSalt;


    std::string s(e.getPassword().toStdString()), qCorrSalt(corrSalt);
    s = s + qCorrSalt;

    free(corrSalt);
    unsigned char hash[32];
    char *printableHash;

    sha2((unsigned char *) s.c_str(), s.length(), hash, 0);
    printableHash = getAscii85((char*) hash, 32);
    QString pass(printableHash), Qsalt(qCorrSalt.c_str());
    try
    {
        database->insertUser(e.getName().toStdString(), pass.toStdString(),
                         Qsalt.toStdString());
    }
    catch(SqlConnection::SqlException e)
    {
        sendError("The user was not added");
        emit error(this->s->error());
        return;
    }

    QByteArray b;
    QDataStream outStr(&b, QIODevice::WriteOnly);
    MessageEnvelop ret(REGISTER_APROOVED);

    isInitialized = false;
    outStr << ret;

    this->s->write(b);





}
void ServerThread::initialize()
{
    isInitialized = true;
    MessageEnvelop e;
    QDataStream str(s);
    try
    {
        str >> e;
    }
    catch(MessageException e)
    {

        return;
    }

    qDebug() << e.getRequestType() << " initialize on data " << e.getName();

    if(e.getRequestType() == REGISTER_TO_SERVER)
    {
        registerNewClient(e);
        return;
    }

    if(e.getRequestType() != SEND_LOGIN_TO_SERVER)
    {
        sendError("The Connection is bad Mkay. You should try it again M'Kay");
        emit error(s->error());
        //s->deleteLater();
        //emit disconnected();
        return;
    }
    bool result = false;
    try
    {
        result = verify(e.getName(), e.getPassword());
    }
    catch (SqlConnection::SqlException e)
    {
        sendError("The username is not in the database M'kay");
        emit error(s->error());
        return;
    }

    if(!result)
    {
        sendError("The password is bad M'kay");
        emit error(s->error());
        return;
    }

    ConnectedClient * c = new ConnectedClient(s->peerAddress(), e.getName(),
                                              s->peerPort(), this);

    qDebug() << "The host " << e.getName()
             << " on address " <<   s->peerAddress() << " on port " << s->peerPort();
    c->init(this);
    qDebug() << "opened connection to " << e.getName();
    if(!list->addClient(c))
    {
        sendError("The connection already exists M'kay");
        emit error(s->error());
    }
    this->c = c;
    this->name = e.getName();
    list->callAllClients();



}