char.c 150 KB

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