1
0

ini.inc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. /**--------------------------------------------------------------------------**\
  2. ===================================
  3. y_users - Registration functions.
  4. ===================================
  5. Description:
  6. Provides access to a user system for registering and saving users.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI malloc include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 2.2
  43. Changelog:
  44. 02/02/13:
  45. Added bits and join timestamp to preload data.
  46. Added backwards compatible salting to the hash.
  47. 15/11/11:
  48. Added comments.
  49. Added language to the top-level player data (needed for login).
  50. 11/11/11:
  51. First version.
  52. Functions:
  53. Public
  54. -
  55. Core:
  56. -
  57. Stock:
  58. -
  59. Static:
  60. -
  61. Inline:
  62. -
  63. API:
  64. -
  65. Callbacks:
  66. -
  67. Definitions:
  68. -
  69. Enums:
  70. -
  71. Macros:
  72. -
  73. Tags:
  74. -
  75. Variables:
  76. Global:
  77. -
  78. Static:
  79. -
  80. Commands:
  81. -
  82. Compile options:
  83. -
  84. Operators:
  85. -
  86. </remarks>
  87. \**--------------------------------------------------------------------------**/
  88. loadtext core[ysi_players];
  89. enum E_USER_PRELOAD
  90. {
  91. E_USER_PRELOAD_YID,
  92. Language:E_USER_PRELOAD_LANG,
  93. E_USER_PRELOAD_PASS[MAX_PASSWORD_LENGTH + 1],
  94. E_USER_PRELOAD_BITS,
  95. E_USER_PRELOAD_DATE
  96. }
  97. static stock
  98. YSI_g_sPlayerIndexFile,
  99. INI:YSI_g_sPlayerWriteFile = INI_NO_FILE,
  100. YSI_g_sPlayerYID[MAX_PLAYERS] = {-2, ...},
  101. //YSI_g_sCallbacks,
  102. YSI_g_sPreloadData[MAX_PLAYERS][E_USER_PRELOAD];
  103. //#define Player_GetIndexFile(%0,%1) ((YSI_g_sPlayerIndexFile=%1[0]),%0[sizeof(%0)-5]=(('a'<=(YSI_g_sPlayerIndexFile|0x20)<='z')?(YSI_g_sPlayerIndexFile|0x20):(('0'<=YSI_g_sPlayerIndexFile<='9')?('0'):('_'))))
  104. #define Player_GetIndexFile(%0,%1) ((YSI_g_sPlayerIndexFile=%1),%0[USER_FILE_LENGTH+4]=(('a'<=(YSI_g_sPlayerIndexFile|0x20)<='z')?(YSI_g_sPlayerIndexFile|0x20):(('0'<=YSI_g_sPlayerIndexFile<='9')?('0'):('_'))))
  105. //foreign Player_DoLogout(playerid, uid);
  106. /**--------------------------------------------------------------------------**\
  107. <summary>Player_Reload</summary>
  108. <param name="playerid">Player who changed name.</param>
  109. <returns>
  110. -
  111. </returns>
  112. <remarks>
  113. Reload a player's basic data when they change name.
  114. </remarks>
  115. \**--------------------------------------------------------------------------**/
  116. stock Player_Reload(playerid)
  117. {
  118. new
  119. name[MAX_PLAYER_NAME];
  120. GetPlayerName(playerid, name, sizeof (name));
  121. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  122. }
  123. stock Player_SetPreload(playerid, data[E_USER_PRELOAD])
  124. {
  125. if (0 <= playerid < MAX_PLAYERS)
  126. {
  127. YSI_g_sPreloadData[playerid] = data;
  128. }
  129. }
  130. /**--------------------------------------------------------------------------**\
  131. <summary>Player_Preload</summary>
  132. <param name="playerid">Player who is logging in.</param>
  133. <returns>
  134. -
  135. </returns>
  136. <remarks>
  137. Loads a player's data to an array.
  138. </remarks>
  139. \**--------------------------------------------------------------------------**/
  140. #if _YSI_PLUGINS_MYSQL == 7
  141. public _Player_Preload(ret[E_USER_PRELOAD])
  142. {
  143. // Need to use some clever AMX hacking to pass the "ret" array...
  144. }
  145. #endif
  146. stock Player_Preload(string:name[], ret[E_USER_PRELOAD])
  147. {
  148. // First, find the player's file. This should be the ONLY place where the
  149. // password is to be loaded.
  150. P:4("Player_Preload called: %s", name);
  151. ret[E_USER_PRELOAD_YID] = -2;
  152. memset(ret[E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  153. ret[E_USER_PRELOAD_LANG] = NO_LANGUAGE;
  154. ret[E_USER_PRELOAD_BITS] = 0;
  155. ret[E_USER_PRELOAD_DATE] = 0;
  156. new
  157. namelen = strlen(name),
  158. filename[] = USER_FILE_PATH "ind_X.YSI",
  159. File:fIndex;
  160. Player_GetIndexFile(filename, name[0]);
  161. fIndex = fopen(filename, io_read);
  162. if (fIndex)
  163. {
  164. P:5("Player_Preload: fIndex OK");
  165. new
  166. line[INDEX_DATA_LINE_LENGTH],
  167. len;
  168. while ((len = fread(fIndex, line)))
  169. {
  170. P:6("Player_Preload: while");
  171. //new
  172. // len;
  173. //len = strlen(line);
  174. // Check if the line is the right length (could be one of three
  175. // lengths depending on newlines). Skip blanks.
  176. if (len < INDEX_DATA_LINE_LENGTH - 3)
  177. {
  178. continue;
  179. }
  180. P:6("Player_Preload: Not len");
  181. // Check the name on the line.
  182. if (!strcmp(line[MAX_INDEX_LENGTH + 1], name, false, namelen) && line[MAX_INDEX_LENGTH + 1 + namelen] == ' ')
  183. {
  184. P:6("Player_Preload: checked name");
  185. // Found the section on this one player.
  186. //P:6("Player_Preload: check pass: %s ?= %s", hash, line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1]);
  187. // Save the loaded data.
  188. line[MAX_INDEX_LENGTH] = '\0';
  189. //printf("line: %s", line);
  190. ret[E_USER_PRELOAD_YID] = strval(line);
  191. //printf("%d %d %d", ret[E_USER_PRELOAD_YID], strval(line), strval("00000022"));
  192. //printf("%d", strval(line));
  193. line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2] = '\0';
  194. ret[E_USER_PRELOAD_LANG] = Langs_GetLanguage(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1]);
  195. strcat(ret[E_USER_PRELOAD_PASS], line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1], MAX_PASSWORD_LENGTH + 1);
  196. // Load the 32 extra "bits".
  197. ret[E_USER_PRELOAD_BITS] = hexstr(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2 + 1]);
  198. // Load the user registration date (if they have one).
  199. ret[E_USER_PRELOAD_DATE] = hexstr(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2 + 1 + 8 + 1]);
  200. P:6("Player_Preload: %s %d %d %x %x", ret[E_USER_PRELOAD_PASS], ret[E_USER_PRELOAD_YID], _:ret[E_USER_PRELOAD_LANG], ret[E_USER_PRELOAD_BITS], ret[E_USER_PRELOAD_DATE]);
  201. fclose(fIndex);
  202. return 1;
  203. }
  204. }
  205. fclose(fIndex);
  206. }
  207. else if (fexist(filename))
  208. {
  209. P:E("Error reading index %c.", filename[sizeof (filename) - 6]);
  210. return -1;
  211. }
  212. ret[E_USER_PRELOAD_YID] = -1;
  213. return 0;
  214. }
  215. stock Language:Player_GetPreloadLanguage(playerid)
  216. {
  217. return Language:YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG];
  218. }
  219. stock Player_GetPreloadBits(playerid)
  220. {
  221. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS];
  222. }
  223. static remotefunc void:_Player_SetPreloadBits(playerid, bits)
  224. {
  225. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] = bits;
  226. }
  227. stock Player_SetPreloadBits(playerid, bits)
  228. {
  229. broadcastfunc _Player_SetPreloadBits(playerid, bits);
  230. Player_RewritePreload(playerid);
  231. }
  232. stock Player_GetPreloadDate(playerid)
  233. {
  234. if (0 <= YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] < 1234567890)
  235. {
  236. return -1;
  237. }
  238. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE];
  239. }
  240. stock Player_IsRegistered(playerid)
  241. {
  242. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] != -1;
  243. }
  244. hook OnScriptInit()
  245. {
  246. switch (ftouch(USER_FILE_PATH "index.YSI"))
  247. {
  248. case -1:
  249. {
  250. P:E(USER_FILE_PATH "index.YSI does not exist and couldn't be created.");
  251. }
  252. case 1:
  253. {
  254. if (Player_CreateNewID())
  255. {
  256. // Don't need upgrades.
  257. if (ftouch(USER_FILE_PATH "y_users_v2_1.YSI") == -1)
  258. {
  259. P:E("Could not touch upgrade file 1.");
  260. // Use "else" so we don't try do upgrade 2 without upgrade
  261. // 1. That would really mess up the files (that don't yet
  262. // exist, but could do).
  263. }
  264. else if (ftouch(USER_FILE_PATH "y_users_v2_2.YSI") == -1)
  265. {
  266. P:E("Could not touch upgrade file 2.");
  267. }
  268. }
  269. else
  270. {
  271. P:E(USER_FILE_PATH "index.YSI does not exist and couldn't be created.");
  272. }
  273. }
  274. }
  275. // NOT using "ALS" for chaining.
  276. /*if (funcidx("OnPlayerLogin") != -1)
  277. {
  278. YSI_g_sCallbacks |= 1;
  279. }
  280. if (funcidx("OnPlayerLogout") != -1)
  281. {
  282. YSI_g_sCallbacks |= 2;
  283. }*/
  284. }
  285. public OnGameModeInit()
  286. {
  287. if (YSI_FILTERSCRIPT)
  288. {
  289. //if (YSI_g_sCallbacks & 16)
  290. //{
  291. //Users_OnGameModeInit();
  292. #if defined Users_OnGameModeInit
  293. Users_OnGameModeInit();
  294. #endif
  295. //}
  296. }
  297. else
  298. {
  299. #if defined Users_OnGameModeInit
  300. Users_OnGameModeInit();
  301. #endif
  302. //Users_OnGameModeInit();
  303. Users_DoUpgrade0();
  304. Users_DoUpgrade1();
  305. }
  306. return 1;
  307. }
  308. #if defined _ALS_OnGameModeInit
  309. #undef OnGameModeInit
  310. #else
  311. #define _ALS_OnGameModeInit
  312. #endif
  313. #if defined Users_OnGameModeInit
  314. forward Users_OnGameModeInit();
  315. #endif
  316. #define OnGameModeInit(%0) Users_OnGameModeInit(%0)
  317. public OnFilterScriptInit()
  318. {
  319. //if (funcidx("Users_OnGameModeInit") != -1)
  320. //{
  321. // YSI_g_sCallbacks |= 16;
  322. //}
  323. // DO ALL (MOST) OTHER INITS FIRST. ENSURE WE COME LATER.
  324. //Users_OnFilterScriptInit();
  325. #if defined Users_OnFilterScriptInit
  326. Users_OnFilterScriptInit();
  327. #endif
  328. Users_DoUpgrade0();
  329. Users_DoUpgrade1();
  330. return 1;
  331. }
  332. #if defined _ALS_OnFilterScriptInit
  333. #undef OnFilterScriptInit
  334. #else
  335. #define _ALS_OnFilterScriptInit
  336. #endif
  337. #if defined Users_OnFilterScriptInit
  338. forward Users_OnFilterScriptInit();
  339. #endif
  340. #define OnFilterScriptInit(%0) Users_OnFilterScriptInit(%0)
  341. static stock Users_DoUpgrade0()
  342. {
  343. // Only ever do this upgrade once.
  344. switch (ftouch(USER_FILE_PATH "y_users_v2_1.YSI"))
  345. {
  346. case -1:
  347. {
  348. P:E("Could not upgrade user files.");
  349. }
  350. case 1:
  351. {
  352. #if !defined YSI_DO_USER_UPGRADE
  353. P:E("y_users needs to upgrade user files. Please back up the old ones and recompile/rerun once with YSI_DO_USER_UPGRADE.");
  354. fremove(USER_FILE_PATH "y_users_v2_1.YSI");
  355. #else
  356. // Do the upgrade to add languages to all files (could take a
  357. // little while, but needs doing).
  358. P:I("Please wait, upgrading user files.");
  359. new
  360. ch,
  361. filename[] = USER_FILE_PATH "ind_X.YSI",
  362. File:f,
  363. File:g,
  364. line[INDEX_DATA_LINE_LENGTH],
  365. //Language:def = Langs_GetLanguageAt(0),
  366. code[3];
  367. strcpy(code, Langs_GetCode(Langs_GetLanguageAt(0)));
  368. for (new i = -2; i != 26; ++i)
  369. {
  370. if (i == -2)
  371. {
  372. ch = '_';
  373. }
  374. else if (i == -1)
  375. {
  376. ch = '0';
  377. }
  378. else
  379. {
  380. ch = 'a' + i;
  381. }
  382. Player_GetIndexFile(filename, ch);
  383. f = fopen(filename, io_read);
  384. if (!f)
  385. {
  386. if (fexist(filename))
  387. {
  388. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  389. }
  390. continue;
  391. }
  392. g = ftemp();
  393. if (!g)
  394. {
  395. fclose(f);
  396. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  397. continue;
  398. }
  399. while ((ch = fread(f, line)))
  400. {
  401. //ch = strlen(line);
  402. if (ch > 3)
  403. {
  404. if (line[ch - 2] < ' ')
  405. {
  406. // "/r/n" or "/n/r".
  407. // Copy the existing line ending.
  408. line[ch + 1] = line[ch - 2];
  409. line[ch + 2] = line[ch - 1];
  410. line[ch - 2] = ' ';
  411. line[ch - 1] = code[0];
  412. line[ch ] = code[1];
  413. line[ch + 3] = '\0';
  414. //line[ch - 1] = '\0';
  415. fwrite(g, line);
  416. }
  417. else
  418. {
  419. // "/n" or "/r".
  420. // Copy the existing line ending.
  421. line[ch + 2] = line[ch - 1];
  422. line[ch - 1] = ' ';
  423. line[ch ] = code[0];
  424. line[ch + 1] = code[1];
  425. line[ch + 2] = '\0';
  426. //line[ch - 1] = '\0';
  427. fwrite(g, line);
  428. }
  429. }
  430. }
  431. fseek(g);
  432. fclose(f);
  433. fremove(filename);
  434. f = fopen(filename, io_write);
  435. if (!f)
  436. {
  437. fclose(g);
  438. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  439. continue;
  440. }
  441. while (fread(g, line))
  442. {
  443. fwrite(f, line);
  444. }
  445. fclose(f);
  446. fclose(g);
  447. }
  448. f = fopen(USER_FILE_PATH "index.YSI", io_append);
  449. if (f)
  450. {
  451. fwrite(f, " ");
  452. fclose(f);
  453. }
  454. else
  455. {
  456. P:E("Upgrade index failed.");
  457. }
  458. P:I("Upgrade complete.");
  459. #endif
  460. }
  461. }
  462. }
  463. static stock Users_DoUpgrade1()
  464. {
  465. // Only ever do this upgrade once.
  466. switch (ftouch(USER_FILE_PATH "y_users_v2_2.YSI"))
  467. {
  468. // case 0: Already exists.
  469. case -1:
  470. {
  471. P:E("Could not upgrade user files.");
  472. }
  473. case 1:
  474. {
  475. #if !defined YSI_DO_USER_UPGRADE
  476. P:E("y_users needs to upgrade user files. Please back up the old ones and recompile/rerun once with YSI_DO_USER_UPGRADE.");
  477. fremove(USER_FILE_PATH "y_users_v2_2.YSI");
  478. #else
  479. // Do the upgrade to add languages to all files (could take a
  480. // little while, but needs doing).
  481. P:I("Please wait, upgrading user files.");
  482. new
  483. ch,
  484. hash[MAX_PASSWORD_LENGTH + MAX_INDEX_LENGTH + 1],
  485. filename[] = USER_FILE_PATH "ind_X.YSI",
  486. File:f,
  487. File:g,
  488. line[INDEX_DATA_LINE_LENGTH],
  489. //Language:def = Langs_GetLanguageAt(0),
  490. code[3];
  491. strcpy(code, Langs_GetCode(Langs_GetLanguageAt(0)));
  492. for (new i = -2; i != 26; ++i)
  493. {
  494. if (i == -2)
  495. {
  496. ch = '_';
  497. }
  498. else if (i == -1)
  499. {
  500. ch = '0';
  501. }
  502. else
  503. {
  504. ch = 'a' + i;
  505. }
  506. Player_GetIndexFile(filename, ch);
  507. f = fopen(filename, io_read);
  508. if (!f)
  509. {
  510. if (fexist(filename))
  511. {
  512. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  513. }
  514. continue;
  515. }
  516. g = ftemp();
  517. if (!g)
  518. {
  519. fclose(f);
  520. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  521. continue;
  522. }
  523. while (fread(f, line))
  524. {
  525. ch = strlen(line);
  526. if (ch > 3)
  527. {
  528. // Get the current hash.
  529. strcpy(hash, line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1], MAX_PASSWORD_LENGTH + 1);
  530. //hash[MAX_PASSWORD_LENGTH] = '\0';
  531. // Append their ID (not QUITE their join time, but
  532. // best we can do retrospectively).
  533. //line[MAX_INDEX_LENGTH] = '\0';
  534. strcat(hash, line, sizeof (hash));
  535. //line[MAX_INDEX_LENGTH] = ' ';
  536. //hash[MAX_PASSWORD_LENGTH + MAX_INDEX_LENGTH] = '\0';
  537. //printf("HASH 1: %s", hash);
  538. Player_HashPass(hash, hash);
  539. //printf("HASH 2: %s", hash);
  540. // Copy the correct line ending.
  541. if (line[ch - 2] < ' ')
  542. {
  543. format(line, sizeof (line), "%.*s %s %.2s 00000000 %.8s%s", MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME, line, hash, line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1], line[0], line[ch - 2]);
  544. //format(line[ch - 2], 21, " 00000000 %.*s%s", MAX_INDEX_LENGTH, line[0], line[ch - 2]);
  545. }
  546. else
  547. {
  548. format(line, sizeof (line), "%.*s %s %.2s 00000000 %.8s%s", MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME, line, hash, line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1], line[0], line[ch - 1]);
  549. //format(line[ch - 1], 21, " 00000000 %.*s%s", MAX_INDEX_LENGTH, line[0], line[ch - 1]);
  550. }
  551. fwrite(g, line);
  552. }
  553. }
  554. fseek(g);
  555. fclose(f);
  556. fremove(filename);
  557. f = fopen(filename, io_write);
  558. if (!f)
  559. {
  560. fclose(g);
  561. P:E("Upgrade %c failed.", filename[USER_FILE_LENGTH + 4]);
  562. continue;
  563. }
  564. while (fread(g, line))
  565. {
  566. fwrite(f, line);
  567. }
  568. fclose(f);
  569. fclose(g);
  570. }
  571. f = fopen(USER_FILE_PATH "index.YSI", io_append);
  572. if (f)
  573. {
  574. fwrite(f, " ");
  575. fclose(f);
  576. }
  577. else
  578. {
  579. P:E("Upgrade index failed.");
  580. }
  581. P:I("Upgrade complete.");
  582. #endif
  583. }
  584. }
  585. }
  586. /*hook OnScriptExit()
  587. {
  588. _Player_CloseLogout();
  589. }*/
  590. hook OnPlayerConnect(playerid)
  591. {
  592. P:1("Users_OnPlayerConnect called: %d", playerid);
  593. // -2 means unknown.
  594. YSI_g_sPlayerYID[playerid] = -2;
  595. broadcastfunc _Player_IsLoggedIn(playerid);
  596. if (existproperty(8, YSIM_LOG_IN))
  597. {
  598. new
  599. uid = getproperty(8, YSIM_LOG_IN);
  600. P:5("Users_OnPlayerConnect: Exists %d", uid);
  601. if (uid == -1)
  602. {
  603. new
  604. name[MAX_PLAYER_NAME];
  605. GetPlayerName(playerid, name, sizeof (name));
  606. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  607. }
  608. else
  609. {
  610. // This DOES NOT use "broadcastfunc" as it's local only.
  611. Player_DoLogin(playerid, uid);
  612. }
  613. deleteproperty(8, YSIM_LOG_IN);
  614. }
  615. else
  616. {
  617. P:5("Users_OnPlayerConnect: Doesn't exist");
  618. new
  619. name[MAX_PLAYER_NAME];
  620. GetPlayerName(playerid, name, sizeof (name));
  621. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  622. P:5("Users_OnPlayerConnect: Done Preload");
  623. // Can do checking in here to see if they just rejoined.
  624. }
  625. YSI_g_sPlayerYID[playerid] = -1;
  626. }
  627. hook OnPlayerDisconnect(playerid, reason)
  628. {
  629. P:2("Users_OnPlayerDisconnect called: %d %d %d", playerid, reason, YSI_g_sPlayerYID[playerid]);
  630. if (YSI_g_sPlayerYID[playerid] >= 0)
  631. {
  632. // DO NOT broadcastfunc this in case it's just because of one script being
  633. // unloaded, not the player actually leaving (and thus do everything in
  634. // different scripts separately).
  635. Player_DoLogout(playerid, YSI_g_sPlayerYID[playerid]);
  636. }
  637. YSI_g_sPlayerYID[playerid] = -2;
  638. }
  639. static remotefunc void:_Player_IsLoggedIn(playerid)
  640. {
  641. P:4("_Player_IsLoggedIn called: %d %d", playerid, YSI_g_sPlayerYID[playerid]);
  642. if (YSI_g_sPlayerYID[playerid] != -2)
  643. {
  644. setproperty(8, YSIM_LOG_IN, YSI_g_sPlayerYID[playerid]);
  645. }
  646. }
  647. stock bool:Player_IsLoggedIn(playerid)
  648. {
  649. // -2 should never be an issue, but if it is...
  650. return YSI_g_sPlayerYID[playerid] >= 0;
  651. }
  652. stock Player_GetYID(playerid)
  653. {
  654. return YSI_g_sPlayerYID[playerid];
  655. }
  656. /**--------------------------------------------------------------------------**\
  657. <summary>Player_TryLogin</summary>
  658. <param name="playerid">Player who is logging in.</param>
  659. <param name="password[]">Password they entered.</param>
  660. <param name="f">Show the failed to login message?</param>
  661. <returns>
  662. -
  663. </returns>
  664. <remarks>
  665. Tries to log in a player - hashes and checks their password and if it's
  666. right calls the core login code. It doesn't matter WHICH script does this
  667. as they ALL get called and ALL track the login status of a player.
  668. </remarks>
  669. \**--------------------------------------------------------------------------**/
  670. //stock Player_TryLogin(playerid, string:password[], f = 0)
  671. //{
  672. //return _Player_TryLogin(playerid, password, f);
  673. //}
  674. global Player_TryLogin(playerid, string:password[])
  675. {
  676. P:2("Player_TryLogin start");
  677. if (Player_IsLoggedIn(playerid))
  678. {
  679. // They are already logged in.
  680. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  681. return 1;
  682. }
  683. new
  684. hash[MAX_PASSWORD_LENGTH + 8 + 1];
  685. Player_HashPass(password, hash);
  686. format(hash[MAX_PASSWORD_LENGTH], sizeof (hash) - MAX_PASSWORD_LENGTH, "%04x%04x", YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  687. //printf("HASH 1: %s", hash);
  688. Player_HashPass(hash, hash);
  689. //printf("HASH 2: %s", hash);
  690. //Player_HashPass(hash, hash);
  691. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  692. {
  693. case -2:
  694. {
  695. Text_Send(playerid, $YSI_LOGIN_INDERR);
  696. }
  697. case -1:
  698. {
  699. Text_Send(playerid, $YSI_LOGIN_NOTF);
  700. }
  701. default:
  702. {
  703. // Match the password.
  704. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0] && !strcmp(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH))
  705. {
  706. // Wipe the password from memory.
  707. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  708. // Extract the uid and call in to the login code.
  709. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  710. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  711. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  712. return 1;
  713. }
  714. else
  715. {
  716. Text_Send(playerid, $YSI_LOGIN_WRONG);
  717. }
  718. }
  719. }
  720. return 0;
  721. }
  722. /**--------------------------------------------------------------------------**\
  723. <summary>Player_ForceLogin</summary>
  724. <param name="playerid">Player who is logging in.</param>
  725. <returns>
  726. -
  727. </returns>
  728. <remarks>
  729. Like "Player_TryLogin" but doesn't take a password so always works.
  730. </remarks>
  731. \**--------------------------------------------------------------------------**/
  732. global Player_ForceLogin(playerid)
  733. {
  734. P:2("Player_TryLogin start");
  735. if (Player_IsLoggedIn(playerid))
  736. {
  737. // They are already logged in.
  738. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  739. return 1;
  740. }
  741. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  742. {
  743. case -2:
  744. {
  745. Text_Send(playerid, $YSI_LOGIN_INDERR);
  746. }
  747. case -1:
  748. {
  749. Text_Send(playerid, $YSI_LOGIN_NOTF);
  750. }
  751. default:
  752. {
  753. // Extract the uid and call in to the login code.
  754. //YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0';
  755. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  756. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  757. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  758. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  759. return 1;
  760. }
  761. }
  762. return 0;
  763. }
  764. /*static*/ remotefunc void:Player_DoLogin(playerid, uid)
  765. {
  766. // Called when a player logs in - either locally (new script) or globally
  767. // (actually only just logged in).
  768. YSI_g_sPlayerYID[playerid] = uid;
  769. // Load any "uvar" variables.
  770. // Call the hooks version of this.
  771. new
  772. filename[64];
  773. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  774. // INI_ParseFile will ONLY load the data for THIS mode, as well as data
  775. // which is mode independent (though there should be none).
  776. INI_ParseFile(filename, "@yU_%s", .bExtra = true, .extra = playerid, .bLocal = true, .bFilter = false, .filter = #MODE_NAME);
  777. //Hooks_OnPlayerLogin(playerid, uid);
  778. //if (YSI_g_sCallbacks & 1)
  779. //{
  780. //CallLocalFunction("OnPlayerLogin", "ii", playerid, uid);
  781. call OnPlayerLogin(playerid, uid);
  782. //}
  783. }
  784. /**--------------------------------------------------------------------------**\
  785. <summary>Player_RemoveEntry</summary>
  786. <param name="name[]">Item to remove.</param>
  787. <returns>
  788. -
  789. </returns>
  790. <remarks>
  791. Wrapper for Player_AddToBuffer for removing data.
  792. </remarks>
  793. \**--------------------------------------------------------------------------**/
  794. stock Player_RemoveEntry(name[])
  795. {
  796. INI_RemoveEntry(YSI_g_sPlayerWriteFile, name);
  797. }
  798. /**--------------------------------------------------------------------------**\
  799. <summary>Player_WriteString</summary>
  800. <param name="name[]">Data name.</param>
  801. <param name="data[]">Data.</param>
  802. <returns>
  803. -
  804. </returns>
  805. <remarks>
  806. Wrapper for Player_AddToBuffer for strings.
  807. </remarks>
  808. \**--------------------------------------------------------------------------**/
  809. stock Player_WriteString(name[], data[])
  810. {
  811. INI_WriteString(YSI_g_sPlayerWriteFile, name, data);
  812. }
  813. stock Player_WriteArray(const name[], data[], len)
  814. {
  815. //printf("name = %s", name);
  816. //printf("data = %d, %d, %d", data[0], data[1], data[2]);
  817. //printf("len = %d", len);
  818. INI_WriteArray(YSI_g_sPlayerWriteFile, name, data, len);
  819. return 1;
  820. }
  821. /**--------------------------------------------------------------------------**\
  822. <summary>Player_WriteInt</summary>
  823. <param name="name[]">Data name.</param>
  824. <param name="data">Integer data.</param>
  825. <returns>
  826. -
  827. </returns>
  828. <remarks>
  829. Wrapper for Player_AddToBuffer for integers.
  830. </remarks>
  831. \**--------------------------------------------------------------------------**/
  832. stock Player_WriteInt(name[], data)
  833. {
  834. INI_WriteInt(YSI_g_sPlayerWriteFile, name, data);
  835. }
  836. /**--------------------------------------------------------------------------**\
  837. <summary>Player_WriteHex</summary>
  838. <param name="name[]">Data name.</param>
  839. <param name="data">Hex data.</param>
  840. <returns>
  841. -
  842. </returns>
  843. <remarks>
  844. Wrapper for Player_AddToBuffer for integers to be written as hex values.
  845. </remarks>
  846. \**--------------------------------------------------------------------------**/
  847. stock Player_WriteHex(name[], data)
  848. {
  849. INI_WriteHex(YSI_g_sPlayerWriteFile, name, data);
  850. }
  851. /**--------------------------------------------------------------------------**\
  852. <summary>Player_WriteBin</summary>
  853. <param name="name[]">Data name.</param>
  854. <param name="data">Binary data.</param>
  855. <returns>
  856. -
  857. </returns>
  858. <remarks>
  859. Wrapper for Player_AddToBuffer for integers to be written as binary values.
  860. </remarks>
  861. \**--------------------------------------------------------------------------**/
  862. stock Player_WriteBin(name[], data)
  863. {
  864. INI_WriteBin(YSI_g_sPlayerWriteFile, name, data);
  865. }
  866. /**--------------------------------------------------------------------------**\
  867. <summary>Player_WriteBool</summary>
  868. <param name="name[]">Data name.</param>
  869. <param name="data">Boolean data.</param>
  870. <returns>
  871. -
  872. </returns>
  873. <remarks>
  874. Wrapper for Player_AddToBuffer for booleans.
  875. </remarks>
  876. \**--------------------------------------------------------------------------**/
  877. stock Player_WriteBool(name[], bool:data)
  878. {
  879. INI_WriteBool(YSI_g_sPlayerWriteFile, name, data);
  880. }
  881. /**--------------------------------------------------------------------------**\
  882. <summary>Player_WriteFloat</summary>
  883. <param name="name[]">Data name.</param>
  884. <param name="Float:data">Float data.</param>
  885. <param name="accuracy">number of decimal places to write.</param>
  886. <returns>
  887. -
  888. </returns>
  889. <remarks>
  890. Wrapper for Player_AddToBuffer for floats. Uses custom code instead of
  891. format() as it's actually faster for something simple like this.
  892. </remarks>
  893. \**--------------------------------------------------------------------------**/
  894. stock Player_WriteFloat(name[], Float:data, accuracy = 6)
  895. {
  896. INI_WriteFloat(YSI_g_sPlayerWriteFile, name, data, accuracy);
  897. }
  898. stock Player_SetTag(tag[])
  899. {
  900. // Make sure we ALWAYS store mode tags with a special prefix.
  901. new
  902. tag2[MAX_INI_TAG] = "@@" #MODE_NAME "-";
  903. strcat(tag2, tag);
  904. //printf("tag = %s, %s", tag, tag2);
  905. INI_SetTag(YSI_g_sPlayerWriteFile, tag2);
  906. }
  907. stock Player_DeleteTag(tag[])
  908. {
  909. INI_DeleteTag(YSI_g_sPlayerWriteFile, tag);
  910. }
  911. //global Player_DoLogout(playerid, uid)
  912. static stock Player_DoLogout(playerid, uid)
  913. {
  914. /*INI:YSI_g_sOneLogoutFile = INI_NO_FILE
  915. YSI_g_sOneLogoutPerson = INVALID_PLAYER_ID*/
  916. new
  917. filename[64];
  918. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  919. YSI_g_sPlayerWriteFile = INI_Open(filename);
  920. if (YSI_g_sPlayerWriteFile != INI_NO_FILE)
  921. {
  922. //Hooks_OnPlayerLogout(playerid, uid);
  923. //if (YSI_g_sCallbacks & 2)
  924. //{
  925. call OnPlayerLogout(playerid, uid);
  926. //}
  927. INI_Close(YSI_g_sPlayerWriteFile);
  928. }
  929. // Do ALL the saving from ONE place. Detect one or many scripts ending.
  930. /*if (YSI_g_sOneLogoutFile == INI_NO_FILE)
  931. {
  932. new
  933. filename[64];
  934. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  935. YSI_g_sOneLogoutFile = INI_Open(filename);
  936. YSI_g_sOneLogoutPerson = playerid;
  937. }
  938. else if (YSI_g_sOneLogoutPerson != playerid)
  939. {
  940. INI_Close(YSI_g_sOneLogoutFile);
  941. }
  942. // Will be called AFTER all "OnPlayerDisconnect" callbacks are called when
  943. // one player leaves, and if more leave it's still used for the last player
  944. // in the list.
  945. //defer _Player_CloseLogout();
  946. _Player_CloseLogout();
  947. // By now "YSI_g_sOneLogoutFile" contains a handle to the user's file,
  948. // either having just been opened, or invoked in a previous call to this
  949. // function from another script still running but loosing the player.
  950. // This could in theory be used from some force-logout script.
  951. YSI_g_sPlayerYID[playerid] = -1;*/
  952. return 1;
  953. }
  954. //static timer _Player_CloseLogout[0]()
  955. /*static stock _Player_CloseLogout()
  956. {
  957. if (YSI_g_sOneLogoutFile != INI_NO_FILE)
  958. {
  959. INI_Close(YSI_g_sOneLogoutFile);
  960. YSI_g_sOneLogoutFile = INI_NO_FILE;
  961. YSI_g_sOneLogoutPerson = INVALID_PLAYER_ID;
  962. }
  963. }*/
  964. /**--------------------------------------------------------------------------**\
  965. <summary>Player_HashPass</summary>
  966. <param name="pass[]">Data to hash.</param>
  967. <returns>
  968. -
  969. </returns>
  970. <remarks>
  971. Based on my Dad's hash system but slightly modifed. Updated for reverse
  972. compatability with other login systems. Needs more code for Whirlpool.
  973. </remarks>
  974. \**--------------------------------------------------------------------------**/
  975. static stock Player_HashPass(pass[], target[])
  976. {
  977. #if defined PP_ADLER32
  978. new
  979. s1 = 1,
  980. s2 = 0,
  981. i,
  982. You_REALLY_shouldnt_use_Adler32;
  983. while (pass[i])
  984. {
  985. s1 = (s1 + pass[i++]) % 65521;
  986. s2 = (s2 + s1) % 65521;
  987. }
  988. //new
  989. // target[MAX_PASSWORD_LENGTH + 1];
  990. format(target, sizeof (target), "%" #MAX_PASSWORD_LENGTH "d", (s2 << 16) + s1);
  991. //return target;
  992. #elseif defined PP_MD5 && defined MD5_Hash
  993. new
  994. You_REALLY_shouldnt_use_MD5;
  995. strcpy(target, MD5_Hash(pass, strlen(pass)));
  996. #elseif defined PP_SHA1
  997. #error SHA1 unsupported.
  998. #elseif defined PP_YSI
  999. static
  1000. charset[] = "A,UbRgdnS#|rT_%5+ZvEK¬NF<9¦IH[(C)2O07 Y-Less]$Qw^?/om4;@'8k£Pp.c{&l\\3zay>DfxV:WXjuG6*!1\"i~=Mh`JB}qt",
  1001. css = 99;
  1002. new
  1003. //target[MAX_PASSWORD_LENGTH + 1],
  1004. j,
  1005. sum = j,
  1006. tmp = 0,
  1007. i,
  1008. mod;
  1009. j = strlen(pass);
  1010. for (i = 0; i < MAX_PASSWORD_LENGTH || i < j; i++)
  1011. {
  1012. mod = i % MAX_PASSWORD_LENGTH;
  1013. tmp = (i >= j) ? charset[(7 * i) % css] : pass[i];
  1014. sum = (sum + chrfind(tmp, charset) + 1) % css;
  1015. target[mod] = charset[(sum + target[mod]) % css];
  1016. }
  1017. target[MAX_PASSWORD_LENGTH] = '\0';
  1018. //return target;
  1019. #elseif defined WP_Hash
  1020. WP_Hash(target, MAX_PASSWORD_LENGTH + 1, pass);
  1021. #else
  1022. #error Whirlpool (or other) hash not found.
  1023. #endif
  1024. }
  1025. // Hooray for bizare bugs! I think this is because the function above is
  1026. // secretly a macro with "if/else" and a block statement, not a real function.
  1027. stock Player_SomeWeirdBugFix()
  1028. {
  1029. }
  1030. /*stock Anything0()
  1031. {
  1032. }*/
  1033. /*stock Anything1()
  1034. {
  1035. }
  1036. stock Anything2()
  1037. {
  1038. }
  1039. stock Anything3()
  1040. {
  1041. }*/
  1042. /*stock Anything4()
  1043. {
  1044. }
  1045. stock Anything5()
  1046. {
  1047. }
  1048. stock Anything6()
  1049. {
  1050. }
  1051. stock Anything7()
  1052. {
  1053. }*/
  1054. /**--------------------------------------------------------------------------**\
  1055. <summary>Player_TryRegister</summary>
  1056. <param name="playerid">Player who is registering.</param>
  1057. <param name="string:password[]">The password they entered.</param>
  1058. <returns>
  1059. -
  1060. </returns>
  1061. <remarks>
  1062. Register the player with the given password if there is no-one else with the
  1063. name already. Or log them in if the username and password match an existing
  1064. account. Note that there is no "Player_ForceRegister" as it would do the
  1065. same thing with no less parameters (a password MUST be given to write in the
  1066. file).
  1067. </remarks>
  1068. \**--------------------------------------------------------------------------**/
  1069. //#endinput
  1070. global Player_TryRegister(playerid, string:password[])
  1071. {
  1072. P:2("Player_TryRegister called");
  1073. if (Player_IsLoggedIn(playerid))
  1074. {
  1075. // They are already logged in.
  1076. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1077. return 1;
  1078. }
  1079. new
  1080. hash[MAX_PASSWORD_LENGTH + 1 + 8];
  1081. Player_HashPass(password, hash);
  1082. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1083. {
  1084. case -2:
  1085. {
  1086. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1087. }
  1088. case -1:
  1089. {
  1090. }
  1091. default:
  1092. {
  1093. // Already registered, just try log them in.
  1094. // Get the salt.
  1095. format(hash[MAX_PASSWORD_LENGTH], sizeof (hash) - MAX_PASSWORD_LENGTH, "%04x%04x", YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1096. Player_HashPass(hash, hash);
  1097. // Match the password.
  1098. if (!strcmp(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0])
  1099. {
  1100. // Extract the uid and call in to the login code.
  1101. //YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0';
  1102. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  1103. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  1104. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  1105. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1106. return 1;
  1107. }
  1108. else
  1109. {
  1110. //Text_Send(playerid, $YSI_LOGIN_WRONG);
  1111. Text_Send(playerid, $YSI_REG_TAKEN);
  1112. return 0;
  1113. }
  1114. }
  1115. }
  1116. new
  1117. name[MAX_PLAYER_NAME + 1];
  1118. GetPlayerName(playerid, name, sizeof (name));
  1119. //format(name, sizeof (name), "%" #MAX_PLAYER_NAME "s", name);
  1120. new
  1121. filename[64] = USER_FILE_PATH "ind_X.YSI",
  1122. File:fIndex;//,
  1123. //hash[MAX_PASSWORD_LENGTH + 1];
  1124. //Player_HashPass(password, hash);
  1125. Player_GetIndexFile(filename, name[0]);
  1126. fIndex = fopen(filename, io_read);
  1127. P:5("Player_TryRegister: fIndex");
  1128. new
  1129. line[INDEX_DATA_LINE_LENGTH];
  1130. if ((fIndex = fopen(filename, io_append)))
  1131. {
  1132. P:5("Player_TryRegister: Write index.");
  1133. // Write the new user to the index file.
  1134. new
  1135. uid = Player_GetNewID();
  1136. if (uid == -1)
  1137. {
  1138. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1139. return 0;
  1140. }
  1141. new
  1142. d = gettime(),
  1143. date[9];
  1144. format(date, sizeof (date), "%04x%04x", d >>> 16, d & 0xFFFF);
  1145. strcat(hash, date);
  1146. Player_HashPass(hash, hash);
  1147. format(line, sizeof (line), "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s 00000000 %s" INI_NEW_LINE, uid, name, hash, Langs_GetCode(Langs_GetPlayerLanguage(playerid)), date);
  1148. fwrite(fIndex, line);
  1149. fclose(fIndex);
  1150. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1151. new
  1152. INI:x = INI_Open(filename);
  1153. if (x == INI_NO_FILE)
  1154. {
  1155. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1156. }
  1157. else
  1158. {
  1159. INI_SetTag(x, "ysi_names");
  1160. INI_WriteString(x, name, "name");
  1161. INI_Close(x);
  1162. }
  1163. // Call in all scripts.
  1164. broadcastfunc Player_DoLogin(playerid, uid);
  1165. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1166. return 1;
  1167. }
  1168. else
  1169. {
  1170. Text_Send(playerid, $YSI_ADDU_INDER2);
  1171. return 0;
  1172. }
  1173. }
  1174. global Player_ChangePassword(playerid, string:password[])
  1175. {
  1176. #pragma unused password
  1177. new
  1178. uid = Player_GetYID(playerid);
  1179. if (uid < 0)
  1180. {
  1181. return 0;
  1182. }
  1183. // Create the new password.
  1184. new
  1185. hash[MAX_PASSWORD_LENGTH + 8 + 1];
  1186. Player_HashPass(password, hash);
  1187. format(hash[MAX_PASSWORD_LENGTH], sizeof (hash) - MAX_PASSWORD_LENGTH, "%04x%04x", YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1188. Player_HashPass(hash, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS]);
  1189. // Save it.
  1190. Player_RewritePreload(playerid);
  1191. // Wipe it.
  1192. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  1193. return 1;
  1194. }
  1195. global Player_ChangeLanguage(playerid, string:code[])
  1196. {
  1197. new
  1198. uid = Player_GetYID(playerid);
  1199. if (uid < 0)
  1200. {
  1201. return 0;
  1202. }
  1203. Langs_SetPlayerCode(playerid, code);
  1204. Player_RewritePreload(playerid);
  1205. return 1;
  1206. }
  1207. /**--------------------------------------------------------------------------**\
  1208. <summary>Player_RewritePreload</summary>
  1209. <param name="playerid">Player whose data should be saved.</param>
  1210. <returns>
  1211. -
  1212. </returns>
  1213. <remarks>
  1214. When a player's preload data is modifed (new bit data or changed password),
  1215. it needs to be written back out to file.
  1216. </remarks>
  1217. \**--------------------------------------------------------------------------**/
  1218. static stock Player_RewritePreload(playerid)
  1219. {
  1220. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] < 0)
  1221. {
  1222. return 0;
  1223. }
  1224. // First, find the player's file. This should be the ONLY place where the
  1225. // password is to be loaded.
  1226. //ret[E_USER_PRELOAD_YID] = -2;
  1227. //ret[E_USER_PRELOAD_PASS] = '\0';
  1228. //ret[E_USER_PRELOAD_LANG] = NO_LANGUAGE;
  1229. //ret[E_USER_PRELOAD_BITS] = 0;
  1230. //ret[E_USER_PRELOAD_DATE] = 0;
  1231. new
  1232. name[MAX_PLAYER_NAME + 1];
  1233. GetPlayerName(playerid, name, sizeof (name));
  1234. P:4("Player_RewritePreload called: %s", name);
  1235. #if _YSI_PLUGINS_MYSQL == 7
  1236. // Right then, lets get coding...
  1237. new
  1238. query[110];
  1239. mysql_format(YSI_g_sMySQL, query, "SELECT `uid`, `language`, `hash` FROM `y_users_register` WHERE `name` = '%e' LIMIT 0,1", name);
  1240. mysql_function_query(YSI_g_sMySQL, query, true, "_Player_Preload", "ii", );
  1241. #else
  1242. new
  1243. namelen = strlen(name),
  1244. filename[] = USER_FILE_PATH "ind_X.YSI",
  1245. File:fIndex;
  1246. Player_GetIndexFile(filename, name[0]);
  1247. fIndex = fopen(filename, io_readwrite);
  1248. if (fIndex)
  1249. {
  1250. P:5("Player_RewritePreload: fIndex OK");
  1251. new
  1252. line[INDEX_DATA_LINE_LENGTH],
  1253. len;
  1254. while ((len = fread(fIndex, line)))
  1255. {
  1256. P:6("Player_RewritePreload: while");
  1257. //new
  1258. // len;
  1259. //len = strlen(line);
  1260. // Check if the line is the right length (could be one of three
  1261. // lengths depending on newlines). Skip blanks.
  1262. if (len < INDEX_DATA_LINE_LENGTH - 3)
  1263. {
  1264. continue;
  1265. }
  1266. P:6("Player_RewritePreload: Not len");
  1267. // Check the name on the line.
  1268. if (!strcmp(line[MAX_INDEX_LENGTH + 1], name, false, namelen) && line[MAX_INDEX_LENGTH + 1 + namelen] == ' ')
  1269. {
  1270. P:6("Player_RewritePreload: checked name");
  1271. fseek(fIndex, -len, seek_current);
  1272. //format(line, sizeof (line),
  1273. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0] == '\0')
  1274. {
  1275. // Keep the old password.
  1276. format(line, sizeof (line),
  1277. "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "." #MAX_PASSWORD_LENGTH "s %02s %04x%04x %04x%04x" INI_NEW_LINE,
  1278. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  1279. name,
  1280. line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1],
  1281. Langs_GetCode(Langs_GetPlayerLanguage(playerid)),
  1282. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] & 0xFFFF,
  1283. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1284. }
  1285. else
  1286. {
  1287. format(line, sizeof (line),
  1288. "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s %04x%04x %04x%04x" INI_NEW_LINE,
  1289. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  1290. name,
  1291. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS],
  1292. Langs_GetCode(Langs_GetPlayerLanguage(playerid)),
  1293. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] & 0xFFFF,
  1294. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1295. }
  1296. P:5("Player_RewritePreload: Writing: %s", line);
  1297. fwrite(fIndex, line);
  1298. fclose(fIndex);
  1299. return 1;
  1300. }
  1301. }
  1302. fclose(fIndex);
  1303. }
  1304. else if (fexist(filename))
  1305. {
  1306. P:E("Error reading index %c.", filename[0]);
  1307. return 0;
  1308. }
  1309. //ret[E_USER_PRELOAD_YID] = -1;
  1310. #endif
  1311. return 0;
  1312. }
  1313. /**--------------------------------------------------------------------------**\
  1314. <summary>Player_TryGroup</summary>
  1315. <param name="playerid">Player who is joining a group.</param>
  1316. <param name="string:other[]">A player name already in the group.</param>
  1317. <param name="string:password[]">The password of the group.</param>
  1318. <returns>
  1319. -
  1320. </returns>
  1321. <remarks>
  1322. Links a player with an existing player such that they share all stats.
  1323. </remarks>
  1324. \**--------------------------------------------------------------------------**/
  1325. //#endinput
  1326. global Player_TryGroup(playerid, string:other[], string:password[])
  1327. {
  1328. P:2("Player_TryGroup called");
  1329. if (Player_IsLoggedIn(playerid))
  1330. {
  1331. // They are already logged in.
  1332. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1333. return 1;
  1334. }
  1335. new
  1336. hash[MAX_PASSWORD_LENGTH + 1];
  1337. Player_HashPass(password, hash);
  1338. // Check if the user is not registered already.
  1339. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1340. {
  1341. case -2:
  1342. {
  1343. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1344. return 0;
  1345. }
  1346. case -1:
  1347. {
  1348. //Text_Send(playerid, $YSI_LOGIN_NOTF);
  1349. }
  1350. default:
  1351. {
  1352. Text_Send(playerid, $YSI_REG_TAKEN);
  1353. return 0;
  1354. }
  1355. }
  1356. // Check if the new data matches the old.
  1357. new
  1358. ret[E_USER_PRELOAD];
  1359. Player_Preload(other, ret);
  1360. switch (ret[E_USER_PRELOAD_YID])
  1361. {
  1362. case -2:
  1363. {
  1364. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1365. }
  1366. case -1:
  1367. {
  1368. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1369. }
  1370. default:
  1371. {
  1372. // Match the password.
  1373. if (!strcmp(ret[E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && ret[E_USER_PRELOAD_PASS][0])
  1374. {
  1375. new
  1376. name[MAX_PLAYER_NAME + 1];
  1377. GetPlayerName(playerid, name, sizeof (name));
  1378. new
  1379. filename[64] = USER_FILE_PATH "ind_X.YSI";
  1380. Player_GetIndexFile(filename, name[0]);
  1381. new
  1382. File:fIndex = fopen(filename, io_append);
  1383. if (!fIndex)
  1384. {
  1385. Text_Send(playerid, $YSI_ADDU_INDERR2);
  1386. return 0;
  1387. }
  1388. P:5("Player_TryGroup: Write index.");
  1389. new
  1390. uid = ret[E_USER_PRELOAD_YID],
  1391. line[INDEX_DATA_LINE_LENGTH];
  1392. // Use the loaded ID.
  1393. format(line, sizeof (line), "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s" INI_NEW_LINE, uid, name, hash, Langs_GetCode(ret[E_USER_PRELOAD_LANG]));
  1394. fwrite(fIndex, line);
  1395. fclose(fIndex);
  1396. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1397. new
  1398. INI:x = INI_Open(filename);
  1399. if (x == INI_NO_FILE)
  1400. {
  1401. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1402. }
  1403. else
  1404. {
  1405. // Add this name to the list of known names.
  1406. INI_SetTag(x, "ysi_names");
  1407. INI_WriteString(x, name, "name");
  1408. INI_Close(x);
  1409. }
  1410. // Call in all scripts.
  1411. Langs_SetPlayerLanguage(playerid, ret[E_USER_PRELOAD_LANG]);
  1412. broadcastfunc Player_DoLogin(playerid, uid);
  1413. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1414. return 1;
  1415. }
  1416. else
  1417. {
  1418. Text_Send(playerid, $YSI_LOGIN_WRONG);
  1419. }
  1420. }
  1421. }
  1422. return 0;
  1423. }
  1424. /**--------------------------------------------------------------------------**\
  1425. <summary>Player_ForceGroup</summary>
  1426. <param name="playerid">Player who is joining a group.</param>
  1427. <param name="string:other[]">A player name already in the group.</param>
  1428. <returns>
  1429. -
  1430. </returns>
  1431. <remarks>
  1432. Like "Player_TryGroup", but doesn't take a password and instead just uses
  1433. the password of the old player (hashed).
  1434. </remarks>
  1435. \**--------------------------------------------------------------------------**/
  1436. //#endinput
  1437. global Player_ForceGroup(playerid, string:other[])
  1438. {
  1439. P:2("Player_ForceGroup called");
  1440. if (Player_IsLoggedIn(playerid))
  1441. {
  1442. // They are already logged in.
  1443. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1444. return 1;
  1445. }
  1446. // Check if the user is not registered already.
  1447. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1448. {
  1449. case -2:
  1450. {
  1451. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1452. return 0;
  1453. }
  1454. case -1:
  1455. {
  1456. //Text_Send(playerid, $YSI_LOGIN_NOTF);
  1457. }
  1458. default:
  1459. {
  1460. Text_Send(playerid, $YSI_REG_TAKEN);
  1461. return 0;
  1462. }
  1463. }
  1464. // Check if the new data matches the old.
  1465. new
  1466. ret[E_USER_PRELOAD];
  1467. Player_Preload(other, ret);
  1468. switch (ret[E_USER_PRELOAD_YID])
  1469. {
  1470. case -2:
  1471. {
  1472. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1473. }
  1474. case -1:
  1475. {
  1476. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1477. }
  1478. default:
  1479. {
  1480. new
  1481. name[MAX_PLAYER_NAME + 1];
  1482. GetPlayerName(playerid, name, sizeof (name));
  1483. new
  1484. filename[64] = USER_FILE_PATH "ind_X.YSI";
  1485. Player_GetIndexFile(filename, name[0]);
  1486. new
  1487. File:fIndex = fopen(filename, io_append);
  1488. if (!fIndex)
  1489. {
  1490. Text_Send(playerid, $YSI_ADDU_INDERR2);
  1491. return 0;
  1492. }
  1493. P:5("Player_TryGroup: Write index.");
  1494. new
  1495. uid = ret[E_USER_PRELOAD_YID],
  1496. line[INDEX_DATA_LINE_LENGTH];
  1497. // Use the loaded ID.
  1498. format(line, sizeof (line), "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s" INI_NEW_LINE, uid, name, ret[E_USER_PRELOAD_PASS], Langs_GetCode(ret[E_USER_PRELOAD_LANG]));
  1499. fwrite(fIndex, line);
  1500. fclose(fIndex);
  1501. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1502. new
  1503. INI:x = INI_Open(filename);
  1504. if (x == INI_NO_FILE)
  1505. {
  1506. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1507. }
  1508. else
  1509. {
  1510. // Add this name to the list of known names.
  1511. INI_SetTag(x, "ysi_names");
  1512. INI_WriteString(x, name, "name");
  1513. INI_Close(x);
  1514. }
  1515. // Call in all scripts.
  1516. Langs_SetPlayerLanguage(playerid, ret[E_USER_PRELOAD_LANG]);
  1517. broadcastfunc Player_DoLogin(playerid, uid);
  1518. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1519. return 1;
  1520. }
  1521. }
  1522. return 0;
  1523. }
  1524. static stock Player_GetNewID()
  1525. {
  1526. new
  1527. File:fHnd = fopen(USER_FILE_PATH "index.YSI", io_readwrite),
  1528. num[MAX_INDEX_LENGTH + 9],
  1529. uid = -1;
  1530. if (fHnd)
  1531. {
  1532. fread(fHnd, num);
  1533. num[strfind(num, " ")] = '\0';
  1534. uid = strval(num) + 1;
  1535. valstr(num, uid);
  1536. fseek(fHnd, 0, seek_start);
  1537. fwrite(fHnd, num);
  1538. fwrite(fHnd, " ");
  1539. fclose(fHnd);
  1540. }
  1541. return uid;
  1542. }
  1543. static stock Player_CreateNewID()
  1544. {
  1545. new
  1546. File:fHnd = fopen(USER_FILE_PATH "index.YSI", io_write);
  1547. if (fHnd)
  1548. {
  1549. fwrite(fHnd, "-1 ");
  1550. fclose(fHnd);
  1551. return 1;
  1552. }
  1553. return 0;
  1554. }