summaryrefslogtreecommitdiff
path: root/plugins/HistorySweeperLight/src/historysweeperlight.cpp
blob: 5ed98ea38590ee272e9c8f16c6a68acb270faebf (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Miranda IM History Sweeper Light plugin
Copyright (C) 2002-2003  Sergey V. Gershovich
Copyright (C) 2006-2009  Boris Krasnovskiy
Copyright (C) 2010, 2011 tico-tico

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; either version 2
of the License, or (at your option) any later version.

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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

typedef struct
{
	time_t time;
	int keep;
} CriteriaStruct;

// build criteria
time_t BuildCriteria(int dwti)
{
	time_t tim = time(0);

	switch (dwti) {
	case 0:
		tim -= 1 * 24 * 3600;
		break;
	case 1:
		tim -= 3 * 24 * 3600;
		break;
	case 2:
		tim -= 7 * 24 * 3600;
		break;
	case 3:
		tim -= 14 * 24 * 3600;
		break;
	case 4:
		tim -= 30 * 24 * 3600;
		break;
	case 5:
		tim -= 90 * 24 * 3600;
		break;
	case 6:
		tim -= 180 * 24 * 3600;
		break;
	default:
		tim -= 365 * 24 * 3600;
	}
	return tim;
}

// keep criteria
int KeepCriteria(int dwti)
{
	switch (dwti) {
	case 0:
		return 1;
	case 1:
		return 2;
	case 2:
		return 5;
	case 3:
		return 10;
	case 4:
		return 20;
	default:
		return 50;
	}
}

struct BEventData
{
	MEVENT hDBEvent;
	uint32_t CRC32;
	uint32_t Timestamp;
};

static int CompareBookmarks(const void* p1, const void* p2)
{
	return (uint8_t*)((BEventData*)p1)->hDBEvent - (uint8_t*)((BEventData*)p2)->hDBEvent;
}

static void GetBookmarks(MCONTACT hContact, BEventData** books, size_t* bookcnt)
{
	*books = nullptr;
	*bookcnt = 0;

	DBVARIANT dbv;
	if (db_get(hContact, "HistoryPlusPlus", "Bookmarks", &dbv) == 0) {
		if (dbv.cpbVal > 2 && *(uint16_t*)dbv.pbVal >= sizeof(BEventData)) {
			size_t recSize = *(uint16_t*)dbv.pbVal;
			size_t numb = (dbv.cpbVal - sizeof(uint16_t)) / recSize;

			BEventData* tbooks = (BEventData*)mir_alloc(sizeof(BEventData)*numb);
			size_t tbookcnt = 0;

			size_t i;
			uint8_t* evs = dbv.pbVal + sizeof(uint16_t);

			for (i = 0; i < numb; ++i) {
				tbooks[tbookcnt++] = *(BEventData*)evs;
				evs += recSize;
			}
			if (tbookcnt != 0) {
				qsort(tbooks, tbookcnt, sizeof(BEventData), CompareBookmarks);
				*bookcnt = tbookcnt;
				*books = tbooks;
			}
			else mir_free(tbooks);
		}

		db_free(&dbv);
	}
}

//Sweep history from specified contact
void SweepHistoryFromContact(MCONTACT hContact, CriteriaStruct Criteria, BOOL keepUnread)
{
	int lPolicy;
	if (hContact == NULL) // for system history
		lPolicy = g_plugin.getByte("SweepSHistory", 0);
	else							// for contact history (or "SweepHistory" - default action)
		lPolicy = g_plugin.getByte(hContact, "SweepHistory", g_plugin.getByte("SweepHistory", 0));

	if (lPolicy == 0) // nothing to do
		return;

	int eventsCnt = db_event_count(hContact);
	if (eventsCnt == 0)
		return;

	BOOL unsafe = g_plugin.getByte("UnsafeMode", 0);
	BEventData *books, *item, ev = { 0 };
	size_t bookcnt, btshift;

	SetCursor(LoadCursor(nullptr, IDC_WAIT));

	// switch off SAFETYMODE if necessary
	if (unsafe)
		db_set_safety_mode(FALSE);

	GetBookmarks(hContact, &books, &bookcnt);

	// Get first event
	DB::ECPTR pCursor(DB::Events(hContact));
	while (MEVENT hDBEvent = pCursor.FetchNext()) {
		DBEVENTINFO dbei = {};
		db_event_get(hDBEvent, &dbei);

		// should we stop processing?
		// lPolicy == 1 - for time criterion, lPolicy == 2 - keep N last events, lPolicy == 3 - delete all events
		if ((lPolicy == 1 && (unsigned)Criteria.time < dbei.timestamp) || (lPolicy == 2 && Criteria.keep > --eventsCnt)) break;

		bool doDelete = true;
		if (!(dbei.flags & (DBEF_SENT | DBEF_READ)) && keepUnread)
			doDelete = false;	// keep unread events

		if (bookcnt != 0) { // keep bookmarks
			ev.hDBEvent = hDBEvent;
			item = (BEventData*)bsearch(&ev, books, bookcnt, sizeof(BEventData), CompareBookmarks);
			if (item != nullptr && item->Timestamp == dbei.timestamp) {
				doDelete = FALSE;
				btshift = (--bookcnt - (item - books)) * sizeof(BEventData);
				if (btshift)
					memmove(item, item + 1, btshift);
			}
		}

		if (doDelete)
			pCursor.DeleteEvent();
	}

	mir_free(books);

	// switch ON safety mode as fast as we can to avoid  DB corruption
	if (unsafe)
		db_set_safety_mode(TRUE);

	SetCursor(LoadCursor(nullptr, IDC_ARROW));
}

// shutdown action
void ShutdownAction(void)
{
	CriteriaStruct Criteria;
	Criteria.keep = KeepCriteria(g_plugin.getByte("StartupShutdownKeep"));
	Criteria.time = BuildCriteria(g_plugin.getByte("StartupShutdownOlder"));

	SweepHistoryFromContact(NULL, Criteria, FALSE);				// sweep system history, keepunread==0

	for (auto &hContact : Contacts())
		SweepHistoryFromContact(hContact, Criteria, TRUE);		// sweep contact history, keepunread==1
}

int OnWindowEvent(WPARAM uType, LPARAM lParam)
{
	auto *pDlg = (CSrmmBaseDialog *)lParam;

	switch (uType) {
	case MSG_WINDOW_EVT_OPENING:
		g_hWindows.insert(PVOID(pDlg->m_hContact));
		SetSrmmIcon(pDlg->m_hContact);
		break;

	case MSG_WINDOW_EVT_CLOSE:
		if (g_plugin.getByte("SweepOnClose", 0)) {
			CriteriaStruct Criteria;

			Criteria.keep = KeepCriteria(g_plugin.getByte("StartupShutdownKeep"));
			Criteria.time = BuildCriteria(g_plugin.getByte("StartupShutdownOlder"));

			SweepHistoryFromContact(pDlg->m_hContact, Criteria, TRUE);
		}

		for (auto &it : g_hWindows.rev_iter())
			if (it == PVOID(pDlg->m_hContact))
				g_hWindows.removeItem(&it);
		break;
	}

	return 0;
}

void SetSrmmIcon(MCONTACT hContact)
{
	int sweep = g_plugin.getByte(hContact, "SweepHistory");

	for (int i = 0; i < 4; i++)
		Srmm_SetIconFlags(hContact, MODULENAME, i, (sweep == i) ? 0 : MBF_HIDDEN);
}