summaryrefslogtreecommitdiff
path: root/protocols/SkypeWeb/src/skype_history_sync.cpp
blob: d8ba51cb75fd2a9aecfdc1efbcc40c6aae34bbf2 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (c) 2015 Miranda NG project (http://miranda-ng.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"

/* HISTORY SYNC */

void CSkypeProto::OnGetServerHistory(const NETLIBHTTPREQUEST *response)
{
	if (response == NULL)
		return;

	JSONROOT root(response->pData);
	if (root == NULL)
		return;

	JSONNODE *conversations = json_as_array(json_get(root, "messages"));
	for (size_t i = 0; i < json_size(conversations); i++)
	{
		JSONNODE *message = json_at(conversations, i);

		ptrA clientMsgId(mir_t2a(ptrT(json_as_string(json_get(message, "clientmessageid")))));
		ptrA skypeEditedId(mir_t2a(ptrT(json_as_string(json_get(message, "skypeeditedid")))));
		ptrA messageType(mir_t2a(ptrT(json_as_string(json_get(message, "messagetype")))));
		ptrA from(mir_t2a(ptrT(json_as_string(json_get(message, "from")))));
		ptrA content(mir_t2a(ptrT(json_as_string(json_get(message, "content")))));
		ptrT composeTime(json_as_string(json_get(message, "composetime")));
		ptrA conversationLink(mir_t2a(ptrT(json_as_string(json_get(message, "conversationLink")))));
		time_t timestamp = IsoToUnixTime(composeTime);
		bool isEdited = (json_get(message, "skypeeditedid") != NULL);
		if (conversationLink != NULL && strstr(conversationLink, "/8:"))
		{
			int emoteOffset = json_as_int(json_get(message, "skypeemoteoffset"));

			int flags = DBEF_UTF | DBEF_READ;

			ptrA skypename(ContactUrlToName(from));

			bool isMe = IsMe(skypename);
			if (isMe)
				flags |= DBEF_SENT;

			MCONTACT hContact = FindContact(ptrA(ContactUrlToName(conversationLink)));

			ptrA message(RemoveHtml(content));
			MEVENT dbevent =  GetMessageFromDb(hContact, skypeEditedId);

			if (isEdited && dbevent != NULL)
			{
				DBEVENTINFO dbei = { sizeof(dbei) };
				CMStringA msg;

				dbei.cbBlob = db_event_getBlobSize(dbevent);
				mir_ptr<BYTE> blob((PBYTE)mir_alloc(dbei.cbBlob));
				dbei.pBlob = blob;

				db_event_get(dbevent, &dbei);
				time_t dbEventTimestamp = dbei.timestamp;
				ptrA dbMsgText((char *)mir_alloc(dbei.cbBlob));

				mir_strcpy(dbMsgText, (char*)dbei.pBlob);

				msg.AppendFormat("%s\n%s [%s]:\n%s", dbMsgText, Translate("Edited at"), ptrA(mir_t2a(composeTime)), message);
				db_event_delete(hContact, dbevent);
				AddMessageToDb(hContact, dbEventTimestamp, flags, clientMsgId, msg.GetBuffer(), emoteOffset);
			}
			else
				AddMessageToDb(hContact, timestamp, flags, clientMsgId, message, emoteOffset);
		}
		else if (conversationLink != NULL && strstr(conversationLink, "/19:"))
		{
			ptrA chatname(ContactUrlToName(conversationLink));
			GCDEST gcd = { m_szModuleName, ptrT(mir_a2t(chatname)), GC_EVENT_MESSAGE };
			GCEVENT gce = { sizeof(GCEVENT), &gcd };
			gce.bIsMe = IsMe(ContactUrlToName(from));
			gce.ptszUID = ptrT(mir_a2t(ContactUrlToName(from)));
			gce.time = timestamp;
			gce.ptszNick = ptrT(mir_a2t(ContactUrlToName(from)));
			gce.ptszText = ptrT(mir_a2t(ptrA(RemoveHtml(content))));
			gce.dwFlags = GCEF_NOTNOTIFY;
			CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce);
		}
	}
}

INT_PTR CSkypeProto::GetContactHistory(WPARAM hContact, LPARAM)
{
	PushRequest(new GetHistoryRequest(RegToken, ptrA(db_get_sa(hContact, m_szModuleName, SKYPE_SETTINGS_ID)), 0, Server), &CSkypeProto::OnGetServerHistory);
	return 0;
}

void CSkypeProto::SyncHistory()
{
	PushRequest(new SyncHistoryFirstRequest(RegToken, Server), &CSkypeProto::OnSyncHistory);
}

void CSkypeProto::OnSyncHistory(const NETLIBHTTPREQUEST *response)
{
	if (response == NULL)
		return;
	JSONROOT root(response->pData);
	if (root == NULL)
		return;
	JSONNODE *conversations = json_as_array(json_get(root, "conversations"));
	for (size_t i = 0; i < json_size(conversations); i++)
	{
		JSONNODE *conversation = json_at(conversations, i);
		JSONNODE *lastMessage = json_get(conversation, "lastMessage");
		if (lastMessage == NULL)
			continue;

		char *clientMsgId = mir_t2a(json_as_string(json_get(lastMessage, "clientmessageid")));
		char *skypeEditedId = mir_t2a(json_as_string(json_get(lastMessage, "skypeeditedid")));
		bool isEdited = (skypeEditedId != NULL);
		char *conversationLink = mir_t2a(json_as_string(json_get(lastMessage, "conversationLink")));
		time_t composeTime(IsoToUnixTime(ptrT(json_as_string(json_get(lastMessage, "conversationLink")))));
		bool isChat = false;
		ptrA skypename(ContactUrlToName(conversationLink));
		if (skypename == NULL)
		{
			skypename = ChatUrlToName(conversationLink);
			if (skypename == NULL)
				continue;
			else 
				isChat = true;
		}
		MCONTACT hContact = !isChat ? AddContact(skypename) : AddChatRoom(skypename);
		if (isChat) StartChatRoom(hContact);
		if (GetMessageFromDb(hContact, clientMsgId, composeTime) == NULL)
			SendRequest(new GetHistoryRequest(RegToken, skypename, 0, Server, isChat), &CSkypeProto::OnGetServerHistory);
	}
}