ini.inc 43 KB

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