bool MojoCompareMatcher::Match(const MojObject& response)
{
	LOG_AM_TRACE("Entering function %s", __FUNCTION__);

	if (response.contains(m_key)) {
		MojObject value;
		MojString valueString;
		response.get(m_key, value);
		value.stringValue(valueString);
		if (value != m_value) {
			MojString oldValueString;
			m_value.stringValue(oldValueString);

			LOG_AM_DEBUG("Compare Matcher: Comparison key \"%s\" value changed from \"%s\" to \"%s\".  Firing.", 
				m_key.data(),
				oldValueString.data(), valueString.data());

			return true;
		} else {
			LOG_AM_DEBUG("Compare Matcher: Comparison key \"%s\" value \"%s\" unchanged.",
				m_key.data(), valueString.data());
		}
	} else {
		LOG_AM_DEBUG("Compare Matcher: Comparison key (%s) not present.",
			m_key.data());
	}

	return false;
}
Example #2
0
MojErr MojObjectTest::emptyTest(MojObject& obj)
{
    MojObject obj2;
    MojString str1;
    MojString str2;

    bool found = false;

    MojTestAssert(obj.size() == 0);
    MojTestAssert(obj.empty());
    MojTestAssert(obj.boolValue() == false);
    MojTestAssert(obj.intValue() == 0);
    MojTestAssert(obj.decimalValue() == MojDecimal());

    MojTestAssert(obj.begin() == obj.end());
    MojTestAssert(!obj.contains(_T("hello")));
    MojTestAssert(!obj.get(_T("hi"), obj2));
    MojErr err = obj.del(_T("hello"), found);
    MojTestErrCheck(err);

    obj2.assign(obj);
    MojTestAssert(obj2.type() == obj.type());
    MojTestAssert(obj2 == obj);
    MojTestAssert(!(obj2 != obj));

    err = obj.stringValue(str1);
    MojTestErrCheck(err);
    err = obj2.stringValue(str2);
    MojTestErrCheck(err);
    MojTestAssert(str1 == str2);

    return MojErrNone;
}
MojObject ImapAccountAdapter::GetFolderId(const MojObject& obj, const char* prop)
{
	MojObject folderId(MojObject::Undefined);
	if(obj.contains(prop)) {
		MojErr err = obj.getRequired(prop, folderId);
		ErrorToException(err);
	}
	return folderId;
}
MojErr MojDbServiceHandler::handleMerge(MojServiceMessage* msg, MojObject& payload, MojDbReq& req)
{
	MojAssert(msg);
	MojLogTrace(s_log);

	MojErr err = MojErrNone;
	MojUInt32 count = 0;
	MojObject obj;
	

	if (payload.get(MojDbServiceDefs::ObjectsKey, obj)) {
		if (payload.contains(MojDbServiceDefs::QueryKey))
			MojErrThrowMsg(MojErrInvalidArg, _T("db: cannot have both an objects param and a query param"));
		MojObject::ArrayIterator begin;
		bool ignoreM = false;
		payload.get(MojDbServiceDefs::IgnoreMissingKey, ignoreM);
		MojUInt32 mflags = MojDb::FlagNone;
		if (ignoreM)
			mflags = mflags | MojDb::FlagIgnoreMissing;
		err = obj.arrayBegin(begin);
		MojErrCheck(err);
		MojObject::ConstArrayIterator end = obj.arrayEnd();
		err = m_db.merge(begin, end, mflags, req);
		MojErrCheck(err);
		err = formatPut(msg, begin, end); // TO DO: we need to drop non-existing objects
		MojErrCheck(err);
	}
	else if (payload.get(MojDbServiceDefs::QueryKey, obj)) {
		MojObject props;
		err = payload.getRequired(MojDbServiceDefs::PropsKey, props);
		MojErrCheck(err);
		bool ignoreM = false;
		if (payload.get(MojDbServiceDefs::IgnoreMissingKey, ignoreM))
			MojErrThrowMsg(MojErrInvalidArg, _T("db: ignoreMissing - invalid option for merge query"));

		MojDbQuery query;
		err = query.fromObject(obj);
		MojErrCheck(err);
		MojUInt32 queryCount = 0;
		err = m_db.merge(query, props, queryCount, MojDb::FlagNone, req);
		MojErrCheck(err);
		count += queryCount;
		err = formatCount(msg, count);
		MojErrCheck(err);
	}
	else {
		MojErrThrowMsg(MojErrInvalidArg, _T("db: either objects or query param required for merge"));
	}

	return MojErrNone;
}
Example #5
0
EmailPartPtr EmailAdapter::ParseEmailPart(const MojObject& partObj)
{
	MojErr err;

	MojString type;
	err = partObj.getRequired(Part::TYPE, type);
	ErrorToException(err);

	EmailPartPtr emailPart = boost::make_shared<EmailPart>( GetPartType(type) );

	MojObject id;
	err = partObj.getRequired(DatabaseAdapter::ID, id);
	ErrorToException(err);
	emailPart->SetId(id);

	emailPart->SetLocalFilePath( DatabaseAdapter::GetOptionalString(partObj, Part::PATH) );
	emailPart->SetDisplayName( DatabaseAdapter::GetOptionalString(partObj, Part::NAME) );
	emailPart->SetContentId( DatabaseAdapter::GetOptionalString(partObj, Part::CONTENT_ID) );
	emailPart->SetMimeType( DatabaseAdapter::GetOptionalString(partObj, Part::MIME_TYPE) );
	emailPart->SetEncoding( DatabaseAdapter::GetOptionalString(partObj, Part::ENCODING) );
	emailPart->SetCharset( DatabaseAdapter::GetOptionalString(partObj, Part::CHARSET) );
	emailPart->SetSection( DatabaseAdapter::GetOptionalString(partObj, Part::SECTION) );

	if(partObj.contains(Part::ESTIMATED_SIZE)) {
		MojInt64 estimatedSize = -1;
		err = partObj.getRequired(Part::ESTIMATED_SIZE, estimatedSize);
		emailPart->SetEstimatedSize(estimatedSize);
	}

	if(partObj.contains(Part::ENCODED_SIZE)) {
		MojInt64 encodedSize = -1;
		err = partObj.getRequired(Part::ENCODED_SIZE, encodedSize);
		emailPart->SetEncodedSize(encodedSize);
	}

	return emailPart;
}
bool MojoKeyMatcher::Match(const MojObject& response)
{
	MojLogTrace(s_log);

	/* If those were the droids we were looking for, fire! */
	if (response.contains(m_key)) {
		MojLogInfo(s_log, _T("Key Matcher: Key \"%s\" found in response %s"),
			m_key.data(), MojoObjectJson(response).c_str());
		return true;
	} else {
		MojLogInfo(s_log, _T("Key Matcher: Key \"%s\" not found in response"
			"%s"), m_key.data(), MojoObjectJson(response).c_str());
		return false;
	}
}
bool MojoKeyMatcher::Match(const MojObject& response)
{
	LOG_AM_TRACE("Entering function %s", __FUNCTION__);

	/* If those were the droids we were looking for, fire! */
	if (response.contains(m_key)) {
		LOG_AM_DEBUG("Key Matcher: Key \"%s\" found in response %s",
			m_key.data(), MojoObjectJson(response).c_str());
		return true;
	} else {
		LOG_AM_DEBUG("Key Matcher: Key \"%s\" not found in response %s", 
			m_key.data(), MojoObjectJson(response).c_str());
		return false;
	}
}
MojErr MojDbServiceHandler::handleDel(MojServiceMessage* msg, MojObject& payload, MojDbReq& req)
{
	MojAssert(msg);
	MojLogTrace(s_log);

	MojErr err = MojErrNone;
	MojUInt32 count = 0;
	MojUInt32 flags = MojDb::FlagNone;

	bool purge = false;
	if (payload.get(MojDbServiceDefs::PurgeKey, purge) && purge) {
		flags |= MojDb::FlagPurge;
	}

	MojObject obj;
	if (payload.get(MojDbServiceDefs::IdsKey, obj)) {
		if (payload.contains(MojDbServiceDefs::QueryKey))
			MojErrThrowMsg(MojErrInvalidArg, _T("db: cannot have both an objects argument and a query argument"));
		MojObject deletedObjects;
		err = m_db.del(obj.arrayBegin(), obj.arrayEnd(), count, deletedObjects, flags, req);
		MojErrCheck(err);
		err = formatPut(msg, deletedObjects.arrayBegin(), deletedObjects.arrayEnd());
		MojErrCheck(err);
	}
	else if (payload.get(MojDbServiceDefs::QueryKey, obj)) {
		MojDbQuery query;
		err = query.fromObject(obj);
		MojErrCheck(err);
		MojUInt32 queryCount = 0;
		err = m_db.del(query, queryCount, flags, req);
		MojErrCheck(err);
		count += queryCount;
		err = formatCount(msg, count);
		MojErrCheck(err);
	}
	return MojErrNone;
}
void MojoNewWhereMatcher::ValidateClause(const MojObject& clause) const
{
	LOG_AM_TRACE("Entering function %s", __FUNCTION__);
	LOG_AM_DEBUG("Validating where clause \"%s\"",
		MojoObjectJson(clause).c_str());

	bool found = false;

	if (clause.contains(_T("and"))) {
		found = true;

		MojObject andClauses;
		clause.get(_T("and"), andClauses);
		ValidateClauses(andClauses);
	}

	if (clause.contains(_T("or"))) {
		if (found) {
			throw std::runtime_error("Only one of \"and\", \"or\", or a valid "
				"clause including \"prop\", \"op\", and a \"val\"ue to "
				"compare against must be present in a clause");
		}

		found = true;

		MojObject orClauses;
		clause.get(_T("or"), orClauses);
		ValidateClauses(orClauses);
	}

	if (!clause.contains(_T("prop"))) {
		if (!found) {
			throw std::runtime_error("Each where clause must contain \"or\", "
				"\"and\", or a \"prop\"erty to compare against");
		} else {
			return;
		}
	} else if (found) {
		throw std::runtime_error("Only one of \"and\", \"or\", or a valid "
			"clause including \"prop\", \"op\", and a \"val\"ue to "
			"compare against must be present in a clause");
	}

	MojObject prop;
	clause.get(_T("prop"), prop);
	ValidateKey(prop);

	if (!clause.contains(_T("val"))) {
		throw std::runtime_error("Each where clause must contain a value to "
			"test against");
	}

	MojObject val;
	clause.get(_T("val"), val);

	if (!clause.contains(_T("op"))) {
		throw std::runtime_error("Each where clause must contain a test "
			"operation to perform");
	}

	MojObject op;
	clause.get(_T("op"), op);
	ValidateOp(op, val);
}
//TODO: when the connection is lost (no internet), complete/cancel the activity and exit the service
MojErr ConnectionState::ConnectionStateHandler::connectionManagerResult(MojObject& result, MojErr err)
{
	// log the parameters
	IMServiceHandler::logMojObjectJsonString(_T("ConnectionStateHandler::connectionManagerResult: %s"), result);

	if (err == MojErrNone && result.contains("$activity"))
	{
		MojObject activity, requirements, internetRequirements;
		// The connectionmanager status is under $activity.requirements.internet
		err = result.getRequired("$activity", activity);
		if (err == MojErrNone)
		{
			err = activity.getRequired("requirements", requirements);
			if (err == MojErrNone)
			{
				err = requirements.getRequired("internet", internetRequirements);
			}
		}

		if (err == MojErrNone)
		{
			m_receivedResponse = true;
			bool prevInetConnected = m_connState->m_inetConnected;
			internetRequirements.get("isInternetConnectionAvailable", m_connState->m_inetConnected);

			bool prevWifiConnected = m_connState->m_wifiConnected;
			bool found = false;
			MojObject wifiObj;
			err = internetRequirements.getRequired("wifi", wifiObj);
			MojString wifiState;
			err = wifiObj.getRequired("state", wifiState);
			m_connState->m_wifiConnected = (err == MojErrNone && wifiState.compare("connected") == 0);
			// If the connection was lost, keep the old ipAddress so it can be used to
			// notify accounts that were using it
			if (m_connState->m_wifiConnected == true)
			{
				err = wifiObj.get("ipAddress", m_connState->m_wifiIpAddress, found);
				if (err != MojErrNone || found == false)
				{
					// It may claim to be connected, but there's no interface IP address
					MojLogError(IMServiceApp::s_log, _T("Marking WiFi as disconnected because ipAddress is missing"));
					m_connState->m_wifiConnected = false;
				}
				MojLogInfo(IMServiceApp::s_log, _T("ConnectionStateHandler::connectionManagerResult found=%i, wifi ipAddress=%s"), found, m_connState->m_wifiIpAddress.data());
			}

			bool prevWanConnected = m_connState->m_wanConnected;

			MojObject wanObj;
			err = internetRequirements.getRequired("wan", wanObj);
			MojErrCheck(err);

			MojString wanState;
			err = wanObj.getRequired("state", wanState);
			MojErrCheck(err);

			m_connState->m_wanConnected = (err == MojErrNone && wanState.compare("connected") == 0);
			// If the connection was lost, keep the old ipAddress so it can be used to
			// notify accounts that were using it
			if (m_connState->m_wanConnected == true)
			{
				err = wanObj.get("ipAddress", m_connState->m_wanIpAddress, found);
				if (err != MojErrNone || found == false)
				{
					// It may claim to be connected, but there's no interface IP address
					MojLogError(IMServiceApp::s_log, _T("Marking WAN as disconnected because ipAddress is missing"));
					m_connState->m_wanConnected = false;
				}
				MojLogInfo(IMServiceApp::s_log, _T("ConnectionStateHandler::connectionManagerResult found=%i, wan ipAddress=%s"), found, m_connState->m_wanIpAddress.data());
			}

			// If the loginState machine setup a listener, then post the change
			if (m_loginState != NULL)
			{
				if (prevInetConnected != m_connState->m_inetConnected ||
					prevWifiConnected != m_connState->m_wifiConnected ||
					prevWanConnected != m_connState->m_wanConnected)
				{
					MojLogInfo(IMServiceApp::s_log, _T("ConnectionStateHandler::connectionManagerResult - connection changed - scheduling activity."));
					ConnectionState::ConnectionChangedScheduler *changeScheduler = new ConnectionState::ConnectionChangedScheduler(m_service);
					changeScheduler->scheduleActivity();
				}
				else {
					MojLogInfo(IMServiceApp::s_log, _T("ConnectionStateHandler::connectionManagerResult - no change from previous state."));
				}
			}
		}
	}
	else {
		MojLogInfo(IMServiceApp::s_log, _T("ConnectionStateHandler::connectionManagerResult no activity object - ignoring. err = %d"), err);
	}
	return MojErrNone;
}