Beispiel #1
0
static TCHAR* parseContactInfo(ARGUMENTSINFO *ai)
{
	if (ai->argc != 3)
		return NULL;

	MCONTACT hContact = NULL;
	CONTACTSINFO ci = { 0 };
	ci.cbSize = sizeof(ci);
	ci.tszContact = ai->targv[1];
	ci.flags = 0xFFFFFFFF ^ (CI_TCHAR == 0 ? CI_UNICODE : 0);
	int count = getContactFromString(&ci);
	if (count == 1 && ci.hContacts != NULL) {
		hContact = ci.hContacts[0];
		mir_free(ci.hContacts);
	}
	else {
		mir_free(ci.hContacts);
		return NULL;
	}
	BYTE type = getContactInfoType(ai->targv[2]);
	if (type == 0)
		return NULL;

	return getContactInfoT(type, hContact);
}
static TCHAR *parseGetDefault(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return NULL;

	MCONTACT hContact = NULL;

	CONTACTSINFO ci = { 0 };
	ci.cbSize = sizeof(ci);
	ci.tszContact = ai->targv[1];
	ci.flags = 0xFFFFFFFF ^ (CI_TCHAR == 0 ? CI_UNICODE : 0);
	int count = getContactFromString(&ci);
	if (count == 1 && ci.hContacts != NULL) {
		hContact = ci.hContacts[0];
		mir_free(ci.hContacts);
	}
	else {
		mir_free(ci.hContacts);
		return NULL;
	}

	hContact = db_mc_getDefault(hContact);
	if (hContact == NULL)
		return NULL;

	TCHAR* szUniqueID = NULL;
	char* szProto = GetContactProto(hContact);
	if (szProto != NULL)
		szUniqueID = getContactInfoT(CNF_UNIQUEID, hContact);

	if (szUniqueID == NULL) {
		szProto = PROTOID_HANDLE;
		szUniqueID = (TCHAR *)mir_alloc(32);
		mir_sntprintf(szUniqueID, 32, _T("%p"), hContact);
		if (szProto == NULL || szUniqueID == NULL)
			return NULL;
	}

	size_t size = strlen(szProto) + _tcslen(szUniqueID) + 4;
	TCHAR *res = (TCHAR *)mir_alloc(size * sizeof(TCHAR));
	if (res == NULL) {
		mir_free(szUniqueID);
		return NULL;
	}

	TCHAR *tszProto = mir_a2t(szProto);
	if (tszProto != NULL && szUniqueID != NULL) {
		mir_sntprintf(res, size, _T("<%s:%s>"), tszProto, szUniqueID);
		mir_free(szUniqueID);
		mir_free(tszProto);
	}

	return res;
}
Beispiel #3
0
// returns a string in the form <PROTOID:UNIQUEID>, cannot be _HANDLE_!
// result must be freed
TCHAR* encodeContactToString(MCONTACT hContact)
{
	char *szProto = GetContactProto(hContact);
	TCHAR *tszUniqueId = getContactInfoT(CNF_UNIQUEID, hContact);
	if (szProto == NULL || tszUniqueId == NULL)
		return NULL;

	size_t size = _tcslen(tszUniqueId) + strlen(szProto) + 4;
	TCHAR *tszResult = (TCHAR *)mir_calloc(size * sizeof(TCHAR));
	if (tszResult)
		mir_sntprintf(tszResult, size, _T("<%S:%s>"), szProto, tszUniqueId);
	return tszResult;
}
static TCHAR *parseGetParent(ARGUMENTSINFO *ai)
{
	if (ai->argc != 2)
		return NULL;

	MCONTACT hContact = NULL;

	CONTACTSINFO ci = { sizeof(ci) };
	ci.tszContact = ai->targv[1];
	ci.flags = 0xFFFFFFFF ^ (CI_TCHAR == 0 ? CI_UNICODE : 0);
	int count = getContactFromString(&ci);
	if (count == 1 && ci.hContacts != NULL) {
		hContact = ci.hContacts[0];
		mir_free(ci.hContacts);
	}
	else {
		mir_free(ci.hContacts);
		return NULL;
	}

	hContact = db_mc_getMeta(hContact);
	if (hContact == NULL)
		return NULL;

	ptrT szUniqueID;
	char* szProto = GetContactProto(hContact);
	if (szProto != NULL)
		szUniqueID = getContactInfoT(CNF_UNIQUEID, hContact);

	if (szUniqueID == NULL) {
		szProto = PROTOID_HANDLE;
		TCHAR tszID[40];
		mir_sntprintf(tszID, SIZEOF(tszID), _T("%p"), hContact);
		szUniqueID = mir_tstrdup(tszID);
	}

	if (szUniqueID == NULL)
		return NULL;

	return mir_tstrdup(CMString(FORMAT, _T("<%S:%s>"), szProto, szUniqueID));
}
Beispiel #5
0
static TCHAR* parseSpecialContact(ARGUMENTSINFO *ai)
{
	if (ai->argc != 1 || ai->fi->hContact == NULL)
		return NULL;

	ai->flags |= AIF_DONTPARSE;
	ptrT szUniqueID;
	char *szProto = GetContactProto(ai->fi->hContact);
	if (szProto != NULL)
		szUniqueID = getContactInfoT(CNF_UNIQUEID, ai->fi->hContact);

	if (szUniqueID == NULL) {
		szProto = PROTOID_HANDLE;
		szUniqueID = (TCHAR*)mir_alloc(40);
		if (szUniqueID == NULL)
			return NULL;
		mir_sntprintf(szUniqueID, 20, _T("%p"), ai->fi->hContact);
	}

	if (szUniqueID == NULL)
		return NULL;

	return CMString(FORMAT, _T("<%S:%s>"), szProto, szUniqueID).Detach();
}
Beispiel #6
0
/*
	MS_VARS_GETCONTACTFROMSTRING
*/
int getContactFromString(CONTACTSINFO *ci)
{
	/* service to retrieve a contact's HANDLE from a given string */
	if (ci == NULL)
		return -1;

	TCHAR *tszContact;
	if (ci->flags & CI_UNICODE)
		tszContact = NEWTSTR_ALLOCA(ci->tszContact);
	else {
		WCHAR* tmp = mir_a2t(ci->szContact);
		tszContact = NEWTSTR_ALLOCA(tmp);
		mir_free(tmp);
	}
	if ((tszContact == NULL) || (_tcslen(tszContact) == 0))
		return -1;

	ci->hContacts = NULL;
	int count = 0;
	/* search the cache */
	{
		mir_cslock lck(csContactCache);
		for (int i=0; i < cacheSize; i++) {
			if ((!_tcscmp(cce[i].tszContact, tszContact)) && (ci->flags == cce[i].flags)) {
				/* found in cache */
				ci->hContacts = (MCONTACT*)mir_alloc(sizeof(MCONTACT));
				if (ci->hContacts == NULL)
					return -1;

				ci->hContacts[0] = cce[i].hContact;
				return 1;
			}
		}
	}

	/* contact was not in cache, do a search */
	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
	{
		TCHAR *szFind = NULL;
		BOOL bMatch = FALSE;

		// <_HANDLE_:hContact>
		size_t size = _tcslen(_T(PROTOID_HANDLE)) + 36;
		szFind = (TCHAR *)mir_alloc(size * sizeof(TCHAR));
		if (szFind != NULL)
		{
			mir_sntprintf(szFind, size, _T("<%s:%p>"), _T(PROTOID_HANDLE), hContact);
			if (!_tcsncmp(tszContact, szFind, _tcslen(tszContact)))
				bMatch = TRUE;

			mir_free(szFind);
		}

		char *szProto = GetContactProto(hContact);
		if (szProto == NULL)
			continue;

		// <proto:id> (exact)
		if ((ci->flags & CI_PROTOID) && !bMatch)
		{
			TCHAR *cInfo = getContactInfoT(CNF_UNIQUEID, hContact);
			if (cInfo)
			{
				size_t size = _tcslen(cInfo) + strlen(szProto) + 4;
				szFind = (TCHAR *)mir_alloc(size * sizeof(TCHAR));
				if (szFind != NULL) {
					mir_sntprintf(szFind, size, _T("<%S:%s>"), szProto, cInfo);
					mir_free(cInfo);
					if (!_tcsncmp(tszContact, szFind, _tcslen(tszContact)))
						bMatch = TRUE;
					mir_free(szFind);
				}
			}
		}
		// id (exact)
		if ((ci->flags & CI_UNIQUEID) && (!bMatch)) {
			szFind = getContactInfoT(CNF_UNIQUEID, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// nick (not exact)
		if ((ci->flags & CI_NICK) && (!bMatch)) {
			szFind = getContactInfoT(CNF_NICK, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// list name (not exact)
		if ((ci->flags & CI_LISTNAME) && (!bMatch)) {
			szFind = getContactInfoT(CNF_DISPLAY, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// firstname (exact)
		if ((ci->flags & CI_FIRSTNAME) && (!bMatch)) {
			szFind = getContactInfoT(CNF_FIRSTNAME, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// lastname (exact)
		if ((ci->flags & CI_LASTNAME) && (!bMatch)) {
			szFind = getContactInfoT(CNF_LASTNAME, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// email (exact)
		if ((ci->flags & CI_EMAIL) && (!bMatch)) {
			szFind = getContactInfoT(CNF_EMAIL, hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		// CNF_ (exact)
		if ((ci->flags & CI_CNFINFO) && (!bMatch)) {
			szFind = getContactInfoT((BYTE)(ci->flags&~(CI_CNFINFO|CI_TCHAR)), hContact);
			if (szFind != NULL) {
				if (!_tcscmp(tszContact, szFind))
					bMatch = TRUE;
				mir_free(szFind);
			}
		}
		if (bMatch) {
			ci->hContacts = (MCONTACT*)mir_realloc(ci->hContacts, (count + 1)*sizeof(MCONTACT));
			if (ci->hContacts == NULL)
				return -1;

			ci->hContacts[count] = hContact;
			count += 1;
		}
	}

	if (count == 1) { /* cache the found result */
		mir_cslock lck(csContactCache);
		cce = (CONTACTCE*)mir_realloc(cce, (cacheSize+1)*sizeof(CONTACTCE));
		if (cce != NULL) {
			cce[cacheSize].hContact = ci->hContacts[0];
			cce[cacheSize].flags = ci->flags;
			cce[cacheSize].tszContact = mir_tstrdup(tszContact);
			if (cce[cacheSize].tszContact != NULL)
				cacheSize += 1;
		}
	}

	return count;
}