summaryrefslogtreecommitdiff
path: root/plugins/FavContacts/src/contact_cache.cpp
blob: 0cfc3b4757380f26bae021dd9f398faef4cb5295 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "headers.h"

int __cdecl CContactCache::OnDbEventAdded(WPARAM wParam, LPARAM lParam)
{
	HANDLE hContact = (HANDLE)wParam;
	HANDLE hEvent = (HANDLE)lParam;

	DBEVENTINFO dbei = {0};
	dbei.cbSize = sizeof(dbei);
	db_event_get(hEvent, &dbei);
	if (dbei.eventType != EVENTTYPE_MESSAGE) return 0;

	float weight = GetEventWeight(time(NULL) - dbei.timestamp);
	float q = GetTimeWeight(time(NULL) - m_lastUpdate);
	m_lastUpdate = time(NULL);
	if (!weight) return 0;

	Lock();
	bool found = false;
	for (int i = 0; i < m_cache.getCount(); ++i)
	{
		m_cache[i].rate *= q;
		if (m_cache[i].hContact == hContact)
		{
			found = true;
			m_cache[i].rate += weight;
		}
	}

	if (!found)
	{
		TContactInfo *info = new TContactInfo;
		info->hContact = hContact;
		info->rate = weight;
		m_cache.insert(info);
	} else
	{
		qsort(m_cache.getArray(), m_cache.getCount(), sizeof(TContactInfo *), TContactInfo::cmp2);
	}

	Unlock();
	return 0;
}

float CContactCache::GetEventWeight(unsigned long age)
{
	const float ceil = 1000.f;
	const float floor = 0.0001f;
	const int depth = 60 * 60 * 24 * 30;
	if (age > depth) return 0;
	return exp(log(ceil) - age * (log(ceil) - log(floor)) / depth);
}

float CContactCache::GetTimeWeight(unsigned long age)
{
	const float ceil = 1000.f;
	const float floor = 0.0001f;
	const int depth = 60 * 60 * 24 * 30;
	if (age > depth) return 0;
	return exp(age * (log(ceil) - log(floor)) / depth);
}

CContactCache::CContactCache(): m_cache(50, TContactInfo::cmp)
{
	InitializeCriticalSection(&m_cs);

	int (__cdecl CContactCache::*pfn)(WPARAM, LPARAM);
	pfn = &CContactCache::OnDbEventAdded;
	HookEventObj(ME_DB_EVENT_ADDED, *(MIRANDAHOOKOBJ *)&pfn, this);

	Rebuild();
}

CContactCache::~CContactCache()
{
	DeleteCriticalSection(&m_cs);
}

void CContactCache::Rebuild()
{
	unsigned long timestamp = time(NULL);
	m_lastUpdate = time(NULL);

	for (HANDLE hContact = db_find_first(); hContact; hContact = db_find_next(hContact)) {
		TContactInfo *info = new TContactInfo;
		info->hContact = hContact;
		info->rate = 0;

		for (HANDLE hEvent = db_event_last(hContact); hEvent; hEvent = db_event_prev(hEvent)) {
			DBEVENTINFO dbei = { sizeof(dbei) };
			if (!db_event_get(hEvent, &dbei)) {
				if (float weight = GetEventWeight(timestamp - dbei.timestamp))
				{
					if (dbei.eventType == EVENTTYPE_MESSAGE)
						info->rate += weight;
				}
				else break;
			}
		}

		m_cache.insert(info);
	}
}

HANDLE CContactCache::get(int rate)
{
	if (rate >= 0 && rate < m_cache.getCount())
		return m_cache[rate].hContact;
	return NULL;
}

float CContactCache::getWeight(int rate)
{
	if (rate >= 0 && rate < m_cache.getCount())
		return m_cache[rate].rate;
	return -1;
}

static bool AppendInfo(TCHAR *buf, int size, HANDLE hContact, int info)
{
	CONTACTINFO ci = {0};
	ci.cbSize = sizeof(ci);
	ci.hContact = hContact;
	ci.dwFlag = info;
	ci.dwFlag |= CNF_UNICODE;


	bool ret = false;

	if (!CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)&ci) && (ci.type == CNFT_ASCIIZ) && ci.pszVal)
	{
		if (*ci.pszVal && (lstrlen(ci.pszVal) < size-2))
		{
			lstrcpy(buf, ci.pszVal);
			ret = true;
		}
		mir_free(ci.pszVal);
	}

	return ret;
}

void CContactCache::TContactInfo::LoadInfo()
{
	if (infoLoaded) return;
	TCHAR *p = info;

	p[0] = p[1] = 0;

	static const int items[] = {
		CNF_FIRSTNAME, CNF_LASTNAME, CNF_NICK , CNF_CUSTOMNICK, CNF_EMAIL, CNF_CITY, CNF_STATE,
		CNF_COUNTRY, CNF_PHONE, CNF_HOMEPAGE, CNF_ABOUT, CNF_UNIQUEID, CNF_MYNOTES, CNF_STREET,
		CNF_CONAME, CNF_CODEPT, CNF_COCITY, CNF_COSTATE, CNF_COSTREET, CNF_COCOUNTRY
	};

	for (int i = 0; i < SIZEOF(items); ++i)
	{
		if (AppendInfo(p, SIZEOF(info) - (p - info), hContact, items[i]))
			p += lstrlen(p) + 1;
	}
	*p = 0;

	infoLoaded = true;
}

TCHAR *nb_stristr(TCHAR *str, TCHAR *substr)
{
	if (!substr || !*substr) return str;
	if (!str || !*str) return NULL;

	TCHAR *str_up = NEWTSTR_ALLOCA(str);
	TCHAR *substr_up = NEWTSTR_ALLOCA(substr);

	CharUpperBuff(str_up, lstrlen(str_up));
	CharUpperBuff(substr_up, lstrlen(substr_up));

	TCHAR* p = _tcsstr(str_up, substr_up);
	return p ? (str + (p - str_up)) : NULL;
}

bool CContactCache::filter(int rate, TCHAR *str)
{
	if (!str || !*str)
		return true;
	m_cache[rate].LoadInfo();

	HKL kbdLayoutActive = GetKeyboardLayout(GetCurrentThreadId());
	HKL kbdLayouts[10];
	int nKbdLayouts = GetKeyboardLayoutList(SIZEOF(kbdLayouts), kbdLayouts);

	TCHAR buf[256];
	BYTE keyState[256] = {0};

	for (int iLayout = 0; iLayout < nKbdLayouts; ++iLayout)
	{
		if (kbdLayoutActive == kbdLayouts[iLayout])
		{
			lstrcpy(buf, str);
		} else
		{
			int i;
			for (i = 0; str[i]; ++i)
			{
				UINT vk = VkKeyScanEx(str[i], kbdLayoutActive);
				UINT scan = MapVirtualKeyEx(vk, 0, kbdLayoutActive);
				ToUnicodeEx(vk, scan, keyState, buf+i, SIZEOF(buf)-i, 0, kbdLayouts[iLayout]);
			}
			buf[i] = 0;
		}

		for (TCHAR *p = m_cache[rate].info; p && *p; p = p + lstrlen(p) + 1)
			if (nb_stristr(p, buf))
				return true;
	}

	return false;
}