Skip to content

Commit

Permalink
Refactor -alertnotify code
Browse files Browse the repository at this point in the history
Refactor common -alertnotify code into static CAlert::Notify method.
  • Loading branch information
gavinandresen authored and laanwj committed Oct 8, 2014
1 parent 5b9f78d commit 25b49b5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
37 changes: 21 additions & 16 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,30 @@ bool CAlert::ProcessAlert(bool fThread)
if(AppliesToMe())
{
uiInterface.NotifyAlertChanged(GetHash(), CT_NEW);
std::string strCmd = GetArg("-alertnotify", "");
if (!strCmd.empty())
{
// Alert text should be plain ascii coming from a trusted source, but to
// be safe we first strip anything not in safeChars, then add single quotes around
// the whole string before passing it to the shell:
std::string singleQuote("'");
std::string safeStatus = SanitizeString(strStatusBar);
safeStatus = singleQuote+safeStatus+singleQuote;
boost::replace_all(strCmd, "%s", safeStatus);

if (fThread)
boost::thread t(runCommand, strCmd); // thread runs free
else
runCommand(strCmd);
}
Notify(strStatusBar, fThread);
}
}

LogPrint("alert", "accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
return true;
}

void
CAlert::Notify(const std::string& strMessage, bool fThread)
{
std::string strCmd = GetArg("-alertnotify", "");
if (strCmd.empty()) return;

// Alert text should be plain ascii coming from a trusted source, but to
// be safe we first strip anything not in safeChars, then add single quotes around
// the whole string before passing it to the shell:
std::string singleQuote("'");
std::string safeStatus = SanitizeString(strMessage);
safeStatus = singleQuote+safeStatus+singleQuote;
boost::replace_all(strCmd, "%s", safeStatus);

if (fThread)
boost::thread t(runCommand, strCmd); // thread runs free
else
runCommand(strCmd);
}
1 change: 1 addition & 0 deletions src/alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class CAlert : public CUnsignedAlert
bool RelayTo(CNode* pnode) const;
bool CheckSignature() const;
bool ProcessAlert(bool fThread = true);
static void Notify(const std::string& strMessage, bool fThread);

/*
* Get copy of (active) alert object by hash. Returns a null alert if it is not found.
Expand Down
11 changes: 3 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,14 +1369,9 @@ void CheckForkWarningConditions()
{
if (!fLargeWorkForkFound)
{
std::string strCmd = GetArg("-alertnotify", "");
if (!strCmd.empty())
{
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
pindexBestForkBase->phashBlock->ToString() + std::string("'");
boost::replace_all(strCmd, "%s", warning);
boost::thread t(runCommand, strCmd); // thread runs free
}
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
pindexBestForkBase->phashBlock->ToString() + std::string("'");
CAlert::Notify(warning, true);
}
if (pindexBestForkTip)
{
Expand Down

0 comments on commit 25b49b5

Please sign in to comment.