ini.inc 43 KB

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