summaryrefslogtreecommitdiff
path: root/protocols/SkypeWeb/src/skype_db.cpp
blob: d253b9219a345e5d7b3765f030f741d0cc841044 (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
/*
Copyright (c) 2015-19 Miranda NG team (https://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 "stdafx.h"

struct SkypeDBType { int type; char *name; DWORD flags; } g_SkypeDBTypes[] =
{
	{ SKYPE_DB_EVENT_TYPE_INCOMING_CALL, LPGEN("Incoming call"), DETF_NONOTIFY },
	{ SKYPE_DB_EVENT_TYPE_EDITED_MESSAGE, LPGEN("Edited message"), 0 },
	{ SKYPE_DB_EVENT_TYPE_ACTION, LPGEN("Action"), 0 },
	{ SKYPE_DB_EVENT_TYPE_CALL_INFO, LPGEN("Call information"), 0 },
	{ SKYPE_DB_EVENT_TYPE_FILETRANSFER_INFO, LPGEN("File transfer information"), 0 },
	{ SKYPE_DB_EVENT_TYPE_URIOBJ, LPGEN("URI object"), 0 },
	{ SKYPE_DB_EVENT_TYPE_MOJI, LPGEN("Moji"), 0 },
	{ SKYPE_DB_EVENT_TYPE_FILE, LPGEN("File"), 0 },
	{ SKYPE_DB_EVENT_TYPE_UNKNOWN, LPGEN("Unknown event"), 0 },
};

MEVENT CSkypeProto::GetMessageFromDb(const char *messageId)
{
	if (messageId == nullptr)
		return NULL;

	return db_event_getById(m_szModuleName, messageId);
}

MEVENT CSkypeProto::AddDbEvent(WORD type, MCONTACT hContact, DWORD timestamp, DWORD flags, const char *content, const char *uid)
{
	if (MEVENT hDbEvent = GetMessageFromDb(uid))
		return hDbEvent;

	MEVENT ret = AddEventToDb(hContact, type, timestamp, flags, (DWORD)mir_strlen(content)+1, (BYTE*)content);
	if (uid && ret)
		db_event_setId(m_szModuleName, ret, uid);
	return ret;
}

void CSkypeProto::EditEvent(MCONTACT hContact, MEVENT hEvent, const char *szContent, time_t edit_time)
{
	mir_cslock lck(m_AppendMessageLock);
	DBEVENTINFO dbei = {};
	dbei.cbBlob = db_event_getBlobSize(hEvent);
	mir_ptr<BYTE> blob((PBYTE)mir_alloc(dbei.cbBlob));
	dbei.pBlob = blob;
	db_event_get(hEvent, &dbei);

	JSONNode jMsg = JSONNode::parse((char*)dbei.pBlob);
	if (jMsg) {
		JSONNode &jEdits = jMsg["edits"];
		if (jEdits) {
			for (auto &it : jEdits)
				if (it["time"].as_int() == edit_time)
					return;

			JSONNode jEdit;
			jEdit
				<< JSONNode("time", (long)edit_time)
				<< JSONNode("text", szContent);

			jEdits << jEdit;
		}
	}
	else {
		jMsg = JSONNode();
		JSONNode jOriginalMsg; jOriginalMsg.set_name("original_message");
		JSONNode jEdits(JSON_ARRAY); jEdits.set_name("edits");
		JSONNode jEdit;

		jOriginalMsg
			<< JSONNode("time", (long)dbei.timestamp)
			<< JSONNode("text", (char*)dbei.pBlob);

		jMsg << jOriginalMsg;

		jEdit
			<< JSONNode("time", (long)edit_time)
			<< JSONNode("text", szContent);

		jEdits << jEdit;
		jMsg << jEdits;
	}
	
	std::string newMsg = jMsg.write().c_str();
	dbei.cbBlob = int(newMsg.size() + 1);
	dbei.pBlob = (PBYTE)newMsg.c_str();
	db_event_edit(hContact, hEvent, &dbei);
}

MEVENT CSkypeProto::AddEventToDb(MCONTACT hContact, WORD type, DWORD timestamp, DWORD flags, DWORD cbBlob, PBYTE pBlob)
{
	DBEVENTINFO dbei = {};
	dbei.szModule = m_szModuleName;
	dbei.timestamp = timestamp;
	dbei.eventType = type;
	dbei.cbBlob = cbBlob;
	dbei.pBlob = pBlob;
	dbei.flags = flags;
	return db_event_add(hContact, &dbei);
}

void CSkypeProto::InitDBEvents()
{
	db_set_resident(m_szModuleName, "LastAuthRequestTime");

	// custom event
	DBEVENTTYPEDESCR dbEventType = { sizeof(dbEventType) };
	dbEventType.module = m_szModuleName;
	dbEventType.flags = DETF_HISTORY | DETF_MSGWINDOW;
	dbEventType.iconService = MODULE "/GetEventIcon";
	dbEventType.textService = MODULE "/GetEventText";

	for (auto &cur : g_SkypeDBTypes) {
		dbEventType.eventType = cur.type;
		dbEventType.descr = Translate(cur.name);
		dbEventType.flags |= cur.flags;

		DbEvent_RegisterType(&dbEventType);

		dbEventType.flags &= (~cur.flags);
	}
}