char.c 132 KB

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