blueg7.inc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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. foreign Player_TryRegister(playerid, string:password[]);
  106. foreign Player_TryLogin(playerid, string:password[]);
  107. foreign Player_TryGroup(playerid, string:other[], string:password[]);
  108. // No longer global because the YID is shared in all scripts.
  109. //foreign Player_GetYID(playerid);
  110. foreign Player_ChangePassword(playerid, string:password[]);
  111. foreign Player_ForceGroup(playerid, string:other[]);
  112. foreign Player_ForceLogin(playerid);
  113. /*----------------------------------------------------------------------------*\
  114. Function:
  115. Player_Reload
  116. Params:
  117. playerid - Player who changed name.
  118. Return:
  119. -
  120. Notes:
  121. Reload a player's basic data when they change name.
  122. \*----------------------------------------------------------------------------*/
  123. stock Player_Reload(playerid)
  124. {
  125. new
  126. name[MAX_PLAYER_NAME];
  127. GetPlayerName(playerid, name, sizeof (name));
  128. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  129. }
  130. stock Player_SetPreload(playerid, data[E_USER_PRELOAD])
  131. {
  132. if (0 <= playerid < MAX_PLAYERS)
  133. {
  134. YSI_g_sPreloadData[playerid] = data;
  135. }
  136. }
  137. /*----------------------------------------------------------------------------*\
  138. Function:
  139. Player_Preload
  140. Params:
  141. playerid - Player who is logging in.
  142. Return:
  143. -
  144. Notes:
  145. Loads a player's data to an array.
  146. \*----------------------------------------------------------------------------*/
  147. #if _YSI_PLUGINS_MYSQL == 7
  148. public _Player_Preload(ret[E_USER_PRELOAD])
  149. {
  150. // Need to use some clever AMX hacking to pass the "ret" array...
  151. }
  152. #endif
  153. stock Player_Preload(string:name[], ret[E_USER_PRELOAD])
  154. {
  155. // First, find the player's file. This should be the ONLY place where the
  156. // password is to be loaded.
  157. P:4("Player_Preload called: %s", name);
  158. ret[E_USER_PRELOAD_YID] = -2;
  159. ret[E_USER_PRELOAD_PASS] = '\0';
  160. ret[E_USER_PRELOAD_LANG] = NO_LANGUAGE;
  161. ret[E_USER_PRELOAD_BITS] = 0;
  162. ret[E_USER_PRELOAD_DATE] = 0;
  163. #if _YSI_PLUGINS_MYSQL == 7
  164. // Right then, lets get coding...
  165. new
  166. query[110];
  167. mysql_format(YSI_g_sMySQL, query, "SELECT `uid`, `language`, `hash` FROM `y_users_register` WHERE `name` = '%e' LIMIT 0,1", name);
  168. mysql_function_query(YSI_g_sMySQL, query, true, "_Player_Preload", "ii", );
  169. #else
  170. new
  171. namelen = strlen(name),
  172. filename[] = USER_FILE_PATH "ind_X.YSI",
  173. File:fIndex;
  174. Player_GetIndexFile(filename, name[0]);
  175. fIndex = fopen(filename, io_read);
  176. if (fIndex)
  177. {
  178. P:5("Player_Preload: fIndex OK");
  179. new
  180. line[INDEX_DATA_LINE_LENGTH],
  181. len;
  182. while ((len = fread(fIndex, line)))
  183. {
  184. P:6("Player_Preload: while");
  185. //new
  186. // len;
  187. //len = strlen(line);
  188. // Check if the line is the right length (could be one of three
  189. // lengths depending on newlines). Skip blanks.
  190. if (len < INDEX_DATA_LINE_LENGTH - 3)
  191. {
  192. continue;
  193. }
  194. P:6("Player_Preload: Not len");
  195. // Check the name on the line.
  196. if (!strcmp(line[MAX_INDEX_LENGTH + 1], name, false, namelen) && line[MAX_INDEX_LENGTH + 1 + namelen] == ' ')
  197. {
  198. P:6("Player_Preload: checked name");
  199. // Found the section on this one player.
  200. //P:6("Player_Preload: check pass: %s ?= %s", hash, line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1]);
  201. // Save the loaded data.
  202. line[MAX_INDEX_LENGTH] = '\0';
  203. //printf("line: %s", line);
  204. ret[E_USER_PRELOAD_YID] = strval(line);
  205. //printf("%d %d %d", ret[E_USER_PRELOAD_YID], strval(line), strval("00000022"));
  206. //printf("%d", strval(line));
  207. line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2] = '\0';
  208. ret[E_USER_PRELOAD_LANG] = Langs_GetLanguage(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1]);
  209. strcat(ret[E_USER_PRELOAD_PASS], line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1], MAX_PASSWORD_LENGTH + 1);
  210. // Load the 32 extra "bits".
  211. ret[E_USER_PRELOAD_BITS] = hexstr(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2 + 1]);
  212. // Load the user registration date (if they have one).
  213. ret[E_USER_PRELOAD_DATE] = hexstr(line[MAX_INDEX_LENGTH + 1 + MAX_PLAYER_NAME + 1 + MAX_PASSWORD_LENGTH + 1 + 2 + 1 + 8 + 1]);
  214. 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]);
  215. fclose(fIndex);
  216. return 1;
  217. }
  218. }
  219. fclose(fIndex);
  220. }
  221. else if (fexist(filename))
  222. {
  223. P:E("Error reading index %c.", filename[0]);
  224. return -1;
  225. }
  226. ret[E_USER_PRELOAD_YID] = -1;
  227. #endif
  228. return 0;
  229. }
  230. stock Language:Player_GetPreloadLanguage(playerid)
  231. {
  232. return Language:YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG];
  233. }
  234. stock Player_GetPreloadBits(playerid)
  235. {
  236. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS];
  237. }
  238. static remotefunc void:_Player_SetPreloadBits(playerid, bits)
  239. {
  240. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] = bits;
  241. }
  242. stock Player_SetPreloadBits(playerid, bits)
  243. {
  244. broadcastfunc _Player_SetPreloadBits(playerid, bits);
  245. Player_RewritePreload(playerid);
  246. }
  247. stock Player_GetPreloadDate(playerid)
  248. {
  249. if (0 <= YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] < 1234567890)
  250. {
  251. return -1;
  252. }
  253. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE];
  254. }
  255. stock Player_IsRegistered(playerid)
  256. {
  257. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] != -1;
  258. }
  259. hook OnScriptInit()
  260. {
  261. switch (ftouch(USER_FILE_PATH "index.YSI"))
  262. {
  263. case -1:
  264. {
  265. P:E(USER_FILE_PATH "index.YSI does not exist and couldn't be created.");
  266. }
  267. case 1:
  268. {
  269. if (!Player_CreateNewID())
  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. Function:
  658. Player_TryLogin
  659. Params:
  660. playerid - Player who is logging in.
  661. password[] - Password they entered.
  662. f - Show the failed to login message?
  663. Return:
  664. -
  665. Notes:
  666. Tries to log in a player - hashes and checks their password and if it's
  667. right calls the core login code. It doesn't matter WHICH script does this
  668. as they ALL get called and ALL track the login status of a player.
  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. Function:
  724. Player_ForceLogin
  725. Params:
  726. playerid - Player who is logging in.
  727. Return:
  728. -
  729. Notes:
  730. Like "Player_TryLogin" but doesn't take a password so always works.
  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. 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. // Get the salt.
  1093. 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);
  1094. Player_HashPass(hash, hash);
  1095. // Match the password.
  1096. if (!strcmp(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0])
  1097. {
  1098. // Extract the uid and call in to the login code.
  1099. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0';
  1100. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  1101. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  1102. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1103. return 1;
  1104. }
  1105. else
  1106. {
  1107. //Text_Send(playerid, $YSI_LOGIN_WRONG);
  1108. Text_Send(playerid, $YSI_REG_TAKEN);
  1109. return 0;
  1110. }
  1111. }
  1112. }
  1113. new
  1114. name[MAX_PLAYER_NAME + 1];
  1115. GetPlayerName(playerid, name, sizeof (name));
  1116. //format(name, sizeof (name), "%" #MAX_PLAYER_NAME "s", name);
  1117. new
  1118. filename[64] = USER_FILE_PATH "ind_X.YSI",
  1119. File:fIndex;//,
  1120. //hash[MAX_PASSWORD_LENGTH + 1];
  1121. //Player_HashPass(password, hash);
  1122. Player_GetIndexFile(filename, name[0]);
  1123. fIndex = fopen(filename, io_read);
  1124. P:5("Player_TryRegister: fIndex");
  1125. new
  1126. line[INDEX_DATA_LINE_LENGTH];
  1127. if ((fIndex = fopen(filename, io_append)))
  1128. {
  1129. P:5("Player_TryRegister: Write index.");
  1130. // Write the new user to the index file.
  1131. new
  1132. uid = Player_GetNewID();
  1133. if (uid == -1)
  1134. {
  1135. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1136. return 0;
  1137. }
  1138. new
  1139. d = gettime(),
  1140. date[9];
  1141. format(date, sizeof (date), "%04x%04x", d >>> 16, d & 0xFFFF);
  1142. strcat(hash, date);
  1143. Player_HashPass(hash, hash);
  1144. 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);
  1145. fwrite(fIndex, line);
  1146. fclose(fIndex);
  1147. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1148. new
  1149. INI:x = INI_Open(filename);
  1150. if (x == INI_NO_FILE)
  1151. {
  1152. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1153. }
  1154. else
  1155. {
  1156. INI_SetTag(x, "ysi_names");
  1157. INI_WriteString(x, name, "name");
  1158. INI_Close(x);
  1159. }
  1160. // Call in all scripts.
  1161. broadcastfunc Player_DoLogin(playerid, uid);
  1162. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1163. return 1;
  1164. }
  1165. else
  1166. {
  1167. Text_Send(playerid, $YSI_ADDU_INDER2);
  1168. return 0;
  1169. }
  1170. }
  1171. global Player_ChangePassword(playerid, string:password[])
  1172. {
  1173. #pragma unused password
  1174. new
  1175. uid = Player_GetYID(playerid);
  1176. if (uid < 0)
  1177. {
  1178. return 0;
  1179. }
  1180. // Create the new password.
  1181. new
  1182. hash[MAX_PASSWORD_LENGTH + 8 + 1];
  1183. Player_HashPass(password, hash);
  1184. 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);
  1185. Player_HashPass(hash, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS]);
  1186. // Save it.
  1187. Player_RewritePreload(playerid);
  1188. // Wipe it.
  1189. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], MAX_PASSWORD_LENGTH, 0);
  1190. return 1;
  1191. }
  1192. /*----------------------------------------------------------------------------*\
  1193. Function:
  1194. Player_RewritePreload
  1195. Params:
  1196. playerid - Player whose data should be saved.
  1197. Return:
  1198. -
  1199. Notes:
  1200. When a player's preload data is modifed (new bit data or changed password),
  1201. it needs to be written back out to file.
  1202. \*----------------------------------------------------------------------------*/
  1203. static stock Player_RewritePreload(playerid)
  1204. {
  1205. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] < 0)
  1206. {
  1207. return 0;
  1208. }
  1209. // First, find the player's file. This should be the ONLY place where the
  1210. // password is to be loaded.
  1211. //ret[E_USER_PRELOAD_YID] = -2;
  1212. //ret[E_USER_PRELOAD_PASS] = '\0';
  1213. //ret[E_USER_PRELOAD_LANG] = NO_LANGUAGE;
  1214. //ret[E_USER_PRELOAD_BITS] = 0;
  1215. //ret[E_USER_PRELOAD_DATE] = 0;
  1216. new
  1217. name[MAX_PLAYER_NAME + 1];
  1218. GetPlayerName(playerid, name, sizeof (name));
  1219. P:4("Player_RewritePreload called: %s", name);
  1220. #if _YSI_PLUGINS_MYSQL == 7
  1221. // Right then, lets get coding...
  1222. new
  1223. query[110];
  1224. mysql_format(YSI_g_sMySQL, query, "SELECT `uid`, `language`, `hash` FROM `y_users_register` WHERE `name` = '%e' LIMIT 0,1", name);
  1225. mysql_function_query(YSI_g_sMySQL, query, true, "_Player_Preload", "ii", );
  1226. #else
  1227. new
  1228. namelen = strlen(name),
  1229. filename[] = USER_FILE_PATH "ind_X.YSI",
  1230. File:fIndex;
  1231. Player_GetIndexFile(filename, name[0]);
  1232. fIndex = fopen(filename, io_readwrite);
  1233. if (fIndex)
  1234. {
  1235. P:5("Player_RewritePreload: fIndex OK");
  1236. new
  1237. line[INDEX_DATA_LINE_LENGTH],
  1238. len;
  1239. while ((len = fread(fIndex, line)))
  1240. {
  1241. P:6("Player_RewritePreload: while");
  1242. //new
  1243. // len;
  1244. //len = strlen(line);
  1245. // Check if the line is the right length (could be one of three
  1246. // lengths depending on newlines). Skip blanks.
  1247. if (len < INDEX_DATA_LINE_LENGTH - 3)
  1248. {
  1249. continue;
  1250. }
  1251. P:6("Player_RewritePreload: Not len");
  1252. // Check the name on the line.
  1253. if (!strcmp(line[MAX_INDEX_LENGTH + 1], name, false, namelen) && line[MAX_INDEX_LENGTH + 1 + namelen] == ' ')
  1254. {
  1255. P:6("Player_RewritePreload: checked name");
  1256. fseek(fIndex, -len, seek_current);
  1257. //format(line, sizeof (line),
  1258. format(line, sizeof (line),
  1259. "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s %04x%04x %04x%04x" INI_NEW_LINE,
  1260. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  1261. name,
  1262. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS],
  1263. Langs_GetCode(Langs_GetPlayerLanguage(playerid)),
  1264. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] & 0xFFFF,
  1265. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1266. P:5("Player_RewritePreload: Writing: %s", line);
  1267. fwrite(fIndex, line);
  1268. fclose(fIndex);
  1269. return 1;
  1270. }
  1271. }
  1272. fclose(fIndex);
  1273. }
  1274. else if (fexist(filename))
  1275. {
  1276. P:E("Error reading index %c.", filename[0]);
  1277. return 0;
  1278. }
  1279. //ret[E_USER_PRELOAD_YID] = -1;
  1280. #endif
  1281. return 0;
  1282. }
  1283. /*----------------------------------------------------------------------------*\
  1284. Function:
  1285. Player_TryGroup
  1286. Params:
  1287. playerid - Player who is joining a group.
  1288. string:other[] - A player name already in the group.
  1289. string:password[] - The password of the group.
  1290. Return:
  1291. -
  1292. Notes:
  1293. Links a player with an existing player such that they share all stats.
  1294. \*----------------------------------------------------------------------------*/
  1295. //#endinput
  1296. global Player_TryGroup(playerid, string:other[], string:password[])
  1297. {
  1298. P:2("Player_TryGroup called");
  1299. if (Player_IsLoggedIn(playerid))
  1300. {
  1301. // They are already logged in.
  1302. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1303. return 1;
  1304. }
  1305. new
  1306. hash[MAX_PASSWORD_LENGTH + 1];
  1307. Player_HashPass(password, hash);
  1308. // Check if the user is not registered already.
  1309. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1310. {
  1311. case -2:
  1312. {
  1313. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1314. return 0;
  1315. }
  1316. case -1:
  1317. {
  1318. //Text_Send(playerid, $YSI_LOGIN_NOTF);
  1319. }
  1320. default:
  1321. {
  1322. Text_Send(playerid, $YSI_REG_TAKEN);
  1323. return 0;
  1324. }
  1325. }
  1326. // Check if the new data matches the old.
  1327. new
  1328. ret[E_USER_PRELOAD];
  1329. Player_Preload(other, ret);
  1330. switch (ret[E_USER_PRELOAD_YID])
  1331. {
  1332. case -2:
  1333. {
  1334. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1335. }
  1336. case -1:
  1337. {
  1338. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1339. }
  1340. default:
  1341. {
  1342. // Match the password.
  1343. if (!strcmp(ret[E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && ret[E_USER_PRELOAD_PASS][0])
  1344. {
  1345. new
  1346. name[MAX_PLAYER_NAME + 1];
  1347. GetPlayerName(playerid, name, sizeof (name));
  1348. new
  1349. filename[64] = USER_FILE_PATH "ind_X.YSI";
  1350. Player_GetIndexFile(filename, name[0]);
  1351. new
  1352. File:fIndex = fopen(filename, io_append);
  1353. if (!fIndex)
  1354. {
  1355. Text_Send(playerid, $YSI_ADDU_INDERR2);
  1356. return 0;
  1357. }
  1358. P:5("Player_TryGroup: Write index.");
  1359. new
  1360. uid = ret[E_USER_PRELOAD_YID],
  1361. line[INDEX_DATA_LINE_LENGTH];
  1362. // Use the loaded ID.
  1363. 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]));
  1364. fwrite(fIndex, line);
  1365. fclose(fIndex);
  1366. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1367. new
  1368. INI:x = INI_Open(filename);
  1369. if (x == INI_NO_FILE)
  1370. {
  1371. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1372. }
  1373. else
  1374. {
  1375. // Add this name to the list of known names.
  1376. INI_SetTag(x, "ysi_names");
  1377. INI_WriteString(x, name, "name");
  1378. INI_Close(x);
  1379. }
  1380. // Call in all scripts.
  1381. Langs_SetPlayerLanguage(playerid, ret[E_USER_PRELOAD_LANG]);
  1382. broadcastfunc Player_DoLogin(playerid, uid);
  1383. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1384. return 1;
  1385. }
  1386. else
  1387. {
  1388. Text_Send(playerid, $YSI_LOGIN_WRONG);
  1389. }
  1390. }
  1391. }
  1392. return 0;
  1393. }
  1394. /*----------------------------------------------------------------------------*\
  1395. Function:
  1396. Player_ForceGroup
  1397. Params:
  1398. playerid - Player who is joining a group.
  1399. string:other[] - A player name already in the group.
  1400. Return:
  1401. -
  1402. Notes:
  1403. Like "Player_TryGroup", but doesn't take a password and instead just uses
  1404. the password of the old player (hashed).
  1405. \*----------------------------------------------------------------------------*/
  1406. //#endinput
  1407. global Player_ForceGroup(playerid, string:other[])
  1408. {
  1409. P:2("Player_ForceGroup called");
  1410. if (Player_IsLoggedIn(playerid))
  1411. {
  1412. // They are already logged in.
  1413. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1414. return 1;
  1415. }
  1416. // Check if the user is not registered already.
  1417. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1418. {
  1419. case -2:
  1420. {
  1421. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1422. return 0;
  1423. }
  1424. case -1:
  1425. {
  1426. //Text_Send(playerid, $YSI_LOGIN_NOTF);
  1427. }
  1428. default:
  1429. {
  1430. Text_Send(playerid, $YSI_REG_TAKEN);
  1431. return 0;
  1432. }
  1433. }
  1434. // Check if the new data matches the old.
  1435. new
  1436. ret[E_USER_PRELOAD];
  1437. Player_Preload(other, ret);
  1438. switch (ret[E_USER_PRELOAD_YID])
  1439. {
  1440. case -2:
  1441. {
  1442. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1443. }
  1444. case -1:
  1445. {
  1446. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1447. }
  1448. default:
  1449. {
  1450. new
  1451. name[MAX_PLAYER_NAME + 1];
  1452. GetPlayerName(playerid, name, sizeof (name));
  1453. new
  1454. filename[64] = USER_FILE_PATH "ind_X.YSI";
  1455. Player_GetIndexFile(filename, name[0]);
  1456. new
  1457. File:fIndex = fopen(filename, io_append);
  1458. if (!fIndex)
  1459. {
  1460. Text_Send(playerid, $YSI_ADDU_INDERR2);
  1461. return 0;
  1462. }
  1463. P:5("Player_TryGroup: Write index.");
  1464. new
  1465. uid = ret[E_USER_PRELOAD_YID],
  1466. line[INDEX_DATA_LINE_LENGTH];
  1467. // Use the loaded ID.
  1468. 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]));
  1469. fwrite(fIndex, line);
  1470. fclose(fIndex);
  1471. format(filename, sizeof (filename), USER_FILE_PATH "%0" #MAX_INDEX_LENGTH "d.INI", uid);
  1472. new
  1473. INI:x = INI_Open(filename);
  1474. if (x == INI_NO_FILE)
  1475. {
  1476. Text_Send(playerid, $YSI_LOGIN_NOLOAD);
  1477. }
  1478. else
  1479. {
  1480. // Add this name to the list of known names.
  1481. INI_SetTag(x, "ysi_names");
  1482. INI_WriteString(x, name, "name");
  1483. INI_Close(x);
  1484. }
  1485. // Call in all scripts.
  1486. Langs_SetPlayerLanguage(playerid, ret[E_USER_PRELOAD_LANG]);
  1487. broadcastfunc Player_DoLogin(playerid, uid);
  1488. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1489. return 1;
  1490. }
  1491. }
  1492. return 0;
  1493. }
  1494. static stock Player_GetNewID()
  1495. {
  1496. new
  1497. File:fHnd = fopen(USER_FILE_PATH "index.YSI", io_readwrite),
  1498. num[MAX_INDEX_LENGTH + 9],
  1499. uid = -1;
  1500. if (fHnd)
  1501. {
  1502. fread(fHnd, num);
  1503. num[strfind(num, " ")] = '\0';
  1504. uid = strval(num) + 1;
  1505. valstr(num, uid);
  1506. fseek(fHnd, 0, seek_start);
  1507. fwrite(fHnd, num);
  1508. fwrite(fHnd, " ");
  1509. fclose(fHnd);
  1510. }
  1511. return uid;
  1512. }
  1513. static stock Player_CreateNewID()
  1514. {
  1515. new
  1516. File:fHnd = fopen(USER_FILE_PATH "index.YSI", io_write);
  1517. if (fHnd)
  1518. {
  1519. fwrite(fHnd, "-1 ");
  1520. fclose(fHnd);
  1521. return 1;
  1522. }
  1523. return 0;
  1524. }