Dan10.inc 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. #if !defined Y_USERS_SQL_HOST
  65. #define Y_USERS_SQL_HOST GetUsersSQLHost()
  66. #endif
  67. #if !defined Y_USERS_SQL_READ_USER
  68. #define Y_USERS_SQL_READ_USER GetUsersSQLReadUser()
  69. #endif
  70. #if !defined Y_USERS_SQL_READ_PASS
  71. #define Y_USERS_SQL_READ_PASS GetUsersSQLReadPass()
  72. #endif
  73. #if !defined Y_USERS_SQL_DB
  74. #define Y_USERS_SQL_DB GetUsersSQLDB()
  75. #endif
  76. #if !defined Y_USERS_SQL_PORT
  77. #define Y_USERS_SQL_PORT GetUsersSQLPort()
  78. #endif
  79. #if !defined Y_USERS_SQL_WRITE_USER
  80. #define Y_USERS_SQL_WRITE_USER Y_USERS_SQL_READ_USER
  81. #endif
  82. #if !defined Y_USERS_SQL_WRITE_PASS
  83. #define Y_USERS_SQL_WRITE_PASS Y_USERS_SQL_READ_PASS
  84. #endif
  85. #if !defined Y_USERS_SQL_MAKE_USER
  86. #define Y_USERS_SQL_MAKE_USER Y_USERS_SQL_READ_USER
  87. #endif
  88. #if !defined Y_USERS_SQL_MAKE_PASS
  89. #define Y_USERS_SQL_MAKE_PASS Y_USERS_SQL_READ_PASS
  90. #endif
  91. loadtext core[ysi_players];
  92. enum E_USER_PRELOAD
  93. {
  94. E_USER_PRELOAD_YID,
  95. Language:E_USER_PRELOAD_LANG,
  96. E_USER_PRELOAD_PASS[MAX_PASSWORD_LENGTH + 1],
  97. E_USER_PRELOAD_BITS,
  98. /*E_USER_PRELOAD_PLAY_TIME,
  99. E_USER_PRELOAD_LAST_IP,
  100. E_USER_PRELOAD_LAST_LOGIN,*/
  101. E_USER_PRELOAD_DATE
  102. }
  103. static stock
  104. YSI_g_sCurrentYID,
  105. YSI_g_sModeNameLength,
  106. YSI_g_sSQLBuffer[2048],
  107. YSI_g_sSQLQuery[256],
  108. SQL:YSI_g_sPreloadHandle,
  109. SQL:YSI_g_sLoadHandle,
  110. SQL:YSI_g_sWriteHandle,
  111. YSI_g_sPlayerYID[MAX_PLAYERS] = {-2, ...},
  112. YSI_g_sPreloadData[MAX_PLAYERS][E_USER_PRELOAD];
  113. forward y_users_SQLLoaded(playerid, uid, Result:r);
  114. forward y_users_SQLCallback(id, Result:r);
  115. /*-------------------------------------------------------------------------*//**
  116. * <remarks>
  117. * This code loads the settings for accessing your database from a file.
  118. * </remarks>
  119. *//*------------------------------------------------------------------------**/
  120. #define GetUsersSQLHost() (Player_LoadDBSettings(),YSI_g_sUsersSQLHost)
  121. #define GetUsersSQLReadUser() (Player_LoadDBSettings(),YSI_g_sUsersSQLReadUser)
  122. #define GetUsersSQLReadPass() (Player_LoadDBSettings(),YSI_g_sUsersSQLReadPass)
  123. #define GetUsersSQLDB() (Player_LoadDBSettings(),YSI_g_sUsersSQLDB)
  124. #define GetUsersSQLPort() (Player_LoadDBSettings(),YSI_g_sUsersSQLPort)
  125. static stock
  126. YSI_g_sUsersSQLHost[16] = "localhost",
  127. YSI_g_sUsersSQLReadUser[16] = "y_users_make",
  128. YSI_g_sUsersSQLReadPass[16] = "y_users_make",
  129. YSI_g_sUsersSQLDB[16] = "y_users",
  130. YSI_g_sUsersSQLPort = 3306;
  131. INI:YSI[y_users](name[], value[])
  132. {
  133. INI_String("Host", YSI_g_sUsersSQLHost, sizeof (YSI_g_sUsersSQLHost));
  134. INI_String("User", YSI_g_sUsersSQLReadUser, sizeof (YSI_g_sUsersSQLReadUser));
  135. INI_String("Pass", YSI_g_sUsersSQLReadPass, sizeof (YSI_g_sUsersSQLReadPass));
  136. INI_String("DB", YSI_g_sUsersSQLDB, sizeof (YSI_g_sUsersSQLDB));
  137. INI_Int("Port", YSI_g_sUsersSQLPort);
  138. return 0;
  139. }
  140. static stock Player_LoadDBSettings()
  141. {
  142. static
  143. bool:sLoad = true;
  144. if (sLoad)
  145. {
  146. INI_Load("YSI.ini");
  147. sLoad = false;
  148. }
  149. }
  150. /*-------------------------------------------------------------------------*//**
  151. * <param name="playerid">Player who changed name.</param>
  152. * <remarks>
  153. * Reload a player's basic data when they change name.
  154. * </remarks>
  155. *//*------------------------------------------------------------------------**/
  156. stock Player_Reload(playerid)
  157. {
  158. new
  159. name[MAX_PLAYER_NAME];
  160. GetPlayerName(playerid, name, sizeof (name)),
  161. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  162. }
  163. stock Player_SetPreload(playerid, data[E_USER_PRELOAD])
  164. {
  165. if (VALID_PLAYERID(playerid))
  166. {
  167. YSI_g_sPreloadData[playerid] = data;
  168. }
  169. }
  170. /*-------------------------------------------------------------------------*//**
  171. * <param name="playerid">Player who is logging in.</param>
  172. * <remarks>
  173. * Loads a player's data to an array.
  174. * </remarks>
  175. *//*------------------------------------------------------------------------**/
  176. stock Player_Preload(string:name[], ret[E_USER_PRELOAD])
  177. {
  178. // First, find the player's file. This should be the ONLY place where the
  179. // password is to be loaded.
  180. P:4("Player_Preload called: %s", name);
  181. ret[E_USER_PRELOAD_YID] = -1,
  182. ret[E_USER_PRELOAD_PASS] = '\0',
  183. ret[E_USER_PRELOAD_LANG] = NO_LANGUAGE,
  184. ret[E_USER_PRELOAD_BITS] = 0,
  185. ret[E_USER_PRELOAD_DATE] = 0;
  186. new
  187. safeName[MAX_PLAYER_NAME];
  188. sql_escape_string(YSI_g_sPreloadHandle, name, safeName),
  189. // I don't normally like "SELECT *" but it is appropriate here.
  190. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery), "SELECT yp.* FROM y_users_preload AS yp JOIN y_users_users WHERE name='%s';", safeName);
  191. new
  192. Result:r = sql_query(YSI_g_sPreloadHandle, YSI_g_sSQLQuery);
  193. if (sql_error(r))
  194. {
  195. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  196. return
  197. sql_error_string(r, YSI_g_sSQLQuery),
  198. sql_free_result(r),
  199. 0;
  200. }
  201. switch (sql_num_rows(r))
  202. {
  203. case 0:
  204. {
  205. ret[E_USER_PRELOAD_YID] = 0;
  206. }
  207. case 1:
  208. {
  209. // YID.
  210. sql_get_field(r, 0, YSI_g_sSQLQuery),
  211. ret[E_USER_PRELOAD_YID] = strval(YSI_g_sSQLQuery),
  212. // Hash.
  213. sql_get_field(r, 1, YSI_g_sSQLQuery),
  214. strcat(ret[E_USER_PRELOAD_PASS], YSI_g_sSQLQuery, MAX_PASSWORD_LENGTH + 1),
  215. // Language
  216. sql_get_field(r, 2, YSI_g_sSQLQuery),
  217. ret[E_USER_PRELOAD_LANG] = Langs_GetLanguage(YSI_g_sSQLQuery),
  218. // Load the 32 extra "bits".
  219. sql_get_field(r, 3, YSI_g_sSQLQuery),
  220. ret[E_USER_PRELOAD_BITS] = strval(YSI_g_sSQLQuery),
  221. // Load the user registration date (if they have one).
  222. sql_get_field(r, 4, YSI_g_sSQLQuery),
  223. ret[E_USER_PRELOAD_DATE] = strval(YSI_g_sSQLQuery);
  224. 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]);
  225. // Load their total play time.
  226. // Load their last IP.
  227. // Load their last login.
  228. }
  229. default:
  230. {
  231. P:E("y_users found mutliple YID results for %s", name);
  232. }
  233. }
  234. return
  235. sql_free_result(r),
  236. (ret[E_USER_PRELOAD_YID] > 0);
  237. }
  238. stock Language:Player_GetPreloadLanguage(playerid)
  239. {
  240. return Language:YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG];
  241. }
  242. stock Player_GetPreloadBits(playerid)
  243. {
  244. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS];
  245. }
  246. static remotefunc void:_Player_SetPreloadBits(playerid, bits)
  247. {
  248. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] = bits;
  249. }
  250. stock Player_SetPreloadBits(playerid, bits)
  251. {
  252. broadcastfunc _Player_SetPreloadBits(playerid, bits);
  253. Player_RewritePreload(playerid);
  254. }
  255. stock Player_GetPreloadDate(playerid)
  256. {
  257. if (0 <= YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] < 1234567890)
  258. {
  259. return -1;
  260. }
  261. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE];
  262. }
  263. stock Player_IsRegistered(playerid)
  264. {
  265. return YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] > 0;
  266. }
  267. hook OnScriptInit()
  268. {
  269. sql_debug(LOG_NONE, LOG_ALL);
  270. #define SQL_CLEANUP(%0) if (sql_error((%0))){sql_error_string((%0),error),P:E("SQL Error: %s",error);return sql_free_result((%0)),sql_disconnect(mkH),0;}sql_free_result((%0))
  271. YSI_g_sModeNameLength = strlen(MODE_NAME),
  272. YSI_g_sPreloadHandle = sql_connect(SQL_HANDLER_MYSQL, Y_USERS_SQL_HOST, Y_USERS_SQL_READ_USER, Y_USERS_SQL_READ_PASS, Y_USERS_SQL_DB),
  273. P:5("Users_OnScriptInit: YSI_g_sPreloadHandle = %d", _:YSI_g_sPreloadHandle);
  274. YSI_g_sLoadHandle = sql_connect(SQL_HANDLER_MYSQL, Y_USERS_SQL_HOST, Y_USERS_SQL_READ_USER, Y_USERS_SQL_READ_PASS, Y_USERS_SQL_DB),
  275. P:5("Users_OnScriptInit: YSI_g_sLoadHandle = %d", _:YSI_g_sLoadHandle);
  276. YSI_g_sWriteHandle = sql_connect(SQL_HANDLER_MYSQL, Y_USERS_SQL_HOST, Y_USERS_SQL_WRITE_USER, Y_USERS_SQL_WRITE_PASS, Y_USERS_SQL_DB);
  277. P:5("Users_OnScriptInit: YSI_g_sWriteHandle = %d", _:YSI_g_sWriteHandle);
  278. // There are two main tables.
  279. new
  280. SQL:mkH = sql_connect(SQL_HANDLER_MYSQL, Y_USERS_SQL_HOST, Y_USERS_SQL_MAKE_USER, Y_USERS_SQL_MAKE_PASS, Y_USERS_SQL_DB),
  281. error[64],
  282. // Create the table for storing users.
  283. Result:r = sql_query(mkH,
  284. "CREATE TABLE IF NOT EXISTS y_users_preload (" \
  285. "yid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY," \
  286. "password_hash CHAR(128), language CHAR(2)," \
  287. "options INT UNSIGNED, registered INT UNSIGNED," \
  288. "play_time INT UNSIGNED, last_ip INT UNSIGNED," \
  289. "last_seen DATETIME" \
  290. ") ENGINE InnoDB;");
  291. P:5("Users_OnScriptInit: mkH = %d", _:mkH);
  292. // "options" was "BIT(32)".
  293. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  294. SQL_CLEANUP(r);
  295. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  296. // Create the table for storing tags.
  297. r = sql_query(mkH,
  298. "CREATE TABLE IF NOT EXISTS y_users_modes (" \
  299. "tagid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY," \
  300. "mode VARCHAR(32) NOT NULL," \
  301. "tag VARCHAR(32)," \
  302. "INDEX (mode) USING HASH," \
  303. "UNIQUE KEY (mode, tag)" \
  304. ") ENGINE InnoDB;");
  305. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  306. SQL_CLEANUP(r);
  307. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  308. // Create the table to map names to YIDs.
  309. r = sql_query(mkH,
  310. "CREATE TABLE IF NOT EXISTS y_users_users (" \
  311. "name VARCHAR(24) NOT NULL PRIMARY KEY," \
  312. "yid INT UNSIGNED," \
  313. "CONSTRAINT FOREIGN KEY (yid)" \
  314. "REFERENCES y_users_preload (yid) ON DELETE CASCADE," \
  315. "INDEX (name) USING HASH" \
  316. ") ENGINE InnoDB;");
  317. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  318. SQL_CLEANUP(r);
  319. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  320. // Create the table for storing key/value pairs.
  321. r = sql_query(mkH,
  322. "CREATE TABLE IF NOT EXISTS y_users_keystore (" \
  323. "yid INT UNSIGNED NOT NULL," \
  324. "tagid INT UNSIGNED NOT NULL," \
  325. "vkey VARCHAR(32) NOT NULL," \
  326. "value VARCHAR(196)," \
  327. "CONSTRAINT FOREIGN KEY (tagid) REFERENCES y_users_modes (tagid) ON DELETE CASCADE," \
  328. "CONSTRAINT FOREIGN KEY (yid) REFERENCES y_users_preload (yid) ON DELETE CASCADE," \
  329. "UNIQUE KEY (yid, tagid, vkey), INDEX (yid)" \
  330. ") ENGINE InnoDB;");
  331. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  332. SQL_CLEANUP(r);
  333. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  334. // Create the table to map names to IPs.
  335. r = sql_query(mkH,
  336. "CREATE TABLE IF NOT EXISTS y_users_name_ips (" \
  337. "row BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY," \
  338. "name VARCHAR(24) NOT NULL," \
  339. "one_ip INT UNSIGNED," \
  340. "validated BOOL," \
  341. "registered DATETIME," \
  342. "leave_time DATETIME" \
  343. ") ENGINE InnoDB;");
  344. // "validated" means that this name/IP combination logged in and was
  345. // confirmed to be a valid connecting user, not someone trying to spoof a
  346. // login.
  347. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  348. SQL_CLEANUP(r);
  349. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  350. // Create the table to map YIDs to IPs.
  351. r = sql_query(mkH,
  352. "CREATE TABLE IF NOT EXISTS y_users_yid_ips (" \
  353. "row BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY," \
  354. "yid INT UNSIGNED NOT NULL," \
  355. "one_ip INT UNSIGNED," \
  356. "registered DATETIME," \
  357. "leave_time DATETIME" \
  358. ") ENGINE InnoDB;");
  359. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  360. SQL_CLEANUP(r);
  361. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  362. // Create the table to store failed logins.
  363. r = sql_query(mkH,
  364. "CREATE TABLE IF NOT EXISTS y_users_fails (" \
  365. "row BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY," \
  366. "try_yid INT UNSIGNED NOT NULL," \
  367. "from_name VARCHAR(24)," \
  368. "from_ip INT UNSIGNED," \
  369. "validated BOOL," \
  370. "attempt_time DATETIME" \
  371. ") ENGINE InnoDB;");
  372. #pragma tabsize 4 // BAD BAD BAD! HACK HACK HACK!
  373. SQL_CLEANUP(r);
  374. #pragma tabsize 4 // RESTORE FROM THE UGLINESS!
  375. sql_disconnect(mkH);
  376. return 1;
  377. #undef SQL_CLEANUP
  378. }
  379. hook OnScriptExit()
  380. {
  381. // Loop through all players.
  382. foreach (new i : Player)
  383. {
  384. Player_DoDisconnect(i);
  385. }
  386. sql_disconnect(YSI_g_sPreloadHandle),
  387. sql_disconnect(YSI_g_sLoadHandle),
  388. sql_disconnect(YSI_g_sWriteHandle);
  389. }
  390. hook OnPlayerConnect(playerid)
  391. {
  392. P:1("Users_OnPlayerConnect called: %d", playerid);
  393. new
  394. name[MAX_PLAYER_NAME];
  395. GetPlayerName(playerid, name, sizeof (name)),
  396. YSI_g_sPlayerYID[playerid] = 0,
  397. // -1 means unknown.
  398. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] = -1,
  399. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0',
  400. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG] = NO_LANGUAGE,
  401. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] = 0,
  402. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] = 0;
  403. broadcastfunc _Player_IsLoggedIn(playerid);
  404. if (existproperty(8, YSIM_LOG_IN))
  405. {
  406. new
  407. uid = getproperty(8, YSIM_LOG_IN);
  408. P:5("Users_OnPlayerConnect: Exists %d", uid);
  409. // We need the preload data even if they're already logged in.
  410. if (uid)
  411. {
  412. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  413. if (uid > 0)
  414. {
  415. // This DOES NOT use "broadcastfunc" as it's local only.
  416. Player_DoLogin(playerid, uid);
  417. }
  418. }
  419. deleteproperty(8, YSIM_LOG_IN);
  420. }
  421. else
  422. {
  423. new
  424. safeName[MAX_PLAYER_NAME],
  425. ip = GetIP(playerid);
  426. // Write this connection.
  427. sql_escape_string(YSI_g_sWriteHandle, name, safeName),
  428. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  429. "INSERT INTO y_users_name_ips VALUES (DEFAULT, '%s', 0x%04x%04x, 0, NOW(), 0)",
  430. safeName,
  431. ip >>> 16,
  432. ip & 0xFFFF),
  433. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  434. P:5("Users_OnPlayerConnect: Doesn't exist");
  435. Player_Preload(name, YSI_g_sPreloadData[playerid]);
  436. P:5("Users_OnPlayerConnect: Done Preload");
  437. // Can do checking in here to see if they just rejoined.
  438. }
  439. return 1;
  440. }
  441. /*-------------------------------------------------------------------------*//**
  442. * <param name="playerid">Player to check.</param>
  443. * <returns>
  444. * -1 - Registered but not logged in.
  445. * 0 - Not registered.
  446. * 1+ - Logged in, and their YID.
  447. * </returns>
  448. * <remarks>
  449. * This is a remote function called in existing scripts when a new script
  450. * starts. If the player is logged in it returns their YID. If a player is
  451. * registered but not logged in it returns "-1". Otherwise it returns 0.
  452. * </remarks>
  453. *//*------------------------------------------------------------------------**/
  454. static remotefunc void:_Player_IsLoggedIn(playerid)
  455. {
  456. P:4("_Player_IsLoggedIn called: %d %d", playerid, YSI_g_sPlayerYID[playerid]);
  457. if (YSI_g_sPlayerYID[playerid] > 0)
  458. {
  459. setproperty(8, YSIM_LOG_IN, YSI_g_sPlayerYID[playerid]);
  460. }
  461. else if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] > 0)
  462. {
  463. setproperty(8, YSIM_LOG_IN, -1);
  464. }
  465. else
  466. {
  467. setproperty(8, YSIM_LOG_IN, 0);
  468. }
  469. }
  470. /*-------------------------------------------------------------------------*//**
  471. * <param name="playerid">Player who left.</param>
  472. * <param name="reason">Why they left.</param>
  473. * <remarks>
  474. * Logs the player out if they're logged in.
  475. * </remarks>
  476. *//*------------------------------------------------------------------------**/
  477. hook OnPlayerDisconnect(playerid, reason)
  478. {
  479. Player_DoDisconnect(playerid);
  480. }
  481. static stock Player_DoDisconnect(playerid)
  482. {
  483. new
  484. yid = YSI_g_sPlayerYID[playerid];
  485. P:2("Users_OnPlayerDisconnect called: %d %d %d", playerid, yid);
  486. if (yid > 0)
  487. {
  488. // DO NOT broadcastfunc this in case it's just because of one script being
  489. // unloaded, not the player actually leaving (and thus do everything in
  490. // different scripts separately).
  491. Player_SaveData(playerid, true);
  492. new
  493. name[MAX_PLAYER_NAME],
  494. ip = GetIP(playerid);
  495. GetPlayerName(playerid, name, sizeof (name)),
  496. // Write this connection.
  497. sql_escape_string(YSI_g_sWriteHandle, name, name),
  498. // Save the online time for this NAME.
  499. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  500. "UPDATE y_users_name_ips SET leave_time=NOW() WHERE name='%s' ORDER BY row DESC LIMIT 1;",
  501. name),
  502. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1),
  503. // Save the total online time for this YID.
  504. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  505. "UPDATE y_users_preload SET last_ip=0x%04x%04x, last_seen=NOW(), play_time=play_time+(" \
  506. "SELECT (NOW()-leave_time) FROM y_users_yid_ips WHERE yid=%d ORDER BY row DESC LIMIT 1)" \
  507. "WHERE yid=%d;",
  508. ip >>> 16, ip & 0xFFFF, yid, yid),
  509. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1),
  510. // Save the current online time for this YID.
  511. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  512. "UPDATE y_users_yid_ips SET leave_time=NOW() WHERE yid=%d ORDER BY row DESC LIMIT 1;",
  513. yid),
  514. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  515. }
  516. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] = -1,
  517. YSI_g_sPlayerYID[playerid] = 0;
  518. }
  519. stock bool:Player_IsLoggedIn(playerid)
  520. {
  521. // -2 should never be an issue, but if it is...
  522. return YSI_g_sPlayerYID[playerid] > 0;
  523. }
  524. stock Player_GetYID(playerid)
  525. {
  526. return YSI_g_sPlayerYID[playerid];
  527. }
  528. /*-------------------------------------------------------------------------*//**
  529. * <param name="playerid">Player who is logging in.</param>
  530. * <param name="password">Password they entered.</param>
  531. * <param name="f">Show the failed to login message?</param>
  532. * <remarks>
  533. * Tries to log in a player - hashes and checks their password and if it's
  534. * right calls the core login code. It doesn't matter WHICH script does this
  535. * as they ALL get called and ALL track the login status of a player.
  536. * </remarks>
  537. *//*------------------------------------------------------------------------**/
  538. global Player_TryLogin(playerid, string:password[])
  539. {
  540. P:2("Player_TryLogin start");
  541. if (Player_IsLoggedIn(playerid))
  542. {
  543. // They are already logged in.
  544. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  545. return 1;
  546. }
  547. new
  548. hash[MAX_PASSWORD_LENGTH + 8 + 1];
  549. Player_HashPass(password, hash);
  550. 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);
  551. Player_HashPass(hash, hash);
  552. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  553. {
  554. case -1:
  555. {
  556. Text_Send(playerid, $YSI_LOGIN_INDERR);
  557. }
  558. case 0:
  559. {
  560. Text_Send(playerid, $YSI_LOGIN_NOTF);
  561. }
  562. default:
  563. {
  564. // Match the password.
  565. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0] && !strcmp(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH))
  566. {
  567. // Wipe the password from memory.
  568. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], 0, MAX_PASSWORD_LENGTH);
  569. // Extract the uid and call in to the login code.
  570. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  571. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  572. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  573. new
  574. name[MAX_PLAYER_NAME],
  575. safeName[MAX_PLAYER_NAME],
  576. ip = GetIP(playerid);
  577. GetPlayerName(playerid, name, sizeof (name)),
  578. // Write this connection.
  579. sql_escape_string(YSI_g_sWriteHandle, name, safeName),
  580. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  581. "INSERT INTO y_users_yid_ips VALUES (DEFAULT, %d, 0x%04x%04x, NOW(), NOW())",
  582. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  583. ip >>> 16,
  584. ip & 0xFFFF),
  585. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  586. return 1;
  587. }
  588. else
  589. {
  590. Text_Send(playerid, $YSI_LOGIN_WRONG);
  591. // Record the failed login in the database.
  592. new
  593. ip = GetIP(playerid);
  594. GetPlayerName(playerid, hash, sizeof (hash)),
  595. sql_escape_string(YSI_g_sWriteHandle, hash, hash),
  596. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  597. "INSERT INTO y_users_fails VALUES (DEFAULT, %d, '%s', 0x%04x%04x, 0, NOW())",
  598. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  599. hash,
  600. ip >>> 16,
  601. ip & 0xFFFF),
  602. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  603. }
  604. }
  605. }
  606. return 0;
  607. }
  608. /*-------------------------------------------------------------------------*//**
  609. * <param name="playerid">Player who is logging in.</param>
  610. * <remarks>
  611. * Like "Player_TryLogin" but doesn't take a password so always works.
  612. * </remarks>
  613. *//*------------------------------------------------------------------------**/
  614. global Player_ForceLogin(playerid)
  615. {
  616. P:2("Player_TryLogin start");
  617. if (Player_IsLoggedIn(playerid))
  618. {
  619. // They are already logged in.
  620. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  621. return 1;
  622. }
  623. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  624. {
  625. case -1:
  626. {
  627. Text_Send(playerid, $YSI_LOGIN_INDERR);
  628. }
  629. case 0:
  630. {
  631. Text_Send(playerid, $YSI_LOGIN_NOTF);
  632. }
  633. default:
  634. {
  635. // Extract the uid and call in to the login code.
  636. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0';
  637. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  638. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  639. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  640. new
  641. name[MAX_PLAYER_NAME],
  642. safeName[MAX_PLAYER_NAME],
  643. ip = GetIP(playerid);
  644. GetPlayerName(playerid, name, sizeof (name)),
  645. // Write this connection.
  646. sql_escape_string(YSI_g_sWriteHandle, name, safeName),
  647. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  648. "INSERT INTO y_users_yid_ips VALUES (DEFAULT, %d, 0x%04x%04x, NOW(), NOW())",
  649. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  650. ip >>> 16,
  651. ip & 0xFFFF),
  652. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  653. return 1;
  654. }
  655. }
  656. return 0;
  657. }
  658. remotefunc void:Player_DoLogin(playerid, uid)
  659. {
  660. // Called when a player logs in - either locally (new script) or globally
  661. // (actually only just logged in).
  662. YSI_g_sPlayerYID[playerid] = uid;
  663. // We don't need to specify how to join the tables because the definitions
  664. // use a FOREIGN KEY CONSTRAINT to map them to each other.
  665. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery), "SELECT tag, vkey, value FROM y_users_modes JOIN y_users_keystore WHERE yid=%d && mode='" #MODE_NAME "';", uid);
  666. sql_query(YSI_g_sLoadHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLLoaded", "iir", playerid, uid);
  667. //UPDATE y_users_name_ips SET validated=1 WHERE name='%s' && one_ip=0x%04x%04x && leave_time=0
  668. //format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery), "SELECT tag, vkey, value FROM y_users_modes JOIN y_users_keystore WHERE yid=%d && mode='" #MODE_NAME "';", uid);
  669. //sql_query(YSI_g_sLoadHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLLoaded", "iir", playerid, uid);
  670. }
  671. public y_users_SQLLoaded(playerid, uid, Result:r)
  672. {
  673. static
  674. func[32] = "@yU_",
  675. name[32];
  676. if (sql_error(r))
  677. {
  678. sql_error_string(r, YSI_g_sSQLQuery),
  679. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  680. return
  681. sql_free_result(r),
  682. 0;
  683. }
  684. // Finally, they've logged in.
  685. call OnPlayerLogin(playerid, uid);
  686. // Loop through all the results.
  687. new
  688. rows = sql_num_rows(r);
  689. for (new row = 0; row != rows; ++row)
  690. {
  691. // Load all the data in a YSI-compatible way.
  692. sql_next_row(r, row),
  693. sql_get_field(r, 0, func[4]),
  694. sql_get_field(r, 1, name),
  695. sql_get_field(r, 2, YSI_g_sSQLQuery),
  696. CallLocalFunction(func, "iss", playerid, name, YSI_g_sSQLQuery);
  697. }
  698. return
  699. sql_free_result(r),
  700. 1;
  701. }
  702. public y_users_SQLCallback(id, Result:r)
  703. {
  704. if (sql_error(r))
  705. {
  706. if (id == 3 && sql_error(r) == 1452) goto y_users_SQLCallback_done;
  707. sql_error_string(r, YSI_g_sSQLQuery),
  708. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  709. return
  710. sql_free_result(r),
  711. 0;
  712. }
  713. y_users_SQLCallback_done:
  714. return
  715. sql_free_result(r),
  716. 1;
  717. }
  718. /*-------------------------------------------------------------------------*//**
  719. * <param name="name">Item to remove.</param>
  720. * <remarks>
  721. * Wrapper for Player_AddToBuffer for removing data.
  722. * </remarks>
  723. *//*------------------------------------------------------------------------**/
  724. stock Player_RemoveEntry(name[])
  725. {
  726. // First, flush the main buffer so we can add anything we might want to
  727. // remove.
  728. Player_FlushData();
  729. sql_escape_string(YSI_g_sWriteHandle, name, YSI_g_sSQLQuery),
  730. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery), "DELETE FROM y_users_keystore WHERE yid=%d && tagid=@T && vkey='%s';", YSI_g_sCurrentYID, YSI_g_sSQLQuery),
  731. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 1);
  732. }
  733. /*-------------------------------------------------------------------------*//**
  734. * <param name="name">Data name.</param>
  735. * <param name="data">Data.</param>
  736. * <remarks>
  737. * Wrapper for Player_AddToBuffer for strings.
  738. * </remarks>
  739. *//*------------------------------------------------------------------------**/
  740. stock Player_WriteString(name[], data[])
  741. {
  742. static
  743. sPart[196 + 32 + 10 + 10 + 10];
  744. format(sPart, sizeof (YSI_g_sSQLBuffer), "(%d,@T,'", YSI_g_sCurrentYID);
  745. sql_escape_string(YSI_g_sWriteHandle, name, sPart[strlen(sPart)])
  746. strcat(sPart, "','")
  747. sql_escape_string(YSI_g_sWriteHandle, data, sPart[strlen(sPart)])
  748. strcat(sPart, "')")
  749. // Already has some query buffered.
  750. if (YSI_g_sSQLBuffer[0])
  751. {
  752. if (strlen(YSI_g_sSQLBuffer) < sizeof (YSI_g_sSQLBuffer) - 44 - 2 - strlen(sPart))
  753. {
  754. // Enough space for the new values and the standard suffix.
  755. strcat(YSI_g_sSQLBuffer, ",");
  756. goto Player_WriteString_run;
  757. }
  758. strcat(YSI_g_sSQLBuffer, "ON DUPLICATE KEY UPDATE value=values(value);");
  759. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLBuffer, QUERY_THREADED, "y_users_SQLCallback", "ir", 3);
  760. }
  761. YSI_g_sSQLBuffer = "INSERT INTO y_users_keystore(yid,tagid,vkey,value)VALUES";
  762. Player_WriteString_run:
  763. strcat(YSI_g_sSQLBuffer, sPart);
  764. }
  765. stock Player_WriteArray(const name[], data[], len)
  766. {
  767. // INI_WriteArray(YSI_g_sPlayerWriteFile, name, data, len);
  768. return 1;
  769. }
  770. /*-------------------------------------------------------------------------*//**
  771. * <param name="name">Data name.</param>
  772. * <param name="data">Integer data.</param>
  773. * <remarks>
  774. * Wrapper for Player_AddToBuffer for integers.
  775. * </remarks>
  776. *//*------------------------------------------------------------------------**/
  777. stock Player_WriteInt(name[], data)
  778. {
  779. static
  780. sVal[12];
  781. strval(sVal, data);
  782. Player_WriteString(name, sVal);
  783. }
  784. /*-------------------------------------------------------------------------*//**
  785. * <param name="name">Data name.</param>
  786. * <param name="data">Hex data.</param>
  787. * <remarks>
  788. * Wrapper for Player_AddToBuffer for integers to be written as hex values.
  789. * </remarks>
  790. *//*------------------------------------------------------------------------**/
  791. stock Player_WriteHex(name[], data)
  792. {
  793. static
  794. sVal[11];
  795. format(sVal, sizeof (sVal), "0x%04x%04x", data >>> 16, data & 0xFFFF);
  796. Player_WriteString(name, data);
  797. }
  798. /*-------------------------------------------------------------------------*//**
  799. * <param name="name">Data name.</param>
  800. * <param name="data">Binary data.</param>
  801. * <remarks>
  802. * Wrapper for Player_AddToBuffer for integers to be written as binary values.
  803. * </remarks>
  804. *//*------------------------------------------------------------------------**/
  805. stock Player_WriteBin(name[], data)
  806. {
  807. static const
  808. sc_values[] = !"0000" "0001" "0010" "0011" "0100" "0101" "0110" "0111" "1000" "1001" "1010" "1011" "1100" "1101" "1110" "1111";
  809. static
  810. str[35];
  811. new
  812. i = 10;
  813. do
  814. {
  815. str[--i] = sc_values[data & 0x0F];
  816. data >>>= 4;
  817. }
  818. while (data);
  819. // Convert the coalesced values to individual values.
  820. strunpack(str[i], str[i], 33);
  821. str[--i] = 'b';
  822. str[--i] = '0';
  823. Player_WriteString(name, str);
  824. }
  825. /*-------------------------------------------------------------------------*//**
  826. * <param name="name">Data name.</param>
  827. * <param name="data">Boolean data.</param>
  828. * <remarks>
  829. * Wrapper for Player_AddToBuffer for booleans.
  830. * </remarks>
  831. *//*------------------------------------------------------------------------**/
  832. stock Player_WriteBool(name[], bool:data)
  833. {
  834. Player_WriteString(name, data ? ("true") : ("false"));
  835. }
  836. /*-------------------------------------------------------------------------*//**
  837. * <param name="name">Data name.</param>
  838. * <param name="data">Float data.</param>
  839. * <param name="accuracy">number of decimal places to write.</param>
  840. * <remarks>
  841. * Wrapper for Player_AddToBuffer for floats. Uses custom code instead of
  842. * format() as it's actually faster for something simple like this.
  843. * </remarks>
  844. *//*------------------------------------------------------------------------**/
  845. stock Player_WriteFloat(name[], Float:data, accuracy = 6)
  846. {
  847. static
  848. str[16];
  849. format(str, sizeof (str), "%.*f", accuracy, data);
  850. Player_WriteString(name, str);
  851. }
  852. static stock Player_FlushData()
  853. {
  854. if (YSI_g_sSQLBuffer[0])
  855. {
  856. new
  857. len = strlen(YSI_g_sSQLBuffer) + 44;
  858. if (len >= sizeof (YSI_g_sSQLBuffer))
  859. {
  860. P:E("Could not save y_users buffer (should not happen).");
  861. return 0;
  862. }
  863. strcat(YSI_g_sSQLBuffer, "ON DUPLICATE KEY UPDATE value=values(value);");
  864. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLBuffer, QUERY_THREADED, "y_users_SQLCallback", "ir", 3),
  865. YSI_g_sSQLBuffer[0] = '\0';
  866. }
  867. return 1;
  868. }
  869. stock Player_SetTag(tag[])
  870. {
  871. Player_FlushData();
  872. sql_escape_string(YSI_g_sWriteHandle, tag, YSI_g_sSQLQuery),
  873. // Run the second query. Use "INSERT IGNORE" to add the tag only if it
  874. // doesn't exist already. The length check here is wrong, but making it
  875. // correct would take more effort and we've already checked the length.
  876. // This stores the tagid in to a global MySQL variable.
  877. format(YSI_g_sSQLBuffer, sizeof (YSI_g_sSQLBuffer),
  878. "INSERT IGNORE INTO y_users_modes(tag,mode)VALUES('%s','" #MODE_NAME "');" \
  879. "SELECT @T:=tagid FROM y_users_modes WHERE tag='%s'&&mode='" #MODE_NAME "';",
  880. YSI_g_sSQLQuery, YSI_g_sSQLQuery),
  881. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLBuffer, QUERY_THREADED, "y_users_SQLCallback", "ir", 4),
  882. YSI_g_sSQLBuffer[0] = '\0';
  883. }
  884. stock Player_DeleteTag(tag[])
  885. {
  886. // Uses "CASCADE ON DELETE".
  887. sql_escape_string(YSI_g_sWriteHandle, tag, YSI_g_sSQLQuery),
  888. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery), "DELETE FROM y_users_modes WHERE tag='%s' && mode='" #MODE_NAME "';", YSI_g_sSQLQuery),
  889. sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery, QUERY_THREADED, "y_users_SQLCallback", "ir", 5);
  890. // Any future data written to this tag will just fail the constraint.
  891. }
  892. stock Player_SaveData(playerid, bool:logout = false)
  893. {
  894. // Just in case they decide to save a player's data in the middle of another
  895. // player's data being saved...
  896. new
  897. stk = YSI_g_sCurrentYID;
  898. if (YSI_g_sPlayerYID[playerid] > 0)
  899. {
  900. // Flush any old data.
  901. Player_FlushData();
  902. // New player's data.
  903. YSI_g_sCurrentYID = YSI_g_sPlayerYID[playerid];
  904. Player_SetTag("");
  905. CallLocalFunction("OnSavePlayerData", "ii", playerid, YSI_g_sCurrentYID);
  906. if (logout)
  907. {
  908. Player_SetTag("");
  909. call OnPlayerLogout(playerid, YSI_g_sCurrentYID);
  910. }
  911. // Flush new data.
  912. Player_FlushData();
  913. }
  914. YSI_g_sCurrentYID = stk;
  915. }
  916. /*-------------------------------------------------------------------------*//**
  917. * <param name="pass">Data to hash.</param>
  918. * <remarks>
  919. * Based on my Dad's hash system but slightly modifed. Updated for reverse
  920. * compatability with other login systems. Needs more code for Whirlpool.
  921. * </remarks>
  922. *//*------------------------------------------------------------------------**/
  923. static stock Player_HashPass(pass[], target[])
  924. {
  925. #if defined PP_ADLER32
  926. new
  927. s1 = 1,
  928. s2 = 0,
  929. i,
  930. You_REALLY_shouldnt_use_Adler32;
  931. while (pass[i])
  932. {
  933. s1 = (s1 + pass[i++]) % 65521,
  934. s2 = (s2 + s1) % 65521;
  935. }
  936. format(target, sizeof (target), "%" #MAX_PASSWORD_LENGTH "d", (s2 << 16) + s1);
  937. #elseif defined PP_MD5 && defined MD5_Hash
  938. new
  939. You_REALLY_shouldnt_use_MD5;
  940. strcpy(target, MD5_Hash(pass, strlen(pass)));
  941. #elseif defined PP_SHA1
  942. #error SHA1 unsupported.
  943. #elseif defined PP_YSI
  944. static
  945. 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",
  946. css = 99;
  947. new
  948. //target[MAX_PASSWORD_LENGTH + 1],
  949. j,
  950. sum = j,
  951. tmp = 0,
  952. i,
  953. mod;
  954. j = strlen(pass);
  955. for (i = 0; i < MAX_PASSWORD_LENGTH || i < j; i++)
  956. {
  957. mod = i % MAX_PASSWORD_LENGTH,
  958. tmp = (i >= j) ? charset[(7 * i) % css] : pass[i],
  959. sum = (sum + chrfind(tmp, charset) + 1) % css,
  960. target[mod] = charset[(sum + target[mod]) % css];
  961. }
  962. target[MAX_PASSWORD_LENGTH] = '\0';
  963. //return target;
  964. #elseif defined WP_Hash
  965. WP_Hash(target, MAX_PASSWORD_LENGTH + 1, pass);
  966. #else
  967. #error Whirlpool (or other) hash not found.
  968. #endif
  969. }
  970. // Hooray for bizare bugs! I think this is because the function above is
  971. // secretly a macro with "if/else" and a block statement, not a real function.
  972. // Only now it isn't, but honestly I'm no longer sure what this was fixing or if
  973. // it is still needed or not. Even if it isn't there's no point removing it as
  974. // it was clearly important at some point in the past!
  975. stock Player_SomeWeirdBugFix()
  976. {
  977. }
  978. /*-------------------------------------------------------------------------*//**
  979. * <param name="playerid">Player who is registering.</param>
  980. * <param name="password">The password they entered.</param>
  981. * <remarks>
  982. * Register the player with the given password if there is no-one else with the
  983. * name already. Or log them in if the username and password match an existing
  984. * account. Note that there is no "Player_ForceRegister" as it would do the
  985. * same thing with no less parameters (a password MUST be given to write in the
  986. * file).
  987. * </remarks>
  988. *//*------------------------------------------------------------------------**/
  989. global Player_TryRegister(playerid, string:password[])
  990. {
  991. P:2("Player_TryRegister called");
  992. if (Player_IsLoggedIn(playerid))
  993. {
  994. // They are already logged in.
  995. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  996. return 1;
  997. }
  998. new
  999. hash[MAX_PASSWORD_LENGTH + 1 + 8];
  1000. Player_HashPass(password, hash);
  1001. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1002. {
  1003. case -1:
  1004. {
  1005. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1006. return 0;
  1007. }
  1008. case 0:
  1009. {
  1010. }
  1011. default:
  1012. {
  1013. // Get the salt.
  1014. 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);
  1015. Player_HashPass(hash, hash);
  1016. // Match the password.
  1017. if (!strcmp(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS][0])
  1018. {
  1019. // Extract the uid and call in to the login code.
  1020. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS] = '\0';
  1021. Langs_SetPlayerLanguage(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_LANG]);
  1022. broadcastfunc Player_DoLogin(playerid, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID]);
  1023. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1024. return 1;
  1025. }
  1026. else
  1027. {
  1028. Text_Send(playerid, $YSI_REG_TAKEN);
  1029. return 0;
  1030. }
  1031. }
  1032. }
  1033. // Flush pending updates as we need to use "YSI_g_sSQLBuffer", mainly
  1034. // (entirely) because the hash pushes us well over the 256 limit for using
  1035. // "YSI_g_sSQLQuery" so we need the larger buffer.
  1036. Player_FlushData();
  1037. new
  1038. name[MAX_PLAYER_NAME + 1];
  1039. GetPlayerName(playerid, name, sizeof (name)),
  1040. sql_escape_string(YSI_g_sWriteHandle, name, name);
  1041. // Hash the password a second time.
  1042. new
  1043. d = gettime(),
  1044. date[9];
  1045. format(date, sizeof (date), "%04x%04x", d >>> 16, d & 0xFFFF),
  1046. strcat(hash, date),
  1047. Player_HashPass(hash, hash),
  1048. // Create a new YID, then associate this name with it.
  1049. format(YSI_g_sSQLBuffer, sizeof (YSI_g_sSQLBuffer),
  1050. "INSERT INTO y_users_preload VALUES (DEFAULT, '%s', '%s', 0, 0x%s, 0, 0, 0);" \
  1051. "SELECT @Y:=LAST_INSERT_ID();" \
  1052. "INSERT INTO y_users_users VALUES ('%s', @Y);" \
  1053. "SELECT @Y;",
  1054. hash, Langs_GetCode(Langs_GetPlayerLanguage(playerid)), date, name);
  1055. new
  1056. Result:r = sql_query(YSI_g_sWriteHandle, YSI_g_sSQLBuffer);
  1057. YSI_g_sSQLBuffer[0] = '\0';
  1058. if (sql_error(r))
  1059. {
  1060. sql_error_string(r, YSI_g_sSQLQuery),
  1061. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  1062. return
  1063. Text_Send(playerid, $YSI_ADDU_INDER2),
  1064. sql_free_result(r),
  1065. 0;
  1066. }
  1067. sql_get_field(r, 0, name),
  1068. sql_free_result(r),
  1069. broadcastfunc Player_DoLogin(playerid, strval(name)),
  1070. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1071. return 1;
  1072. }
  1073. #endinput
  1074. global Player_ChangePassword(playerid, string:password[])
  1075. {
  1076. #pragma unused password
  1077. new
  1078. uid = Player_GetYID(playerid);
  1079. if (uid <= 0)
  1080. {
  1081. return 0;
  1082. }
  1083. // Create the new password.
  1084. new
  1085. hash[MAX_PASSWORD_LENGTH + 8 + 1];
  1086. Player_HashPass(password, hash);
  1087. 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);
  1088. Player_HashPass(hash, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS]);
  1089. // Save it.
  1090. Player_RewritePreload(playerid);
  1091. // Wipe it.
  1092. memset(YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS], 0, MAX_PASSWORD_LENGTH);
  1093. return 1;
  1094. }
  1095. /*-------------------------------------------------------------------------*//**
  1096. * <param name="playerid">Player whose data should be saved.</param>
  1097. * <remarks>
  1098. * When a player's preload data is modifed (new bit data or changed password),
  1099. * it needs to be written back out to file.
  1100. * </remarks>
  1101. *//*------------------------------------------------------------------------**/
  1102. static stock Player_RewritePreload(playerid)
  1103. {
  1104. if (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID] <= 0)
  1105. {
  1106. return 0;
  1107. }
  1108. // First, find the player's file. This should be the ONLY place where the
  1109. // password is to be loaded.
  1110. new
  1111. name[MAX_PLAYER_NAME + 1];
  1112. GetPlayerName(playerid, name, sizeof (name));
  1113. P:4("Player_RewritePreload called: %s", name);
  1114. #if _YSI_PLUGINS_MYSQL == 7
  1115. // Right then, lets get coding...
  1116. new
  1117. query[110];
  1118. mysql_format(YSI_g_sMySQL, query, "SELECT `uid`, `language`, `hash` FROM `y_users_register` WHERE `name` = '%e' LIMIT 1", name);
  1119. mysql_function_query(YSI_g_sMySQL, query, true, "_Player_Preload", "ii", );
  1120. #else
  1121. new
  1122. namelen = strlen(name),
  1123. filename[] = USER_FILE_PATH "ind_X.YSI",
  1124. File:fIndex;
  1125. Player_GetIndexFile(filename, name[0]);
  1126. fIndex = fopen(filename, io_readwrite);
  1127. if (fIndex)
  1128. {
  1129. P:5("Player_RewritePreload: fIndex OK");
  1130. new
  1131. line[INDEX_DATA_LINE_LENGTH],
  1132. len;
  1133. while ((len = fread(fIndex, line)))
  1134. {
  1135. P:6("Player_RewritePreload: while");
  1136. // Check if the line is the right length (could be one of three
  1137. // lengths depending on newlines). Skip blanks.
  1138. if (len < INDEX_DATA_LINE_LENGTH - 3)
  1139. {
  1140. continue;
  1141. }
  1142. P:6("Player_RewritePreload: Not len");
  1143. // Check the name on the line.
  1144. if (!strcmp(line[MAX_INDEX_LENGTH + 1], name, false, namelen) && line[MAX_INDEX_LENGTH + 1 + namelen] == ' ')
  1145. {
  1146. P:6("Player_RewritePreload: checked name");
  1147. fseek(fIndex, -len, seek_current);
  1148. format(line, sizeof (line),
  1149. "%0" #MAX_INDEX_LENGTH "d %" #MAX_PLAYER_NAME "s %" #MAX_PASSWORD_LENGTH "s %02s %04x%04x %04x%04x" INI_NEW_LINE,
  1150. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID],
  1151. name,
  1152. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_PASS],
  1153. Langs_GetCode(Langs_GetPlayerLanguage(playerid)),
  1154. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_BITS] & 0xFFFF,
  1155. YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] >>> 16, YSI_g_sPreloadData[playerid][E_USER_PRELOAD_DATE] & 0xFFFF);
  1156. P:5("Player_RewritePreload: Writing: %s", line);
  1157. fwrite(fIndex, line);
  1158. fclose(fIndex);
  1159. return 1;
  1160. }
  1161. }
  1162. fclose(fIndex);
  1163. }
  1164. else if (fexist(filename))
  1165. {
  1166. P:E("Error reading index %c.", filename[0]);
  1167. return 0;
  1168. }
  1169. //ret[E_USER_PRELOAD_YID] = 0;
  1170. #endif
  1171. return 0;
  1172. }
  1173. /*-------------------------------------------------------------------------*//**
  1174. * <param name="playerid">Player who is joining a group.</param>
  1175. * <param name="other">A player name already in the group.</param>
  1176. * <param name="password">The password of the group.</param>
  1177. * <remarks>
  1178. * Links a player with an existing player such that they share all stats.
  1179. * </remarks>
  1180. *//*------------------------------------------------------------------------**/
  1181. global Player_TryGroup(playerid, string:other[], string:password[])
  1182. {
  1183. P:2("Player_TryGroup called");
  1184. if (Player_IsLoggedIn(playerid))
  1185. {
  1186. // They are already logged in.
  1187. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1188. return 1;
  1189. }
  1190. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1191. {
  1192. case -1:
  1193. {
  1194. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1195. return 0;
  1196. }
  1197. case 0:
  1198. {
  1199. }
  1200. default:
  1201. {
  1202. Text_Send(playerid, $YSI_REG_TAKEN);
  1203. return 0;
  1204. }
  1205. }
  1206. // Check if the new data matches the old.
  1207. new
  1208. ret[E_USER_PRELOAD];
  1209. Player_Preload(other, ret);
  1210. switch (ret[E_USER_PRELOAD_YID])
  1211. {
  1212. case -1:
  1213. {
  1214. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1215. }
  1216. case 0:
  1217. {
  1218. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1219. }
  1220. default:
  1221. {
  1222. new
  1223. hash[MAX_PASSWORD_LENGTH + 1 + 8],
  1224. d = ret[E_USER_PRELOAD_DATE],
  1225. date[9];
  1226. // Hash the password twice, second time with the salt.
  1227. Player_HashPass(password, hash),
  1228. format(date, sizeof (date), "%04x%04x", d >>> 16, d & 0xFFFF),
  1229. strcat(hash, date),
  1230. Player_HashPass(hash, hash);
  1231. // Match the password.
  1232. if (!strcmp(ret[E_USER_PRELOAD_PASS], hash, false, MAX_PASSWORD_LENGTH) && ret[E_USER_PRELOAD_PASS][0])
  1233. {
  1234. new
  1235. name[MAX_PLAYER_NAME + 1];
  1236. GetPlayerName(playerid, name, sizeof (name)),
  1237. sql_escape_string(YSI_g_sWriteHandle, name, name),
  1238. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  1239. "INSERT INTO y_users_users VALUES ('%s', %d);",
  1240. name, ret[E_USER_PRELOAD_YID]);
  1241. new
  1242. Result:r = sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery);
  1243. if (sql_error(r))
  1244. {
  1245. sql_error_string(r, YSI_g_sSQLQuery),
  1246. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  1247. return
  1248. Text_Send(playerid, $YSI_ADDU_INDER2),
  1249. sql_free_result(r),
  1250. 0;
  1251. }
  1252. sql_free_result(r),
  1253. // Copy over the preload data.
  1254. YSI_g_sPreloadData[playerid] = ret,
  1255. broadcastfunc Player_DoLogin(playerid, ret[E_USER_PRELOAD_YID]),
  1256. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1257. }
  1258. else
  1259. {
  1260. Text_Send(playerid, $YSI_LOGIN_WRONG);
  1261. return 0;
  1262. }
  1263. }
  1264. }
  1265. return 1;
  1266. }
  1267. /*-------------------------------------------------------------------------*//**
  1268. * <param name="playerid">Player who is joining a group.</param>
  1269. * <param name="other">A player name already in the group.</param>
  1270. * <remarks>
  1271. * Like "Player_TryGroup", but doesn't take a password and instead just uses
  1272. * the password of the old player (hashed).
  1273. * </remarks>
  1274. *//*------------------------------------------------------------------------**/
  1275. global Player_ForceGroup(playerid, string:other[])
  1276. {
  1277. P:2("Player_ForceGroup called");
  1278. if (Player_IsLoggedIn(playerid))
  1279. {
  1280. // They are already logged in.
  1281. Text_Send(playerid, $YSI_LOGIN_ALREADY);
  1282. return 1;
  1283. }
  1284. switch (YSI_g_sPreloadData[playerid][E_USER_PRELOAD_YID])
  1285. {
  1286. case -1:
  1287. {
  1288. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1289. return 0;
  1290. }
  1291. case 0:
  1292. {
  1293. }
  1294. default:
  1295. {
  1296. Text_Send(playerid, $YSI_REG_TAKEN);
  1297. return 0;
  1298. }
  1299. }
  1300. // Check if the new data matches the old.
  1301. new
  1302. ret[E_USER_PRELOAD];
  1303. Player_Preload(other, ret);
  1304. switch (ret[E_USER_PRELOAD_YID])
  1305. {
  1306. case -1:
  1307. {
  1308. Text_Send(playerid, $YSI_LOGIN_INDERR);
  1309. }
  1310. case 0:
  1311. {
  1312. Text_Send(playerid, $YSI_LOGIN_NOTF);
  1313. }
  1314. default:
  1315. {
  1316. new
  1317. name[MAX_PLAYER_NAME + 1];
  1318. GetPlayerName(playerid, name, sizeof (name)),
  1319. sql_escape_string(YSI_g_sWriteHandle, name, name),
  1320. format(YSI_g_sSQLQuery, sizeof (YSI_g_sSQLQuery),
  1321. "INSERT INTO y_users_users VALUES ('%s', %d);",
  1322. name, ret[E_USER_PRELOAD_YID]);
  1323. new
  1324. Result:r = sql_query(YSI_g_sWriteHandle, YSI_g_sSQLQuery);
  1325. if (sql_error(r))
  1326. {
  1327. sql_error_string(r, YSI_g_sSQLQuery),
  1328. P:E("SQL Error: %s", YSI_g_sSQLQuery);
  1329. return
  1330. Text_Send(playerid, $YSI_ADDU_INDER2),
  1331. sql_free_result(r),
  1332. 0;
  1333. }
  1334. sql_free_result(r),
  1335. // Copy over the preload data.
  1336. YSI_g_sPreloadData[playerid] = ret,
  1337. broadcastfunc Player_DoLogin(playerid, ret[E_USER_PRELOAD_YID]),
  1338. Text_Send(playerid, $YSI_LOGIN_LOGIN);
  1339. }
  1340. }
  1341. return 1;
  1342. }