char.c 150 KB

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