summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_thread.cpp
blob: 4bb62c23d7a2472e78e0266c1cd87d6619819173 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "common.h"

int CSteamProto::PollStatus(const char *token, const char *sessionId, UINT32 messageId)
{
	SteamWebApi::PollApi::PollResult pollResult;
	SteamWebApi::PollApi::PollStatus(m_hNetlibUser, token, sessionId, messageId, &pollResult);

	if (!pollResult.IsSuccess())
		return 0;

	if (pollResult.IsNeedRelogin())
	{
		SteamWebApi::LoginApi::LoginResult loginResult;
		SteamWebApi::LoginApi::Logon(m_hNetlibUser, token, &loginResult);

		if (!loginResult.IsSuccess())
			return 0;

		return messageId;
	}

	for (int i = 0; i < pollResult.GetItemCount(); i++)
	{
		switch (pollResult[i]->GetType())
		{
		case SteamWebApi::PollApi::POOL_TYPE::TYPING:
			break;

		case SteamWebApi::PollApi::POOL_TYPE::MESSAGE:
			{
				SteamWebApi::PollApi::Message *message = (SteamWebApi::PollApi::Message*)pollResult[i];

				MCONTACT hContact = FindContact(message->GetSteamId());
				if (hContact)
				{
					const wchar_t *text = message->GetText();

					PROTORECVEVENT recv = { 0 };
					recv.flags = PREF_UTF;
					recv.timestamp = message->GetTimestamp();
					recv.szMessage = mir_utf8encodeW(text);

					ProtoChainRecvMsg(hContact, &recv);
				}
			}
			break;

		case SteamWebApi::PollApi::POOL_TYPE::MYMESSAGE:
			{
				SteamWebApi::PollApi::Message *message = (SteamWebApi::PollApi::Message*)pollResult[i];

				MCONTACT hContact = FindContact(message->GetSteamId());
				if (hContact)
				{
					const wchar_t *text = message->GetText();

					DBEVENTINFO dbei = { sizeof(dbei) };
					dbei.szModule = this->m_szModuleName;
					dbei.timestamp = message->GetTimestamp();
					dbei.eventType = EVENTTYPE_MESSAGE;
					dbei.cbBlob = lstrlen(text);
					dbei.pBlob = (BYTE*)mir_utf8encodeW(text);
					dbei.flags = DBEF_UTF | DBEF_SENT;

					db_event_add(hContact, &dbei);
				}
			}
			break;

		case SteamWebApi::PollApi::POOL_TYPE::STATE:
			{
				SteamWebApi::PollApi::State *state = (SteamWebApi::PollApi::State*)pollResult[i];

				WORD status = CSteamProto::SteamToMirandaStatus(state->GetStatus());
				const char *cSteamId = state->GetSteamId();
				const wchar_t *nickname = state->GetNickname();

				ptrA steamId(getStringA("SteamID"));
				if (!lstrcmpA(steamId, cSteamId))
				{
					const wchar_t *oldNickname = getWStringA("Nick");
					if (lstrcmp(oldNickname, nickname))
						setWString("Nick", nickname);
						SetStatus(status);
						
				}
				else
				{
					MCONTACT hContact = FindContact(cSteamId);
					if (hContact)
					{
						const wchar_t *oldNickname = getWStringA(hContact, "Nick");
						if (lstrcmp(oldNickname, nickname))
							setWString(hContact, "Nick", nickname);
						SetContactStatus(hContact, status);
					}
				}
			}
			break;
		}
	}

	return pollResult.GetMessageId();
}

void CSteamProto::PollingThread(void*)
{
	debugLogA("CSteamProto::PollingThread: entering");

	ptrA token(getStringA("TokenSecret"));
	ptrA sessionId(getStringA("SessionID"));
	UINT32 messageId = getDword("MessageID", 0);

	while (!m_bTerminated)
	{
		messageId = PollStatus(token, sessionId, messageId);
		if (messageId == 0)
			break;
	}

	if (messageId > 0)
		setDword("MessageID", messageId);

	m_hPollingThread = NULL;
	debugLogA("CSteamProto::PollingThread: leaving");
}