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