Merge branch 'jerry-sandbox2'

This commit is contained in:
Jerry (Xinyu Hou)
2015-11-23 09:54:05 -08:00
34 changed files with 560 additions and 170 deletions

View File

@@ -44,8 +44,6 @@ ArchSystemUnix::getOSName() const
msg += info.sysname;
msg += " ";
msg += info.release;
msg += " ";
msg += info.version;
return msg;
}
#endif

View File

@@ -224,6 +224,28 @@ stringToSizeType(String string)
return value;
}
std::vector<String>
splitString(String string, const char c)
{
std::vector<String> results;
size_t head = 0;
size_t separator = string.find(c);
while (separator != String::npos) {
if (head!=separator) {
results.push_back(string.substr(head, separator - head));
}
head = separator + 1;
separator = string.find(c, head);
}
if (head < string.size()) {
results.push_back(string.substr(head, string.size() - head));
}
return results;
}
//
// CaselessCmp
//

View File

@@ -22,6 +22,7 @@
#include "common/stdstring.h"
#include <stdarg.h>
#include <vector>
// use standard C++ string class for our string class
typedef std::string String;
@@ -100,6 +101,12 @@ Convert an a \c string to an size type
*/
size_t stringToSizeType(String string);
//! Split a string into substrings
/*!
Split a \c string that separated by a \c c into substrings
*/
std::vector<String> splitString(String string, const char c);
//! Case-insensitive comparisons
/*!
This class provides case-insensitve comparison functions.

View File

@@ -225,6 +225,10 @@ ArgParser::parseToolArgs(ToolArgs& args, int argc, const char* const* argv)
args.m_checkSubscription = true;
return true;
}
else if (isArg(i, argc, argv, NULL, "--notify-activation", 0)) {
args.m_notifyActivation = true;
return true;
}
else {
return false;
}

View File

@@ -22,6 +22,8 @@
struct SubscriptionKey {
String m_name;
String m_type;
String m_email;
String m_company;
int m_userLimit;
int m_warnTime;
int m_expireTime;

View File

@@ -140,24 +140,30 @@ SubscriptionManager::parsePlainSerial(const String& plainText, SubscriptionKey&
pos += 1;
}
// e.g.: {v1;trial;Bob;1;1398297600;1398384000}
if ((parts.size() == 6)
// e.g.: {v1;trial;Bob;1;email;company name;1398297600;1398384000}
if ((parts.size() == 8)
&& (parts.at(0).find("v1") != String::npos)) {
key.m_type = parts.at(1);
key.m_name = parts.at(2);
sscanf(parts.at(3).c_str(), "%d", &key.m_userLimit);
sscanf(parts.at(4).c_str(), "%d", &key.m_warnTime);
sscanf(parts.at(5).c_str(), "%d", &key.m_expireTime);
key.m_email = parts.at(4);
key.m_company = parts.at(5);
sscanf(parts.at(6).c_str(), "%d", &key.m_warnTime);
sscanf(parts.at(7).c_str(), "%d", &key.m_expireTime);
// TODO: use Arch time
if (time(0) > key.m_expireTime) {
throw XSubscription(synergy::string::sprintf(
"%s subscription has expired",
key.m_type.c_str()));
}
else if (time(0) > key.m_warnTime) {
LOG((CLOG_WARN "%s subscription will expire soon",
key.m_type.c_str()));
// only limit to trial version
if (key.m_type == "trial") {
if (time(0) > key.m_expireTime) {
throw XSubscription("trial has expired");
}
else if (time(0) > key.m_warnTime) {
int secLeft = key.m_expireTime - static_cast<int>(time(0));
const int spd = 60 * 60 * 24;
int dayLeft = secLeft / spd + 1;
LOG((CLOG_NOTE "trial will end in %d %s",
dayLeft,
dayLeft == 1 ? "day" : "days"));
}
}
const char* userText = (key.m_userLimit == 1) ? "user" : "users";

View File

@@ -37,12 +37,14 @@ public:
private:
FRIEND_TEST(SubscriptionTests, decode_invalidLength_throwException);
FRIEND_TEST(SubscriptionTests, decode_unrecognizedDigit_throwException);
FRIEND_TEST(SubscriptionTests, decode_invalidSerial_outputPlainText);
FRIEND_TEST(SubscriptionTests, decode_unrecognizedDigit_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_noParity_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_invalidSerial_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_validSerial_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_expiredSerial_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_validSerial_validSubscriptionKey);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_expiredTrialSerial_throwException);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_expiredBasicSerial_validSubscriptionKey);
FRIEND_TEST(SubscriptionTests, parsePlainSerial_validSerialWithoutCompany_validSubscriptionKey);
private:
String decode(const String& input);

View File

@@ -30,7 +30,7 @@
#include "platform/MSWindowsSession.h"
#endif
#define JSON_URL "https://synergy-project.org/premium/json/"
#define JSON_URL "http://test.synergy-project.org/premium/json/"
enum {
kErrorOk,
@@ -117,6 +117,9 @@ ToolApp::run(int argc, char** argv)
return kExitSubscription;
}
}
else if (m_args.m_notifyActivation) {
notifyActivation();
}
else {
throw XSynergy("Nothing to do");
}
@@ -149,16 +152,23 @@ ToolApp::loginAuth()
String credentials;
std::cin >> credentials;
size_t separator = credentials.find(':');
String email = credentials.substr(0, separator);
String password = credentials.substr(separator + 1, credentials.length());
std::vector<String> parts = synergy::string::splitString(credentials, ':');
size_t count = parts.size();
std::stringstream ss;
ss << JSON_URL << "auth/";
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
if (count == 2 ) {
String email = parts[0];
String password = parts[1];
std::cout << ARCH->internet().get(ss.str()) << std::endl;
std::stringstream ss;
ss << JSON_URL << "auth/";
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
}
else {
throw XSynergy("Invalid credentials.");
}
}
void
@@ -177,4 +187,49 @@ ToolApp::getPluginList()
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
}
}
void
ToolApp::notifyActivation()
{
String info;
std::cin >> info;
std::vector<String> parts = synergy::string::splitString(info, ':');
size_t count = parts.size();
if (count == 3 || count == 4) {
String action = parts[0];
String identity = parts[1];
String macHash = parts[2];
String os;
if (count == 4) {
os = parts[3];
}
else {
os = ARCH->getOSName();
}
std::stringstream ss;
ss << JSON_URL << "notify/";
ss << "?action=" << action;
ss << "&identity=" << ARCH->internet().urlEncode(identity);
ss << "&mac=" << ARCH->internet().urlEncode(macHash);
ss << "&os=" << ARCH->internet().urlEncode(ARCH->getOSName());
ss << "&arch=" << ARCH->internet().urlEncode(ARCH->getPlatformName());
try {
std::cout << ARCH->internet().get(ss.str()) << std::endl;
}
catch (std::exception& e) {
LOG((CLOG_NOTE "An error occurred during notification: %s\n", e.what()));
}
catch (...) {
LOG((CLOG_NOTE "An unknown error occurred during notification.\n"));
}
}
else {
LOG((CLOG_NOTE "notification failed"));
}
}

View File

@@ -30,6 +30,7 @@ public:
private:
void loginAuth();
void getPluginList();
void notifyActivation();
private:
ToolArgs m_args;

View File

@@ -27,6 +27,7 @@ ToolArgs::ToolArgs() :
m_getArch(false),
m_getSubscriptionFilename(false),
m_checkSubscription(false),
m_notifyActivation(false),
m_subscriptionSerial()
{
}

View File

@@ -33,5 +33,6 @@ public:
bool m_getArch;
bool m_getSubscriptionFilename;
bool m_checkSubscription;
bool m_notifyActivation;
String m_subscriptionSerial;
};