Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPCN: Enables resend token button #11002

Merged
merged 1 commit into from Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 40 additions & 4 deletions rpcs3/Emu/NP/rpcn_client.cpp
Expand Up @@ -299,7 +299,8 @@ namespace rpcn
// Those commands are handled synchronously and won't be forwarded to NP Handler
if (command == CommandType::Login || command == CommandType::GetServerList || command == CommandType::Create ||
command == CommandType::AddFriend || command == CommandType::RemoveFriend ||
command == CommandType::AddBlock || command == CommandType::RemoveBlock || command == CommandType::SendMessage)
command == CommandType::AddBlock || command == CommandType::RemoveBlock ||
command == CommandType::SendMessage || command == CommandType::SendToken)
{
std::lock_guard lock(mutex_replies_sync);
replies_sync.insert(std::make_pair(packet_id, std::make_pair(command, std::move(data))));
Expand Down Expand Up @@ -833,6 +834,41 @@ namespace rpcn
return ErrorType::NoError;
}

ErrorType rpcn_client::resend_token(const std::string& npid, const std::string& password)
{
if (authentified)
{
// If you're already logged in why do you need a token?
return ErrorType::LoginAlreadyLoggedIn;
}

std::vector<u8> data;
std::copy(npid.begin(), npid.end(), std::back_inserter(data));
data.push_back(0);
std::copy(password.begin(), password.end(), std::back_inserter(data));
data.push_back(0);

u64 req_id = rpcn_request_counter.fetch_add(1);

std::vector<u8> packet_data;
if (!forge_send_reply(CommandType::SendToken, req_id, data, packet_data))
{
return ErrorType::Malformed;
}

vec_stream reply(packet_data);
auto error = static_cast<ErrorType>(reply.get<u8>());

if (is_error(error))
{
return error;
}

rpcn_log.success("Token has successfully been resent!");

return ErrorType::NoError;
}

bool rpcn_client::add_friend(const std::string& friend_username)
{
std::vector<u8> data;
Expand Down Expand Up @@ -1591,10 +1627,10 @@ namespace rpcn
case AlreadyJoined: rpcn_log.error("User has already joined!"); break;
case DbFail: rpcn_log.error("A db query failed on the server!"); break;
case EmailFail: rpcn_log.error("An email action failed on the server!"); break;
case NotFound: rpcn_log.error("A request replied not found!"); return false;
case NotFound: rpcn_log.error("A request replied not found!"); break;
case Blocked: rpcn_log.error("You're blocked!"); break;
case AlreadyFriend: rpcn_log.error("You're already friends!"); break;
case Unsupported: rpcn_log.error("An unsupported operation was attempted!"); return false;
case Unsupported: rpcn_log.error("An unsupported operation was attempted!"); break;
default: rpcn_log.fatal("Unhandled ErrorType reached the switch?"); break;
}

Expand All @@ -1621,7 +1657,7 @@ namespace rpcn
{
return state;
}

want_conn = true;
sem_rpcn.release();
}
Expand Down
1 change: 1 addition & 0 deletions rpcs3/Emu/NP/rpcn_client.h
Expand Up @@ -285,6 +285,7 @@ namespace rpcn
void remove_friend_cb(friend_cb_func, void* cb_param);

ErrorType create_user(const std::string& npid, const std::string& password, const std::string& online_name, const std::string& avatar_url, const std::string& email);
ErrorType resend_token(const std::string& npid, const std::string& password);
bool add_friend(const std::string& friend_username);
bool remove_friend(const std::string& friend_username);

Expand Down
66 changes: 50 additions & 16 deletions rpcs3/rpcs3qt/rpcn_settings_dialog.cpp
Expand Up @@ -110,8 +110,7 @@ rpcn_account_dialog::rpcn_account_dialog(QWidget* parent)

QPushButton* btn_create = new QPushButton(tr("Create Account"), this);
QPushButton* btn_resendtoken = new QPushButton(tr("Resend Token"), this);
btn_resendtoken->setEnabled(false);
QPushButton* btn_changepass = new QPushButton(tr("Change Password"), this);
QPushButton* btn_changepass = new QPushButton(tr("Change Password"), this);
btn_changepass->setEnabled(false);
QPushButton* btn_save = new QPushButton(tr("Save"), this);

Expand Down Expand Up @@ -171,11 +170,11 @@ rpcn_account_dialog::rpcn_account_dialog(QWidget* parent)

connect(btn_save, &QAbstractButton::clicked, this, [this]()
{
if (this->save_config())
this->close();
if (save_config())
close();
});
connect(btn_create, &QAbstractButton::clicked, this, [this]()
{ this->create_account(); });
connect(btn_create, &QAbstractButton::clicked, this, &rpcn_account_dialog::create_account);
connect(btn_resendtoken, &QAbstractButton::clicked, this, &rpcn_account_dialog::resend_token);

g_cfg_rpcn.load();

Expand Down Expand Up @@ -223,11 +222,11 @@ bool rpcn_account_dialog::save_config()
return true;
}

bool rpcn_account_dialog::create_account()
void rpcn_account_dialog::create_account()
{
// Validate and save
if (!save_config())
return false;
return;

QString email;
const QRegExpValidator simple_email_validator(QRegExp("^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"));
Expand All @@ -237,7 +236,7 @@ bool rpcn_account_dialog::create_account()
bool clicked_ok = false;
email = QInputDialog::getText(this, tr("Email address"), tr("An email address is required, please note:\n*A valid email is needed to validate your account.\n*Your email won't be used for anything beyond sending you the token.\n*Upon successful creation a token will be sent to your email which you'll need to login.\n\n"), QLineEdit::Normal, "", &clicked_ok);
if (!clicked_ok)
return false;
return;

int pos = 0;
if (email.isEmpty() || simple_email_validator.validate(email, pos) != QValidator::Acceptable)
Expand All @@ -252,7 +251,6 @@ bool rpcn_account_dialog::create_account()

const auto rpcn = rpcn::rpcn_client::get_instance();

const auto host = g_cfg_rpcn.get_host();
const auto npid = g_cfg_rpcn.get_npid();
const auto online_name = npid;
const auto avatar_url = "https://rpcs3.net/cdn/netplay/DefaultAvatar.png";
Expand All @@ -262,7 +260,7 @@ bool rpcn_account_dialog::create_account()
{
const QString error_message = tr("Failed to connect to RPCN server:\n%0").arg(rpcn_state_to_qstr(result));
QMessageBox::critical(this, tr("Error Connecting"), error_message, QMessageBox::Ok);
return false;
return;
}

if (auto error = rpcn->create_user(npid, password, online_name, avatar_url, email.toStdString()); error != rpcn::ErrorType::NoError)
Expand All @@ -277,11 +275,47 @@ bool rpcn_account_dialog::create_account()
default: error_message = tr("Unknown error"); break;
}
QMessageBox::critical(this, tr("Error Creating Account"), tr("Failed to create the account:\n%0").arg(error_message), QMessageBox::Ok);
return false;
return;
}

QMessageBox::information(this, tr("Account created!"), tr("Your account has been created successfully!\nCheck your email for your token!"), QMessageBox::Ok);
return true;
}

void rpcn_account_dialog::resend_token()
{
if (!save_config())
return;

const auto rpcn = rpcn::rpcn_client::get_instance();

const auto npid = g_cfg_rpcn.get_npid();
const auto password = g_cfg_rpcn.get_password();

if (auto result = rpcn->wait_for_connection(); result != rpcn::rpcn_state::failure_no_failure)
{
const QString error_message = tr("Failed to connect to RPCN server:\n%0").arg(rpcn_state_to_qstr(result));
QMessageBox::critical(this, tr("Error Connecting"), error_message, QMessageBox::Ok);
return;
}

if (auto error = rpcn->resend_token(npid, password); error != rpcn::ErrorType::NoError)
{
QString error_message;
switch (error)
{
case rpcn::ErrorType::Invalid: error_message = tr("The server has no email verification and doesn't need a token!"); break;
case rpcn::ErrorType::LoginAlreadyLoggedIn: error_message = tr("You can't ask for your token while authentified!"); break;
case rpcn::ErrorType::DbFail: error_message = tr("A database related error happened on the server!"); break;
case rpcn::ErrorType::TooSoon: error_message = tr("You can only ask for a token mail once every 24 hours!"); break;
case rpcn::ErrorType::EmailFail: error_message = tr("The mail couldn't be sent successfully!"); break;
case rpcn::ErrorType::LoginError: error_message = tr("The login/password pair is invalid!"); break;
default: error_message = tr("Unknown error"); break;
}
QMessageBox::critical(this, tr("Error Sending Token"), tr("Failed to send the token:\n%0").arg(error_message), QMessageBox::Ok);
return;
}

QMessageBox::information(this, tr("Token Sent!"), tr("Your token was successfully resent to the email associated with your account!"), QMessageBox::Ok);
}

rpcn_ask_password_dialog::rpcn_ask_password_dialog(QWidget* parent)
Expand Down Expand Up @@ -363,7 +397,7 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)

QGroupBox* grp_list_friends = new QGroupBox(tr("Friends"));
QVBoxLayout* vbox_lst_friends = new QVBoxLayout();
m_lst_friends = new QListWidget(this);
m_lst_friends = new QListWidget(this);
m_lst_friends->setContextMenuPolicy(Qt::CustomContextMenu);
vbox_lst_friends->addWidget(m_lst_friends);
QPushButton* btn_addfriend = new QPushButton(tr("Add Friend"));
Expand All @@ -373,7 +407,7 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)

QGroupBox* grp_list_requests = new QGroupBox(tr("Friend Requests"));
QVBoxLayout* vbox_lst_requests = new QVBoxLayout();
m_lst_requests = new QListWidget(this);
m_lst_requests = new QListWidget(this);
m_lst_requests->setContextMenuPolicy(Qt::CustomContextMenu);
vbox_lst_requests->addWidget(m_lst_requests);
QHBoxLayout* hbox_request_btns = new QHBoxLayout();
Expand All @@ -383,7 +417,7 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)

QGroupBox* grp_list_blocks = new QGroupBox(tr("Blocked Users"));
QVBoxLayout* vbox_lst_blocks = new QVBoxLayout();
m_lst_blocks = new QListWidget(this);
m_lst_blocks = new QListWidget(this);
vbox_lst_blocks->addWidget(m_lst_blocks);
grp_list_blocks->setLayout(vbox_lst_blocks);
hbox_groupboxes->addWidget(grp_list_blocks);
Expand Down
6 changes: 5 additions & 1 deletion rpcs3/rpcs3qt/rpcn_settings_dialog.h
Expand Up @@ -21,8 +21,12 @@ class rpcn_account_dialog : public QDialog
public:
rpcn_account_dialog(QWidget* parent = nullptr);

private:
bool save_config();
bool create_account();

private Q_SLOTS:
void create_account();
void resend_token();
RipleyTom marked this conversation as resolved.
Show resolved Hide resolved

protected:
QLineEdit *m_edit_host, *m_edit_npid, *m_edit_token;
Expand Down