char.c 154 KB

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