char.c 150 KB

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