VersionCheck.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author: Ben Allison [email protected]
  6. ** Created:
  7. **/
  8. #include "Main.h"
  9. #include "resource.h"
  10. #include "api.h"
  11. #include "language.h"
  12. #include "..\Components\wac_network\wac_network_http_receiver_api.h"
  13. #include "api/service/waServiceFactory.h"
  14. #include "Browser.h"
  15. #include "../nu/AutoUrl.h"
  16. #include "../nu/threadname.h"
  17. #include "stats.h"
  18. #include "../nu/threadpool/TimerHandle.hpp"
  19. #include "../WAT/WAT.h"
  20. extern UpdateBrowser *updateBrowser;
  21. class VersionCheckCallback : public ifc_downloadManagerCallback
  22. {
  23. public:
  24. void OnInit(DownloadToken token)
  25. {
  26. api_httpreceiver *http = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  27. if (http)
  28. {
  29. http->AllowCompression();
  30. http->addheader("Accept: */*");
  31. }
  32. }
  33. int IsCharDigit(char digit)
  34. {
  35. WORD type=0;
  36. GetStringTypeExA(LOCALE_USER_DEFAULT, CT_CTYPE1, &digit, 1, &type);
  37. return type&C1_DIGIT;
  38. }
  39. void OnFinish(DownloadToken token)
  40. {
  41. api_httpreceiver *http = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  42. if (http && http->getreplycode() == 200)
  43. {
  44. char *buf;
  45. size_t size;
  46. if (WAC_API_DOWNLOADMANAGER->GetBuffer(token, (void **)&buf, &size) == 0)
  47. {
  48. // buf[size] = 0;
  49. char *p = buf;
  50. while (size && (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n'))
  51. {
  52. p++;
  53. size--;
  54. }
  55. char newVer[6] = {0,};
  56. if (size >= 3 && p[1] == '.')
  57. {
  58. size_t i = 0;
  59. while (size && i != 6 && (i == 1 || IsCharDigit(p[i])))
  60. {
  61. newVer[i] = p[i];
  62. newVer[i + 1] = 0;
  63. size--;
  64. i++;
  65. }
  66. p += i;
  67. while (size && (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n'))
  68. {
  69. size--;
  70. p++;
  71. }
  72. char curVer_tmp[ 6 ] = APP_VERSION;
  73. char curVer[ 6 ] = { 0 };
  74. i = 0;
  75. size_t j = 0;
  76. bool l_has_point = false;
  77. while ( i != 6 && j != 6 )
  78. {
  79. if ( IsCharDigit( curVer_tmp[ j ] ) )
  80. {
  81. curVer[ i ] = curVer_tmp[ j ];
  82. i++;
  83. }
  84. j++;
  85. if ( !IsCharDigit( curVer_tmp[ j ] ) && !l_has_point )
  86. {
  87. curVer[ i ] = curVer_tmp[ j ];
  88. l_has_point = true;
  89. i++;
  90. j++;
  91. }
  92. }
  93. while (lstrlenA(curVer) < 5)
  94. StringCchCatA(curVer, 6, "0");
  95. while (lstrlenA(newVer) < 5)
  96. StringCchCatA(newVer, 6, "0");
  97. int verDif = strcmp(newVer, curVer);
  98. //#if defined(BETA) || defined(NIGHTLY)
  99. // if (verDif == 0)
  100. // verDif = 1; // if this is a BETA version, then we should upgrade if the versions are equal
  101. //#endif
  102. if (verDif == 0) // same version
  103. {
  104. char updateNumber[32] = "";
  105. char *u = updateNumber;
  106. while (size && u != (updateNumber + 31) && *p && *p != '\r' && *p != '\n')
  107. {
  108. size--;
  109. *u++ = *p++;
  110. *u = 0;
  111. }
  112. int update = atoi(updateNumber);
  113. if (update > config_newverchk3)
  114. {
  115. if (config_newverchk3) // only display update if we've already established a serial #
  116. verDif = 1;
  117. config_newverchk3 = update;
  118. }
  119. }
  120. if (verDif > 0) // same version or older
  121. {
  122. while (size && (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n'))
  123. {
  124. size--;
  125. p++;
  126. }
  127. if (size)
  128. {
  129. char *disp = (char *)calloc(size + 1, sizeof(char));
  130. memcpy(disp, p, size);
  131. disp[size]=0;
  132. if (!_strnicmp(p, "http://", 7))
  133. {
  134. PostMessageW(hMainWindow, WM_WA_IPC, (WPARAM)disp, IPC_UPDATE_URL);
  135. }
  136. else
  137. {
  138. if (MessageBoxA(NULL, disp, getString(IDS_WINAMP_UPDATE_MSG,NULL,0), MB_YESNO) == IDYES)
  139. {
  140. wa::strings::wa_string l_url_new_version( disp );
  141. myOpenURL( NULL, l_url_new_version.GetW().c_str() );
  142. }
  143. free(disp);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. config_newverchk = getDay();
  151. }
  152. void OnError(DownloadToken token)
  153. {
  154. config_newverchk = getDay();
  155. }
  156. RECVS_DISPATCH;
  157. };
  158. #define CBCLASS VersionCheckCallback
  159. START_DISPATCH;
  160. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit )
  161. VCB( IFC_DOWNLOADMANAGERCALLBACK_ONFINISH, OnFinish )
  162. END_DISPATCH;
  163. #undef CBCLASS
  164. static VersionCheckCallback versionCheckCallback;
  165. static void CheckVersion(int allowedChannel = 0)
  166. {
  167. if ( WAC_API_DOWNLOADMANAGER )
  168. {
  169. char url[ 1024 ] = { 0 };
  170. char *urlend = 0;
  171. size_t urlend_size = 0;
  172. wa::strings::wa_string l_winamp_product_ver( STR_WINAMP_PRODUCTVER );
  173. l_winamp_product_ver.replaceAll( ",", "." );
  174. StringCchPrintfExA( url, 1024, &urlend, &urlend_size, 0, "http://client.winamp.com/update/latest-version.php?v=%s", l_winamp_product_ver.GetA().c_str() );
  175. char uid[ 512 ] = "";
  176. stats_getuidstr( uid );
  177. if ( uid[ 0 ] )
  178. StringCchPrintfExA( urlend, urlend_size, &urlend, &urlend_size, 0, "&ID=%s", uid );
  179. const wchar_t *langIdentifier = langManager ? ( langManager->GetLanguageIdentifier( LANG_IDENT_STR ) ) : 0;
  180. if ( langIdentifier )
  181. StringCchPrintfA( urlend, urlend_size, "&lang=%s", (char *)AutoUrl( langIdentifier ) );
  182. OSVERSIONINFOEX info;
  183. ZeroMemory( &info, sizeof( OSVERSIONINFOEX ) );
  184. info.dwOSVersionInfoSize = sizeof( OSVERSIONINFOEX );
  185. GetVersionEx( (LPOSVERSIONINFO)&info );
  186. char l_os_version[ 32 ];
  187. sprintf( l_os_version, "&osver=%u.%u.%u", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber );
  188. strcat( url, l_os_version );
  189. char szAllowedChannel[32];
  190. sprintf(szAllowedChannel, "&allowedchannel=%d", allowedChannel);
  191. strcat(url, szAllowedChannel);
  192. WAC_API_DOWNLOADMANAGER->DownloadEx( url, &versionCheckCallback, api_downloadManager::DOWNLOADEX_BUFFER );
  193. }
  194. }
  195. bool DoVerChk(int verchk)
  196. {
  197. return verchk == 1 || (verchk > 1 && verchk + 1 < (int)getDay());
  198. }
  199. class PingCallback : public ifc_downloadManagerCallback
  200. {
  201. public:
  202. void OnInit(DownloadToken token)
  203. {
  204. api_httpreceiver *http = WAC_API_DOWNLOADMANAGER->GetReceiver(token);
  205. if (http)
  206. http->addheader("Accept: */*");
  207. }
  208. RECVS_DISPATCH;
  209. };
  210. #define CBCLASS PingCallback
  211. START_DISPATCH;
  212. VCB(IFC_DOWNLOADMANAGERCALLBACK_ONINIT, OnInit)
  213. END_DISPATCH;
  214. #undef CBCLASS
  215. static PingCallback pingCallback;
  216. void Ping(const char *url)
  217. {
  218. if (WAC_API_DOWNLOADMANAGER)
  219. WAC_API_DOWNLOADMANAGER->DownloadEx(url, &pingCallback, api_downloadManager::DOWNLOADEX_BUFFER);
  220. }
  221. void newversioncheck(void)
  222. {
  223. if (isInetAvailable())
  224. {
  225. if (DoVerChk(config_newverchk))
  226. {
  227. // go ahead and call this on the main thread to ensure that the GUID gets created w/o a race condition
  228. char uid[512]="";
  229. stats_getuidstr(uid);
  230. CheckVersion(config_newverchk_rc);
  231. }
  232. if (DoVerChk(config_newverchk2))
  233. {
  234. char _url[MAX_URL] = {0};
  235. char *url=_url;
  236. size_t urlsize=MAX_URL;
  237. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"http://client.winamp.com/update/client_session.php?v=%s",APP_VERSION);
  238. char uid[512]="";
  239. stats_getuidstr(uid);
  240. if (uid[0])
  241. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"&ID=%s", uid);
  242. int values[Stats::NUM_STATS] = {0, };
  243. stats.GetStats(values);
  244. for (int x = 0; x < Stats::NUM_STATS; x ++)
  245. {
  246. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"&st%d=%d", x + 1, values[x]);
  247. }
  248. wchar_t stat_str[256] = {0};
  249. stats.GetString("skin", stat_str, 256);
  250. if (stat_str[0])
  251. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"&skin=%s",(char *)AutoUrl(stat_str));
  252. stats.GetString("colortheme", stat_str, 256);
  253. if (stat_str[0])
  254. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"&ct=%s",(char *)AutoUrl(stat_str));
  255. stats.GetString("pmp", stat_str, 256);
  256. if (stat_str[0])
  257. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0,"&pmp=%s",(char *)AutoUrl(stat_str));
  258. const wchar_t *langIdentifier = langManager?(langManager->GetLanguageIdentifier(LANG_IDENT_STR)):0;
  259. if (langIdentifier)
  260. StringCchPrintfExA(url, urlsize, &url, &urlsize, 0, "&lang=%s", (char *)AutoUrl(langIdentifier));
  261. Ping(_url);
  262. config_newverchk2 = getDay();
  263. }
  264. }
  265. }