summaryrefslogtreecommitdiff
path: root/plugins/WebView/src/webview_services.cpp
blob: 277683ea74ea1a621d6ea71d7e5f51cf31651735 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* A plugin for Miranda IM which displays web page text in a window Copyright 
* (C) 2005 Vincent Joyce.
* 
* Miranda IM: the free icq client for MS Windows  Copyright (C) 2000-2
* Richard Hughes, Roland Rabien & Tristan Van de Vreede
* 
* 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"
#include "webview.h"

static int searchId = -1;

/*****************************************************************************/
static char szInvalidChars[] = { '\\', '/', ':', '*', '?', '\"', '<', '>', '|' };

int DBSettingChanged(WPARAM wParam, LPARAM lParam)
{
	// We can't upload changes to NULL contact
	MCONTACT hContact = wParam;
	if (hContact == NULL)
		return 0;

	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING *) lParam;
	if (!strcmp(cws->szModule, "CList")) {
		int invalidpresent = 0;

		char *szProto = GetContactProto(hContact);
		if (szProto == nullptr || strcmp(szProto, MODULENAME))
			return 0;

		// A contact is renamed
		if (!strcmp(cws->szSetting, "MyHandle")) {
			ptrW oldName( db_get_wsa(hContact, MODULENAME, PRESERVE_NAME_KEY));
			if (oldName == NULL)
				return 0;

			wchar_t nick[100];
			ptrW oldnick( db_get_wsa(hContact, "CList", "MyHandle"));
			if (oldnick != NULL)
				wcsncpy_s(nick, oldnick, _TRUNCATE);
			else
				nick[0] = 0;

			for (int i=0; i < _countof(szInvalidChars); i++ ) {
				wchar_t *p = wcschr(nick, szInvalidChars[i]);
				if (p != nullptr) {
					WErrorPopup((UINT_PTR)"ERROR", TranslateT("Invalid symbol present in contact name."));
					*p = '_';
					invalidpresent =1;
				}
			}

			if (invalidpresent) {
				srand((unsigned)time(nullptr));
				wchar_t ranStr[7];
				_itow((int)10000 *rand() / (RAND_MAX + 1.0), ranStr, 10);
				mir_wstrcat(nick, ranStr); 
			}  

			if ( wcschr(nick, '(') == nullptr) {
				db_set_ws(hContact, MODULENAME, PRESERVE_NAME_KEY, nick);
				db_set_ws(hContact, MODULENAME, "Nick", nick);
				db_set_ws(hContact, "CList", "MyHandle", nick);
			}

			// TEST GET NAME FOR CACHE
			wchar_t cachepath[MAX_PATH], cachedirectorypath[MAX_PATH];
			GetModuleFileName(g_hInstance, cachepath, _countof(cachepath));
			wchar_t *cacheend = wcsrchr(cachepath, '\\');
			cacheend++;
			*cacheend = '\0';
			mir_snwprintf(cachedirectorypath, L"%s" _A2W(MODULENAME) L"cache\\", cachepath);
			CreateDirectory(cachedirectorypath, nullptr);

			wchar_t newcachepath[MAX_PATH + 50], renamedcachepath[MAX_PATH + 50];
			mir_snwprintf(newcachepath, L"%s" _A2W(MODULENAME) L"cache\\%s.txt", cachepath, oldName);
			mir_snwprintf(renamedcachepath, L"%s" _A2W(MODULENAME) L"cache\\%s.txt", cachepath, nick);

			// file exists?
			if ( _waccess(newcachepath, 0) != -1) {
				FILE *pcachefile = _wfopen(newcachepath, L"r");
				if (pcachefile != nullptr) {
					fclose(pcachefile);
					if (mir_wstrcmp(newcachepath, renamedcachepath)) {
						MoveFile(newcachepath, renamedcachepath);
						db_set_ws(hContact, MODULENAME, CACHE_FILE_KEY, renamedcachepath);
					}
				}
			}
		}
	}
	return 0;
}

/*****************************************************************************/
int SiteDeleted(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = wParam;
	if (mir_strcmp(GetContactProto(hContact), MODULENAME))
		return 0;

	ptrW contactName( db_get_wsa(hContact, MODULENAME, PRESERVE_NAME_KEY));

	// TEST GET NAME FOR CACHE
	wchar_t cachepath[MAX_PATH], cachedirectorypath[MAX_PATH], newcachepath[MAX_PATH + 50];
	GetModuleFileName(g_hInstance, cachepath, _countof(cachepath));
	wchar_t *cacheend = wcsrchr(cachepath, '\\');
	cacheend++;
	*cacheend = '\0';

	mir_snwprintf(cachedirectorypath, L"%s" _A2W(MODULENAME) L"cache\\", cachepath);
	CreateDirectory(cachedirectorypath, nullptr);
	mir_snwprintf(newcachepath, L"%s" _A2W(MODULENAME) L"cache\\%s.txt", cachepath,  contactName);
	// file exists?
	if ( _waccess(newcachepath, 0) != -1) {
		FILE *pcachefile = _wfopen(newcachepath, L"r");
		if (pcachefile != nullptr) {
			fclose(pcachefile);
			DeleteFile(newcachepath);
			db_set_s(hContact, MODULENAME, CACHE_FILE_KEY, "");
		}
	}
	return 0;
}

/*****************************************************************************/
INT_PTR OpenCacheDir(WPARAM, LPARAM)
{
	//GET NAME FOR CACHE
	wchar_t cachepath[MAX_PATH], cachedirectorypath[MAX_PATH];
	GetModuleFileName(g_hInstance, cachepath, _countof(cachepath));
	wchar_t *cacheend = wcsrchr(cachepath, '\\');
	cacheend++;
	*cacheend = '\0';

	mir_snwprintf(cachedirectorypath, L"%s" _A2W(MODULENAME) L"cache\\%s", cachepath, cacheend);

	if( _waccess(cachedirectorypath, 0) != 0)
		WErrorPopup((UINT_PTR)"ERROR", TranslateT("Cache folder does not exist."));
	else
		ShellExecute(nullptr, L"open", cachedirectorypath, nullptr, nullptr, SW_SHOWNORMAL);
	return 0;
}

/*****************************************************************************/
INT_PTR PingWebsiteMenuCommand(WPARAM wParam, LPARAM)
{
	FILE *pfile = fopen("psite.bat", "r");
	if (pfile == nullptr) {
		WErrorPopup((UINT_PTR)"ERROR", TranslateT("Missing \"psite.bat\" file."));
		return 0;
	}

	ptrW url( db_get_wsa(wParam, MODULENAME, "URL"));
	if (url == NULL)
		return 0;

	wchar_t Cnick[200], *Oldnick;
	wcsncpy(Cnick, url, _countof(Cnick));
	if ((Oldnick = wcsstr(Cnick, L"://")) != nullptr)
		Oldnick += 3;
	else 
		Oldnick = Cnick;

	wchar_t *Nend = wcschr(Oldnick, '/');
	if (Nend) *Nend = '\0';

	ShellExecute(nullptr, L"open", L"psite.bat", Oldnick, nullptr, SW_HIDE);
	return 0;
}

/*****************************************************************************/
INT_PTR StpPrcssMenuCommand(WPARAM wParam, LPARAM)
{
	db_set_b(wParam, MODULENAME, STOP_KEY, 1);  
	return 0;
}

//=======================================================
// GetCaps
// =======================================================

INT_PTR GetCaps(WPARAM wParam, LPARAM)
{
	switch(wParam)
	{
	case PFLAGNUM_1:
		return PF1_BASICSEARCH | PF1_ADDSEARCHRES | PF1_VISLIST;
	case PFLAGNUM_2:
		return db_get_b(NULL, MODULENAME, HIDE_STATUS_ICON_KEY, 0) ? 0 : (PF2_ONLINE | PF2_SHORTAWAY | PF2_LONGAWAY | PF2_LIGHTDND | PF2_HEAVYDND); 
	case PFLAGNUM_3:
		return 0;
	case PFLAGNUM_4:
		return PF4_NOCUSTOMAUTH | PF4_NOAUTHDENYREASON;
	case PFLAGNUM_5:
		return PF2_INVISIBLE|PF2_SHORTAWAY|PF2_LONGAWAY|PF2_LIGHTDND|PF2_HEAVYDND|PF2_FREECHAT|PF2_OUTTOLUNCH|PF2_ONTHEPHONE;
	case PFLAG_UNIQUEIDTEXT:
		return (INT_PTR)Translate("Site URL");
	default:
		return 0;
	}
}

// =======================================================
// GetName
// =======================================================

INT_PTR GetName(WPARAM wParam, LPARAM lParam)
{
	mir_strncpy((char*) lParam, MODULENAME, wParam);
	return 0;
}

//=======================================================
// SetStatus
// =======================================================

INT_PTR SetStatus(WPARAM wParam, LPARAM)
{
	int oldStatus = bpStatus;

	if (wParam == ID_STATUS_ONLINE)
		wParam = ID_STATUS_ONLINE;
	else if (wParam == ID_STATUS_OFFLINE)
		wParam = ID_STATUS_OFFLINE;
	else
		wParam = ID_STATUS_ONLINE;

	// broadcast the message
	bpStatus = wParam;

	ProtoBroadcastAck(MODULENAME, NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE) oldStatus, wParam);

	// Make sure no contact has offline status for any reason on first time run     
	if ( db_get_b(NULL, MODULENAME, "FirstTime", 100) == 100) {
		for (auto &hContact : Contacts(MODULENAME))
			db_set_w(hContact, MODULENAME, "Status", ID_STATUS_ONLINE);

		db_set_b(NULL, MODULENAME, "FirstTime", 1);
	}

	db_set_b(NULL, MODULENAME, OFFLINE_STATUS, bpStatus == ID_STATUS_OFFLINE);
	return 0;
}

//=======================================================
// GetStatus
// =======================================================

INT_PTR GetStatus(WPARAM, LPARAM)
{
	if (bpStatus == ID_STATUS_ONLINE)
		return ID_STATUS_ONLINE;
	if (bpStatus == ID_STATUS_AWAY)
		return ID_STATUS_AWAY;
	if (bpStatus == ID_STATUS_NA)
		return ID_STATUS_NA;
	if (bpStatus == ID_STATUS_OCCUPIED)
		return ID_STATUS_OCCUPIED;
	if (bpStatus == ID_STATUS_DND)
		return ID_STATUS_DND;
	return ID_STATUS_OFFLINE;
}

//=======================================================
// BPLoadIcon
// =======================================================

INT_PTR BPLoadIcon(WPARAM wParam, LPARAM)
{
	UINT id;

	switch (wParam & 0xFFFF) {
	case PLI_PROTOCOL:
		id = IDI_SITE;
		break;
	default:
		return 0;
	}
	return (INT_PTR)LoadImage(g_hInstance, MAKEINTRESOURCE(id), IMAGE_ICON,
		GetSystemMetrics(wParam & PLIF_SMALL ? SM_CXSMICON : SM_CXICON),
		GetSystemMetrics(wParam & PLIF_SMALL ? SM_CYSMICON : SM_CYICON), 0);
}

/*****************************************************************************/
static void __cdecl BasicSearchTimerProc(void *pszNick)
{
	PROTOSEARCHRESULT psr = { sizeof(psr) };
	psr.flags = PSR_UNICODE;
	psr.nick.w = (wchar_t*)pszNick;

	// broadcast the search result
	ProtoBroadcastAck(MODULENAME, NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)&psr);
	ProtoBroadcastAck(MODULENAME, NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);

	// exit the search
	searchId = -1;
	mir_free(psr.nick.w);
}

INT_PTR BasicSearch(WPARAM, LPARAM lParam)
{
	if (searchId != -1)
		return 0; // only one search at a time

	searchId = 1;

	// create a thread for the ID search
	mir_forkthread(BasicSearchTimerProc, mir_wstrdup((const wchar_t*)lParam));
	return searchId;
}

/*****************************************************************************/
INT_PTR AddToList(WPARAM, LPARAM lParam)
{
	PROTOSEARCHRESULT *psr = (PROTOSEARCHRESULT *) lParam;
	DBVARIANT dbv;
	int sameurl = 0;
	int samename = 0;

	if (psr == nullptr)
		return 0;
	if (psr->nick.w == nullptr) {
		WErrorPopup((UINT_PTR)"ERROR", TranslateT("Please select site in Find/Add contacts..."));
		return 0;
	}   
	// if contact with the same ID was not found, add it
	if (psr->cbSize != sizeof(PROTOSEARCHRESULT))
		return NULL;
	// search for existing contact
	for (auto &hContact : Contacts(MODULENAME)) {
		// check ID to see if the contact already exist in the database
		if (db_get_ws(hContact, MODULENAME, "URL", &dbv))
			continue;
		if (!mir_wstrcmpi(psr->nick.w, dbv.ptszVal)) {
			// remove the flag for not on list and hidden, thus make the
			// contact visible
			// and add them on the list
			sameurl ++;
			if (db_get_b(hContact, "CList", "NotOnList", 1)) {
				db_unset(hContact, "CList", "NotOnList");
				db_unset(hContact, "CList", "Hidden");
			}
		}
		db_free(&dbv);
	}

	MCONTACT hContact = db_add_contact();
	Proto_AddToContact(hContact, MODULENAME);

	/////////write to db
	db_set_b(hContact, MODULENAME, ON_TOP_KEY, 0);
	db_set_b(hContact, MODULENAME, DBLE_WIN_KEY, 1);
	db_set_s(hContact, MODULENAME, END_STRING_KEY, "");
	db_set_b(hContact, MODULENAME, RWSPACE_KEY, 1);

	//Convert url into a name for contact
	wchar_t Cnick[255];
	if (psr->nick.w != nullptr)
		wcsncpy(Cnick, psr->nick.w, _countof(Cnick));
	else
		Cnick[0] = 0;

	wchar_t *Oldnick = wcsstr(Cnick, L"://");
	if (Oldnick != nullptr)
		Oldnick += 3;
	else
		Oldnick = Cnick;

	wchar_t *Newnick = wcsstr(Oldnick, L"www.");
	if (Newnick != nullptr)
		Newnick += 4;
	else {
		Newnick = wcsstr(Oldnick, L"WWW.");
		if (Newnick != nullptr)
			Newnick += 4;
		else
			Newnick = Oldnick;
	}

	wchar_t *Nend = wcschr(Newnick, '.');
	if (Nend) *Nend = '\0';

	for (auto &hContact2 : Contacts(MODULENAME)) {
		if (!db_get_ws(hContact2, MODULENAME, PRESERVE_NAME_KEY, &dbv)) {
			if (!mir_wstrcmpi(Newnick, dbv.ptszVal)) {
				// remove the flag for not on list and hidden, thus make the
				// contact visible
				// and add them on the list
				samename++;
				if (db_get_b(hContact2, "CList", "NotOnList", 1)) {
					db_unset(hContact2, "CList", "NotOnList");
					db_unset(hContact2, "CList", "Hidden");
				}
				db_free(&dbv);
			}
		}
		db_free(&dbv);
	}

	if ((sameurl > 0) || (samename > 0)) // contact has the same url or name as another contact, add rand num to name
	{
		srand((unsigned) time(nullptr));
		
		wchar_t ranStr[10];
		_itow((int) 10000 *rand() / (RAND_MAX + 1.0), ranStr, 10);
		mir_wstrcat(Newnick, ranStr);
	}
	//end convert

	db_set_ws(hContact, "CList", "MyHandle", Newnick);
	db_set_ws(hContact, MODULENAME, PRESERVE_NAME_KEY, Newnick);
	db_set_ws(hContact, MODULENAME, "Nick", Newnick);
	db_set_b(hContact, MODULENAME, CLEAR_DISPLAY_KEY, 1);
	db_set_s(hContact, MODULENAME, START_STRING_KEY, "");
	db_set_ws(hContact, MODULENAME, URL_KEY, psr->nick.w);
	db_set_ws(hContact, MODULENAME, "Homepage", psr->nick.w);
	db_set_b(hContact, MODULENAME, U_ALLSITE_KEY, 1);
	db_set_w(hContact, MODULENAME, "Status", ID_STATUS_ONLINE);

	// ignore status change
	db_set_dw(hContact, "Ignore", "Mask", 8);

	Sleep(2);

	db_free(&dbv);


	return (INT_PTR)hContact;
}

/*****************************************************************************/
INT_PTR GetInfo(WPARAM, LPARAM)
{
	mir_forkthread(AckFunc, nullptr);
	return 1;
}

/*****************************************************************************/
void AckFunc(void*)
{
	for (auto &hContact : Contacts(MODULENAME))
		ProtoBroadcastAck(MODULENAME, hContact, ACKTYPE_GETINFO, ACKRESULT_SUCCESS, (HANDLE)1, 0);
}