char.c 147 KB

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