summaryrefslogtreecommitdiff
path: root/protocols/Skype/src/skype_database.cpp
blob: b607c8e4b7fedb1040357242ed5f99697383dcee (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "skype_proto.h"

HANDLE CSkypeProto::AddDataBaseEvent(HANDLE hContact, WORD type, DWORD time, DWORD flags, DWORD cbBlob, PBYTE pBlob)
{
	DBEVENTINFO dbei = {0};

	dbei.cbSize = sizeof(dbei);
	dbei.szModule = this->m_szModuleName;
	dbei.timestamp = time;
	dbei.flags = flags;
	dbei.eventType = type;
	dbei.cbBlob = cbBlob;
	dbei.pBlob = pBlob;

	return (HANDLE)CallService(MS_DB_EVENT_ADD, (WPARAM)hContact, (LPARAM)&dbei);
}

void CSkypeProto::RaiseAuthRequestEvent(
	DWORD timestamp,
	CContact::Ref contact)
{	
	char *sid = ::mir_utf8decodeA(contact->GetSid());
	char *nick = ::mir_utf8decodeA(contact->GetNick());

	SEString data;

	contact->GetPropReceivedAuthrequest(data);
	char* reason = ::mir_utf8decodeA(data);

	SEString last;
	contact->GetFullname(data, last);
	char* firstName = ::mir_utf8decodeA(data);
	char* lastName = ::mir_utf8decodeA(last);

	PROTORECVEVENT pre = {0};

	CCSDATA ccs = {0};
	ccs.szProtoService = PSR_AUTH;
	ccs.hContact = this->AddContact(contact);
	ccs.wParam = 0;
	ccs.lParam = (LPARAM)⪯
	pre.timestamp = timestamp;
	pre.lParam = (DWORD)
		(sizeof(DWORD) * 2) + 
		::strlen(nick) + 
		::strlen(firstName) + 
		::strlen(lastName) + 
		::strlen(sid) + 
		::strlen(reason) + 		
		5;

	/*blob is: 0(DWORD), hContact(DWORD), nick(ASCIIZ), firstName(ASCIIZ), lastName(ASCIIZ), sid(ASCIIZ), reason(ASCIIZ)*/
	char *pCurBlob = pre.szMessage = (char*)::mir_alloc(pre.lParam);
	
	*((PDWORD)pCurBlob) = 0; pCurBlob += sizeof(DWORD);
	*((PDWORD)pCurBlob) = (DWORD)ccs.hContact; pCurBlob += sizeof(DWORD);
	::strcpy((char*)pCurBlob, nick); pCurBlob += ::strlen(nick) + 1;
	::strcpy((char*)pCurBlob, firstName); pCurBlob += ::strlen(firstName) + 1;
	::strcpy((char*)pCurBlob, lastName); pCurBlob += ::strlen(lastName) + 1;
	::strcpy((char*)pCurBlob, sid); pCurBlob += ::strlen(sid) + 1;
	::strcpy((char*)pCurBlob, reason);
	
	::CallService(MS_PROTO_CHAINRECV, 0, (LPARAM)&ccs);
}

bool CSkypeProto::IsMessageInDB(HANDLE hContact, DWORD timestamp, const char* message, int flag)
{
	bool result = false;

	int length = ::strlen(message);

	HANDLE hDbEvent = ::db_event_last(hContact);
	while (hDbEvent) 
	{
		DBEVENTINFO dbei = { sizeof(dbei) };
		dbei.cbBlob = ::db_event_getBlobSize(hDbEvent);
		dbei.pBlob = (PBYTE)::mir_alloc(dbei.cbBlob);
		::db_event_get(hDbEvent, &dbei);

		if (dbei.timestamp < timestamp)
		{
			::mir_free(dbei.pBlob);
			break;
		}

		int sendFlag = dbei.flags & DBEF_SENT;
		if (dbei.eventType == EVENTTYPE_MESSAGE && sendFlag == flag)
		{
			char *dbMessage = (char *)dbei.pBlob;
			if (::strncmp(dbMessage, message, length) == 0 && dbei.timestamp == timestamp)
			{
				::mir_free(dbei.pBlob);
				result = true;
				break;
			}
		}

		::mir_free(dbei.pBlob);
		hDbEvent = ::db_event_prev(hDbEvent);		
	}

	return result;
}

void CSkypeProto::RaiseMessageReceivedEvent(
	HANDLE hContact,
	DWORD timestamp,
	const wchar_t *message,
	bool isNeedCheck)
{	
	/*if (isNeedCheck)
		if (this->IsMessageInDB(hContact, timestamp, message))
			return;*/

	PROTORECVEVENT recv;
	recv.flags = PREF_UNICODE;
	recv.timestamp = timestamp;
	recv.tszMessage = ::mir_wstrdup(message);
	
	::ProtoChainRecvMsg(hContact, &recv);
}

void CSkypeProto::RaiseMessageSendedEvent(
	HANDLE hContact,
	DWORD timestamp,
	const wchar_t *message)
{	
	/*if (this->IsMessageInDB(hContact, timestamp, message, DBEF_SENT))
		return;*/

	char *msg = ::mir_utf8encodeW(message);

	DBEVENTINFO dbei = { 0 };
	dbei.cbSize = sizeof(dbei);
	dbei.szModule = this->m_szModuleName;
	dbei.timestamp = timestamp;
	dbei.eventType = EVENTTYPE_MESSAGE;
	dbei.cbBlob = (DWORD)::strlen(msg) + 1;
	dbei.pBlob = (PBYTE)msg;
	dbei.flags = PREF_UTF | DBEF_SENT;

	::db_event_add(hContact, &dbei);
}

//void CSkypeProto::RaiseFileReceivedEvent(
//	DWORD timestamp,
//	const char* sid, 
//	const char* nick, 
//	const char* message)
//{	
//	PROTORECVFILET pre = {0};
//
//	CCSDATA ccs = {0};
//	ccs.szProtoService = PSR_FILE;
//	ccs.hContact = this->AddContactBySid(sid, nick);
//	ccs.wParam = 0;
//	ccs.lParam = (LPARAM)&pre;
//	pre.flags = PREF_UTF;
//	pre.timestamp = timestamp;
//	//pre.szMessage = (char *)message;
//	
//	::CallService(MS_PROTO_CHAINRECV, 0, (LPARAM)&ccs);
//}