Portability: Updated to make main re-entrant()
- New INSTALL instructions to emscripten, and AUTHORS for contribution.
This commit is contained in:
committed by
Paul Beckingham
parent
cae3f06b7d
commit
7af6db4c17
@@ -36,8 +36,6 @@
|
||||
#include <format.h>
|
||||
#include <util.h>
|
||||
|
||||
extern Context context;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
CmdSync::CmdSync ()
|
||||
{
|
||||
@@ -67,12 +65,12 @@ int CmdSync::execute (std::string& output)
|
||||
|
||||
// Loog for the 'init' keyword to indicate one-time pending.data upload.
|
||||
bool first_time_init = false;
|
||||
std::vector <std::string> words = context.cli2.getWords ();
|
||||
std::vector <std::string> words = Context::getContext ().cli2.getWords ();
|
||||
for (auto& word : words)
|
||||
{
|
||||
if (closeEnough ("initialize", word, 4))
|
||||
{
|
||||
if (!context.config.getBoolean ("confirmation") ||
|
||||
if (!Context::getContext ().config.getBoolean ("confirmation") ||
|
||||
confirm ("Please confirm that you wish to upload all your tasks to the Taskserver"))
|
||||
first_time_init = true;
|
||||
else
|
||||
@@ -81,13 +79,13 @@ int CmdSync::execute (std::string& output)
|
||||
}
|
||||
|
||||
// If no server is set up, quit.
|
||||
std::string connection = context.config.get ("taskd.server");
|
||||
std::string connection = Context::getContext ().config.get ("taskd.server");
|
||||
if (connection == "" ||
|
||||
connection.rfind (':') == std::string::npos)
|
||||
throw std::string ("Taskserver is not configured.");
|
||||
|
||||
// Obtain credentials.
|
||||
std::string credentials_string = context.config.get ("taskd.credentials");
|
||||
std::string credentials_string = Context::getContext ().config.get ("taskd.credentials");
|
||||
if (credentials_string == "")
|
||||
throw std::string ("Taskserver credentials malformed.");
|
||||
|
||||
@@ -96,7 +94,7 @@ int CmdSync::execute (std::string& output)
|
||||
throw std::string ("Taskserver credentials malformed.");
|
||||
|
||||
// This was a Boolean value in 2.3.0, and is a tri-state since 2.4.0.
|
||||
std::string trust_value = context.config.get ("taskd.trust");
|
||||
std::string trust_value = Context::getContext ().config.get ("taskd.trust");
|
||||
if (trust_value != "strict" &&
|
||||
trust_value != "ignore hostname" &&
|
||||
trust_value != "allow all")
|
||||
@@ -109,18 +107,18 @@ int CmdSync::execute (std::string& output)
|
||||
trust = TLSClient::ignore_hostname;
|
||||
|
||||
// CA must exist, if provided.
|
||||
File ca (context.config.get ("taskd.ca"));
|
||||
File ca (Context::getContext ().config.get ("taskd.ca"));
|
||||
if (ca._data != "" && ! ca.exists ())
|
||||
throw std::string ("CA certificate not found.");
|
||||
|
||||
if (trust == TLSClient::allow_all && ca._data != "")
|
||||
throw std::string ("You should either provide a CA certificate or override verification, but not both.");
|
||||
|
||||
File certificate (context.config.get ("taskd.certificate"));
|
||||
File certificate (Context::getContext ().config.get ("taskd.certificate"));
|
||||
if (! certificate.exists ())
|
||||
throw std::string ("Taskserver certificate missing.");
|
||||
|
||||
File key (context.config.get ("taskd.key"));
|
||||
File key (Context::getContext ().config.get ("taskd.key"));
|
||||
if (! key.exists ())
|
||||
throw std::string ("Taskserver key missing.");
|
||||
|
||||
@@ -132,9 +130,9 @@ int CmdSync::execute (std::string& output)
|
||||
{
|
||||
// Delete backlog.data. Because if we're uploading everything, the list of
|
||||
// deltas is meaningless.
|
||||
context.tdb2.backlog._file.truncate ();
|
||||
Context::getContext ().tdb2.backlog._file.truncate ();
|
||||
|
||||
auto all_tasks = context.tdb2.all_tasks ();
|
||||
auto all_tasks = Context::getContext ().tdb2.all_tasks ();
|
||||
for (auto& i : all_tasks)
|
||||
{
|
||||
payload += i.composeJSON () + '\n';
|
||||
@@ -143,7 +141,7 @@ int CmdSync::execute (std::string& output)
|
||||
}
|
||||
else
|
||||
{
|
||||
std::vector <std::string> lines = context.tdb2.backlog.get_lines ();
|
||||
std::vector <std::string> lines = Context::getContext ().tdb2.backlog.get_lines ();
|
||||
for (auto& i : lines)
|
||||
{
|
||||
if (i[0] == '{')
|
||||
@@ -169,7 +167,7 @@ int CmdSync::execute (std::string& output)
|
||||
|
||||
request.setPayload (payload);
|
||||
|
||||
if (context.verbose ("sync"))
|
||||
if (Context::getContext ().verbose ("sync"))
|
||||
out << format ("Syncing with {1}", connection)
|
||||
<< '\n';
|
||||
|
||||
@@ -189,10 +187,10 @@ int CmdSync::execute (std::string& output)
|
||||
{
|
||||
Color colorAdded;
|
||||
Color colorChanged;
|
||||
if (context.color ())
|
||||
if (Context::getContext ().color ())
|
||||
{
|
||||
colorAdded = Color (context.config.get ("color.sync.added"));
|
||||
colorChanged = Color (context.config.get ("color.sync.changed"));
|
||||
colorAdded = Color (Context::getContext ().config.get ("color.sync.added"));
|
||||
colorChanged = Color (Context::getContext ().config.get ("color.sync.changed"));
|
||||
}
|
||||
|
||||
int download_count = 0;
|
||||
@@ -203,7 +201,7 @@ int CmdSync::execute (std::string& output)
|
||||
// the payload, so if there are two or more lines, then we have merging
|
||||
// to perform, otherwise it's just a backlog.data update.
|
||||
if (lines.size () > 1)
|
||||
context.tdb2.all_tasks ();
|
||||
Context::getContext ().tdb2.all_tasks ();
|
||||
|
||||
std::string sync_key = "";
|
||||
for (auto& line : lines)
|
||||
@@ -217,33 +215,33 @@ int CmdSync::execute (std::string& output)
|
||||
|
||||
// Is it a new task from the server, or an update to an existing one?
|
||||
Task dummy;
|
||||
if (context.tdb2.get (uuid, dummy))
|
||||
if (Context::getContext ().tdb2.get (uuid, dummy))
|
||||
{
|
||||
if (context.verbose ("sync"))
|
||||
if (Context::getContext ().verbose ("sync"))
|
||||
out << " "
|
||||
<< colorChanged.colorize (
|
||||
format ("modify {1} '{2}'",
|
||||
uuid,
|
||||
from_server.get ("description")))
|
||||
<< '\n';
|
||||
context.tdb2.modify (from_server, false);
|
||||
Context::getContext ().tdb2.modify (from_server, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (context.verbose ("sync"))
|
||||
if (Context::getContext ().verbose ("sync"))
|
||||
out << " "
|
||||
<< colorAdded.colorize (
|
||||
format (" add {1} '{2}'",
|
||||
uuid,
|
||||
from_server.get ("description")))
|
||||
<< '\n';
|
||||
context.tdb2.add (from_server, false);
|
||||
Context::getContext ().tdb2.add (from_server, false);
|
||||
}
|
||||
}
|
||||
else if (line != "")
|
||||
{
|
||||
sync_key = line;
|
||||
context.debug ("Sync key " + sync_key);
|
||||
Context::getContext ().debug ("Sync key " + sync_key);
|
||||
}
|
||||
|
||||
// Otherwise line is blank, so ignore it.
|
||||
@@ -254,46 +252,46 @@ int CmdSync::execute (std::string& output)
|
||||
if (sync_key != "")
|
||||
{
|
||||
// Truncate backlog.data, save new sync_key.
|
||||
context.tdb2.backlog._file.truncate ();
|
||||
context.tdb2.backlog.clear_tasks ();
|
||||
context.tdb2.backlog.clear_lines ();
|
||||
context.tdb2.backlog.add_line (sync_key + '\n');
|
||||
Context::getContext ().tdb2.backlog._file.truncate ();
|
||||
Context::getContext ().tdb2.backlog.clear_tasks ();
|
||||
Context::getContext ().tdb2.backlog.clear_lines ();
|
||||
Context::getContext ().tdb2.backlog.add_line (sync_key + '\n');
|
||||
|
||||
// Present a clear status message.
|
||||
if (upload_count == 0 && download_count == 0)
|
||||
// Note: should not happen - expect code 201 instead.
|
||||
context.footnote ("Sync successful.");
|
||||
Context::getContext ().footnote ("Sync successful.");
|
||||
else if (upload_count == 0 && download_count > 0)
|
||||
context.footnote (format ("Sync successful. {1} changes downloaded.", download_count));
|
||||
Context::getContext ().footnote (format ("Sync successful. {1} changes downloaded.", download_count));
|
||||
else if (upload_count > 0 && download_count == 0)
|
||||
context.footnote (format ("Sync successful. {1} changes uploaded.", upload_count));
|
||||
Context::getContext ().footnote (format ("Sync successful. {1} changes uploaded.", upload_count));
|
||||
else if (upload_count > 0 && download_count > 0)
|
||||
context.footnote (format ("Sync successful. {1} changes uploaded, {2} changes downloaded.", upload_count, download_count));
|
||||
Context::getContext ().footnote (format ("Sync successful. {1} changes uploaded, {2} changes downloaded.", upload_count, download_count));
|
||||
}
|
||||
|
||||
status = 0;
|
||||
}
|
||||
else if (code == "201")
|
||||
{
|
||||
context.footnote ("Sync successful. No changes.");
|
||||
Context::getContext ().footnote ("Sync successful. No changes.");
|
||||
status = 0;
|
||||
}
|
||||
else if (code == "301")
|
||||
{
|
||||
std::string new_server = response.get ("info");
|
||||
context.config.set ("taskd.server", new_server);
|
||||
context.error ("The server account has been relocated. Please update your configuration using:");
|
||||
context.error (" " + format ("task config taskd.server {1}", new_server));
|
||||
Context::getContext ().config.set ("taskd.server", new_server);
|
||||
Context::getContext ().error ("The server account has been relocated. Please update your configuration using:");
|
||||
Context::getContext ().error (" " + format ("task config taskd.server {1}", new_server));
|
||||
status = 2;
|
||||
}
|
||||
else if (code == "430")
|
||||
{
|
||||
context.error ("Sync failed. Either your credentials are incorrect, or your account doesn't exist on the Taskserver.");
|
||||
Context::getContext ().error ("Sync failed. Either your credentials are incorrect, or your account doesn't exist on the Taskserver.");
|
||||
status = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.error (format ("Sync failed. The Taskserver returned error: {1} {2}",
|
||||
Context::getContext ().error (format ("Sync failed. The Taskserver returned error: {1} {2}",
|
||||
code,
|
||||
response.get ("status")));
|
||||
status = 2;
|
||||
@@ -303,10 +301,10 @@ int CmdSync::execute (std::string& output)
|
||||
std::string to_be_displayed = response.get ("messages");
|
||||
if (to_be_displayed != "")
|
||||
{
|
||||
if (context.verbose ("footnote"))
|
||||
context.footnote (to_be_displayed);
|
||||
if (Context::getContext ().verbose ("footnote"))
|
||||
Context::getContext ().footnote (to_be_displayed);
|
||||
else
|
||||
context.debug (to_be_displayed);
|
||||
Context::getContext ().debug (to_be_displayed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,11 +317,11 @@ int CmdSync::execute (std::string& output)
|
||||
// - No signal/cable
|
||||
else
|
||||
{
|
||||
context.error ("Sync failed. Could not connect to the Taskserver.");
|
||||
Context::getContext ().error ("Sync failed. Could not connect to the Taskserver.");
|
||||
status = 1;
|
||||
}
|
||||
|
||||
if (context.verbose ("sync"))
|
||||
if (Context::getContext ().verbose ("sync"))
|
||||
out << '\n';
|
||||
output = out.str ();
|
||||
|
||||
@@ -365,10 +363,10 @@ bool CmdSync::send (
|
||||
try
|
||||
{
|
||||
TLSClient client;
|
||||
client.debug (context.config.getInteger ("debug.tls"));
|
||||
client.debug (Context::getContext ().config.getInteger ("debug.tls"));
|
||||
|
||||
client.trust (trust);
|
||||
client.ciphers (context.config.get ("taskd.ciphers"));
|
||||
client.ciphers (Context::getContext ().config.get ("taskd.ciphers"));
|
||||
client.init (ca, certificate, key);
|
||||
client.connect (server, port);
|
||||
client.send (request.serialize () + '\n');
|
||||
@@ -383,7 +381,7 @@ bool CmdSync::send (
|
||||
|
||||
catch (std::string& error)
|
||||
{
|
||||
context.error (error);
|
||||
Context::getContext ().error (error);
|
||||
}
|
||||
|
||||
// Indicate message failed.
|
||||
|
||||
Reference in New Issue
Block a user