Skip to content

Commit

Permalink
partially fixed the relogin problem
Browse files Browse the repository at this point in the history
  • Loading branch information
benapetr committed Jul 19, 2024
1 parent 5f621e1 commit 887bb8f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/huggle/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ int main(int argc, char *argv[])

// Event loop
ReturnCode = a.exec();
if (Huggle::Core::HuggleCore && Huggle::Core::HuggleCore->Running)
if (Huggle::Core::IsRunning())
Huggle::Core::HuggleCore->Shutdown();
delete Huggle::Core::HuggleCore;
Huggle::Exception::ExitBreakpad();
Expand Down
8 changes: 8 additions & 0 deletions src/huggle_core/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ QString Configuration::GetProjectScriptURL()
return Configuration::GetProjectURL(hcfg->Project) + Configuration::HuggleConfiguration->Project->ScriptPath;
}

QString Configuration::GetLoginName()
{
if (hcfg->SystemConfig_BotPassword)
return hcfg->SystemConfig_BotLogin;
else
return hcfg->SystemConfig_UserName;
}

void Configuration::InsertConfig(const QString &key, const QString &value, QXmlStreamWriter *s)
{
s->writeStartElement("local");
Expand Down
1 change: 1 addition & 0 deletions src/huggle_core/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ namespace Huggle
static QString GetProjectWikiURL();
//! Return a script url like http://en.wikipedia.org/w/
static QString GetProjectScriptURL();
static QString GetLoginName();
//! Helper function that will return URL of project in question
/*!
* \param Project Site
Expand Down
16 changes: 13 additions & 3 deletions src/huggle_core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using namespace Huggle;

// definitions
CoreState Core::coreState = CoreState::Initializing;
Core *Core::HuggleCore = nullptr;

void Core::Init()
Expand Down Expand Up @@ -119,6 +120,7 @@ void Core::Init()
Syslog::HuggleLogs->Log("Not loading plugins in a safe mode");
}
Syslog::HuggleLogs->Log("Loaded in " + QString::number(this->StartupTime.msecsTo(QDateTime::currentDateTime())) + "ms");
Core::coreState = CoreState::Running;
HUGGLE_PROFILER_PRINT_TIME("Core::Init()@finalize");
}

Expand All @@ -127,12 +129,13 @@ Core::Core()
this->processorThread = nullptr;
this->HuggleSyslog = nullptr;
this->StartupTime = QDateTime::currentDateTime();
this->Running = true;
Core::coreState = CoreState::Initializing;
this->gc = nullptr;
}

Core::~Core()
{
Core::coreState = CoreState::Terminated;
delete this->gc;
delete this->processorThread;
delete this->exceptionHandler;
Expand Down Expand Up @@ -320,8 +323,11 @@ void Core::VersionRead()

void Core::Shutdown()
{
if (Core::coreState != CoreState::Running)
return;

// Now we can shutdown whole huggle
this->Running = false;
Core::coreState = CoreState::ShuttingDown;
// We need to disable all extensions first
Hooks::Shutdown();
foreach (WikiSite *site, Configuration::HuggleConfiguration->Projects)
Expand Down Expand Up @@ -451,7 +457,11 @@ qint64 Core::GetUptimeInSeconds()
return this->StartupTime.secsTo(QDateTime::currentDateTime());
}

// Exception handling
CoreState Core::GetState()
{
return Core::coreState;
}

void Core::HandleException(Exception *exception)
{
Core::HuggleCore->exceptionHandler->HandleException(exception);
Expand Down
20 changes: 18 additions & 2 deletions src/huggle_core/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

namespace Huggle
{
/*!
* \brief This is a helper enum that is always statically allocated and existing that we can use to
* safely determine which application state we are currently in, to avoid segfaults by accessing
* objects that are already deleted or not constructed yet
*/
enum CoreState
{
None,
Initializing,
Running,
ShuttingDown,
Terminated
};

// Predeclaring some types
class Configuration;
class Exception;
Expand Down Expand Up @@ -51,6 +65,8 @@ namespace Huggle
class HUGGLE_EX_CORE Core
{
public:
static CoreState GetState();
static bool IsRunning() { return Core::GetState() == CoreState::Running || Core::GetState() == CoreState::Initializing; }
static void HandleException(Exception *exception);
/*!
* \brief VersionRead - read the version from embedded git file
Expand Down Expand Up @@ -89,11 +105,11 @@ namespace Huggle
//! List of extensions loaded in huggle
QList<iExtension*> Extensions;
QList<HuggleQueueFilter *> FilterDB;
//! Change this to false when you want to terminate all threads properly (you will need to wait few ms)
bool Running;
//! Garbage collector
Huggle::GC *gc;
private:
static CoreState coreState;

//! This is a post-processor for edits
WikiEdit_ProcessorThread *processorThread;
ExceptionHandler *exceptionHandler;
Expand Down
2 changes: 1 addition & 1 deletion src/huggle_core/wikiedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ QList<WikiEdit*> WikiEdit_ProcessorThread::PendingEdits;

void WikiEdit_ProcessorThread::run()
{
while(Core::HuggleCore->Running)
while(Core::IsRunning())
{
WikiEdit_ProcessorThread::EditLock.lock();
int e=0;
Expand Down
31 changes: 15 additions & 16 deletions src/huggle_ui/reloginform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include "uigeneric.hpp"
#include "ui_reloginform.h"

bool program_terminated = false;

using namespace Huggle;
ReloginForm::ReloginForm(WikiSite *site, QWidget *parent) : QDialog(parent), ui(new Ui::ReloginForm)
{
Expand All @@ -52,7 +50,6 @@ ReloginForm::~ReloginForm()

void Huggle::ReloginForm::on_pushButton_ForceExit_clicked()
{
program_terminated = true;
MainWindow::HuggleMain->ShutdownForm();
Core::HuggleCore->Shutdown();
}
Expand All @@ -69,7 +66,9 @@ void Huggle::ReloginForm::on_pushButton_Relog_clicked()
this->ui->lineEdit->setEnabled(false);
this->qReloginTokenReq = new ApiQuery(ActionLogin, this->loginSite);
this->reloginTimer->start(HUGGLE_TIMER);
this->qReloginTokenReq->Parameters = "lgname=" + QUrl::toPercentEncoding(Configuration::HuggleConfiguration->SystemConfig_UserName);
QString username = Configuration::GetLoginName();
HUGGLE_DEBUG1("Trying to login again using username " + username);
this->qReloginTokenReq->Parameters = "lgname=" + QUrl::toPercentEncoding(username);
this->qReloginTokenReq->HiddenQuery = true;
this->qReloginTokenReq->UsingPOST = true;
this->qReloginTokenReq->Process();
Expand All @@ -87,21 +86,21 @@ void ReloginForm::ReloginTick()
this->Fail(this->qReloginPw->GetFailureReason());
return;
}
ApiQueryResultNode *login_ = this->qReloginPw->GetApiQueryResult()->GetNode("login");
if (login_ == nullptr)
ApiQueryResultNode *login_result_node = this->qReloginPw->GetApiQueryResult()->GetNode("login");
if (login_result_node == nullptr)
{
this->Fail("No data returned by login query");
HUGGLE_DEBUG1(this->qReloginPw->Result->Data);
return;
}
if (!login_->Attributes.contains("result"))
if (!login_result_node->Attributes.contains("result"))
{
this->Fail("No result was provided by login query");
HUGGLE_DEBUG1(this->qReloginPw->Result->Data);
return;
}
QString Result = login_->GetAttribute("result");
if (Result == "Success")
QString result_code = login_result_node->GetAttribute("result");
if (result_code == "Success")
{
// we are logged back in
this->loginSite->ProjectConfig->IsLoggedIn = true;
Expand All @@ -112,22 +111,23 @@ void ReloginForm::ReloginTick()
this->qReloginPw.Delete();
return;
}
if (Result == "EmptyPass")
if (result_code == "EmptyPass")
{
this->Fail(_l("login-password-empty"));
return;
}
if (Result == "WrongPass")
if (result_code == "WrongPass")
{
this->Fail(_l("login-error-password"));
return;
}
if (Result == "NoName")
if (result_code == "NoName")
{
this->Fail(_l("login-fail-wrong-name"));
return;
}
this->Fail(_l("login-api", Result));
HUGGLE_DEBUG1(this->qReloginPw->Result->Data);
this->Fail(_l("login-api", result_code));
} else if (this->qReloginTokenReq != nullptr && this->qReloginTokenReq->IsProcessed())
{
if (this->qReloginTokenReq->IsFailed())
Expand Down Expand Up @@ -164,8 +164,7 @@ void ReloginForm::ReloginTick()
QString token = query_result->GetAttribute("token");
this->qReloginPw = new ApiQuery(ActionLogin, this->loginSite);
this->qReloginPw->HiddenQuery = true;
this->qReloginPw->Parameters = "lgname=" + QUrl::toPercentEncoding(Configuration::HuggleConfiguration->SystemConfig_UserName)
+ "&lgpassword=" + QUrl::toPercentEncoding(this->ui->lineEdit->text()) + "&lgtoken=" + QUrl::toPercentEncoding(token);
this->qReloginPw->Parameters = "lgname=" + QUrl::toPercentEncoding(Configuration::GetLoginName()) + "&lgpassword=" + QUrl::toPercentEncoding(this->ui->lineEdit->text()) + "&lgtoken=" + QUrl::toPercentEncoding(token);
this->qReloginPw->UsingPOST = true;
this->qReloginPw->Process();
}
Expand All @@ -189,7 +188,7 @@ void ReloginForm::Localize()

void ReloginForm::reject()
{
if (program_terminated)
if (!Core::IsRunning())
{
// At this point touching any configuration or core structures is unsafe as we performed emergency shutdown previous step
QDialog::reject();
Expand Down

0 comments on commit 887bb8f

Please sign in to comment.