summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_history.cpp
blob: 65c77f490ced0e4f24e3377bf11c2c7041dcab44 (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
#include "stdafx.h"

void CSteamProto::SendHistoryRequest(uint64_t accountId, uint32_t startTime)
{
	CFriendMessagesGetRecentMessagesRequest request;
	request.steamid1 = m_iSteamId; request.has_steamid1 = true;
	request.steamid2 = AccountIdToSteamId(accountId); request.has_steamid2 = true;
	request.rtime32_start_time = startTime; request.has_rtime32_start_time = true;
	WSSendService(FriendGetRecentMessages, request);
}

void CSteamProto::OnGotRecentMessages(const CFriendMessagesGetRecentMessagesResponse &reply, const CMsgProtoBufHeader &hdr)
{
	if (hdr.failed())
		return;

	for (int i = 0; i < reply.n_messages; i++) {
		auto *pMsg = reply.messages[i];
		auto steamId = AccountIdToSteamId(pMsg->accountid);

		MCONTACT hContact = GetContact(steamId);
		if (!hContact)
			continue;

		MEVENT hEvent;
		char szMsgId[100];
		if (pMsg->has_timestamp) {
			itoa(pMsg->timestamp, szMsgId, 10);
			hEvent = db_event_getById(m_szModuleName, szMsgId);
		}
		else hEvent = 0;

		DB::EventInfo dbei(hEvent);
		dbei.flags = DBEF_UTF;
		if (steamId == m_iSteamId)
			dbei.flags |= DBEF_SENT;
		dbei.cbBlob = (int)mir_strlen(pMsg->message);
		dbei.pBlob = mir_strdup(pMsg->message);
		if (pMsg->has_timestamp) {
			if (getDword(hContact, DBKEY_LASTMSG) < pMsg->timestamp)
				setDword(hContact, DBKEY_LASTMSG, pMsg->timestamp);

			dbei.szId = szMsgId;
			dbei.timestamp = pMsg->timestamp;
		}
		else dbei.timestamp = time(0);

		if (dbei.getEvent())
			db_event_edit(hEvent, &dbei, true);
		else
			ProtoChainRecvMsg(hContact, dbei);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

void CSteamProto::OnGotConversations(const CFriendsMessagesGetActiveMessageSessionsResponse &reply, const CMsgProtoBufHeader &hdr)
{
	if (hdr.failed())
		return;

	for (int i=0; i < reply.n_message_sessions; i++) {
		auto *session = reply.message_sessions[i];

		uint64_t steamId = AccountIdToSteamId(session->accountid_friend);
		MCONTACT hContact = GetContact(steamId);
		if (!hContact)
			continue;

		time_t storedMessageTS = getDword(hContact, DBKEY_LASTMSG);
		if (session->last_message > storedMessageTS)
			SendHistoryRequest(steamId, storedMessageTS);
	}
}

void CSteamProto::OnGotHistoryMessages(const CMsgClientChatGetFriendMessageHistoryResponse &reply, const CMsgProtoBufHeader &hdr)
{
	if (hdr.failed())
		return;

	MCONTACT hContact = GetContact(reply.steamid);
	if (!hContact)
		return;

	uint32_t iLastMessage = getDword(hContact, DBKEY_LASTMSG);

	for (int i = 0; i < reply.n_messages; i++) {
		auto *pMsg = reply.messages[i];
		
		MEVENT hEvent;
		char szMsgId[100];

		if (pMsg->has_timestamp) {
			if (iLastMessage < pMsg->timestamp)
				iLastMessage = pMsg->timestamp;
			itoa(pMsg->timestamp, szMsgId, 10);
			hEvent = db_event_getById(m_szModuleName, szMsgId);
		}
		else hEvent = 0;

		DB::EventInfo dbei(hEvent);
		dbei.flags = DBEF_UTF;
		if (pMsg->has_unread && !pMsg->unread)
			dbei.flags |= DBEF_READ;
		if (pMsg->accountid == m_iSteamId)
			dbei.flags |= DBEF_SENT;
		dbei.cbBlob = (int)mir_strlen(pMsg->message);
		dbei.pBlob = mir_strdup(pMsg->message);
		if (pMsg->has_timestamp) {
			dbei.timestamp = pMsg->timestamp;
			dbei.szId = szMsgId;
		}
		else dbei.timestamp = time(0);

		if (dbei.getEvent())
			db_event_edit(hEvent, &dbei, true);
		else
			ProtoChainRecvMsg(hContact, dbei);
	}

	setDword(hContact, DBKEY_LASTMSG, iLastMessage);
}