Beispiel #1
0
void DoStartupProcess() {
	if(checking) return;

	if(OldXMLDataExists(MC_PLUGINS) && OldXMLDataExists(MC_LOCALIZATION)) {
		if(options.check_on_startup) {
			SYSTEMTIME now;
			GetSystemTime(&now);

			if(options.check_once) {
				if(DBGetContactSettingWord(0, MODULE, "LastUpdateDay", 32) == 32) {
					CallService(MS_UPDATE_CHECKFORUPDATES, 0, 0);
				} else {
					if(now.wDay != DBGetContactSettingWord(0, MODULE, "LastUpdateDay", now.wDay)
						|| now.wMonth != DBGetContactSettingWord(0, MODULE, "LastUpdateMonth", now.wMonth)
						|| now.wYear != DBGetContactSettingWord(0, MODULE, "LastUpdateYear", now.wYear))
					{
						CallService(MS_UPDATE_CHECKFORUPDATES, 0, 0);
					} else {
						if(LoadOldXMLData(MC_PLUGINS, false)) {
							ScanPlugins(0, 0);
							FreeXMLData(MC_PLUGINS);
						}
						if(LoadOldXMLData(MC_LOCALIZATION, false)) {
							ScanLangpacks(0, 0);
							FreeXMLData(MC_LOCALIZATION);
						}

						RestoreStatus();
					}
				}
			} else {
				CallService(MS_UPDATE_CHECKFORUPDATES, 0, 0);
			}
		} else {
			if(LoadOldXMLData(MC_PLUGINS, false)) {
				ScanPlugins(0, 0);
				FreeXMLData(MC_PLUGINS);
			}
			if(LoadOldXMLData(MC_LOCALIZATION, false)) {
				ScanLangpacks(0, 0);
				FreeXMLData(MC_LOCALIZATION);
			}

			RestoreStatus();
		}
	} else {
		if(options.check_on_startup) CallService(MS_UPDATE_CHECKFORUPDATES, 0, 0);
		else RestoreStatus();
	}

}
bool UpdateXMLData(const Category cat, const char *redirect_url /*= 0*/, int recurse_count /*=0*/) {
	
	if(recurse_count > MAX_REDIRECT_RECURSE) {
		PUShowMessageT(TranslateT("Updater: Error getting data - too many redirects"), SM_WARNING);
		return false;
	}

	NETLIBHTTPREQUEST req = {0};
	NETLIBHTTPHEADER etag_hdr = {0};

	if(OldXMLDataExists(cat)) {
		// ensure backend not checked more than once every MIN_XMLDATA_AGE hours
		long age = OldXMLDataAge(cat);
		if(age >= 0 && age < MIN_XMLDATA_AGE)	{	// get it only if our file is at least 8 hours old
#ifdef DEBUG_HTTP_POPUPS
			char buff[512];
			sprintf(buff, "XML Data is recent (%d hours old) - not downloading, using local copy", age);
			PUShowMessage(buff, SM_NOTIFY);
#endif

			return LoadOldXMLData(cat, false);
		}

		// add ETag header for conditional get
		DBCONTACTGETSETTING cgs;
		DBVARIANT dbv;
		cgs.szModule = MODULE;
		char buff[256];
		strcpy(buff, "DataETag_");
		strcat(buff, category_files[cat]);
		cgs.szSetting = buff;
		cgs.pValue = &dbv;
		if(!CallService(MS_DB_CONTACT_GETSETTING, 0, (LPARAM)&cgs)) {
			req.headersCount = 1;
			req.headers = &etag_hdr;
			etag_hdr.szName = "If-None-Match";
			etag_hdr.szValue = _strdup(dbv.pszVal);
			DBFreeVariant(&dbv);
		}
	}

	req.cbSize = sizeof(req);
	req.requestType = REQUEST_GET;
	char URL[MAX_PATH];
	if(!redirect_url) {
		strcpy(URL, MIM_BACKEND_URL_PREFIX);
		strcat(URL, category_files[cat]);
		strcat(URL, ".bz2");
	} else {
		strcpy(URL, redirect_url);
	}
	req.szUrl = URL;
	req.flags = 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);
	free(etag_hdr.szValue);

	if (!resp) 
	{
		hNetlibHttp = NULL;
		if (!Miranda_Terminated())
		{
			int err = GetLastError();
			if (err) 
			{
				TCHAR buff[512];
				int len = mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download XML data: "));
				FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, err, 0, buff + len, 512 - len, 0);
				ShowError(buff);
				//MessageBox(0, buff + len, TranslateT("Updater: Error Downloading XML Data"), MB_OK | MB_ICONWARNING);
			} 
			else 
			{
				ShowError(TranslateT("Failed to download XML data - Response is NULL"));
				//MessageBox(0, TranslateT("Error downloading XML data...\nResponse is NULL"), TranslateT("Updater Error"), MB_OK | MB_ICONWARNING);
				NLog("Failed to download XML data - Response is NULL");
			}
		}
		return LoadOldXMLData(cat, false);
	} 
	else if (resp->resultCode == 304) { // 'Not Modified' response
		hNetlibHttp = resp->nlc;
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
#ifdef DEBUG_HTTP_POPUPS
		PUShowMessage("XML Data unchanged - using local copy", SM_NOTIFY);
#endif

		// mark data as current
		return LoadOldXMLData(cat, true);
	} else if(resp->resultCode >= 300 && resp->resultCode < 400) { // redirect response
		hNetlibHttp = resp->nlc;
		// 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 = UpdateXMLData(cat, resp->headers[i].szValue, recurse_count + 1);
				break;
			}
		}
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
		if(!ret) return LoadOldXMLData(cat, false);

		return ret;

	} else if(resp->resultCode != 200) {
		hNetlibHttp = resp->nlc;
		TCHAR buff[512];
		mir_sntprintf(buff, SIZEOF(buff), TranslateT("Failed to download XML data - Invalid response, code %d"), resp->resultCode);
		ShowError(buff);
		NLog(buff);
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
		return LoadOldXMLData(cat, false);
	}
		
	// resp->resultCode == 200
	hNetlibHttp = resp->nlc;

	if(!bz2_decompress_xml(resp->pData, resp->dataLength, &pData[cat], &dataLength[cat])) {
		ShowError(TranslateT("Failed to decompress XML data"));
		CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
		resp = 0;
		return LoadOldXMLData(cat, false);
	}

	// store date header and data for response 'Not Modified' (304) above
	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, "ETag") == 0) {
			//MessageBox(0, resp->headers[i].szValue, "Storing ETag", MB_OK);
			char buff[256];
			strcpy(buff, "DataETag_");
			strcat(buff, category_files[cat]);
			DBWriteContactSettingString(0, MODULE, buff, resp->headers[i].szValue);
		}
	}

	CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)resp);
	resp = 0;

	SaveXMLData(cat);

	doc[cat] = ezxml_parse_str((char*)pData[cat], strlen((char*)pData[cat]));
	if (!doc[cat]) return false;

	return true;
}