char.c 138 KB

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