summaryrefslogtreecommitdiff
path: root/updater/socket.cpp
blob: 7e5db538575b4e42a72713f1bbd31bb135cb6f37 (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
#include "common.h"
#include "socket.h"

/*
#include <string>

typedef struct SettingsProcEnumParam_tag {
	int file_id;
	std::list<std::string> *list;
	char *current_filename;
	char *temp_folder;
} SettingsProcEnumParam;

int SettingsEnumProc(const char *szSetting,LPARAM lParam) {
	SettingsProcEnumParam *param = (SettingsProcEnumParam *)lParam;

	if(strcmp(szSetting, param->current_filename) != 0) {
		int file_id = DBGetContactSettingDword(0, MODULE, szSetting, -1);
		if(file_id == param->file_id) {
			std::string new_file_name(param->temp_folder);
			new_file_name += "\\";
			new_file_name += szSetting;
			param->list->push_back(new_file_name);
		}
	}

	return 0;
}
*/


void ShowAndLogError(TCHAR *message) {
}

bool GetFile(char *url, TCHAR *temp_folder, char *plugin_name, char *version, bool dlls_only, int recurse_count /*=0*/) {
	if(recurse_count > MAX_REDIRECT_RECURSE) {
		NLog("GetFile: error, too many redirects");
		return false;
	}

	TCHAR save_file[MAX_PATH];

	if(url == 0 || temp_folder == 0 || plugin_name == 0)
		return false;

	// ensure temp_folder exists
	if(!CreatePath(options.temp_folder)) {
		DWORD err = GetLastError();
		char buff[128];
		sprintf(buff, "GetFile: error creating temp folder, code %d", (unsigned)err);
		NLog(buff);
		return false;
	}

	// ensure zip_folder exists, if necessary
	if(options.save_zips && !CreatePath(options.zip_folder)) {
		DWORD err = GetLastError();
		char buff[128];
		sprintf(buff, "GetFile: error creating zip folder, code %d", (unsigned)err);
		NLog(buff);
		return false;
	}

	_tcscpy(save_file, temp_folder);
	_tcscat(save_file, _T("\\"));
	TCHAR *temp_str = GetTString(plugin_name);
	_tcscat(save_file, temp_str);
	free(temp_str);
	if(version) {
		temp_str = GetTString(version);
		_tcscat(save_file, _T("_"));
		_tcscat(save_file, temp_str);
		free(temp_str);
	}
	// copt extension from url
	char *ext = strrchr(url, '.');
	if(ext && *ext && strcmp(ext, ".dll") == 0) {
		_tcscat(save_file, _T(".dll"));
	} else { // default to zip extension (e.g. miranda fl)
		_tcscat(save_file, _T(".zip"));
		ext = ".zip";
	}

	// replace version text in URL
	char tmp_url[1024];
	if (version != NULL) {
		char *p;
		size_t pos = 0;
		size_t version_len = strlen(version);
		while ((p = strstr(url, "%VERSION%")) != NULL && (p - url + version_len < sizeof(tmp_url) - 1)) {
			strncpy(&tmp_url[pos], url, p - url);
			pos += p - url;
			strcpy(&tmp_url[pos], version);
			pos += version_len;
			url += p - url + 9; // 9 == strlen("%VERSION%")
		}
		if (strlen(url) < sizeof(tmp_url) - 1) {
			strcpy(&tmp_url[pos], url);
			pos += strlen(url);
		}
		tmp_url[pos] = 0;
		url = tmp_url;
	}


	NETLIBHTTPREQUEST req = {0};

	req.cbSize = sizeof(req);
	req.requestType = REQUEST_GET;
	req.szUrl = url;
	req.flags = NLHRF_NODUMP;

	NETLIBHTTPREQUEST *resp = (NETLIBHTTPREQUEST *)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&req);

	if(resp) {
		if(resp->resultCode == 200) {
			HANDLE hSaveFile = CreateFile(save_file, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
			if(hSaveFile != INVALID_HANDLE_VALUE) {
				unsigned long bytes_written = 0;
				if(WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL) == TRUE) {
					CloseHandle(hSaveFile);
					CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
					resp = 0;

					if(ext && *ext && strcmp(ext, ".zip") == 0) {

						if(!options.no_unzip) {
							char *ans_save_file = GetAString(save_file), *ans_temp_folder = GetAString(temp_folder);
							//MessageBoxA(0, ans_temp_folder, ans_save_file, MB_OK);
							unzip_file(ans_save_file, ans_temp_folder); 
							free(ans_save_file);
							free(ans_temp_folder);
						}
												
						if(options.save_zips) {
							TCHAR save_archive[MAX_PATH];
							_tcscpy(save_archive, options.zip_folder);
							_tcscat(save_archive, _T("\\"));
							_tcscat(save_archive, save_file + _tcslen(temp_folder) + 1);

							DeleteFile(save_archive);
							if(!MoveFile(save_file, save_archive)) {
								char buff[128];
								sprintf(buff, "GetFile: could not move file, code: %d", (unsigned)GetLastError());
								NLog(buff);
								if(!DeleteFile(save_file)) {
									char buff[128];
									sprintf(buff, "GetFile: error deleting file, code: %d", (unsigned)GetLastError());
									NLog(buff);
								}
							}

						} else {
							if(!DeleteFile(save_file)) {
								char buff[128];
								sprintf(buff, "GetFile: error deleting file, code: %d", (unsigned)GetLastError());
								NLog(buff);
							}
						}
						
						if(dlls_only) {
							NLog("Deleting non-dlls");
							DeleteNonDlls(temp_folder);
						}

					}
					return true;

				} else {
					char buff[128];
					sprintf(buff, "GetFile: error writing file, code %d", (unsigned)GetLastError());
					NLog(buff);
				}
				CloseHandle(hSaveFile);
			} else {
				char buff[128];
				sprintf(buff, "GetFile: error creating file, code %d", (unsigned)GetLastError());
				NLog(buff);
			}
		} else if(resp->resultCode >= 300 && resp->resultCode < 400) {
			// get new location
			bool ret = false;
			for(int i = 0; i < resp->headersCount; i++) {
				//MessageBox(0, resp->headers[i].szValue, resp->headers[i].szName, MB_OK);
				if(strcmp(resp->headers[i].szName, "Location") == 0) {
					ret = GetFile(resp->headers[i].szValue, temp_folder, plugin_name, version, dlls_only, recurse_count + 1);
					break;
				}
			}
			CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
			resp = 0;
			return ret;
		} else {
			TCHAR buff[1024];
			_stprintf(buff, TranslateT("Failed to download \"%s\" - Invalid response, code %d"), plugin_name, resp->resultCode);

			ShowError(buff);
			char *ts = GetAString(buff);
			NLog(ts);
			free(ts);

		}
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
	} else {
		int err = GetLastError();
		if(err) {
			TCHAR buff[1024];
			_stprintf(buff, TranslateT("Failed to download \"%s\": "), plugin_name);
			int len = _tcslen(buff);
			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0, buff + len, 512 - len, 0);

			ShowError(buff);
			char *ts = GetAString(buff);
			NLog(ts);
			free(ts);
		}
	}

	return false;
}

char *CheckVersionURL(char *url, BYTE *pbPrefixBytes, int cpbPrefixBytes, BYTE *pbVersionBytes, int cpbVersionBytes) {

	/*
	bool check = false;

	if(strncmp((char *)pbPrefixBytes, "My Details ", cpbPrefixBytes) == 0) {
		//MessageBox(0, "Checking version URL", "Updater -> My Details", MB_OK);
		check = true;
	}
	*/

	if(url == 0 || pbPrefixBytes == 0 || cpbPrefixBytes == 0 || pbVersionBytes == 0 || cpbVersionBytes == 0)
		return 0;

	/*
	if(check) {
		MessageBox(0, "Real URL check", "CheckVersionURL", MB_OK);
		MessageBox(0, url, "URL", MB_OK);
		MessageBox(0, (char *)pbPrefixBytes, "Prefix", MB_OK);
		MessageBox(0, (char *)pbVersionBytes, "Version", MB_OK);
	}
	*/

	NETLIBHTTPREQUEST req = {0};

	req.cbSize = sizeof(req);
	req.requestType = REQUEST_GET;
	req.szUrl = url;
	req.flags = NLHRF_DUMPASTEXT; //NLHRF_SMARTREMOVEHOST | NLHRF_SMARTAUTHHEADER;

	NETLIBHTTPREQUEST *resp = (NETLIBHTTPREQUEST_tag *)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&req);

	if(resp) {
		if(resp->resultCode == 200) {
			// find the location of the prefix
			int index, index2;
			for(int i = 0; i < resp->dataLength; i++) {
				index =  0;
				while(index < cpbPrefixBytes && i + index < resp->dataLength && (BYTE)resp->pData[i + index] == pbPrefixBytes[index]) index++;
				if(index == cpbPrefixBytes) {
					// we found the prefix - now compare the following bytes
					// i + index the first byte after the prefix
					index2 = 0;
					while(index2 + i + index < resp->dataLength && index2 < cpbVersionBytes && resp->pData[i + index + index2] == pbVersionBytes[index2]) index2++;
					if(index2 == cpbVersionBytes) {
						// same version as current
						//if(check)
							//MessageBox(0, "same version", "check url", MB_OK);
						CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
						resp = 0;
						return 0;
					} else {
						DWORD ver_current, ver_potential;
						char *buff = (char *)malloc(cpbVersionBytes + 1);
						strncpy(buff, (char *)pbVersionBytes, cpbVersionBytes);
						buff[cpbVersionBytes] = 0;

						// this is safe because pData finishes with a zero always (according to m_netlib.h docs)
						if(VersionFromString(buff, &ver_current) && VersionFromString(&resp->pData[i + index], &ver_potential)) 
						{
							switch(options.ver_req) {
							case VR_MAJOR:
								ver_current &= 0xFF000000;
								ver_potential &= 0xFF000000;
								break;
							case VR_MINOR:
								ver_current &= 0xFFFF0000;
								ver_potential &= 0xFFFF0000;
								break;
							case VR_RELEASE:
								ver_current &= 0xFFFFFF00;
								ver_potential &= 0xFFFFFF00;
								break;
							case VR_BUILD:
								break;
							}
							//if(check) MessageBox(0, (char *)resp->pData[i + index], buff, MB_OK);
							free(buff);
							CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
							resp = 0;
							// we can covert the versions to DWORDS, so compare...
							if(ver_current < ver_potential) {
								char buff2[16];
								CreateVersionString(ver_potential, buff2);
								return _strdup(buff2);
							} else
								return 0;
						} else { // invalid version(s), but different from current - assume it's an update
							free(buff);
							CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
							resp = 0;
							return _strdup(Translate("Yes"));
						}
					}
				}
			}
		} else if(resp->resultCode == 302) { // redirect
			char *ret = 0;
			// get new location
			for(int i = 0; i < resp->headersCount; i++) {
				//MessageBox(0, resp->headers[i].szValue, resp->headers[i].szName, MB_OK);
				if(strcmp(resp->headers[i].szName, "Location") == 0) {
					ret = CheckVersionURL(resp->headers[i].szValue, pbPrefixBytes, cpbPrefixBytes, pbVersionBytes, cpbVersionBytes);
					break;
				}
			}
			CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
			resp = 0;

			return ret;
		} else {
			char buff[128];
			sprintf(buff, "CheckVersionURL: error, http result code %d", resp->resultCode);
			NLog(buff);
		}
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
	} else {
		int err = GetLastError();
		if(err) {
			char buff[128];
			sprintf(buff, "CheckVersionURL: error code %d", err);
			NLog(buff);
			return 0;
		}
	}

	return 0;
}

char *UpdateRequired(UpdateInternal *update_internal, bool *beta) {
	// determine whether update is required

	char *ret = 0, *ret_beta = 0;

	if(options.use_xml_backend && update_internal->file_id != -1) {
		if(update_internal->cat == MC_UNKNOWN) {
			if(XMLDataAvailable(MC_PLUGINS) 
				&& (ret = FindVersion(update_internal->file_id, update_internal->update.pbVersion, update_internal->update.cpbVersion, MC_PLUGINS)))
			{
				update_internal->cat = MC_PLUGINS;
				if(strcmp(ret, "same") == 0) {
					free(ret);
					ret = 0;
				}
			} else if(XMLDataAvailable(MC_LOCALIZATION) 
				&& (ret = FindVersion(update_internal->file_id, update_internal->update.pbVersion, update_internal->update.cpbVersion, MC_LOCALIZATION)))
			{
				update_internal->cat = MC_LOCALIZATION;
				if(strcmp(ret, "same") == 0) {
					free(ret);
					ret =  0;
				}
			}
		} else {
			ret = FindVersion(update_internal->file_id, update_internal->update.pbVersion, update_internal->update.cpbVersion, update_internal->cat);
			if(ret && strcmp(ret, "same") == 0) {
				free(ret);
				ret = 0;
			}
		}
	} else {
		ret = CheckVersionURL(update_internal->update.szVersionURL, update_internal->update.pbVersionPrefix,
			update_internal->update.cpbVersionPrefix, update_internal->update.pbVersion, update_internal->update.cpbVersion);
	}

	if(update_internal->update_options.use_beta) {
		ret_beta = CheckVersionURL(update_internal->update.szBetaVersionURL, update_internal->update.pbBetaVersionPrefix,
			update_internal->update.cpbBetaVersionPrefix, update_internal->update.pbVersion, update_internal->update.cpbVersion);
	}

	if(ret && !ret_beta) {
		if(beta) *beta = false;
		return ret;
	} else if(!ret && ret_beta) {
		if(beta) *beta = true;
		return ret_beta;
	} else if(ret && ret_beta) {
		// find highest version of ret and ret_beta

		//MessageBox(0, ret, ret_beta, MB_OK);

		DWORD vRet, vRetBeta;
		VersionFromString(ret, &vRet);
		VersionFromString(ret_beta, &vRetBeta);

		if(vRetBeta > vRet) {
			free(ret);
			if(beta) *beta = true;
			return ret_beta;
		} else {
			free(ret_beta);
			if(beta) *beta = false;
			return ret;
		}
	}

	return 0;
}