char.c 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/core.h"
  5. #include "../common/db.h"
  6. #include "../common/malloc.h"
  7. #include "../common/mapindex.h"
  8. #include "../common/mmo.h"
  9. #include "../common/showmsg.h"
  10. #include "../common/socket.h"
  11. #include "../common/strlib.h"
  12. #include "../common/timer.h"
  13. #include "../common/utils.h"
  14. #include "../common/cli.h"
  15. #include "../common/random.h"
  16. #include "../common/ers.h"
  17. #include "int_guild.h"
  18. #include "int_homun.h"
  19. #include "int_mercenary.h"
  20. #include "int_elemental.h"
  21. #include "int_party.h"
  22. #include "int_storage.h"
  23. #include "char.h"
  24. #include "inter.h"
  25. #include <sys/types.h>
  26. #include <time.h>
  27. #include <signal.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <malloc.h>
  33. #define MAX_STARTITEM 32
  34. #define CHAR_MAX_MSG 300
  35. static char* msg_table[CHAR_MAX_MSG]; // Login Server messages_conf
  36. char char_db[256] = "char";
  37. char scdata_db[256] = "sc_data";
  38. char cart_db[256] = "cart_inventory";
  39. char inventory_db[256] = "inventory";
  40. char charlog_db[256] = "charlog";
  41. char storage_db[256] = "storage";
  42. char interlog_db[256] = "interlog";
  43. char reg_db[256] = "global_reg_value";
  44. char skill_db[256] = "skill";
  45. char memo_db[256] = "memo";
  46. char guild_db[256] = "guild";
  47. char guild_alliance_db[256] = "guild_alliance";
  48. char guild_castle_db[256] = "guild_castle";
  49. char guild_expulsion_db[256] = "guild_expulsion";
  50. char guild_member_db[256] = "guild_member";
  51. char guild_position_db[256] = "guild_position";
  52. char guild_skill_db[256] = "guild_skill";
  53. char guild_storage_db[256] = "guild_storage";
  54. char party_db[256] = "party";
  55. char pet_db[256] = "pet";
  56. char mail_db[256] = "mail"; // MAIL SYSTEM
  57. char auction_db[256] = "auction"; // Auctions System
  58. char friend_db[256] = "friends";
  59. char hotkey_db[256] = "hotkey";
  60. char quest_db[256] = "quest";
  61. char homunculus_db[256] = "homunculus";
  62. char skill_homunculus_db[256] = "skill_homunculus";
  63. char mercenary_db[256] = "mercenary";
  64. char mercenary_owner_db[256] = "mercenary_owner";
  65. char elemental_db[256] = "elemental";
  66. char ragsrvinfo_db[256] = "ragsrvinfo";
  67. // show loading/saving messages
  68. int save_log = 1;
  69. static DBMap* char_db_; // int char_id -> struct mmo_charstatus*
  70. char db_path[1024] = "db";
  71. int db_use_sqldbs;
  72. struct mmo_map_server {
  73. int fd;
  74. uint32 ip;
  75. uint16 port;
  76. int users;
  77. unsigned short map[MAX_MAP_PER_SERVER];
  78. } server[MAX_MAP_SERVERS];
  79. int login_fd=-1, char_fd=-1;
  80. char userid[24];
  81. char passwd[24];
  82. char server_name[20];
  83. char wisp_server_name[NAME_LENGTH] = "Server";
  84. char login_ip_str[128];
  85. uint32 login_ip = 0;
  86. uint16 login_port = 6900;
  87. char char_ip_str[128];
  88. uint32 char_ip = 0;
  89. char bind_ip_str[128];
  90. uint32 bind_ip = INADDR_ANY;
  91. uint16 char_port = 6121;
  92. int char_maintenance = 0;
  93. bool char_new = true;
  94. int char_new_display = 0;
  95. bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  96. 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]
  97. char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
  98. #define TRIM_CHARS "\255\xA0\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  99. char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor]
  100. int char_per_account = 0; //Maximum chars per account (default unlimited) [Sirius]
  101. int char_del_level = 0; //From which level u can delete character [Lupus]
  102. int char_del_delay = 86400;
  103. int log_char = 1; // loggin char or not [devil]
  104. int log_inter = 1; // loggin inter or not [devil]
  105. // Advanced subnet check [LuzZza]
  106. struct s_subnet {
  107. uint32 mask;
  108. uint32 char_ip;
  109. uint32 map_ip;
  110. } subnet[16];
  111. int subnet_count = 0;
  112. struct char_session_data {
  113. bool auth; // whether the session is authed or not
  114. int account_id, login_id1, login_id2, sex;
  115. int found_char[MAX_CHARS]; // ids of chars on this account
  116. char email[40]; // e-mail (default: a@a.com) by [Yor]
  117. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  118. int group_id; // permission
  119. uint8 char_slots;
  120. uint32 version;
  121. uint8 clienttype;
  122. char new_name[NAME_LENGTH];
  123. char birthdate[10+1]; // YYYY-MM-DD
  124. // Pincode system
  125. char pincode[PINCODE_LENGTH+1];
  126. uint32 pincode_seed;
  127. time_t pincode_change;
  128. uint16 pincode_try;
  129. // Addon system
  130. unsigned int char_moves[MAX_CHARS]; // character moves left
  131. };
  132. struct startitem {
  133. int nameid; //Item ID
  134. int amount; //Number of items
  135. int pos; //Position (for auto-equip)
  136. } start_items[MAX_STARTITEM+1];
  137. int max_connect_user = -1;
  138. int gm_allow_group = -1;
  139. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  140. int start_zeny = 0;
  141. int guild_exp_rate = 100;
  142. // Pincode system
  143. #define PINCODE_OK 0
  144. #define PINCODE_ASK 1
  145. #define PINCODE_NOTSET 2
  146. #define PINCODE_EXPIRED 3
  147. #define PINCODE_NEW 4
  148. #define PINCODE_PASSED 7
  149. #define PINCODE_WRONG 8
  150. bool pincode_enabled = true;
  151. int pincode_changetime = 0;
  152. int pincode_maxtry = 3;
  153. bool pincode_force = true;
  154. void pincode_check( int fd, struct char_session_data* sd );
  155. void pincode_change( int fd, struct char_session_data* sd );
  156. void pincode_setnew( int fd, struct char_session_data* sd );
  157. void pincode_sendstate( int fd, struct char_session_data* sd, uint16 state );
  158. void pincode_notifyLoginPinUpdate( int account_id, char* pin );
  159. void pincode_notifyLoginPinError( int account_id );
  160. void pincode_decrypt( uint32 userSeed, char* pin );
  161. int pincode_compare( int fd, struct char_session_data* sd, char* pin );
  162. // Addon system
  163. bool char_move_enabled = true;
  164. bool char_movetoused = true;
  165. bool char_moves_unlimited = false;
  166. void moveCharSlot( int fd, struct char_session_data* sd, unsigned short from, unsigned short to );
  167. void moveCharSlotReply( int fd, struct char_session_data* sd, unsigned short index, short reason );
  168. //Custom limits for the fame lists. [Skotlex]
  169. int fame_list_size_chemist = MAX_FAME_LIST;
  170. int fame_list_size_smith = MAX_FAME_LIST;
  171. int fame_list_size_taekwon = MAX_FAME_LIST;
  172. // Char-server-side stored fame lists [DracoRPG]
  173. struct fame_list smith_fame_list[MAX_FAME_LIST];
  174. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  175. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  176. // check for exit signal
  177. // 0 is saving complete
  178. // other is char_id
  179. unsigned int save_flag = 0;
  180. // Initial position (it's possible to set it in conf file)
  181. struct point start_point = { 0, 53, 111 };
  182. int console = 0;
  183. //-----------------------------------------------------
  184. // Auth database
  185. //-----------------------------------------------------
  186. #define AUTH_TIMEOUT 30000
  187. struct auth_node {
  188. int account_id;
  189. int char_id;
  190. uint32 login_id1;
  191. uint32 login_id2;
  192. uint32 ip;
  193. int sex;
  194. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  195. int group_id;
  196. unsigned changing_mapservers : 1;
  197. };
  198. static DBMap* auth_db; // int account_id -> struct auth_node*
  199. //-----------------------------------------------------
  200. // Online User Database
  201. //-----------------------------------------------------
  202. struct online_char_data {
  203. int account_id;
  204. int char_id;
  205. int fd;
  206. int waiting_disconnect;
  207. short server; // -2: unknown server, -1: not connected, 0+: id of server
  208. bool pincode_success;
  209. };
  210. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  211. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data);
  212. int delete_char_sql(int char_id);
  213. /**
  214. * @see DBCreateData
  215. */
  216. static DBData create_online_char_data(DBKey key, va_list args)
  217. {
  218. struct online_char_data* character;
  219. CREATE(character, struct online_char_data, 1);
  220. character->account_id = key.i;
  221. character->char_id = -1;
  222. character->server = -1;
  223. character->fd = -1;
  224. character->waiting_disconnect = INVALID_TIMER;
  225. return db_ptr2data(character);
  226. }
  227. void set_char_charselect(int account_id)
  228. {
  229. struct online_char_data* character;
  230. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  231. if( character->server > -1 )
  232. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  233. server[character->server].users--;
  234. character->char_id = -1;
  235. character->server = -1;
  236. if(character->waiting_disconnect != INVALID_TIMER) {
  237. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  238. character->waiting_disconnect = INVALID_TIMER;
  239. }
  240. if (login_fd > 0 && !session[login_fd]->flag.eof)
  241. {
  242. WFIFOHEAD(login_fd,6);
  243. WFIFOW(login_fd,0) = 0x272b;
  244. WFIFOL(login_fd,2) = account_id;
  245. WFIFOSET(login_fd,6);
  246. }
  247. }
  248. void set_char_online(int map_id, int char_id, int account_id)
  249. {
  250. struct online_char_data* character;
  251. struct mmo_charstatus *cp;
  252. //Update DB
  253. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
  254. Sql_ShowDebug(sql_handle);
  255. //Check to see for online conflicts
  256. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  257. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  258. {
  259. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  260. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  261. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  262. }
  263. //Update state data
  264. character->char_id = char_id;
  265. character->server = map_id;
  266. if( character->server > -1 )
  267. server[character->server].users++;
  268. //Get rid of disconnect timer
  269. if(character->waiting_disconnect != INVALID_TIMER) {
  270. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  271. character->waiting_disconnect = INVALID_TIMER;
  272. }
  273. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  274. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  275. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  276. //Notify login server
  277. if (login_fd > 0 && !session[login_fd]->flag.eof)
  278. {
  279. WFIFOHEAD(login_fd,6);
  280. WFIFOW(login_fd,0) = 0x272b;
  281. WFIFOL(login_fd,2) = account_id;
  282. WFIFOSET(login_fd,6);
  283. }
  284. }
  285. void set_char_offline(int char_id, int account_id)
  286. {
  287. struct online_char_data* character;
  288. if ( char_id == -1 )
  289. {
  290. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) )
  291. Sql_ShowDebug(sql_handle);
  292. }
  293. else
  294. {
  295. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  296. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  297. if (cp)
  298. idb_remove(char_db_,char_id);
  299. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d' LIMIT 1", char_db, char_id) )
  300. Sql_ShowDebug(sql_handle);
  301. }
  302. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  303. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  304. if( character->server > -1 )
  305. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  306. server[character->server].users--;
  307. if(character->waiting_disconnect != INVALID_TIMER){
  308. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  309. character->waiting_disconnect = INVALID_TIMER;
  310. }
  311. if(character->char_id == char_id)
  312. {
  313. character->char_id = -1;
  314. character->server = -1;
  315. // needed if player disconnects completely since Skotlex did not want to free the session
  316. character->pincode_success = false;
  317. }
  318. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  319. }
  320. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  321. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
  322. {
  323. WFIFOHEAD(login_fd,6);
  324. WFIFOW(login_fd,0) = 0x272c;
  325. WFIFOL(login_fd,2) = account_id;
  326. WFIFOSET(login_fd,6);
  327. }
  328. }
  329. /**
  330. * @see DBApply
  331. */
  332. static int char_db_setoffline(DBKey key, DBData *data, va_list ap)
  333. {
  334. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  335. int server = va_arg(ap, int);
  336. if (server == -1) {
  337. character->char_id = -1;
  338. character->server = -1;
  339. if(character->waiting_disconnect != INVALID_TIMER){
  340. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  341. character->waiting_disconnect = INVALID_TIMER;
  342. }
  343. } else if (character->server == server)
  344. character->server = -2; //In some map server that we aren't connected to.
  345. return 0;
  346. }
  347. /**
  348. * @see DBApply
  349. */
  350. static int char_db_kickoffline(DBKey key, DBData *data, va_list ap)
  351. {
  352. struct online_char_data* character = (struct online_char_data*)db_data2ptr(data);
  353. int server_id = va_arg(ap, int);
  354. if (server_id > -1 && character->server != server_id)
  355. return 0;
  356. //Kick out any connected characters, and set them offline as appropriate.
  357. if (character->server > -1)
  358. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  359. else if (character->waiting_disconnect == INVALID_TIMER)
  360. set_char_offline(character->char_id, character->account_id);
  361. else
  362. return 0; // fail
  363. return 1;
  364. }
  365. void set_all_offline(int id)
  366. {
  367. if (id < 0)
  368. ShowNotice("Sending all users offline.\n");
  369. else
  370. ShowNotice("Sending users of map-server %d offline.\n",id);
  371. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  372. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  373. return;
  374. //Tell login-server to also mark all our characters as offline.
  375. WFIFOHEAD(login_fd,2);
  376. WFIFOW(login_fd,0) = 0x2737;
  377. WFIFOSET(login_fd,2);
  378. }
  379. void set_all_offline_sql(void)
  380. {
  381. //Set all players to 'OFFLINE'
  382. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) )
  383. Sql_ShowDebug(sql_handle);
  384. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) )
  385. Sql_ShowDebug(sql_handle);
  386. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) )
  387. Sql_ShowDebug(sql_handle);
  388. }
  389. /**
  390. * @see DBCreateData
  391. */
  392. static DBData create_charstatus(DBKey key, va_list args)
  393. {
  394. struct mmo_charstatus *cp;
  395. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  396. cp->char_id = key.i;
  397. return db_ptr2data(cp);
  398. }
  399. int inventory_to_sql(const struct item items[], int max, int id);
  400. int mmo_char_tosql(int char_id, struct mmo_charstatus* p)
  401. {
  402. int i = 0;
  403. int count = 0;
  404. int diff = 0;
  405. char save_status[128]; //For displaying save information. [Skotlex]
  406. struct mmo_charstatus *cp;
  407. int errors = 0; //If there are any errors while saving, "cp" will not be updated at the end.
  408. StringBuf buf;
  409. if (char_id!=p->char_id) return 0;
  410. cp = idb_ensure(char_db_, char_id, create_charstatus);
  411. StringBuf_Init(&buf);
  412. memset(save_status, 0, sizeof(save_status));
  413. //map inventory data
  414. if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) ) {
  415. if (!inventory_to_sql(p->inventory, MAX_INVENTORY, p->char_id))
  416. strcat(save_status, " inventory");
  417. else
  418. errors++;
  419. }
  420. //map cart data
  421. if( memcmp(p->cart, cp->cart, sizeof(p->cart)) ) {
  422. if (!memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART))
  423. strcat(save_status, " cart");
  424. else
  425. errors++;
  426. }
  427. //map storage data
  428. if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) ) {
  429. if (!memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE))
  430. strcat(save_status, " storage");
  431. else
  432. errors++;
  433. }
  434. if (
  435. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  436. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  437. (p->zeny != cp->zeny) ||
  438. (p->last_point.map != cp->last_point.map) ||
  439. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  440. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  441. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  442. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  443. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  444. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  445. (p->option != cp->option) ||
  446. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  447. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  448. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  449. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom) || (p->delete_date != cp->delete_date) ||
  450. (p->rename != cp->rename) || (p->robe != cp->robe) || (p->character_moves != cp->character_moves)
  451. )
  452. { //Save status
  453. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  454. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  455. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  456. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  457. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  458. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  459. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  460. "`delete_date`='%lu',`robe`='%d',`moves`='%d'"
  461. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  462. char_db, p->base_level, p->job_level,
  463. p->base_exp, p->job_exp, p->zeny,
  464. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  465. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  466. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  467. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  468. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  469. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  470. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  471. p->robe,p->character_moves,
  472. p->account_id, p->char_id) )
  473. {
  474. Sql_ShowDebug(sql_handle);
  475. errors++;
  476. } else
  477. strcat(save_status, " status");
  478. }
  479. //Values that will seldom change (to speed up saving)
  480. if (
  481. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  482. (p->class_ != cp->class_) ||
  483. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  484. (p->mother != cp->mother) || (p->child != cp->child) ||
  485. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  486. (p->fame != cp->fame)
  487. )
  488. {
  489. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  490. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  491. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  492. "`karma`='%d',`manner`='%d', `fame`='%d'"
  493. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  494. char_db, p->class_,
  495. p->hair, p->hair_color, p->clothes_color,
  496. p->partner_id, p->father, p->mother, p->child,
  497. p->karma, p->manner, p->fame,
  498. p->account_id, p->char_id) )
  499. {
  500. Sql_ShowDebug(sql_handle);
  501. errors++;
  502. } else
  503. strcat(save_status, " status2");
  504. }
  505. /* Mercenary Owner */
  506. if( (p->mer_id != cp->mer_id) ||
  507. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  508. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  509. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  510. {
  511. if (mercenary_owner_tosql(char_id, p))
  512. strcat(save_status, " mercenary");
  513. else
  514. errors++;
  515. }
  516. //memo points
  517. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  518. {
  519. char esc_mapname[NAME_LENGTH*2+1];
  520. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  521. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) )
  522. {
  523. Sql_ShowDebug(sql_handle);
  524. errors++;
  525. }
  526. //insert here.
  527. StringBuf_Clear(&buf);
  528. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  529. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  530. {
  531. if( p->memo_point[i].map )
  532. {
  533. if( count )
  534. StringBuf_AppendStr(&buf, ",");
  535. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  536. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  537. ++count;
  538. }
  539. }
  540. if( count )
  541. {
  542. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  543. {
  544. Sql_ShowDebug(sql_handle);
  545. errors++;
  546. }
  547. }
  548. strcat(save_status, " memo");
  549. }
  550. //FIXME: is this neccessary? [ultramage]
  551. for(i=0;i<MAX_SKILL;i++)
  552. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  553. p->skill[i].id = i; // Fix skill tree
  554. //skills
  555. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  556. {
  557. //`skill` (`char_id`, `id`, `lv`)
  558. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) )
  559. {
  560. Sql_ShowDebug(sql_handle);
  561. errors++;
  562. }
  563. StringBuf_Clear(&buf);
  564. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`,`flag`) VALUES ", skill_db);
  565. //insert here.
  566. for( i = 0, count = 0; i < MAX_SKILL; ++i ) {
  567. if( p->skill[i].id != 0 && p->skill[i].flag != SKILL_FLAG_TEMPORARY ) {
  568. if( p->skill[i].lv == 0 && ( p->skill[i].flag == SKILL_FLAG_PERM_GRANTED || p->skill[i].flag == SKILL_FLAG_PERMANENT ) )
  569. continue;
  570. if( p->skill[i].flag != SKILL_FLAG_PERMANENT && p->skill[i].flag != SKILL_FLAG_PERM_GRANTED && (p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0) == 0 )
  571. continue;
  572. if( count )
  573. StringBuf_AppendStr(&buf, ",");
  574. StringBuf_Printf(&buf, "('%d','%d','%d','%d')", char_id, p->skill[i].id,
  575. ( (p->skill[i].flag == SKILL_FLAG_PERMANENT || p->skill[i].flag == SKILL_FLAG_PERM_GRANTED) ? p->skill[i].lv : p->skill[i].flag - SKILL_FLAG_REPLACED_LV_0),
  576. p->skill[i].flag == SKILL_FLAG_PERM_GRANTED ? p->skill[i].flag : 0);/* other flags do not need to be saved */
  577. ++count;
  578. }
  579. }
  580. if( count )
  581. {
  582. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  583. {
  584. Sql_ShowDebug(sql_handle);
  585. errors++;
  586. }
  587. }
  588. strcat(save_status, " skills");
  589. }
  590. diff = 0;
  591. for(i = 0; i < MAX_FRIENDS; i++){
  592. if(p->friends[i].char_id != cp->friends[i].char_id ||
  593. p->friends[i].account_id != cp->friends[i].account_id){
  594. diff = 1;
  595. break;
  596. }
  597. }
  598. if(diff == 1)
  599. { //Save friends
  600. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) )
  601. {
  602. Sql_ShowDebug(sql_handle);
  603. errors++;
  604. }
  605. StringBuf_Clear(&buf);
  606. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  607. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  608. {
  609. if( p->friends[i].char_id > 0 )
  610. {
  611. if( count )
  612. StringBuf_AppendStr(&buf, ",");
  613. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  614. count++;
  615. }
  616. }
  617. if( count )
  618. {
  619. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  620. {
  621. Sql_ShowDebug(sql_handle);
  622. errors++;
  623. }
  624. }
  625. strcat(save_status, " friends");
  626. }
  627. #ifdef HOTKEY_SAVING
  628. // hotkeys
  629. StringBuf_Clear(&buf);
  630. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db);
  631. diff = 0;
  632. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  633. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  634. {
  635. if( diff )
  636. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  637. StringBuf_Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
  638. diff = 1;
  639. }
  640. }
  641. if(diff) {
  642. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  643. {
  644. Sql_ShowDebug(sql_handle);
  645. errors++;
  646. } else
  647. strcat(save_status, " hotkeys");
  648. }
  649. #endif
  650. StringBuf_Destroy(&buf);
  651. if (save_status[0]!='\0' && save_log)
  652. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  653. if (!errors)
  654. memcpy(cp, p, sizeof(struct mmo_charstatus));
  655. return 0;
  656. }
  657. /// Saves an array of 'item' entries into the specified table.
  658. int memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch)
  659. {
  660. StringBuf buf;
  661. SqlStmt* stmt;
  662. int i;
  663. int j;
  664. const char* tablename;
  665. const char* selectoption;
  666. struct item item; // temp storage variable
  667. bool* flag; // bit array for inventory matching
  668. bool found;
  669. int errors = 0;
  670. switch (tableswitch) {
  671. case TABLE_INVENTORY: tablename = inventory_db; selectoption = "char_id"; break;
  672. case TABLE_CART: tablename = cart_db; selectoption = "char_id"; break;
  673. case TABLE_STORAGE: tablename = storage_db; selectoption = "account_id"; break;
  674. case TABLE_GUILD_STORAGE: tablename = guild_storage_db; selectoption = "guild_id"; break;
  675. default:
  676. ShowError("Invalid table name!\n");
  677. return 1;
  678. }
  679. // The following code compares inventory with current database values
  680. // and performs modification/deletion/insertion only on relevant rows.
  681. // This approach is more complicated than a trivial delete&insert, but
  682. // it significantly reduces cpu load on the database server.
  683. StringBuf_Init(&buf);
  684. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`");
  685. for( j = 0; j < MAX_SLOTS; ++j )
  686. StringBuf_Printf(&buf, ", `card%d`", j);
  687. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  688. stmt = SqlStmt_Malloc(sql_handle);
  689. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  690. || SQL_ERROR == SqlStmt_Execute(stmt) )
  691. {
  692. SqlStmt_ShowDebug(stmt);
  693. SqlStmt_Free(stmt);
  694. StringBuf_Destroy(&buf);
  695. return 1;
  696. }
  697. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  698. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  699. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  700. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  701. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  702. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  703. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  704. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  705. SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &item.bound, 0, NULL, NULL);
  706. for( j = 0; j < MAX_SLOTS; ++j )
  707. SqlStmt_BindColumn(stmt, 9+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  708. // bit array indicating which inventory items have already been matched
  709. flag = (bool*) aCalloc(max, sizeof(bool));
  710. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  711. {
  712. found = false;
  713. // search for the presence of the item in the char's inventory
  714. for( i = 0; i < max; ++i )
  715. {
  716. // skip empty and already matched entries
  717. if( items[i].nameid == 0 || flag[i] )
  718. continue;
  719. if( items[i].nameid == item.nameid
  720. && items[i].card[0] == item.card[0]
  721. && items[i].card[2] == item.card[2]
  722. && items[i].card[3] == item.card[3]
  723. ) { //They are the same item.
  724. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  725. if( j == MAX_SLOTS &&
  726. items[i].amount == item.amount &&
  727. items[i].equip == item.equip &&
  728. items[i].identify == item.identify &&
  729. items[i].refine == item.refine &&
  730. items[i].attribute == item.attribute &&
  731. items[i].expire_time == item.expire_time &&
  732. items[i].bound == item.bound )
  733. ; //Do nothing.
  734. else
  735. {
  736. // update all fields.
  737. StringBuf_Clear(&buf);
  738. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `bound`='%d'",
  739. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound);
  740. for( j = 0; j < MAX_SLOTS; ++j )
  741. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  742. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  743. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  744. {
  745. Sql_ShowDebug(sql_handle);
  746. errors++;
  747. }
  748. }
  749. found = flag[i] = true; //Item dealt with,
  750. break; //skip to next item in the db.
  751. }
  752. }
  753. if( !found )
  754. {// Item not present in inventory, remove it.
  755. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", tablename, item.id) )
  756. {
  757. Sql_ShowDebug(sql_handle);
  758. errors++;
  759. }
  760. }
  761. }
  762. SqlStmt_Free(stmt);
  763. StringBuf_Clear(&buf);
  764. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`", tablename, selectoption);
  765. for( j = 0; j < MAX_SLOTS; ++j )
  766. StringBuf_Printf(&buf, ", `card%d`", j);
  767. StringBuf_AppendStr(&buf, ") VALUES ");
  768. found = false;
  769. // insert non-matched items into the db as new items
  770. for( i = 0; i < max; ++i )
  771. {
  772. // skip empty and already matched entries
  773. if( items[i].nameid == 0 || flag[i] )
  774. continue;
  775. if( found )
  776. StringBuf_AppendStr(&buf, ",");
  777. else
  778. found = true;
  779. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%"PRIu64"'",
  780. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].bound, items[i].unique_id);
  781. for( j = 0; j < MAX_SLOTS; ++j )
  782. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  783. StringBuf_AppendStr(&buf, ")");
  784. updateLastUid(items[i].unique_id); // Unique Non Stackable Item ID
  785. }
  786. dbUpdateUid(sql_handle); // Unique Non Stackable Item ID
  787. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  788. {
  789. Sql_ShowDebug(sql_handle);
  790. errors++;
  791. }
  792. StringBuf_Destroy(&buf);
  793. aFree(flag);
  794. return errors;
  795. }
  796. /* pretty much a copy of memitemdata_to_sql except it handles inventory_db exclusively,
  797. * - this is required because inventory db is the only one with the 'favorite' column. */
  798. int inventory_to_sql(const struct item items[], int max, int id) {
  799. StringBuf buf;
  800. SqlStmt* stmt;
  801. int i;
  802. int j;
  803. struct item item; // temp storage variable
  804. bool* flag; // bit array for inventory matching
  805. bool found;
  806. int errors = 0;
  807. // The following code compares inventory with current database values
  808. // and performs modification/deletion/insertion only on relevant rows.
  809. // This approach is more complicated than a trivial delete&insert, but
  810. // it significantly reduces cpu load on the database server.
  811. StringBuf_Init(&buf);
  812. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`");
  813. for( j = 0; j < MAX_SLOTS; ++j )
  814. StringBuf_Printf(&buf, ", `card%d`", j);
  815. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`='%d'", inventory_db, id);
  816. stmt = SqlStmt_Malloc(sql_handle);
  817. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  818. || SQL_ERROR == SqlStmt_Execute(stmt) )
  819. {
  820. SqlStmt_ShowDebug(stmt);
  821. SqlStmt_Free(stmt);
  822. StringBuf_Destroy(&buf);
  823. return 1;
  824. }
  825. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  826. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  827. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  828. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  829. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  830. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  831. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  832. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  833. SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &item.favorite, 0, NULL, NULL);
  834. SqlStmt_BindColumn(stmt, 9, SQLDT_CHAR, &item.bound, 0, NULL, NULL);
  835. for( j = 0; j < MAX_SLOTS; ++j )
  836. SqlStmt_BindColumn(stmt, 10+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  837. // bit array indicating which inventory items have already been matched
  838. flag = (bool*) aCalloc(max, sizeof(bool));
  839. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) {
  840. found = false;
  841. // search for the presence of the item in the char's inventory
  842. for( i = 0; i < max; ++i ) {
  843. // skip empty and already matched entries
  844. if( items[i].nameid == 0 || flag[i] )
  845. continue;
  846. if( items[i].nameid == item.nameid
  847. && items[i].card[0] == item.card[0]
  848. && items[i].card[2] == item.card[2]
  849. && items[i].card[3] == item.card[3]
  850. ) { //They are the same item.
  851. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  852. if( j == MAX_SLOTS &&
  853. items[i].amount == item.amount &&
  854. items[i].equip == item.equip &&
  855. items[i].identify == item.identify &&
  856. items[i].refine == item.refine &&
  857. items[i].attribute == item.attribute &&
  858. items[i].expire_time == item.expire_time &&
  859. items[i].favorite == item.favorite &&
  860. items[i].bound == item.bound )
  861. ; //Do nothing.
  862. else {
  863. // update all fields.
  864. StringBuf_Clear(&buf);
  865. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `favorite`='%d', `bound`='%d'",
  866. inventory_db, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite, items[i].bound);
  867. for( j = 0; j < MAX_SLOTS; ++j )
  868. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  869. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  870. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  871. Sql_ShowDebug(sql_handle);
  872. errors++;
  873. }
  874. }
  875. found = flag[i] = true; //Item dealt with,
  876. break; //skip to next item in the db.
  877. }
  878. }
  879. if( !found ) {// Item not present in inventory, remove it.
  880. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d' LIMIT 1", inventory_db, item.id) ) {
  881. Sql_ShowDebug(sql_handle);
  882. errors++;
  883. }
  884. }
  885. }
  886. SqlStmt_Free(stmt);
  887. StringBuf_Clear(&buf);
  888. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`, `unique_id`", inventory_db);
  889. for( j = 0; j < MAX_SLOTS; ++j )
  890. StringBuf_Printf(&buf, ", `card%d`", j);
  891. StringBuf_AppendStr(&buf, ") VALUES ");
  892. found = false;
  893. // insert non-matched items into the db as new items
  894. for( i = 0; i < max; ++i ) {
  895. // skip empty and already matched entries
  896. if( items[i].nameid == 0 || flag[i] )
  897. continue;
  898. if( found )
  899. StringBuf_AppendStr(&buf, ",");
  900. else
  901. found = true;
  902. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u', '%d', '%d', '%"PRIu64"'",
  903. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].favorite, items[i].bound, items[i].unique_id);
  904. for( j = 0; j < MAX_SLOTS; ++j )
  905. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  906. StringBuf_AppendStr(&buf, ")");
  907. updateLastUid(items[i].unique_id);// Unique Non Stackable Item ID
  908. }
  909. dbUpdateUid(sql_handle);
  910. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ) {
  911. Sql_ShowDebug(sql_handle);
  912. errors++;
  913. }
  914. StringBuf_Destroy(&buf);
  915. aFree(flag);
  916. return errors;
  917. }
  918. int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  919. //=====================================================================================================
  920. // Loads the basic character rooster for the given account. Returns total buffer used.
  921. int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf)
  922. {
  923. SqlStmt* stmt;
  924. struct mmo_charstatus p;
  925. int j = 0, i;
  926. char last_map[MAP_NAME_LENGTH_EXT];
  927. stmt = SqlStmt_Malloc(sql_handle);
  928. if( stmt == NULL )
  929. {
  930. SqlStmt_ShowDebug(stmt);
  931. return 0;
  932. }
  933. memset(&p, 0, sizeof(p));
  934. for( i = 0; i < MAX_CHARS; i++ ){
  935. sd->found_char[i] = -1;
  936. }
  937. // read char data
  938. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  939. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  940. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  941. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  942. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  943. "`robe`,`moves`"
  944. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", char_db, sd->account_id, MAX_CHARS)
  945. || SQL_ERROR == SqlStmt_Execute(stmt)
  946. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  947. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  948. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  949. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  950. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  951. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  952. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  953. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  954. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  955. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  956. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  957. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  958. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  959. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  960. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  961. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL)
  962. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL)
  963. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL)
  964. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL)
  965. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  966. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  967. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  968. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  969. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  970. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  971. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  972. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  973. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  974. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  975. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  976. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  977. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  978. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  979. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  980. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  981. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  982. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_UINT, &p.character_moves, 0, NULL, NULL)
  983. )
  984. {
  985. SqlStmt_ShowDebug(stmt);
  986. SqlStmt_Free(stmt);
  987. return 0;
  988. }
  989. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  990. {
  991. p.last_point.map = mapindex_name2id(last_map);
  992. sd->found_char[p.slot] = p.char_id;
  993. j += mmo_char_tobuf(WBUFP(buf, j), &p);
  994. // Addon System
  995. // store the required info into the session
  996. sd->char_moves[p.slot] = p.character_moves;
  997. }
  998. memset(sd->new_name,0,sizeof(sd->new_name));
  999. SqlStmt_Free(stmt);
  1000. return j;
  1001. }
  1002. //=====================================================================================================
  1003. int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything)
  1004. {
  1005. int i,j;
  1006. char t_msg[128] = "";
  1007. struct mmo_charstatus* cp;
  1008. StringBuf buf;
  1009. SqlStmt* stmt;
  1010. char last_map[MAP_NAME_LENGTH_EXT];
  1011. char save_map[MAP_NAME_LENGTH_EXT];
  1012. char point_map[MAP_NAME_LENGTH_EXT];
  1013. struct point tmp_point;
  1014. struct item tmp_item;
  1015. struct s_skill tmp_skill;
  1016. struct s_friend tmp_friend;
  1017. #ifdef HOTKEY_SAVING
  1018. struct hotkey tmp_hotkey;
  1019. int hotkey_num;
  1020. #endif
  1021. memset(p, 0, sizeof(struct mmo_charstatus));
  1022. if (save_log) ShowInfo("Char load request (%d)\n", char_id);
  1023. stmt = SqlStmt_Malloc(sql_handle);
  1024. if( stmt == NULL )
  1025. {
  1026. SqlStmt_ShowDebug(stmt);
  1027. return 0;
  1028. }
  1029. // read char data
  1030. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  1031. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  1032. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  1033. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  1034. "`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  1035. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`, `moves`"
  1036. " FROM `%s` WHERE `char_id`=? LIMIT 1", char_db)
  1037. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1038. || SQL_ERROR == SqlStmt_Execute(stmt)
  1039. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  1040. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  1041. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  1042. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  1043. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  1044. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  1045. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  1046. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  1047. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  1048. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  1049. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  1050. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  1051. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  1052. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  1053. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  1054. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  1055. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL)
  1056. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL)
  1057. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL)
  1058. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL)
  1059. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  1060. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  1061. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  1062. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  1063. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  1064. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  1065. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  1066. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  1067. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  1068. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  1069. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  1070. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  1071. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  1072. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  1073. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  1074. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  1075. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  1076. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  1077. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  1078. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  1079. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  1080. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  1081. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  1082. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  1083. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_INT, &p->partner_id, 0, NULL, NULL)
  1084. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_INT, &p->father, 0, NULL, NULL)
  1085. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_INT, &p->mother, 0, NULL, NULL)
  1086. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_INT, &p->child, 0, NULL, NULL)
  1087. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_INT, &p->fame, 0, NULL, NULL)
  1088. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  1089. || SQL_ERROR == SqlStmt_BindColumn(stmt, 50, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  1090. || SQL_ERROR == SqlStmt_BindColumn(stmt, 51, SQLDT_SHORT, &p->robe, 0, NULL, NULL)
  1091. || SQL_ERROR == SqlStmt_BindColumn(stmt, 52, SQLDT_UINT32, &p->character_moves, 0, NULL, NULL)
  1092. )
  1093. {
  1094. SqlStmt_ShowDebug(stmt);
  1095. SqlStmt_Free(stmt);
  1096. return 0;
  1097. }
  1098. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  1099. {
  1100. ShowError("Requested non-existant character id: %d!\n", char_id);
  1101. SqlStmt_Free(stmt);
  1102. return 0;
  1103. }
  1104. p->last_point.map = mapindex_name2id(last_map);
  1105. p->save_point.map = mapindex_name2id(save_map);
  1106. if( p->last_point.map == 0 ) {
  1107. p->last_point.map = mapindex_name2id(MAP_DEFAULT);
  1108. p->last_point.x = MAP_DEFAULT_X;
  1109. p->last_point.y = MAP_DEFAULT_Y;
  1110. }
  1111. if( p->save_point.map == 0 ) {
  1112. p->save_point.map = mapindex_name2id(MAP_DEFAULT);
  1113. p->save_point.x = MAP_DEFAULT_X;
  1114. p->save_point.y = MAP_DEFAULT_Y;
  1115. }
  1116. strcat(t_msg, " status");
  1117. if (!load_everything) // For quick selection of data when displaying the char menu
  1118. {
  1119. SqlStmt_Free(stmt);
  1120. return 1;
  1121. }
  1122. //read memo data
  1123. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1124. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", memo_db, MAX_MEMOPOINTS)
  1125. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1126. || SQL_ERROR == SqlStmt_Execute(stmt)
  1127. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1128. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1129. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1130. SqlStmt_ShowDebug(stmt);
  1131. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1132. {
  1133. tmp_point.map = mapindex_name2id(point_map);
  1134. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1135. }
  1136. strcat(t_msg, " memo");
  1137. //read inventory
  1138. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, `expire_time`, `favorite`, `unique_id`)
  1139. StringBuf_Init(&buf);
  1140. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `favorite`, `bound`, `unique_id`");
  1141. for( i = 0; i < MAX_SLOTS; ++i )
  1142. StringBuf_Printf(&buf, ", `card%d`", i);
  1143. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", inventory_db, MAX_INVENTORY);
  1144. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1145. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1146. || SQL_ERROR == SqlStmt_Execute(stmt)
  1147. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1148. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1149. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1150. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1151. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1152. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1153. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1154. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1155. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &tmp_item.favorite, 0, NULL, NULL)
  1156. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_CHAR, &tmp_item.bound, 0, NULL, NULL)
  1157. || SQL_ERROR == SqlStmt_BindColumn(stmt,10, SQLDT_ULONGLONG, &tmp_item.unique_id, 0, NULL, NULL) )
  1158. SqlStmt_ShowDebug(stmt);
  1159. for( i = 0; i < MAX_SLOTS; ++i )
  1160. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 11+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1161. SqlStmt_ShowDebug(stmt);
  1162. for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1163. memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item));
  1164. strcat(t_msg, " inventory");
  1165. //read cart
  1166. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`, expire_time`, `unique_id`)
  1167. StringBuf_Clear(&buf);
  1168. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `bound`, `unique_id`");
  1169. for( j = 0; j < MAX_SLOTS; ++j )
  1170. StringBuf_Printf(&buf, ", `card%d`", j);
  1171. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", cart_db, MAX_CART);
  1172. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1173. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1174. || SQL_ERROR == SqlStmt_Execute(stmt)
  1175. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1176. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1177. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1178. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1179. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1180. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1181. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1182. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1183. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_CHAR, &tmp_item.bound, 0, NULL, NULL)
  1184. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_ULONGLONG, &tmp_item.unique_id, 0, NULL, NULL) )
  1185. SqlStmt_ShowDebug(stmt);
  1186. for( i = 0; i < MAX_SLOTS; ++i )
  1187. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 10+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1188. SqlStmt_ShowDebug(stmt);
  1189. for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1190. memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item));
  1191. strcat(t_msg, " cart");
  1192. //read storage
  1193. storage_fromsql(p->account_id, &p->storage);
  1194. strcat(t_msg, " storage");
  1195. //read skill
  1196. //`skill` (`char_id`, `id`, `lv`)
  1197. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv`,`flag` FROM `%s` WHERE `char_id`=? LIMIT %d", skill_db, MAX_SKILL)
  1198. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1199. || SQL_ERROR == SqlStmt_Execute(stmt)
  1200. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_USHORT, &tmp_skill.id , 0, NULL, NULL)
  1201. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR , &tmp_skill.lv , 0, NULL, NULL)
  1202. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR , &tmp_skill.flag, 0, NULL, NULL) )
  1203. SqlStmt_ShowDebug(stmt);
  1204. if( tmp_skill.flag != SKILL_FLAG_PERM_GRANTED )
  1205. tmp_skill.flag = SKILL_FLAG_PERMANENT;
  1206. for( i = 0; i < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1207. {
  1208. if( tmp_skill.id < ARRAYLENGTH(p->skill) )
  1209. memcpy(&p->skill[tmp_skill.id], &tmp_skill, sizeof(tmp_skill));
  1210. else
  1211. ShowWarning("mmo_char_fromsql: ignoring invalid skill (id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", tmp_skill.id, tmp_skill.lv, p->name, p->account_id, p->char_id);
  1212. }
  1213. strcat(t_msg, " skills");
  1214. //read friends
  1215. //`friends` (`char_id`, `friend_account`, `friend_id`)
  1216. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_account` = c.`account_id` AND f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", char_db, friend_db, MAX_FRIENDS)
  1217. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1218. || SQL_ERROR == SqlStmt_Execute(stmt)
  1219. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1220. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1221. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1222. SqlStmt_ShowDebug(stmt);
  1223. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1224. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1225. strcat(t_msg, " friends");
  1226. #ifdef HOTKEY_SAVING
  1227. //read hotkeys
  1228. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1229. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", hotkey_db)
  1230. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1231. || SQL_ERROR == SqlStmt_Execute(stmt)
  1232. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1233. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1234. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1235. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1236. SqlStmt_ShowDebug(stmt);
  1237. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1238. {
  1239. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  1240. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1241. else
  1242. ShowWarning("mmo_char_fromsql: ignoring invalid hotkey (hotkey=%d,type=%u,id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", hotkey_num, tmp_hotkey.type, tmp_hotkey.id, tmp_hotkey.lv, p->name, p->account_id, p->char_id);
  1243. }
  1244. strcat(t_msg, " hotkeys");
  1245. #endif
  1246. /* Mercenary Owner DataBase */
  1247. mercenary_owner_fromsql(char_id, p);
  1248. strcat(t_msg, " mercenary");
  1249. if (save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  1250. SqlStmt_Free(stmt);
  1251. StringBuf_Destroy(&buf);
  1252. cp = idb_ensure(char_db_, char_id, create_charstatus);
  1253. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1254. return 1;
  1255. }
  1256. //==========================================================================================================
  1257. int mmo_char_sql_init(void)
  1258. {
  1259. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1260. ShowStatus("Characters per Account: '%d'.\n", char_per_account);
  1261. //the 'set offline' part is now in check_login_conn ...
  1262. //if the server connects to loginserver
  1263. //it will dc all off players
  1264. //and send the loginserver the new state....
  1265. // Force all users offline in sql when starting char-server
  1266. // (useful when servers crashs and don't clean the database)
  1267. set_all_offline_sql();
  1268. return 0;
  1269. }
  1270. //-----------------------------------
  1271. // Function to change chararcter's names
  1272. //-----------------------------------
  1273. int rename_char_sql(struct char_session_data *sd, int char_id)
  1274. {
  1275. struct mmo_charstatus char_dat;
  1276. char esc_name[NAME_LENGTH*2+1];
  1277. if( sd->new_name[0] == 0 ) // Not ready for rename
  1278. return 2;
  1279. if( !mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1280. return 2;
  1281. if( char_dat.rename == 0 )
  1282. return 1;
  1283. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1284. // check if the char exist
  1285. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` LIKE '%s' LIMIT 1", char_db, esc_name) )
  1286. {
  1287. Sql_ShowDebug(sql_handle);
  1288. return 4;
  1289. }
  1290. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", char_db, esc_name, --char_dat.rename, char_id) )
  1291. {
  1292. Sql_ShowDebug(sql_handle);
  1293. return 3;
  1294. }
  1295. // Change character's name into guild_db.
  1296. if( char_dat.guild_id )
  1297. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1298. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1299. memset(sd->new_name,0,sizeof(sd->new_name));
  1300. // log change
  1301. if( log_char )
  1302. {
  1303. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1304. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1305. charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1306. Sql_ShowDebug(sql_handle);
  1307. }
  1308. return 0;
  1309. }
  1310. int check_char_name(char * name, char * esc_name)
  1311. {
  1312. int i;
  1313. // check length of character name
  1314. if( name[0] == '\0' )
  1315. return -2; // empty character name
  1316. /**
  1317. * The client does not allow you to create names with less than 4 characters, however,
  1318. * the use of WPE can bypass this, and this fixes the exploit.
  1319. **/
  1320. if( strlen( name ) < 4 )
  1321. return -2;
  1322. // check content of character name
  1323. if( remove_control_chars(name) )
  1324. return -2; // control chars in name
  1325. // check for reserved names
  1326. if( strcmpi(name, wisp_server_name) == 0 )
  1327. return -1; // nick reserved for internal server messages
  1328. // Check Authorised letters/symbols in the name of the character
  1329. if( char_name_option == 1 )
  1330. { // only letters/symbols in char_name_letters are authorised
  1331. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1332. if( strchr(char_name_letters, name[i]) == NULL )
  1333. return -2;
  1334. }
  1335. else if( char_name_option == 2 )
  1336. { // letters/symbols in char_name_letters are forbidden
  1337. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1338. if( strchr(char_name_letters, name[i]) != NULL )
  1339. return -2;
  1340. }
  1341. if( name_ignoring_case ) {
  1342. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE BINARY `name` = '%s' LIMIT 1", char_db, esc_name) ) {
  1343. Sql_ShowDebug(sql_handle);
  1344. return -2;
  1345. }
  1346. } else {
  1347. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s' LIMIT 1", char_db, esc_name) ) {
  1348. Sql_ShowDebug(sql_handle);
  1349. return -2;
  1350. }
  1351. }
  1352. if( Sql_NumRows(sql_handle) > 0 )
  1353. return -1; // name already exists
  1354. return 0;
  1355. }
  1356. //-----------------------------------
  1357. // Function to create a new character
  1358. //-----------------------------------
  1359. #if PACKETVER >= 20120307
  1360. int make_new_char_sql(struct char_session_data* sd, char* name_, int slot, int hair_color, int hair_style) {
  1361. int str = 1, agi = 1, vit = 1, int_ = 1, dex = 1, luk = 1;
  1362. #else
  1363. int make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style) {
  1364. #endif
  1365. char name[NAME_LENGTH];
  1366. char esc_name[NAME_LENGTH*2+1];
  1367. int char_id, flag, k;
  1368. safestrncpy(name, name_, NAME_LENGTH);
  1369. normalize_name(name,TRIM_CHARS);
  1370. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1371. flag = check_char_name(name,esc_name);
  1372. if( flag < 0 )
  1373. return flag;
  1374. //check other inputs
  1375. #if PACKETVER >= 20120307
  1376. if(slot >= sd->char_slots)
  1377. #else
  1378. if((slot >= sd->char_slots) // slots
  1379. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1380. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1381. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1382. #endif
  1383. #if PACKETVER >= 20100413
  1384. return -4; // invalid slot
  1385. #else
  1386. return -2; // invalid input
  1387. #endif
  1388. // check the number of already existing chars in this account
  1389. if( char_per_account != 0 ) {
  1390. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) )
  1391. Sql_ShowDebug(sql_handle);
  1392. if( Sql_NumRows(sql_handle) >= char_per_account )
  1393. return -2; // character account limit exceeded
  1394. }
  1395. // check char slot
  1396. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' LIMIT 1", char_db, sd->account_id, slot) )
  1397. Sql_ShowDebug(sql_handle);
  1398. if( Sql_NumRows(sql_handle) > 0 )
  1399. return -2; // slot already in use
  1400. // validation success, log result
  1401. if (log_char) {
  1402. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1403. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1404. charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1405. Sql_ShowDebug(sql_handle);
  1406. }
  1407. #if PACKETVER >= 20120307
  1408. //Insert the new char entry to the database
  1409. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `status_point`,`str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1410. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1411. "'%d', '%d', '%s', '%d', '%d','%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1412. char_db, sd->account_id , slot, esc_name, start_zeny, 48, str, agi, vit, int_, dex, luk,
  1413. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1414. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1415. {
  1416. Sql_ShowDebug(sql_handle);
  1417. return -2; //No, stop the procedure!
  1418. }
  1419. #else
  1420. //Insert the new char entry to the database
  1421. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1422. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1423. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1424. char_db, sd->account_id , slot, esc_name, start_zeny, str, agi, vit, int_, dex, luk,
  1425. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1426. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1427. {
  1428. Sql_ShowDebug(sql_handle);
  1429. return -2; //No, stop the procedure!
  1430. }
  1431. #endif
  1432. //Retrieve the newly auto-generated char id
  1433. char_id = (int)Sql_LastInsertId(sql_handle);
  1434. //Give the char the default items
  1435. for (k = 0; k <= MAX_STARTITEM && start_items[k].nameid != 0; k ++) {
  1436. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", inventory_db, char_id, start_items[k].nameid, start_items[k].amount, start_items[k].pos, 1) )
  1437. Sql_ShowDebug(sql_handle);
  1438. }
  1439. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1440. return char_id;
  1441. }
  1442. /*----------------------------------------------------------------------------------------------------------*/
  1443. /* Divorce Players */
  1444. /*----------------------------------------------------------------------------------------------------------*/
  1445. int divorce_char_sql(int partner_id1, int partner_id2)
  1446. {
  1447. unsigned char buf[64];
  1448. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d' LIMIT 2", char_db, partner_id1, partner_id2) )
  1449. Sql_ShowDebug(sql_handle);
  1450. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND (`char_id`='%d' OR `char_id`='%d') LIMIT 2", inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1451. Sql_ShowDebug(sql_handle);
  1452. WBUFW(buf,0) = 0x2b12;
  1453. WBUFL(buf,2) = partner_id1;
  1454. WBUFL(buf,6) = partner_id2;
  1455. mapif_sendall(buf,10);
  1456. return 0;
  1457. }
  1458. /*----------------------------------------------------------------------------------------------------------*/
  1459. /* Delete char - davidsiaw */
  1460. /*----------------------------------------------------------------------------------------------------------*/
  1461. /* Returns 0 if successful
  1462. * Returns < 0 for error
  1463. */
  1464. int delete_char_sql(int char_id)
  1465. {
  1466. char name[NAME_LENGTH];
  1467. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1468. int account_id, party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id, elemental_id;
  1469. char *data;
  1470. size_t len;
  1471. if (SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother`,`elemental_id` FROM `%s` WHERE `char_id`='%d'", char_db, char_id))
  1472. Sql_ShowDebug(sql_handle);
  1473. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1474. {
  1475. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1476. Sql_FreeResult(sql_handle);
  1477. return -1;
  1478. }
  1479. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1480. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1481. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1482. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1483. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1484. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1485. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1486. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1487. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1488. Sql_GetData(sql_handle, 9, &data, NULL); elemental_id = atoi(data);
  1489. Sql_EscapeStringLen(sql_handle, esc_name, name, min(len, NAME_LENGTH));
  1490. Sql_FreeResult(sql_handle);
  1491. //check for config char del condition [Lupus]
  1492. // TODO: Move this out to packet processing (0x68/0x1fb).
  1493. if( ( char_del_level > 0 && base_level >= char_del_level )
  1494. || ( char_del_level < 0 && base_level <= -char_del_level )
  1495. ) {
  1496. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1497. return -1;
  1498. }
  1499. /* Divorce [Wizputer] */
  1500. if( partner_id )
  1501. divorce_char_sql(char_id, partner_id);
  1502. /* De-addopt [Zephyrus] */
  1503. if( father_id || mother_id )
  1504. { // Char is Baby
  1505. unsigned char buf[64];
  1506. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, father_id, mother_id) )
  1507. Sql_ShowDebug(sql_handle);
  1508. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", skill_db, father_id, mother_id) )
  1509. Sql_ShowDebug(sql_handle);
  1510. WBUFW(buf,0) = 0x2b25;
  1511. WBUFL(buf,2) = father_id;
  1512. WBUFL(buf,6) = mother_id;
  1513. WBUFL(buf,10) = char_id; // Baby
  1514. mapif_sendall(buf,14);
  1515. }
  1516. //Make the character leave the party [Skotlex]
  1517. if (party_id)
  1518. inter_party_leave(party_id, account_id, char_id);
  1519. /* delete char's pet */
  1520. //Delete the hatched pet if you have one...
  1521. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'", pet_db, char_id) )
  1522. Sql_ShowDebug(sql_handle);
  1523. //Delete all pets that are stored in eggs (inventory + cart)
  1524. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, inventory_db, inventory_db, char_id) )
  1525. Sql_ShowDebug(sql_handle);
  1526. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, cart_db, cart_db, char_id) )
  1527. Sql_ShowDebug(sql_handle);
  1528. /* remove homunculus */
  1529. if( hom_id )
  1530. mapif_homunculus_delete(hom_id);
  1531. /* remove elemental */
  1532. if (elemental_id)
  1533. mapif_elemental_delete(elemental_id);
  1534. /* remove mercenary data */
  1535. mercenary_owner_delete(char_id);
  1536. /* delete char's friends list */
  1537. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", friend_db, char_id) )
  1538. Sql_ShowDebug(sql_handle);
  1539. /* delete char from other's friend list */
  1540. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1541. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", friend_db, char_id) )
  1542. Sql_ShowDebug(sql_handle);
  1543. #ifdef HOTKEY_SAVING
  1544. /* delete hotkeys */
  1545. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", hotkey_db, char_id) )
  1546. Sql_ShowDebug(sql_handle);
  1547. #endif
  1548. /* delete inventory */
  1549. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id) )
  1550. Sql_ShowDebug(sql_handle);
  1551. /* delete cart inventory */
  1552. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", cart_db, char_id) )
  1553. Sql_ShowDebug(sql_handle);
  1554. /* delete memo areas */
  1555. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, char_id) )
  1556. Sql_ShowDebug(sql_handle);
  1557. /* delete character registry */
  1558. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
  1559. Sql_ShowDebug(sql_handle);
  1560. /* delete skills */
  1561. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, char_id) )
  1562. Sql_ShowDebug(sql_handle);
  1563. /* delete mails (only received) */
  1564. if (SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `dest_id`='%d'", mail_db, char_id))
  1565. Sql_ShowDebug(sql_handle);
  1566. #ifdef ENABLE_SC_SAVING
  1567. /* status changes */
  1568. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, account_id, char_id) )
  1569. Sql_ShowDebug(sql_handle);
  1570. #endif
  1571. if (log_char) {
  1572. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`char_msg`,`name`) VALUES (NOW(), '%d', '%d', 'Deleted char (CID %d)', '%s')",
  1573. charlog_db, account_id, 0, char_id, esc_name) )
  1574. Sql_ShowDebug(sql_handle);
  1575. }
  1576. /* delete character */
  1577. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1578. Sql_ShowDebug(sql_handle);
  1579. /* No need as we used inter_guild_leave [Skotlex]
  1580. // Also delete info from guildtables.
  1581. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1582. Sql_ShowDebug(sql_handle);
  1583. */
  1584. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", guild_db, char_id) )
  1585. Sql_ShowDebug(sql_handle);
  1586. else if( Sql_NumRows(sql_handle) > 0 )
  1587. mapif_parse_BreakGuild(0,guild_id);
  1588. else if( guild_id )
  1589. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1590. return 0;
  1591. }
  1592. //---------------------------------------------------------------------
  1593. // This function return the number of online players in all map-servers
  1594. //---------------------------------------------------------------------
  1595. int count_users(void)
  1596. {
  1597. int i, users;
  1598. users = 0;
  1599. for(i = 0; i < ARRAYLENGTH(server); i++) {
  1600. if (server[i].fd > 0) {
  1601. users += server[i].users;
  1602. }
  1603. }
  1604. return users;
  1605. }
  1606. // Writes char data to the buffer in the format used by the client.
  1607. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1608. // Returns the size
  1609. #define MAX_CHAR_BUF 144 //Max size (for WFIFOHEAD calls)
  1610. int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1611. {
  1612. unsigned short offset = 0;
  1613. uint8* buf;
  1614. if( buffer == NULL || p == NULL )
  1615. return 0;
  1616. buf = WBUFP(buffer,0);
  1617. WBUFL(buf,0) = p->char_id;
  1618. WBUFL(buf,4) = min(p->base_exp, INT32_MAX);
  1619. WBUFL(buf,8) = p->zeny;
  1620. WBUFL(buf,12) = min(p->job_exp, INT32_MAX);
  1621. WBUFL(buf,16) = p->job_level;
  1622. WBUFL(buf,20) = 0; // probably opt1
  1623. WBUFL(buf,24) = 0; // probably opt2
  1624. WBUFL(buf,28) = p->option;
  1625. WBUFL(buf,32) = p->karma;
  1626. WBUFL(buf,36) = p->manner;
  1627. WBUFW(buf,40) = min(p->status_point, INT16_MAX);
  1628. WBUFL(buf,42) = p->hp;
  1629. WBUFL(buf,46) = p->max_hp;
  1630. offset+=4;
  1631. buf = WBUFP(buffer,offset);
  1632. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1633. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1634. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1635. WBUFW(buf,52) = p->class_;
  1636. WBUFW(buf,54) = p->hair;
  1637. //When the weapon is sent and your option is riding, the client crashes on login!?
  1638. WBUFW(buf,56) = p->option&(0x20|0x80000|0x100000|0x200000|0x400000|0x800000|0x1000000|0x2000000|0x4000000|0x8000000) ? 0 : p->weapon;
  1639. WBUFW(buf,58) = p->base_level;
  1640. WBUFW(buf,60) = min(p->skill_point, INT16_MAX);
  1641. WBUFW(buf,62) = p->head_bottom;
  1642. WBUFW(buf,64) = p->shield;
  1643. WBUFW(buf,66) = p->head_top;
  1644. WBUFW(buf,68) = p->head_mid;
  1645. WBUFW(buf,70) = p->hair_color;
  1646. WBUFW(buf,72) = p->clothes_color;
  1647. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1648. WBUFB(buf,98) = min(p->str, UINT8_MAX);
  1649. WBUFB(buf,99) = min(p->agi, UINT8_MAX);
  1650. WBUFB(buf,100) = min(p->vit, UINT8_MAX);
  1651. WBUFB(buf,101) = min(p->int_, UINT8_MAX);
  1652. WBUFB(buf,102) = min(p->dex, UINT8_MAX);
  1653. WBUFB(buf,103) = min(p->luk, UINT8_MAX);
  1654. WBUFW(buf,104) = p->slot;
  1655. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1656. offset += 2;
  1657. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1658. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1659. offset += MAP_NAME_LENGTH_EXT;
  1660. #endif
  1661. #if PACKETVER >= 20100803
  1662. WBUFL(buf,124) = TOL(p->delete_date);
  1663. offset += 4;
  1664. #endif
  1665. #if PACKETVER >= 20110111
  1666. WBUFL(buf,128) = p->robe;
  1667. offset += 4;
  1668. #endif
  1669. #if PACKETVER != 20111116 //2011-11-16 wants 136, ask gravity.
  1670. #if PACKETVER >= 20110928
  1671. // change slot feature (0 = disabled, otherwise enabled)
  1672. if( !char_move_enabled )
  1673. WBUFL(buf,132) = 0;
  1674. else if( char_moves_unlimited )
  1675. WBUFL(buf,132) = 1;
  1676. else
  1677. WBUFL(buf,132) = max( 0, (int)p->character_moves );
  1678. offset += 4;
  1679. #endif
  1680. #if PACKETVER >= 20111025
  1681. WBUFL(buf,136) = ( p->rename > 0 ) ? 1 : 0; // (0 = disabled, otherwise displays "Add-Ons" sidebar)
  1682. offset += 4;
  1683. #endif
  1684. #endif
  1685. return 106+offset;
  1686. }
  1687. //----------------------------------------
  1688. // Tell client how many pages, kRO sends 17 (Yommy)
  1689. //----------------------------------------
  1690. void char_charlist_notify( int fd, struct char_session_data* sd ){
  1691. WFIFOHEAD(fd, 6);
  1692. WFIFOW(fd, 0) = 0x9a0;
  1693. // pages to req / send them all in 1 until mmo_chars_fromsql can split them up
  1694. WFIFOL(fd, 2) = (sd->char_slots>3)?sd->char_slots/3:1; //int TotalCnt (nb page to load)
  1695. WFIFOSET(fd,6);
  1696. }
  1697. void char_block_character( int fd, struct char_session_data* sd ){
  1698. WFIFOHEAD(fd, 4);
  1699. WFIFOW(fd, 0) = 0x20d;
  1700. WFIFOW(fd, 2) = 4; //packet len
  1701. WFIFOSET(fd,4);
  1702. }
  1703. void mmo_char_send099d(int fd, struct char_session_data *sd) {
  1704. WFIFOHEAD(fd,4 + (MAX_CHARS*MAX_CHAR_BUF));
  1705. WFIFOW(fd,0) = 0x99d;
  1706. WFIFOW(fd,2) = mmo_chars_fromsql(sd, WFIFOP(fd,4)) + 4;
  1707. WFIFOSET(fd,WFIFOW(fd,2));
  1708. }
  1709. //struct PACKET_CH_CHARLIST_REQ { 0x0 short PacketType}
  1710. void char_parse_req_charlist(int fd, struct char_session_data* sd){
  1711. mmo_char_send099d(fd,sd);
  1712. }
  1713. //----------------------------------------
  1714. // Function to send characters to a player
  1715. //----------------------------------------
  1716. int mmo_char_send006b(int fd, struct char_session_data* sd){
  1717. int j, offset = 0;
  1718. //bool newvers = (sd->version >= date2version(20100413) );
  1719. #if PACKETVER >= 20100413
  1720. //if(newvers) //20100413
  1721. offset += 3;
  1722. #endif
  1723. if (save_log)
  1724. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1725. j = 24 + offset; // offset
  1726. WFIFOHEAD(fd,j + MAX_CHARS*MAX_CHAR_BUF);
  1727. WFIFOW(fd,0) = 0x6b;
  1728. #if PACKETVER >= 20100413
  1729. // if(newvers){ //20100413
  1730. WFIFOB(fd,4) = MAX_CHARS; // Max slots.
  1731. WFIFOB(fd,5) = sd->char_slots; // Available slots. (PremiumStartSlot)
  1732. WFIFOB(fd,6) = MAX_CHARS; // Premium slots. (Any existent chars past sd->char_slots but within MAX_CHARS will show a 'Premium Service' in red)
  1733. // }
  1734. #endif
  1735. memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes
  1736. j+=mmo_chars_fromsql(sd, WFIFOP(fd,j));
  1737. WFIFOW(fd,2) = j; // packet len
  1738. WFIFOSET(fd,j);
  1739. return 0;
  1740. }
  1741. //----------------------------------------
  1742. // Notify client about charselect window data [Ind]
  1743. //----------------------------------------
  1744. void mmo_char_send082d(int fd, struct char_session_data* sd) {
  1745. if (save_log)
  1746. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1747. WFIFOHEAD(fd,29);
  1748. WFIFOW(fd,0) = 0x82d;
  1749. WFIFOW(fd,2) = 29;
  1750. WFIFOB(fd,4) = sd->char_slots;
  1751. WFIFOB(fd,5) = MAX_CHARS - sd->char_slots;
  1752. WFIFOB(fd,6) = MAX_CHARS - sd->char_slots;
  1753. WFIFOB(fd,7) = sd->char_slots;
  1754. WFIFOB(fd,8) = sd->char_slots;
  1755. memset(WFIFOP(fd,9), 0, 20); // unused bytes
  1756. WFIFOSET(fd,29);
  1757. }
  1758. void mmo_char_send(int fd, struct char_session_data* sd){
  1759. //ShowInfo("sd->version = %d\n",sd->version);
  1760. #if PACKETVER >= 20130000
  1761. //if(sd->version > date2version(20130000) ){
  1762. mmo_char_send082d(fd,sd);
  1763. char_charlist_notify(fd,sd);
  1764. char_block_character(fd,sd);
  1765. #endif
  1766. //}
  1767. //@FIXME dump from kro doesn't show 6b transmission
  1768. mmo_char_send006b(fd,sd);
  1769. }
  1770. int char_married(int pl1, int pl2)
  1771. {
  1772. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1) )
  1773. Sql_ShowDebug(sql_handle);
  1774. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1775. {
  1776. char* data;
  1777. Sql_GetData(sql_handle, 0, &data, NULL);
  1778. if( pl2 == atoi(data) )
  1779. {
  1780. Sql_FreeResult(sql_handle);
  1781. return 1;
  1782. }
  1783. }
  1784. Sql_FreeResult(sql_handle);
  1785. return 0;
  1786. }
  1787. int char_child(int parent_id, int child_id)
  1788. {
  1789. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id) )
  1790. Sql_ShowDebug(sql_handle);
  1791. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1792. {
  1793. char* data;
  1794. Sql_GetData(sql_handle, 0, &data, NULL);
  1795. if( child_id == atoi(data) )
  1796. {
  1797. Sql_FreeResult(sql_handle);
  1798. return 1;
  1799. }
  1800. }
  1801. Sql_FreeResult(sql_handle);
  1802. return 0;
  1803. }
  1804. int char_family(int cid1, int cid2, int cid3)
  1805. {
  1806. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", char_db, cid1, cid2, cid3) )
  1807. Sql_ShowDebug(sql_handle);
  1808. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1809. {
  1810. int charid;
  1811. int partnerid;
  1812. int childid;
  1813. char* data;
  1814. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1815. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1816. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1817. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1818. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1819. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1820. {
  1821. Sql_FreeResult(sql_handle);
  1822. return childid;
  1823. }
  1824. }
  1825. Sql_FreeResult(sql_handle);
  1826. return 0;
  1827. }
  1828. //----------------------------------------------------------------------
  1829. // Force disconnection of an online player (with account value) by [Yor]
  1830. //----------------------------------------------------------------------
  1831. void disconnect_player(int account_id)
  1832. {
  1833. int i;
  1834. struct char_session_data* sd;
  1835. // disconnect player if online on char-server
  1836. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1837. if( i < fd_max )
  1838. set_eof(i);
  1839. }
  1840. static void char_auth_ok(int fd, struct char_session_data *sd)
  1841. {
  1842. struct online_char_data* character;
  1843. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1844. { // check if character is not online already. [Skotlex]
  1845. if (character->server > -1)
  1846. { //Character already online. KICK KICK KICK
  1847. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1848. if (character->waiting_disconnect == INVALID_TIMER)
  1849. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1850. WFIFOHEAD(fd,3);
  1851. WFIFOW(fd,0) = 0x81;
  1852. WFIFOB(fd,2) = 8;
  1853. WFIFOSET(fd,3);
  1854. return;
  1855. }
  1856. if (character->fd >= 0 && character->fd != fd)
  1857. { //There's already a connection from this account that hasn't picked a char yet.
  1858. WFIFOHEAD(fd,3);
  1859. WFIFOW(fd,0) = 0x81;
  1860. WFIFOB(fd,2) = 8;
  1861. WFIFOSET(fd,3);
  1862. return;
  1863. }
  1864. character->fd = fd;
  1865. }
  1866. if (login_fd > 0) {
  1867. // request account data
  1868. WFIFOHEAD(login_fd,6);
  1869. WFIFOW(login_fd,0) = 0x2716;
  1870. WFIFOL(login_fd,2) = sd->account_id;
  1871. WFIFOSET(login_fd,6);
  1872. }
  1873. // mark session as 'authed'
  1874. sd->auth = true;
  1875. // set char online on charserver
  1876. set_char_charselect(sd->account_id);
  1877. // continues when account data is received...
  1878. }
  1879. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  1880. void mapif_server_reset(int id);
  1881. /// Resets all the data.
  1882. void loginif_reset(void)
  1883. {
  1884. int id;
  1885. // TODO kick everyone out and reset everything or wait for connect and try to reaquire locks [FlavioJS]
  1886. for( id = 0; id < ARRAYLENGTH(server); ++id )
  1887. mapif_server_reset(id);
  1888. flush_fifos();
  1889. exit(EXIT_FAILURE);
  1890. }
  1891. /// Checks the conditions for the server to stop.
  1892. /// Releases the cookie when all characters are saved.
  1893. /// If all the conditions are met, it stops the core loop.
  1894. void loginif_check_shutdown(void)
  1895. {
  1896. if( runflag != CHARSERVER_ST_SHUTDOWN )
  1897. return;
  1898. runflag = CORE_ST_STOP;
  1899. }
  1900. /// Called when the connection to Login Server is disconnected.
  1901. void loginif_on_disconnect(void)
  1902. {
  1903. ShowWarning("Connection to Login Server lost.\n\n");
  1904. }
  1905. /// Called when all the connection steps are completed.
  1906. void loginif_on_ready(void)
  1907. {
  1908. int i;
  1909. loginif_check_shutdown();
  1910. //Send online accounts to login server.
  1911. send_accounts_tologin(INVALID_TIMER, gettick(), 0, 0);
  1912. // if no map-server already connected, display a message...
  1913. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd > 0 && server[i].map[0] );
  1914. if( i == ARRAYLENGTH(server) )
  1915. ShowStatus("Awaiting maps from map-server.\n");
  1916. }
  1917. int parse_fromlogin(int fd) {
  1918. struct char_session_data* sd = NULL;
  1919. int i;
  1920. // only process data from the login-server
  1921. if( fd != login_fd ) {
  1922. ShowDebug("parse_fromlogin: Disconnecting invalid session #%d (is not the login-server)\n", fd);
  1923. do_close(fd);
  1924. return 0;
  1925. }
  1926. if( session[fd]->flag.eof ) {
  1927. do_close(fd);
  1928. login_fd = -1;
  1929. loginif_on_disconnect();
  1930. return 0;
  1931. } else if ( session[fd]->flag.ping ) {/* we've reached stall time */
  1932. if( DIFF_TICK(last_tick, session[fd]->rdata_tick) > (stall_time * 2) ) {/* we can't wait any longer */
  1933. set_eof(fd);
  1934. return 0;
  1935. } else if( session[fd]->flag.ping != 2 ) { /* we haven't sent ping out yet */
  1936. WFIFOHEAD(fd,2);// sends a ping packet to login server (will receive pong 0x2718)
  1937. WFIFOW(fd,0) = 0x2719;
  1938. WFIFOSET(fd,2);
  1939. session[fd]->flag.ping = 2;
  1940. }
  1941. }
  1942. sd = (struct char_session_data*)session[fd]->session_data;
  1943. while(RFIFOREST(fd) >= 2) {
  1944. uint16 command = RFIFOW(fd,0);
  1945. switch( command )
  1946. {
  1947. // acknowledgement of connect-to-loginserver request
  1948. case 0x2711:
  1949. if (RFIFOREST(fd) < 3)
  1950. return 0;
  1951. if (RFIFOB(fd,2)) {
  1952. //printf("connect login server error : %d\n", RFIFOB(fd,2));
  1953. ShowError("Can not connect to login-server.\n");
  1954. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1955. ShowError("Also, please make sure your login db has the correct communication username/passwords and the gender of the account is S.\n");
  1956. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1957. set_eof(fd);
  1958. return 0;
  1959. } else {
  1960. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1961. loginif_on_ready();
  1962. }
  1963. RFIFOSKIP(fd,3);
  1964. break;
  1965. // acknowledgement of account authentication request
  1966. case 0x2713:
  1967. if (RFIFOREST(fd) < 25)
  1968. return 0;
  1969. {
  1970. int account_id = RFIFOL(fd,2);
  1971. uint32 login_id1 = RFIFOL(fd,6);
  1972. uint32 login_id2 = RFIFOL(fd,10);
  1973. uint8 sex = RFIFOB(fd,14);
  1974. uint8 result = RFIFOB(fd,15);
  1975. int request_id = RFIFOL(fd,16);
  1976. uint32 version = RFIFOL(fd,20);
  1977. uint8 clienttype = RFIFOB(fd,24);
  1978. RFIFOSKIP(fd,25);
  1979. if( session_isActive(request_id) && (sd=(struct char_session_data*)session[request_id]->session_data) &&
  1980. !sd->auth && sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 && sd->sex == sex )
  1981. {
  1982. int client_fd = request_id;
  1983. sd->version = version;
  1984. sd->clienttype = clienttype;
  1985. switch( result )
  1986. {
  1987. case 0:// ok
  1988. char_auth_ok(client_fd, sd);
  1989. break;
  1990. case 1:// auth failed
  1991. WFIFOHEAD(client_fd,3);
  1992. WFIFOW(client_fd,0) = 0x6c;
  1993. WFIFOB(client_fd,2) = 0;// rejected from server
  1994. WFIFOSET(client_fd,3);
  1995. break;
  1996. }
  1997. }
  1998. }
  1999. break;
  2000. case 0x2717: // account data
  2001. if (RFIFOREST(fd) < 72)
  2002. return 0;
  2003. // find the authenticated session with this account id
  2004. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) );
  2005. if( i < fd_max )
  2006. {
  2007. int server_id;
  2008. memcpy(sd->email, RFIFOP(fd,6), 40);
  2009. sd->expiration_time = (time_t)RFIFOL(fd,46);
  2010. sd->group_id = RFIFOB(fd,50);
  2011. sd->char_slots = RFIFOB(fd,51);
  2012. if( sd->char_slots > MAX_CHARS ) {
  2013. ShowError("Account '%d' `character_slots` column is higher than supported MAX_CHARS (%d), update MAX_CHARS in mmo.h! capping to MAX_CHARS...\n",sd->account_id,sd->char_slots);
  2014. sd->char_slots = MAX_CHARS;/* cap to maximum */
  2015. } else if ( !sd->char_slots )/* no value aka 0 in sql */
  2016. sd->char_slots = MAX_CHARS;/* cap to maximum */
  2017. safestrncpy(sd->birthdate, (const char*)RFIFOP(fd,52), sizeof(sd->birthdate));
  2018. safestrncpy(sd->pincode, (const char*)RFIFOP(fd,63), sizeof(sd->pincode));
  2019. sd->pincode_change = (time_t)RFIFOL(fd,68);
  2020. ARR_FIND( 0, ARRAYLENGTH(server), server_id, server[server_id].fd > 0 && server[server_id].map[0] );
  2021. // continued from char_auth_ok...
  2022. if( server_id == ARRAYLENGTH(server) || //server not online, bugreport:2359
  2023. (max_connect_user == 0 && sd->group_id != gm_allow_group) ||
  2024. ( max_connect_user > 0 && count_users() >= max_connect_user && sd->group_id != gm_allow_group ) ) {
  2025. // refuse connection (over populated)
  2026. WFIFOHEAD(i,3);
  2027. WFIFOW(i,0) = 0x6c;
  2028. WFIFOW(i,2) = 0;
  2029. WFIFOSET(i,3);
  2030. } else {
  2031. // send characters to player
  2032. mmo_char_send(i, sd);
  2033. #if PACKETVER >= 20110309
  2034. if( pincode_enabled ){
  2035. // PIN code system enabled
  2036. if( strlen( sd->pincode ) <= 0 ){
  2037. // No PIN code has been set yet
  2038. if( pincode_force ){
  2039. pincode_sendstate( i, sd, PINCODE_NEW );
  2040. }else{
  2041. pincode_sendstate( i, sd, PINCODE_PASSED );
  2042. }
  2043. }else{
  2044. if( !pincode_changetime || ( sd->pincode_change + pincode_changetime ) > time(NULL) ){
  2045. struct online_char_data* node = (struct online_char_data*)idb_get( online_char_db, sd->account_id );
  2046. if( node != NULL && node->pincode_success ){
  2047. // User has already passed the check
  2048. pincode_sendstate( i, sd, PINCODE_PASSED );
  2049. }else{
  2050. // Ask user for his PIN code
  2051. pincode_sendstate( i, sd, PINCODE_ASK );
  2052. }
  2053. }else{
  2054. // User hasnt changed his PIN code too long
  2055. pincode_sendstate( i, sd, PINCODE_EXPIRED );
  2056. }
  2057. }
  2058. }else{
  2059. // PIN code system disabled
  2060. pincode_sendstate( i, sd, PINCODE_OK );
  2061. }
  2062. #endif
  2063. }
  2064. }
  2065. RFIFOSKIP(fd,72);
  2066. break;
  2067. // login-server alive packet
  2068. case 0x2718:
  2069. if (RFIFOREST(fd) < 2)
  2070. return 0;
  2071. RFIFOSKIP(fd,2);
  2072. session[fd]->flag.ping = 0;
  2073. break;
  2074. // changesex reply
  2075. case 0x2723:
  2076. if (RFIFOREST(fd) < 7)
  2077. return 0;
  2078. {
  2079. unsigned char buf[7];
  2080. int acc = RFIFOL(fd,2);
  2081. int sex = RFIFOB(fd,6);
  2082. RFIFOSKIP(fd,7);
  2083. if( acc > 0 )
  2084. {// TODO: Is this even possible?
  2085. int char_id[MAX_CHARS];
  2086. int class_[MAX_CHARS];
  2087. int guild_id[MAX_CHARS];
  2088. int num;
  2089. char* data;
  2090. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  2091. if( node != NULL )
  2092. node->sex = sex;
  2093. // get characters
  2094. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`class`,`guild_id` FROM `%s` WHERE `account_id` = '%d'", char_db, acc) )
  2095. Sql_ShowDebug(sql_handle);
  2096. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2097. {
  2098. Sql_GetData(sql_handle, 0, &data, NULL); char_id[i] = atoi(data);
  2099. Sql_GetData(sql_handle, 1, &data, NULL); class_[i] = atoi(data);
  2100. Sql_GetData(sql_handle, 2, &data, NULL); guild_id[i] = atoi(data);
  2101. }
  2102. num = i;
  2103. for( i = 0; i < num; ++i )
  2104. {
  2105. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER ||
  2106. class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY ||
  2107. class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER ||
  2108. class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER ||
  2109. class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T ||
  2110. class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER ||
  2111. class_[i] == JOB_KAGEROU || class_[i] == JOB_OBORO )
  2112. {
  2113. // job modification
  2114. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER )
  2115. class_[i] = (sex ? JOB_BARD : JOB_DANCER);
  2116. else if( class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY )
  2117. class_[i] = (sex ? JOB_CLOWN : JOB_GYPSY);
  2118. else if( class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER )
  2119. class_[i] = (sex ? JOB_BABY_BARD : JOB_BABY_DANCER);
  2120. else if( class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER )
  2121. class_[i] = (sex ? JOB_MINSTREL : JOB_WANDERER);
  2122. else if( class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T )
  2123. class_[i] = (sex ? JOB_MINSTREL_T : JOB_WANDERER_T);
  2124. else if( class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER )
  2125. class_[i] = (sex ? JOB_BABY_MINSTREL : JOB_BABY_WANDERER);
  2126. else if( class_[i] == JOB_KAGEROU || class_[i] == JOB_OBORO )
  2127. class_[i] = (sex ? JOB_KAGEROU : JOB_OBORO);
  2128. }
  2129. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d', `weapon`='0', `shield`='0', `head_top`='0', `head_mid`='0', `head_bottom`='0' WHERE `char_id`='%d'", char_db, class_[i], char_id[i]) )
  2130. Sql_ShowDebug(sql_handle);
  2131. if( guild_id[i] )// If there is a guild, update the guild_member data [Skotlex]
  2132. inter_guild_sex_changed(guild_id[i], acc, char_id[i], sex);
  2133. }
  2134. Sql_FreeResult(sql_handle);
  2135. // disconnect player if online on char-server
  2136. disconnect_player(acc);
  2137. }
  2138. // notify all mapservers about this change
  2139. WBUFW(buf,0) = 0x2b0d;
  2140. WBUFL(buf,2) = acc;
  2141. WBUFB(buf,6) = sex;
  2142. mapif_sendall(buf, 7);
  2143. }
  2144. break;
  2145. // reply to an account_reg2 registry request
  2146. case 0x2729:
  2147. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2148. return 0;
  2149. { //Receive account_reg2 registry, forward to map servers.
  2150. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  2151. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  2152. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  2153. mapif_sendall(buf, WBUFW(buf,2));
  2154. RFIFOSKIP(fd, RFIFOW(fd,2));
  2155. }
  2156. break;
  2157. // State change of account/ban notification (from login-server)
  2158. case 0x2731:
  2159. if (RFIFOREST(fd) < 11)
  2160. return 0;
  2161. { // send to all map-servers to disconnect the player
  2162. unsigned char buf[11];
  2163. WBUFW(buf,0) = 0x2b14;
  2164. WBUFL(buf,2) = RFIFOL(fd,2);
  2165. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  2166. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  2167. mapif_sendall(buf, 11);
  2168. }
  2169. // disconnect player if online on char-server
  2170. disconnect_player(RFIFOL(fd,2));
  2171. RFIFOSKIP(fd,11);
  2172. break;
  2173. // Login server request to kick a character out. [Skotlex]
  2174. case 0x2734:
  2175. if (RFIFOREST(fd) < 6)
  2176. return 0;
  2177. {
  2178. int aid = RFIFOL(fd,2);
  2179. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  2180. RFIFOSKIP(fd,6);
  2181. if( character != NULL )
  2182. {// account is already marked as online!
  2183. if( character->server > -1 )
  2184. { //Kick it from the map server it is on.
  2185. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2186. if (character->waiting_disconnect == INVALID_TIMER)
  2187. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  2188. }
  2189. else
  2190. {// Manual kick from char server.
  2191. struct char_session_data *tsd;
  2192. int i;
  2193. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  2194. if( i < fd_max )
  2195. {
  2196. WFIFOHEAD(i,3);
  2197. WFIFOW(i,0) = 0x81;
  2198. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  2199. WFIFOSET(i,3);
  2200. set_eof(i);
  2201. }
  2202. else // still moving to the map-server
  2203. set_char_offline(-1, aid);
  2204. }
  2205. }
  2206. idb_remove(auth_db, aid);// reject auth attempts from map-server
  2207. }
  2208. break;
  2209. // ip address update signal from login server
  2210. case 0x2735:
  2211. {
  2212. unsigned char buf[2];
  2213. uint32 new_ip = 0;
  2214. WBUFW(buf,0) = 0x2b1e;
  2215. mapif_sendall(buf, 2);
  2216. new_ip = host2ip(login_ip_str);
  2217. if (new_ip && new_ip != login_ip)
  2218. login_ip = new_ip; //Update login ip, too.
  2219. new_ip = host2ip(char_ip_str);
  2220. if (new_ip && new_ip != char_ip)
  2221. { //Update ip.
  2222. char_ip = new_ip;
  2223. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  2224. // notify login server about the change
  2225. WFIFOHEAD(fd,6);
  2226. WFIFOW(fd,0) = 0x2736;
  2227. WFIFOL(fd,2) = htonl(char_ip);
  2228. WFIFOSET(fd,6);
  2229. }
  2230. RFIFOSKIP(fd,2);
  2231. }
  2232. break;
  2233. default:
  2234. ShowError("Unknown packet 0x%04x received from login-server, disconnecting.\n", command);
  2235. set_eof(fd);
  2236. return 0;
  2237. }
  2238. }
  2239. RFIFOFLUSH(fd);
  2240. return 0;
  2241. }
  2242. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data);
  2243. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data);
  2244. void do_init_loginif(void)
  2245. {
  2246. // establish char-login connection if not present
  2247. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  2248. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  2249. // send a list of all online account IDs to login server
  2250. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  2251. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour
  2252. }
  2253. void do_final_loginif(void)
  2254. {
  2255. if( login_fd != -1 )
  2256. {
  2257. do_close(login_fd);
  2258. login_fd = -1;
  2259. }
  2260. }
  2261. int request_accreg2(int account_id, int char_id)
  2262. {
  2263. if (login_fd > 0) {
  2264. WFIFOHEAD(login_fd,10);
  2265. WFIFOW(login_fd,0) = 0x272e;
  2266. WFIFOL(login_fd,2) = account_id;
  2267. WFIFOL(login_fd,6) = char_id;
  2268. WFIFOSET(login_fd,10);
  2269. return 1;
  2270. }
  2271. return 0;
  2272. }
  2273. //Send packet forward to login-server for account saving
  2274. int save_accreg2(unsigned char* buf, int len)
  2275. {
  2276. if (login_fd > 0) {
  2277. WFIFOHEAD(login_fd,len+4);
  2278. memcpy(WFIFOP(login_fd,4), buf, len);
  2279. WFIFOW(login_fd,0) = 0x2728;
  2280. WFIFOW(login_fd,2) = len+4;
  2281. WFIFOSET(login_fd,len+4);
  2282. return 1;
  2283. }
  2284. return 0;
  2285. }
  2286. void char_read_fame_list(void)
  2287. {
  2288. int i;
  2289. char* data;
  2290. size_t len;
  2291. // Empty ranking lists
  2292. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  2293. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  2294. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  2295. // Build Blacksmith ranking list
  2296. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, fame_list_size_smith) )
  2297. Sql_ShowDebug(sql_handle);
  2298. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2299. {
  2300. // char_id
  2301. Sql_GetData(sql_handle, 0, &data, NULL);
  2302. smith_fame_list[i].id = atoi(data);
  2303. // fame
  2304. Sql_GetData(sql_handle, 1, &data, &len);
  2305. smith_fame_list[i].fame = atoi(data);
  2306. // name
  2307. Sql_GetData(sql_handle, 2, &data, &len);
  2308. memcpy(smith_fame_list[i].name, data, min(len, NAME_LENGTH));
  2309. }
  2310. // Build Alchemist ranking list
  2311. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, fame_list_size_chemist) )
  2312. Sql_ShowDebug(sql_handle);
  2313. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2314. {
  2315. // char_id
  2316. Sql_GetData(sql_handle, 0, &data, NULL);
  2317. chemist_fame_list[i].id = atoi(data);
  2318. // fame
  2319. Sql_GetData(sql_handle, 1, &data, &len);
  2320. chemist_fame_list[i].fame = atoi(data);
  2321. // name
  2322. Sql_GetData(sql_handle, 2, &data, &len);
  2323. memcpy(chemist_fame_list[i].name, data, min(len, NAME_LENGTH));
  2324. }
  2325. // Build Taekwon ranking list
  2326. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_TAEKWON, fame_list_size_taekwon) )
  2327. Sql_ShowDebug(sql_handle);
  2328. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2329. {
  2330. // char_id
  2331. Sql_GetData(sql_handle, 0, &data, NULL);
  2332. taekwon_fame_list[i].id = atoi(data);
  2333. // fame
  2334. Sql_GetData(sql_handle, 1, &data, &len);
  2335. taekwon_fame_list[i].fame = atoi(data);
  2336. // name
  2337. Sql_GetData(sql_handle, 2, &data, &len);
  2338. memcpy(taekwon_fame_list[i].name, data, min(len, NAME_LENGTH));
  2339. }
  2340. Sql_FreeResult(sql_handle);
  2341. }
  2342. // Send map-servers the fame ranking lists
  2343. int char_send_fame_list(int fd)
  2344. {
  2345. int i, len = 8;
  2346. unsigned char buf[32000];
  2347. WBUFW(buf,0) = 0x2b1b;
  2348. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  2349. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  2350. len += sizeof(struct fame_list);
  2351. }
  2352. // add blacksmith's block length
  2353. WBUFW(buf, 6) = len;
  2354. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  2355. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  2356. len += sizeof(struct fame_list);
  2357. }
  2358. // add alchemist's block length
  2359. WBUFW(buf, 4) = len;
  2360. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  2361. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  2362. len += sizeof(struct fame_list);
  2363. }
  2364. // add total packet length
  2365. WBUFW(buf, 2) = len;
  2366. if (fd != -1)
  2367. mapif_send(fd, buf, len);
  2368. else
  2369. mapif_sendall(buf, len);
  2370. return 0;
  2371. }
  2372. void char_update_fame_list(int type, int index, int fame)
  2373. {
  2374. unsigned char buf[8];
  2375. WBUFW(buf,0) = 0x2b22;
  2376. WBUFB(buf,2) = type;
  2377. WBUFB(buf,3) = index;
  2378. WBUFL(buf,4) = fame;
  2379. mapif_sendall(buf, 8);
  2380. }
  2381. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  2382. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  2383. int char_loadName(int char_id, char* name)
  2384. {
  2385. char* data;
  2386. size_t len;
  2387. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  2388. Sql_ShowDebug(sql_handle);
  2389. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  2390. {
  2391. Sql_GetData(sql_handle, 0, &data, &len);
  2392. safestrncpy(name, data, NAME_LENGTH);
  2393. return 1;
  2394. }
  2395. else
  2396. {
  2397. safestrncpy(name, unknown_char_name, NAME_LENGTH);
  2398. }
  2399. return 0;
  2400. }
  2401. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  2402. /// Initializes a server structure.
  2403. void mapif_server_init(int id)
  2404. {
  2405. memset(&server[id], 0, sizeof(server[id]));
  2406. server[id].fd = -1;
  2407. }
  2408. /// Destroys a server structure.
  2409. void mapif_server_destroy(int id)
  2410. {
  2411. if( server[id].fd == -1 )
  2412. {
  2413. do_close(server[id].fd);
  2414. server[id].fd = -1;
  2415. }
  2416. }
  2417. /// Resets all the data related to a server.
  2418. void mapif_server_reset(int id)
  2419. {
  2420. int i,j;
  2421. unsigned char buf[16384];
  2422. int fd = server[id].fd;
  2423. //Notify other map servers that this one is gone. [Skotlex]
  2424. WBUFW(buf,0) = 0x2b20;
  2425. WBUFL(buf,4) = htonl(server[id].ip);
  2426. WBUFW(buf,8) = htons(server[id].port);
  2427. j = 0;
  2428. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2429. if (server[id].map[i])
  2430. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2431. if (j > 0) {
  2432. WBUFW(buf,2) = j * 4 + 10;
  2433. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2434. }
  2435. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `index`='%d'", ragsrvinfo_db, server[id].fd) )
  2436. Sql_ShowDebug(sql_handle);
  2437. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2438. mapif_server_destroy(id);
  2439. mapif_server_init(id);
  2440. }
  2441. /// Called when the connection to a Map Server is disconnected.
  2442. void mapif_on_disconnect(int id)
  2443. {
  2444. ShowStatus("Map-server #%d has disconnected.\n", id);
  2445. mapif_server_reset(id);
  2446. }
  2447. int parse_frommap(int fd)
  2448. {
  2449. int i, j;
  2450. int id;
  2451. ARR_FIND( 0, ARRAYLENGTH(server), id, server[id].fd == fd );
  2452. if( id == ARRAYLENGTH(server) )
  2453. {// not a map server
  2454. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  2455. do_close(fd);
  2456. return 0;
  2457. }
  2458. if( session[fd]->flag.eof )
  2459. {
  2460. do_close(fd);
  2461. server[id].fd = -1;
  2462. mapif_on_disconnect(id);
  2463. return 0;
  2464. }
  2465. while(RFIFOREST(fd) >= 2){
  2466. switch(RFIFOW(fd,0)){
  2467. case 0x2afa: // Receiving map names list from the map-server
  2468. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2469. return 0;
  2470. memset(server[id].map, 0, sizeof(server[id].map));
  2471. j = 0;
  2472. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2473. server[id].map[j] = RFIFOW(fd,i);
  2474. j++;
  2475. }
  2476. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2477. id, j, CONVIP(server[id].ip), server[id].port);
  2478. ShowStatus("Map-server %d loading complete.\n", id);
  2479. // send name for wisp to player
  2480. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  2481. WFIFOW(fd,0) = 0x2afb;
  2482. WFIFOB(fd,2) = 0;
  2483. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  2484. WFIFOSET(fd,3+NAME_LENGTH);
  2485. char_send_fame_list(fd); //Send fame list.
  2486. {
  2487. unsigned char buf[16384];
  2488. int x;
  2489. if (j == 0) {
  2490. ShowWarning("Map-server %d has NO maps.\n", id);
  2491. } else {
  2492. // Transmitting maps information to the other map-servers
  2493. WBUFW(buf,0) = 0x2b04;
  2494. WBUFW(buf,2) = j * 4 + 10;
  2495. WBUFL(buf,4) = htonl(server[id].ip);
  2496. WBUFW(buf,8) = htons(server[id].port);
  2497. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2498. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2499. }
  2500. // Transmitting the maps of the other map-servers to the new map-server
  2501. for(x = 0; x < ARRAYLENGTH(server); x++) {
  2502. if (server[x].fd > 0 && x != id) {
  2503. WFIFOHEAD(fd,10 +4*ARRAYLENGTH(server[x].map));
  2504. WFIFOW(fd,0) = 0x2b04;
  2505. WFIFOL(fd,4) = htonl(server[x].ip);
  2506. WFIFOW(fd,8) = htons(server[x].port);
  2507. j = 0;
  2508. for(i = 0; i < ARRAYLENGTH(server[x].map); i++)
  2509. if (server[x].map[i])
  2510. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2511. if (j > 0) {
  2512. WFIFOW(fd,2) = j * 4 + 10;
  2513. WFIFOSET(fd,WFIFOW(fd,2));
  2514. }
  2515. }
  2516. }
  2517. }
  2518. RFIFOSKIP(fd,RFIFOW(fd,2));
  2519. break;
  2520. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  2521. if (RFIFOREST(fd) < 10)
  2522. return 0;
  2523. {
  2524. #ifdef ENABLE_SC_SAVING
  2525. int aid, cid;
  2526. aid = RFIFOL(fd,2);
  2527. cid = RFIFOL(fd,6);
  2528. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2529. scdata_db, aid, cid) )
  2530. {
  2531. Sql_ShowDebug(sql_handle);
  2532. break;
  2533. }
  2534. if( Sql_NumRows(sql_handle) > 0 )
  2535. {
  2536. struct status_change_data scdata;
  2537. int count;
  2538. char* data;
  2539. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  2540. WFIFOW(fd,0) = 0x2b1d;
  2541. WFIFOL(fd,4) = aid;
  2542. WFIFOL(fd,8) = cid;
  2543. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  2544. {
  2545. Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data);
  2546. Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = atoi(data);
  2547. Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data);
  2548. Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data);
  2549. Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data);
  2550. Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data);
  2551. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  2552. }
  2553. if (count >= 50)
  2554. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  2555. if (count > 0)
  2556. {
  2557. WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data);
  2558. WFIFOW(fd,12) = count;
  2559. WFIFOSET(fd,WFIFOW(fd,2));
  2560. //Clear the data once loaded.
  2561. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid) )
  2562. Sql_ShowDebug(sql_handle);
  2563. }
  2564. }
  2565. Sql_FreeResult(sql_handle);
  2566. #endif
  2567. RFIFOSKIP(fd, 10);
  2568. }
  2569. break;
  2570. case 0x2afe: //set MAP user count
  2571. if (RFIFOREST(fd) < 4)
  2572. return 0;
  2573. if (RFIFOW(fd,2) != server[id].users) {
  2574. server[id].users = RFIFOW(fd,2);
  2575. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2576. }
  2577. RFIFOSKIP(fd, 4);
  2578. break;
  2579. case 0x2aff: //set MAP users
  2580. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2581. return 0;
  2582. {
  2583. //TODO: When data mismatches memory, update guild/party online/offline states.
  2584. int aid, cid;
  2585. struct online_char_data* character;
  2586. server[id].users = RFIFOW(fd,4);
  2587. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2588. for(i = 0; i < server[id].users; i++) {
  2589. aid = RFIFOL(fd,6+i*8);
  2590. cid = RFIFOL(fd,6+i*8+4);
  2591. character = idb_ensure(online_char_db, aid, create_online_char_data);
  2592. if( character->server > -1 && character->server != id )
  2593. {
  2594. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2595. character->account_id, character->char_id, character->server, id, aid, cid);
  2596. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2597. }
  2598. character->server = id;
  2599. character->char_id = cid;
  2600. }
  2601. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2602. RFIFOSKIP(fd,RFIFOW(fd,2));
  2603. }
  2604. break;
  2605. case 0x2b01: // Receive character data from map-server for saving
  2606. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2607. return 0;
  2608. {
  2609. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2);
  2610. struct online_char_data* character;
  2611. if (size - 13 != sizeof(struct mmo_charstatus))
  2612. {
  2613. ShowError("parse_from_map (save-char): Size mismatch! %d != %d\n", size-13, sizeof(struct mmo_charstatus));
  2614. RFIFOSKIP(fd,size);
  2615. break;
  2616. }
  2617. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  2618. if (RFIFOB(fd,12) || RFIFOB(fd,13) || (
  2619. (character = (struct online_char_data*)idb_get(online_char_db, aid)) != NULL &&
  2620. character->char_id == cid))
  2621. {
  2622. struct mmo_charstatus char_dat;
  2623. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2624. mmo_char_tosql(cid, &char_dat);
  2625. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  2626. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  2627. set_char_online(id, cid, aid);
  2628. }
  2629. if (RFIFOB(fd,12))
  2630. { //Flag, set character offline after saving. [Skotlex]
  2631. set_char_offline(cid, aid);
  2632. WFIFOHEAD(fd,10);
  2633. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2634. WFIFOL(fd,2) = aid;
  2635. WFIFOL(fd,6) = cid;
  2636. WFIFOSET(fd,10);
  2637. }
  2638. RFIFOSKIP(fd,size);
  2639. }
  2640. break;
  2641. case 0x2b02: // req char selection
  2642. if( RFIFOREST(fd) < 18 )
  2643. return 0;
  2644. else{
  2645. int account_id = RFIFOL(fd,2);
  2646. uint32 login_id1 = RFIFOL(fd,6);
  2647. uint32 login_id2 = RFIFOL(fd,10);
  2648. uint32 ip = RFIFOL(fd,14);
  2649. RFIFOSKIP(fd,18);
  2650. if( runflag != CHARSERVER_ST_RUNNING ){
  2651. WFIFOHEAD(fd,7);
  2652. WFIFOW(fd,0) = 0x2b03;
  2653. WFIFOL(fd,2) = account_id;
  2654. WFIFOB(fd,6) = 0;// not ok
  2655. WFIFOSET(fd,7);
  2656. }else{
  2657. struct auth_node* node;
  2658. // create temporary auth entry
  2659. CREATE(node, struct auth_node, 1);
  2660. node->account_id = account_id;
  2661. node->char_id = 0;
  2662. node->login_id1 = login_id1;
  2663. node->login_id2 = login_id2;
  2664. //node->sex = 0;
  2665. node->ip = ntohl(ip);
  2666. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2667. //node->gmlevel = 0;
  2668. idb_put(auth_db, account_id, node);
  2669. //Set char to "@ char select" in online db [Kevin]
  2670. set_char_charselect(account_id);
  2671. {
  2672. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, account_id);
  2673. if( character != NULL ){
  2674. character->pincode_success = true;
  2675. }
  2676. }
  2677. WFIFOHEAD(fd,7);
  2678. WFIFOW(fd,0) = 0x2b03;
  2679. WFIFOL(fd,2) = account_id;
  2680. WFIFOB(fd,6) = 1;// ok
  2681. WFIFOSET(fd,7);
  2682. }
  2683. }
  2684. break;
  2685. case 0x2b05: // request "change map server"
  2686. if (RFIFOREST(fd) < 39)
  2687. return 0;
  2688. {
  2689. int map_id, map_fd = -1;
  2690. struct mmo_charstatus* char_data;
  2691. struct mmo_charstatus char_dat;
  2692. map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2693. if (map_id >= 0)
  2694. map_fd = server[map_id].fd;
  2695. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2696. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2697. if (char_data == NULL) { //Really shouldn't happen.
  2698. mmo_char_fromsql(RFIFOL(fd,14), &char_dat, true);
  2699. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2700. }
  2701. if( runflag == CHARSERVER_ST_RUNNING &&
  2702. session_isActive(map_fd) &&
  2703. char_data )
  2704. { //Send the map server the auth of this player.
  2705. struct online_char_data* data;
  2706. struct auth_node* node;
  2707. //Update the "last map" as this is where the player must be spawned on the new map server.
  2708. char_data->last_point.map = RFIFOW(fd,18);
  2709. char_data->last_point.x = RFIFOW(fd,20);
  2710. char_data->last_point.y = RFIFOW(fd,22);
  2711. char_data->sex = RFIFOB(fd,30);
  2712. // create temporary auth entry
  2713. CREATE(node, struct auth_node, 1);
  2714. node->account_id = RFIFOL(fd,2);
  2715. node->char_id = RFIFOL(fd,14);
  2716. node->login_id1 = RFIFOL(fd,6);
  2717. node->login_id2 = RFIFOL(fd,10);
  2718. node->sex = RFIFOB(fd,30);
  2719. node->expiration_time = 0; // FIXME (this thing isn't really supported we could as well purge it instead of fixing)
  2720. node->ip = ntohl(RFIFOL(fd,31));
  2721. node->group_id = RFIFOL(fd,35);
  2722. node->changing_mapservers = 1;
  2723. idb_put(auth_db, RFIFOL(fd,2), node);
  2724. data = idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2725. data->char_id = char_data->char_id;
  2726. data->server = map_id; //Update server where char is.
  2727. //Reply with an ack.
  2728. WFIFOHEAD(fd,30);
  2729. WFIFOW(fd,0) = 0x2b06;
  2730. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2731. WFIFOSET(fd,30);
  2732. } else { //Reply with nak
  2733. WFIFOHEAD(fd,30);
  2734. WFIFOW(fd,0) = 0x2b06;
  2735. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2736. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2737. WFIFOSET(fd,30);
  2738. }
  2739. RFIFOSKIP(fd,39);
  2740. }
  2741. break;
  2742. case 0x2b07: // Remove RFIFOL(fd,6) (friend_id) from RFIFOL(fd,2) (char_id) friend list [Ind]
  2743. if (RFIFOREST(fd) < 10)
  2744. return 0;
  2745. {
  2746. int char_id, friend_id;
  2747. char_id = RFIFOL(fd,2);
  2748. friend_id = RFIFOL(fd,6);
  2749. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `friend_id`='%d' LIMIT 1",
  2750. friend_db, char_id, friend_id) ) {
  2751. Sql_ShowDebug(sql_handle);
  2752. break;
  2753. }
  2754. RFIFOSKIP(fd,10);
  2755. }
  2756. break;
  2757. case 0x2b08: // char name request
  2758. if (RFIFOREST(fd) < 6)
  2759. return 0;
  2760. WFIFOHEAD(fd,30);
  2761. WFIFOW(fd,0) = 0x2b09;
  2762. WFIFOL(fd,2) = RFIFOL(fd,2);
  2763. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2764. WFIFOSET(fd,30);
  2765. RFIFOSKIP(fd,6);
  2766. break;
  2767. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2768. if (RFIFOREST(fd) < 86)
  2769. return 0;
  2770. if (login_fd > 0) { // don't send request if no login-server
  2771. WFIFOHEAD(login_fd,86);
  2772. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2773. WFIFOW(login_fd,0) = 0x2722;
  2774. WFIFOSET(login_fd,86);
  2775. }
  2776. RFIFOSKIP(fd, 86);
  2777. break;
  2778. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  2779. if (RFIFOREST(fd) < 44)
  2780. return 0;
  2781. {
  2782. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  2783. char esc_name[NAME_LENGTH*2+1];
  2784. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  2785. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  2786. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2787. short year = RFIFOW(fd,32);
  2788. short month = RFIFOW(fd,34);
  2789. short day = RFIFOW(fd,36);
  2790. short hour = RFIFOW(fd,38);
  2791. short minute = RFIFOW(fd,40);
  2792. short second = RFIFOW(fd,42);
  2793. RFIFOSKIP(fd,44);
  2794. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  2795. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
  2796. Sql_ShowDebug(sql_handle);
  2797. else
  2798. if( Sql_NumRows(sql_handle) == 0 )
  2799. {
  2800. result = 1; // 1-player not found
  2801. }
  2802. else
  2803. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  2804. Sql_ShowDebug(sql_handle);
  2805. //FIXME: set proper result value?
  2806. else
  2807. {
  2808. char name[NAME_LENGTH];
  2809. int account_id;
  2810. char* data;
  2811. Sql_GetData(sql_handle, 0, &data, NULL); account_id = atoi(data);
  2812. Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(name, data, sizeof(name));
  2813. if( login_fd <= 0 )
  2814. result = 3; // 3-login-server offline
  2815. //FIXME: need to move this check to login server [ultramage]
  2816. // else
  2817. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  2818. // result = 2; // 2-gm level too low
  2819. else
  2820. switch( type ) {
  2821. case 1: // block
  2822. WFIFOHEAD(login_fd,10);
  2823. WFIFOW(login_fd,0) = 0x2724;
  2824. WFIFOL(login_fd,2) = account_id;
  2825. WFIFOL(login_fd,6) = 5; // new account status
  2826. WFIFOSET(login_fd,10);
  2827. break;
  2828. case 2: // ban
  2829. WFIFOHEAD(login_fd,18);
  2830. WFIFOW(login_fd, 0) = 0x2725;
  2831. WFIFOL(login_fd, 2) = account_id;
  2832. WFIFOW(login_fd, 6) = year;
  2833. WFIFOW(login_fd, 8) = month;
  2834. WFIFOW(login_fd,10) = day;
  2835. WFIFOW(login_fd,12) = hour;
  2836. WFIFOW(login_fd,14) = minute;
  2837. WFIFOW(login_fd,16) = second;
  2838. WFIFOSET(login_fd,18);
  2839. break;
  2840. case 3: // unblock
  2841. WFIFOHEAD(login_fd,10);
  2842. WFIFOW(login_fd,0) = 0x2724;
  2843. WFIFOL(login_fd,2) = account_id;
  2844. WFIFOL(login_fd,6) = 0; // new account status
  2845. WFIFOSET(login_fd,10);
  2846. break;
  2847. case 4: // unban
  2848. WFIFOHEAD(login_fd,6);
  2849. WFIFOW(login_fd,0) = 0x272a;
  2850. WFIFOL(login_fd,2) = account_id;
  2851. WFIFOSET(login_fd,6);
  2852. break;
  2853. case 5: // changesex
  2854. WFIFOHEAD(login_fd,6);
  2855. WFIFOW(login_fd,0) = 0x2727;
  2856. WFIFOL(login_fd,2) = account_id;
  2857. WFIFOSET(login_fd,6);
  2858. break;
  2859. }
  2860. }
  2861. Sql_FreeResult(sql_handle);
  2862. // send answer if a player ask, not if the server ask
  2863. if( acc != -1 && type != 5) { // Don't send answer for changesex
  2864. WFIFOHEAD(fd,34);
  2865. WFIFOW(fd, 0) = 0x2b0f;
  2866. WFIFOL(fd, 2) = acc;
  2867. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  2868. WFIFOW(fd,30) = type;
  2869. WFIFOW(fd,32) = result;
  2870. WFIFOSET(fd,34);
  2871. }
  2872. }
  2873. break;
  2874. case 0x2b10: // Update and send fame ranking list
  2875. if (RFIFOREST(fd) < 11)
  2876. return 0;
  2877. {
  2878. int cid = RFIFOL(fd, 2);
  2879. int fame = RFIFOL(fd, 6);
  2880. char type = RFIFOB(fd, 10);
  2881. int size;
  2882. struct fame_list* list;
  2883. int player_pos;
  2884. int fame_pos;
  2885. switch(type)
  2886. {
  2887. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  2888. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  2889. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  2890. default: size = 0; list = NULL; break;
  2891. }
  2892. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  2893. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  2894. if( player_pos == size && fame_pos == size )
  2895. ;// not on list and not enough fame to get on it
  2896. else if( fame_pos == player_pos )
  2897. {// same position
  2898. list[player_pos].fame = fame;
  2899. char_update_fame_list(type, player_pos, fame);
  2900. }
  2901. else
  2902. {// move in the list
  2903. if( player_pos == size )
  2904. {// new ranker - not in the list
  2905. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  2906. list[fame_pos].id = cid;
  2907. list[fame_pos].fame = fame;
  2908. char_loadName(cid, list[fame_pos].name);
  2909. }
  2910. else
  2911. {// already in the list
  2912. if( fame_pos == size )
  2913. --fame_pos;// move to the end of the list
  2914. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  2915. list[fame_pos].fame = fame;
  2916. }
  2917. char_send_fame_list(-1);
  2918. }
  2919. RFIFOSKIP(fd,11);
  2920. }
  2921. break;
  2922. // Divorce chars
  2923. case 0x2b11:
  2924. if( RFIFOREST(fd) < 10 )
  2925. return 0;
  2926. divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  2927. RFIFOSKIP(fd,10);
  2928. break;
  2929. case 0x2b16: // Receive rates [Wizputer]
  2930. if( RFIFOREST(fd) < 14 )
  2931. return 0;
  2932. {
  2933. char esc_server_name[sizeof(server_name)*2+1];
  2934. Sql_EscapeString(sql_handle, esc_server_name, server_name);
  2935. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d'",
  2936. ragsrvinfo_db, fd, esc_server_name, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)) )
  2937. Sql_ShowDebug(sql_handle);
  2938. RFIFOSKIP(fd,14);
  2939. }
  2940. break;
  2941. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  2942. if (RFIFOREST(fd) < 6)
  2943. return 0;
  2944. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2945. RFIFOSKIP(fd,10);
  2946. break;
  2947. case 0x2b18: // Reset all chars to offline [Wizputer]
  2948. set_all_offline(id);
  2949. RFIFOSKIP(fd,2);
  2950. break;
  2951. case 0x2b19: // Character set online [Wizputer]
  2952. if (RFIFOREST(fd) < 10)
  2953. return 0;
  2954. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2955. RFIFOSKIP(fd,10);
  2956. break;
  2957. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  2958. if (RFIFOREST(fd) < 2)
  2959. return 0;
  2960. char_read_fame_list();
  2961. char_send_fame_list(-1);
  2962. RFIFOSKIP(fd,2);
  2963. break;
  2964. case 0x2b1c: //Request to save status change data. [Skotlex]
  2965. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2966. return 0;
  2967. {
  2968. #ifdef ENABLE_SC_SAVING
  2969. int count, aid, cid;
  2970. aid = RFIFOL(fd, 4);
  2971. cid = RFIFOL(fd, 8);
  2972. count = RFIFOW(fd, 12);
  2973. if( count > 0 )
  2974. {
  2975. struct status_change_data data;
  2976. StringBuf buf;
  2977. int i;
  2978. StringBuf_Init(&buf);
  2979. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  2980. for( i = 0; i < count; ++i )
  2981. {
  2982. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2983. if( i > 0 )
  2984. StringBuf_AppendStr(&buf, ", ");
  2985. StringBuf_Printf(&buf, "('%d','%d','%hu','%d','%d','%d','%d','%d')", aid, cid,
  2986. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  2987. }
  2988. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  2989. Sql_ShowDebug(sql_handle);
  2990. StringBuf_Destroy(&buf);
  2991. }
  2992. #endif
  2993. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2994. }
  2995. break;
  2996. case 0x2b23: // map-server alive packet
  2997. WFIFOHEAD(fd,2);
  2998. WFIFOW(fd,0) = 0x2b24;
  2999. WFIFOSET(fd,2);
  3000. RFIFOSKIP(fd,2);
  3001. break;
  3002. case 0x2b26: // auth request from map-server
  3003. if (RFIFOREST(fd) < 19)
  3004. return 0;
  3005. {
  3006. int account_id;
  3007. int char_id;
  3008. int login_id1;
  3009. char sex;
  3010. uint32 ip;
  3011. struct auth_node* node;
  3012. struct mmo_charstatus* cd;
  3013. struct mmo_charstatus char_dat;
  3014. account_id = RFIFOL(fd,2);
  3015. char_id = RFIFOL(fd,6);
  3016. login_id1 = RFIFOL(fd,10);
  3017. sex = RFIFOB(fd,14);
  3018. ip = ntohl(RFIFOL(fd,15));
  3019. RFIFOSKIP(fd,19);
  3020. node = (struct auth_node*)idb_get(auth_db, account_id);
  3021. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  3022. if( cd == NULL )
  3023. { //Really shouldn't happen.
  3024. mmo_char_fromsql(char_id, &char_dat, true);
  3025. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  3026. }
  3027. if( runflag == CHARSERVER_ST_RUNNING &&
  3028. cd != NULL &&
  3029. node != NULL &&
  3030. node->account_id == account_id &&
  3031. node->char_id == char_id &&
  3032. node->login_id1 == login_id1 &&
  3033. node->sex == sex /*&&
  3034. node->ip == ip*/ )
  3035. {// auth ok
  3036. cd->sex = sex;
  3037. WFIFOHEAD(fd,25 + sizeof(struct mmo_charstatus));
  3038. WFIFOW(fd,0) = 0x2afd;
  3039. WFIFOW(fd,2) = 25 + sizeof(struct mmo_charstatus);
  3040. WFIFOL(fd,4) = account_id;
  3041. WFIFOL(fd,8) = node->login_id1;
  3042. WFIFOL(fd,12) = node->login_id2;
  3043. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  3044. WFIFOL(fd,20) = node->group_id;
  3045. WFIFOB(fd,24) = node->changing_mapservers;
  3046. memcpy(WFIFOP(fd,25), cd, sizeof(struct mmo_charstatus));
  3047. WFIFOSET(fd, WFIFOW(fd,2));
  3048. // only use the auth once and mark user online
  3049. idb_remove(auth_db, account_id);
  3050. set_char_online(id, char_id, account_id);
  3051. }
  3052. else
  3053. {// auth failed
  3054. WFIFOHEAD(fd,19);
  3055. WFIFOW(fd,0) = 0x2b27;
  3056. WFIFOL(fd,2) = account_id;
  3057. WFIFOL(fd,6) = char_id;
  3058. WFIFOL(fd,10) = login_id1;
  3059. WFIFOB(fd,14) = sex;
  3060. WFIFOL(fd,15) = htonl(ip);
  3061. WFIFOSET(fd,19);
  3062. }
  3063. }
  3064. break;
  3065. case 0x2736: // ip address update
  3066. if (RFIFOREST(fd) < 6) return 0;
  3067. server[id].ip = ntohl(RFIFOL(fd, 2));
  3068. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  3069. RFIFOSKIP(fd,6);
  3070. break;
  3071. case 0x3008:
  3072. if( RFIFOREST(fd) < RFIFOW(fd,4) )
  3073. return 0;/* packet wasn't fully received yet (still fragmented) */
  3074. else {
  3075. int sfd;/* stat server fd */
  3076. RFIFOSKIP(fd, 2);/* we skip first 2 bytes which are the 0x3008, so we end up with a buffer equal to the one we send */
  3077. if( (sfd = make_connection(host2ip("stats.rathena.org"),(uint16)25421,true,10) ) == -1 ) {
  3078. RFIFOSKIP(fd, RFIFOW(fd,2) );/* skip this packet */
  3079. break;/* connection not possible, we drop the report */
  3080. }
  3081. session[sfd]->flag.server = 1;/* to ensure we won't drop our own packet */
  3082. WFIFOHEAD(sfd, RFIFOW(fd,2) );
  3083. memcpy((char*)WFIFOP(sfd,0), (char*)RFIFOP(fd, 0), RFIFOW(fd,2));
  3084. WFIFOSET(sfd, RFIFOW(fd,2) );
  3085. flush_fifo(sfd);
  3086. do_close(sfd);
  3087. RFIFOSKIP(fd, RFIFOW(fd,2) );/* skip this packet */
  3088. }
  3089. break;
  3090. default:
  3091. {
  3092. // inter server - packet
  3093. int r = inter_parse_frommap(fd);
  3094. if (r == 1) break; // processed
  3095. if (r == 2) return 0; // need more packet
  3096. // no inter server packet. no char server packet -> disconnect
  3097. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  3098. set_eof(fd);
  3099. return 0;
  3100. }
  3101. } // switch
  3102. } // while
  3103. return 0;
  3104. }
  3105. void do_init_mapif(void)
  3106. {
  3107. int i;
  3108. for( i = 0; i < ARRAYLENGTH(server); ++i )
  3109. mapif_server_init(i);
  3110. }
  3111. void do_final_mapif(void)
  3112. {
  3113. int i;
  3114. for( i = 0; i < ARRAYLENGTH(server); ++i )
  3115. mapif_server_destroy(i);
  3116. }
  3117. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  3118. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  3119. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  3120. {
  3121. int i, j;
  3122. for(i = 0; i < ARRAYLENGTH(server); i++)
  3123. {
  3124. if (server[i].fd > 0
  3125. && (ip == (uint32)-1 || server[i].ip == ip)
  3126. && (port == (uint16)-1 || server[i].port == port))
  3127. {
  3128. for (j = 0; server[i].map[j]; j++)
  3129. if (server[i].map[j] == map)
  3130. return i;
  3131. }
  3132. }
  3133. return -1;
  3134. }
  3135. // Initialization process (currently only initialization inter_mapif)
  3136. static int char_mapif_init(int fd)
  3137. {
  3138. return inter_mapif_init(fd);
  3139. }
  3140. //--------------------------------------------
  3141. // Test to know if an IP come from LAN or WAN.
  3142. //--------------------------------------------
  3143. int lan_subnetcheck(uint32 ip)
  3144. {
  3145. int i;
  3146. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  3147. if( i < subnet_count ) {
  3148. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  3149. return subnet[i].map_ip;
  3150. } else {
  3151. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  3152. return 0;
  3153. }
  3154. }
  3155. /// @param result
  3156. /// 0 (0x718): An unknown error has occurred.
  3157. /// 1: none/success
  3158. /// 3 (0x719): A database error occurred.
  3159. /// 4 (0x71a): To delete a character you must withdraw from the guild.
  3160. /// 5 (0x71b): To delete a character you must withdraw from the party.
  3161. /// Any (0x718): An unknown error has occurred.
  3162. void char_delete2_ack(int fd, int char_id, uint32 result, time_t delete_date)
  3163. {// HC: <0828>.W <char id>.L <Msg:0-5>.L <deleteDate>.L
  3164. WFIFOHEAD(fd,14);
  3165. WFIFOW(fd,0) = 0x828;
  3166. WFIFOL(fd,2) = char_id;
  3167. WFIFOL(fd,6) = result;
  3168. WFIFOL(fd,10) = TOL(delete_date);
  3169. WFIFOSET(fd,14);
  3170. }
  3171. /// @param result
  3172. /// 0 (0x718): An unknown error has occurred.
  3173. /// 1: none/success
  3174. /// 2 (0x71c): Due to system settings can not be deleted.
  3175. /// 3 (0x719): A database error occurred.
  3176. /// 4 (0x71d): Deleting not yet possible time.
  3177. /// 5 (0x71e): Date of birth do not match.
  3178. /// Any (0x718): An unknown error has occurred.
  3179. void char_delete2_accept_ack(int fd, int char_id, uint32 result)
  3180. {// HC: <082a>.W <char id>.L <Msg:0-5>.L
  3181. WFIFOHEAD(fd,10);
  3182. WFIFOW(fd,0) = 0x82a;
  3183. WFIFOL(fd,2) = char_id;
  3184. WFIFOL(fd,6) = result;
  3185. WFIFOSET(fd,10);
  3186. }
  3187. /// @param result
  3188. /// 1 (0x718): none/success, (if char id not in deletion process): An unknown error has occurred.
  3189. /// 2 (0x719): A database error occurred.
  3190. /// Any (0x718): An unknown error has occurred.
  3191. void char_delete2_cancel_ack(int fd, int char_id, uint32 result)
  3192. {// HC: <082c>.W <char id>.L <Msg:1-2>.L
  3193. WFIFOHEAD(fd,10);
  3194. WFIFOW(fd,0) = 0x82c;
  3195. WFIFOL(fd,2) = char_id;
  3196. WFIFOL(fd,6) = result;
  3197. WFIFOSET(fd,10);
  3198. }
  3199. static void char_delete2_req(int fd, struct char_session_data* sd)
  3200. {// CH: <0827>.W <char id>.L
  3201. int char_id, i;
  3202. char* data;
  3203. time_t delete_date;
  3204. char_id = RFIFOL(fd,2);
  3205. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3206. if( i == MAX_CHARS )
  3207. {// character not found
  3208. char_delete2_ack(fd, char_id, 3, 0);
  3209. return;
  3210. }
  3211. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3212. {
  3213. Sql_ShowDebug(sql_handle);
  3214. char_delete2_ack(fd, char_id, 3, 0);
  3215. return;
  3216. }
  3217. Sql_GetData(sql_handle, 0, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3218. if( delete_date ) {// character already queued for deletion
  3219. char_delete2_ack(fd, char_id, 0, 0);
  3220. return;
  3221. }
  3222. /*
  3223. // Aegis imposes these checks probably to avoid dead member
  3224. // entries in guilds/parties, otherwise they are not required.
  3225. // TODO: Figure out how these are enforced during waiting.
  3226. if( guild_id )
  3227. {// character in guild
  3228. char_delete2_ack(fd, char_id, 4, 0);
  3229. return;
  3230. }
  3231. if( party_id )
  3232. {// character in party
  3233. char_delete2_ack(fd, char_id, 5, 0);
  3234. return;
  3235. }
  3236. */
  3237. // success
  3238. delete_date = time(NULL)+char_del_delay;
  3239. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='%lu' WHERE `char_id`='%d'", char_db, (unsigned long)delete_date, char_id) )
  3240. {
  3241. Sql_ShowDebug(sql_handle);
  3242. char_delete2_ack(fd, char_id, 3, 0);
  3243. return;
  3244. }
  3245. char_delete2_ack(fd, char_id, 1, delete_date);
  3246. }
  3247. static void char_delete2_accept(int fd, struct char_session_data* sd)
  3248. {// CH: <0829>.W <char id>.L <birth date:YYMMDD>.6B
  3249. char birthdate[8+1];
  3250. int char_id, i, k;
  3251. unsigned int base_level;
  3252. char* data;
  3253. time_t delete_date;
  3254. char_id = RFIFOL(fd,2);
  3255. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, char_id);
  3256. // construct "YY-MM-DD"
  3257. birthdate[0] = RFIFOB(fd,6);
  3258. birthdate[1] = RFIFOB(fd,7);
  3259. birthdate[2] = '-';
  3260. birthdate[3] = RFIFOB(fd,8);
  3261. birthdate[4] = RFIFOB(fd,9);
  3262. birthdate[5] = '-';
  3263. birthdate[6] = RFIFOB(fd,10);
  3264. birthdate[7] = RFIFOB(fd,11);
  3265. birthdate[8] = 0;
  3266. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3267. if( i == MAX_CHARS )
  3268. {// character not found
  3269. char_delete2_accept_ack(fd, char_id, 3);
  3270. return;
  3271. }
  3272. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `base_level`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3273. {// data error
  3274. Sql_ShowDebug(sql_handle);
  3275. char_delete2_accept_ack(fd, char_id, 3);
  3276. return;
  3277. }
  3278. Sql_GetData(sql_handle, 0, &data, NULL); base_level = (unsigned int)strtoul(data, NULL, 10);
  3279. Sql_GetData(sql_handle, 1, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3280. if( !delete_date || delete_date>time(NULL) )
  3281. {// not queued or delay not yet passed
  3282. char_delete2_accept_ack(fd, char_id, 4);
  3283. return;
  3284. }
  3285. if( strcmp(sd->birthdate+2, birthdate) ) // +2 to cut off the century
  3286. {// birth date is wrong
  3287. char_delete2_accept_ack(fd, char_id, 5);
  3288. return;
  3289. }
  3290. if( ( char_del_level > 0 && base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && base_level <= (unsigned int)(-char_del_level) ) )
  3291. {// character level config restriction
  3292. char_delete2_accept_ack(fd, char_id, 2);
  3293. return;
  3294. }
  3295. // success
  3296. if( delete_char_sql(char_id) < 0 )
  3297. {
  3298. char_delete2_accept_ack(fd, char_id, 3);
  3299. return;
  3300. }
  3301. // refresh character list cache
  3302. for(k = i; k < MAX_CHARS-1; k++)
  3303. {
  3304. sd->found_char[k] = sd->found_char[k+1];
  3305. }
  3306. sd->found_char[MAX_CHARS-1] = -1;
  3307. char_delete2_accept_ack(fd, char_id, 1);
  3308. }
  3309. static void char_delete2_cancel(int fd, struct char_session_data* sd)
  3310. {// CH: <082b>.W <char id>.L
  3311. int char_id, i;
  3312. char_id = RFIFOL(fd,2);
  3313. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3314. if( i == MAX_CHARS )
  3315. {// character not found
  3316. char_delete2_cancel_ack(fd, char_id, 2);
  3317. return;
  3318. }
  3319. // there is no need to check, whether or not the character was
  3320. // queued for deletion, as the client prints an error message by
  3321. // itself, if it was not the case (@see char_delete2_cancel_ack)
  3322. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='0' WHERE `char_id`='%d'", char_db, char_id) )
  3323. {
  3324. Sql_ShowDebug(sql_handle);
  3325. char_delete2_cancel_ack(fd, char_id, 2);
  3326. return;
  3327. }
  3328. char_delete2_cancel_ack(fd, char_id, 1);
  3329. }
  3330. int parse_char(int fd)
  3331. {
  3332. int i, ch;
  3333. char email[40];
  3334. unsigned short cmd;
  3335. int map_fd;
  3336. struct char_session_data* sd;
  3337. uint32 ipl = session[fd]->client_addr;
  3338. sd = (struct char_session_data*)session[fd]->session_data;
  3339. // disconnect any player if no login-server.
  3340. if(login_fd < 0)
  3341. set_eof(fd);
  3342. if(session[fd]->flag.eof)
  3343. {
  3344. if( sd != NULL && sd->auth )
  3345. { // already authed client
  3346. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  3347. if( data != NULL && data->fd == fd)
  3348. data->fd = -1;
  3349. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  3350. set_char_offline(-1,sd->account_id);
  3351. }
  3352. do_close(fd);
  3353. return 0;
  3354. }
  3355. while( RFIFOREST(fd) >= 2 )
  3356. {
  3357. //For use in packets that depend on an sd being present [Skotlex]
  3358. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  3359. cmd = RFIFOW(fd,0);
  3360. switch( cmd )
  3361. {
  3362. // request to connect
  3363. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  3364. case 0x65:
  3365. if( RFIFOREST(fd) < 17 )
  3366. return 0;
  3367. {
  3368. struct auth_node* node;
  3369. int account_id = RFIFOL(fd,2);
  3370. uint32 login_id1 = RFIFOL(fd,6);
  3371. uint32 login_id2 = RFIFOL(fd,10);
  3372. int sex = RFIFOB(fd,16);
  3373. RFIFOSKIP(fd,17);
  3374. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  3375. if (sd) {
  3376. //Received again auth packet for already authentified account?? Discard it.
  3377. //TODO: Perhaps log this as a hack attempt?
  3378. //TODO: and perhaps send back a reply?
  3379. break;
  3380. }
  3381. CREATE(session[fd]->session_data, struct char_session_data, 1);
  3382. sd = (struct char_session_data*)session[fd]->session_data;
  3383. sd->account_id = account_id;
  3384. sd->login_id1 = login_id1;
  3385. sd->login_id2 = login_id2;
  3386. sd->sex = sex;
  3387. sd->auth = false; // not authed yet
  3388. // send back account_id
  3389. WFIFOHEAD(fd,4);
  3390. WFIFOL(fd,0) = account_id;
  3391. WFIFOSET(fd,4);
  3392. if( runflag != CHARSERVER_ST_RUNNING )
  3393. {
  3394. WFIFOHEAD(fd,3);
  3395. WFIFOW(fd,0) = 0x6c;
  3396. WFIFOB(fd,2) = 0;// rejected from server
  3397. WFIFOSET(fd,3);
  3398. break;
  3399. }
  3400. // search authentification
  3401. node = (struct auth_node*)idb_get(auth_db, account_id);
  3402. if( node != NULL &&
  3403. node->account_id == account_id &&
  3404. node->login_id1 == login_id1 &&
  3405. node->login_id2 == login_id2 /*&&
  3406. node->ip == ipl*/ )
  3407. {// authentication found (coming from map server)
  3408. idb_remove(auth_db, account_id);
  3409. char_auth_ok(fd, sd);
  3410. }
  3411. else
  3412. {// authentication not found (coming from login server)
  3413. if (login_fd > 0) { // don't send request if no login-server
  3414. WFIFOHEAD(login_fd,23);
  3415. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  3416. WFIFOL(login_fd,2) = sd->account_id;
  3417. WFIFOL(login_fd,6) = sd->login_id1;
  3418. WFIFOL(login_fd,10) = sd->login_id2;
  3419. WFIFOB(login_fd,14) = sd->sex;
  3420. WFIFOL(login_fd,15) = htonl(ipl);
  3421. WFIFOL(login_fd,19) = fd;
  3422. WFIFOSET(login_fd,23);
  3423. } else { // if no login-server, we must refuse connection
  3424. WFIFOHEAD(fd,3);
  3425. WFIFOW(fd,0) = 0x6c;
  3426. WFIFOB(fd,2) = 0;
  3427. WFIFOSET(fd,3);
  3428. }
  3429. }
  3430. }
  3431. break;
  3432. // char select
  3433. case 0x66:
  3434. FIFOSD_CHECK(3);
  3435. {
  3436. struct mmo_charstatus char_dat;
  3437. struct mmo_charstatus *cd;
  3438. char* data;
  3439. int char_id;
  3440. uint32 subnet_map_ip;
  3441. struct auth_node* node;
  3442. int slot = RFIFOB(fd,2);
  3443. RFIFOSKIP(fd,3);
  3444. if ( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'", char_db, sd->account_id, slot)
  3445. || SQL_SUCCESS != Sql_NextRow(sql_handle)
  3446. || SQL_SUCCESS != Sql_GetData(sql_handle, 0, &data, NULL) )
  3447. { //Not found?? May be forged packet.
  3448. Sql_ShowDebug(sql_handle);
  3449. Sql_FreeResult(sql_handle);
  3450. WFIFOHEAD(fd,3);
  3451. WFIFOW(fd,0) = 0x6c;
  3452. WFIFOB(fd,2) = 0; // rejected from server
  3453. WFIFOSET(fd,3);
  3454. break;
  3455. }
  3456. char_id = atoi(data);
  3457. Sql_FreeResult(sql_handle);
  3458. /* set char as online prior to loading its data so 3rd party applications will realise the sql data is not reliable */
  3459. set_char_online(-2,char_id,sd->account_id);
  3460. if( !mmo_char_fromsql(char_id, &char_dat, true) ) { /* failed? set it back offline */
  3461. set_char_offline(char_id, sd->account_id);
  3462. /* failed to load something. REJECT! */
  3463. WFIFOHEAD(fd,3);
  3464. WFIFOW(fd,0) = 0x6c;
  3465. WFIFOB(fd,2) = 0;
  3466. WFIFOSET(fd,3);
  3467. break;/* jump off this boat */
  3468. }
  3469. //Have to switch over to the DB instance otherwise data won't propagate [Kevin]
  3470. cd = (struct mmo_charstatus *)idb_get(char_db_, char_id);
  3471. cd->sex = sd->sex;
  3472. if (log_char) {
  3473. char esc_name[NAME_LENGTH*2+1];
  3474. Sql_EscapeStringLen(sql_handle, esc_name, char_dat.name, strnlen(char_dat.name, NAME_LENGTH));
  3475. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  3476. charlog_db, sd->account_id, slot, esc_name) )
  3477. Sql_ShowDebug(sql_handle);
  3478. }
  3479. ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, char_dat.name);
  3480. // searching map server
  3481. i = search_mapserver(cd->last_point.map, -1, -1);
  3482. // if map is not found, we check major cities
  3483. if (i < 0 || !cd->last_point.map) {
  3484. unsigned short j;
  3485. //First check that there's actually a map server online.
  3486. ARR_FIND( 0, ARRAYLENGTH(server), j, server[j].fd >= 0 && server[j].map[0] );
  3487. if (j == ARRAYLENGTH(server)) {
  3488. ShowInfo("Connection Closed. No map servers available.\n");
  3489. WFIFOHEAD(fd,3);
  3490. WFIFOW(fd,0) = 0x81;
  3491. WFIFOB(fd,2) = 1; // 01 = Server closed
  3492. WFIFOSET(fd,3);
  3493. break;
  3494. }
  3495. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  3496. cd->last_point.x = 273;
  3497. cd->last_point.y = 354;
  3498. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  3499. cd->last_point.x = 120;
  3500. cd->last_point.y = 100;
  3501. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  3502. cd->last_point.x = 160;
  3503. cd->last_point.y = 94;
  3504. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  3505. cd->last_point.x = 116;
  3506. cd->last_point.y = 57;
  3507. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  3508. cd->last_point.x = 87;
  3509. cd->last_point.y = 117;
  3510. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  3511. cd->last_point.x = 94;
  3512. cd->last_point.y = 103;
  3513. } else {
  3514. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(cd->last_point.map));
  3515. WFIFOHEAD(fd,3);
  3516. WFIFOW(fd,0) = 0x81;
  3517. WFIFOB(fd,2) = 1; // 01 = Server closed
  3518. WFIFOSET(fd,3);
  3519. break;
  3520. }
  3521. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(cd->last_point.map), mapindex_id2name(j));
  3522. cd->last_point.map = j;
  3523. }
  3524. //Send NEW auth packet [Kevin]
  3525. //FIXME: is this case even possible? [ultramage]
  3526. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  3527. {
  3528. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  3529. server[i].fd = -1;
  3530. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3531. //Send server closed.
  3532. WFIFOHEAD(fd,3);
  3533. WFIFOW(fd,0) = 0x81;
  3534. WFIFOB(fd,2) = 1; // 01 = Server closed
  3535. WFIFOSET(fd,3);
  3536. break;
  3537. }
  3538. //Send player to map
  3539. WFIFOHEAD(fd,28);
  3540. WFIFOW(fd,0) = 0x71;
  3541. WFIFOL(fd,2) = cd->char_id;
  3542. mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
  3543. subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
  3544. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  3545. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  3546. WFIFOSET(fd,28);
  3547. // create temporary auth entry
  3548. CREATE(node, struct auth_node, 1);
  3549. node->account_id = sd->account_id;
  3550. node->char_id = cd->char_id;
  3551. node->login_id1 = sd->login_id1;
  3552. node->login_id2 = sd->login_id2;
  3553. node->sex = sd->sex;
  3554. node->expiration_time = sd->expiration_time;
  3555. node->group_id = sd->group_id;
  3556. node->ip = ipl;
  3557. idb_put(auth_db, sd->account_id, node);
  3558. }
  3559. break;
  3560. // create new char
  3561. #if PACKETVER >= 20120307
  3562. // S 0970 <name>.24B <slot>.B <hair color>.W <hair style>.W
  3563. case 0x970:
  3564. FIFOSD_CHECK(31);
  3565. #else
  3566. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  3567. case 0x67:
  3568. FIFOSD_CHECK(37);
  3569. #endif
  3570. if( !char_new ) //turn character creation on/off [Kevin]
  3571. i = -2;
  3572. else
  3573. #if PACKETVER >= 20120307
  3574. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOW(fd,27),RFIFOW(fd,29));
  3575. #else
  3576. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  3577. #endif
  3578. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  3579. if (i < 0) {
  3580. WFIFOHEAD(fd,3);
  3581. WFIFOW(fd,0) = 0x6e;
  3582. /* Others I found [Ind] */
  3583. /* 0x02 = Symbols in Character Names are forbidden */
  3584. /* 0x03 = You are not elegible to open the Character Slot. */
  3585. switch (i) {
  3586. case -1: WFIFOB(fd,2) = 0x00; break;
  3587. case -2: WFIFOB(fd,2) = 0xFF; break;
  3588. case -3: WFIFOB(fd,2) = 0x01; break;
  3589. case -4: WFIFOB(fd,2) = 0x03; break;
  3590. }
  3591. WFIFOSET(fd,3);
  3592. } else {
  3593. int len;
  3594. // retrieve data
  3595. struct mmo_charstatus char_dat;
  3596. mmo_char_fromsql(i, &char_dat, false); //Only the short data is needed.
  3597. // send to player
  3598. WFIFOHEAD(fd,2+MAX_CHAR_BUF);
  3599. WFIFOW(fd,0) = 0x6d;
  3600. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat);
  3601. WFIFOSET(fd,len);
  3602. // add new entry to the chars list
  3603. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  3604. if( ch < MAX_CHARS )
  3605. sd->found_char[ch] = i; // the char_id of the new char
  3606. }
  3607. #if PACKETVER >= 20120307
  3608. RFIFOSKIP(fd,31);
  3609. #else
  3610. RFIFOSKIP(fd,37);
  3611. #endif
  3612. break;
  3613. // delete char
  3614. case 0x68:
  3615. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  3616. case 0x1fb:
  3617. if (cmd == 0x68) FIFOSD_CHECK(46);
  3618. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  3619. {
  3620. int cid = RFIFOL(fd,2);
  3621. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  3622. memcpy(email, RFIFOP(fd,6), 40);
  3623. RFIFOSKIP(fd,( cmd == 0x68) ? 46 : 56);
  3624. // Check if e-mail is correct
  3625. if(strcmpi(email, sd->email) && //email does not matches and
  3626. (
  3627. strcmp("a@a.com", sd->email) || //it is not default email, or
  3628. (strcmp("a@a.com", email) && strcmp("", email)) //email sent does not matches default
  3629. )) { //Fail
  3630. WFIFOHEAD(fd,3);
  3631. WFIFOW(fd,0) = 0x70;
  3632. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3633. WFIFOSET(fd,3);
  3634. break;
  3635. }
  3636. // check if this char exists
  3637. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3638. if( i == MAX_CHARS )
  3639. { // Such a character does not exist in the account
  3640. WFIFOHEAD(fd,3);
  3641. WFIFOW(fd,0) = 0x70;
  3642. WFIFOB(fd,2) = 0;
  3643. WFIFOSET(fd,3);
  3644. break;
  3645. }
  3646. // remove char from list and compact it
  3647. for(ch = i; ch < MAX_CHARS-1; ch++)
  3648. sd->found_char[ch] = sd->found_char[ch+1];
  3649. sd->found_char[MAX_CHARS-1] = -1;
  3650. /* Delete character */
  3651. if(delete_char_sql(cid)<0){
  3652. //can't delete the char
  3653. //either SQL error or can't delete by some CONFIG conditions
  3654. //del fail
  3655. WFIFOHEAD(fd,3);
  3656. WFIFOW(fd, 0) = 0x70;
  3657. WFIFOB(fd, 2) = 0;
  3658. WFIFOSET(fd, 3);
  3659. break;
  3660. }
  3661. /* Char successfully deleted.*/
  3662. WFIFOHEAD(fd,2);
  3663. WFIFOW(fd,0) = 0x6f;
  3664. WFIFOSET(fd,2);
  3665. }
  3666. break;
  3667. // client keep-alive packet (every 12 seconds)
  3668. // R 0187 <account ID>.l
  3669. case 0x187:
  3670. if (RFIFOREST(fd) < 6)
  3671. return 0;
  3672. RFIFOSKIP(fd,6);
  3673. break;
  3674. // char rename request
  3675. // R 08fc <char ID>.l <new name>.24B
  3676. case 0x8fc:
  3677. FIFOSD_CHECK(30);
  3678. {
  3679. int i, cid =RFIFOL(fd,2);
  3680. char name[NAME_LENGTH];
  3681. char esc_name[NAME_LENGTH*2+1];
  3682. safestrncpy(name, (char *)RFIFOP(fd,6), NAME_LENGTH);
  3683. RFIFOSKIP(fd,30);
  3684. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3685. if( i == MAX_CHARS )
  3686. break;
  3687. normalize_name(name,TRIM_CHARS);
  3688. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  3689. if( !check_char_name(name,esc_name) ) {
  3690. i = 1;
  3691. safestrncpy(sd->new_name, name, NAME_LENGTH);
  3692. } else
  3693. i = 0;
  3694. WFIFOHEAD(fd, 4);
  3695. WFIFOW(fd,0) = 0x28e;
  3696. WFIFOW(fd,2) = i;
  3697. WFIFOSET(fd,4);
  3698. }
  3699. break;
  3700. // char rename request
  3701. // R 028d <account ID>.l <char ID>.l <new name>.24B
  3702. case 0x28d:
  3703. FIFOSD_CHECK(34);
  3704. {
  3705. int i, aid = RFIFOL(fd,2), cid =RFIFOL(fd,6);
  3706. char name[NAME_LENGTH];
  3707. char esc_name[NAME_LENGTH*2+1];
  3708. safestrncpy(name, (char *)RFIFOP(fd,10), NAME_LENGTH);
  3709. RFIFOSKIP(fd,34);
  3710. if( aid != sd->account_id )
  3711. break;
  3712. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3713. if( i == MAX_CHARS )
  3714. break;
  3715. normalize_name(name,TRIM_CHARS);
  3716. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  3717. if( !check_char_name(name,esc_name) )
  3718. {
  3719. i = 1;
  3720. safestrncpy(sd->new_name, name, NAME_LENGTH);
  3721. }
  3722. else
  3723. i = 0;
  3724. WFIFOHEAD(fd, 4);
  3725. WFIFOW(fd,0) = 0x28e;
  3726. WFIFOW(fd,2) = i;
  3727. WFIFOSET(fd,4);
  3728. }
  3729. break;
  3730. //Confirm change name.
  3731. // 0x28f <char_id>.L
  3732. case 0x28f:
  3733. // 0: Sucessfull
  3734. // 1: This character's name has already been changed. You cannot change a character's name more than once.
  3735. // 2: User information is not correct.
  3736. // 3: You have failed to change this character's name.
  3737. // 4: Another user is using this character name, so please select another one.
  3738. FIFOSD_CHECK(6);
  3739. {
  3740. int i;
  3741. int cid = RFIFOL(fd,2);
  3742. RFIFOSKIP(fd,6);
  3743. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  3744. if( i == MAX_CHARS )
  3745. break;
  3746. i = rename_char_sql(sd, cid);
  3747. WFIFOHEAD(fd, 4);
  3748. WFIFOW(fd,0) = 0x290;
  3749. WFIFOW(fd,2) = i;
  3750. WFIFOSET(fd,4);
  3751. }
  3752. break;
  3753. // captcha code request (not implemented)
  3754. // R 07e5 <?>.w <aid>.l
  3755. case 0x7e5:
  3756. WFIFOHEAD(fd,5);
  3757. WFIFOW(fd,0) = 0x7e9;
  3758. WFIFOW(fd,2) = 5;
  3759. WFIFOB(fd,4) = 1;
  3760. WFIFOSET(fd,5);
  3761. RFIFOSKIP(fd,8);
  3762. break;
  3763. // captcha code check (not implemented)
  3764. // R 07e7 <len>.w <aid>.l <code>.b10 <?>.b14
  3765. case 0x7e7:
  3766. WFIFOHEAD(fd,5);
  3767. WFIFOW(fd,0) = 0x7e9;
  3768. WFIFOW(fd,2) = 5;
  3769. WFIFOB(fd,4) = 1;
  3770. WFIFOSET(fd,5);
  3771. RFIFOSKIP(fd,32);
  3772. break;
  3773. // deletion timer request
  3774. case 0x827:
  3775. FIFOSD_CHECK(6);
  3776. char_delete2_req(fd, sd);
  3777. RFIFOSKIP(fd,6);
  3778. break;
  3779. // deletion accept request
  3780. case 0x829:
  3781. FIFOSD_CHECK(12);
  3782. char_delete2_accept(fd, sd);
  3783. RFIFOSKIP(fd,12);
  3784. break;
  3785. // deletion cancel request
  3786. case 0x82b:
  3787. FIFOSD_CHECK(6);
  3788. char_delete2_cancel(fd, sd);
  3789. RFIFOSKIP(fd,6);
  3790. break;
  3791. // login as map-server
  3792. case 0x2af8:
  3793. if (RFIFOREST(fd) < 60)
  3794. return 0;
  3795. else {
  3796. char* l_user = (char*)RFIFOP(fd,2);
  3797. char* l_pass = (char*)RFIFOP(fd,26);
  3798. l_user[23] = '\0';
  3799. l_pass[23] = '\0';
  3800. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd <= 0 );
  3801. if( runflag != CHARSERVER_ST_RUNNING ||
  3802. i == ARRAYLENGTH(server) ||
  3803. strcmp(l_user, userid) != 0 ||
  3804. strcmp(l_pass, passwd) != 0 )
  3805. {
  3806. WFIFOHEAD(fd,3);
  3807. WFIFOW(fd,0) = 0x2af9;
  3808. WFIFOB(fd,2) = 3;
  3809. WFIFOSET(fd,3);
  3810. } else {
  3811. WFIFOHEAD(fd,3);
  3812. WFIFOW(fd,0) = 0x2af9;
  3813. WFIFOB(fd,2) = 0;
  3814. WFIFOSET(fd,3);
  3815. server[i].fd = fd;
  3816. server[i].ip = ntohl(RFIFOL(fd,54));
  3817. server[i].port = ntohs(RFIFOW(fd,58));
  3818. server[i].users = 0;
  3819. memset(server[i].map, 0, sizeof(server[i].map));
  3820. session[fd]->func_parse = parse_frommap;
  3821. session[fd]->flag.server = 1;
  3822. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3823. char_mapif_init(fd);
  3824. }
  3825. RFIFOSKIP(fd,60);
  3826. }
  3827. return 0; // avoid processing of followup packets here
  3828. // checks the entered pin
  3829. case 0x8b8:
  3830. if( RFIFOREST(fd) < 10 )
  3831. return 0;
  3832. if( pincode_enabled && RFIFOL(fd,2) == sd->account_id )
  3833. pincode_check( fd, sd );
  3834. RFIFOSKIP(fd,10);
  3835. break;
  3836. // request for PIN window
  3837. case 0x8c5:
  3838. if( RFIFOREST(fd) < 6 )
  3839. return 0;
  3840. if( pincode_enabled && RFIFOL(fd,2) == sd->account_id ){
  3841. if( strlen( sd->pincode ) <= 0 ){
  3842. pincode_sendstate( fd, sd, PINCODE_NEW );
  3843. }else{
  3844. pincode_sendstate( fd, sd, PINCODE_ASK );
  3845. }
  3846. }
  3847. RFIFOSKIP(fd,6);
  3848. break;
  3849. // pincode change request
  3850. case 0x8be:
  3851. if( RFIFOREST(fd) < 14 )
  3852. return 0;
  3853. if( pincode_enabled && RFIFOL(fd,2) == sd->account_id )
  3854. pincode_change( fd, sd );
  3855. RFIFOSKIP(fd,14);
  3856. break;
  3857. // activate PIN system and set first PIN
  3858. case 0x8ba:
  3859. if( RFIFOREST(fd) < 10 )
  3860. return 0;
  3861. if( pincode_enabled && RFIFOL(fd,2) == sd->account_id )
  3862. pincode_setnew( fd, sd );
  3863. RFIFOSKIP(fd,10);
  3864. break;
  3865. // character movement request
  3866. case 0x8d4:
  3867. if( RFIFOREST(fd) < 8 )
  3868. return 0;
  3869. moveCharSlot( fd, sd, RFIFOW(fd, 2), RFIFOW(fd, 4) );
  3870. mmo_char_send(fd, sd);
  3871. RFIFOSKIP(fd,8);
  3872. break;
  3873. case 0x9a1:
  3874. if( RFIFOREST(fd) < 2 )
  3875. return 0;
  3876. char_parse_req_charlist(fd,sd);
  3877. RFIFOSKIP(fd,2);
  3878. break;
  3879. // unknown packet received
  3880. default:
  3881. ShowError("parse_char: Received unknown packet "CL_WHITE"0x%x"CL_RESET" from ip '"CL_WHITE"%s"CL_RESET"'! Disconnecting!\n", RFIFOW(fd,0), ip2str(ipl, NULL));
  3882. set_eof(fd);
  3883. return 0;
  3884. }
  3885. }
  3886. RFIFOFLUSH(fd);
  3887. return 0;
  3888. }
  3889. // Console Command Parser [Wizputer]
  3890. int parse_console(const char* buf)
  3891. {
  3892. char type[64];
  3893. char command[64];
  3894. int n=0;
  3895. if( ( n = sscanf(buf, "%63[^:]:%63[^\n]", type, command) ) < 2 ){
  3896. if((n = sscanf(buf, "%63[^\n]", type))<1) return -1; //nothing to do no arg
  3897. }
  3898. if( n != 2 ){ //end string
  3899. ShowNotice("Type: '%s'\n",type);
  3900. command[0] = '\0';
  3901. }
  3902. else
  3903. ShowNotice("Type of command: '%s' || Command: '%s'\n",type,command);
  3904. if( n == 2 && strcmpi("server", type) == 0 ){
  3905. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 ){
  3906. runflag = 0;
  3907. }
  3908. else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
  3909. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  3910. }
  3911. else if( strcmpi("ers_report", type) == 0 ){
  3912. ers_report();
  3913. }
  3914. else if( strcmpi("help", type) == 0 ){
  3915. ShowInfo("Available commands:\n");
  3916. ShowInfo("\t server:shutdown => Stops the server.\n");
  3917. ShowInfo("\t server:alive => Checks if the server is running.\n");
  3918. ShowInfo("\t ers_report => Displays database usage.\n");
  3919. }
  3920. return 0;
  3921. }
  3922. int mapif_sendall(unsigned char *buf, unsigned int len)
  3923. {
  3924. int i, c;
  3925. c = 0;
  3926. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3927. int fd;
  3928. if ((fd = server[i].fd) > 0) {
  3929. WFIFOHEAD(fd,len);
  3930. memcpy(WFIFOP(fd,0), buf, len);
  3931. WFIFOSET(fd,len);
  3932. c++;
  3933. }
  3934. }
  3935. return c;
  3936. }
  3937. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  3938. {
  3939. int i, c;
  3940. c = 0;
  3941. for(i = 0; i < ARRAYLENGTH(server); i++) {
  3942. int fd;
  3943. if ((fd = server[i].fd) > 0 && fd != sfd) {
  3944. WFIFOHEAD(fd,len);
  3945. memcpy(WFIFOP(fd,0), buf, len);
  3946. WFIFOSET(fd,len);
  3947. c++;
  3948. }
  3949. }
  3950. return c;
  3951. }
  3952. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  3953. {
  3954. if (fd >= 0) {
  3955. int i;
  3956. ARR_FIND( 0, ARRAYLENGTH(server), i, fd == server[i].fd );
  3957. if( i < ARRAYLENGTH(server) )
  3958. {
  3959. WFIFOHEAD(fd,len);
  3960. memcpy(WFIFOP(fd,0), buf, len);
  3961. WFIFOSET(fd,len);
  3962. return 1;
  3963. }
  3964. }
  3965. return 0;
  3966. }
  3967. int broadcast_user_count(int tid, unsigned int tick, int id, intptr_t data)
  3968. {
  3969. uint8 buf[6];
  3970. int users = count_users();
  3971. // only send an update when needed
  3972. static int prev_users = 0;
  3973. if( prev_users == users )
  3974. return 0;
  3975. prev_users = users;
  3976. if( login_fd > 0 && session[login_fd] )
  3977. {
  3978. // send number of user to login server
  3979. WFIFOHEAD(login_fd,6);
  3980. WFIFOW(login_fd,0) = 0x2714;
  3981. WFIFOL(login_fd,2) = users;
  3982. WFIFOSET(login_fd,6);
  3983. }
  3984. // send number of players to all map-servers
  3985. WBUFW(buf,0) = 0x2b00;
  3986. WBUFL(buf,2) = users;
  3987. mapif_sendall(buf,6);
  3988. return 0;
  3989. }
  3990. /**
  3991. * Load this character's account id into the 'online accounts' packet
  3992. * @see DBApply
  3993. */
  3994. static int send_accounts_tologin_sub(DBKey key, DBData *data, va_list ap)
  3995. {
  3996. struct online_char_data* character = db_data2ptr(data);
  3997. int* i = va_arg(ap, int*);
  3998. if(character->server > -1)
  3999. {
  4000. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  4001. (*i)++;
  4002. return 1;
  4003. }
  4004. return 0;
  4005. }
  4006. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr_t data)
  4007. {
  4008. if (login_fd > 0 && session[login_fd])
  4009. {
  4010. // send account list to login server
  4011. int users = online_char_db->size(online_char_db);
  4012. int i = 0;
  4013. WFIFOHEAD(login_fd,8+users*4);
  4014. WFIFOW(login_fd,0) = 0x272d;
  4015. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  4016. WFIFOW(login_fd,2) = 8+ i*4;
  4017. WFIFOL(login_fd,4) = i;
  4018. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  4019. }
  4020. return 0;
  4021. }
  4022. int check_connect_login_server(int tid, unsigned int tick, int id, intptr_t data)
  4023. {
  4024. if (login_fd > 0 && session[login_fd] != NULL)
  4025. return 0;
  4026. ShowInfo("Attempt to connect to login-server...\n");
  4027. login_fd = make_connection(login_ip, login_port, false,10);
  4028. if (login_fd == -1)
  4029. { //Try again later. [Skotlex]
  4030. login_fd = 0;
  4031. return 0;
  4032. }
  4033. session[login_fd]->func_parse = parse_fromlogin;
  4034. session[login_fd]->flag.server = 1;
  4035. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  4036. WFIFOHEAD(login_fd,86);
  4037. WFIFOW(login_fd,0) = 0x2710;
  4038. memcpy(WFIFOP(login_fd,2), userid, 24);
  4039. memcpy(WFIFOP(login_fd,26), passwd, 24);
  4040. WFIFOL(login_fd,50) = 0;
  4041. WFIFOL(login_fd,54) = htonl(char_ip);
  4042. WFIFOW(login_fd,58) = htons(char_port);
  4043. memcpy(WFIFOP(login_fd,60), server_name, 20);
  4044. WFIFOW(login_fd,80) = 0;
  4045. WFIFOW(login_fd,82) = char_maintenance;
  4046. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  4047. WFIFOSET(login_fd,86);
  4048. return 1;
  4049. }
  4050. //------------------------------------------------
  4051. //Pincode system
  4052. //------------------------------------------------
  4053. void pincode_check( int fd, struct char_session_data* sd ){
  4054. char pin[PINCODE_LENGTH+1];
  4055. memset(pin,0,PINCODE_LENGTH+1);
  4056. strncpy((char*)pin, (char*)RFIFOP(fd, 6), PINCODE_LENGTH);
  4057. pincode_decrypt(sd->pincode_seed, pin );
  4058. if( pincode_compare( fd, sd, pin ) ){
  4059. pincode_sendstate( fd, sd, PINCODE_PASSED );
  4060. }
  4061. }
  4062. int pincode_compare( int fd, struct char_session_data* sd, char* pin ){
  4063. if( strcmp( sd->pincode, pin ) == 0 ){
  4064. sd->pincode_try = 0;
  4065. return 1;
  4066. }else{
  4067. pincode_sendstate( fd, sd, PINCODE_WRONG );
  4068. if( pincode_maxtry && ++sd->pincode_try >= pincode_maxtry ){
  4069. pincode_notifyLoginPinError( sd->account_id );
  4070. }
  4071. return 0;
  4072. }
  4073. }
  4074. void pincode_change( int fd, struct char_session_data* sd ){
  4075. char oldpin[PINCODE_LENGTH+1];
  4076. char newpin[PINCODE_LENGTH+1];
  4077. memset(oldpin,0,PINCODE_LENGTH+1);
  4078. memset(newpin,0,PINCODE_LENGTH+1);
  4079. strncpy(oldpin, (char*)RFIFOP(fd,6), PINCODE_LENGTH);
  4080. pincode_decrypt(sd->pincode_seed,oldpin);
  4081. if( !pincode_compare( fd, sd, oldpin ) )
  4082. return;
  4083. strncpy(newpin, (char*)RFIFOP(fd,10), PINCODE_LENGTH);
  4084. pincode_decrypt(sd->pincode_seed,newpin);
  4085. pincode_notifyLoginPinUpdate( sd->account_id, newpin );
  4086. strncpy(sd->pincode, newpin, sizeof(newpin));
  4087. pincode_sendstate( fd, sd, PINCODE_PASSED );
  4088. }
  4089. void pincode_setnew( int fd, struct char_session_data* sd ){
  4090. char newpin[PINCODE_LENGTH+1];
  4091. memset(newpin,0,PINCODE_LENGTH+1);
  4092. strncpy( newpin, (char*)RFIFOP(fd,6), PINCODE_LENGTH );
  4093. pincode_decrypt( sd->pincode_seed, newpin );
  4094. pincode_notifyLoginPinUpdate( sd->account_id, newpin );
  4095. strncpy( sd->pincode, newpin, strlen( newpin ) );
  4096. pincode_sendstate( fd, sd, PINCODE_PASSED );
  4097. }
  4098. // 0 = disabled / pin is correct
  4099. // 1 = ask for pin - client sends 0x8b8
  4100. // 2 = create new pin - client sends 0x8ba
  4101. // 3 = pin must be changed - client 0x8be
  4102. // 4 = create new pin - client sends 0x8ba
  4103. // 5 = client shows msgstr(1896)
  4104. // 6 = client shows msgstr(1897) Unable to use your KSSN number
  4105. // 7 = char select window shows a button - client sends 0x8c5
  4106. // 8 = pincode was incorrect
  4107. void pincode_sendstate( int fd, struct char_session_data* sd, uint16 state ){
  4108. WFIFOHEAD(fd, 12);
  4109. WFIFOW(fd, 0) = 0x8b9;
  4110. WFIFOL(fd, 2) = sd->pincode_seed = rnd() % 0xFFFF;
  4111. WFIFOL(fd, 6) = sd->account_id;
  4112. WFIFOW(fd,10) = state;
  4113. WFIFOSET(fd,12);
  4114. }
  4115. void pincode_notifyLoginPinUpdate( int account_id, char* pin ){
  4116. WFIFOHEAD(login_fd,11);
  4117. WFIFOW(login_fd,0) = 0x2738;
  4118. WFIFOL(login_fd,2) = account_id;
  4119. strncpy( (char*)WFIFOP(login_fd,6), pin, PINCODE_LENGTH+1 );
  4120. WFIFOSET(login_fd,11);
  4121. }
  4122. void pincode_notifyLoginPinError( int account_id ){
  4123. WFIFOHEAD(login_fd,6);
  4124. WFIFOW(login_fd,0) = 0x2739;
  4125. WFIFOL(login_fd,2) = account_id;
  4126. WFIFOSET(login_fd,6);
  4127. }
  4128. void pincode_decrypt( uint32 userSeed, char* pin ){
  4129. int i, pos;
  4130. char tab[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  4131. char *buf;
  4132. uint32 multiplier = 0x3498, baseSeed = 0x881234;
  4133. for( i = 1; i < 10; i++ ){
  4134. userSeed = baseSeed + userSeed * multiplier;
  4135. pos = userSeed % ( i + 1 );
  4136. if( i != pos ){
  4137. tab[i] ^= tab[pos];
  4138. tab[pos] ^= tab[i];
  4139. tab[i] ^= tab[pos];
  4140. }
  4141. }
  4142. buf = (char *)malloc( sizeof(char) * ( PINCODE_LENGTH + 1 ) );
  4143. memset( buf, 0, PINCODE_LENGTH + 1 );
  4144. for( i = 0; i < PINCODE_LENGTH; i++ ){
  4145. sprintf( buf + i, "%d", tab[pin[i] - '0'] );
  4146. }
  4147. strcpy( pin, buf );
  4148. free( buf );
  4149. }
  4150. //------------------------------------------------
  4151. //Add On system
  4152. //------------------------------------------------
  4153. void moveCharSlot( int fd, struct char_session_data* sd, unsigned short from, unsigned short to ){
  4154. // Have we changed to often or is it disabled?
  4155. if( !char_move_enabled || ( !char_moves_unlimited && sd->char_moves[from] <= 0 ) ){
  4156. moveCharSlotReply( fd, sd, from, 1 );
  4157. return;
  4158. }
  4159. // We dont even have a character on the chosen slot?
  4160. if( sd->found_char[from] <= 0 ){
  4161. moveCharSlotReply( fd, sd, from, 1 );
  4162. return;
  4163. }
  4164. if( sd->found_char[to] > 0 ){
  4165. // We want to move to a used position
  4166. if( char_movetoused ){ // TODO: check if the target is in deletion process
  4167. // Admin is friendly and uses triangle exchange
  4168. if( SQL_ERROR == Sql_QueryStr(sql_handle, "START TRANSACTION")
  4169. || SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `char_num`='%d' WHERE `char_id` = '%d'", char_db, to, sd->found_char[from] )
  4170. || SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `char_num`='%d' WHERE `char_id` = '%d'", char_db, from, sd->found_char[to] )
  4171. || SQL_ERROR == Sql_QueryStr(sql_handle, "COMMIT")
  4172. ){
  4173. moveCharSlotReply( fd, sd, from, 1 );
  4174. Sql_ShowDebug(sql_handle);
  4175. Sql_QueryStr(sql_handle,"ROLLBACK");
  4176. return;
  4177. }
  4178. }else{
  4179. // Admin doesnt allow us to
  4180. moveCharSlotReply( fd, sd, from, 1 );
  4181. return;
  4182. }
  4183. }else if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `char_num`='%d' WHERE `char_id`='%d'", char_db, to, sd->found_char[from] ) ){
  4184. Sql_ShowDebug(sql_handle);
  4185. moveCharSlotReply( fd, sd, from, 1 );
  4186. return;
  4187. }
  4188. if( !char_moves_unlimited ){
  4189. sd->char_moves[from]--;
  4190. Sql_Query(sql_handle, "UPDATE `%s` SET `moves`='%d' WHERE `char_id`='%d'", char_db, sd->char_moves[from], sd->found_char[from] );
  4191. }
  4192. // We successfully moved the char - time to notify the client
  4193. moveCharSlotReply( fd, sd, from, 0 );
  4194. mmo_char_send(fd, sd);
  4195. }
  4196. // reason
  4197. // 0: success
  4198. // 1: failed
  4199. void moveCharSlotReply( int fd, struct char_session_data* sd, unsigned short index, short reason ){
  4200. WFIFOHEAD(fd,8);
  4201. WFIFOW(fd,0) = 0x8d5;
  4202. WFIFOW(fd,2) = 8;
  4203. WFIFOW(fd,4) = reason;
  4204. WFIFOW(fd,6) = sd->char_moves[index];
  4205. WFIFOSET(fd,8);
  4206. }
  4207. //------------------------------------------------
  4208. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  4209. //replies/disconnect the player we tried to kick. [Skotlex]
  4210. //------------------------------------------------
  4211. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr_t data)
  4212. {
  4213. struct online_char_data* character;
  4214. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  4215. { //Mark it offline due to timeout.
  4216. character->waiting_disconnect = INVALID_TIMER;
  4217. set_char_offline(character->char_id, character->account_id);
  4218. }
  4219. return 0;
  4220. }
  4221. /**
  4222. * @see DBApply
  4223. */
  4224. static int online_data_cleanup_sub(DBKey key, DBData *data, va_list ap)
  4225. {
  4226. struct online_char_data *character= db_data2ptr(data);
  4227. if (character->fd != -1)
  4228. return 0; //Character still connected
  4229. if (character->server == -2) //Unknown server.. set them offline
  4230. set_char_offline(character->char_id, character->account_id);
  4231. if (character->server < 0)
  4232. //Free data from players that have not been online for a while.
  4233. db_remove(online_char_db, key);
  4234. return 0;
  4235. }
  4236. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr_t data)
  4237. {
  4238. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  4239. return 0;
  4240. }
  4241. //----------------------------------
  4242. // Reading Lan Support configuration
  4243. // Rewrote: Anvanced subnet check [LuzZza]
  4244. //----------------------------------
  4245. int char_lan_config_read(const char *lancfgName)
  4246. {
  4247. FILE *fp;
  4248. int line_num = 0;
  4249. char line[1024], w1[64], w2[64], w3[64], w4[64];
  4250. if((fp = fopen(lancfgName, "r")) == NULL) {
  4251. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  4252. return 1;
  4253. }
  4254. while(fgets(line, sizeof(line), fp)) {
  4255. line_num++;
  4256. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  4257. continue;
  4258. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  4259. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  4260. continue;
  4261. }
  4262. remove_control_chars(w1);
  4263. remove_control_chars(w2);
  4264. remove_control_chars(w3);
  4265. remove_control_chars(w4);
  4266. if( strcmpi(w1, "subnet") == 0 )
  4267. {
  4268. subnet[subnet_count].mask = str2ip(w2);
  4269. subnet[subnet_count].char_ip = str2ip(w3);
  4270. subnet[subnet_count].map_ip = str2ip(w4);
  4271. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  4272. {
  4273. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  4274. continue;
  4275. }
  4276. subnet_count++;
  4277. }
  4278. }
  4279. if( subnet_count > 1 ) /* only useful if there is more than 1 */
  4280. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  4281. fclose(fp);
  4282. return 0;
  4283. }
  4284. void sql_config_read(const char* cfgName)
  4285. {
  4286. char line[1024], w1[1024], w2[1024];
  4287. FILE* fp;
  4288. if ((fp = fopen(cfgName, "r")) == NULL) {
  4289. ShowError("File not found: %s\n", cfgName);
  4290. return;
  4291. }
  4292. while(fgets(line, sizeof(line), fp)) {
  4293. if(line[0] == '/' && line[1] == '/')
  4294. continue;
  4295. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  4296. continue;
  4297. if(!strcmpi(w1,"char_db"))
  4298. safestrncpy(char_db, w2, sizeof(char_db));
  4299. else if(!strcmpi(w1,"scdata_db"))
  4300. safestrncpy(scdata_db, w2, sizeof(scdata_db));
  4301. else if(!strcmpi(w1,"cart_db"))
  4302. safestrncpy(cart_db, w2, sizeof(cart_db));
  4303. else if(!strcmpi(w1,"inventory_db"))
  4304. safestrncpy(inventory_db, w2, sizeof(inventory_db));
  4305. else if(!strcmpi(w1,"charlog_db"))
  4306. safestrncpy(charlog_db, w2, sizeof(charlog_db));
  4307. else if(!strcmpi(w1,"storage_db"))
  4308. safestrncpy(storage_db, w2, sizeof(storage_db));
  4309. else if(!strcmpi(w1,"reg_db"))
  4310. safestrncpy(reg_db, w2, sizeof(reg_db));
  4311. else if(!strcmpi(w1,"skill_db"))
  4312. safestrncpy(skill_db, w2, sizeof(skill_db));
  4313. else if(!strcmpi(w1,"interlog_db"))
  4314. safestrncpy(interlog_db, w2, sizeof(interlog_db));
  4315. else if(!strcmpi(w1,"memo_db"))
  4316. safestrncpy(memo_db, w2, sizeof(memo_db));
  4317. else if(!strcmpi(w1,"guild_db"))
  4318. safestrncpy(guild_db, w2, sizeof(guild_db));
  4319. else if(!strcmpi(w1,"guild_alliance_db"))
  4320. safestrncpy(guild_alliance_db, w2, sizeof(guild_alliance_db));
  4321. else if(!strcmpi(w1,"guild_castle_db"))
  4322. safestrncpy(guild_castle_db, w2, sizeof(guild_castle_db));
  4323. else if(!strcmpi(w1,"guild_expulsion_db"))
  4324. safestrncpy(guild_expulsion_db, w2, sizeof(guild_expulsion_db));
  4325. else if(!strcmpi(w1,"guild_member_db"))
  4326. safestrncpy(guild_member_db, w2, sizeof(guild_member_db));
  4327. else if(!strcmpi(w1,"guild_skill_db"))
  4328. safestrncpy(guild_skill_db, w2, sizeof(guild_skill_db));
  4329. else if(!strcmpi(w1,"guild_position_db"))
  4330. safestrncpy(guild_position_db, w2, sizeof(guild_position_db));
  4331. else if(!strcmpi(w1,"guild_storage_db"))
  4332. safestrncpy(guild_storage_db, w2, sizeof(guild_storage_db));
  4333. else if(!strcmpi(w1,"party_db"))
  4334. safestrncpy(party_db, w2, sizeof(party_db));
  4335. else if(!strcmpi(w1,"pet_db"))
  4336. safestrncpy(pet_db, w2, sizeof(pet_db));
  4337. else if(!strcmpi(w1,"mail_db"))
  4338. safestrncpy(mail_db, w2, sizeof(mail_db));
  4339. else if(!strcmpi(w1,"auction_db"))
  4340. safestrncpy(auction_db, w2, sizeof(auction_db));
  4341. else if(!strcmpi(w1,"friend_db"))
  4342. safestrncpy(friend_db, w2, sizeof(friend_db));
  4343. else if(!strcmpi(w1,"hotkey_db"))
  4344. safestrncpy(hotkey_db, w2, sizeof(hotkey_db));
  4345. else if(!strcmpi(w1,"quest_db"))
  4346. safestrncpy(quest_db,w2,sizeof(quest_db));
  4347. else if(!strcmpi(w1,"homunculus_db"))
  4348. safestrncpy(homunculus_db,w2,sizeof(homunculus_db));
  4349. else if(!strcmpi(w1,"skill_homunculus_db"))
  4350. safestrncpy(skill_homunculus_db,w2,sizeof(skill_homunculus_db));
  4351. else if(!strcmpi(w1,"mercenary_db"))
  4352. safestrncpy(mercenary_db,w2,sizeof(mercenary_db));
  4353. else if(!strcmpi(w1,"mercenary_owner_db"))
  4354. safestrncpy(mercenary_owner_db,w2,sizeof(mercenary_owner_db));
  4355. else if(!strcmpi(w1,"elemental_db"))
  4356. safestrncpy(elemental_db,w2,sizeof(elemental_db));
  4357. //support the import command, just like any other config
  4358. else if(!strcmpi(w1,"import"))
  4359. sql_config_read(w2);
  4360. }
  4361. fclose(fp);
  4362. ShowInfo("Done reading %s.\n", cfgName);
  4363. }
  4364. int char_config_read(const char* cfgName)
  4365. {
  4366. char line[1024], w1[1024], w2[1024];
  4367. FILE* fp = fopen(cfgName, "r");
  4368. if (fp == NULL) {
  4369. ShowError("Configuration file not found: %s.\n", cfgName);
  4370. return 1;
  4371. }
  4372. while(fgets(line, sizeof(line), fp)) {
  4373. if (line[0] == '/' && line[1] == '/')
  4374. continue;
  4375. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  4376. continue;
  4377. remove_control_chars(w1);
  4378. remove_control_chars(w2);
  4379. if(strcmpi(w1,"timestamp_format") == 0) {
  4380. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  4381. } else if(strcmpi(w1,"console_silent")==0){
  4382. msg_silent = atoi(w2);
  4383. if( msg_silent ) /* only bother if its actually enabled */
  4384. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  4385. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  4386. stdout_with_ansisequence = config_switch(w2);
  4387. } else if (strcmpi(w1, "userid") == 0) {
  4388. safestrncpy(userid, w2, sizeof(userid));
  4389. } else if (strcmpi(w1, "passwd") == 0) {
  4390. safestrncpy(passwd, w2, sizeof(passwd));
  4391. } else if (strcmpi(w1, "server_name") == 0) {
  4392. safestrncpy(server_name, w2, sizeof(server_name));
  4393. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  4394. if (strlen(w2) >= 4) {
  4395. safestrncpy(wisp_server_name, w2, sizeof(wisp_server_name));
  4396. }
  4397. } else if (strcmpi(w1, "login_ip") == 0) {
  4398. login_ip = host2ip(w2);
  4399. if (login_ip) {
  4400. char ip_str[16];
  4401. safestrncpy(login_ip_str, w2, sizeof(login_ip_str));
  4402. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  4403. }
  4404. } else if (strcmpi(w1, "login_port") == 0) {
  4405. login_port = atoi(w2);
  4406. } else if (strcmpi(w1, "char_ip") == 0) {
  4407. char_ip = host2ip(w2);
  4408. if (char_ip) {
  4409. char ip_str[16];
  4410. safestrncpy(char_ip_str, w2, sizeof(char_ip_str));
  4411. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  4412. }
  4413. } else if (strcmpi(w1, "bind_ip") == 0) {
  4414. bind_ip = host2ip(w2);
  4415. if (bind_ip) {
  4416. char ip_str[16];
  4417. safestrncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  4418. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  4419. }
  4420. } else if (strcmpi(w1, "char_port") == 0) {
  4421. char_port = atoi(w2);
  4422. } else if (strcmpi(w1, "char_maintenance") == 0) {
  4423. char_maintenance = atoi(w2);
  4424. } else if (strcmpi(w1, "char_new") == 0) {
  4425. char_new = (bool)atoi(w2);
  4426. } else if (strcmpi(w1, "char_new_display") == 0) {
  4427. char_new_display = atoi(w2);
  4428. } else if (strcmpi(w1, "max_connect_user") == 0) {
  4429. max_connect_user = atoi(w2);
  4430. if (max_connect_user < -1)
  4431. max_connect_user = -1;
  4432. } else if(strcmpi(w1, "gm_allow_group") == 0) {
  4433. gm_allow_group = atoi(w2);
  4434. } else if (strcmpi(w1, "autosave_time") == 0) {
  4435. autosave_interval = atoi(w2)*1000;
  4436. if (autosave_interval <= 0)
  4437. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  4438. } else if (strcmpi(w1, "save_log") == 0) {
  4439. save_log = config_switch(w2);
  4440. } else if (strcmpi(w1, "start_point") == 0) {
  4441. char map[MAP_NAME_LENGTH_EXT];
  4442. int x, y;
  4443. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  4444. continue;
  4445. start_point.map = mapindex_name2id(map);
  4446. if (!start_point.map)
  4447. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  4448. start_point.x = x;
  4449. start_point.y = y;
  4450. } else if (strcmpi(w1, "start_zeny") == 0) {
  4451. start_zeny = atoi(w2);
  4452. if (start_zeny < 0)
  4453. start_zeny = 0;
  4454. } else if (strcmpi(w1, "start_items") == 0) {
  4455. int i=0, n=0;
  4456. char *lineitem, **fields;
  4457. int fields_length = 3+1;
  4458. fields = (char**)aMalloc(fields_length*sizeof(char*));
  4459. lineitem = strtok(w2, ":");
  4460. while (lineitem != NULL) {
  4461. n = sv_split(lineitem, strlen(lineitem), 0, ',', fields, fields_length, SV_NOESCAPE_NOTERMINATE);
  4462. if(n+1 < fields_length){
  4463. ShowDebug("start_items: not enough arguments for %s! Skipping...\n",lineitem);
  4464. lineitem = strtok(NULL, ":"); //next itemline
  4465. continue;
  4466. }
  4467. if(i > MAX_STARTITEM){
  4468. ShowDebug("start_items: too many items, only %d are allowed! Ignoring parameter %s...\n",MAX_STARTITEM,lineitem);
  4469. } else {
  4470. start_items[i].nameid = max(0,atoi(fields[1]));
  4471. start_items[i].amount = max(0,atoi(fields[2]));
  4472. start_items[i].pos = max(0,atoi(fields[3]));
  4473. }
  4474. lineitem = strtok(NULL, ":"); //next itemline
  4475. i++;
  4476. }
  4477. aFree(fields);
  4478. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  4479. log_char = atoi(w2);
  4480. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  4481. safestrncpy(unknown_char_name, w2, sizeof(unknown_char_name));
  4482. unknown_char_name[NAME_LENGTH-1] = '\0';
  4483. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  4484. name_ignoring_case = (bool)config_switch(w2);
  4485. } else if (strcmpi(w1, "char_name_option") == 0) {
  4486. char_name_option = atoi(w2);
  4487. } else if (strcmpi(w1, "char_name_letters") == 0) {
  4488. safestrncpy(char_name_letters, w2, sizeof(char_name_letters));
  4489. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  4490. char_per_account = atoi(w2);
  4491. if( char_per_account == 0 || char_per_account > MAX_CHARS ) {
  4492. if( char_per_account > MAX_CHARS )
  4493. ShowWarning("Max chars per account '%d' exceeded limit. Defaulting to '%d'.\n", char_per_account, MAX_CHARS);
  4494. char_per_account = MAX_CHARS;
  4495. }
  4496. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  4497. char_del_level = atoi(w2);
  4498. } else if (strcmpi(w1, "char_del_delay") == 0) {
  4499. char_del_delay = atoi(w2);
  4500. } else if(strcmpi(w1,"db_path")==0) {
  4501. safestrncpy(db_path, w2, sizeof(db_path));
  4502. } else if (strcmpi(w1, "console") == 0) {
  4503. console = config_switch(w2);
  4504. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  4505. fame_list_size_chemist = atoi(w2);
  4506. if (fame_list_size_chemist > MAX_FAME_LIST) {
  4507. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  4508. fame_list_size_chemist = MAX_FAME_LIST;
  4509. }
  4510. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  4511. fame_list_size_smith = atoi(w2);
  4512. if (fame_list_size_smith > MAX_FAME_LIST) {
  4513. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  4514. fame_list_size_smith = MAX_FAME_LIST;
  4515. }
  4516. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  4517. fame_list_size_taekwon = atoi(w2);
  4518. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  4519. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  4520. fame_list_size_taekwon = MAX_FAME_LIST;
  4521. }
  4522. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  4523. guild_exp_rate = atoi(w2);
  4524. } else if (strcmpi(w1, "pincode_enabled") == 0) {
  4525. pincode_enabled = config_switch(w2);
  4526. #if PACKETVER < 20110309
  4527. if( pincode_enabled ) {
  4528. ShowWarning("pincode_enabled requires PACKETVER 20110309 or higher. Disabling...\n");
  4529. pincode_enabled = false;
  4530. }
  4531. #endif
  4532. } else if (strcmpi(w1, "pincode_changetime") == 0) {
  4533. pincode_changetime = atoi(w2)*60*60*24;
  4534. } else if (strcmpi(w1, "pincode_maxtry") == 0) {
  4535. pincode_maxtry = atoi(w2);
  4536. } else if (strcmpi(w1, "pincode_force") == 0) {
  4537. pincode_force = config_switch(w2);
  4538. } else if (strcmpi(w1, "char_move_enabled") == 0) {
  4539. char_move_enabled = config_switch(w2);
  4540. } else if (strcmpi(w1, "char_movetoused") == 0) {
  4541. char_movetoused = config_switch(w2);
  4542. } else if (strcmpi(w1, "char_moves_unlimited") == 0) {
  4543. char_moves_unlimited = config_switch(w2);
  4544. } else if (strcmpi(w1, "import") == 0) {
  4545. char_config_read(w2);
  4546. }
  4547. }
  4548. fclose(fp);
  4549. ShowInfo("Done reading %s.\n", cfgName);
  4550. return 0;
  4551. }
  4552. void do_final(void)
  4553. {
  4554. ShowStatus("Terminating...\n");
  4555. set_all_offline(-1);
  4556. set_all_offline_sql();
  4557. inter_final();
  4558. flush_fifos();
  4559. do_final_msg();
  4560. do_final_mapif();
  4561. do_final_loginif();
  4562. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s`", ragsrvinfo_db) )
  4563. Sql_ShowDebug(sql_handle);
  4564. char_db_->destroy(char_db_, NULL);
  4565. online_char_db->destroy(online_char_db, NULL);
  4566. auth_db->destroy(auth_db, NULL);
  4567. if( char_fd != -1 )
  4568. {
  4569. do_close(char_fd);
  4570. char_fd = -1;
  4571. }
  4572. Sql_Free(sql_handle);
  4573. mapindex_final();
  4574. ShowStatus("Finished.\n");
  4575. }
  4576. //------------------------------
  4577. // Function called when the server
  4578. // has received a crash signal.
  4579. //------------------------------
  4580. void do_abort(void)
  4581. {
  4582. }
  4583. void set_server_type(void)
  4584. {
  4585. SERVER_TYPE = ATHENA_SERVER_CHAR;
  4586. }
  4587. /// Called when a terminate signal is received.
  4588. void do_shutdown(void)
  4589. {
  4590. if( runflag != CHARSERVER_ST_SHUTDOWN )
  4591. {
  4592. int id;
  4593. runflag = CHARSERVER_ST_SHUTDOWN;
  4594. ShowStatus("Shutting down...\n");
  4595. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  4596. for( id = 0; id < ARRAYLENGTH(server); ++id )
  4597. mapif_server_reset(id);
  4598. loginif_check_shutdown();
  4599. flush_fifos();
  4600. runflag = CORE_ST_STOP;
  4601. }
  4602. }
  4603. int do_init(int argc, char **argv)
  4604. {
  4605. //Read map indexes
  4606. mapindex_init();
  4607. start_point.map = mapindex_name2id("new_zone01");
  4608. CHAR_CONF_NAME = "conf/char_athena.conf";
  4609. LAN_CONF_NAME = "conf/subnet_athena.conf";
  4610. SQL_CONF_NAME = "conf/inter_athena.conf";
  4611. MSG_CONF_NAME_EN = "conf/msg_conf/char_msg.conf";
  4612. cli_get_options(argc,argv);
  4613. msg_config_read(MSG_CONF_NAME_EN);
  4614. char_config_read(CHAR_CONF_NAME);
  4615. char_lan_config_read(LAN_CONF_NAME);
  4616. sql_config_read(SQL_CONF_NAME);
  4617. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  4618. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  4619. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  4620. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  4621. }
  4622. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server configuration
  4623. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4624. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  4625. mmo_char_sql_init();
  4626. char_read_fame_list(); //Read fame lists.
  4627. if ((naddr_ != 0) && (!login_ip || !char_ip))
  4628. {
  4629. char ip_str[16];
  4630. ip2str(addr_[0], ip_str);
  4631. if (naddr_ > 1)
  4632. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  4633. else
  4634. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  4635. if (!login_ip) {
  4636. safestrncpy(login_ip_str, ip_str, sizeof(login_ip_str));
  4637. login_ip = str2ip(login_ip_str);
  4638. }
  4639. if (!char_ip) {
  4640. safestrncpy(char_ip_str, ip_str, sizeof(char_ip_str));
  4641. char_ip = str2ip(char_ip_str);
  4642. }
  4643. }
  4644. do_init_loginif();
  4645. do_init_mapif();
  4646. // periodically update the overall user count on all mapservers + login server
  4647. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  4648. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  4649. // Timer to clear (online_char_db)
  4650. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  4651. // Online Data timers (checking if char still connected)
  4652. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  4653. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  4654. //Cleaning the tables for NULL entrys @ startup [Sirius]
  4655. //Chardb clean
  4656. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", char_db) )
  4657. Sql_ShowDebug(sql_handle);
  4658. //guilddb clean
  4659. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db) )
  4660. Sql_ShowDebug(sql_handle);
  4661. //guildmemberdb clean
  4662. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db) )
  4663. Sql_ShowDebug(sql_handle);
  4664. set_defaultparse(parse_char);
  4665. if( (char_fd = make_listen_bind(bind_ip,char_port)) == -1 ) {
  4666. ShowFatalError("Failed to bind to port '"CL_WHITE"%d"CL_RESET"'\n",char_port);
  4667. exit(EXIT_FAILURE);
  4668. }
  4669. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  4670. if( runflag != CORE_ST_STOP )
  4671. {
  4672. shutdown_callback = do_shutdown;
  4673. runflag = CHARSERVER_ST_RUNNING;
  4674. }
  4675. if( console ){ //start listening
  4676. add_timer_func_list(parse_console_timer, "parse_console_timer");
  4677. add_timer_interval(gettick()+1000, parse_console_timer, 0, 0, 1000); //start in 1s each 1sec
  4678. }
  4679. return 0;
  4680. }
  4681. int char_msg_config_read(char *cfgName){
  4682. return _msg_config_read(cfgName,CHAR_MAX_MSG,msg_table);
  4683. }
  4684. const char* char_msg_txt(int msg_number){
  4685. return _msg_txt(msg_number,CHAR_MAX_MSG,msg_table);
  4686. }
  4687. void char_do_final_msg(void){
  4688. _do_final_msg(CHAR_MAX_MSG,msg_table);
  4689. }
  4690. /*======================================================
  4691. * Login-Server help option info
  4692. *------------------------------------------------------*/
  4693. void display_helpscreen(bool do_exit)
  4694. {
  4695. ShowInfo("Usage: %s [options]\n", SERVER_NAME);
  4696. ShowInfo("\n");
  4697. ShowInfo("Options:\n");
  4698. ShowInfo(" -?, -h [--help]\t\tDisplays this help screen.\n");
  4699. ShowInfo(" -v [--version]\t\tDisplays the server's version.\n");
  4700. ShowInfo(" --run-once\t\t\tCloses server after loading (testing).\n");
  4701. ShowInfo(" --char-config <file>\t\tAlternative char-server configuration.\n");
  4702. ShowInfo(" --lan-config <file>\t\tAlternative lag configuration.\n");
  4703. ShowInfo(" --inter-config <file>\t\tAlternative inter-server configuration.\n");
  4704. ShowInfo(" --msg-config <file>\t\tAlternative message configuration.\n");
  4705. if( do_exit )
  4706. exit(EXIT_SUCCESS);
  4707. }