Example #1
0
// Try to send an open job from the job list
// this is ONLY called from the WM_TIMER handler and should never be executed directly.
int CSendLater::sendIt(CSendLaterJob *job)
{
	time_t now = time(0);
	if (job->bCode == CSendLaterJob::JOB_HOLD || job->bCode == CSendLaterJob::JOB_DEFERRED || job->fSuccess || job->fFailed || job->lastSent > now)
		return 0;											// this one is frozen or done (will be removed soon), don't process it now.

	if (now - job->created > SENDLATER_AGE_THRESHOLD) {		// too old, this will be discarded and user informed by popup
		job->fFailed = true;
		job->bCode = CSendLaterJob::JOB_AGE;
		return 0;
	}

	// mark job as deferred (5 unsuccessful sends). Job will not be removed, but 
	// the user must manually reset it in order to trigger a new send attempt.
	if (job->iSendCount == 5) {
		job->bCode = CSendLaterJob::JOB_DEFERRED;
		return 0;
	}

	if (job->iSendCount > 0 && (now - job->lastSent < SENDLATER_RESEND_THRESHOLD))
		return 0;											// this one was sent, but probably failed. Resend it after a while

	CContactCache *c = CContactCache::getContactCache(job->hContact);
	if (c == NULL)
		return 0;						// should not happen

	if (!c->isValid()) {
		job->fFailed = true;
		job->bCode = CSendLaterJob::INVALID_CONTACT;
		return 0;						// can happen (contact has been deleted). mark the job as failed
	}

	MCONTACT hContact = c->getActiveContact();
	const char *szProto = c->getActiveProto();
	if (!hContact || szProto == 0)
		return 0;

	WORD wMyStatus = (WORD)CallProtoService(szProto, PS_GETSTATUS, 0, 0);
	WORD wContactStatus = c->getActiveStatus();

	// status mode checks
	if (wMyStatus == ID_STATUS_OFFLINE) {
		job->bCode = CSendLaterJob::JOB_MYSTATUS;
		return 0;
	}
	if (job->szId[0] == 'S') {
		if (!(wMyStatus == ID_STATUS_ONLINE || wMyStatus == ID_STATUS_FREECHAT)) {
			job->bCode = CSendLaterJob::JOB_MYSTATUS;
			return 0;
		}
	}

	job->lastSent = now;
	job->iSendCount++;
	job->hTargetContact = hContact;
	job->bCode = CSendLaterJob::JOB_WAITACK;

	DWORD dwFlags = IsUtfSendAvailable(hContact) ? PREF_UTF : PREF_UNICODE;
	if (dwFlags & PREF_UTF)
		job->hProcess = (HANDLE)CallContactService(hContact, PSS_MESSAGE, dwFlags, (LPARAM)job->sendBuffer);
	else
		job->hProcess = (HANDLE)CallContactService(hContact, PSS_MESSAGE, dwFlags, (LPARAM)job->pBuf);
	return 0;
}