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

void unzip_mem(char* buf, int len, TCHAR* dest);

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)) {
		NLogF("GetFile: error creating temp folder, code %u", GetLastError());
		return false;
	}

	// ensure zip_folder exists, if necessary
	if(options.save_zips && !CreatePath(options.zip_folder)) {
		NLogF("GetFile: error creating zip folder, code %u", GetLastError());
		return false;
	}

	TCHAR *temp_str = GetTString(plugin_name);
	mir_sntprintf(save_file, SIZEOF(save_file), _T("%s\\%s"), temp_folder, temp_str);
	mir_free(temp_str);
	if (version) 
	{
		temp_str = GetTString(version);
		_tcscat(save_file, _T("_"));
		_tcscat(save_file, temp_str);
		mir_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 | NLHRF_HTTP11;
	req.nlc = hNetlibHttp;

	if (CallService(MS_SYSTEM_GETVERSION, 0, 0) >= PLUGIN_MAKE_VERSION(0,9,0,5))
		req.flags |= NLHRF_PERSISTENT | NLHRF_REDIRECT;

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

	if (resp) 
	{
		hNetlibHttp = resp->nlc;
		if (resp->resultCode == 200) 
		{
			if (ext && *ext && _stricmp(ext, ".zip") == 0) 
			{
				if (!options.no_unzip) 
					unzip_mem(resp->pData, resp->dataLength, temp_folder); 
										
				if (options.save_zips) 
				{
					TCHAR save_archive[MAX_PATH];
					mir_sntprintf(save_archive, SIZEOF(save_archive), _T("%s%s"), options.zip_folder, _tcsrchr(save_file, '\\'));

					HANDLE hSaveFile = CreateFile(save_archive, GENERIC_WRITE, FILE_SHARE_WRITE, 0, 
						CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
					if (hSaveFile != INVALID_HANDLE_VALUE) 
					{
						unsigned long bytes_written;
						WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL); 
						CloseHandle(hSaveFile);
					}
					else
						NLogF("GetFile: error creating file, code %u", GetLastError());
				} 
				
				if(dlls_only) 
				{
					NLog("Deleting non-dlls");
					DeleteNonDlls(temp_folder);
				}

			}
			else
			{
				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;
					WriteFile(hSaveFile, resp->pData, resp->dataLength, &bytes_written, NULL); 
					CloseHandle(hSaveFile);
				}
				else
					NLogF("GetFile: error creating file, code %u", GetLastError());
			}

			CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
			return true;
		} 
		else if (resp->resultCode >= 300 && resp->resultCode < 400) 
		{
			// get new location
			bool ret = false;
			for (int i = 0; i < resp->headersCount; i++) 
			{
				if (_stricmp(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);
			return ret;
		} 
		else 
		{
			TCHAR buff[1024], *tmp;
			mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download \"%s\" - Invalid response, code %d"), (tmp = mir_a2t(plugin_name)), resp->resultCode);
			ShowError(buff);
			mir_free(tmp);
		}
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
	} 
	else 
	{
		hNetlibHttp = NULL;
		int err = GetLastError();
		if (err && !Miranda_Terminated()) 
		{
			TCHAR buff[1024], *tmp;
			int len = mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download \"%s\": "), (tmp = mir_a2t(plugin_name)));
			FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0, buff + len, 512 - len, 0);
			ShowError(buff);
			mir_free(tmp);
		}
	}

	return false;
}

char *CheckVersionURL(char *url, BYTE *pbPrefixBytes, int cpbPrefixBytes, BYTE *pbVersionBytes, int cpbVersionBytes)
{
	if (url == 0 || pbPrefixBytes == 0 || cpbPrefixBytes == 0 || pbVersionBytes == 0 || cpbVersionBytes == 0)
		return 0;

	char *ret = NULL;
	NETLIBHTTPREQUEST req = {0};

	req.cbSize = sizeof(req);
	req.requestType = REQUEST_GET;
	req.szUrl = url;
	req.flags = NLHRF_DUMPASTEXT | NLHRF_HTTP11;
	req.nlc = hNetlibHttp;

	if (CallService(MS_SYSTEM_GETVERSION, 0, 0) >= PLUGIN_MAKE_VERSION(0,9,0,5))
		req.flags |= NLHRF_PERSISTENT | NLHRF_REDIRECT;

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

	if (resp) 
	{
		hNetlibHttp = resp->nlc;
		if (resp->resultCode == 200) 
		{
			// find the location of the prefix
			char* ver = (char*)memmem(resp->pData, resp->dataLength, pbPrefixBytes, cpbPrefixBytes);
			if (ver)
			{
				int len = resp->dataLength - ((ver += cpbPrefixBytes) - resp->pData);
				if (cpbVersionBytes <= len && memcmp(ver, pbVersionBytes, cpbVersionBytes) == 0)
				{
					// same version as current
					CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
					return 0;
				} 
				else 
				{
					DWORD ver_current, ver_potential;
					char *buff = (char *)alloca(cpbVersionBytes + 1);
					memcpy(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(ver, &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;
						}

						// we can covert the versions to DWORDS, so compare...
						if (ver_current < ver_potential) 
						{
							char buff2[16];
							CreateVersionString(ver_potential, buff2);
							ret = _strdup(buff2);
						} 
					} 
					else  // invalid version(s), but different from current - assume it's an update
						ret = _strdup(Translate("Yes"));
				}
			}
		} 
		else if (resp->resultCode == 302) // redirect
		{
			// get new location
			for (int i = 0; i < resp->headersCount; i++) 
			{
				if(_stricmp(resp->headers[i].szName, "Location") == 0) 
				{
					ret = CheckVersionURL(resp->headers[i].szValue, pbPrefixBytes, cpbPrefixBytes, pbVersionBytes, cpbVersionBytes);
					break;
				}
			}
		} 
		else 
			NLogF("CheckVersionURL: error, http result code %d", resp->resultCode);

		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
	} 
	else 
	{
		hNetlibHttp = NULL;
		int err = GetLastError();
		if (err) 
			NLogF("CheckVersionURL: error code %d", err);
	}

	return ret;
}

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

    char *ret = NULL, *ret_beta = NULL;

	if (options.use_xml_backend && update_internal.file_id != -1) 
	{
		const char * res;
		if (update_internal.cat == MC_UNKNOWN) 
		{
			if (XMLDataAvailable(MC_PLUGINS) && 
				(res = FindVersion(update_internal.file_id, update_internal.update.pbVersion, update_internal.update.cpbVersion, MC_PLUGINS)))
			{
				update_internal.cat = MC_PLUGINS;
				if (strcmp(res, "same")) ret = _strdup(res);
			} 
			else if (XMLDataAvailable(MC_LOCALIZATION) && 
				(res = FindVersion(update_internal.file_id, update_internal.update.pbVersion, update_internal.update.cpbVersion, MC_LOCALIZATION)))
			{
				update_internal.cat = MC_LOCALIZATION;
				if (strcmp(res, "same")) ret = _strdup(res);
			}
		} 
		else 
		{
			res = FindVersion(update_internal.file_id, update_internal.update.pbVersion, update_internal.update.cpbVersion, update_internal.cat);
			if (res && strcmp(res, "same")) ret = _strdup(res);
		}
	} 
	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

		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;
}