char.c 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. // original code from athena
  4. // SQL conversion by Jioh L. Jung
  5. // TXT 1.105
  6. #include <sys/types.h>
  7. #ifdef _WIN32
  8. #include <winsock.h>
  9. typedef long in_addr_t;
  10. //#pragma lib <libmysql.lib> // Not required [Lance]
  11. #else
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <time.h>
  21. #include <signal.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "char.h"
  28. #include "../common/utils.h"
  29. #include "../common/strlib.h"
  30. #include "../common/showmsg.h"
  31. #include "itemdb.h"
  32. #include "inter.h"
  33. #include "db.h"
  34. #include "malloc.h"
  35. #include "int_guild.h"
  36. static struct dbt *char_db_;
  37. char char_db[256] = "char";
  38. char scdata_db[256] = "sc_data";
  39. char cart_db[256] = "cart_inventory";
  40. char inventory_db[256] = "inventory";
  41. char charlog_db[256] = "charlog";
  42. char storage_db[256] = "storage";
  43. char interlog_db[256] = "interlog";
  44. char reg_db[256] = "global_reg_value";
  45. char skill_db[256] = "skill";
  46. char memo_db[256] = "memo";
  47. char guild_db[256] = "guild";
  48. char guild_alliance_db[256] = "guild_alliance";
  49. char guild_castle_db[256] = "guild_castle";
  50. char guild_expulsion_db[256] = "guild_expulsion";
  51. char guild_member_db[256] = "guild_member";
  52. char guild_position_db[256] = "guild_position";
  53. char guild_skill_db[256] = "guild_skill";
  54. char guild_storage_db[256] = "guild_storage";
  55. char party_db[256] = "party";
  56. char pet_db[256] = "pet";
  57. char friend_db[256] = "friends";
  58. int db_use_sqldbs;
  59. char login_db_account_id[32] = "account_id";
  60. char login_db_level[32] = "level";
  61. int lowest_gm_level = 1;
  62. char *SQL_CONF_NAME = "conf/inter_athena.conf";
  63. struct mmo_map_server server[MAX_MAP_SERVERS];
  64. int server_fd[MAX_MAP_SERVERS];
  65. int login_fd, char_fd;
  66. char userid[24];
  67. char passwd[24];
  68. char server_name[20];
  69. char wisp_server_name[NAME_LENGTH] = "Server";
  70. int login_ip_set_ = 0;
  71. char login_ip_str[128];
  72. in_addr_t login_ip;
  73. int login_port = 6900;
  74. int char_ip_set_ = 0;
  75. char char_ip_str[128];
  76. int bind_ip_set_ = 0;
  77. char bind_ip_str[128];
  78. in_addr_t char_ip;
  79. int char_port = 6121;
  80. int char_maintenance;
  81. int char_new;
  82. int char_new_display;
  83. int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor]
  84. int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  85. char char_name_letters[1024] = ""; // list of letters/symbols used to authorise or not a name of a character. by [Yor]
  86. //The following are characters that are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  87. #define TRIM_CHARS "\032\t\n "
  88. int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius]
  89. int log_char = 1; // loggin char or not [devil]
  90. int log_inter = 1; // loggin inter or not [devil]
  91. char lan_map_ip[128]; // Lan map ip added by kashy
  92. int subnetmaski[4]; // Subnetmask added by kashy
  93. char unknown_char_name[NAME_LENGTH] = "Unknown";
  94. char db_path[1024]="db";
  95. //These are used to aid the map server in identifying valid clients. [Skotlex]
  96. static int max_account_id = DEFAULT_MAX_ACCOUNT_ID, max_char_id = DEFAULT_MAX_CHAR_ID;
  97. static int online_check = 1; //If one, it won't let players connect when their account is already registered online and will send the relevant map server a kick user request. [Skotlex]
  98. struct char_session_data{
  99. int account_id, login_id1, login_id2,sex;
  100. int found_char[9];
  101. char email[40]; // e-mail (default: a@a.com) by [Yor]
  102. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  103. };
  104. #define AUTH_FIFO_SIZE 256
  105. struct {
  106. int account_id, char_id, login_id1, login_id2, ip, char_pos, delflag,sex;
  107. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  108. } auth_fifo[AUTH_FIFO_SIZE];
  109. int auth_fifo_pos = 0;
  110. int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system)
  111. int char_id_count = START_CHAR_NUM;
  112. struct mmo_charstatus *char_dat;
  113. int char_num,char_max;
  114. int max_connect_user = 0;
  115. int gm_allow_level = 99;
  116. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  117. int save_log = 1;
  118. int start_zeny = 500;
  119. int start_weapon = 1201;
  120. int start_armor = 2301;
  121. // check for exit signal
  122. // 0 is saving complete
  123. // other is char_id
  124. unsigned int save_flag = 0;
  125. // start point (you can reset point on conf file)
  126. struct point start_point = { 0, 53, 111};
  127. struct gm_account *gm_account = NULL;
  128. int GM_num = 0;
  129. int console = 0;
  130. //Structure for holding in memory which characters are online on the map servers connected.
  131. struct online_char_data {
  132. int account_id;
  133. int char_id;
  134. short server;
  135. unsigned waiting_disconnect :1;
  136. };
  137. struct dbt *online_char_db; //Holds all online characters.
  138. #ifndef SQL_DEBUG
  139. #define mysql_query(_x, _y) mysql_real_query(_x, _y, strlen(_y)) //supports ' in names and runs faster [Kevin]
  140. #else
  141. #define mysql_query(_x, _y) debug_mysql_query(__FILE__, __LINE__, _x, _y)
  142. #endif
  143. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data);
  144. static void * create_online_char_data(DBKey key, va_list args) {
  145. struct online_char_data* character;
  146. character = aCalloc(1, sizeof(struct online_char_data));
  147. character->account_id = key.i;
  148. character->char_id = -1;
  149. character->server = -1;
  150. return character;
  151. }
  152. //-------------------------------------------------
  153. // Set Character online/offline [Wizputer]
  154. //-------------------------------------------------
  155. void set_char_online(int map_id, int char_id, int account_id) {
  156. struct online_char_data* character;
  157. if ( char_id != 99 ) {
  158. sprintf(tmp_sql, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d'",char_db,char_id);
  159. if (mysql_query(&mysql_handle, tmp_sql)) {
  160. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  161. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  162. }
  163. if (max_account_id < account_id || max_char_id < char_id)
  164. { //Notify map-server of the new max IDs [Skotlex]
  165. if (account_id > max_account_id)
  166. max_account_id = account_id;
  167. if (char_id > max_char_id)
  168. max_char_id = char_id;
  169. mapif_send_maxid(max_account_id, max_char_id);
  170. }
  171. }
  172. character = idb_ensure(online_char_db, account_id, create_online_char_data);
  173. if (online_check && character->char_id != -1 && character->server > -1 && character->server != map_id)
  174. {
  175. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  176. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  177. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  178. }
  179. character->char_id = (char_id==99)?-1:char_id;
  180. character->server = (char_id==99)?-1:map_id;
  181. character->waiting_disconnect = 0;
  182. if (char_id != 99)
  183. { //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  184. struct mmo_charstatus *cp;
  185. cp = idb_get(char_db_,char_id);
  186. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  187. }
  188. if (login_fd <= 0 || session[login_fd]->eof)
  189. return;
  190. WFIFOW(login_fd,0) = 0x272b;
  191. WFIFOL(login_fd,2) = account_id;
  192. WFIFOSET(login_fd,6);
  193. }
  194. void set_char_offline(int char_id, int account_id) {
  195. struct mmo_charstatus *cp;
  196. struct online_char_data* character;
  197. if ( char_id == 99 )
  198. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id);
  199. else {
  200. cp = idb_get(char_db_,char_id);
  201. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  202. if (cp)
  203. idb_remove(char_db_,char_id);
  204. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, char_id);
  205. if (mysql_query(&mysql_handle, tmp_sql))
  206. {
  207. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  208. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  209. }
  210. }
  211. if ((character = idb_get(online_char_db, account_id)) != NULL)
  212. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  213. character->char_id = -1;
  214. character->server = -1;
  215. character->waiting_disconnect = 0;
  216. }
  217. if (login_fd <= 0 || session[login_fd]->eof)
  218. return;
  219. WFIFOW(login_fd,0) = 0x272c;
  220. WFIFOL(login_fd,2) = account_id;
  221. WFIFOSET(login_fd,6);
  222. }
  223. static int char_db_setoffline(DBKey key, void* data, va_list ap) {
  224. struct online_char_data* character = (struct online_char_data*)data;
  225. int server = va_arg(ap, int);
  226. if (server == -1) {
  227. character->char_id = -1;
  228. character->server = -1;
  229. character->waiting_disconnect = 0;
  230. } else if (character->server == server)
  231. character->server = -2; //In some map server that we aren't connected to.
  232. return 0;
  233. }
  234. void set_all_offline(void) {
  235. MYSQL_RES* sql_res2; //Needed because it is used inside inter_guild_CharOffline; [Skotlex]
  236. int char_id;
  237. sprintf(tmp_sql, "SELECT `account_id`, `char_id`, `guild_id` FROM `%s` WHERE `online`='1'",char_db);
  238. if (mysql_query(&mysql_handle, tmp_sql)) {
  239. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  240. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  241. }
  242. ShowNotice("Sending all users offline.\n");
  243. sql_res2 = mysql_store_result(&mysql_handle);
  244. if (sql_res2) {
  245. while((sql_row = mysql_fetch_row(sql_res2))) {
  246. char_id = atoi(sql_row[1]);
  247. inter_guild_CharOffline(char_id, atoi(sql_row[2]));
  248. if ( login_fd > 0 ) {
  249. ShowInfo("send user offline: %d\n",char_id);
  250. WFIFOW(login_fd,0) = 0x272c;
  251. WFIFOL(login_fd,2) = char_id;
  252. WFIFOSET(login_fd,6);
  253. }
  254. }
  255. mysql_free_result(sql_res2);
  256. }
  257. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db);
  258. if (mysql_query(&mysql_handle, tmp_sql)) {
  259. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  260. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  261. }
  262. online_char_db->foreach(online_char_db,char_db_setoffline,-1);
  263. }
  264. //----------------------------------------------------------------------
  265. // Determine if an account (id) is a GM account
  266. // and returns its level (or 0 if it isn't a GM account or if not found)
  267. //----------------------------------------------------------------------
  268. // Removed since nothing GM related goes on in the char server [CLOWNISIUS]
  269. int isGM(int account_id) {
  270. int i;
  271. for(i = 0; i < GM_num; i++)
  272. if (gm_account[i].account_id == account_id)
  273. return gm_account[i].level;
  274. return 0;
  275. }
  276. int compare_item(struct item *a, struct item *b) {
  277. if(a->id == b->id &&
  278. a->nameid == b->nameid &&
  279. a->amount == b->amount &&
  280. a->equip == b->equip &&
  281. a->identify == b->identify &&
  282. a->refine == b->refine &&
  283. a->attribute == b->attribute)
  284. {
  285. int i;
  286. for (i=0; i<MAX_SLOTS && a->card[i]==b->card[i]; i++);
  287. return (i == MAX_SLOTS);
  288. }
  289. return 0;
  290. }
  291. static void* create_charstatus(DBKey key, va_list args) {
  292. struct mmo_charstatus *cp;
  293. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  294. cp->char_id = key.i;
  295. return cp;
  296. }
  297. int mmo_char_tosql(int char_id, struct mmo_charstatus *p){
  298. int i=0,j,party_exist,guild_exist;
  299. int count = 0;
  300. int diff = 0;
  301. char *tmp_ptr; //Building a single query should be more efficient than running
  302. //multiple queries for each thing about to be saved, right? [Skotlex]
  303. char save_status[128]; //For displaying save information. [Skotlex]
  304. struct mmo_charstatus *cp;
  305. struct itemtmp mapitem[MAX_GUILD_STORAGE];
  306. if (char_id!=p->char_id) return 0;
  307. cp = idb_ensure(char_db_, char_id, create_charstatus);
  308. // ShowInfo("Saving char "CL_WHITE"%d"CL_RESET" (%s)...\n",char_id,char_dat[0].name);
  309. memset(save_status, 0, sizeof(save_status));
  310. diff = 0;
  311. //map inventory data
  312. for(i=0;i<MAX_INVENTORY;i++){
  313. if (!compare_item(&p->inventory[i], &cp->inventory[i]))
  314. diff = 1;
  315. if(p->inventory[i].nameid>0){
  316. mapitem[count].flag=0;
  317. mapitem[count].id = p->inventory[i].id;
  318. mapitem[count].nameid=p->inventory[i].nameid;
  319. mapitem[count].amount = p->inventory[i].amount;
  320. mapitem[count].equip = p->inventory[i].equip;
  321. mapitem[count].identify = p->inventory[i].identify;
  322. mapitem[count].refine = p->inventory[i].refine;
  323. mapitem[count].attribute = p->inventory[i].attribute;
  324. for (j=0; j<MAX_SLOTS; j++)
  325. mapitem[count].card[j] = p->inventory[i].card[j];
  326. count++;
  327. }
  328. }
  329. //printf("- Save item data to MySQL!\n");
  330. if (diff)
  331. if (!memitemdata_to_sql(mapitem, count, p->char_id,TABLE_INVENTORY))
  332. strcat(save_status, " inventory");
  333. count = 0;
  334. diff = 0;
  335. //map cart data
  336. for(i=0;i<MAX_CART;i++){
  337. if (!compare_item(&p->cart[i], &cp->cart[i]))
  338. diff = 1;
  339. if(p->cart[i].nameid>0){
  340. mapitem[count].flag=0;
  341. mapitem[count].id = p->cart[i].id;
  342. mapitem[count].nameid=p->cart[i].nameid;
  343. mapitem[count].amount = p->cart[i].amount;
  344. mapitem[count].equip = p->cart[i].equip;
  345. mapitem[count].identify = p->cart[i].identify;
  346. mapitem[count].refine = p->cart[i].refine;
  347. mapitem[count].attribute = p->cart[i].attribute;
  348. for (j=0; j<MAX_SLOTS; j++)
  349. mapitem[count].card[j] = p->cart[i].card[j];
  350. count++;
  351. }
  352. }
  353. if (diff)
  354. if (!memitemdata_to_sql(mapitem, count, p->char_id,TABLE_CART))
  355. strcat(save_status, " cart");
  356. if (
  357. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  358. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  359. (p->zeny != cp->zeny) ||
  360. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  361. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  362. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  363. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  364. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  365. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  366. (p->option != cp->option) ||
  367. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  368. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) ||
  369. (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  370. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom)
  371. )
  372. { //Save status
  373. //Check for party
  374. party_exist=1;
  375. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `party_id` = '%d'",party_db, p->party_id); // TBR
  376. if (mysql_query(&mysql_handle, tmp_sql)) {
  377. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  378. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  379. } else { //In case of failure, don't touch the data. [Skotlex
  380. sql_res = mysql_store_result(&mysql_handle);
  381. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  382. if (sql_row)
  383. party_exist = atoi(sql_row[0]);
  384. mysql_free_result(sql_res);
  385. }
  386. //check guild_exist
  387. guild_exist=1;
  388. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `guild_id` = '%d'",guild_db, p->guild_id); // TBR
  389. if (mysql_query(&mysql_handle, tmp_sql)) {
  390. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  391. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  392. } else { //If we fail to confirm, don't touch the data.
  393. sql_res = mysql_store_result(&mysql_handle);
  394. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  395. if (sql_row)
  396. guild_exist = atoi(sql_row[0]);
  397. mysql_free_result(sql_res);
  398. }
  399. if (guild_exist==0) p->guild_id=0;
  400. if (party_exist==0) p->party_id=0;
  401. //query
  402. sprintf(tmp_sql ,"UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  403. "`base_exp`='%d', `job_exp`='%d', `zeny`='%d',"
  404. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  405. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  406. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',"
  407. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  408. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d'"
  409. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  410. char_db, p->base_level, p->job_level,
  411. p->base_exp, p->job_exp, p->zeny,
  412. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  413. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  414. p->option, p->party_id, p->guild_id, p->pet_id,
  415. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  416. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  417. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y,
  418. p->account_id, p->char_id
  419. );
  420. if(mysql_query(&mysql_handle, tmp_sql))
  421. {
  422. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  423. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  424. } else
  425. strcat(save_status, " status");
  426. }
  427. //Values that will seldom change (to speed up saving)
  428. if (
  429. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  430. (p->class_ != cp->class_) ||
  431. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  432. (p->mother != cp->mother) || (p->child != cp->child) ||
  433. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  434. (p->fame != cp->fame)
  435. )
  436. {
  437. sprintf(tmp_sql ,"UPDATE `%s` SET `class`='%d',"
  438. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  439. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  440. "`karma`='%d',`manner`='%d', `fame`='%d'"
  441. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  442. char_db, p->class_,
  443. p->hair, p->hair_color, p->clothes_color,
  444. p->partner_id, p->father, p->mother, p->child,
  445. p->karma, p->manner, p->fame,
  446. p->account_id, p->char_id
  447. );
  448. if(mysql_query(&mysql_handle, tmp_sql))
  449. {
  450. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  451. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  452. } else
  453. strcat(save_status, " status2");
  454. }
  455. diff = 0;
  456. for(i=0;i<MAX_MEMOPOINTS;i++){
  457. if(p->memo_point[i].map == cp->memo_point[i].map && p->memo_point[i].x == cp->memo_point[i].x && p->memo_point[i].y == cp->memo_point[i].y)
  458. continue;
  459. diff = 1;
  460. break;
  461. }
  462. if (diff)
  463. { //Save memo
  464. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  465. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, p->char_id);
  466. if(mysql_query(&mysql_handle, tmp_sql))
  467. {
  468. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  469. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  470. }
  471. //insert here.
  472. tmp_ptr = tmp_sql;
  473. tmp_ptr += sprintf(tmp_ptr, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  474. count = 0;
  475. for(i=0;i<MAX_MEMOPOINTS;i++){
  476. if(p->memo_point[i].map){
  477. tmp_ptr += sprintf(tmp_ptr,"('%d', '%s', '%d', '%d'),",
  478. char_id, mapindex_id2name(p->memo_point[i].map), p->memo_point[i].x, p->memo_point[i].y);
  479. count++;
  480. }
  481. }
  482. if (count)
  483. { //Dangerous? Only if none of the above sprintf worked. [Skotlex]
  484. tmp_ptr[-1] = '\0'; //Remove the trailing comma.
  485. if(mysql_query(&mysql_handle, tmp_sql))
  486. {
  487. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  488. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  489. } else
  490. strcat(save_status, " memo");
  491. } else //Memo Points cleared (how is this possible?).
  492. strcat(save_status, " memo");
  493. }
  494. diff = 0;
  495. for(i=0;i<MAX_SKILL;i++) {
  496. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  497. p->skill[i].id = i; // Fix skill tree
  498. if((p->skill[i].id != cp->skill[i].id) || (p->skill[i].lv != cp->skill[i].lv) ||
  499. (p->skill[i].flag != cp->skill[i].flag))
  500. {
  501. diff = 1;
  502. break;
  503. }
  504. }
  505. if (diff)
  506. { //Save skills
  507. //`skill` (`char_id`, `id`, `lv`)
  508. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, p->char_id);
  509. if(mysql_query(&mysql_handle, tmp_sql))
  510. {
  511. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  512. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  513. }
  514. tmp_ptr = tmp_sql;
  515. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db);
  516. count = 0;
  517. //insert here.
  518. for(i=0;i<MAX_SKILL;i++){
  519. if(p->skill[i].id && p->skill[i].flag!=1)
  520. {
  521. tmp_ptr += sprintf(tmp_ptr,"('%d','%d','%d'),",
  522. char_id, p->skill[i].id, (p->skill[i].flag==0)?p->skill[i].lv:p->skill[i].flag-2);
  523. count++;
  524. }
  525. }
  526. if (count)
  527. {
  528. tmp_ptr[-1] = '\0'; //Remove trailing comma.
  529. if(mysql_query(&mysql_handle, tmp_sql))
  530. {
  531. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  532. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  533. } else
  534. strcat(save_status, " skills");
  535. } else //Skills removed (reset?)
  536. strcat(save_status, " skills");
  537. }
  538. /* Saving of global registry values is now handled by the inter-server. [Skotlex]
  539. diff = 0;
  540. for(i=0;i<p->global_reg_num;i++) {
  541. if ((p->global_reg[i].str == NULL) && (cp->global_reg[i].str == NULL))
  542. continue;
  543. if (((p->global_reg[i].str == NULL) != (cp->global_reg[i].str == NULL)) ||
  544. strcmp(p->global_reg[i].value, cp->global_reg[i].value) != 0 ||
  545. strcmp(p->global_reg[i].str, cp->global_reg[i].str) != 0
  546. ) {
  547. diff = 1;
  548. break;
  549. }
  550. }
  551. if (diff)
  552. { //Save global registry.
  553. //`global_reg_value` (`char_id`, `str`, `value`)
  554. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, p->char_id);
  555. if (mysql_query(&mysql_handle, tmp_sql))
  556. {
  557. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  558. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  559. }
  560. //insert here.
  561. tmp_ptr = tmp_sql;
  562. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s` (`type`, `char_id`, `str`, `value`) VALUES", reg_db);
  563. count = 0;
  564. for(i=0;i<p->global_reg_num;i++)
  565. {
  566. if (p->global_reg[i].str && p->global_reg[i].value)
  567. {
  568. tmp_ptr += sprintf(tmp_ptr,"('3','%d','%s','%s'),",
  569. char_id, jstrescapecpy(temp_str,p->global_reg[i].str), jstrescapecpy(temp_str2,p->global_reg[i].value));
  570. if (++count%100 == 0)
  571. { //Save every X registers to avoid overflowing tmp_sql [Skotlex]
  572. tmp_ptr[-1] = '\0';
  573. if(mysql_query(&mysql_handle, tmp_sql))
  574. {
  575. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  576. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  577. } else
  578. strcat(save_status, " global_reg");
  579. tmp_ptr = tmp_sql;
  580. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s` (`type`, `char_id`, `str`, `value`) VALUES", reg_db);
  581. count = 0;
  582. }
  583. }
  584. }
  585. if (count)
  586. {
  587. tmp_ptr[-1] = '\0';
  588. if(mysql_query(&mysql_handle, tmp_sql))
  589. {
  590. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  591. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  592. } else
  593. strcat(save_status, " global_reg");
  594. } else //Values cleared.
  595. strcat(save_status, " global_reg");
  596. }
  597. */
  598. diff = 0;
  599. for(i = 0; i < MAX_FRIENDS; i++){
  600. if(p->friends[i].char_id != cp->friends[i].char_id ||
  601. p->friends[i].account_id != cp->friends[i].account_id){
  602. diff = 1;
  603. break;
  604. }
  605. }
  606. if(diff == 1)
  607. { //Save friends
  608. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id);
  609. if(mysql_query(&mysql_handle, tmp_sql)){
  610. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  611. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  612. }
  613. tmp_ptr = tmp_sql;
  614. tmp_ptr += sprintf(tmp_ptr, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  615. count = 0;
  616. for(i = 0; i < MAX_FRIENDS; i++){
  617. if(p->friends[i].char_id > 0)
  618. {
  619. tmp_ptr += sprintf(tmp_ptr, "('%d','%d','%d'),", char_id, p->friends[i].account_id, p->friends[i].char_id);
  620. count++;
  621. }
  622. }
  623. if (count)
  624. {
  625. tmp_ptr[-1] = '\0'; //Remove the last comma. [Skotlex]
  626. if(mysql_query(&mysql_handle, tmp_sql))
  627. {
  628. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  629. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  630. } else
  631. strcat(save_status, " friends");
  632. } else //Friend list cleared.
  633. strcat(save_status, " friends");
  634. }
  635. if (save_status[0]!='\0' && save_log)
  636. ShowInfo("Saved char %d - %s:%s.\n", char_id, char_dat[0].name, save_status);
  637. memcpy(cp, p, sizeof(struct mmo_charstatus));
  638. return 0;
  639. }
  640. // [Ilpalazzo-sama]
  641. int memitemdata_to_sql(struct itemtmp mapitem[], int count, int char_id, int tableswitch)
  642. {
  643. int i,j, flag, id;
  644. char *tablename;
  645. char selectoption[16];
  646. char * str_p = tmp_sql;
  647. switch (tableswitch) {
  648. case TABLE_INVENTORY:
  649. tablename = inventory_db; // no need for sprintf here as *_db are char*.
  650. sprintf(selectoption,"char_id");
  651. break;
  652. case TABLE_CART:
  653. tablename = cart_db;
  654. sprintf(selectoption,"char_id");
  655. break;
  656. case TABLE_STORAGE:
  657. tablename = storage_db;
  658. sprintf(selectoption,"account_id");
  659. break;
  660. case TABLE_GUILD_STORAGE:
  661. tablename = guild_storage_db;
  662. sprintf(selectoption,"guild_id");
  663. break;
  664. default:
  665. ShowError("Invalid table name!\n");
  666. return 1;
  667. }
  668. //=======================================mysql database data > memory===============================================
  669. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  670. for (j=0; j<MAX_SLOTS; j++)
  671. str_p += sprintf(str_p, ", `card%d`", j);
  672. str_p += sprintf(str_p, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, char_id);
  673. if (mysql_query(&mysql_handle, tmp_sql)) {
  674. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  675. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  676. return 1;
  677. }
  678. sql_res = mysql_store_result(&mysql_handle);
  679. if (sql_res) {
  680. while ((sql_row = mysql_fetch_row(sql_res))) {
  681. flag = 0;
  682. id = atoi(sql_row[0]);
  683. for(i = 0; i < count; i++) {
  684. if(mapitem[i].flag == 1)
  685. continue;
  686. if(mapitem[i].nameid == atoi(sql_row[1])
  687. && mapitem[i].card[0] == atoi(sql_row[7])
  688. && mapitem[i].card[2] == atoi(sql_row[9])
  689. && mapitem[i].card[3] == atoi(sql_row[10])
  690. ) { //They are the same item.
  691. for (j = 0; j<MAX_SLOTS && mapitem[i].card[j] == atoi(sql_row[7+j]); j++);
  692. if (j == MAX_SLOTS &&
  693. mapitem[i].amount == atoi(sql_row[2]) &&
  694. mapitem[i].equip == atoi(sql_row[3]) &&
  695. mapitem[i].identify == atoi(sql_row[4]) &&
  696. mapitem[i].refine == atoi(sql_row[5]) &&
  697. mapitem[i].attribute == atoi(sql_row[6]))
  698. { //Do nothing.
  699. } else
  700. //==============================================Memory data > SQL ===============================
  701. if(!itemdb_isequip(mapitem[i].nameid))
  702. { //Quick update of stackable items. Update Qty and Equip should be enough, but in case we are also updating identify
  703. sprintf(tmp_sql,"UPDATE `%s` SET `equip`='%d', `identify`='%d', `amount`='%d' WHERE `id`='%d' LIMIT 1",
  704. tablename, mapitem[i].equip, mapitem[i].identify,mapitem[i].amount, id);
  705. if(mysql_query(&mysql_handle, tmp_sql))
  706. {
  707. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  708. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  709. }
  710. } else
  711. { //Equipment or Misc item, just update all fields.
  712. str_p = tmp_sql;
  713. str_p += sprintf(str_p,"UPDATE `%s` SET `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d'",
  714. tablename, mapitem[i].equip, mapitem[i].identify, mapitem[i].refine, mapitem[i].attribute);
  715. for(j=0; j<MAX_SLOTS; j++)
  716. str_p += sprintf(str_p, ", `card%d`=%d", j, mapitem[i].card[j]);
  717. str_p += sprintf(str_p,", `amount`='%d' WHERE `id`='%d' LIMIT 1",
  718. mapitem[i].amount, id);
  719. if(mysql_query(&mysql_handle, tmp_sql))
  720. {
  721. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  722. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  723. }
  724. }
  725. flag = mapitem[i].flag = 1; //Item dealt with,
  726. break; //skip to next item in the db.
  727. }
  728. }
  729. if(!flag) { //Item not updated, remove it.
  730. sprintf(tmp_sql,"DELETE from `%s` where `id`='%d'", tablename, id);
  731. if(mysql_query(&mysql_handle, tmp_sql))
  732. {
  733. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  734. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  735. }
  736. }
  737. }
  738. mysql_free_result(sql_res);
  739. }
  740. for(i = 0; i < count; i++) {
  741. if(!mapitem[i].flag) {
  742. str_p = tmp_sql;
  743. str_p += sprintf(str_p,"INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`",
  744. tablename, selectoption);
  745. for(j=0; j<MAX_SLOTS; j++)
  746. str_p += sprintf(str_p,", `card%d`", j);
  747. str_p += sprintf(str_p,") VALUES ( '%d','%d', '%d', '%d', '%d', '%d', '%d'",
  748. char_id, mapitem[i].nameid, mapitem[i].amount, mapitem[i].equip, mapitem[i].identify, mapitem[i].refine,
  749. mapitem[i].attribute);
  750. for(j=0; j<MAX_SLOTS; j++)
  751. str_p +=sprintf(str_p,", '%d'",mapitem[i].card[j]);
  752. strcat(tmp_sql, ")");
  753. if(mysql_query(&mysql_handle, tmp_sql))
  754. {
  755. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  756. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  757. }
  758. }
  759. }
  760. return 0;
  761. }
  762. //=====================================================================================================
  763. int mmo_char_fromsql(int char_id, struct mmo_charstatus *p){
  764. int i,j, n;
  765. char t_msg[128];
  766. char *str_p = tmp_sql;
  767. struct mmo_charstatus *cp;
  768. memset(p, 0, sizeof(struct mmo_charstatus));
  769. t_msg[0]= '\0';
  770. p->char_id = char_id;
  771. if (save_log)
  772. ShowInfo("Char load request (%d)\n", char_id);
  773. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  774. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  775. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  776. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  777. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  778. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  779. //splite 2 parts. cause veeeery long SQL syntax
  780. sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  781. "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  782. if (mysql_query(&mysql_handle, tmp_sql)) {
  783. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  784. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  785. }
  786. sql_res = mysql_store_result(&mysql_handle);
  787. if (sql_res) {
  788. sql_row = mysql_fetch_row(sql_res);
  789. if (!sql_row)
  790. { //Just how does this happens? [Skotlex]
  791. ShowError("Requested non-existant character id: %d!\n", char_id);
  792. return 0;
  793. }
  794. p->char_id = char_id;
  795. p->account_id = atoi(sql_row[1]);
  796. p->char_num = atoi(sql_row[2]);
  797. strcpy(p->name, sql_row[3]);
  798. p->class_ = atoi(sql_row[4]);
  799. p->base_level = atoi(sql_row[5]);
  800. p->job_level = atoi(sql_row[6]);
  801. p->base_exp = atoi(sql_row[7]);
  802. p->job_exp = atoi(sql_row[8]);
  803. p->zeny = atoi(sql_row[9]);
  804. p->str = atoi(sql_row[10]);
  805. p->agi = atoi(sql_row[11]);
  806. p->vit = atoi(sql_row[12]);
  807. p->int_ = atoi(sql_row[13]);
  808. p->dex = atoi(sql_row[14]);
  809. p->luk = atoi(sql_row[15]);
  810. p->max_hp = atoi(sql_row[16]);
  811. p->hp = atoi(sql_row[17]);
  812. p->max_sp = atoi(sql_row[18]);
  813. p->sp = atoi(sql_row[19]);
  814. p->status_point = atoi(sql_row[20]);
  815. p->skill_point = atoi(sql_row[21]);
  816. //free mysql result.
  817. mysql_free_result(sql_res);
  818. strcat (t_msg, " status");
  819. } else
  820. ShowError("Load char failed (%d - table %s).\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  821. sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`hair`,`hair_color`,"
  822. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,"
  823. "`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`, `partner_id`, `father`, `mother`, `child`, `fame`"
  824. "FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  825. if (mysql_query(&mysql_handle, tmp_sql)) {
  826. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  827. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  828. }
  829. sql_res = mysql_store_result(&mysql_handle);
  830. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  831. if (sql_row) {
  832. p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]);
  833. p->party_id = atoi(sql_row[3]); p->guild_id = atoi(sql_row[4]); p->pet_id = atoi(sql_row[5]);
  834. p->hair = atoi(sql_row[6]); p->hair_color = atoi(sql_row[7]); p->clothes_color = atoi(sql_row[8]);
  835. p->weapon = atoi(sql_row[9]); p->shield = atoi(sql_row[10]);
  836. p->head_top = atoi(sql_row[11]); p->head_mid = atoi(sql_row[12]); p->head_bottom = atoi(sql_row[13]);
  837. p->last_point.map = mapindex_name2id(sql_row[14]); p->last_point.x = atoi(sql_row[15]); p->last_point.y = atoi(sql_row[16]);
  838. p->save_point.map = mapindex_name2id(sql_row[17]); p->save_point.x = atoi(sql_row[18]); p->save_point.y = atoi(sql_row[19]);
  839. p->partner_id = atoi(sql_row[20]); p->father = atoi(sql_row[21]); p->mother = atoi(sql_row[22]); p->child = atoi(sql_row[23]);
  840. p->fame = atoi(sql_row[24]);
  841. //free mysql result.
  842. mysql_free_result(sql_res);
  843. strcat (t_msg, " status2");
  844. } else
  845. ShowError("Char load failed (%d - table %s)\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  846. /* We shouldn't need this at all! [Skotlex]
  847. if (p->last_point.x == 0 || p->last_point.y == 0 || p->last_point.map == 0)
  848. memcpy(&p->last_point, &start_point, sizeof(start_point));
  849. if (p->save_point.x == 0 || p->save_point.y == 0 || p->save_point.map == 0)
  850. memcpy(&p->save_point, &start_point, sizeof(start_point));
  851. */
  852. //read memo data
  853. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  854. sprintf(tmp_sql, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`='%d' ORDER by `memo_id`",memo_db, char_id); // TBR
  855. if (mysql_query(&mysql_handle, tmp_sql)) {
  856. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  857. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  858. }
  859. sql_res = mysql_store_result(&mysql_handle);
  860. if (sql_res) {
  861. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  862. p->memo_point[i].map = mapindex_name2id(sql_row[0]);
  863. p->memo_point[i].x=atoi(sql_row[1]);
  864. p->memo_point[i].y=atoi(sql_row[2]);
  865. //i ++;
  866. }
  867. mysql_free_result(sql_res);
  868. strcat (t_msg, " memo");
  869. }
  870. //read inventory
  871. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  872. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  873. for (j=0; j<MAX_SLOTS; j++)
  874. str_p += sprintf(str_p, ", `card%d`", j);
  875. str_p += sprintf(str_p, " FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id);
  876. if (mysql_query(&mysql_handle, tmp_sql)) {
  877. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  878. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  879. }
  880. sql_res = mysql_store_result(&mysql_handle);
  881. if (sql_res) {
  882. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  883. p->inventory[i].id = atoi(sql_row[0]);
  884. p->inventory[i].nameid = atoi(sql_row[1]);
  885. p->inventory[i].amount = atoi(sql_row[2]);
  886. p->inventory[i].equip = atoi(sql_row[3]);
  887. p->inventory[i].identify = atoi(sql_row[4]);
  888. p->inventory[i].refine = atoi(sql_row[5]);
  889. p->inventory[i].attribute = atoi(sql_row[6]);
  890. for (j=0; j<MAX_SLOTS; j++)
  891. p->inventory[i].card[j] = atoi(sql_row[7+j]);
  892. }
  893. mysql_free_result(sql_res);
  894. strcat (t_msg, " inventory");
  895. }
  896. //read cart.
  897. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  898. str_p = tmp_sql;
  899. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  900. for (j=0; j<MAX_SLOTS; j++)
  901. str_p += sprintf(str_p, ", `card%d`", j);
  902. str_p += sprintf(str_p, " FROM `%s` WHERE `char_id`='%d'", cart_db, char_id);
  903. if (mysql_query(&mysql_handle, tmp_sql)) {
  904. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  905. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  906. }
  907. sql_res = mysql_store_result(&mysql_handle);
  908. if (sql_res) {
  909. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  910. p->cart[i].id = atoi(sql_row[0]);
  911. p->cart[i].nameid = atoi(sql_row[1]);
  912. p->cart[i].amount = atoi(sql_row[2]);
  913. p->cart[i].equip = atoi(sql_row[3]);
  914. p->cart[i].identify = atoi(sql_row[4]);
  915. p->cart[i].refine = atoi(sql_row[5]);
  916. p->cart[i].attribute = atoi(sql_row[6]);
  917. for(j=0; j<MAX_SLOTS; j++)
  918. p->cart[i].card[j] = atoi(sql_row[7+j]);
  919. }
  920. mysql_free_result(sql_res);
  921. strcat (t_msg, " cart");
  922. }
  923. //read skill
  924. //`skill` (`char_id`, `id`, `lv`)
  925. sprintf(tmp_sql, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`='%d'",skill_db, char_id); // TBR
  926. if (mysql_query(&mysql_handle, tmp_sql)) {
  927. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  928. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  929. }
  930. sql_res = mysql_store_result(&mysql_handle);
  931. if (sql_res) {
  932. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  933. n = atoi(sql_row[0]);
  934. p->skill[n].id = n; //memory!? shit!.
  935. p->skill[n].lv = atoi(sql_row[1]);
  936. }
  937. mysql_free_result(sql_res);
  938. strcat (t_msg, " skills");
  939. }
  940. /* Global-reg loading is now handled by the inter-server.
  941. //global_reg
  942. //`global_reg_value` (`char_id`, `str`, `value`)
  943. sprintf(tmp_sql, "SELECT `str`, `value` FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, char_id); // TBR
  944. if (mysql_query(&mysql_handle, tmp_sql)) {
  945. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  946. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  947. }
  948. i = 0;
  949. sql_res = mysql_store_result(&mysql_handle);
  950. if (sql_res) {
  951. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  952. strcpy (p->global_reg[i].str, sql_row[0]);
  953. strcpy (p->global_reg[i].value, sql_row[1]);
  954. }
  955. mysql_free_result(sql_res);
  956. strcat (t_msg, " reg_values");
  957. }
  958. p->global_reg_num=i;
  959. */
  960. //Shamelessly stolen from its_sparky (ie: thanks) and then assimilated by [Skotlex]
  961. //Friend list
  962. sprintf(tmp_sql, "SELECT f.friend_account, f.friend_id, c.name FROM `%s` f LEFT JOIN `%s` c ON f.friend_account=c.account_id AND f.friend_id=c.char_id WHERE f.char_id='%d'", friend_db, char_db, char_id);
  963. if(mysql_query(&mysql_handle, tmp_sql)){
  964. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  965. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  966. }
  967. sql_res = mysql_store_result(&mysql_handle);
  968. if(sql_res)
  969. {
  970. for(i = 0; (sql_row = mysql_fetch_row(sql_res)) && i<MAX_FRIENDS; i++)
  971. {
  972. if(sql_row) { //need to check if we have sql_row before we check if we have sql_row[2] because we don't want a segfault
  973. if(sql_row[2]) {
  974. p->friends[i].account_id = atoi(sql_row[0]);
  975. p->friends[i].char_id = atoi(sql_row[1]);
  976. strncpy(p->friends[i].name, sql_row[2], NAME_LENGTH-1); //The -1 is to avoid losing the ending \0 [Skotlex]
  977. }
  978. }
  979. }
  980. mysql_free_result(sql_res);
  981. strcat (t_msg, " friends");
  982. }
  983. if (save_log)
  984. ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  985. cp = idb_ensure(char_db_, char_id, create_charstatus);
  986. memcpy(cp, p, sizeof(struct mmo_charstatus));
  987. return 1;
  988. }
  989. // For quick selection of data when displaying the char menu. [Skotlex]
  990. //
  991. int mmo_char_fromsql_short(int char_id, struct mmo_charstatus *p){
  992. char t_msg[128];
  993. memset(p, 0, sizeof(struct mmo_charstatus));
  994. t_msg[0]= '\0';
  995. p->char_id = char_id;
  996. // ShowInfo("Quick Char load request (%d)\n", char_id);
  997. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  998. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  999. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  1000. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  1001. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  1002. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  1003. //splite 2 parts. cause veeeery long SQL syntax
  1004. sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  1005. "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  1006. if (mysql_query(&mysql_handle, tmp_sql)) {
  1007. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1008. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1009. }
  1010. sql_res = mysql_store_result(&mysql_handle);
  1011. if (sql_res) {
  1012. sql_row = mysql_fetch_row(sql_res);
  1013. if (!sql_row)
  1014. { //Just how does this happens? [Skotlex]
  1015. ShowError("Requested non-existant character id: %d!\n", char_id);
  1016. return 0;
  1017. }
  1018. p->char_id = char_id;
  1019. p->account_id = atoi(sql_row[1]);
  1020. p->char_num = atoi(sql_row[2]);
  1021. strcpy(p->name, sql_row[3]);
  1022. p->class_ = atoi(sql_row[4]);
  1023. p->base_level = atoi(sql_row[5]);
  1024. p->job_level = atoi(sql_row[6]);
  1025. p->base_exp = atoi(sql_row[7]);
  1026. p->job_exp = atoi(sql_row[8]);
  1027. p->zeny = atoi(sql_row[9]);
  1028. p->str = atoi(sql_row[10]);
  1029. p->agi = atoi(sql_row[11]);
  1030. p->vit = atoi(sql_row[12]);
  1031. p->int_ = atoi(sql_row[13]);
  1032. p->dex = atoi(sql_row[14]);
  1033. p->luk = atoi(sql_row[15]);
  1034. p->max_hp = atoi(sql_row[16]);
  1035. p->hp = atoi(sql_row[17]);
  1036. p->max_sp = atoi(sql_row[18]);
  1037. p->sp = atoi(sql_row[19]);
  1038. p->status_point = atoi(sql_row[20]);
  1039. p->skill_point = atoi(sql_row[21]);
  1040. //free mysql result.
  1041. mysql_free_result(sql_res);
  1042. strcat (t_msg, " status");
  1043. } else
  1044. ShowError("Load char failed (%d - table %s).\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  1045. sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`hair`,`hair_color`,"
  1046. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`"
  1047. "FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  1048. if (mysql_query(&mysql_handle, tmp_sql)) {
  1049. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1050. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1051. }
  1052. sql_res = mysql_store_result(&mysql_handle);
  1053. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  1054. if (sql_row) {
  1055. p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]);
  1056. p->hair = atoi(sql_row[3]); p->hair_color = atoi(sql_row[4]); p->clothes_color = atoi(sql_row[5]);
  1057. p->weapon = atoi(sql_row[6]); p->shield = atoi(sql_row[7]);
  1058. p->head_top = atoi(sql_row[8]); p->head_mid = atoi(sql_row[9]); p->head_bottom = atoi(sql_row[10]);
  1059. //free mysql result.
  1060. mysql_free_result(sql_res);
  1061. strcat (t_msg, " status2");
  1062. } else
  1063. ShowError("Char load failed (%d - table %s)\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  1064. if (save_log)
  1065. ShowInfo("Quick Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  1066. return 1;
  1067. }
  1068. //==========================================================================================================
  1069. int mmo_char_sql_init(void) {
  1070. int charcount;
  1071. ShowInfo("Begin Initializing.......\n");
  1072. char_db_= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA, sizeof(int));
  1073. // memory initialize
  1074. // no need to set twice size in this routine. but some cause segmentation error. :P
  1075. // The hell? Why segmentation faults? Sounds like a bug that needs addressing... [Skotlex]
  1076. ShowDebug("initializing char memory...(%d byte)\n",sizeof(struct mmo_charstatus)*2);
  1077. CREATE(char_dat, struct mmo_charstatus, 2);
  1078. memset(char_dat, 0, sizeof(struct mmo_charstatus)*2);
  1079. //Check for max id (in case new chars would get their IDs set below 150K) [Skotlex]
  1080. sprintf(tmp_sql , "SELECT max(`char_id`) FROM `%s`", char_db);
  1081. if (mysql_query(&mysql_handle, tmp_sql)) {
  1082. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1083. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1084. } else {
  1085. sql_res = mysql_store_result(&mysql_handle);
  1086. if (sql_res)
  1087. {
  1088. if (mysql_num_rows(sql_res) > 0 &&
  1089. (sql_row = mysql_fetch_row(sql_res)) != NULL &&
  1090. sql_row[0] != NULL && atoi(sql_row[0]) >= char_id_count)
  1091. char_id_count = 0; //No need for setting the char id.
  1092. mysql_free_result(sql_res);
  1093. }
  1094. }
  1095. sprintf(tmp_sql, "SELECT `char_id` FROM `%s`", char_db);
  1096. if(mysql_query(&mysql_handle, tmp_sql)){
  1097. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1098. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1099. }else{
  1100. sql_res = mysql_store_result(&mysql_handle);
  1101. if(sql_res){
  1102. charcount = (int)mysql_num_rows(sql_res);
  1103. ShowStatus("total char data -> '%d'.......\n", charcount);
  1104. mysql_free_result(sql_res);
  1105. }else{
  1106. ShowStatus("total char data -> '0'.......\n");
  1107. }
  1108. }
  1109. if(char_per_account == 0){
  1110. ShowStatus("Chars per Account: 'Unlimited'.......\n");
  1111. }else{
  1112. ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
  1113. }
  1114. //the 'set offline' part is now in check_login_conn ...
  1115. //if the server connects to loginserver
  1116. //it will dc all off players
  1117. //and send the loginserver the new state....
  1118. ShowInfo("Finished initilizing.......\n");
  1119. return 0;
  1120. }
  1121. //==========================================================================================================
  1122. int make_new_char_sql(int fd, unsigned char *dat) {
  1123. struct char_session_data *sd;
  1124. char name[NAME_LENGTH];
  1125. char t_name[NAME_LENGTH*2];
  1126. unsigned int i; // Used in for loop and comparing with strlen, safe to be unsigned. [Lance]
  1127. int char_id, temp;
  1128. strncpy(name, dat, NAME_LENGTH);
  1129. name[NAME_LENGTH-1] = '\0'; //Always terminate string.
  1130. trim(name,TRIM_CHARS); //Trim character name. [Skotlex]
  1131. jstrescapecpy(t_name, name);
  1132. // disabled until fixed >.>
  1133. // Note: escape characters should be added to jstrescape()!
  1134. //mysql_real_escape_string(&mysql_handle, t_name, t_name_temp, sizeof(t_name_temp));
  1135. if (!session_isValid(fd) || !(sd = (struct char_session_data*)session[fd]->session_data))
  1136. return -2;
  1137. ShowInfo("New character request (%d)\n", sd->account_id);
  1138. //check name != main chat nick [LuzZza]
  1139. if(strcmpi(name, main_chat_nick) == 0) {
  1140. ShowInfo("Create char failed (%d): this nick (%s) reserved for mainchat messages.\n", sd->account_id, name);
  1141. return -2;
  1142. }
  1143. //check for charcount (maxchars) :)
  1144. if(char_per_account != 0){
  1145. sprintf(tmp_sql, "SELECT `account_id` FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id);
  1146. if(mysql_query(&mysql_handle, tmp_sql)){
  1147. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1148. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1149. }
  1150. sql_res = mysql_store_result(&mysql_handle);
  1151. if(sql_res){
  1152. //ok
  1153. temp = (int)mysql_num_rows(sql_res);
  1154. if(temp >= char_per_account){
  1155. //hehe .. limit exceeded :P
  1156. ShowInfo("Create char failed (%d): charlimit exceeded.\n", sd->account_id);
  1157. mysql_free_result(sql_res);
  1158. return -2;
  1159. }
  1160. mysql_free_result(sql_res);
  1161. }
  1162. }
  1163. // Check Authorised letters/symbols in the name of the character
  1164. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  1165. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1166. if (strchr(char_name_letters, name[i]) == NULL)
  1167. return -2;
  1168. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  1169. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1170. if (strchr(char_name_letters, name[i]) != NULL)
  1171. return -2;
  1172. } // else, all letters/symbols are authorised (except control char removed before)
  1173. //check stat error
  1174. if ((dat[24]+dat[25]+dat[26]+dat[27]+dat[28]+dat[29]!=6*5 ) || // stats
  1175. (dat[30] >= 9) || // slots (dat[30] can not be negativ)
  1176. (dat[33] <= 0) || (dat[33] >= 24) || // hair style
  1177. (dat[31] >= 9)) { // hair color (dat[31] can not be negativ)
  1178. if (log_char) {
  1179. // char.log to charlog
  1180. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1181. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1182. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1183. //query
  1184. if(mysql_query(&mysql_handle, tmp_sql)){
  1185. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1186. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1187. }
  1188. }
  1189. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1190. return -2;
  1191. } // for now we have checked: stat points used <31, char slot is less then 9, hair style/color values are acceptable
  1192. // check individual stat value
  1193. for(i = 24; i <= 29; i++) {
  1194. if (dat[i] < 1 || dat[i] > 9) {
  1195. if (log_char) {
  1196. // char.log to charlog
  1197. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1198. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1199. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1200. //query
  1201. if(mysql_query(&mysql_handle, tmp_sql)){
  1202. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1203. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1204. }
  1205. }
  1206. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1207. return -2;
  1208. }
  1209. } // now we know that every stat has proper value but we have to check if str/int agi/luk vit/dex pairs are correct
  1210. if( ((dat[24]+dat[27]) > 10) || ((dat[25]+dat[29]) > 10) || ((dat[26]+dat[28]) > 10) ) {
  1211. if (log_char) {
  1212. // char.log to charlog
  1213. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1214. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1215. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1216. //query
  1217. if(mysql_query(&mysql_handle, tmp_sql)){
  1218. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1219. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1220. }
  1221. }
  1222. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1223. return -2;
  1224. } // now when we have passed all stat checks
  1225. if (log_char) {
  1226. // char.log to charlog
  1227. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1228. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1229. charlog_db,"make new char", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1230. //query
  1231. if (mysql_query(&mysql_handle, tmp_sql)) {
  1232. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1233. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1234. }
  1235. }
  1236. //printf("make new char %d-%d %s %d, %d, %d, %d, %d, %d - %d, %d" RETCODE,
  1237. // fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1238. //Check Name (already in use?)
  1239. sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `name` = '%s'",char_db, t_name);
  1240. if (mysql_query(&mysql_handle, tmp_sql)) {
  1241. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1242. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1243. return -2;
  1244. }
  1245. sql_res = mysql_store_result(&mysql_handle);
  1246. if(sql_res){
  1247. temp = (int)mysql_num_rows(sql_res);
  1248. mysql_free_result(sql_res);
  1249. if (temp > 0) {
  1250. ShowInfo("Create char failed: charname already in use\n");
  1251. return -1;
  1252. }
  1253. }
  1254. // check char slot.
  1255. sprintf(tmp_sql, "SELECT `account_id`, `char_num` FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'",char_db, sd->account_id, dat[30]);
  1256. if (mysql_query(&mysql_handle, tmp_sql)) {
  1257. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1258. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1259. }
  1260. sql_res = mysql_store_result(&mysql_handle);
  1261. if(sql_res){
  1262. temp = (int)mysql_num_rows(sql_res);
  1263. mysql_free_result(sql_res);
  1264. if (temp > 0) {
  1265. ShowWarning("Create char failed (%d, slot: %d), slot already in use\n", sd->account_id, dat[30]);
  1266. return -2;
  1267. }
  1268. }
  1269. //New Querys [Sirius]
  1270. //Insert the char to the 'chardb' ^^
  1271. if (char_id_count) //Force initial char id. [Skotlex]
  1272. sprintf(tmp_sql, "INSERT INTO `%s` (`char_id`,`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1273. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1274. "'%d', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1275. char_db, char_id_count, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
  1276. (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31],
  1277. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y);
  1278. else
  1279. sprintf(tmp_sql, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1280. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1281. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1282. char_db, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
  1283. (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31],
  1284. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y);
  1285. if(mysql_query(&mysql_handle, tmp_sql)){
  1286. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1287. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1288. return -2; //No, stop the procedure!
  1289. }
  1290. if (char_id_count) //Clear this out for future inserts.
  1291. char_id_count = 0;
  1292. //Now we need the charid from sql!
  1293. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id , dat[30] , t_name);
  1294. if(mysql_query(&mysql_handle, tmp_sql)){
  1295. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1296. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1297. //delete the char ..(no trash in DB!)
  1298. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1299. if(mysql_query(&mysql_handle, tmp_sql)){
  1300. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1301. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1302. }
  1303. return -2; //XD end of the (World? :P) .. charcreate (denied)
  1304. } else {
  1305. //query ok -> get the data!
  1306. sql_res = mysql_store_result(&mysql_handle);
  1307. if(sql_res){
  1308. sql_row = mysql_fetch_row(sql_res);
  1309. char_id = sql_row?atoi(sql_row[0]):0; //char id :)
  1310. mysql_free_result(sql_res);
  1311. if(char_id <= 0){
  1312. ShowError("failed (get char id..) CHARID (%d) wrong!\n", char_id);
  1313. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1314. if(mysql_query(&mysql_handle, tmp_sql)){
  1315. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1316. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1317. }
  1318. return -2; //charcreate denied ..
  1319. }
  1320. }else{
  1321. //prevent to crash (if its false, and we want to free -> segfault :)
  1322. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1323. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1324. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1325. if(mysql_query(&mysql_handle, tmp_sql)){
  1326. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1327. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1328. }
  1329. return -2; //end ...... -> charcreate failed :)
  1330. }
  1331. }
  1332. //Give the char the default items
  1333. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  1334. if (start_weapon > 0) { //add Start Weapon (Knife?)
  1335. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon,1,0x02,1);
  1336. if (mysql_query(&mysql_handle, tmp_sql)){
  1337. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1338. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1339. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1340. if(mysql_query(&mysql_handle, tmp_sql)){
  1341. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1342. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1343. }
  1344. return -2;//end XD
  1345. }
  1346. }
  1347. if (start_armor > 0) { //Add default armor (cotton shirt?)
  1348. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor,1,0x10,1);
  1349. if (mysql_query(&mysql_handle, tmp_sql)){
  1350. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1351. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1352. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1353. if(mysql_query(&mysql_handle, tmp_sql)){
  1354. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1355. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1356. }
  1357. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d'", inventory_db, char_id);
  1358. if(mysql_query(&mysql_handle, tmp_sql)){
  1359. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1360. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1361. }
  1362. return -2; //end....
  1363. }
  1364. }
  1365. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, dat[30], name);
  1366. return char_id;
  1367. }
  1368. /*----------------------------------------------------------------------------------------------------------*/
  1369. /* Delete char - davidsiaw */
  1370. /*----------------------------------------------------------------------------------------------------------*/
  1371. /* Returns 0 if successful
  1372. * Returns < 0 for error
  1373. */
  1374. int delete_char_sql(int char_id, int partner_id)
  1375. {
  1376. char char_name[NAME_LENGTH], t_name[NAME_LENGTH*2]; //Name needs be escaped.
  1377. int account_id=0, party_id=0, guild_id=0;
  1378. sprintf(tmp_sql, "SELECT `name`,`account_id`,`party_id`,`guild_id` FROM `%s` WHERE `char_id`='%d'",char_db, char_id);
  1379. if (mysql_query(&mysql_handle, tmp_sql)) {
  1380. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1381. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1382. }
  1383. sql_res = mysql_store_result(&mysql_handle);
  1384. if(sql_res)
  1385. sql_row = mysql_fetch_row(sql_res);
  1386. if (sql_res == NULL || sql_row == NULL)
  1387. {
  1388. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1389. return -1;
  1390. }
  1391. strncpy(char_name, sql_row[0], NAME_LENGTH);
  1392. char_name[NAME_LENGTH-1] = '\0';
  1393. jstrescapecpy(t_name, char_name); //Escape string for sql use... [Skotlex]
  1394. account_id = atoi(sql_row[1]);
  1395. party_id = atoi(sql_row[2]);
  1396. guild_id = atoi(sql_row[3]);
  1397. mysql_free_result(sql_res); //Let's free this as soon as possible to avoid problems later on.
  1398. /* Divorce [Wizputer] */
  1399. if (partner_id) {
  1400. sprintf(tmp_sql,"UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d'",char_db,partner_id);
  1401. if(mysql_query(&mysql_handle, tmp_sql)) {
  1402. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1403. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1404. }
  1405. sprintf(tmp_sql,"DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND `char_id`='%d'",inventory_db,WEDDING_RING_M,WEDDING_RING_F,partner_id);
  1406. if(mysql_query(&mysql_handle, tmp_sql)) {
  1407. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1408. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1409. }
  1410. }
  1411. //Make the character leave the party [Skotlex]
  1412. if (party_id)
  1413. inter_party_leave(party_id, account_id, char_id);
  1414. /* delete char's pet */
  1415. //Delete the hatched pet if you have one...
  1416. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'",pet_db, char_id);
  1417. if(mysql_query(&mysql_handle, tmp_sql)) {
  1418. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1419. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1420. }
  1421. // Komurka's suggested way to clear pets, modified by [Skotlex] (because I always personalize what I do :X)
  1422. //Removing pets that are in the char's inventory....
  1423. { //NOTE: The syntax for multi-table deletes is a bit changed between 4.0 and 4.1 regarding aliases, so we have to consider the version... [Skotlex]
  1424. //Since we only care about the major and minor version, a double conversion is good enough. (4.1.20 -> 4.10000)
  1425. double mysql_version = atof(mysql_get_server_info(&mysql_handle));
  1426. sprintf(tmp_sql,
  1427. "delete FROM `%s` USING `%s` as c LEFT JOIN `%s` as i ON c.char_id = i.char_id, `%s` as p WHERE c.char_id = '%d' AND i.card0 = -256 AND p.pet_id = (i.card1|(i.card2<<2))",
  1428. (mysql_version<4.1?pet_db:"p"), char_db, inventory_db, pet_db, char_id);
  1429. if(mysql_query(&mysql_handle, tmp_sql)) {
  1430. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1431. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1432. }
  1433. //Removing pets that are in the char's cart....
  1434. sprintf(tmp_sql,
  1435. "delete FROM `%s` USING `%s` as c LEFT JOIN `%s` as i ON c.char_id = i.char_id, `%s` as p WHERE c.char_id = '%d' AND i.card0 = -256 AND p.pet_id = (i.card1|(i.card2<<2))",
  1436. (mysql_version<4.1?pet_db:"p"), char_db, cart_db, pet_db, char_id);
  1437. if(mysql_query(&mysql_handle, tmp_sql)) {
  1438. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1439. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1440. }
  1441. }
  1442. /* delete char's friends list */
  1443. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d'",friend_db, char_id);
  1444. if(mysql_query(&mysql_handle, tmp_sql)) {
  1445. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1446. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1447. }
  1448. /* delete char from other's friend list */
  1449. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1450. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `friend_id` = '%d'",friend_db, char_id);
  1451. if(mysql_query(&mysql_handle, tmp_sql)) {
  1452. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1453. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1454. }
  1455. /* delete inventory */
  1456. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",inventory_db, char_id);
  1457. if(mysql_query(&mysql_handle, tmp_sql)) {
  1458. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1459. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1460. }
  1461. /* delete cart inventory */
  1462. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",cart_db, char_id);
  1463. if(mysql_query(&mysql_handle, tmp_sql)) {
  1464. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1465. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1466. }
  1467. /* delete memo areas */
  1468. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, char_id);
  1469. if(mysql_query(&mysql_handle, tmp_sql)) {
  1470. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1471. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1472. }
  1473. /* delete skills */
  1474. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, char_id);
  1475. if(mysql_query(&mysql_handle, tmp_sql)) {
  1476. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1477. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1478. }
  1479. /* delete character */
  1480. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",char_db, char_id);
  1481. if(mysql_query(&mysql_handle, tmp_sql)) {
  1482. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1483. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1484. }
  1485. /* No need as we used inter_guild_leave [Skotlex]
  1486. // Also delete info from guildtables.
  1487. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",guild_member_db, char_id);
  1488. if (mysql_query(&mysql_handle, tmp_sql)) {
  1489. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1490. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1491. }
  1492. */
  1493. sprintf(tmp_sql, "SELECT `guild_id` FROM `%s` WHERE `master` = '%s'", guild_db, t_name);
  1494. if (mysql_query(&mysql_handle, tmp_sql))
  1495. {
  1496. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1497. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1498. } else {
  1499. sql_res = mysql_store_result(&mysql_handle);
  1500. if (sql_res == NULL) {
  1501. if (mysql_errno(&mysql_handle) != 0)
  1502. {
  1503. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1504. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1505. }
  1506. return -1;
  1507. } else {
  1508. int rows = (int)mysql_num_rows(sql_res);
  1509. mysql_free_result(sql_res);
  1510. if (rows > 0) {
  1511. mapif_parse_BreakGuild(0,guild_id);
  1512. }
  1513. else if (guild_id) //Leave your guild.
  1514. inter_guild_leave(guild_id, account_id, char_id);
  1515. }
  1516. }
  1517. return 0;
  1518. }
  1519. //==========================================================================================================
  1520. void mmo_char_sync(void)
  1521. {
  1522. ShowWarning("mmo_char_sync() - nothing to do\n");
  1523. }
  1524. // to do
  1525. ///////////////////////////
  1526. int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) {
  1527. ShowWarning("mmo_char_sync_timer() tic - no works to do\n");
  1528. return 0;
  1529. }
  1530. int count_users(void) {
  1531. int i, users;
  1532. if (login_fd > 0 && session[login_fd]){
  1533. users = 0;
  1534. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  1535. if (server_fd[i] > 0) {
  1536. users += server[i].users;
  1537. }
  1538. }
  1539. return users;
  1540. }
  1541. return 0;
  1542. }
  1543. int mmo_char_send006b(int fd, struct char_session_data *sd) {
  1544. int i, j, found_num = 0;
  1545. struct mmo_charstatus *p = NULL;
  1546. // hehe. commented other. anyway there's no need to use older version.
  1547. // if use older packet version just uncomment that!
  1548. //#ifdef NEW_006b
  1549. const int offset = 24;
  1550. //#else
  1551. // int offset = 4;
  1552. //#endif
  1553. // ShowDebug("mmo_char_send006b start.. (account:%d)\n",sd->account_id);
  1554. // printf("offset -> %d...\n",offset);
  1555. set_char_online(-1, 99,sd->account_id);
  1556. //search char.
  1557. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d'",char_db, sd->account_id);
  1558. if (mysql_query(&mysql_handle, tmp_sql)) {
  1559. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1560. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1561. }
  1562. sql_res = mysql_store_result(&mysql_handle);
  1563. if (sql_res) {
  1564. found_num = (int)mysql_num_rows(sql_res);
  1565. // ShowInfo("number of chars: %d\n", found_num);
  1566. i = 0;
  1567. while((sql_row = mysql_fetch_row(sql_res))) {
  1568. sd->found_char[i] = atoi(sql_row[0]);
  1569. i++;
  1570. }
  1571. mysql_free_result(sql_res);
  1572. }
  1573. // printf("char fetching end (total: %d)....\n", found_num);
  1574. for(i = found_num; i < 9; i++)
  1575. sd->found_char[i] = -1;
  1576. memset(WFIFOP(fd, 0), 0, offset + found_num * 106);
  1577. WFIFOW(fd, 0) = 0x6b;
  1578. WFIFOW(fd, 2) = offset + found_num * 106;
  1579. if (save_log)
  1580. ShowInfo("Request Char Data ("CL_BOLD"%d"CL_RESET"):\n",sd->account_id);
  1581. for(i = 0; i < found_num; i++) {
  1582. mmo_char_fromsql_short(sd->found_char[i], char_dat);
  1583. p = &char_dat[0];
  1584. j = offset + (i * 106); // increase speed of code
  1585. WFIFOL(fd,j) = p->char_id;
  1586. WFIFOL(fd,j+4) = p->base_exp;
  1587. WFIFOL(fd,j+8) = p->zeny;
  1588. WFIFOL(fd,j+12) = p->job_exp;
  1589. WFIFOL(fd,j+16) = p->job_level;
  1590. WFIFOL(fd,j+20) = 0;
  1591. WFIFOL(fd,j+24) = 0;
  1592. WFIFOL(fd,j+28) = p->option;
  1593. WFIFOL(fd,j+32) = p->karma;
  1594. WFIFOL(fd,j+36) = p->manner;
  1595. WFIFOW(fd,j+40) = p->status_point;
  1596. WFIFOW(fd,j+42) = (p->hp > 0x7fff) ? 0x7fff : p->hp;
  1597. WFIFOW(fd,j+44) = (p->max_hp > 0x7fff) ? 0x7fff : p->max_hp;
  1598. WFIFOW(fd,j+46) = (p->sp > 0x7fff) ? 0x7fff : p->sp;
  1599. WFIFOW(fd,j+48) = (p->max_sp > 0x7fff) ? 0x7fff : p->max_sp;
  1600. WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed;
  1601. WFIFOW(fd,j+52) = p->class_;
  1602. WFIFOW(fd,j+54) = p->hair;
  1603. // pecopeco knights/crusaders crash fix
  1604. if (p->class_ == 13 || p->class_ == 21 ||
  1605. p->class_ == 4014 || p->class_ == 4022 ||
  1606. p->class_ == 4036 || p->class_ == 4044)
  1607. WFIFOW(fd,j+56) = 0;
  1608. else WFIFOW(fd,j+56) = p->weapon;
  1609. WFIFOW(fd,j+58) = p->base_level;
  1610. WFIFOW(fd,j+60) = p->skill_point;
  1611. WFIFOW(fd,j+62) = p->head_bottom;
  1612. WFIFOW(fd,j+64) = p->shield;
  1613. WFIFOW(fd,j+66) = p->head_top;
  1614. WFIFOW(fd,j+68) = p->head_mid;
  1615. WFIFOW(fd,j+70) = p->hair_color;
  1616. WFIFOW(fd,j+72) = p->clothes_color;
  1617. memcpy(WFIFOP(fd,j+74), p->name, NAME_LENGTH);
  1618. WFIFOB(fd,j+98) = (p->str > 255) ? 255 : p->str;
  1619. WFIFOB(fd,j+99) = (p->agi > 255) ? 255 : p->agi;
  1620. WFIFOB(fd,j+100) = (p->vit > 255) ? 255 : p->vit;
  1621. WFIFOB(fd,j+101) = (p->int_ > 255) ? 255 : p->int_;
  1622. WFIFOB(fd,j+102) = (p->dex > 255) ? 255 : p->dex;
  1623. WFIFOB(fd,j+103) = (p->luk > 255) ? 255 : p->luk;
  1624. WFIFOB(fd,j+104) = p->char_num;
  1625. }
  1626. WFIFOSET(fd,WFIFOW(fd,2));
  1627. // printf("mmo_char_send006b end..\n");
  1628. return 0;
  1629. }
  1630. int parse_tologin(int fd) {
  1631. int i;
  1632. struct char_session_data *sd;
  1633. // only login-server can have an access to here.
  1634. // so, if it isn't the login-server, we disconnect the session.
  1635. //session eof check!
  1636. if(fd != login_fd)
  1637. session[fd]->eof = 1;
  1638. if(session[fd]->eof) {
  1639. if (fd == login_fd) {
  1640. ShowWarning("Connection to login-server lost (connection #%d).\n", fd);
  1641. login_fd = -1;
  1642. }
  1643. do_close(fd);
  1644. return 0;
  1645. }
  1646. sd = (struct char_session_data*)session[fd]->session_data;
  1647. // hehe. no need to set user limit on SQL version. :P
  1648. // but char limitation is good way to maintain server. :D
  1649. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  1650. // printf("parse_tologin : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0));
  1651. switch(RFIFOW(fd, 0)){
  1652. case 0x2711:
  1653. if (RFIFOREST(fd) < 3)
  1654. return 0;
  1655. if (RFIFOB(fd, 2)) {
  1656. //printf("connect login server error : %d\n", RFIFOB(fd, 2));
  1657. ShowError("Can not connect to login-server.\n");
  1658. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1659. ShowError("Also, please make sure your login db has the correct coounication username/passwords and the gender of the account is S.\n");
  1660. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1661. return 0;
  1662. //exit(1); //fixed for server shutdown.
  1663. }else {
  1664. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1665. if (kick_on_disconnect)
  1666. set_all_offline();
  1667. // if no map-server already connected, display a message...
  1668. for(i = 0; i < MAX_MAP_SERVERS; i++)
  1669. if (server_fd[i] > 0 && server[i].map[0]) // if map-server online and at least 1 map
  1670. break;
  1671. if (i == MAX_MAP_SERVERS)
  1672. ShowStatus("Awaiting maps from map-server.\n");
  1673. }
  1674. RFIFOSKIP(fd, 3);
  1675. break;
  1676. case 0x2713:
  1677. if(RFIFOREST(fd)<51)
  1678. return 0;
  1679. for(i = 0; i < fd_max; i++) {
  1680. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) {
  1681. if (RFIFOB(fd,6) != 0) {
  1682. WFIFOW(i,0) = 0x6c;
  1683. WFIFOB(i,2) = 0x42;
  1684. WFIFOSET(i,3);
  1685. } else if (max_connect_user == 0 || count_users() < max_connect_user) {
  1686. // if (max_connect_user == 0)
  1687. // printf("max_connect_user (unlimited) -> accepted.\n");
  1688. // else
  1689. // printf("count_users(): %d < max_connect_user (%d) -> accepted.\n", count_users(), max_connect_user);
  1690. sd->connect_until_time = (time_t)RFIFOL(fd,47);
  1691. memcpy(sd->email, RFIFOP(fd, 7), 40);
  1692. // send characters to player
  1693. mmo_char_send006b(i, sd);
  1694. } else if(isGM(sd->account_id) >= gm_allow_level) {
  1695. sd->connect_until_time = (time_t)RFIFOL(fd,47);
  1696. memcpy(sd->email, RFIFOP(fd, 7), 40);
  1697. // send characters to player
  1698. mmo_char_send006b(i, sd);
  1699. } else {
  1700. // refuse connection: too much online players
  1701. // printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user);
  1702. WFIFOW(i,0) = 0x6c;
  1703. WFIFOW(i,2) = 0;
  1704. WFIFOSET(i,3);
  1705. }
  1706. }
  1707. }
  1708. RFIFOSKIP(fd,51);
  1709. break;
  1710. case 0x2717:
  1711. if (RFIFOREST(fd) < 50)
  1712. return 0;
  1713. for(i = 0; i < fd_max; i++) {
  1714. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1715. if (sd->account_id == RFIFOL(fd,2)) {
  1716. sd->connect_until_time = (time_t)RFIFOL(fd,46);
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. RFIFOSKIP(fd,50);
  1722. break;
  1723. // login-server alive packet
  1724. case 0x2718:
  1725. if (RFIFOREST(fd) < 2)
  1726. return 0;
  1727. RFIFOSKIP(fd,2);
  1728. break;
  1729. // Receiving authentification from Freya-type login server (to avoid char->login->char)
  1730. case 0x2719:
  1731. if (RFIFOREST(fd) < 18)
  1732. return 0;
  1733. // to conserv a maximum of authentification, search if account is already authentified and replace it
  1734. // that will reduce multiple connection too
  1735. for(i = 0; i < AUTH_FIFO_SIZE; i++)
  1736. if (auth_fifo[i].account_id == RFIFOL(fd,2))
  1737. break;
  1738. // if not found, use next value
  1739. if (i == AUTH_FIFO_SIZE) {
  1740. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  1741. auth_fifo_pos = 0;
  1742. i = auth_fifo_pos;
  1743. auth_fifo_pos++;
  1744. }
  1745. //printf("auth_fifo set (auth #%d) - account: %d, secure: %08x-%08x\n", i, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10));
  1746. auth_fifo[i].account_id = RFIFOL(fd,2);
  1747. auth_fifo[i].char_id = 0;
  1748. auth_fifo[i].login_id1 = RFIFOL(fd,6);
  1749. auth_fifo[i].login_id2 = RFIFOL(fd,10);
  1750. auth_fifo[i].delflag = 2; // 0: auth_fifo canceled/void, 2: auth_fifo received from login/map server in memory, 1: connection authentified
  1751. auth_fifo[i].char_pos = 0;
  1752. auth_fifo[i].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server)
  1753. auth_fifo[i].ip = RFIFOL(fd,14);
  1754. //auth_fifo[i].map_auth = 0;
  1755. RFIFOSKIP(fd,18);
  1756. break;
  1757. /* case 0x2721: // gm reply. I don't want to support this function.
  1758. printf("0x2721:GM reply\n");
  1759. {
  1760. int oldacc, newacc;
  1761. unsigned char buf[64];
  1762. if (RFIFOREST(fd) < 10)
  1763. return 0;
  1764. oldacc = RFIFOL(fd, 2);
  1765. newacc = RFIFOL(fd, 6);
  1766. RFIFOSKIP(fd, 10);
  1767. if (newacc > 0) {
  1768. for(i=0;i<char_num;i++){
  1769. if(char_dat[i].account_id==oldacc)
  1770. char_dat[i].account_id=newacc;
  1771. }
  1772. }
  1773. WBUFW(buf,0)=0x2b0b;
  1774. WBUFL(buf,2)=oldacc;
  1775. WBUFL(buf,6)=newacc;
  1776. mapif_sendall(buf,10);
  1777. // printf("char -> map\n");
  1778. }
  1779. break;
  1780. */
  1781. case 0x2723: // changesex reply (modified by [Yor])
  1782. if (RFIFOREST(fd) < 7)
  1783. return 0;
  1784. {
  1785. int acc, sex;
  1786. unsigned char buf[16];
  1787. acc = RFIFOL(fd,2);
  1788. sex = RFIFOB(fd,6);
  1789. RFIFOSKIP(fd, 7);
  1790. if (acc > 0) {
  1791. sprintf(tmp_sql, "SELECT `char_id`,`class`,`skill_point`,`guild_id` FROM `%s` WHERE `account_id` = '%d'",char_db, acc);
  1792. if (mysql_query(&mysql_handle, tmp_sql)) {
  1793. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1794. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1795. }
  1796. sql_res = mysql_store_result(&mysql_handle);
  1797. while(sql_res && (sql_row = mysql_fetch_row(sql_res))) {
  1798. int char_id, guild_id, jobclass, skill_point, class_;
  1799. char_id = atoi(sql_row[0]);
  1800. jobclass = atoi(sql_row[1]);
  1801. skill_point = atoi(sql_row[2]);
  1802. guild_id = atoi(sql_row[3]);
  1803. class_ = jobclass;
  1804. if (jobclass == 19 || jobclass == 20 ||
  1805. jobclass == 4020 || jobclass == 4021 ||
  1806. jobclass == 4042 || jobclass == 4043) {
  1807. // job modification
  1808. if (jobclass == 19 || jobclass == 20) {
  1809. class_ = (sex) ? 19 : 20;
  1810. } else if (jobclass == 4020 || jobclass == 4021) {
  1811. class_ = (sex) ? 4020 : 4021;
  1812. } else if (jobclass == 4042 || jobclass == 4043) {
  1813. class_ = (sex) ? 4042 : 4043;
  1814. }
  1815. // remove specifical skills of classes 19,20 4020,4021 and 4042,4043
  1816. sprintf(tmp_sql, "SELECT `lv` FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1817. if (mysql_query(&mysql_handle, tmp_sql)) {
  1818. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1819. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1820. }
  1821. sql_res = mysql_store_result(&mysql_handle);
  1822. if (sql_res) {
  1823. while(( sql_row = mysql_fetch_row(sql_res))) {
  1824. skill_point += atoi(sql_row[0]);
  1825. }
  1826. }
  1827. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1828. if (mysql_query(&mysql_handle, tmp_sql)) {
  1829. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1830. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1831. }
  1832. }
  1833. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  1834. sprintf(tmp_sql, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'",inventory_db, char_id);
  1835. if (mysql_query(&mysql_handle, tmp_sql)) {
  1836. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1837. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1838. }
  1839. sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d' , `skill_point`='%d' , `weapon`='0' , `shield='0' , `head_top`='0' , `head_mid`='0' , `head_bottom`='0' WHERE `char_id` = '%d'",char_db, class_, skill_point, char_id);
  1840. if (mysql_query(&mysql_handle, tmp_sql)) {
  1841. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1842. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1843. }
  1844. if (guild_id) //If there is a guild, update the guild_member data [Skotlex]
  1845. inter_guild_sex_changed(guild_id, acc, char_id, sex);
  1846. }
  1847. }
  1848. // disconnect player if online on char-server
  1849. for(i = 0; i < fd_max; i++) {
  1850. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1851. if (sd->account_id == acc) {
  1852. session[i]->eof = 1;
  1853. break;
  1854. }
  1855. }
  1856. }
  1857. WBUFW(buf,0) = 0x2b0d;
  1858. WBUFL(buf,2) = acc;
  1859. WBUFB(buf,6) = sex;
  1860. mapif_sendall(buf, 7);
  1861. }
  1862. break;
  1863. // account_reg2変更通知
  1864. case 0x2729:
  1865. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1866. return 0;
  1867. { //Receive account_reg2 registry, forward to map servers.
  1868. unsigned char buf[ACCOUNT_REG2_NUM*(256+32+2)+16];
  1869. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  1870. // WBUFW(buf,0) = 0x2b11;
  1871. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  1872. mapif_sendall(buf, WBUFW(buf,2));
  1873. RFIFOSKIP(fd, RFIFOW(fd,2));
  1874. }
  1875. break;
  1876. // State change of account/ban notification (from login-server) by [Yor]
  1877. case 0x2731:
  1878. if (RFIFOREST(fd) < 11)
  1879. return 0;
  1880. // send to all map-servers to disconnect the player
  1881. {
  1882. unsigned char buf[16];
  1883. WBUFW(buf,0) = 0x2b14;
  1884. WBUFL(buf,2) = RFIFOL(fd,2);
  1885. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  1886. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  1887. mapif_sendall(buf, 11);
  1888. }
  1889. // disconnect player if online on char-server
  1890. for(i = 0; i < fd_max; i++) {
  1891. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1892. if (sd->account_id == RFIFOL(fd,2)) {
  1893. session[i]->eof = 1;
  1894. break;
  1895. }
  1896. }
  1897. }
  1898. RFIFOSKIP(fd,11);
  1899. break;
  1900. case 0x2732:
  1901. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1902. return 0;
  1903. {
  1904. unsigned char buf[32000];
  1905. if (gm_account != NULL)
  1906. aFree(gm_account);
  1907. gm_account = (struct gm_account*)aCalloc(sizeof(struct gm_account) * ((RFIFOW(fd,2) - 4) / 5), 1);
  1908. GM_num = 0;
  1909. for (i = 4; i < RFIFOW(fd,2); i = i + 5) {
  1910. gm_account[GM_num].account_id = RFIFOL(fd,i);
  1911. gm_account[GM_num].level = (int)RFIFOB(fd,i+4);
  1912. //printf("GM account: %d -> level %d\n", gm_account[GM_num].account_id, gm_account[GM_num].level);
  1913. GM_num++;
  1914. }
  1915. ShowStatus("From login-server: receiving information of %d GM accounts.\n", GM_num);
  1916. // send new gm acccounts level to map-servers
  1917. memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2));
  1918. WBUFW(buf,0) = 0x2b15;
  1919. mapif_sendall(buf, RFIFOW(fd,2));
  1920. }
  1921. RFIFOSKIP(fd,RFIFOW(fd,2));
  1922. break;
  1923. // Receive GM accounts [Freya login server packet by Yor]
  1924. case 0x2733:
  1925. // add test here to remember that the login-server is Freya-type
  1926. // sprintf (login_server_type, "Freya");
  1927. if (RFIFOREST(fd) < 7)
  1928. return 0;
  1929. {
  1930. int new_level = 0;
  1931. for(i = 0; i < GM_num; i++)
  1932. if (gm_account[i].account_id == RFIFOL(fd,2)) {
  1933. if (gm_account[i].level != (int)RFIFOB(fd,6)) {
  1934. gm_account[i].level = (int)RFIFOB(fd,6);
  1935. new_level = 1;
  1936. }
  1937. break;
  1938. }
  1939. // if not found, add it
  1940. if (i == GM_num) {
  1941. // limited to 4000, because we send information to char-servers (more than 4000 GM accounts???)
  1942. // int (id) + int (level) = 8 bytes * 4000 = 32k (limit of packets in windows)
  1943. if (((int)RFIFOB(fd,6)) > 0 && GM_num < 4000) {
  1944. if (GM_num == 0) {
  1945. gm_account = (struct gm_account*)aMalloc(sizeof(struct gm_account));
  1946. } else {
  1947. gm_account = (struct gm_account*)aRealloc(gm_account, sizeof(struct gm_account) * (GM_num + 1));
  1948. }
  1949. gm_account[GM_num].account_id = RFIFOL(fd,2);
  1950. gm_account[GM_num].level = (int)RFIFOB(fd,6);
  1951. new_level = 1;
  1952. GM_num++;
  1953. if (GM_num >= 4000)
  1954. ShowWarning("4000 GM accounts found. Next GM accounts are not readed.\n");
  1955. }
  1956. }
  1957. if (new_level == 1) {
  1958. ShowStatus("From login-server: receiving GM account information (%d: level %d).\n", RFIFOL(fd,2), (int)RFIFOB(fd,6));
  1959. mapif_send_gmaccounts();
  1960. //create_online_files(); // not change online file for only 1 player (in next timer, that will be done
  1961. // send gm acccounts level to map-servers
  1962. }
  1963. }
  1964. RFIFOSKIP(fd,7);
  1965. break;
  1966. //Login server request to kick a character out. [Skotlex]
  1967. case 0x2734:
  1968. if (RFIFOREST(fd) < 6)
  1969. return 0;
  1970. {
  1971. struct online_char_data* character;
  1972. int aid = RFIFOL(fd,2);
  1973. if ((character = idb_get(online_char_db, aid)) != NULL)
  1974. { //Kick out this player.
  1975. if (character->server > -1)
  1976. { //Kick it from the map server it is on.
  1977. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  1978. if (!character->waiting_disconnect)
  1979. add_timer(gettick()+15000, chardb_waiting_disconnect, character->account_id, 0);
  1980. character->waiting_disconnect = 1;
  1981. } else { //Manual kick from char server.
  1982. struct char_session_data *tsd;
  1983. int i;
  1984. for(i = 0; i < fd_max; i++) {
  1985. if (session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid)
  1986. {
  1987. WFIFOW(i,0) = 0x81;
  1988. WFIFOB(i,2) = 2;
  1989. WFIFOSET(i,3);
  1990. break;
  1991. }
  1992. }
  1993. if (i == fd_max) //Shouldn't happen, but just in case.
  1994. set_char_offline(99, aid);
  1995. }
  1996. }
  1997. RFIFOSKIP(fd,6);
  1998. }
  1999. break;
  2000. default:
  2001. ShowError("Unknown packet 0x%04x from login server, disconnecting.\n", RFIFOW(fd, 0));
  2002. session[fd]->eof = 1;
  2003. return 0;
  2004. }
  2005. }
  2006. RFIFOFLUSH(fd);
  2007. return 0;
  2008. }
  2009. int request_accreg2(int account_id, int char_id) {
  2010. if (login_fd > 0) {
  2011. WFIFOW(login_fd, 0) = 0x272e;
  2012. WFIFOL(login_fd, 2) = account_id;
  2013. WFIFOL(login_fd, 6) = char_id;
  2014. WFIFOSET(login_fd, 10);
  2015. return 1;
  2016. }
  2017. return 0;
  2018. }
  2019. //Send packet forward to login-server for account saving
  2020. int save_accreg2(unsigned char* buf, int len) {
  2021. if (login_fd > 0) {
  2022. WFIFOHEAD(login_fd, len+4);
  2023. memcpy(WFIFOP(login_fd,4), buf, len);
  2024. WFIFOW(login_fd,0) = 0x2728;
  2025. WFIFOW(login_fd,2) = len+4;
  2026. WFIFOSET(login_fd,len+4);
  2027. return 1;
  2028. }
  2029. return 0;
  2030. }
  2031. int search_mapserver(unsigned short map, long ip, short port);
  2032. int parse_frommap(int fd) {
  2033. int i = 0, j = 0;
  2034. int id;
  2035. // int auth_fifo_flag=0;
  2036. // Sometimes fd=0, and it will cause server crash. Don't know why. :(
  2037. if (fd <= 0) {
  2038. ShowError("parse_frommap error fd=%d\n", fd);
  2039. return 0;
  2040. }
  2041. for(id = 0; id < MAX_MAP_SERVERS; id++)
  2042. if (server_fd[id] == fd)
  2043. break;
  2044. if(id == MAX_MAP_SERVERS)
  2045. session[fd]->eof = 1;
  2046. if(session[fd]->eof) {
  2047. if (id < MAX_MAP_SERVERS) {
  2048. unsigned char buf[16384];
  2049. ShowStatus("Map-server %d (session #%d) has disconnected.\n", id, fd);
  2050. //Notify other map servers that this one is gone. [Skotlex]
  2051. WBUFW(buf,0) = 0x2b20;
  2052. WBUFL(buf,4) = server[id].ip;
  2053. WBUFW(buf,8) = server[id].port;
  2054. j = 0;
  2055. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2056. if (server[id].map[i])
  2057. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2058. if (j > 0) {
  2059. WBUFW(buf,2) = j * 4 + 10;
  2060. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2061. }
  2062. memset(&server[id], 0, sizeof(struct mmo_map_server));
  2063. sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[id]);
  2064. if (mysql_query(&mysql_handle, tmp_sql)) {
  2065. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2066. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2067. }
  2068. server_fd[id] = -1;
  2069. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2070. }
  2071. do_close(fd);
  2072. return 0;
  2073. }
  2074. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  2075. // printf("parse_frommap : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd,0));
  2076. switch(RFIFOW(fd, 0)) {
  2077. // map-server alive packet
  2078. case 0x2718:
  2079. if (RFIFOREST(fd) < 2)
  2080. return 0;
  2081. RFIFOSKIP(fd,2);
  2082. break;
  2083. case 0x2af7:
  2084. RFIFOSKIP(fd,2);
  2085. if (login_fd > 0) { // don't send request if no login-server
  2086. WFIFOW(login_fd,0) = 0x2709;
  2087. WFIFOSET(login_fd, 2);
  2088. }
  2089. break;
  2090. // mapserver -> map names recv.
  2091. case 0x2afa:
  2092. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2093. return 0;
  2094. memset(server[id].map, 0, sizeof(server[id].map));
  2095. j = 0;
  2096. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2097. server[id].map[j] = RFIFOW(fd,i);
  2098. // printf("set map %d.%d : %s\n", id, j, server[id].map[j]);
  2099. j++;
  2100. }
  2101. i = server[id].ip;
  2102. {
  2103. unsigned char *p = (unsigned char *)&server[id].ip;
  2104. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2105. id, j, p[0], p[1], p[2], p[3], server[id].port);
  2106. ShowStatus("Map-server %d loading complete.\n", id);
  2107. if (kick_on_disconnect)
  2108. set_all_offline();
  2109. if (max_account_id != DEFAULT_MAX_ACCOUNT_ID || max_char_id != DEFAULT_MAX_CHAR_ID)
  2110. mapif_send_maxid(max_account_id, max_char_id); //Send the current max ids to the server to keep in sync [Skotlex]
  2111. }
  2112. WFIFOW(fd,0) = 0x2afb;
  2113. WFIFOB(fd,2) = 0;
  2114. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH); // name for wisp to player
  2115. WFIFOSET(fd,3+NAME_LENGTH);
  2116. //WFIFOSET(fd,27);
  2117. {
  2118. unsigned char buf[16384];
  2119. int x;
  2120. if (j == 0) {
  2121. ShowWarning("Map-Server %d have NO maps.\n", id);
  2122. // Transmitting maps information to the other map-servers
  2123. } else {
  2124. WBUFW(buf,0) = 0x2b04;
  2125. WBUFW(buf,2) = j * 4 + 10;
  2126. WBUFL(buf,4) = server[id].ip;
  2127. WBUFW(buf,8) = server[id].port;
  2128. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2129. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2130. }
  2131. // Transmitting the maps of the other map-servers to the new map-server
  2132. for(x = 0; x < MAX_MAP_SERVERS; x++) {
  2133. if (server_fd[x] > 0 && x != id) {
  2134. WFIFOW(fd,0) = 0x2b04;
  2135. WFIFOL(fd,4) = server[x].ip;
  2136. WFIFOW(fd,8) = server[x].port;
  2137. j = 0;
  2138. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2139. if (server[x].map[i])
  2140. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2141. if (j > 0) {
  2142. WFIFOW(fd,2) = j * 4 + 10;
  2143. WFIFOSET(fd,WFIFOW(fd,2));
  2144. }
  2145. }
  2146. }
  2147. }
  2148. RFIFOSKIP(fd,RFIFOW(fd,2));
  2149. break;
  2150. //Packet command is now used for sc_data request. [Skotlex]
  2151. case 0x2afc:
  2152. if (RFIFOREST(fd) < 10)
  2153. return 0;
  2154. {
  2155. int aid, cid;
  2156. aid = RFIFOL(fd,2);
  2157. cid = RFIFOL(fd,6);
  2158. RFIFOSKIP(fd, 10);
  2159. #ifdef ENABLE_SC_SAVING
  2160. sprintf(tmp_sql, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2161. scdata_db, aid, cid);
  2162. if (mysql_query(&mysql_handle, tmp_sql)) {
  2163. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2164. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2165. break;
  2166. }
  2167. sql_res = mysql_store_result(&mysql_handle);
  2168. if (sql_res) {
  2169. struct status_change_data data;
  2170. int count = 0;
  2171. WFIFOW(fd, 0) = 0x2b1d;
  2172. WFIFOL(fd, 4) = aid;
  2173. WFIFOL(fd, 8) = cid;
  2174. while((sql_row = mysql_fetch_row(sql_res)))
  2175. {
  2176. data.type = atoi(sql_row[0]);
  2177. data.tick = atoi(sql_row[1]);
  2178. data.val1 = atoi(sql_row[2]);
  2179. data.val2 = atoi(sql_row[3]);
  2180. data.val3 = atoi(sql_row[4]);
  2181. data.val4 = atoi(sql_row[5]);
  2182. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &data, sizeof(struct status_change_data));
  2183. count++;
  2184. }
  2185. if (count > 0)
  2186. {
  2187. WFIFOW(fd, 2) = 14 + count*sizeof(struct status_change_data);
  2188. WFIFOW(fd, 12) = count;
  2189. WFIFOSET(fd, WFIFOW(fd,2));
  2190. //Clear the data once loaded.
  2191. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid);
  2192. if (mysql_query(&mysql_handle, tmp_sql)) {
  2193. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2194. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2195. }
  2196. }
  2197. }
  2198. #endif
  2199. break;
  2200. }
  2201. //set MAP user count
  2202. case 0x2afe:
  2203. if (RFIFOREST(fd) < 4)
  2204. return 0;
  2205. if (RFIFOW(fd,2) != server[id].users) {
  2206. server[id].users = RFIFOW(fd,2);
  2207. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2208. }
  2209. RFIFOSKIP(fd, 4);
  2210. break;
  2211. // set MAP user
  2212. case 0x2aff:
  2213. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2214. return 0;
  2215. {
  2216. int i, aid, cid;
  2217. struct online_char_data* character;
  2218. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2219. server[id].users = RFIFOW(fd,4);
  2220. for(i = 0; i < server[id].users; i++) {
  2221. aid = RFIFOL(fd,6+i*8);
  2222. cid = RFIFOL(fd,6+i*8+4);
  2223. character = idb_ensure(online_char_db, aid, create_online_char_data);
  2224. if (character->server > -1 && character->server != id)
  2225. {
  2226. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2227. character->account_id, character->char_id, character->server, id, aid, cid);
  2228. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  2229. }
  2230. character->server = id;
  2231. character->char_id = cid;
  2232. }
  2233. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2234. RFIFOSKIP(fd,RFIFOW(fd,2));
  2235. break;
  2236. }
  2237. // char saving
  2238. case 0x2b01:
  2239. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2240. return 0;
  2241. {
  2242. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8);
  2243. i = 0;
  2244. //check account
  2245. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",char_db, aid, cid); // TBR
  2246. if (mysql_query(&mysql_handle, tmp_sql)) {
  2247. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2248. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2249. }
  2250. sql_res = mysql_store_result(&mysql_handle);
  2251. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2252. if (sql_row) i = atoi(sql_row[0]);
  2253. if (sql_res) mysql_free_result(sql_res);
  2254. if (i == 1) {
  2255. memcpy(&char_dat[0], RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2256. mmo_char_tosql(cid, char_dat);
  2257. //save to DB
  2258. } else
  2259. ShowError("parse_from_map (save-char): Received data for non-existant character (%d:%d)!\n", aid, cid);
  2260. if (RFIFOB(fd,12)) { //Flag? Set character offline after saving [Skotlex]
  2261. set_char_offline(cid, aid);
  2262. }
  2263. RFIFOSKIP(fd,RFIFOW(fd,2));
  2264. break;
  2265. }
  2266. // req char selection
  2267. case 0x2b02:
  2268. if (RFIFOREST(fd) < 18)
  2269. return 0;
  2270. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  2271. auth_fifo_pos = 0;
  2272. auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2);
  2273. auth_fifo[auth_fifo_pos].char_id = 0;
  2274. auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6);
  2275. auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10);
  2276. auth_fifo[auth_fifo_pos].delflag = 2;
  2277. auth_fifo[auth_fifo_pos].char_pos = 0;
  2278. auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server)
  2279. auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,14);
  2280. auth_fifo_pos++;
  2281. WFIFOW(fd, 0) = 0x2b03;
  2282. WFIFOL(fd, 2) = RFIFOL(fd, 2);
  2283. WFIFOB(fd, 6) = 0;
  2284. WFIFOSET(fd, 7);
  2285. RFIFOSKIP(fd, 18);
  2286. break;
  2287. // request "change map server"
  2288. case 0x2b05:
  2289. if (RFIFOREST(fd) < 35)
  2290. return 0;
  2291. {
  2292. unsigned short name;
  2293. int map_id, map_fd = -1;
  2294. struct online_char_data* data;
  2295. struct mmo_charstatus* char_data;
  2296. name = RFIFOW(fd,18);
  2297. map_id = search_mapserver(name, RFIFOL(fd,24), RFIFOW(fd,28)); //Locate mapserver by ip and port.
  2298. if (map_id >= 0)
  2299. map_fd = server_fd[map_id];
  2300. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2301. char_data = uidb_get(char_db_,RFIFOL(fd,14));
  2302. if (char_data == NULL)
  2303. { //Really shouldn't happen.
  2304. mmo_char_fromsql(RFIFOL(fd,14), char_dat);
  2305. char_data = char_dat;
  2306. }
  2307. //Tell the new map server about this player using Kevin's new auth packet. [Skotlex]
  2308. if (map_fd>=0 && session[map_fd] && char_data)
  2309. { //Send the map server the auth of this player.
  2310. //Update the "last map" as this is where the player must be spawned on the new map server.
  2311. char_data->last_point.map = RFIFOW(fd,18);
  2312. char_data->last_point.x = RFIFOW(fd,20);
  2313. char_data->last_point.y = RFIFOW(fd,22);
  2314. WFIFOW(map_fd,0) = 0x2afd;
  2315. WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus);
  2316. WFIFOL(map_fd,4) = RFIFOL(fd, 2); //Account ID
  2317. WFIFOL(map_fd,8) = RFIFOL(fd, 6); //Login1
  2318. WFIFOL(map_fd,16) = RFIFOL(fd,10); //Login2
  2319. WFIFOL(map_fd,12) = (unsigned long)0; //TODO: connect_until_time, how do I figure it out right now?
  2320. memcpy(WFIFOP(map_fd,20), char_data, sizeof(struct mmo_charstatus));
  2321. WFIFOSET(map_fd, WFIFOW(map_fd,2));
  2322. data = idb_ensure(online_char_db, RFIFOL(fd, 2), create_online_char_data);
  2323. data->char_id = char_data->char_id;
  2324. data->server = map_id; //Update server where char is.
  2325. //Reply with an ack.
  2326. WFIFOW(fd, 0) = 0x2b06;
  2327. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2328. WFIFOSET(fd, 30);
  2329. } else { //Reply with nak
  2330. WFIFOW(fd, 0) = 0x2b06;
  2331. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2332. WFIFOL(fd, 6) = 0; //Set login1 to 0.
  2333. WFIFOSET(fd, 30);
  2334. }
  2335. RFIFOSKIP(fd, 35);
  2336. }
  2337. break;
  2338. // char name check
  2339. case 0x2b08:
  2340. if (RFIFOREST(fd) < 6)
  2341. return 0;
  2342. sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, (int)RFIFOL(fd,2));
  2343. if (mysql_query(&mysql_handle, tmp_sql)) {
  2344. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2345. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2346. }
  2347. sql_res = mysql_store_result(&mysql_handle);
  2348. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2349. WFIFOW(fd,0) = 0x2b09;
  2350. WFIFOL(fd,2) = RFIFOL(fd,2);
  2351. if (sql_row)
  2352. memcpy(WFIFOP(fd,6), sql_row[0], NAME_LENGTH);
  2353. else
  2354. memcpy(WFIFOP(fd,6), unknown_char_name, NAME_LENGTH);
  2355. if (sql_res) mysql_free_result(sql_res);
  2356. WFIFOSET(fd,30);
  2357. RFIFOSKIP(fd,6);
  2358. break;
  2359. // I want become GM - fuck!
  2360. case 0x2b0a:
  2361. if(RFIFOREST(fd)<4)
  2362. return 0;
  2363. if(RFIFOREST(fd)<RFIFOW(fd,2))
  2364. return 0;
  2365. /*
  2366. memcpy(WFIFOP(login_fd,2),RFIFOP(fd,2),RFIFOW(fd,2)-2);
  2367. WFIFOW(login_fd,0)=0x2720;
  2368. WFIFOSET(login_fd,RFIFOW(fd,2));
  2369. // printf("char : change gm -> login %d %s %d\n", RFIFOL(fd, 4), RFIFOP(fd, 8), RFIFOW(fd, 2));
  2370. */
  2371. ShowWarning("packet 0x2ba (become GM) is not supported by the Char-Server.\n");
  2372. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2373. break;
  2374. //Packet 0x2b10 deprecated in favor of packet 0x3004 for registry saving. [Skotlex]
  2375. //case 0x2b10:
  2376. // Map server send information to change an email of an account -> login-server
  2377. case 0x2b0c:
  2378. if (RFIFOREST(fd) < 86)
  2379. return 0;
  2380. if (login_fd > 0) { // don't send request if no login-server
  2381. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), 86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2382. WFIFOW(login_fd,0) = 0x2722;
  2383. WFIFOSET(login_fd, 86);
  2384. }
  2385. RFIFOSKIP(fd, 86);
  2386. break;
  2387. // Receiving from map-server a status change resquest. Transmission to login-server (by Yor)
  2388. case 0x2b0e:
  2389. if (RFIFOREST(fd) < 44)
  2390. return 0;
  2391. {
  2392. char character_name[NAME_LENGTH], t_name[NAME_LENGTH*2];
  2393. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody)
  2394. memcpy(character_name, RFIFOP(fd,6), NAME_LENGTH);
  2395. character_name[NAME_LENGTH-1] = '\0';
  2396. jstrescapecpy(t_name, character_name); //Escape string for sql use... [Skotlex]
  2397. // prepare answer
  2398. WFIFOW(fd,0) = 0x2b0f; // answer
  2399. WFIFOL(fd,2) = acc; // who want do operation
  2400. WFIFOW(fd,30) = RFIFOW(fd, 30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2401. sprintf(tmp_sql, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'",char_db, t_name);
  2402. if (mysql_query(&mysql_handle, tmp_sql)) {
  2403. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2404. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2405. }
  2406. sql_res = mysql_store_result(&mysql_handle);
  2407. if (sql_res) {
  2408. if (mysql_num_rows(sql_res)) {
  2409. sql_row = mysql_fetch_row(sql_res);
  2410. memcpy(WFIFOP(fd,6), sql_row[1], NAME_LENGTH); // put correct name if found
  2411. WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2412. switch(RFIFOW(fd, 30)) {
  2413. case 1: // block
  2414. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2415. if (login_fd > 0) { // don't send request if no login-server
  2416. WFIFOW(login_fd,0) = 0x2724;
  2417. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  2418. WFIFOL(login_fd,6) = 5; // status of the account
  2419. WFIFOSET(login_fd, 10);
  2420. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5);
  2421. } else
  2422. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2423. } else
  2424. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2425. break;
  2426. case 2: // ban
  2427. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2428. if (login_fd > 0) { // don't send request if no login-server
  2429. WFIFOW(login_fd, 0) = 0x2725;
  2430. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2431. WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year
  2432. WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month
  2433. WFIFOW(login_fd,10) = RFIFOW(fd,36); // day
  2434. WFIFOW(login_fd,12) = RFIFOW(fd,38); // hour
  2435. WFIFOW(login_fd,14) = RFIFOW(fd,40); // minute
  2436. WFIFOW(login_fd,16) = RFIFOW(fd,42); // second
  2437. WFIFOSET(login_fd,18);
  2438. // printf("char : status -> login: account %d, ban: %dy %dm %dd %dh %dmn %ds\n",
  2439. // char_dat[i].account_id, (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), (short)RFIFOW(fd,38), (short)RFIFOW(fd,40), (short)RFIFOW(fd,42));
  2440. } else
  2441. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2442. } else
  2443. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2444. break;
  2445. case 3: // unblock
  2446. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2447. if (login_fd > 0) { // don't send request if no login-server
  2448. WFIFOW(login_fd,0) = 0x2724;
  2449. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  2450. WFIFOL(login_fd,6) = 0; // status of the account
  2451. WFIFOSET(login_fd, 10);
  2452. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0);
  2453. } else
  2454. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2455. } else
  2456. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2457. break;
  2458. case 4: // unban
  2459. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2460. if (login_fd > 0) { // don't send request if no login-server
  2461. WFIFOW(login_fd, 0) = 0x272a;
  2462. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2463. WFIFOSET(login_fd, 6);
  2464. // printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id);
  2465. } else
  2466. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2467. } else
  2468. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2469. break;
  2470. case 5: // changesex
  2471. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2472. if (login_fd > 0) { // don't send request if no login-server
  2473. WFIFOW(login_fd, 0) = 0x2727;
  2474. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2475. WFIFOSET(login_fd, 6);
  2476. // printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id);
  2477. } else
  2478. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2479. } else
  2480. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2481. break;
  2482. }
  2483. } else {
  2484. // character name not found
  2485. memcpy(WFIFOP(fd,6), character_name, NAME_LENGTH);
  2486. WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2487. }
  2488. // send answer if a player ask, not if the server ask
  2489. if (acc != -1) {
  2490. WFIFOSET(fd, 34);
  2491. }
  2492. }
  2493. }
  2494. RFIFOSKIP(fd, 44);
  2495. break;
  2496. // Recieve rates [Wizputer]
  2497. case 0x2b16:
  2498. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8))
  2499. return 0;
  2500. {
  2501. char motd[256], t_name[512]; //Required for jstrescapecpy [Skotlex]
  2502. strncpy(motd, RFIFOP(fd,10), 255); //First copy it to make sure the motd fits.
  2503. motd[255]='\0';
  2504. jstrescapecpy(t_name,motd);
  2505. sprintf(tmp_sql, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d',`motd`='%s'",
  2506. fd, server_name, RFIFOW(fd,2), RFIFOW(fd,4), RFIFOW(fd,6), t_name);
  2507. if (mysql_query(&mysql_handle, tmp_sql)) {
  2508. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2509. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2510. }
  2511. RFIFOSKIP(fd,RFIFOW(fd,8));
  2512. break;
  2513. }
  2514. // Character disconnected set online 0 [Wizputer]
  2515. case 0x2b17:
  2516. if (RFIFOREST(fd) < 6 )
  2517. return 0;
  2518. //printf("Setting %d char offline\n",RFIFOL(fd,2));
  2519. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2520. RFIFOSKIP(fd,10);
  2521. break;
  2522. // Reset all chars to offline [Wizputer]
  2523. case 0x2b18:
  2524. ShowNotice("Map server [%d] requested to set all characters offline.\n", id);
  2525. set_all_offline();
  2526. RFIFOSKIP(fd,2);
  2527. break;
  2528. // Character set online [Wizputer]
  2529. case 0x2b19:
  2530. if (RFIFOREST(fd) < 6 )
  2531. return 0;
  2532. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2533. RFIFOSKIP(fd,10);
  2534. break;
  2535. // Request sending of fame list
  2536. case 0x2b1a:
  2537. if (RFIFOREST(fd) < 2)
  2538. return 0;
  2539. {
  2540. int len = 8, num = 0;
  2541. unsigned char buf[32000];
  2542. struct fame_list fame_item;
  2543. WBUFW(buf,0) = 0x2b1b;
  2544. sprintf(tmp_sql, "SELECT `char_id`,`fame`, `name` FROM `%s` WHERE `fame`>0 AND (`class`='10' OR `class`='4011'OR `class`='4033') ORDER BY `fame` DESC LIMIT 0,10", char_db);
  2545. if (mysql_query(&mysql_handle, tmp_sql)) {
  2546. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2547. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2548. }
  2549. sql_res = mysql_store_result(&mysql_handle);
  2550. if (sql_res) {
  2551. while((sql_row = mysql_fetch_row(sql_res))) {
  2552. fame_item.id = atoi(sql_row[0]);
  2553. fame_item.fame = atoi(sql_row[1]);
  2554. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2555. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2556. len += sizeof(struct fame_list);
  2557. if (++num == 10)
  2558. break;
  2559. }
  2560. }
  2561. mysql_free_result(sql_res);
  2562. WBUFW(buf, 6) = len; //Blacksmith block size
  2563. num = 0;
  2564. sprintf(tmp_sql, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='18' OR `class`='4019' OR `class`='4041') ORDER BY `fame` DESC LIMIT 0,10", char_db);
  2565. if (mysql_query(&mysql_handle, tmp_sql)) {
  2566. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2567. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2568. }
  2569. sql_res = mysql_store_result(&mysql_handle);
  2570. if (sql_res) {
  2571. while((sql_row = mysql_fetch_row(sql_res))) {
  2572. fame_item.id = atoi(sql_row[0]);
  2573. fame_item.fame = atoi(sql_row[1]);
  2574. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2575. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2576. len += sizeof(struct fame_list);
  2577. if (++num == 10)
  2578. break;
  2579. }
  2580. }
  2581. mysql_free_result(sql_res);
  2582. WBUFW(buf, 4) = len; //Alchemist block size
  2583. num = 0;
  2584. sprintf(tmp_sql, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND `class`='4046' ORDER BY `fame` DESC LIMIT 0,10", char_db);
  2585. if (mysql_query(&mysql_handle, tmp_sql)) {
  2586. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2587. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2588. }
  2589. sql_res = mysql_store_result(&mysql_handle);
  2590. if (sql_res) {
  2591. while((sql_row = mysql_fetch_row(sql_res))) {
  2592. fame_item.id = atoi(sql_row[0]);
  2593. fame_item.fame = atoi(sql_row[1]);
  2594. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2595. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2596. len += sizeof(struct fame_list);
  2597. if (++num == 10)
  2598. break;
  2599. }
  2600. }
  2601. mysql_free_result(sql_res);
  2602. WBUFW(buf, 2) = len; //Total packet length
  2603. mapif_sendall(buf, len);
  2604. RFIFOSKIP(fd,2);
  2605. break;
  2606. }
  2607. //Request saving sc_data of a player. [Skotlex]
  2608. case 0x2b1c:
  2609. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2610. return 0;
  2611. {
  2612. #ifdef ENABLE_SC_SAVING
  2613. int count, aid, cid, i;
  2614. struct status_change_data data;
  2615. char *p = tmp_sql;
  2616. aid = RFIFOL(fd, 4);
  2617. cid = RFIFOL(fd, 8);
  2618. count = RFIFOW(fd, 12);
  2619. p+= sprintf(p, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  2620. for (i = 0; i < count; i++)
  2621. {
  2622. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2623. p += sprintf (p, " ('%d','%d','%hu','%d','%d','%d','%d','%d'),", aid, cid,
  2624. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  2625. }
  2626. if (count > 0)
  2627. {
  2628. *--p = '\0'; //Remove final comma.
  2629. if (mysql_query(&mysql_handle, tmp_sql)) {
  2630. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2631. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2632. }
  2633. }
  2634. #endif
  2635. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2636. break;
  2637. }
  2638. default:
  2639. // inter server - packet
  2640. {
  2641. int r = inter_parse_frommap(fd);
  2642. if (r == 1) break; // processed
  2643. if (r == 2) return 0; // need more packet
  2644. }
  2645. // no inter server packet. no char server packet -> disconnect
  2646. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2647. session[fd]->eof = 1;
  2648. return 0;
  2649. }
  2650. }
  2651. return 0;
  2652. }
  2653. int search_mapserver(unsigned short map, long ip, short port) {
  2654. int i, j;
  2655. if (!map)
  2656. return -1;
  2657. for(i = 0; i < MAX_MAP_SERVERS; i++)
  2658. if (server_fd[i] > 0)
  2659. for (j = 0; server[i].map[j]; j++)
  2660. if (server[i].map[j] == map) {
  2661. if (ip > 0 && server[i].ip != ip)
  2662. continue;
  2663. if (port > 0 && server[i].port != port)
  2664. continue;
  2665. return i;
  2666. }
  2667. return -1;
  2668. }
  2669. int char_mapif_init(int fd) {
  2670. return inter_mapif_init(fd);
  2671. }
  2672. //-----------------------------------------------------
  2673. // Test to know if an IP come from LAN or WAN. by [Yor]
  2674. //-----------------------------------------------------
  2675. int lan_ip_check(unsigned char *p){
  2676. int i;
  2677. int lancheck = 1;
  2678. int subneti[4];
  2679. unsigned int k0, k1, k2, k3;
  2680. sscanf(lan_map_ip, "%d.%d.%d.%d", &k0, &k1, &k2, &k3);
  2681. subneti[0] = k0; subneti[1] = k1; subneti[2] = k2; subneti[3] = k3;
  2682. // printf("lan_ip_check: to compare: %d.%d.%d.%d, network: %d.%d.%d.%d/%d.%d.%d.%d\n",
  2683. // p[0], p[1], p[2], p[3],
  2684. // subneti[0], subneti[1], subneti[2], subneti[3],
  2685. // subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]);
  2686. for(i = 0; i < 4; i++) {
  2687. if ((subneti[i] & subnetmaski[i]) != (p[i] & subnetmaski[i])) {
  2688. lancheck = 0;
  2689. break;
  2690. }
  2691. }
  2692. return lancheck;
  2693. }
  2694. int parse_char(int fd) {
  2695. int i, ch = 0;
  2696. char email[40];
  2697. unsigned char buf[64];
  2698. unsigned short cmd;
  2699. int map_fd;
  2700. struct char_session_data *sd;
  2701. unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr;
  2702. sd = (struct char_session_data*)session[fd]->session_data;
  2703. if(login_fd < 0)
  2704. session[fd]->eof = 1;
  2705. if(session[fd]->eof) {
  2706. if (fd == login_fd)
  2707. login_fd = -1;
  2708. if (sd != NULL)
  2709. {
  2710. struct online_char_data* data = idb_get(online_char_db, sd->account_id);
  2711. if (!data || data->server== -1) //If it is not in any server, send it offline. [Skotlex]
  2712. set_char_offline(99,sd->account_id);
  2713. }
  2714. do_close(fd);
  2715. return 0;
  2716. }
  2717. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  2718. cmd = RFIFOW(fd,0);
  2719. // crc32のスキップ用
  2720. if( sd==NULL && // 未ログインor管理パケット
  2721. RFIFOREST(fd)>=4 && // 最低バイト数制限 & 0x7530,0x7532管理パケ除去
  2722. RFIFOREST(fd)<=21 && // 最大バイト数制限 & サーバーログイン除去
  2723. cmd!=0x20b && // md5通知パケット除去
  2724. (RFIFOREST(fd)<6 || RFIFOW(fd,4)==0x65) ){ // 次に何かパケットが来てるなら、接続でないとだめ
  2725. RFIFOSKIP(fd,4);
  2726. cmd = RFIFOW(fd,0);
  2727. ShowDebug("parse_char : %d crc32 skipped\n",fd);
  2728. if(RFIFOREST(fd)==0)
  2729. return 0;
  2730. }
  2731. //For use in packets that depend on an sd being present [Skotlex]
  2732. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL) { RFIFOSKIP(fd,rest); return 0; } }
  2733. switch(cmd){
  2734. case 0x20b: //20040622 encryption ragexe correspondence
  2735. if (RFIFOREST(fd) < 19)
  2736. return 0;
  2737. RFIFOSKIP(fd,19);
  2738. break;
  2739. case 0x65: // request to connect
  2740. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  2741. if (RFIFOREST(fd) < 17)
  2742. return 0;
  2743. {
  2744. if (sd == NULL) {
  2745. CREATE(session[fd]->session_data, struct char_session_data, 1);
  2746. sd = (struct char_session_data*)session[fd]->session_data;
  2747. sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server)
  2748. }
  2749. sd->account_id = RFIFOL(fd, 2);
  2750. sd->login_id1 = RFIFOL(fd, 6);
  2751. sd->login_id2 = RFIFOL(fd, 10);
  2752. sd->sex = RFIFOB(fd, 16);
  2753. WFIFOL(fd, 0) = RFIFOL(fd, 2);
  2754. WFIFOSET(fd, 4);
  2755. for(i = 0; i < AUTH_FIFO_SIZE; i++) {
  2756. if (auth_fifo[i].account_id == sd->account_id &&
  2757. auth_fifo[i].login_id1 == sd->login_id1 &&
  2758. #if CMP_AUTHFIFO_LOGIN2 != 0
  2759. auth_fifo[i].login_id2 == sd->login_id2 && // relate to the versions higher than 18
  2760. #endif
  2761. (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) &&
  2762. auth_fifo[i].delflag == 2) {
  2763. auth_fifo[i].delflag = 1;
  2764. if (online_check)
  2765. { // check if character is not online already. [Skotlex]
  2766. struct online_char_data* character;
  2767. character = idb_get(online_char_db, sd->account_id);
  2768. if (character)
  2769. {
  2770. if (character->server > -1)
  2771. { //Character already online. KICK KICK KICK
  2772. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  2773. if (!character->waiting_disconnect)
  2774. add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  2775. character->waiting_disconnect = 1;
  2776. /* Not a good idea because this would trigger when you do a char-change from the map server! [Skotlex]
  2777. } else { //Kick from char server.
  2778. struct char_session_data *tsd;
  2779. int i;
  2780. for(i = 0; i < fd_max; i++) {
  2781. if (session[i] && i != fd && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == sd->account_id)
  2782. {
  2783. WFIFOW(i,0) = 0x81;
  2784. WFIFOB(i,2) = 2;
  2785. WFIFOSET(i,3);
  2786. break;
  2787. }
  2788. if (i == fd_max) //Shouldn't happen, but just in case.
  2789. set_char_offline(99, sd->account_id);
  2790. }
  2791. */
  2792. WFIFOW(fd,0) = 0x81;
  2793. WFIFOB(fd,2) = 8;
  2794. WFIFOSET(fd,3);
  2795. break;
  2796. }
  2797. }
  2798. }
  2799. if (max_connect_user == 0 || count_users() < max_connect_user) {
  2800. if (login_fd > 0) { // don't send request if no login-server
  2801. // request to login-server to obtain e-mail/time limit
  2802. WFIFOW(login_fd,0) = 0x2716;
  2803. WFIFOL(login_fd,2) = sd->account_id;
  2804. WFIFOSET(login_fd,6);
  2805. }
  2806. // send characters to player
  2807. mmo_char_send006b(fd, sd);
  2808. } else {
  2809. // refuse connection (over populated)
  2810. WFIFOW(fd,0) = 0x6c;
  2811. WFIFOW(fd,2) = 0;
  2812. WFIFOSET(fd,3);
  2813. }
  2814. // printf("connection request> set delflag 1(o:2)- account_id:%d/login_id1:%d(fifo_id:%d)\n", sd->account_id, sd->login_id1, i);
  2815. break;
  2816. }
  2817. }
  2818. if (i == AUTH_FIFO_SIZE) {
  2819. if (login_fd > 0) { // don't send request if no login-server
  2820. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  2821. WFIFOL(login_fd,2) = sd->account_id;
  2822. WFIFOL(login_fd,6) = sd->login_id1;
  2823. WFIFOL(login_fd,10) = sd->login_id2;
  2824. WFIFOB(login_fd,14) = sd->sex;
  2825. WFIFOL(login_fd,15) = session[fd]->client_addr.sin_addr.s_addr;
  2826. WFIFOSET(login_fd,19);
  2827. } else { // if no login-server, we must refuse connection
  2828. WFIFOW(fd,0) = 0x6c;
  2829. WFIFOB(fd,2) = 0;
  2830. WFIFOSET(fd,3);
  2831. }
  2832. }
  2833. }
  2834. RFIFOSKIP(fd, 17);
  2835. break;
  2836. case 0x66: // char select
  2837. FIFOSD_CHECK(3);
  2838. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'",char_db, sd->account_id, RFIFOB(fd, 2));
  2839. RFIFOSKIP(fd, 3);
  2840. if (mysql_query(&mysql_handle, tmp_sql)) {
  2841. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2842. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2843. }
  2844. sql_res = mysql_store_result(&mysql_handle);
  2845. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2846. if (sql_row)
  2847. {
  2848. mmo_char_fromsql(atoi(sql_row[0]), char_dat);
  2849. char_dat[0].sex = sd->sex;
  2850. } else {
  2851. mysql_free_result(sql_res);
  2852. break;
  2853. }
  2854. if (log_char) {
  2855. char escaped_name[NAME_LENGTH*2];
  2856. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  2857. charlog_db, sd->account_id, RFIFOB(fd, 2), jstrescapecpy(escaped_name, char_dat[0].name));
  2858. //query
  2859. if(mysql_query(&mysql_handle, tmp_sql)) {
  2860. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2861. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2862. }
  2863. }
  2864. ShowInfo("Selected char: (Account %d: %d - %s)" RETCODE, sd->account_id, RFIFOB(fd, 2), char_dat[0].name);
  2865. i = search_mapserver(char_dat[0].last_point.map, -1, -1);
  2866. // if map is not found, we check major cities
  2867. if (i < 0) {
  2868. unsigned short j;
  2869. ShowWarning("Unable to find map-server for '%s', resorting to sending to a major city.\n", mapindex_id2name(char_dat[0].last_point.map));
  2870. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  2871. char_dat[0].last_point.map = j;
  2872. char_dat[0].last_point.x = 273; // savepoint coordinates
  2873. char_dat[0].last_point.y = 354;
  2874. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  2875. char_dat[0].last_point.map = j;
  2876. char_dat[0].last_point.x = 120; // savepoint coordinates
  2877. char_dat[0].last_point.y = 100;
  2878. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  2879. char_dat[0].last_point.map = j;
  2880. char_dat[0].last_point.x = 160; // savepoint coordinates
  2881. char_dat[0].last_point.y = 94;
  2882. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  2883. char_dat[0].last_point.map = j;
  2884. char_dat[0].last_point.x = 116; // savepoint coordinates
  2885. char_dat[0].last_point.y = 57;
  2886. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  2887. char_dat[0].last_point.map = j;
  2888. char_dat[0].last_point.x = 87; // savepoint coordinates
  2889. char_dat[0].last_point.y = 117;
  2890. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  2891. char_dat[0].last_point.map = j;
  2892. char_dat[0].last_point.x = 94; // savepoint coordinates
  2893. char_dat[0].last_point.y = 103;
  2894. } else {
  2895. // get first online server
  2896. i = 0;
  2897. for(j = 0; j < MAX_MAP_SERVERS; j++)
  2898. if (server_fd[j] > 0 && server[j].map[0]) {
  2899. i = j;
  2900. ShowDebug("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]);
  2901. break;
  2902. }
  2903. // if no map-servers are connected, we send: server closed
  2904. if (j == MAX_MAP_SERVERS) {
  2905. WFIFOW(fd,0) = 0x81;
  2906. WFIFOB(fd,2) = 1; // 01 = Server closed
  2907. WFIFOSET(fd,3);
  2908. break;
  2909. }
  2910. }
  2911. }
  2912. WFIFOW(fd, 0) =0x71;
  2913. WFIFOL(fd, 2) =char_dat[0].char_id;
  2914. memcpy(WFIFOP(fd,6), mapindex_id2name(char_dat[0].last_point.map), MAP_NAME_LENGTH);
  2915. //Lan check added by Kashy
  2916. if (lan_ip_check(p))
  2917. WFIFOL(fd, 22) = inet_addr(lan_map_ip);
  2918. else
  2919. WFIFOL(fd, 22) = server[i].ip;
  2920. WFIFOW(fd,26) = server[i].port;
  2921. WFIFOSET(fd,28);
  2922. if (auth_fifo_pos >= AUTH_FIFO_SIZE) {
  2923. auth_fifo_pos = 0;
  2924. }
  2925. auth_fifo[auth_fifo_pos].account_id = sd->account_id;
  2926. auth_fifo[auth_fifo_pos].char_id = char_dat[0].char_id;
  2927. auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1;
  2928. auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2;
  2929. auth_fifo[auth_fifo_pos].delflag = 0;
  2930. auth_fifo[auth_fifo_pos].char_pos = 0;
  2931. auth_fifo[auth_fifo_pos].sex = sd->sex;
  2932. auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time;
  2933. auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr;
  2934. //Send NEW auth packet [Kevin]
  2935. if ((map_fd = server_fd[i]) < 1 || session[map_fd] == NULL)
  2936. {
  2937. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  2938. server_fd[i] = -1;
  2939. memset(&server[i], 0, sizeof(struct mmo_map_server));
  2940. break;
  2941. }
  2942. WFIFOW(map_fd,0) = 0x2afd;
  2943. WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus);
  2944. WFIFOL(map_fd,4) = auth_fifo[auth_fifo_pos].account_id;
  2945. WFIFOL(map_fd,8) = auth_fifo[auth_fifo_pos].login_id1;
  2946. WFIFOL(map_fd,16) = auth_fifo[auth_fifo_pos].login_id2;
  2947. WFIFOL(map_fd,12) = (unsigned long)auth_fifo[auth_fifo_pos].connect_until_time;
  2948. memcpy(WFIFOP(map_fd,20), &char_dat[0], sizeof(struct mmo_charstatus));
  2949. WFIFOSET(map_fd, WFIFOW(map_fd,2));
  2950. set_char_online(i, auth_fifo[auth_fifo_pos].char_id, auth_fifo[auth_fifo_pos].account_id);
  2951. //Checks to see if the even share setting of the party must be broken.
  2952. inter_party_logged(char_dat[0].party_id, char_dat[0].account_id, char_dat[0].char_id);
  2953. auth_fifo_pos++;
  2954. break;
  2955. case 0x67: // make new
  2956. FIFOSD_CHECK(37);
  2957. if(char_new == 0) //turn character creation on/off [Kevin]
  2958. i = -2;
  2959. else
  2960. i = make_new_char_sql(fd, RFIFOP(fd, 2));
  2961. //if (i < 0) {
  2962. // WFIFOW(fd, 0) = 0x6e;
  2963. // WFIFOB(fd, 2) = 0x00;
  2964. // WFIFOSET(fd, 3);
  2965. // RFIFOSKIP(fd, 37);
  2966. // break;
  2967. //}
  2968. //Changed that we can support 'Charname already exists' (-1) amd 'Char creation denied' (-2)
  2969. //And 'You are underaged' (-3) (XD) [Sirius]
  2970. if(i == -1){
  2971. //already exists
  2972. WFIFOW(fd, 0) = 0x6e;
  2973. WFIFOB(fd, 2) = 0x00;
  2974. WFIFOSET(fd, 3);
  2975. RFIFOSKIP(fd, 37);
  2976. break;
  2977. }else if(i == -2){
  2978. //denied
  2979. WFIFOW(fd, 0) = 0x6e;
  2980. WFIFOB(fd, 2) = 0x02;
  2981. WFIFOSET(fd, 3);
  2982. RFIFOSKIP(fd, 37);
  2983. break;
  2984. }else if(i == -3){
  2985. //underaged XD
  2986. WFIFOW(fd, 0) = 0x6e;
  2987. WFIFOB(fd, 2) = 0x01;
  2988. WFIFOSET(fd, 3);
  2989. RFIFOSKIP(fd, 37);
  2990. break;
  2991. }
  2992. WFIFOW(fd, 0) = 0x6d;
  2993. memset(WFIFOP(fd, 2), 0x00, 106);
  2994. mmo_char_fromsql_short(i, char_dat); //Only the short data is needed.
  2995. //mmo_char_fromsql(i, char_dat);
  2996. i = 0;
  2997. WFIFOL(fd, 2) = char_dat[i].char_id;
  2998. WFIFOL(fd,2+4) = char_dat[i].base_exp;
  2999. WFIFOL(fd,2+8) = char_dat[i].zeny;
  3000. WFIFOL(fd,2+12) = char_dat[i].job_exp;
  3001. WFIFOL(fd,2+16) = char_dat[i].job_level;
  3002. WFIFOL(fd,2+28) = char_dat[i].karma;
  3003. WFIFOL(fd,2+32) = char_dat[i].manner;
  3004. WFIFOW(fd,2+40) = 0x30;
  3005. WFIFOW(fd,2+42) = (char_dat[i].hp > 0x7fff) ? 0x7fff : char_dat[i].hp;
  3006. WFIFOW(fd,2+44) = (char_dat[i].max_hp > 0x7fff) ? 0x7fff : char_dat[i].max_hp;
  3007. WFIFOW(fd,2+46) = (char_dat[i].sp > 0x7fff) ? 0x7fff : char_dat[i].sp;
  3008. WFIFOW(fd,2+48) = (char_dat[i].max_sp > 0x7fff) ? 0x7fff : char_dat[i].max_sp;
  3009. WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed;
  3010. WFIFOW(fd,2+52) = char_dat[i].class_;
  3011. WFIFOW(fd,2+54) = char_dat[i].hair;
  3012. WFIFOW(fd,2+58) = char_dat[i].base_level;
  3013. WFIFOW(fd,2+60) = char_dat[i].skill_point;
  3014. WFIFOW(fd,2+64) = char_dat[i].shield;
  3015. WFIFOW(fd,2+66) = char_dat[i].head_top;
  3016. WFIFOW(fd,2+68) = char_dat[i].head_mid;
  3017. WFIFOW(fd,2+70) = char_dat[i].hair_color;
  3018. memcpy(WFIFOP(fd,2+74), char_dat[i].name, NAME_LENGTH);
  3019. WFIFOB(fd,2+98) = char_dat[i].str>255?255:char_dat[i].str;
  3020. WFIFOB(fd,2+99) = char_dat[i].agi>255?255:char_dat[i].agi;
  3021. WFIFOB(fd,2+100) = char_dat[i].vit>255?255:char_dat[i].vit;
  3022. WFIFOB(fd,2+101) = char_dat[i].int_>255?255:char_dat[i].int_;
  3023. WFIFOB(fd,2+102) = char_dat[i].dex>255?255:char_dat[i].dex;
  3024. WFIFOB(fd,2+103) = char_dat[i].luk>255?255:char_dat[i].luk;
  3025. WFIFOB(fd,2+104) = char_dat[i].char_num;
  3026. WFIFOSET(fd, 108);
  3027. RFIFOSKIP(fd, 37);
  3028. //to do
  3029. for(ch = 0; ch < 9; ch++) {
  3030. if (sd->found_char[ch] == -1) {
  3031. sd->found_char[ch] = char_dat[i].char_id;
  3032. break;
  3033. }
  3034. }
  3035. break;
  3036. case 0x68: /* delete char */
  3037. FIFOSD_CHECK(46);
  3038. {
  3039. int cid = RFIFOL(fd,2);
  3040. ShowInfo(CL_RED" Request Char Deletion:"CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  3041. memcpy(email, RFIFOP(fd,6), 40);
  3042. /* Check if e-mail is correct */
  3043. if(strcmpi(email, sd->email)){
  3044. if(strcmp("a@a.com", sd->email) == 0){
  3045. if(strcmp("a@a.com", email) == 0 || strcmp("", email) == 0){
  3046. //ignore
  3047. }else{
  3048. //del fail
  3049. WFIFOW(fd, 0) = 0x70;
  3050. WFIFOB(fd, 2) = 0;
  3051. WFIFOSET(fd, 3);
  3052. RFIFOSKIP(fd, 46);
  3053. break;
  3054. }
  3055. }else{
  3056. //del fail
  3057. WFIFOW(fd, 0) = 0x70;
  3058. WFIFOB(fd, 2) = 0;
  3059. WFIFOSET(fd, 3);
  3060. RFIFOSKIP(fd, 46);
  3061. break;
  3062. }
  3063. }
  3064. for(i = 0; i < 9; i++) {
  3065. /* Debug:
  3066. printf("Checking if char to be deleted: %d - %d (%d)\n", sd->found_char[i], RFIFOL(fd, 2), sd->account_id);
  3067. */
  3068. if (sd->found_char[i] == cid) {
  3069. for(ch = i; ch < 9-1; ch++)
  3070. sd->found_char[ch] = sd->found_char[ch+1];
  3071. sd->found_char[8] = -1;
  3072. break;
  3073. }
  3074. }
  3075. /* Such a character does not exist in the account */
  3076. /* If so, you are so screwed. */
  3077. if (i == 9) {
  3078. WFIFOW(fd, 0) = 0x70;
  3079. WFIFOB(fd, 2) = 0;
  3080. WFIFOSET(fd, 3);
  3081. break;
  3082. }
  3083. /* Grab the partner id */
  3084. sprintf(tmp_sql, "SELECT `partner_id` FROM `%s` WHERE `char_id`='%d'",char_db, cid);
  3085. if (mysql_query(&mysql_handle, tmp_sql)) {
  3086. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3087. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3088. }
  3089. sql_res = mysql_store_result(&mysql_handle);
  3090. if(sql_res)
  3091. {
  3092. int char_pid=0;
  3093. sql_row = mysql_fetch_row(sql_res);
  3094. if (sql_row)
  3095. char_pid = atoi(sql_row[0]);
  3096. mysql_free_result(sql_res);
  3097. /* Delete character and partner (if any) */
  3098. delete_char_sql(cid, char_pid);
  3099. if (char_pid != 0)
  3100. { /* If there is partner, tell map server to do divorce */
  3101. WBUFW(buf,0) = 0x2b12;
  3102. WBUFL(buf,2) = RFIFOL(fd,2);
  3103. WBUFL(buf,6) = char_pid;
  3104. mapif_sendall(buf,10);
  3105. }
  3106. }
  3107. /* Char successfully deleted. <- For sure? There could had been an sql db error, what is done then?. [Skotlex] */
  3108. WFIFOW(fd, 0) = 0x6f;
  3109. WFIFOSET(fd, 2);
  3110. RFIFOSKIP(fd, 46);
  3111. break;
  3112. }
  3113. case 0x2af8: // login as map-server
  3114. if (RFIFOREST(fd) < 60)
  3115. return 0;
  3116. WFIFOW(fd, 0) = 0x2af9;
  3117. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3118. if (server_fd[i] <= 0)
  3119. break;
  3120. }
  3121. if (i == MAX_MAP_SERVERS || strcmp((const char*)RFIFOP(fd,2), userid) || strcmp((const char*)RFIFOP(fd,26), passwd)) {
  3122. WFIFOB(fd,2) = 3;
  3123. WFIFOSET(fd, 3);
  3124. } else {
  3125. // int len;
  3126. WFIFOB(fd,2) = 0;
  3127. WFIFOSET(fd, 3);
  3128. session[fd]->func_parse = parse_frommap;
  3129. server_fd[i] = fd;
  3130. server[i].ip = RFIFOL(fd, 54);
  3131. server[i].port = RFIFOW(fd, 58);
  3132. server[i].users = 0;
  3133. memset(server[i].map, 0, sizeof(server[i].map));
  3134. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3135. char_mapif_init(fd);
  3136. // send gm acccounts level to map-servers
  3137. /* removed by CLOWNISIUS due to isGM
  3138. len = 4;
  3139. WFIFOW(fd,0) = 0x2b15;
  3140. for(i = 0; i < GM_num; i++) {
  3141. WFIFOL(fd,len) = gm_account[i].account_id;
  3142. WFIFOB(fd,len+4) = (unsigned char)gm_account[i].level;
  3143. len += 5;
  3144. }
  3145. WFIFOW(fd,2) = len;
  3146. WFIFOSET(fd,len);*/
  3147. }
  3148. RFIFOSKIP(fd,60);
  3149. break;
  3150. case 0x187: // Alive?
  3151. if (RFIFOREST(fd) < 6) {
  3152. return 0;
  3153. }
  3154. RFIFOSKIP(fd, 6);
  3155. break;
  3156. case 0x7530: // Athena info get
  3157. WFIFOW(fd, 0) = 0x7531;
  3158. WFIFOB(fd, 2) = ATHENA_MAJOR_VERSION;
  3159. WFIFOB(fd, 3) = ATHENA_MINOR_VERSION;
  3160. WFIFOB(fd, 4) = ATHENA_REVISION;
  3161. WFIFOB(fd, 5) = ATHENA_RELEASE_FLAG;
  3162. WFIFOB(fd, 6) = ATHENA_OFFICIAL_FLAG;
  3163. WFIFOB(fd, 7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  3164. WFIFOW(fd, 8) = ATHENA_MOD_VERSION;
  3165. WFIFOSET(fd, 10);
  3166. RFIFOSKIP(fd, 2);
  3167. return 0;
  3168. case 0x7532: // disconnect(default also disconnect)
  3169. default:
  3170. session[fd]->eof = 1;
  3171. return 0;
  3172. }
  3173. }
  3174. RFIFOFLUSH(fd);
  3175. return 0;
  3176. }
  3177. // Console Command Parser [Wizputer]
  3178. int parse_console(char *buf) {
  3179. char *type,*command;
  3180. type = (char *)aMalloc(64);
  3181. command = (char *)aMalloc(64);
  3182. memset(type,0,64);
  3183. memset(command,0,64);
  3184. ShowNotice("Console: %s\n",buf);
  3185. if ( sscanf(buf, "%[^:]:%[^\n]", type , command ) < 2 )
  3186. sscanf(buf,"%[^\n]",type);
  3187. ShowNotice("Type of command: %s || Command: %s \n",type,command);
  3188. if(buf) aFree(buf);
  3189. if(type) aFree(type);
  3190. if(command) aFree(command);
  3191. return 0;
  3192. }
  3193. // MAP send all
  3194. int mapif_sendall(unsigned char *buf, unsigned int len) {
  3195. int i, c;
  3196. int fd;
  3197. c = 0;
  3198. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3199. if ((fd = server_fd[i]) > 0) { //0 Should not be a valid server_fd [Skotlex]
  3200. if (session[fd] == NULL)
  3201. { //Could this be the crash's source? [Skotlex]
  3202. ShowError("mapif_sendall: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", fd, i);
  3203. server_fd[i] = -1;
  3204. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3205. continue;
  3206. }
  3207. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3208. realloc_writefifo(fd, len);
  3209. memcpy(WFIFOP(fd,0), buf, len);
  3210. WFIFOSET(fd,len);
  3211. c++;
  3212. }
  3213. }
  3214. return c;
  3215. }
  3216. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len) {
  3217. int i, c;
  3218. int fd;
  3219. c = 0;
  3220. for(i=0, c=0;i<MAX_MAP_SERVERS;i++){
  3221. if ((fd = server_fd[i]) > 0 && fd != sfd) {
  3222. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3223. realloc_writefifo(fd, len);
  3224. memcpy(WFIFOP(fd,0), buf, len);
  3225. WFIFOSET(fd, len);
  3226. c++;
  3227. }
  3228. }
  3229. return c;
  3230. }
  3231. int mapif_send(int fd, unsigned char *buf, unsigned int len) {
  3232. int i;
  3233. if (fd >= 0) {
  3234. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3235. if (fd == server_fd[i]) {
  3236. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3237. realloc_writefifo(fd, len);
  3238. memcpy(WFIFOP(fd,0), buf, len);
  3239. WFIFOSET(fd,len);
  3240. return 1;
  3241. }
  3242. }
  3243. }
  3244. return 0;
  3245. }
  3246. int send_users_tologin(int tid, unsigned int tick, int id, int data) {
  3247. int users = count_users();
  3248. unsigned char buf[16];
  3249. if (login_fd > 0 && session[login_fd]) {
  3250. // send number of user to login server
  3251. WFIFOW(login_fd,0) = 0x2714;
  3252. WFIFOL(login_fd,2) = users;
  3253. WFIFOSET(login_fd,6);
  3254. }
  3255. // send number of players to all map-servers
  3256. WBUFW(buf,0) = 0x2b00;
  3257. WBUFL(buf,2) = users;
  3258. mapif_sendall(buf, 6);
  3259. return 0;
  3260. }
  3261. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap) {
  3262. struct online_char_data* character = (struct online_char_data*)data;
  3263. int *i = va_arg(ap, int*);
  3264. int count = va_arg(ap, int);
  3265. if ((*i) >= count)
  3266. return 0; //This is an error that shouldn't happen....
  3267. if(character->server > -1) {
  3268. WFIFOHEAD(login_fd, 8+count*4);
  3269. WFIFOL(login_fd, 8+(*i)*4) =character->account_id;
  3270. (*i)++;
  3271. return 1;
  3272. }
  3273. return 0;
  3274. }
  3275. int send_accounts_tologin(int tid, unsigned int tick, int id, int data) {
  3276. int users = count_users(), i=0;
  3277. if (login_fd > 0 && session[login_fd]) {
  3278. // send account list to login server
  3279. WFIFOHEAD(login_fd, 8+users*4);
  3280. WFIFOW(login_fd,0) = 0x272d;
  3281. WFIFOL(login_fd,4) = users;
  3282. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  3283. WFIFOW(login_fd,2) = 8+ i*4;
  3284. if (i > 0)
  3285. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  3286. }
  3287. return 0;
  3288. }
  3289. int check_connect_login_server(int tid, unsigned int tick, int id, int data) {
  3290. if (login_fd <= 0 || session[login_fd] == NULL) {
  3291. struct char_session_data *sd;
  3292. int i, cc;
  3293. unsigned char buf[16];
  3294. ShowInfo("Attempt to connect to login-server...\n");
  3295. login_fd = make_connection(login_ip, login_port);
  3296. if (login_fd == -1)
  3297. { //Try again later. [Skotlex]
  3298. login_fd = 0;
  3299. return 0;
  3300. }
  3301. session[login_fd]->func_parse = parse_tologin;
  3302. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3303. WFIFOW(login_fd,0) = 0x2710;
  3304. memcpy(WFIFOP(login_fd,2), userid, 24);
  3305. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3306. WFIFOL(login_fd,50) = 0;
  3307. WFIFOL(login_fd,54) = char_ip;
  3308. WFIFOL(login_fd,58) = char_port;
  3309. memset(WFIFOP(login_fd,60), 0, 20);
  3310. memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20);
  3311. WFIFOW(login_fd,80) = 0;
  3312. WFIFOW(login_fd,82) = char_maintenance;
  3313. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3314. WFIFOSET(login_fd,86);
  3315. //(re)connected to login-server,
  3316. //now wi'll look in sql which player's are ON and set them OFF
  3317. //AND send to all mapservers (if we have one / ..) to kick the players
  3318. //so the bug is fixed, if'ure using more than one charservers (worlds)
  3319. //that the player'S got reejected from server after a 'world' crash^^
  3320. //2b1f AID.L B1
  3321. sprintf(tmp_sql, "SELECT `account_id`, `online` FROM `%s` WHERE `online` = '1'", char_db);
  3322. if(mysql_query(&mysql_handle, tmp_sql)){
  3323. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3324. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3325. return -1;
  3326. }
  3327. sql_res = mysql_store_result(&mysql_handle);
  3328. if(sql_res){
  3329. cc = (int)mysql_num_rows(sql_res);
  3330. ShowStatus("Setting %d Players offline\n", cc);
  3331. while((sql_row = mysql_fetch_row(sql_res))){
  3332. //sql_row[0] == AID
  3333. //tell the loginserver
  3334. WFIFOW(login_fd, 0) = 0x272c; //set off
  3335. WFIFOL(login_fd, 2) = atoi(sql_row[0]); //AID
  3336. WFIFOSET(login_fd, 6);
  3337. //tell map to 'kick' the player (incase of 'on' ..)
  3338. WBUFW(buf, 0) = 0x2b1f;
  3339. WBUFL(buf, 2) = atoi(sql_row[0]);
  3340. WBUFB(buf, 6) = 1;
  3341. mapif_sendall(buf, 7);
  3342. //kick the player if he's on charselect
  3343. for(i = 0; i < fd_max; i++){
  3344. if(session[i] && (sd = (struct char_session_data*)session[i]->session_data)){
  3345. if(sd->account_id == atoi(sql_row[0])){
  3346. session[i]->eof = 1;
  3347. break;
  3348. }
  3349. }
  3350. }
  3351. }
  3352. mysql_free_result(sql_res);
  3353. }else{
  3354. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3355. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3356. return -1;
  3357. }
  3358. //Now Update all players to 'OFFLINE'
  3359. sprintf(tmp_sql, "UPDATE `%s` SET `online` = '0'", char_db);
  3360. if(mysql_query(&mysql_handle, tmp_sql)){
  3361. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3362. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3363. }
  3364. sprintf(tmp_sql, "UPDATE `%s` SET `online` = '0'", guild_member_db);
  3365. if(mysql_query(&mysql_handle, tmp_sql)){
  3366. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3367. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3368. }
  3369. sprintf(tmp_sql, "UPDATE `%s` SET `connect_member` = '0'", guild_db);
  3370. if(mysql_query(&mysql_handle, tmp_sql)){
  3371. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3372. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3373. }
  3374. }
  3375. return 0;
  3376. }
  3377. //------------------------------------------------
  3378. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3379. //replies/disconnect the player we tried to kick. [Skotlex]
  3380. //------------------------------------------------
  3381. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data)
  3382. {
  3383. struct online_char_data* character;
  3384. if ((character = idb_get(online_char_db, id)) != NULL && character->waiting_disconnect)
  3385. { //Mark it offline due to timeout.
  3386. set_char_offline(character->char_id, character->account_id);
  3387. }
  3388. return 0;
  3389. }
  3390. //----------------------------------------------------------
  3391. // Return numerical value of a switch configuration by [Yor]
  3392. // on/off, english, fran軋is, deutsch, espa�l
  3393. //----------------------------------------------------------
  3394. int config_switch(const char *str) {
  3395. if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0)
  3396. return 1;
  3397. if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0)
  3398. return 0;
  3399. return atoi(str);
  3400. }
  3401. // Lan Support conf reading added by Kashy
  3402. int char_lan_config_read(const char *lancfgName){
  3403. char subnetmask[128];
  3404. char line[1024], w1[1024], w2[1024];
  3405. FILE *fp;
  3406. struct hostent * h = NULL;
  3407. if ((fp = fopen(lancfgName, "r")) == NULL) {
  3408. ShowError("file not found: %s\n", lancfgName);
  3409. return 1;
  3410. }
  3411. ShowInfo("Reading file %s...\n", lancfgName);
  3412. while(fgets(line, sizeof(line)-1, fp)){
  3413. if (line[0] == '/' && line[1] == '/')
  3414. continue;
  3415. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3416. continue;
  3417. else if (strcmpi(w1, "lan_map_ip") == 0) {
  3418. h = gethostbyname(w2);
  3419. if (h != NULL) {
  3420. sprintf(lan_map_ip, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3421. } else {
  3422. strncpy(lan_map_ip, w2, sizeof(lan_map_ip));
  3423. lan_map_ip[sizeof(lan_map_ip)-1] = 0;
  3424. }
  3425. ShowStatus("set Lan_map_IP : %s\n", lan_map_ip);
  3426. }
  3427. else if (strcmpi(w1, "subnetmask") == 0) {
  3428. unsigned int k0, k1, k2, k3;
  3429. strcpy(subnetmask, w2);
  3430. sscanf(subnetmask, "%d.%d.%d.%d", &k0, &k1, &k2, &k3);
  3431. subnetmaski[0] = k0; subnetmaski[1] = k1; subnetmaski[2] = k2; subnetmaski[3] = k3;
  3432. ShowStatus("set subnetmask : %s\n", w2);
  3433. }
  3434. }
  3435. fclose(fp);
  3436. ShowInfo("Done reading %s.\n", lancfgName);
  3437. return 0;
  3438. }
  3439. void do_final(void) {
  3440. ShowInfo("Doing final stage...\n");
  3441. //mmo_char_sync();
  3442. //inter_save();
  3443. do_final_itemdb();
  3444. //check SQL save progress.
  3445. //wait until save char complete
  3446. set_all_offline();
  3447. inter_final();
  3448. flush_fifos();
  3449. mapindex_final();
  3450. sprintf(tmp_sql,"DELETE FROM `ragsrvinfo");
  3451. if (mysql_query(&mysql_handle, tmp_sql))
  3452. {
  3453. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3454. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3455. }
  3456. if(gm_account) {
  3457. aFree(gm_account);
  3458. gm_account = 0;
  3459. }
  3460. if(char_dat) {
  3461. aFree(char_dat);
  3462. char_dat = 0;
  3463. }
  3464. delete_session(login_fd);
  3465. delete_session(char_fd);
  3466. char_db_->destroy(char_db_, NULL);
  3467. online_char_db->destroy(online_char_db, NULL);
  3468. mysql_close(&mysql_handle);
  3469. mysql_close(&lmysql_handle);
  3470. ShowInfo("ok! all done...\n");
  3471. }
  3472. void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */
  3473. char line[1024], w1[1024], w2[1024];
  3474. FILE *fp;
  3475. ShowInfo("Reading file %s...\n", cfgName);
  3476. if ((fp = fopen(cfgName, "r")) == NULL) {
  3477. ShowFatalError("file not found: %s\n", cfgName);
  3478. exit(1);
  3479. }
  3480. while(fgets(line, sizeof(line)-1, fp)){
  3481. if(line[0] == '/' && line[1] == '/')
  3482. continue;
  3483. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3484. continue;
  3485. if(strcmpi(w1,"char_db")==0){
  3486. strcpy(char_db,w2);
  3487. }else if(strcmpi(w1,"scdata_db")==0){
  3488. strcpy(scdata_db,w2);
  3489. }else if(strcmpi(w1,"cart_db")==0){
  3490. strcpy(cart_db,w2);
  3491. }else if(strcmpi(w1,"inventory_db")==0){
  3492. strcpy(inventory_db,w2);
  3493. }else if(strcmpi(w1,"charlog_db")==0){
  3494. strcpy(charlog_db,w2);
  3495. }else if(strcmpi(w1,"storage_db")==0){
  3496. strcpy(storage_db,w2);
  3497. }else if(strcmpi(w1,"reg_db")==0){
  3498. strcpy(reg_db,w2);
  3499. }else if(strcmpi(w1,"skill_db")==0){
  3500. strcpy(skill_db,w2);
  3501. }else if(strcmpi(w1,"interlog_db")==0){
  3502. strcpy(interlog_db,w2);
  3503. }else if(strcmpi(w1,"memo_db")==0){
  3504. strcpy(memo_db,w2);
  3505. }else if(strcmpi(w1,"guild_db")==0){
  3506. strcpy(guild_db,w2);
  3507. }else if(strcmpi(w1,"guild_alliance_db")==0){
  3508. strcpy(guild_alliance_db,w2);
  3509. }else if(strcmpi(w1,"guild_castle_db")==0){
  3510. strcpy(guild_castle_db,w2);
  3511. }else if(strcmpi(w1,"guild_expulsion_db")==0){
  3512. strcpy(guild_expulsion_db,w2);
  3513. }else if(strcmpi(w1,"guild_member_db")==0){
  3514. strcpy(guild_member_db,w2);
  3515. }else if(strcmpi(w1,"guild_skill_db")==0){
  3516. strcpy(guild_skill_db,w2);
  3517. }else if(strcmpi(w1,"guild_position_db")==0){
  3518. strcpy(guild_position_db,w2);
  3519. }else if(strcmpi(w1,"guild_storage_db")==0){
  3520. strcpy(guild_storage_db,w2);
  3521. }else if(strcmpi(w1,"party_db")==0){
  3522. strcpy(party_db,w2);
  3523. }else if(strcmpi(w1,"pet_db")==0){
  3524. strcpy(pet_db,w2);
  3525. }else if(strcmpi(w1,"friend_db")==0){
  3526. strcpy(friend_db,w2);
  3527. }else if(strcmpi(w1,"db_path")==0){
  3528. strcpy(db_path,w2);
  3529. //Map server option to use SQL db or not
  3530. }else if(strcmpi(w1,"use_sql_db")==0){ // added for sql item_db read for char server [Valaris]
  3531. db_use_sqldbs = config_switch(w2);
  3532. ShowStatus("Using SQL dbs: %s\n",w2);
  3533. //custom columns for login database
  3534. }else if(strcmpi(w1,"login_db_level")==0){
  3535. strcpy(login_db_level,w2);
  3536. }else if(strcmpi(w1,"login_db_account_id")==0){
  3537. strcpy(login_db_account_id,w2);
  3538. }else if(strcmpi(w1,"lowest_gm_level")==0){
  3539. lowest_gm_level = atoi(w2);
  3540. ShowStatus("set lowest_gm_level : %s\n",w2);
  3541. //support the import command, just like any other config
  3542. }else if(strcmpi(w1,"import")==0){
  3543. sql_config_read(w2);
  3544. }
  3545. }
  3546. fclose(fp);
  3547. ShowInfo("done reading %s.\n", cfgName);
  3548. }
  3549. int char_config_read(const char *cfgName) {
  3550. struct hostent *h = NULL;
  3551. char line[1024], w1[1024], w2[1024];
  3552. FILE *fp;
  3553. if ((fp = fopen(cfgName, "r")) == NULL) {
  3554. ShowFatalError("Configuration file not found: %s.\n", cfgName);
  3555. exit(1);
  3556. }
  3557. ShowInfo("Reading file %s...\n", cfgName);
  3558. while(fgets(line, sizeof(line)-1, fp)) {
  3559. if (line[0] == '/' && line[1] == '/')
  3560. continue;
  3561. line[sizeof(line)-1] = '\0';
  3562. if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2)
  3563. continue;
  3564. remove_control_chars((unsigned char *) w1);
  3565. remove_control_chars((unsigned char *) w2);
  3566. if(strcmpi(w1,"timestamp_format")==0) {
  3567. strncpy(timestamp_format, w2, 20);
  3568. } else if(strcmpi(w1,"console_silent")==0){
  3569. msg_silent = 0; //To always allow the next line to show up.
  3570. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3571. msg_silent = atoi(w2);
  3572. } else if (strcmpi(w1, "userid") == 0) {
  3573. strncpy(userid, w2, 24);
  3574. } else if (strcmpi(w1, "passwd") == 0) {
  3575. strncpy(passwd, w2, 24);
  3576. } else if (strcmpi(w1, "server_name") == 0) {
  3577. memcpy(server_name, w2, sizeof(server_name));
  3578. server_name[sizeof(server_name) - 1] = '\0';
  3579. ShowStatus("%s server has been initialized\n", w2);
  3580. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3581. if (strlen(w2) >= 4) {
  3582. memcpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3583. wisp_server_name[sizeof(wisp_server_name) - 1] = '\0';
  3584. }
  3585. } else if (strcmpi(w1, "login_ip") == 0) {
  3586. login_ip_set_ = 1;
  3587. h = gethostbyname (w2);
  3588. if (h != NULL) {
  3589. ShowStatus("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3590. sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3591. } else
  3592. memcpy(login_ip_str,w2,16);
  3593. } else if (strcmpi(w1, "login_port") == 0) {
  3594. login_port=atoi(w2);
  3595. } else if (strcmpi(w1, "char_ip") == 0) {
  3596. char_ip_set_ = 1;
  3597. h = gethostbyname (w2);
  3598. if(h != NULL) {
  3599. ShowStatus("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3600. sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3601. } else
  3602. memcpy(char_ip_str, w2, 16);
  3603. } else if (strcmpi(w1, "bind_ip") == 0) {
  3604. bind_ip_set_ = 1;
  3605. h = gethostbyname (w2);
  3606. if(h != NULL) {
  3607. ShowStatus("Character server binding IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3608. sprintf(bind_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3609. } else
  3610. memcpy(bind_ip_str, w2, 16);
  3611. } else if (strcmpi(w1, "char_port") == 0) {
  3612. char_port = atoi(w2);
  3613. } else if (strcmpi(w1, "char_maintenance") == 0) {
  3614. char_maintenance = atoi(w2);
  3615. } else if (strcmpi(w1, "char_new")==0){
  3616. char_new = atoi(w2);
  3617. } else if (strcmpi(w1, "char_new_display")==0){
  3618. char_new_display = atoi(w2);
  3619. } else if (strcmpi(w1, "max_connect_user") == 0) {
  3620. max_connect_user = atoi(w2);
  3621. if (max_connect_user < 0)
  3622. max_connect_user = 0; // unlimited online players
  3623. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  3624. gm_allow_level = atoi(w2);
  3625. if(gm_allow_level < 0)
  3626. gm_allow_level = 99;
  3627. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  3628. check_ip_flag = config_switch(w2);
  3629. } else if (strcmpi(w1, "online_check") == 0) {
  3630. online_check = config_switch(w2);
  3631. } else if (strcmpi(w1, "autosave_time") == 0) {
  3632. autosave_interval = atoi(w2)*1000;
  3633. if (autosave_interval <= 0)
  3634. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  3635. } else if (strcmpi(w1, "save_log") == 0) {
  3636. save_log = config_switch(w2);
  3637. } else if (strcmpi(w1, "start_point") == 0) {
  3638. char map[MAP_NAME_LENGTH];
  3639. int x, y;
  3640. if (sscanf(w2,"%16[^,],%d,%d", map, &x, &y) < 3)
  3641. continue;
  3642. if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name
  3643. start_point.map = mapindex_name2id(map);
  3644. if (!start_point.map)
  3645. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  3646. start_point.x = x;
  3647. start_point.y = y;
  3648. }
  3649. } else if (strcmpi(w1, "start_zeny") == 0) {
  3650. start_zeny = atoi(w2);
  3651. if (start_zeny < 0)
  3652. start_zeny = 0;
  3653. } else if (strcmpi(w1, "start_weapon") == 0) {
  3654. start_weapon = atoi(w2);
  3655. if (start_weapon < 0)
  3656. start_weapon = 0;
  3657. } else if (strcmpi(w1, "start_armor") == 0) {
  3658. start_armor = atoi(w2);
  3659. if (start_armor < 0)
  3660. start_armor = 0;
  3661. } else if(strcmpi(w1,"log_char")==0){ //log char or not [devil]
  3662. log_char = atoi(w2);
  3663. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  3664. strcpy(unknown_char_name, w2);
  3665. unknown_char_name[NAME_LENGTH-1] = 0;
  3666. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  3667. name_ignoring_case = config_switch(w2);
  3668. } else if (strcmpi(w1, "char_name_option") == 0) {
  3669. char_name_option = atoi(w2);
  3670. } else if (strcmpi(w1, "char_name_letters") == 0) {
  3671. strcpy(char_name_letters, w2);
  3672. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  3673. check_ip_flag = config_switch(w2);
  3674. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  3675. char_per_account = atoi(w2);
  3676. } else if (strcmpi(w1, "console") == 0) {
  3677. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 )
  3678. console = 1;
  3679. } else if (strcmpi(w1, "import") == 0) {
  3680. char_config_read(w2);
  3681. }
  3682. }
  3683. fclose(fp);
  3684. ShowInfo("Done reading %s.\n", cfgName);
  3685. return 0;
  3686. }
  3687. void set_server_type(void)
  3688. {
  3689. SERVER_TYPE = ATHENA_SERVER_CHAR;
  3690. }
  3691. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3692. {
  3693. struct online_char_data *character= (struct online_char_data*)data;
  3694. if (character->server == -2) //Unknown server.. set them offline
  3695. set_char_offline(character->char_id, character->account_id);
  3696. if (character->server < 0)
  3697. //Free data from players that have not been online for a while.
  3698. db_remove(online_char_db, key);
  3699. return 0;
  3700. }
  3701. static int online_data_cleanup(int tid, unsigned int tick, int id, int data)
  3702. {
  3703. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3704. return 0;
  3705. }
  3706. int do_init(int argc, char **argv){
  3707. int i;
  3708. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3709. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3710. server_fd[i] = -1;
  3711. }
  3712. //Read map indexes
  3713. mapindex_init();
  3714. start_point.map = mapindex_name2id("new_1-1.gat");
  3715. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  3716. char_lan_config_read((argc > 1) ? argv[1] : LAN_CONF_NAME);
  3717. sql_config_read(SQL_CONF_NAME);
  3718. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  3719. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  3720. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  3721. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  3722. }
  3723. ShowInfo("Finished reading the char-server configuration.\n");
  3724. inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server テハア篳ュ
  3725. ShowInfo("Finished reading the inter-server configuration.\n");
  3726. //Read ItemDB
  3727. do_init_itemdb();
  3728. ShowInfo("Initializing char server.\n");
  3729. online_char_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3730. mmo_char_sql_init();
  3731. ShowInfo("char server initialized.\n");
  3732. // ShowDebug("set parser -> parse_char()...\n");
  3733. set_defaultparse(parse_char);
  3734. // ShowDebug("set terminate function -> do_final().....\n");
  3735. if ((naddr_ != 0) && (login_ip_set_ == 0 || char_ip_set_ == 0)) {
  3736. // The char server should know what IP address it is running on
  3737. // - MouseJstr
  3738. int localaddr = ntohl(addr_[0]);
  3739. unsigned char *ptr = (unsigned char *) &localaddr;
  3740. char buf[16];
  3741. sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);
  3742. if (naddr_ != 1)
  3743. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", buf);
  3744. else
  3745. ShowStatus("Defaulting to %s as our IP address\n", buf);
  3746. if (login_ip_set_ == 0)
  3747. strcpy(login_ip_str, buf);
  3748. if (char_ip_set_ == 0)
  3749. strcpy(char_ip_str, buf);
  3750. if (ptr[0] == 192 && ptr[1] == 168)
  3751. ShowWarning("Firewall detected.. edit lan_support.conf and char_athena.conf\n");
  3752. }
  3753. login_ip = inet_addr(login_ip_str);
  3754. char_ip = inet_addr(char_ip_str);
  3755. ShowInfo("open port %d.....\n",char_port);
  3756. //char_fd = make_listen_port(char_port);
  3757. if (bind_ip_set_)
  3758. char_fd = make_listen_bind(inet_addr(bind_ip_str),char_port);
  3759. else
  3760. char_fd = make_listen_bind(INADDR_ANY,char_port);
  3761. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  3762. add_timer_func_list(send_users_tologin, "send_users_tologin");
  3763. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  3764. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  3765. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  3766. add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600 * 1000);
  3767. // send ALIVE PING to login server.
  3768. add_timer_interval(gettick() + 10, check_connect_login_server, 0, 0, 10 * 1000);
  3769. // send USER COUNT PING to login server.
  3770. add_timer_interval(gettick() + 10, send_users_tologin, 0, 0, 5 * 1000);
  3771. add_timer_interval(gettick() + 3600*1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour.
  3772. if ( console ) {
  3773. set_defaultconsoleparse(parse_console);
  3774. start_console();
  3775. }
  3776. //Cleaning the tables for NULL entrys @ startup [Sirius]
  3777. //Chardb clean
  3778. ShowInfo("Cleaning the '%s' table...\n", char_db);
  3779. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `account_id` = '0'", char_db);
  3780. if(mysql_query(&mysql_handle, tmp_sql)){
  3781. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3782. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3783. }
  3784. //guilddb clean
  3785. ShowInfo("Cleaning the '%s' table...\n", guild_db);
  3786. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db);
  3787. if(mysql_query(&mysql_handle, tmp_sql)){
  3788. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3789. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3790. }
  3791. //guildmemberdb clean
  3792. ShowInfo("Cleaning the '%s' table...\n", guild_member_db);
  3793. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db);
  3794. if(mysql_query(&mysql_handle, tmp_sql)){
  3795. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3796. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3797. }
  3798. ShowInfo("End of char server initilization function.\n");
  3799. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  3800. return 0;
  3801. }
  3802. #undef mysql_query
  3803. int debug_mysql_query(char *file, int line, void *mysql, const char *q) {
  3804. #ifdef TWILIGHT
  3805. ShowDebug("sql: %s:%d# %s\n", file, line, q);
  3806. #endif
  3807. return mysql_query((MYSQL *) mysql, q);
  3808. }
  3809. int char_child(int parent_id, int child_id) {
  3810. int tmp_id = 0;
  3811. sprintf (tmp_sql, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id);
  3812. if (mysql_query (&mysql_handle, tmp_sql)) {
  3813. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3814. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3815. }
  3816. sql_res = mysql_store_result (&mysql_handle);
  3817. sql_row = sql_res?mysql_fetch_row (sql_res):NULL;
  3818. if (sql_row)
  3819. tmp_id = atoi (sql_row[0]);
  3820. else
  3821. ShowError("CHAR: child Failed!\n");
  3822. if (sql_res) mysql_free_result (sql_res);
  3823. if ( tmp_id == child_id )
  3824. return 1;
  3825. else
  3826. return 0;
  3827. }
  3828. int char_married(int pl1,int pl2) {
  3829. int tmp_id = 0;
  3830. sprintf (tmp_sql, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1);
  3831. if (mysql_query (&mysql_handle, tmp_sql)) {
  3832. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3833. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3834. }
  3835. sql_res = mysql_store_result (&mysql_handle);
  3836. sql_row = sql_res?mysql_fetch_row (sql_res):NULL;
  3837. if (sql_row)
  3838. tmp_id = atoi (sql_row[0]);
  3839. else
  3840. ShowError("CHAR: married Failed!\n");
  3841. if (sql_res) mysql_free_result (sql_res);
  3842. if ( tmp_id == pl2 )
  3843. return 1;
  3844. else
  3845. return 0;
  3846. }
  3847. int char_nick2id (char *name) {
  3848. int char_id = 0;
  3849. sprintf (tmp_sql, "SELECT `char_id` FROM `%s` WHERE `name` = '%s'", char_db, name);
  3850. if (mysql_query (&mysql_handle, tmp_sql)) {
  3851. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3852. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3853. }
  3854. sql_res = mysql_store_result (&mysql_handle);
  3855. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  3856. if (sql_row)
  3857. char_id = atoi (sql_row[0]);
  3858. else
  3859. ShowError ("CHAR: nick2id Failed!\n");
  3860. if (sql_res) mysql_free_result (sql_res);
  3861. return char_id;
  3862. }