summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_messages.cpp
blob: 1a51cccb90199530768b55d7bd0f8efe06ddac55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "stdafx.h"

struct SendMessageParam
{
	MCONTACT hContact;
	HANDLE hMessage;
	char *message;
};

void MessageParamFree(void *arg)
{
	SendMessageParam *param = (SendMessageParam*)arg;
	mir_free(param->message);
	mir_free(param);
}

int CSteamProto::OnSendMessage(MCONTACT hContact, const char* message)
{
	UINT hMessage = InterlockedIncrement(&hMessageProcess);

	SendMessageParam *param = (SendMessageParam*)mir_calloc(sizeof(SendMessageParam));
	param->hContact = hContact;
	param->hMessage = (HANDLE)hMessage;
	param->message = mir_strdup(message);

	ptrA token(getStringA("TokenSecret"));
	ptrA umqid(getStringA("UMQID"));
	ptrA steamId(getStringA(hContact, "SteamID"));
	PushRequest(
		new SendMessageRequest(token, umqid, steamId, message),
		&CSteamProto::OnMessageSent,
		param, MessageParamFree);

	return hMessage;
}

void CSteamProto::OnMessageSent(const HttpResponse *response, void *arg)
{
	SendMessageParam *param = (SendMessageParam*)arg;

	ptrT error(mir_tstrdup(TranslateT("Unknown error")));
	ptrT steamId(getTStringA(param->hContact, "SteamID"));

	if (ResponseHttpOk(response))
	{
		JSONROOT root(response->pData);
		JSONNode *node = json_get(root, "error");
		if (node)
			error = json_as_string(node);
	}

	if (mir_tstrcmpi(error, _T("OK")) != 0)
	{
		ptrA errorA(mir_t2a(error));
		debugLogA("CSteamProto::OnMessageSent: failed to send message for %s (%s)", steamId, errorA);
		ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hMessage, (LPARAM)errorA);
	}
	else
		ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hMessage, 0);
}