char.c 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345
  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/mmo.h"
  5. #include "../common/db.h"
  6. #include "../common/lock.h"
  7. #include "../common/malloc.h"
  8. #include "../common/core.h"
  9. #include "../common/socket.h"
  10. #include "../common/strlib.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/timer.h"
  13. #include "../common/lock.h"
  14. #include "../common/malloc.h"
  15. #include "../common/mapindex.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/utils.h"
  18. #include "../common/version.h"
  19. #include "inter.h"
  20. #include "int_guild.h"
  21. #include "int_homun.h"
  22. #include "int_pet.h"
  23. #include "int_party.h"
  24. #include "int_storage.h"
  25. #include "int_status.h"
  26. #include "char.h"
  27. #include <sys/types.h>
  28. #ifdef WIN32
  29. #include <winsock2.h>
  30. #else
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. #endif
  35. #include <time.h>
  36. #include <signal.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. // private declarations
  43. #define CHAR_CONF_NAME "conf/char_athena.conf"
  44. #define LAN_CONF_NAME "conf/subnet_athena.conf"
  45. char char_txt[1024] = "save/athena.txt";
  46. char friends_txt[1024] = "save/friends.txt";
  47. char hotkeys_txt[1024] = "save/hotkeys.txt";
  48. char char_log_filename[1024] = "log/char.log";
  49. int save_log = 1; // show loading/saving messages
  50. #ifndef TXT_SQL_CONVERT
  51. char db_path[1024] = "db";
  52. struct mmo_map_server {
  53. int fd;
  54. uint32 ip;
  55. uint16 port;
  56. int users;
  57. unsigned short map[MAX_MAP_PER_SERVER];
  58. } server[MAX_MAP_SERVERS];
  59. int login_fd=-1, char_fd=-1;
  60. char userid[24];
  61. char passwd[24];
  62. char server_name[20];
  63. char wisp_server_name[NAME_LENGTH] = "Server";
  64. char login_ip_str[128];
  65. uint32 login_ip = 0;
  66. uint16 login_port = 6900;
  67. char char_ip_str[128];
  68. uint32 char_ip = 0;
  69. char bind_ip_str[128];
  70. uint32 bind_ip = INADDR_ANY;
  71. uint16 char_port = 6121;
  72. int char_maintenance = 0;
  73. bool char_new = true;
  74. int char_new_display = 0;
  75. int email_creation = 0; // disabled by default
  76. bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  77. 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]
  78. char unknown_char_name[1024] = "Unknown"; // Name to use when the requested name cannot be determined
  79. #define TRIM_CHARS "\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  80. char char_name_letters[1024] = ""; // list of letters/symbols authorised (or not) in a character name. by [Yor]
  81. int log_char = 1; // loggin char or not [devil]
  82. int log_inter = 1; // loggin inter or not [devil]
  83. // Advanced subnet check [LuzZza]
  84. struct s_subnet {
  85. uint32 mask;
  86. uint32 char_ip;
  87. uint32 map_ip;
  88. } subnet[16];
  89. int subnet_count = 0;
  90. struct char_session_data {
  91. bool auth; // whether the session is authed or not
  92. int account_id, login_id1, login_id2, sex;
  93. int found_char[MAX_CHARS]; // ids of chars on this account
  94. char email[40]; // e-mail (default: a@a.com) by [Yor]
  95. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  96. int gmlevel;
  97. uint32 version;
  98. uint8 clienttype;
  99. };
  100. int char_id_count = START_CHAR_NUM;
  101. struct character_data *char_dat;
  102. int char_num, char_max;
  103. int max_connect_user = 0;
  104. int gm_allow_level = 99;
  105. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  106. int start_zeny = 500;
  107. int start_weapon = 1201;
  108. int start_armor = 2301;
  109. int guild_exp_rate = 100;
  110. //Custom limits for the fame lists. [Skotlex]
  111. int fame_list_size_chemist = MAX_FAME_LIST;
  112. int fame_list_size_smith = MAX_FAME_LIST;
  113. int fame_list_size_taekwon = MAX_FAME_LIST;
  114. // Char-server-side stored fame lists [DracoRPG]
  115. struct fame_list smith_fame_list[MAX_FAME_LIST];
  116. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  117. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  118. // Initial position (it's possible to set it in conf file)
  119. struct point start_point = { 0, 53, 111 };
  120. // online players by [Yor]
  121. char online_txt_filename[1024] = "online.txt";
  122. char online_html_filename[1024] = "online.html";
  123. int online_sorting_option = 0; // sorting option to display online players in online files
  124. int online_display_option = 1; // display options: to know which columns must be displayed
  125. int online_refresh_html = 20; // refresh time (in sec) of the html file in the explorer
  126. int online_gm_display_min_level = 20; // minimum GM level to display 'GM' when we want to display it
  127. int console = 0;
  128. //-----------------------------------------------------
  129. // Auth database
  130. //-----------------------------------------------------
  131. #define AUTH_TIMEOUT 30000
  132. struct auth_node {
  133. int account_id;
  134. int char_id;
  135. uint32 login_id1;
  136. uint32 login_id2;
  137. uint32 ip;
  138. int sex;
  139. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  140. int gmlevel;
  141. };
  142. static DBMap* auth_db; // int account_id -> struct auth_node*
  143. //-----------------------------------------------------
  144. // Online User Database
  145. //-----------------------------------------------------
  146. struct online_char_data {
  147. int account_id;
  148. int char_id;
  149. int fd;
  150. int waiting_disconnect;
  151. short server;
  152. };
  153. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  154. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data);
  155. static void* create_online_char_data(DBKey key, va_list args)
  156. {
  157. struct online_char_data* character;
  158. CREATE(character, struct online_char_data, 1);
  159. character->account_id = key.i;
  160. character->char_id = -1;
  161. character->server = -1;
  162. character->fd = -1;
  163. character->waiting_disconnect = -1;
  164. return character;
  165. }
  166. void set_char_charselect(int account_id)
  167. {
  168. struct online_char_data* character;
  169. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  170. if( character->server > -1 )
  171. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  172. server[character->server].users--;
  173. character->char_id = -1;
  174. character->server = -1;
  175. if(character->waiting_disconnect != -1) {
  176. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  177. character->waiting_disconnect = -1;
  178. }
  179. if (login_fd > 0 && !session[login_fd]->flag.eof)
  180. {
  181. WFIFOHEAD(login_fd,6);
  182. WFIFOW(login_fd,0) = 0x272b;
  183. WFIFOL(login_fd,2) = account_id;
  184. WFIFOSET(login_fd,6);
  185. }
  186. }
  187. void set_char_online(int map_id, int char_id, int account_id)
  188. {
  189. struct online_char_data* character;
  190. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  191. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  192. {
  193. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  194. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  195. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  196. }
  197. //Update state data
  198. character->char_id = char_id;
  199. character->server = map_id;
  200. if( character->server > -1 )
  201. server[character->server].users++;
  202. if(character->waiting_disconnect != -1) {
  203. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  204. character->waiting_disconnect = -1;
  205. }
  206. if (login_fd > 0 && !session[login_fd]->flag.eof)
  207. {
  208. WFIFOHEAD(login_fd,6);
  209. WFIFOW(login_fd,0) = 0x272b;
  210. WFIFOL(login_fd,2) = account_id;
  211. WFIFOSET(login_fd,6);
  212. }
  213. }
  214. void set_char_offline(int char_id, int account_id)
  215. {
  216. struct online_char_data* character;
  217. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  218. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  219. if( character->server > -1 )
  220. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  221. server[character->server].users--;
  222. if(character->waiting_disconnect != -1){
  223. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  224. character->waiting_disconnect = -1;
  225. }
  226. if(character->char_id == char_id)
  227. {
  228. character->char_id = -1;
  229. character->server = -1;
  230. }
  231. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  232. }
  233. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  234. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
  235. {
  236. WFIFOHEAD(login_fd,6);
  237. WFIFOW(login_fd,0) = 0x272c;
  238. WFIFOL(login_fd,2) = account_id;
  239. WFIFOSET(login_fd,6);
  240. }
  241. }
  242. static int char_db_setoffline(DBKey key, void* data, va_list ap)
  243. {
  244. struct online_char_data* character = (struct online_char_data*)data;
  245. int server = va_arg(ap, int);
  246. if (server == -1) {
  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. } else if (character->server == server)
  254. character->server = -2; //In some map server that we aren't connected to.
  255. return 0;
  256. }
  257. static int char_db_kickoffline(DBKey key, void* data, va_list ap)
  258. {
  259. struct online_char_data* character = (struct online_char_data*)data;
  260. int server_id = va_arg(ap, int);
  261. if (server_id > -1 && character->server != server_id)
  262. return 0;
  263. //Kick out any connected characters, and set them offline as appropiate.
  264. if (character->server > -1)
  265. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  266. else if (character->waiting_disconnect == -1)
  267. set_char_offline(character->char_id, character->account_id);
  268. else
  269. return 0; // fail
  270. return 1;
  271. }
  272. void set_all_offline(int id)
  273. {
  274. if (id < 0)
  275. ShowNotice("Sending all users offline.\n");
  276. else
  277. ShowNotice("Sending users of map-server %d offline.\n",id);
  278. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  279. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  280. return;
  281. //Tell login-server to also mark all our characters as offline.
  282. WFIFOHEAD(login_fd,2);
  283. WFIFOW(login_fd,0) = 0x2737;
  284. WFIFOSET(login_fd,2);
  285. }
  286. //------------------------------
  287. // Writing function of logs file
  288. //------------------------------
  289. int char_log(char *fmt, ...)
  290. {
  291. if(log_char)
  292. {
  293. FILE *logfp;
  294. va_list ap;
  295. time_t raw_time;
  296. char tmpstr[2048];
  297. va_start(ap, fmt);
  298. logfp = fopen(char_log_filename, "a");
  299. if (logfp) {
  300. if (fmt[0] == '\0') // jump a line if no message
  301. fprintf(logfp, "\n");
  302. else {
  303. time(&raw_time);
  304. strftime(tmpstr, 24, "%d-%m-%Y %H:%M:%S", localtime(&raw_time));
  305. sprintf(tmpstr + 19, ": %s", fmt);
  306. vfprintf(logfp, tmpstr, ap);
  307. }
  308. fclose(logfp);
  309. }
  310. va_end(ap);
  311. }
  312. return 0;
  313. }
  314. //Search character data from the aid/cid givem
  315. struct mmo_charstatus* search_character(int aid, int cid)
  316. {
  317. int i;
  318. for (i = 0; i < char_num; i++) {
  319. if (char_dat[i].status.char_id == cid && char_dat[i].status.account_id == aid)
  320. return &char_dat[i].status;
  321. }
  322. return NULL;
  323. }
  324. struct mmo_charstatus* search_character_byname(char* character_name)
  325. {
  326. int i = search_character_index(character_name);
  327. if (i == -1) return NULL;
  328. return &char_dat[i].status;
  329. }
  330. // Searches if the given character is online, and returns the fd of the
  331. // map-server it is connected to.
  332. int search_character_online(int aid, int cid)
  333. {
  334. //Look for online character.
  335. struct online_char_data* character;
  336. character = idb_get(online_char_db, aid);
  337. if(character &&
  338. character->char_id == cid &&
  339. character->server > -1)
  340. return server[character->server].fd;
  341. return -1;
  342. }
  343. //----------------------------------------------
  344. // Search an character id
  345. // (return character index or -1 (if not found))
  346. // If exact character name is not found,
  347. // the function checks without case sensitive
  348. // and returns index if only 1 character is found
  349. // and similar to the searched name.
  350. //----------------------------------------------
  351. int search_character_index(char* character_name)
  352. {
  353. int i, quantity, index;
  354. quantity = 0;
  355. index = -1;
  356. for(i = 0; i < char_num; i++) {
  357. // Without case sensitive check (increase the number of similar character names found)
  358. if (stricmp(char_dat[i].status.name, character_name) == 0) {
  359. // Strict comparison (if found, we finish the function immediatly with correct value)
  360. if (strcmp(char_dat[i].status.name, character_name) == 0)
  361. return i;
  362. quantity++;
  363. index = i;
  364. }
  365. }
  366. // Here, the exact character name is not found
  367. // We return the found index of a similar account ONLY if there is 1 similar character
  368. if (quantity == 1)
  369. return index;
  370. // Exact character name is not found and 0 or more than 1 similar characters have been found ==> we say not found
  371. return -1;
  372. }
  373. /*---------------------------------------------------
  374. Make a data line for friends list
  375. --------------------------------------------------*/
  376. int mmo_friends_list_data_str(char *str, struct mmo_charstatus *p)
  377. {
  378. int i;
  379. char *str_p = str;
  380. str_p += sprintf(str_p, "%d", p->char_id);
  381. for (i=0;i<MAX_FRIENDS;i++){
  382. if (p->friends[i].account_id > 0 && p->friends[i].char_id > 0 && p->friends[i].name[0])
  383. str_p += sprintf(str_p, ",%d,%d,%s", p->friends[i].account_id, p->friends[i].char_id, p->friends[i].name);
  384. }
  385. str_p += '\0';
  386. return 0;
  387. }
  388. /*---------------------------------------------------
  389. Make a data line for hotkeys list
  390. --------------------------------------------------*/
  391. int mmo_hotkeys_tostr(char *str, struct mmo_charstatus *p)
  392. {
  393. #ifdef HOTKEY_SAVING
  394. int i;
  395. char *str_p = str;
  396. str_p += sprintf(str_p, "%d", p->char_id);
  397. for (i=0;i<MAX_HOTKEYS;i++)
  398. str_p += sprintf(str_p, ",%d,%d,%d", p->hotkeys[i].type, p->hotkeys[i].id, p->hotkeys[i].lv);
  399. str_p += '\0';
  400. #endif
  401. return 0;
  402. }
  403. //-------------------------------------------------
  404. // Function to create the character line (for save)
  405. //-------------------------------------------------
  406. int mmo_char_tostr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int reg_num)
  407. {
  408. int i,j;
  409. char *str_p = str;
  410. str_p += sprintf(str_p,
  411. "%d\t%d,%d\t%s\t%d,%d,%d\t%u,%u,%d" //Up to Zeny field
  412. "\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d" //Up to Skill Point
  413. "\t%d,%d,%d\t%d,%d,%d,%d" //Up to hom id
  414. "\t%d,%d,%d\t%d,%d,%d,%d,%d" //Up to head bottom
  415. "\t%d,%d,%d\t%d,%d,%d" //last point + save point
  416. ",%d,%d,%d,%d,%d\t", //Family info
  417. p->char_id, p->account_id, p->slot, p->name, //
  418. p->class_, p->base_level, p->job_level,
  419. p->base_exp, p->job_exp, p->zeny,
  420. p->hp, p->max_hp, p->sp, p->max_sp,
  421. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  422. p->status_point, p->skill_point,
  423. p->option, p->karma, p->manner, //
  424. p->party_id, p->guild_id, p->pet_id, p->hom_id,
  425. p->hair, p->hair_color, p->clothes_color,
  426. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  427. p->last_point.map, p->last_point.x, p->last_point.y, //
  428. p->save_point.map, p->save_point.x, p->save_point.y,
  429. p->partner_id,p->father,p->mother,p->child,p->fame);
  430. for(i = 0; i < MAX_MEMOPOINTS; i++)
  431. if (p->memo_point[i].map) {
  432. str_p += sprintf(str_p, "%d,%d,%d ", p->memo_point[i].map, p->memo_point[i].x, p->memo_point[i].y);
  433. }
  434. *(str_p++) = '\t';
  435. for(i = 0; i < MAX_INVENTORY; i++)
  436. if (p->inventory[i].nameid) {
  437. str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d",
  438. p->inventory[i].id,p->inventory[i].nameid,p->inventory[i].amount,p->inventory[i].equip,
  439. p->inventory[i].identify,p->inventory[i].refine,p->inventory[i].attribute);
  440. for(j=0; j<MAX_SLOTS; j++)
  441. str_p += sprintf(str_p,",%d",p->inventory[i].card[j]);
  442. str_p += sprintf(str_p," ");
  443. }
  444. *(str_p++) = '\t';
  445. for(i = 0; i < MAX_CART; i++)
  446. if (p->cart[i].nameid) {
  447. str_p += sprintf(str_p,"%d,%d,%d,%d,%d,%d,%d",
  448. p->cart[i].id,p->cart[i].nameid,p->cart[i].amount,p->cart[i].equip,
  449. p->cart[i].identify,p->cart[i].refine,p->cart[i].attribute);
  450. for(j=0; j<MAX_SLOTS; j++)
  451. str_p += sprintf(str_p,",%d",p->cart[i].card[j]);
  452. str_p += sprintf(str_p," ");
  453. }
  454. *(str_p++) = '\t';
  455. for(i = 0; i < MAX_SKILL; i++)
  456. if (p->skill[i].id && p->skill[i].flag != 1) {
  457. str_p += sprintf(str_p, "%d,%d ", p->skill[i].id, (p->skill[i].flag == 0) ? p->skill[i].lv : p->skill[i].flag-2);
  458. }
  459. *(str_p++) = '\t';
  460. for(i = 0; i < reg_num; i++)
  461. if (reg[i].str[0])
  462. str_p += sprintf(str_p, "%s,%s ", reg[i].str, reg[i].value);
  463. *(str_p++) = '\t';
  464. *str_p = '\0';
  465. return 0;
  466. }
  467. #endif //TXT_SQL_CONVERT
  468. //-------------------------------------------------------------------------
  469. // Function to set the character from the line (at read of characters file)
  470. //-------------------------------------------------------------------------
  471. int mmo_char_fromstr(char *str, struct mmo_charstatus *p, struct global_reg *reg, int *reg_num)
  472. {
  473. char tmp_str[3][128]; //To avoid deleting chars with too long names.
  474. int tmp_int[256];
  475. unsigned int tmp_uint[2]; //To read exp....
  476. int next, len, i, j;
  477. // initilialise character
  478. memset(p, '\0', sizeof(struct mmo_charstatus));
  479. // Char structure of version 1500 (homun + mapindex maps)
  480. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  481. "\t%d,%d,%d\t%d,%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  482. "\t%d,%d,%d\t%d,%d,%d,%d,%d,%d,%d,%d%n",
  483. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  484. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  485. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  486. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  487. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  488. &tmp_int[19], &tmp_int[20],
  489. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  490. &tmp_int[24], &tmp_int[25], &tmp_int[26], &tmp_int[44],
  491. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  492. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  493. &tmp_int[45], &tmp_int[35], &tmp_int[36],
  494. &tmp_int[46], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  495. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 48)
  496. {
  497. tmp_int[44] = 0; //Hom ID.
  498. // Char structure of version 1488 (fame field addition)
  499. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  500. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  501. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d,%d%n",
  502. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0],
  503. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  504. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  505. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  506. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  507. &tmp_int[19], &tmp_int[20],
  508. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  509. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  510. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  511. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  512. tmp_str[1], &tmp_int[35], &tmp_int[36],
  513. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  514. &tmp_int[40], &tmp_int[41], &tmp_int[42], &tmp_int[43], &next) != 47)
  515. {
  516. tmp_int[43] = 0; //Fame
  517. // Char structure of version 1363 (family data addition)
  518. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  519. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  520. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d,%d,%d,%d%n",
  521. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  522. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  523. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  524. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  525. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  526. &tmp_int[19], &tmp_int[20],
  527. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  528. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  529. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  530. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  531. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  532. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39],
  533. &tmp_int[40], &tmp_int[41], &tmp_int[42], &next) != 46)
  534. {
  535. tmp_int[40] = 0; // father
  536. tmp_int[41] = 0; // mother
  537. tmp_int[42] = 0; // child
  538. // Char structure version 1008 (marriage partner addition)
  539. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  540. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  541. "\t%127[^,],%d,%d\t%127[^,],%d,%d,%d%n",
  542. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  543. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  544. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  545. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  546. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  547. &tmp_int[19], &tmp_int[20],
  548. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  549. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  550. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  551. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  552. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  553. tmp_str[2], &tmp_int[37], &tmp_int[38], &tmp_int[39], &next) != 43)
  554. {
  555. tmp_int[39] = 0; // partner id
  556. // Char structure version 384 (pet addition)
  557. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  558. "\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  559. "\t%127[^,],%d,%d\t%127[^,],%d,%d%n",
  560. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  561. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  562. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  563. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  564. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  565. &tmp_int[19], &tmp_int[20],
  566. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  567. &tmp_int[24], &tmp_int[25], &tmp_int[26],
  568. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  569. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  570. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  571. tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 42)
  572. {
  573. tmp_int[26] = 0; // pet id
  574. // Char structure of a version 1 (original data structure)
  575. if (sscanf(str, "%d\t%d,%d\t%127[^\t]\t%d,%d,%d\t%u,%u,%d\t%d,%d,%d,%d\t%d,%d,%d,%d,%d,%d\t%d,%d"
  576. "\t%d,%d,%d\t%d,%d\t%d,%d,%d\t%d,%d,%d,%d,%d"
  577. "\t%127[^,],%d,%d\t%127[^,],%d,%d%n",
  578. &tmp_int[0], &tmp_int[1], &tmp_int[2], tmp_str[0], //
  579. &tmp_int[3], &tmp_int[4], &tmp_int[5],
  580. &tmp_uint[0], &tmp_uint[1], &tmp_int[8],
  581. &tmp_int[9], &tmp_int[10], &tmp_int[11], &tmp_int[12],
  582. &tmp_int[13], &tmp_int[14], &tmp_int[15], &tmp_int[16], &tmp_int[17], &tmp_int[18],
  583. &tmp_int[19], &tmp_int[20],
  584. &tmp_int[21], &tmp_int[22], &tmp_int[23], //
  585. &tmp_int[24], &tmp_int[25], //
  586. &tmp_int[27], &tmp_int[28], &tmp_int[29],
  587. &tmp_int[30], &tmp_int[31], &tmp_int[32], &tmp_int[33], &tmp_int[34],
  588. tmp_str[1], &tmp_int[35], &tmp_int[36], //
  589. tmp_str[2], &tmp_int[37], &tmp_int[38], &next) != 41)
  590. {
  591. ShowError("Char-loading: Unrecognized character data version, info lost!\n");
  592. ShowDebug("Character info: %s\n", str);
  593. return 0;
  594. }
  595. } // Char structure version 384 (pet addition)
  596. } // Char structure version 1008 (marriage partner addition)
  597. } // Char structure of version 1363 (family data addition)
  598. } // Char structure of version 1488 (fame field addition)
  599. //Convert save data from string to integer for older formats
  600. tmp_int[45] = mapindex_name2id(tmp_str[1]);
  601. tmp_int[46] = mapindex_name2id(tmp_str[2]);
  602. } // Char structure of version 1500 (homun + mapindex maps)
  603. memcpy(p->name, tmp_str[0], NAME_LENGTH); //Overflow protection [Skotlex]
  604. p->char_id = tmp_int[0];
  605. p->account_id = tmp_int[1];
  606. p->slot = tmp_int[2];
  607. p->class_ = tmp_int[3];
  608. p->base_level = tmp_int[4];
  609. p->job_level = tmp_int[5];
  610. p->base_exp = tmp_uint[0];
  611. p->job_exp = tmp_uint[1];
  612. p->zeny = tmp_int[8];
  613. p->hp = tmp_int[9];
  614. p->max_hp = tmp_int[10];
  615. p->sp = tmp_int[11];
  616. p->max_sp = tmp_int[12];
  617. p->str = tmp_int[13];
  618. p->agi = tmp_int[14];
  619. p->vit = tmp_int[15];
  620. p->int_ = tmp_int[16];
  621. p->dex = tmp_int[17];
  622. p->luk = tmp_int[18];
  623. p->status_point = tmp_int[19];
  624. p->skill_point = tmp_int[20];
  625. p->option = tmp_int[21];
  626. p->karma = tmp_int[22];
  627. p->manner = tmp_int[23];
  628. p->party_id = tmp_int[24];
  629. p->guild_id = tmp_int[25];
  630. p->pet_id = tmp_int[26];
  631. p->hair = tmp_int[27];
  632. p->hair_color = tmp_int[28];
  633. p->clothes_color = tmp_int[29];
  634. p->weapon = tmp_int[30];
  635. p->shield = tmp_int[31];
  636. p->head_top = tmp_int[32];
  637. p->head_mid = tmp_int[33];
  638. p->head_bottom = tmp_int[34];
  639. p->last_point.x = tmp_int[35];
  640. p->last_point.y = tmp_int[36];
  641. p->save_point.x = tmp_int[37];
  642. p->save_point.y = tmp_int[38];
  643. p->partner_id = tmp_int[39];
  644. p->father = tmp_int[40];
  645. p->mother = tmp_int[41];
  646. p->child = tmp_int[42];
  647. p->fame = tmp_int[43];
  648. p->hom_id = tmp_int[44];
  649. p->last_point.map = tmp_int[45];
  650. p->save_point.map = tmp_int[46];
  651. #ifndef TXT_SQL_CONVERT
  652. // Some checks
  653. for(i = 0; i < char_num; i++) {
  654. if (char_dat[i].status.char_id == p->char_id) {
  655. ShowError(CL_RED"mmmo_auth_init: a character has an identical id to another.\n");
  656. ShowError(" character id #%d -> new character not readed.\n", p->char_id);
  657. ShowError(" Character saved in log file."CL_RESET"\n");
  658. return -1;
  659. } else if (strcmp(char_dat[i].status.name, p->name) == 0) {
  660. ShowError(CL_RED"mmmo_auth_init: a character name already exists.\n");
  661. ShowError(" character name '%s' -> new character not read.\n", p->name);
  662. ShowError(" Character saved in log file."CL_RESET"\n");
  663. return -2;
  664. }
  665. }
  666. if (strcmpi(wisp_server_name, p->name) == 0) {
  667. ShowWarning("mmo_auth_init: ******WARNING: character name has wisp server name.\n");
  668. ShowWarning(" Character name '%s' = wisp server name '%s'.\n", p->name, wisp_server_name);
  669. ShowWarning(" Character readed. Suggestion: change the wisp server name.\n");
  670. char_log("mmo_auth_init: ******WARNING: character name has wisp server name: Character name '%s' = wisp server name '%s'.\n",
  671. p->name, wisp_server_name);
  672. }
  673. #endif //TXT_SQL_CONVERT
  674. if (str[next] == '\n' || str[next] == '\r')
  675. return 1; // 新規データ
  676. next++;
  677. for(i = 0; str[next] && str[next] != '\t'; i++) {
  678. //mapindex memo format
  679. if (sscanf(str+next, "%d,%d,%d%n", &tmp_int[2], &tmp_int[0], &tmp_int[1], &len) != 3)
  680. { //Old string-based memo format.
  681. if (sscanf(str+next, "%[^,],%d,%d%n", tmp_str[0], &tmp_int[0], &tmp_int[1], &len) != 3)
  682. return -3;
  683. tmp_int[2] = mapindex_name2id(tmp_str[0]);
  684. }
  685. if (i < MAX_MEMOPOINTS)
  686. { //Avoid overflowing (but we must also read through all saved memos)
  687. p->memo_point[i].x = tmp_int[0];
  688. p->memo_point[i].y = tmp_int[1];
  689. p->memo_point[i].map = tmp_int[2];
  690. }
  691. next += len;
  692. if (str[next] == ' ')
  693. next++;
  694. }
  695. next++;
  696. for(i = 0; str[next] && str[next] != '\t'; i++) {
  697. if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n",
  698. &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3],
  699. &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8)
  700. {
  701. p->inventory[i].id = tmp_int[0];
  702. p->inventory[i].nameid = tmp_int[1];
  703. p->inventory[i].amount = tmp_int[2];
  704. p->inventory[i].equip = tmp_int[3];
  705. p->inventory[i].identify = tmp_int[4];
  706. p->inventory[i].refine = tmp_int[5];
  707. p->inventory[i].attribute = tmp_int[6];
  708. for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++)
  709. p->inventory[i].card[j] = tmp_int[0];
  710. next += len;
  711. if (str[next] == ' ')
  712. next++;
  713. } else // invalid structure
  714. return -4;
  715. }
  716. next++;
  717. for(i = 0; str[next] && str[next] != '\t'; i++) {
  718. if(sscanf(str + next, "%d,%d,%d,%d,%d,%d,%d%[0-9,-]%n",
  719. &tmp_int[0], &tmp_int[1], &tmp_int[2], &tmp_int[3],
  720. &tmp_int[4], &tmp_int[5], &tmp_int[6], tmp_str[0], &len) == 8)
  721. {
  722. p->cart[i].id = tmp_int[0];
  723. p->cart[i].nameid = tmp_int[1];
  724. p->cart[i].amount = tmp_int[2];
  725. p->cart[i].equip = tmp_int[3];
  726. p->cart[i].identify = tmp_int[4];
  727. p->cart[i].refine = tmp_int[5];
  728. p->cart[i].attribute = tmp_int[6];
  729. for(j = 0; j < MAX_SLOTS && tmp_str[0][0] && sscanf(tmp_str[0], ",%d%[0-9,-]",&tmp_int[0], tmp_str[0]) > 0; j++)
  730. p->cart[i].card[j] = tmp_int[0];
  731. next += len;
  732. if (str[next] == ' ')
  733. next++;
  734. } else // invalid structure
  735. return -5;
  736. }
  737. next++;
  738. for(i = 0; str[next] && str[next] != '\t'; i++) {
  739. if (sscanf(str + next, "%d,%d%n", &tmp_int[0], &tmp_int[1], &len) != 2)
  740. return -6;
  741. p->skill[tmp_int[0]].id = tmp_int[0];
  742. p->skill[tmp_int[0]].lv = tmp_int[1];
  743. next += len;
  744. if (str[next] == ' ')
  745. next++;
  746. }
  747. next++;
  748. for(i = 0; str[next] && str[next] != '\t' && str[next] != '\n' && str[next] != '\r'; i++) { // global_reg実装以前のathena.txt互換のため一応'\n'チェック
  749. if (sscanf(str + next, "%[^,],%[^ ] %n", reg[i].str, reg[i].value, &len) != 2) {
  750. // because some scripts are not correct, the str can be "". So, we must check that.
  751. // If it's, we must not refuse the character, but just this REG value.
  752. // Character line will have something like: nov_2nd_cos,9 ,9 nov_1_2_cos_c,1 (here, ,9 is not good)
  753. if (str[next] == ',' && sscanf(str + next, ",%[^ ] %n", reg[i].value, &len) == 1)
  754. i--;
  755. else
  756. return -7;
  757. }
  758. next += len;
  759. if (str[next] == ' ')
  760. next++;
  761. }
  762. *reg_num = i;
  763. return 1;
  764. }
  765. //---------------------------------
  766. // Function to read friend list
  767. //---------------------------------
  768. int parse_friend_txt(struct mmo_charstatus *p)
  769. {
  770. char line[1024], temp[1024];
  771. int pos = 0, count = 0, next;
  772. int i,len;
  773. FILE *fp;
  774. // Open the file and look for the ID
  775. fp = fopen(friends_txt, "r");
  776. if(fp == NULL)
  777. return -1;
  778. while(fgets(line, sizeof(line), fp))
  779. {
  780. if(line[0] == '/' && line[1] == '/')
  781. continue;
  782. if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id)
  783. continue; //Not this line...
  784. //Read friends
  785. len = strlen(line);
  786. next = pos;
  787. for (count = 0; next < len && count < MAX_FRIENDS; count++)
  788. { //Read friends.
  789. if (sscanf(line+next, ",%d,%d,%23[^,^\n]%n",&p->friends[count].account_id,&p->friends[count].char_id, p->friends[count].name, &pos) < 3)
  790. { //Invalid friend?
  791. memset(&p->friends[count], 0, sizeof(p->friends[count]));
  792. break;
  793. }
  794. next+=pos;
  795. //What IF the name contains a comma? while the next field is not a
  796. //number, we assume it belongs to the current name. [Skotlex]
  797. //NOTE: Of course, this will fail if someone sets their name to something like
  798. //Bob,2005 but... meh, it's the problem of parsing a text file (encasing it in "
  799. //won't do as quotes are also valid name chars!)
  800. while(next < len && sscanf(line+next, ",%23[^,^\n]%n", temp, &pos) > 0)
  801. {
  802. if (atoi(temp)) //We read the next friend, just continue.
  803. break;
  804. //Append the name.
  805. next+=pos;
  806. i = strlen(p->friends[count].name);
  807. if (i + strlen(temp) +1 < NAME_LENGTH)
  808. {
  809. p->friends[count].name[i] = ',';
  810. strcpy(p->friends[count].name+i+1, temp);
  811. }
  812. } //End Guess Block
  813. } //Friend's for.
  814. break; //Found friends.
  815. }
  816. fclose(fp);
  817. return count;
  818. }
  819. //---------------------------------
  820. // Function to read hotkey list
  821. //---------------------------------
  822. int parse_hotkey_txt(struct mmo_charstatus *p)
  823. {
  824. #ifdef HOTKEY_SAVING
  825. char line[1024];
  826. int pos = 0, count = 0, next;
  827. int i,len;
  828. int type, id, lv;
  829. FILE *fp;
  830. // Open the file and look for the ID
  831. fp = fopen(hotkeys_txt, "r");
  832. if(fp == NULL)
  833. return -1;
  834. while(fgets(line, sizeof(line), fp))
  835. {
  836. if(line[0] == '/' && line[1] == '/')
  837. continue;
  838. if (sscanf(line, "%d%n",&i, &pos) < 1 || i != p->char_id)
  839. continue; //Not this line...
  840. //Read hotkeys
  841. len = strlen(line);
  842. next = pos;
  843. for (count = 0; next < len && count < MAX_HOTKEYS; count++)
  844. {
  845. if (sscanf(line+next, ",%d,%d,%d%n",&type,&id,&lv, &pos) < 3)
  846. //Invalid entry?
  847. break;
  848. p->hotkeys[count].type = type;
  849. p->hotkeys[count].id = id;
  850. p->hotkeys[count].lv = lv;
  851. next+=pos;
  852. }
  853. break; //Found hotkeys.
  854. }
  855. fclose(fp);
  856. return count;
  857. #else
  858. return 0;
  859. #endif
  860. }
  861. #ifndef TXT_SQL_CONVERT
  862. //---------------------------------
  863. // Function to read characters file
  864. //---------------------------------
  865. int mmo_char_init(void)
  866. {
  867. char line[65536];
  868. int ret, line_count;
  869. FILE* fp;
  870. char_num = 0;
  871. char_max = 0;
  872. char_dat = NULL;
  873. fp = fopen(char_txt, "r");
  874. if (fp == NULL) {
  875. ShowError("Characters file not found: %s.\n", char_txt);
  876. char_log("Characters file not found: %s.\n", char_txt);
  877. char_log("Id for the next created character: %d.\n", char_id_count);
  878. return 0;
  879. }
  880. line_count = 0;
  881. while(fgets(line, sizeof(line), fp))
  882. {
  883. int i, j;
  884. line_count++;
  885. if (line[0] == '/' && line[1] == '/')
  886. continue;
  887. j = 0;
  888. if (sscanf(line, "%d\t%%newid%%%n", &i, &j) == 1 && j > 0) {
  889. if (char_id_count < i)
  890. char_id_count = i;
  891. continue;
  892. }
  893. if (char_num >= char_max) {
  894. char_max += 256;
  895. char_dat = (struct character_data*)aRealloc(char_dat, sizeof(struct character_data) * char_max);
  896. if (!char_dat) {
  897. ShowFatalError("Out of memory: mmo_char_init (realloc of char_dat).\n");
  898. char_log("Out of memory: mmo_char_init (realloc of char_dat).\n");
  899. exit(EXIT_FAILURE);
  900. }
  901. }
  902. ret = mmo_char_fromstr(line, &char_dat[char_num].status, char_dat[char_num].global, &char_dat[char_num].global_num);
  903. // Initialize friends list
  904. parse_friend_txt(&char_dat[char_num].status); // Grab friends for the character
  905. // Initialize hotkey list
  906. parse_hotkey_txt(&char_dat[char_num].status); // Grab hotkeys for the character
  907. if (ret > 0) { // negative value or zero for errors
  908. if (char_dat[char_num].status.char_id >= char_id_count)
  909. char_id_count = char_dat[char_num].status.char_id + 1;
  910. char_num++;
  911. } else {
  912. ShowError("mmo_char_init: in characters file, unable to read the line #%d.\n", line_count);
  913. ShowError(" -> Character saved in log file.\n");
  914. switch (ret) {
  915. case -1:
  916. char_log("Duplicate character id in the next character line (character not readed):\n");
  917. break;
  918. case -2:
  919. char_log("Duplicate character name in the next character line (character not readed):\n");
  920. break;
  921. case -3:
  922. char_log("Invalid memo point structure in the next character line (character not readed):\n");
  923. break;
  924. case -4:
  925. char_log("Invalid inventory item structure in the next character line (character not readed):\n");
  926. break;
  927. case -5:
  928. char_log("Invalid cart item structure in the next character line (character not readed):\n");
  929. break;
  930. case -6:
  931. char_log("Invalid skill structure in the next character line (character not readed):\n");
  932. break;
  933. case -7:
  934. char_log("Invalid register structure in the next character line (character not readed):\n");
  935. break;
  936. default: // 0
  937. char_log("Unabled to get a character in the next line - Basic structure of line (before inventory) is incorrect (character not readed):\n");
  938. break;
  939. }
  940. char_log("%s", line);
  941. }
  942. }
  943. fclose(fp);
  944. if (char_num == 0) {
  945. ShowNotice("mmo_char_init: No character found in %s.\n", char_txt);
  946. char_log("mmo_char_init: No character found in %s.\n", char_txt);
  947. } else if (char_num == 1) {
  948. ShowStatus("mmo_char_init: 1 character read in %s.\n", char_txt);
  949. char_log("mmo_char_init: 1 character read in %s.\n", char_txt);
  950. } else {
  951. ShowStatus("mmo_char_init: %d characters read in %s.\n", char_num, char_txt);
  952. char_log("mmo_char_init: %d characters read in %s.\n", char_num, char_txt);
  953. }
  954. char_log("Id for the next created character: %d.\n", char_id_count);
  955. return 0;
  956. }
  957. //---------------------------------------------------------
  958. // Function to save characters in files (speed up by [Yor])
  959. //---------------------------------------------------------
  960. void mmo_char_sync(void)
  961. {
  962. char line[65536],f_line[1024];
  963. int i, j, k;
  964. int lock;
  965. FILE *fp,*f_fp;
  966. CREATE_BUFFER(id, int, char_num);
  967. // Sorting before save (by [Yor])
  968. for(i = 0; i < char_num; i++) {
  969. id[i] = i;
  970. for(j = 0; j < i; j++) {
  971. if ((char_dat[i].status.account_id < char_dat[id[j]].status.account_id) ||
  972. // if same account id, we sort by slot.
  973. (char_dat[i].status.account_id == char_dat[id[j]].status.account_id &&
  974. char_dat[i].status.slot < char_dat[id[j]].status.slot)) {
  975. for(k = i; k > j; k--)
  976. id[k] = id[k-1];
  977. id[j] = i; // id[i]
  978. break;
  979. }
  980. }
  981. }
  982. // Data save
  983. fp = lock_fopen(char_txt, &lock);
  984. if (fp == NULL) {
  985. ShowWarning("Server cannot save characters.\n");
  986. char_log("WARNING: Server cannot save characters.\n");
  987. } else {
  988. for(i = 0; i < char_num; i++) {
  989. mmo_char_tostr(line, &char_dat[id[i]].status, char_dat[id[i]].global, char_dat[id[i]].global_num); // use of sorted index
  990. fprintf(fp, "%s\n", line);
  991. }
  992. fprintf(fp, "%d\t%%newid%%\n", char_id_count);
  993. lock_fclose(fp, char_txt, &lock);
  994. }
  995. // Friends List data save (davidsiaw)
  996. f_fp = lock_fopen(friends_txt, &lock);
  997. for(i = 0; i < char_num; i++) {
  998. mmo_friends_list_data_str(f_line, &char_dat[id[i]].status);
  999. fprintf(f_fp, "%s\n", f_line);
  1000. }
  1001. lock_fclose(f_fp, friends_txt, &lock);
  1002. #ifdef HOTKEY_SAVING
  1003. // Hotkey List data save (Skotlex)
  1004. f_fp = lock_fopen(hotkeys_txt, &lock);
  1005. for(i = 0; i < char_num; i++) {
  1006. mmo_hotkeys_tostr(f_line, &char_dat[id[i]].status);
  1007. fprintf(f_fp, "%s\n", f_line);
  1008. }
  1009. lock_fclose(f_fp, hotkeys_txt, &lock);
  1010. #endif
  1011. DELETE_BUFFER(id);
  1012. return;
  1013. }
  1014. //----------------------------------------------------
  1015. // Function to save (in a periodic way) datas in files
  1016. //----------------------------------------------------
  1017. int mmo_char_sync_timer(int tid, unsigned int tick, int id, intptr data)
  1018. {
  1019. if (save_log)
  1020. ShowInfo("Saving all files...\n");
  1021. mmo_char_sync();
  1022. inter_save();
  1023. return 0;
  1024. }
  1025. //-----------------------------------
  1026. // Function to create a new character
  1027. //-----------------------------------
  1028. int make_new_char(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style)
  1029. {
  1030. char name[NAME_LENGTH];
  1031. int i;
  1032. safestrncpy(name, name_, NAME_LENGTH);
  1033. normalize_name(name,TRIM_CHARS);
  1034. // check length of character name
  1035. if( name[0] == '\0' )
  1036. return -2; // empty character name
  1037. // check content of character name
  1038. if( remove_control_chars(name) )
  1039. return -2; // control chars in name
  1040. // check for reserved names
  1041. if( strcmpi(name, main_chat_nick) == 0 || strcmpi(name, wisp_server_name) == 0 )
  1042. return -1; // nick reserved for internal server messages
  1043. // Check Authorised letters/symbols in the name of the character
  1044. if( char_name_option == 1 ) { // only letters/symbols in char_name_letters are authorised
  1045. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1046. if( strchr(char_name_letters, name[i]) == NULL )
  1047. return -2;
  1048. } else
  1049. if( char_name_option == 2 ) { // letters/symbols in char_name_letters are forbidden
  1050. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1051. if( strchr(char_name_letters, name[i]) != NULL )
  1052. return -2;
  1053. } // else, all letters/symbols are authorised (except control char removed before)
  1054. // check name (already in use?)
  1055. ARR_FIND( 0, char_num, i,
  1056. (name_ignoring_case && strncmp(char_dat[i].status.name, name, NAME_LENGTH) == 0) ||
  1057. (!name_ignoring_case && strncmpi(char_dat[i].status.name, name, NAME_LENGTH) == 0) );
  1058. if( i < char_num )
  1059. return -1; // name already exists
  1060. //check other inputs
  1061. if((slot >= MAX_CHARS) // slots
  1062. || (hair_style >= 24) // hair style
  1063. || (hair_color >= 9) // hair color
  1064. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1065. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1066. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1067. return -2; // invalid input
  1068. // check char slot
  1069. ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == sd->account_id && char_dat[i].status.slot == slot );
  1070. if( i < char_num )
  1071. return -2; // slot already in use
  1072. if (char_num >= char_max) {
  1073. char_max += 256;
  1074. RECREATE(char_dat, struct character_data, char_max);
  1075. if (!char_dat) {
  1076. ShowFatalError("Out of memory: make_new_char (realloc of char_dat).\n");
  1077. char_log("Out of memory: make_new_char (realloc of char_dat).\n");
  1078. exit(EXIT_FAILURE);
  1079. }
  1080. }
  1081. // validation success, log result
  1082. char_log("make new char: account: %d, slot %d, name: %s, stats: %d/%d/%d/%d/%d/%d, hair: %d, hair color: %d.\n",
  1083. sd->account_id, slot, name, str, agi, vit, int_, dex, luk, hair_style, hair_color);
  1084. i = char_num;
  1085. memset(&char_dat[i], 0, sizeof(struct character_data));
  1086. char_dat[i].status.char_id = char_id_count++;
  1087. char_dat[i].status.account_id = sd->account_id;
  1088. char_dat[i].status.slot = slot;
  1089. safestrncpy(char_dat[i].status.name,name,NAME_LENGTH);
  1090. char_dat[i].status.class_ = 0;
  1091. char_dat[i].status.base_level = 1;
  1092. char_dat[i].status.job_level = 1;
  1093. char_dat[i].status.base_exp = 0;
  1094. char_dat[i].status.job_exp = 0;
  1095. char_dat[i].status.zeny = start_zeny;
  1096. char_dat[i].status.str = str;
  1097. char_dat[i].status.agi = agi;
  1098. char_dat[i].status.vit = vit;
  1099. char_dat[i].status.int_ = int_;
  1100. char_dat[i].status.dex = dex;
  1101. char_dat[i].status.luk = luk;
  1102. char_dat[i].status.max_hp = 40 * (100 + char_dat[i].status.vit) / 100;
  1103. char_dat[i].status.max_sp = 11 * (100 + char_dat[i].status.int_) / 100;
  1104. char_dat[i].status.hp = char_dat[i].status.max_hp;
  1105. char_dat[i].status.sp = char_dat[i].status.max_sp;
  1106. char_dat[i].status.status_point = 0;
  1107. char_dat[i].status.skill_point = 0;
  1108. char_dat[i].status.option = 0;
  1109. char_dat[i].status.karma = 0;
  1110. char_dat[i].status.manner = 0;
  1111. char_dat[i].status.party_id = 0;
  1112. char_dat[i].status.guild_id = 0;
  1113. char_dat[i].status.hair = hair_style;
  1114. char_dat[i].status.hair_color = hair_color;
  1115. char_dat[i].status.clothes_color = 0;
  1116. char_dat[i].status.inventory[0].nameid = start_weapon; // Knife
  1117. char_dat[i].status.inventory[0].amount = 1;
  1118. char_dat[i].status.inventory[0].identify = 1;
  1119. char_dat[i].status.inventory[1].nameid = start_armor; // Cotton Shirt
  1120. char_dat[i].status.inventory[1].amount = 1;
  1121. char_dat[i].status.inventory[1].identify = 1;
  1122. char_dat[i].status.weapon = 0; // W_FIST
  1123. char_dat[i].status.shield = 0;
  1124. char_dat[i].status.head_top = 0;
  1125. char_dat[i].status.head_mid = 0;
  1126. char_dat[i].status.head_bottom = 0;
  1127. memcpy(&char_dat[i].status.last_point, &start_point, sizeof(start_point));
  1128. memcpy(&char_dat[i].status.save_point, &start_point, sizeof(start_point));
  1129. char_num++;
  1130. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, i, slot, name);
  1131. mmo_char_sync();
  1132. return i;
  1133. }
  1134. //----------------------------------------------------
  1135. // This function return the name of the job (by [Yor])
  1136. //----------------------------------------------------
  1137. char * job_name(int class_)
  1138. {
  1139. switch (class_) {
  1140. case JOB_NOVICE: return "Novice";
  1141. case JOB_SWORDMAN: return "Swordsman";
  1142. case JOB_MAGE: return "Mage";
  1143. case JOB_ARCHER: return "Archer";
  1144. case JOB_ACOLYTE: return "Acolyte";
  1145. case JOB_MERCHANT: return "Merchant";
  1146. case JOB_THIEF: return "Thief";
  1147. case JOB_KNIGHT: return "Knight";
  1148. case JOB_PRIEST: return "Priest";
  1149. case JOB_WIZARD: return "Wizard";
  1150. case JOB_BLACKSMITH: return "Blacksmith";
  1151. case JOB_HUNTER: return "Hunter";
  1152. case JOB_ASSASSIN: return "Assassin";
  1153. case JOB_KNIGHT2: return "Peco-Knight";
  1154. case JOB_CRUSADER: return "Crusader";
  1155. case JOB_MONK: return "Monk";
  1156. case JOB_SAGE: return "Sage";
  1157. case JOB_ROGUE: return "Rogue";
  1158. case JOB_ALCHEMIST: return "Alchemist";
  1159. case JOB_BARD: return "Bard";
  1160. case JOB_DANCER: return "Dancer";
  1161. case JOB_CRUSADER2: return "Peco-Crusader";
  1162. case JOB_WEDDING: return "Wedding";
  1163. case JOB_SUPER_NOVICE: return "Super Novice";
  1164. case JOB_GUNSLINGER: return "Gunslinger";
  1165. case JOB_NINJA: return "Ninja";
  1166. case JOB_XMAS: return "Christmas";
  1167. case JOB_NOVICE_HIGH: return "Novice High";
  1168. case JOB_SWORDMAN_HIGH: return "Swordsman High";
  1169. case JOB_MAGE_HIGH: return "Mage High";
  1170. case JOB_ARCHER_HIGH: return "Archer High";
  1171. case JOB_ACOLYTE_HIGH: return "Acolyte High";
  1172. case JOB_MERCHANT_HIGH: return "Merchant High";
  1173. case JOB_THIEF_HIGH: return "Thief High";
  1174. case JOB_LORD_KNIGHT: return "Lord Knight";
  1175. case JOB_HIGH_PRIEST: return "High Priest";
  1176. case JOB_HIGH_WIZARD: return "High Wizard";
  1177. case JOB_WHITESMITH: return "Whitesmith";
  1178. case JOB_SNIPER: return "Sniper";
  1179. case JOB_ASSASSIN_CROSS: return "Assassin Cross";
  1180. case JOB_LORD_KNIGHT2: return "Peko Knight";
  1181. case JOB_PALADIN: return "Paladin";
  1182. case JOB_CHAMPION: return "Champion";
  1183. case JOB_PROFESSOR: return "Professor";
  1184. case JOB_STALKER: return "Stalker";
  1185. case JOB_CREATOR: return "Creator";
  1186. case JOB_CLOWN: return "Clown";
  1187. case JOB_GYPSY: return "Gypsy";
  1188. case JOB_PALADIN2: return "Peko Paladin";
  1189. case JOB_BABY: return "Baby Novice";
  1190. case JOB_BABY_SWORDMAN: return "Baby Swordsman";
  1191. case JOB_BABY_MAGE: return "Baby Mage";
  1192. case JOB_BABY_ARCHER: return "Baby Archer";
  1193. case JOB_BABY_ACOLYTE: return "Baby Acolyte";
  1194. case JOB_BABY_MERCHANT: return "Baby Merchant";
  1195. case JOB_BABY_THIEF: return "Baby Thief";
  1196. case JOB_BABY_KNIGHT: return "Baby Knight";
  1197. case JOB_BABY_PRIEST: return "Baby Priest";
  1198. case JOB_BABY_WIZARD: return "Baby Wizard";
  1199. case JOB_BABY_BLACKSMITH: return "Baby Blacksmith";
  1200. case JOB_BABY_HUNTER: return "Baby Hunter";
  1201. case JOB_BABY_ASSASSIN: return "Baby Assassin";
  1202. case JOB_BABY_KNIGHT2: return "Baby Peco Knight";
  1203. case JOB_BABY_CRUSADER: return "Baby Crusader";
  1204. case JOB_BABY_MONK: return "Baby Monk";
  1205. case JOB_BABY_SAGE: return "Baby Sage";
  1206. case JOB_BABY_ROGUE: return "Baby Rogue";
  1207. case JOB_BABY_ALCHEMIST: return "Baby Alchemist";
  1208. case JOB_BABY_BARD: return "Baby Bard";
  1209. case JOB_BABY_DANCER: return "Baby Dancer";
  1210. case JOB_BABY_CRUSADER2: return "Baby Peco Crusader";
  1211. case JOB_SUPER_BABY: return "Super Baby";
  1212. case JOB_TAEKWON: return "Taekwon";
  1213. case JOB_STAR_GLADIATOR: return "Star Gladiator";
  1214. case JOB_STAR_GLADIATOR2: return "Flying Star Gladiator";
  1215. case JOB_SOUL_LINKER: return "Soul Linker";
  1216. }
  1217. return "Unknown Job";
  1218. }
  1219. static int create_online_files_sub(DBKey key, void* data, va_list va)
  1220. {
  1221. struct online_char_data *character;
  1222. int* players;
  1223. int *id;
  1224. int j,k,l;
  1225. character = (struct online_char_data*) data;
  1226. players = va_arg(va, int*);
  1227. id = va_arg(va, int*);
  1228. // check if map-server is online
  1229. if (character->server == -1 || character->char_id == -1) { //Character not currently online.
  1230. return -1;
  1231. }
  1232. j = character->server;
  1233. if (server[j].fd < 0) {
  1234. server[j].users = 0;
  1235. return -1;
  1236. }
  1237. // search position of character in char_dat and sort online characters.
  1238. for(j = 0; j < char_num; j++) {
  1239. if (char_dat[j].status.char_id != character->char_id)
  1240. continue;
  1241. id[*players] = j;
  1242. // use sorting option
  1243. switch (online_sorting_option) {
  1244. case 1: // by name (without case sensitive)
  1245. for(k = 0; k < *players; k++)
  1246. if (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0 ||
  1247. // if same name, we sort with case sensitive.
  1248. (stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) == 0 &&
  1249. strcmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1250. for(l = *players; l > k; l--)
  1251. id[l] = id[l-1];
  1252. id[k] = j; // id[*players]
  1253. break;
  1254. }
  1255. break;
  1256. case 2: // by zeny
  1257. for(k = 0; k < *players; k++)
  1258. if (char_dat[j].status.zeny < char_dat[id[k]].status.zeny ||
  1259. // if same number of zenys, we sort by name.
  1260. (char_dat[j].status.zeny == char_dat[id[k]].status.zeny &&
  1261. stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1262. for(l = *players; l > k; l--)
  1263. id[l] = id[l-1];
  1264. id[k] = j; // id[*players]
  1265. break;
  1266. }
  1267. break;
  1268. case 3: // by base level
  1269. for(k = 0; k < *players; k++)
  1270. if (char_dat[j].status.base_level < char_dat[id[k]].status.base_level ||
  1271. // if same base level, we sort by base exp.
  1272. (char_dat[j].status.base_level == char_dat[id[k]].status.base_level &&
  1273. char_dat[j].status.base_exp < char_dat[id[k]].status.base_exp)) {
  1274. for(l = *players; l > k; l--)
  1275. id[l] = id[l-1];
  1276. id[k] = j; // id[*players]
  1277. break;
  1278. }
  1279. break;
  1280. case 4: // by job (and job level)
  1281. for(k = 0; k < *players; k++)
  1282. if (char_dat[j].status.class_ < char_dat[id[k]].status.class_ ||
  1283. // if same job, we sort by job level.
  1284. (char_dat[j].status.class_ == char_dat[id[k]].status.class_ &&
  1285. char_dat[j].status.job_level < char_dat[id[k]].status.job_level) ||
  1286. // if same job and job level, we sort by job exp.
  1287. (char_dat[j].status.class_ == char_dat[id[k]].status.class_ &&
  1288. char_dat[j].status.job_level == char_dat[id[k]].status.job_level &&
  1289. char_dat[j].status.job_exp < char_dat[id[k]].status.job_exp)) {
  1290. for(l = *players; l > k; l--)
  1291. id[l] = id[l-1];
  1292. id[k] = j; // id[*players]
  1293. break;
  1294. }
  1295. break;
  1296. case 5: // by location map name
  1297. {
  1298. const char *map1, *map2;
  1299. map1 = mapindex_id2name(char_dat[j].status.last_point.map);
  1300. for(k = 0; k < *players; k++) {
  1301. map2 = mapindex_id2name(char_dat[id[k]].status.last_point.map);
  1302. if (!map1 || !map2 || //Avoid sorting if either one failed to resolve.
  1303. stricmp(map1, map2) < 0 ||
  1304. // if same map name, we sort by name.
  1305. (stricmp(map1, map2) == 0 &&
  1306. stricmp(char_dat[j].status.name, char_dat[id[k]].status.name) < 0)) {
  1307. for(l = *players; l > k; l--)
  1308. id[l] = id[l-1];
  1309. id[k] = j; // id[*players]
  1310. break;
  1311. }
  1312. }
  1313. }
  1314. break;
  1315. default: // 0 or invalid value: no sorting
  1316. break;
  1317. }
  1318. (*players)++;
  1319. break;
  1320. }
  1321. return 0;
  1322. }
  1323. //-------------------------------------------------------------
  1324. // Function to create the online files (txt and html). by [Yor]
  1325. //-------------------------------------------------------------
  1326. void create_online_files(void)
  1327. {
  1328. unsigned int k, j; // for loop with strlen comparing
  1329. int i, l; // for loops
  1330. int players; // count the number of players
  1331. FILE *fp; // for the txt file
  1332. FILE *fp2; // for the html file
  1333. char temp[256]; // to prepare what we must display
  1334. time_t time_server; // for number of seconds
  1335. struct tm *datetime; // variable for time in structure ->tm_mday, ->tm_sec, ...
  1336. int id[4096];
  1337. if (online_display_option == 0) // we display nothing, so return
  1338. return;
  1339. // Get number of online players, id of each online players, and verify if a server is offline
  1340. players = 0;
  1341. online_char_db->foreach(online_char_db, create_online_files_sub, &players, &id);
  1342. // write files
  1343. fp = fopen(online_txt_filename, "w");
  1344. if (fp != NULL) {
  1345. fp2 = fopen(online_html_filename, "w");
  1346. if (fp2 != NULL) {
  1347. // get time
  1348. time(&time_server); // get time in seconds since 1/1/1970
  1349. datetime = localtime(&time_server); // convert seconds in structure
  1350. strftime(temp, sizeof(temp), "%d %b %Y %X", datetime); // like sprintf, but only for date/time (05 dec 2003 15:12:52)
  1351. // write heading
  1352. fprintf(fp2, "<HTML>\n");
  1353. fprintf(fp2, " <META http-equiv=\"Refresh\" content=\"%d\">\n", online_refresh_html); // update on client explorer every x seconds
  1354. fprintf(fp2, " <HEAD>\n");
  1355. fprintf(fp2, " <TITLE>Online Players on %s</TITLE>\n", server_name);
  1356. fprintf(fp2, " </HEAD>\n");
  1357. fprintf(fp2, " <BODY>\n");
  1358. fprintf(fp2, " <H3>Online Players on %s (%s):</H3>\n", server_name, temp);
  1359. fprintf(fp, "Online Players on %s (%s):\n", server_name, temp);
  1360. fprintf(fp, "\n");
  1361. for (i = 0; i < players; i++) {
  1362. // if it's the first player
  1363. if (i == 0) {
  1364. j = 0; // count the number of characters for the txt version and to set the separate line
  1365. fprintf(fp2, " <table border=\"1\" cellspacing=\"1\">\n");
  1366. fprintf(fp2, " <tr>\n");
  1367. if ((online_display_option & 1) || (online_display_option & 64)) {
  1368. fprintf(fp2, " <td><b>Name</b></td>\n");
  1369. if (online_display_option & 64) {
  1370. fprintf(fp, "Name "); // 30
  1371. j += 30;
  1372. } else {
  1373. fprintf(fp, "Name "); // 25
  1374. j += 25;
  1375. }
  1376. }
  1377. if ((online_display_option & 6) == 6) {
  1378. fprintf(fp2, " <td><b>Job (levels)</b></td>\n");
  1379. fprintf(fp, "Job Levels "); // 27
  1380. j += 27;
  1381. } else if (online_display_option & 2) {
  1382. fprintf(fp2, " <td><b>Job</b></td>\n");
  1383. fprintf(fp, "Job "); // 19
  1384. j += 19;
  1385. } else if (online_display_option & 4) {
  1386. fprintf(fp2, " <td><b>Levels</b></td>\n");
  1387. fprintf(fp, " Levels "); // 8
  1388. j += 8;
  1389. }
  1390. if (online_display_option & 24) { // 8 or 16
  1391. fprintf(fp2, " <td><b>Location</b></td>\n");
  1392. if (online_display_option & 16) {
  1393. fprintf(fp, "Location ( x , y ) "); // 23
  1394. j += 23;
  1395. } else {
  1396. fprintf(fp, "Location "); // 13
  1397. j += 13;
  1398. }
  1399. }
  1400. if (online_display_option & 32) {
  1401. fprintf(fp2, " <td ALIGN=CENTER><b>zenys</b></td>\n");
  1402. fprintf(fp, " Zenys "); // 16
  1403. j += 16;
  1404. }
  1405. fprintf(fp2, " </tr>\n");
  1406. fprintf(fp, "\n");
  1407. for (k = 0; k < j; k++)
  1408. fprintf(fp, "-");
  1409. fprintf(fp, "\n");
  1410. }
  1411. fprintf(fp2, " <tr>\n");
  1412. // get id of the character (more speed)
  1413. j = id[i];
  1414. // displaying the character name
  1415. if ((online_display_option & 1) || (online_display_option & 64)) { // without/with 'GM' display
  1416. strcpy(temp, char_dat[j].status.name);
  1417. //l = isGM(char_dat[j].status.account_id);
  1418. l = 0; //FIXME: how to get the gm level?
  1419. if (online_display_option & 64) {
  1420. if (l >= online_gm_display_min_level)
  1421. fprintf(fp, "%-24s (GM) ", temp);
  1422. else
  1423. fprintf(fp, "%-24s ", temp);
  1424. } else
  1425. fprintf(fp, "%-24s ", temp);
  1426. // name of the character in the html (no < >, because that create problem in html code)
  1427. fprintf(fp2, " <td>");
  1428. if ((online_display_option & 64) && l >= online_gm_display_min_level)
  1429. fprintf(fp2, "<b>");
  1430. for (k = 0; k < strlen(temp); k++) {
  1431. switch(temp[k]) {
  1432. case '<': // <
  1433. fprintf(fp2, "&lt;");
  1434. break;
  1435. case '>': // >
  1436. fprintf(fp2, "&gt;");
  1437. break;
  1438. default:
  1439. fprintf(fp2, "%c", temp[k]);
  1440. break;
  1441. };
  1442. }
  1443. if ((online_display_option & 64) && l >= online_gm_display_min_level)
  1444. fprintf(fp2, "</b> (GM)");
  1445. fprintf(fp2, "</td>\n");
  1446. }
  1447. // displaying of the job
  1448. if (online_display_option & 6) {
  1449. char * jobname = job_name(char_dat[j].status.class_);
  1450. if ((online_display_option & 6) == 6) {
  1451. fprintf(fp2, " <td>%s %d/%d</td>\n", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level);
  1452. fprintf(fp, "%-18s %3d/%3d ", jobname, char_dat[j].status.base_level, char_dat[j].status.job_level);
  1453. } else if (online_display_option & 2) {
  1454. fprintf(fp2, " <td>%s</td>\n", jobname);
  1455. fprintf(fp, "%-18s ", jobname);
  1456. } else if (online_display_option & 4) {
  1457. fprintf(fp2, " <td>%d/%d</td>\n", char_dat[j].status.base_level, char_dat[j].status.job_level);
  1458. fprintf(fp, "%3d/%3d ", char_dat[j].status.base_level, char_dat[j].status.job_level);
  1459. }
  1460. }
  1461. // displaying of the map
  1462. if (online_display_option & 24) { // 8 or 16
  1463. // prepare map name
  1464. memcpy(temp, mapindex_id2name(char_dat[j].status.last_point.map), MAP_NAME_LENGTH);
  1465. // write map name
  1466. if (online_display_option & 16) { // map-name AND coordinates
  1467. fprintf(fp2, " <td>%s (%d, %d)</td>\n", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y);
  1468. fprintf(fp, "%-12s (%3d,%3d) ", temp, char_dat[j].status.last_point.x, char_dat[j].status.last_point.y);
  1469. } else {
  1470. fprintf(fp2, " <td>%s</td>\n", temp);
  1471. fprintf(fp, "%-12s ", temp);
  1472. }
  1473. }
  1474. // displaying nimber of zenys
  1475. if (online_display_option & 32) {
  1476. // write number of zenys
  1477. if (char_dat[j].status.zeny == 0) { // if no zeny
  1478. fprintf(fp2, " <td ALIGN=RIGHT>no zeny</td>\n");
  1479. fprintf(fp, " no zeny ");
  1480. } else {
  1481. fprintf(fp2, " <td ALIGN=RIGHT>%d z</td>\n", char_dat[j].status.zeny);
  1482. fprintf(fp, "%13d z ", char_dat[j].status.zeny);
  1483. }
  1484. }
  1485. fprintf(fp, "\n");
  1486. fprintf(fp2, " </tr>\n");
  1487. }
  1488. // If we display at least 1 player
  1489. if (players > 0) {
  1490. fprintf(fp2, " </table>\n");
  1491. fprintf(fp, "\n");
  1492. }
  1493. // Displaying number of online players
  1494. if (players == 0) {
  1495. fprintf(fp2, " <p>No user is online.</p>\n");
  1496. fprintf(fp, "No user is online.\n");
  1497. } else if (players == 1) {
  1498. fprintf(fp2, " <p>%d user is online.</p>\n", players);
  1499. fprintf(fp, "%d user is online.\n", players);
  1500. } else {
  1501. fprintf(fp2, " <p>%d users are online.</p>\n", players);
  1502. fprintf(fp, "%d users are online.\n", players);
  1503. }
  1504. fprintf(fp2, " </BODY>\n");
  1505. fprintf(fp2, "</HTML>\n");
  1506. fclose(fp2);
  1507. }
  1508. fclose(fp);
  1509. }
  1510. return;
  1511. }
  1512. //---------------------------------------------------------------------
  1513. // This function return the number of online players in all map-servers
  1514. //---------------------------------------------------------------------
  1515. int count_users(void)
  1516. {
  1517. int i, users;
  1518. users = 0;
  1519. for(i = 0; i < MAX_MAP_SERVERS; i++)
  1520. if (server[i].fd >= 0)
  1521. users += server[i].users;
  1522. return users;
  1523. }
  1524. // Writes char data to the buffer in the format used by the client.
  1525. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1526. // Returns the size
  1527. #define MAX_CHAR_BUF 110 //Max size (for WFIFOHEAD calls)
  1528. int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1529. {
  1530. unsigned short offset = 0;
  1531. uint8* buf;
  1532. if( buffer == NULL || p == NULL )
  1533. return 0;
  1534. buf = WBUFP(buffer,0);
  1535. WBUFL(buf,0) = p->char_id;
  1536. WBUFL(buf,4) = min(p->base_exp, LONG_MAX);
  1537. WBUFL(buf,8) = p->zeny;
  1538. WBUFL(buf,12) = min(p->job_exp, LONG_MAX);
  1539. WBUFL(buf,16) = p->job_level;
  1540. WBUFL(buf,20) = 0; // probably opt1
  1541. WBUFL(buf,24) = 0; // probably opt2
  1542. WBUFL(buf,28) = p->option;
  1543. WBUFL(buf,32) = p->karma;
  1544. WBUFL(buf,36) = p->manner;
  1545. WBUFW(buf,40) = min(p->status_point, SHRT_MAX);
  1546. #if PACKETVER > 20081217
  1547. WBUFL(buf,42) = p->hp;
  1548. WBUFL(buf,46) = p->max_hp;
  1549. offset+=4;
  1550. buf = WBUFP(buffer,offset);
  1551. #else
  1552. WBUFW(buf,42) = min(p->hp, SHRT_MAX);
  1553. WBUFW(buf,44) = min(p->max_hp, SHRT_MAX);
  1554. #endif
  1555. WBUFW(buf,46) = min(p->sp, SHRT_MAX);
  1556. WBUFW(buf,48) = min(p->max_sp, SHRT_MAX);
  1557. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1558. WBUFW(buf,52) = p->class_;
  1559. WBUFW(buf,54) = p->hair;
  1560. WBUFW(buf,56) = p->option&0x20 ? 0 : p->weapon; //When the weapon is sent and your option is riding, the client crashes on login!?
  1561. WBUFW(buf,58) = p->base_level;
  1562. WBUFW(buf,60) = min(p->skill_point, SHRT_MAX);
  1563. WBUFW(buf,62) = p->head_bottom;
  1564. WBUFW(buf,64) = p->shield;
  1565. WBUFW(buf,66) = p->head_top;
  1566. WBUFW(buf,68) = p->head_mid;
  1567. WBUFW(buf,70) = p->hair_color;
  1568. WBUFW(buf,72) = p->clothes_color;
  1569. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1570. WBUFB(buf,98) = min(p->str, UCHAR_MAX);
  1571. WBUFB(buf,99) = min(p->agi, UCHAR_MAX);
  1572. WBUFB(buf,100) = min(p->vit, UCHAR_MAX);
  1573. WBUFB(buf,101) = min(p->int_, UCHAR_MAX);
  1574. WBUFB(buf,102) = min(p->dex, UCHAR_MAX);
  1575. WBUFB(buf,103) = min(p->luk, UCHAR_MAX);
  1576. WBUFW(buf,104) = p->slot;
  1577. #if PACKETVER >= 20061023
  1578. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1579. offset += 2;
  1580. #endif
  1581. #if PACKETVER >= 20100720 && PACKETVER <= 20100727
  1582. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1583. offset += 16;
  1584. #endif
  1585. return 106+offset;
  1586. }
  1587. //----------------------------------------
  1588. // Function to send characters to a player
  1589. //----------------------------------------
  1590. int mmo_char_send006b(int fd, struct char_session_data* sd)
  1591. {
  1592. int i, j, found_num, offset = 0;
  1593. #if PACKETVER >= 20100413
  1594. offset += 3;
  1595. #endif
  1596. found_num = 0;
  1597. for(i = 0; i < char_num; i++) {
  1598. if (char_dat[i].status.account_id == sd->account_id) {
  1599. sd->found_char[found_num] = i;
  1600. if( ++found_num == MAX_CHARS )
  1601. break;
  1602. }
  1603. }
  1604. for(i = found_num; i < MAX_CHARS; i++)
  1605. sd->found_char[i] = -1;
  1606. j = 24 + offset; // offset
  1607. WFIFOHEAD(fd,j + found_num*MAX_CHAR_BUF);
  1608. WFIFOW(fd,0) = 0x6b;
  1609. #if PACKETVER >= 20100413
  1610. WFIFOB(fd,4) = MAX_CHARS; // Max slots.
  1611. WFIFOB(fd,5) = MAX_CHARS; // Available slots.
  1612. WFIFOB(fd,6) = MAX_CHARS; // Premium slots.
  1613. #endif
  1614. memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes
  1615. for(i = 0; i < found_num; i++)
  1616. j += mmo_char_tobuf(WFIFOP(fd,j), &char_dat[sd->found_char[i]].status);
  1617. WFIFOW(fd,2) = j; // packet len
  1618. WFIFOSET(fd,j);
  1619. return 0;
  1620. }
  1621. // 離婚(char削除時に使用)
  1622. int char_divorce(struct mmo_charstatus *cs)
  1623. {
  1624. if (cs == NULL)
  1625. return 0;
  1626. if (cs->partner_id > 0){
  1627. int i, j;
  1628. for(i = 0; i < char_num; i++) {
  1629. if (char_dat[i].status.char_id == cs->partner_id && char_dat[i].status.partner_id == cs->char_id) {
  1630. cs->partner_id = 0;
  1631. char_dat[i].status.partner_id = 0;
  1632. for(j = 0; j < MAX_INVENTORY; j++)
  1633. {
  1634. if (char_dat[i].status.inventory[j].nameid == WEDDING_RING_M || char_dat[i].status.inventory[j].nameid == WEDDING_RING_F)
  1635. memset(&char_dat[i].status.inventory[j], 0, sizeof(char_dat[i].status.inventory[0]));
  1636. if (cs->inventory[j].nameid == WEDDING_RING_M || cs->inventory[j].nameid == WEDDING_RING_F)
  1637. memset(&cs->inventory[j], 0, sizeof(cs->inventory[0]));
  1638. }
  1639. return 0;
  1640. }
  1641. }
  1642. }
  1643. return 0;
  1644. }
  1645. int char_married(int pl1,int pl2)
  1646. {
  1647. return (char_dat[pl1].status.char_id == char_dat[pl2].status.partner_id && char_dat[pl2].status.char_id == char_dat[pl1].status.partner_id);
  1648. }
  1649. int char_child(int parent_id, int child_id)
  1650. {
  1651. return (char_dat[parent_id].status.child == char_dat[child_id].status.char_id &&
  1652. ((char_dat[parent_id].status.char_id == char_dat[child_id].status.father) ||
  1653. (char_dat[parent_id].status.char_id == char_dat[child_id].status.mother)));
  1654. }
  1655. int char_family(int cid1, int cid2, int cid3)
  1656. {
  1657. int i, idx1 = -1, idx2 =-1;//, idx3 =-1;
  1658. for(i = 0; i < char_num && (idx1 == -1 || idx2 == -1/* || idx3 == 1*/); i++)
  1659. {
  1660. if (char_dat[i].status.char_id == cid1)
  1661. idx1 = i;
  1662. if (char_dat[i].status.char_id == cid2)
  1663. idx2 = i;
  1664. // if (char_dat[i].status.char_id == cid2)
  1665. // idx3 = i;
  1666. }
  1667. if (idx1 == -1 || idx2 == -1/* || idx3 == -1*/)
  1668. return 0; //Some character not found??
  1669. //Unless the dbs are corrupted, these 3 checks should suffice, even though
  1670. //we could do a lot more checks and force cross-reference integrity.
  1671. if(char_dat[idx1].status.partner_id == cid2 &&
  1672. char_dat[idx1].status.child == cid3)
  1673. return cid3; //cid1/cid2 parents. cid3 child.
  1674. if(char_dat[idx1].status.partner_id == cid3 &&
  1675. char_dat[idx1].status.child == cid2)
  1676. return cid2; //cid1/cid3 parents. cid2 child.
  1677. if(char_dat[idx2].status.partner_id == cid3 &&
  1678. char_dat[idx2].status.child == cid1)
  1679. return cid1; //cid2/cid3 parents. cid1 child.
  1680. return 0;
  1681. }
  1682. //Clears the given party id from all characters.
  1683. //Since sometimes the party format changes and parties must be wiped, this
  1684. //method is required to prevent stress during the "party not found!" stages.
  1685. void char_clearparty(int party_id)
  1686. {
  1687. int i;
  1688. for(i = 0; i < char_num; i++)
  1689. {
  1690. if (char_dat[i].status.party_id == party_id)
  1691. char_dat[i].status.party_id = 0;
  1692. }
  1693. }
  1694. //----------------------------------------------------------------------
  1695. // Force disconnection of an online player (with account value) by [Yor]
  1696. //----------------------------------------------------------------------
  1697. int disconnect_player(int account_id)
  1698. {
  1699. int i;
  1700. struct char_session_data *sd;
  1701. // disconnect player if online on char-server
  1702. for(i = 0; i < fd_max; i++) {
  1703. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1704. if (sd->account_id == account_id) {
  1705. set_eof(i);
  1706. return 1;
  1707. }
  1708. }
  1709. }
  1710. return 0;
  1711. }
  1712. // キャラ削除に伴うデータ削除
  1713. static int char_delete(struct mmo_charstatus *cs)
  1714. {
  1715. int j;
  1716. // ペット削除
  1717. if (cs->pet_id)
  1718. inter_pet_delete(cs->pet_id);
  1719. if (cs->hom_id)
  1720. inter_homun_delete(cs->hom_id);
  1721. for (j = 0; j < MAX_INVENTORY; j++)
  1722. if (cs->inventory[j].card[0] == (short)0xff00)
  1723. inter_pet_delete(MakeDWord(cs->inventory[j].card[1],cs->inventory[j].card[2]));
  1724. for (j = 0; j < MAX_CART; j++)
  1725. if (cs->cart[j].card[0] == (short)0xff00)
  1726. inter_pet_delete( MakeDWord(cs->cart[j].card[1],cs->cart[j].card[2]) );
  1727. // ギルド脱退
  1728. if (cs->guild_id)
  1729. inter_guild_leave(cs->guild_id, cs->account_id, cs->char_id);
  1730. // パーティー脱退
  1731. if (cs->party_id)
  1732. inter_party_leave(cs->party_id, cs->account_id, cs->char_id);
  1733. // 離婚
  1734. if (cs->partner_id){
  1735. // 離婚情報をmapに通知
  1736. unsigned char buf[10];
  1737. WBUFW(buf,0) = 0x2b12;
  1738. WBUFL(buf,2) = cs->char_id;
  1739. WBUFL(buf,6) = cs->partner_id;
  1740. mapif_sendall(buf,10);
  1741. // 離婚
  1742. char_divorce(cs);
  1743. }
  1744. #ifdef ENABLE_SC_SAVING
  1745. status_delete_scdata(cs->account_id, cs->char_id);
  1746. #endif
  1747. return 0;
  1748. }
  1749. static void char_auth_ok(int fd, struct char_session_data *sd)
  1750. {
  1751. struct online_char_data* character;
  1752. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1753. { // check if character is not online already. [Skotlex]
  1754. if (character->server > -1)
  1755. { //Character already online. KICK KICK KICK
  1756. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1757. if (character->waiting_disconnect == -1)
  1758. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1759. WFIFOW(fd,0) = 0x81;
  1760. WFIFOB(fd,2) = 8;
  1761. WFIFOSET(fd,3);
  1762. return;
  1763. }
  1764. if (character->fd >= 0 && character->fd != fd)
  1765. { //There's already a connection from this account that hasn't picked a char yet.
  1766. WFIFOW(fd,0) = 0x81;
  1767. WFIFOB(fd,2) = 8;
  1768. WFIFOSET(fd,3);
  1769. return;
  1770. }
  1771. character->fd = fd;
  1772. }
  1773. if (login_fd > 0) {
  1774. // request account data
  1775. WFIFOHEAD(login_fd,6);
  1776. WFIFOW(login_fd,0) = 0x2716;
  1777. WFIFOL(login_fd,2) = sd->account_id;
  1778. WFIFOSET(login_fd,6);
  1779. }
  1780. // mark session as 'authed'
  1781. sd->auth = true;
  1782. // set char online on charserver
  1783. set_char_charselect(sd->account_id);
  1784. // continues when account data is received...
  1785. }
  1786. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data);
  1787. int parse_fromlogin(int fd)
  1788. {
  1789. int i;
  1790. struct char_session_data *sd;
  1791. // only login-server can have an access to here.
  1792. // so, if it isn't the login-server, we disconnect the session.
  1793. if( fd != login_fd )
  1794. set_eof(fd);
  1795. if(session[fd]->flag.eof) {
  1796. if (fd == login_fd) {
  1797. ShowWarning("Connection to login-server lost (connection #%d).\n", fd);
  1798. login_fd = -1;
  1799. }
  1800. do_close(fd);
  1801. return 0;
  1802. }
  1803. sd = (struct char_session_data*)session[fd]->session_data;
  1804. while(RFIFOREST(fd) >= 2)
  1805. {
  1806. uint16 command = RFIFOW(fd,0);
  1807. switch( command )
  1808. {
  1809. // acknowledgement of connect-to-loginserver request
  1810. case 0x2711:
  1811. if (RFIFOREST(fd) < 3)
  1812. return 0;
  1813. if (RFIFOB(fd,2)) {
  1814. //printf("connect login server error : %d\n", RFIFOB(fd,2));
  1815. ShowError("Can not connect to login-server.\n");
  1816. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1817. ShowInfo("Also, please make sure your accounts file (default: accounts.txt) has those values present.\n");
  1818. ShowInfo("The communication passwords can be changed in map_athena.conf and char_athena.conf\n");
  1819. } else {
  1820. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1821. //Send online accounts to login server.
  1822. send_accounts_tologin(-1, gettick(), 0, 0);
  1823. // if no map-server already connected, display a message...
  1824. ARR_FIND( 0, MAX_MAP_SERVERS, i, server[i].fd > 0 && server[i].map[0] );
  1825. if( i == MAX_MAP_SERVERS )
  1826. ShowStatus("Awaiting maps from map-server.\n");
  1827. }
  1828. RFIFOSKIP(fd,3);
  1829. break;
  1830. // acknowledgement of account authentication request
  1831. case 0x2713:
  1832. if (RFIFOREST(fd) < 25)
  1833. return 0;
  1834. {
  1835. int account_id = RFIFOL(fd,2);
  1836. uint32 login_id1 = RFIFOL(fd,6);
  1837. uint32 login_id2 = RFIFOL(fd,10);
  1838. uint8 sex = RFIFOB(fd,14);
  1839. uint8 result = RFIFOB(fd,15);
  1840. int request_id = RFIFOL(fd,16);
  1841. uint32 version = RFIFOL(fd,20);
  1842. uint8 clienttype = RFIFOB(fd,24);
  1843. RFIFOSKIP(fd,25);
  1844. if( session_isActive(request_id) && (sd=(struct char_session_data*)session[request_id]->session_data) &&
  1845. !sd->auth && sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 && sd->sex == sex )
  1846. {
  1847. int client_fd = request_id;
  1848. sd->version = version;
  1849. sd->clienttype = clienttype;
  1850. switch( result )
  1851. {
  1852. case 0:// ok
  1853. char_auth_ok(client_fd, sd);
  1854. break;
  1855. case 1:// auth failed
  1856. WFIFOHEAD(client_fd,3);
  1857. WFIFOW(client_fd,0) = 0x6c;
  1858. WFIFOB(client_fd,2) = 0;// rejected from server
  1859. WFIFOSET(client_fd,3);
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. break;
  1865. case 0x2717: // account data
  1866. if (RFIFOREST(fd) < 51)
  1867. return 0;
  1868. // find the authenticated session with this account id
  1869. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) );
  1870. if( i < fd_max )
  1871. {
  1872. memcpy(sd->email, RFIFOP(fd,6), 40);
  1873. sd->expiration_time = (time_t)RFIFOL(fd,46);
  1874. sd->gmlevel = RFIFOB(fd,50);
  1875. // continued from char_auth_ok...
  1876. if( max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level )
  1877. {
  1878. // refuse connection (over populated)
  1879. WFIFOW(i,0) = 0x6c;
  1880. WFIFOW(i,2) = 0;
  1881. WFIFOSET(i,3);
  1882. }
  1883. else
  1884. {
  1885. // send characters to player
  1886. mmo_char_send006b(i, sd);
  1887. }
  1888. }
  1889. RFIFOSKIP(fd,51);
  1890. break;
  1891. // login-server alive packet
  1892. case 0x2718:
  1893. if (RFIFOREST(fd) < 2)
  1894. return 0;
  1895. RFIFOSKIP(fd,2);
  1896. break;
  1897. // changesex reply
  1898. case 0x2723:
  1899. if (RFIFOREST(fd) < 7)
  1900. return 0;
  1901. {
  1902. int i, j;
  1903. unsigned char buf[7];
  1904. int acc = RFIFOL(fd,2);
  1905. int sex = RFIFOB(fd,6);
  1906. RFIFOSKIP(fd,7);
  1907. if( acc > 0 )
  1908. {
  1909. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  1910. if( node != NULL )
  1911. node->sex = sex;
  1912. ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == acc );
  1913. if( i < char_num )
  1914. {
  1915. int jobclass = char_dat[i].status.class_;
  1916. char_dat[i].status.sex = sex;
  1917. if (jobclass == JOB_BARD || jobclass == JOB_DANCER ||
  1918. jobclass == JOB_CLOWN || jobclass == JOB_GYPSY ||
  1919. jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) {
  1920. // job modification
  1921. if (jobclass == JOB_BARD || jobclass == JOB_DANCER) {
  1922. char_dat[i].status.class_ = (sex) ? JOB_BARD : JOB_DANCER;
  1923. } else if (jobclass == JOB_CLOWN || jobclass == JOB_GYPSY) {
  1924. char_dat[i].status.class_ = (sex) ? JOB_CLOWN : JOB_GYPSY;
  1925. } else if (jobclass == JOB_BABY_BARD || jobclass == JOB_BABY_DANCER) {
  1926. char_dat[i].status.class_ = (sex) ? JOB_BABY_BARD : JOB_BABY_DANCER;
  1927. }
  1928. // remove specifical skills of classes 19, 4020 and 4042
  1929. for(j = 315; j <= 322; j++) {
  1930. if (char_dat[i].status.skill[j].id > 0 && !char_dat[i].status.skill[j].flag) {
  1931. char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv;
  1932. char_dat[i].status.skill[j].id = 0;
  1933. char_dat[i].status.skill[j].lv = 0;
  1934. }
  1935. }
  1936. // remove specifical skills of classes 20, 4021 and 4043
  1937. for(j = 323; j <= 330; j++) {
  1938. if (char_dat[i].status.skill[j].id > 0 && !char_dat[i].status.skill[j].flag) {
  1939. char_dat[i].status.skill_point += char_dat[i].status.skill[j].lv;
  1940. char_dat[i].status.skill[j].id = 0;
  1941. char_dat[i].status.skill[j].lv = 0;
  1942. }
  1943. }
  1944. }
  1945. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  1946. for (j = 0; j < MAX_INVENTORY; j++) {
  1947. if (char_dat[i].status.inventory[j].nameid && char_dat[i].status.inventory[j].equip)
  1948. char_dat[i].status.inventory[j].equip = 0;
  1949. }
  1950. char_dat[i].status.weapon = 0;
  1951. char_dat[i].status.shield = 0;
  1952. char_dat[i].status.head_top = 0;
  1953. char_dat[i].status.head_mid = 0;
  1954. char_dat[i].status.head_bottom = 0;
  1955. if (char_dat[i].status.guild_id) //If there is a guild, update the guild_member data [Skotlex]
  1956. inter_guild_sex_changed(char_dat[i].status.guild_id, acc, char_dat[i].status.char_id, sex);
  1957. }
  1958. // disconnect player if online on char-server
  1959. disconnect_player(acc);
  1960. }
  1961. WBUFW(buf,0) = 0x2b0d;
  1962. WBUFL(buf,2) = acc;
  1963. WBUFB(buf,6) = sex;
  1964. mapif_sendall(buf, 7);
  1965. }
  1966. break;
  1967. case 0x2726: // Request to send a broadcast message (no answer)
  1968. if (RFIFOREST(fd) < 8 || RFIFOREST(fd) < (8 + RFIFOL(fd,4)))
  1969. return 0;
  1970. if (RFIFOL(fd,4) < 1)
  1971. char_log("Receiving a message for broadcast, but message is void.\n");
  1972. else
  1973. {
  1974. // at least 1 map-server
  1975. ARR_FIND( 0, MAX_MAP_SERVERS, i, server[i].fd >= 0 );
  1976. if (i == MAX_MAP_SERVERS)
  1977. char_log("'ladmin': Receiving a message for broadcast, but no map-server is online.\n");
  1978. else {
  1979. unsigned char buf[128];
  1980. char message[4096]; // +1 to add a null terminated if not exist in the packet
  1981. int lp;
  1982. char *p;
  1983. memset(message, '\0', sizeof(message));
  1984. memcpy(message, RFIFOP(fd,8), RFIFOL(fd,4));
  1985. message[sizeof(message)-1] = '\0';
  1986. remove_control_chars(message);
  1987. // remove all first spaces
  1988. p = message;
  1989. while(p[0] == ' ')
  1990. p++;
  1991. // if message is only composed of spaces
  1992. if (p[0] == '\0')
  1993. char_log("Receiving a message for broadcast, but message is only a lot of spaces.\n");
  1994. // else send message to all map-servers
  1995. else {
  1996. if (RFIFOW(fd,2) == 0) {
  1997. char_log("'ladmin': Receiving a message for broadcast (message (in yellow): %s)\n",
  1998. message);
  1999. lp = 4;
  2000. } else {
  2001. char_log("'ladmin': Receiving a message for broadcast (message (in blue): %s)\n",
  2002. message);
  2003. lp = 8;
  2004. }
  2005. // split message to max 80 char
  2006. while(p[0] != '\0') { // if not finish
  2007. if (p[0] == ' ') // jump if first char is a space
  2008. p++;
  2009. else {
  2010. char split[80];
  2011. char* last_space;
  2012. sscanf(p, "%79[^\t]", split); // max 79 char, any char (\t is control char and control char was removed before)
  2013. split[sizeof(split)-1] = '\0'; // last char always \0
  2014. if ((last_space = strrchr(split, ' ')) != NULL) { // searching space from end of the string
  2015. last_space[0] = '\0'; // replace it by NULL to have correct length of split
  2016. p++; // to jump the new NULL
  2017. }
  2018. p += strlen(split);
  2019. // send broadcast to all map-servers
  2020. WBUFW(buf,0) = 0x3800;
  2021. WBUFW(buf,2) = lp + strlen(split) + 1;
  2022. WBUFL(buf,4) = 0x65756c62; // only write if in blue (lp = 8)
  2023. memcpy(WBUFP(buf,lp), split, strlen(split) + 1);
  2024. mapif_sendall(buf, WBUFW(buf,2));
  2025. }
  2026. }
  2027. }
  2028. }
  2029. }
  2030. RFIFOSKIP(fd,8 + RFIFOL(fd,4));
  2031. break;
  2032. // account_reg2変更通知
  2033. case 0x2729:
  2034. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2035. return 0;
  2036. { //Receive account_reg2 registry, forward to map servers.
  2037. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  2038. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  2039. // WBUFW(buf,0) = 0x2b11;
  2040. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  2041. mapif_sendall(buf, WBUFW(buf,2));
  2042. RFIFOSKIP(fd, RFIFOW(fd,2));
  2043. }
  2044. break;
  2045. // Account deletion notification (from login-server)
  2046. case 0x2730:
  2047. if (RFIFOREST(fd) < 6)
  2048. return 0;
  2049. // Deletion of all characters of the account
  2050. for(i = 0; i < char_num; i++) {
  2051. if (char_dat[i].status.account_id == RFIFOL(fd,2)) {
  2052. char_delete(&char_dat[i].status);
  2053. if (i < char_num - 1) {
  2054. memcpy(&char_dat[i], &char_dat[char_num-1], sizeof(struct character_data));
  2055. // if moved character owns to deleted account, check again it's character
  2056. if (char_dat[i].status.account_id == RFIFOL(fd,2)) {
  2057. i--;
  2058. // Correct moved character reference in the character's owner by [Yor]
  2059. } else {
  2060. int j, k;
  2061. struct char_session_data *sd2;
  2062. for (j = 0; j < fd_max; j++) {
  2063. if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) &&
  2064. sd2->account_id == char_dat[char_num-1].status.account_id) {
  2065. for (k = 0; k < MAX_CHARS; k++) {
  2066. if (sd2->found_char[k] == char_num-1) {
  2067. sd2->found_char[k] = i;
  2068. break;
  2069. }
  2070. }
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. }
  2076. char_num--;
  2077. }
  2078. }
  2079. // Deletion of the storage
  2080. inter_storage_delete(RFIFOL(fd,2));
  2081. // send to all map-servers to disconnect the player
  2082. {
  2083. unsigned char buf[6];
  2084. WBUFW(buf,0) = 0x2b13;
  2085. WBUFL(buf,2) = RFIFOL(fd,2);
  2086. mapif_sendall(buf, 6);
  2087. }
  2088. // disconnect player if online on char-server
  2089. disconnect_player(RFIFOL(fd,2));
  2090. RFIFOSKIP(fd,6);
  2091. break;
  2092. // State change of account/ban notification (from login-server) by [Yor]
  2093. case 0x2731:
  2094. if (RFIFOREST(fd) < 11)
  2095. return 0;
  2096. // send to all map-servers to disconnect the player
  2097. {
  2098. unsigned char buf[11];
  2099. WBUFW(buf,0) = 0x2b14;
  2100. WBUFL(buf,2) = RFIFOL(fd,2);
  2101. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  2102. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  2103. mapif_sendall(buf, 11);
  2104. }
  2105. // disconnect player if online on char-server
  2106. disconnect_player(RFIFOL(fd,2));
  2107. RFIFOSKIP(fd,11);
  2108. break;
  2109. // Login server request to kick a character out. [Skotlex]
  2110. case 0x2734:
  2111. if (RFIFOREST(fd) < 6)
  2112. return 0;
  2113. {
  2114. int aid = RFIFOL(fd,2);
  2115. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  2116. RFIFOSKIP(fd,6);
  2117. if( character != NULL )
  2118. {// account is already marked as online!
  2119. if( character->server > -1 )
  2120. { //Kick it from the map server it is on.
  2121. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2122. if (character->waiting_disconnect == -1)
  2123. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  2124. }
  2125. else
  2126. {// Manual kick from char server.
  2127. struct char_session_data *tsd;
  2128. int i;
  2129. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  2130. if( i < fd_max )
  2131. {
  2132. WFIFOHEAD(i,3);
  2133. WFIFOW(i,0) = 0x81;
  2134. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  2135. WFIFOSET(i,3);
  2136. set_eof(i);
  2137. }
  2138. else // still moving to the map-server
  2139. set_char_offline(-1, aid);
  2140. }
  2141. }
  2142. idb_remove(auth_db, aid);// reject auth attempts from map-server
  2143. }
  2144. break;
  2145. case 0x2735:
  2146. {
  2147. unsigned char buf[2];
  2148. uint32 new_ip = 0;
  2149. WBUFW(buf,0) = 0x2b1e;
  2150. mapif_sendall(buf, 2);
  2151. new_ip = host2ip(login_ip_str);
  2152. if (new_ip && new_ip != login_ip)
  2153. login_ip = new_ip; //Update login up.
  2154. new_ip = host2ip(char_ip_str);
  2155. if (new_ip && new_ip != char_ip)
  2156. { //Update ip.
  2157. char_ip = new_ip;
  2158. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  2159. WFIFOHEAD(fd,6);
  2160. WFIFOW(fd,0) = 0x2736;
  2161. WFIFOL(fd,2) = htonl(char_ip);
  2162. WFIFOSET(fd,6);
  2163. }
  2164. RFIFOSKIP(fd,2);
  2165. }
  2166. break;
  2167. default:
  2168. ShowError("Unknown packet 0x%04x received from login-server, disconnecting.\n", RFIFOW(fd,0));
  2169. set_eof(fd);
  2170. return 0;
  2171. }
  2172. }
  2173. RFIFOFLUSH(fd);
  2174. return 0;
  2175. }
  2176. int request_accreg2(int account_id, int char_id)
  2177. {
  2178. if (login_fd > 0) {
  2179. WFIFOHEAD(login_fd,10);
  2180. WFIFOW(login_fd,0) = 0x272e;
  2181. WFIFOL(login_fd,2) = account_id;
  2182. WFIFOL(login_fd,6) = char_id;
  2183. WFIFOSET(login_fd,10);
  2184. return 1;
  2185. }
  2186. return 0;
  2187. }
  2188. //Send packet forward to login-server for account saving
  2189. int save_accreg2(unsigned char* buf, int len)
  2190. {
  2191. if (login_fd > 0) {
  2192. WFIFOHEAD(login_fd,len+4);
  2193. memcpy(WFIFOP(login_fd,4), buf, len);
  2194. WFIFOW(login_fd,0) = 0x2728;
  2195. WFIFOW(login_fd,2) = len+4;
  2196. WFIFOSET(login_fd,len+4);
  2197. return 1;
  2198. }
  2199. return 0;
  2200. }
  2201. //Receive Registry information for a character.
  2202. int char_parse_Registry(int account_id, int char_id, unsigned char *buf, int buf_len)
  2203. {
  2204. int i,j,p,len;
  2205. for (i = 0; i < char_num; i++) {
  2206. if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id)
  2207. break;
  2208. }
  2209. if(i >= char_num) //Character not found?
  2210. return 1;
  2211. for(j=0,p=0;j<GLOBAL_REG_NUM && p<buf_len;j++){
  2212. sscanf((char*)WBUFP(buf,p), "%31c%n",char_dat[i].global[j].str,&len);
  2213. char_dat[i].global[j].str[len]='\0';
  2214. p +=len+1; //+1 to skip the '\0' between strings.
  2215. sscanf((char*)WBUFP(buf,p), "%255c%n",char_dat[i].global[j].value,&len);
  2216. char_dat[i].global[j].value[len]='\0';
  2217. p +=len+1;
  2218. }
  2219. char_dat[i].global_num = j;
  2220. return 0;
  2221. }
  2222. //Reply to map server with acc reg values.
  2223. int char_account_reg_reply(int fd,int account_id,int char_id)
  2224. {
  2225. int i,j,p;
  2226. WFIFOHEAD(fd, GLOBAL_REG_NUM*288 + 13);
  2227. WFIFOW(fd,0)=0x3804;
  2228. WFIFOL(fd,4)=account_id;
  2229. WFIFOL(fd,8)=char_id;
  2230. WFIFOB(fd,12)=3; //Type 3: char acc reg.
  2231. for (i = 0;i < char_num; i++) {
  2232. if (char_dat[i].status.account_id == account_id && char_dat[i].status.char_id == char_id)
  2233. break;
  2234. }
  2235. if(i >= char_num){ //Character not found? Sent empty packet.
  2236. WFIFOW(fd,2)=13;
  2237. }else{
  2238. for (p=13,j = 0; j < char_dat[i].global_num; j++) {
  2239. if (char_dat[i].global[j].str[0]) {
  2240. p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].str)+1; //We add 1 to consider the '\0' in place.
  2241. p+= sprintf((char*)WFIFOP(fd,p), "%s", char_dat[i].global[j].value)+1;
  2242. }
  2243. }
  2244. WFIFOW(fd,2)=p;
  2245. }
  2246. WFIFOSET(fd,WFIFOW(fd,2));
  2247. return 0;
  2248. }
  2249. void char_read_fame_list(void)
  2250. {
  2251. int i, j, k;
  2252. struct fame_list fame_item;
  2253. CREATE_BUFFER(id, int, char_num);
  2254. for(i = 0; i < char_num; i++) {
  2255. id[i] = i;
  2256. for(j = 0; j < i; j++) {
  2257. if (char_dat[i].status.fame > char_dat[id[j]].status.fame) {
  2258. for(k = i; k > j; k--)
  2259. id[k] = id[k-1];
  2260. id[j] = i; // id[i]
  2261. break;
  2262. }
  2263. }
  2264. }
  2265. // Empty ranking lists
  2266. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  2267. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  2268. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  2269. // Build Blacksmith ranking list
  2270. for (i = 0, j = 0; i < char_num && j < fame_list_size_smith; i++) {
  2271. if (char_dat[id[i]].status.fame && (
  2272. char_dat[id[i]].status.class_ == JOB_BLACKSMITH ||
  2273. char_dat[id[i]].status.class_ == JOB_WHITESMITH ||
  2274. char_dat[id[i]].status.class_ == JOB_BABY_BLACKSMITH))
  2275. {
  2276. fame_item.id = char_dat[id[i]].status.char_id;
  2277. fame_item.fame = char_dat[id[i]].status.fame;
  2278. strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2279. memcpy(&smith_fame_list[j],&fame_item,sizeof(struct fame_list));
  2280. j++;
  2281. }
  2282. }
  2283. // Build Alchemist ranking list
  2284. for (i = 0, j = 0; i < char_num && j < fame_list_size_chemist; i++) {
  2285. if (char_dat[id[i]].status.fame && (
  2286. char_dat[id[i]].status.class_ == JOB_ALCHEMIST ||
  2287. char_dat[id[i]].status.class_ == JOB_CREATOR ||
  2288. char_dat[id[i]].status.class_ == JOB_BABY_ALCHEMIST))
  2289. {
  2290. fame_item.id = char_dat[id[i]].status.char_id;
  2291. fame_item.fame = char_dat[id[i]].status.fame;
  2292. strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2293. memcpy(&chemist_fame_list[j],&fame_item,sizeof(struct fame_list));
  2294. j++;
  2295. }
  2296. }
  2297. // Build Taekwon ranking list
  2298. for (i = 0, j = 0; i < char_num && j < fame_list_size_taekwon; i++) {
  2299. if (char_dat[id[i]].status.fame &&
  2300. char_dat[id[i]].status.class_ == JOB_TAEKWON)
  2301. {
  2302. fame_item.id = char_dat[id[i]].status.char_id;
  2303. fame_item.fame = char_dat[id[i]].status.fame;
  2304. strncpy(fame_item.name, char_dat[id[i]].status.name, NAME_LENGTH);
  2305. memcpy(&taekwon_fame_list[j],&fame_item,sizeof(struct fame_list));
  2306. j++;
  2307. }
  2308. }
  2309. DELETE_BUFFER(id);
  2310. }
  2311. // Send map-servers the fame ranking lists
  2312. int char_send_fame_list(int fd)
  2313. {
  2314. int i, len = 8;
  2315. unsigned char buf[32000];
  2316. WBUFW(buf,0) = 0x2b1b;
  2317. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  2318. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  2319. len += sizeof(struct fame_list);
  2320. }
  2321. // add blacksmith's block length
  2322. WBUFW(buf, 6) = len;
  2323. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  2324. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  2325. len += sizeof(struct fame_list);
  2326. }
  2327. // add alchemist's block length
  2328. WBUFW(buf, 4) = len;
  2329. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  2330. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  2331. len += sizeof(struct fame_list);
  2332. }
  2333. // add total packet length
  2334. WBUFW(buf, 2) = len;
  2335. if(fd!=-1)
  2336. mapif_send(fd, buf, len);
  2337. else
  2338. mapif_sendall(buf, len);
  2339. return 0;
  2340. }
  2341. void char_update_fame_list(int type, int index, int fame)
  2342. {
  2343. unsigned char buf[9];
  2344. WBUFW(buf,0) = 0x2b22;
  2345. WBUFB(buf,2) = type;
  2346. WBUFB(buf,3) = index;
  2347. WBUFL(buf,4) = fame;
  2348. mapif_sendall(buf, 8);
  2349. }
  2350. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  2351. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  2352. int char_loadName(int char_id, char* name)
  2353. {
  2354. int j;
  2355. for( j = 0; j < char_num && char_dat[j].status.char_id != char_id; ++j )
  2356. ;// find char
  2357. if( j < char_num )
  2358. strncpy(name, char_dat[j].status.name, NAME_LENGTH);
  2359. else
  2360. strncpy(name, unknown_char_name, NAME_LENGTH);
  2361. return (j < char_num) ? 1 : 0;
  2362. }
  2363. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  2364. int parse_frommap(int fd)
  2365. {
  2366. int i, j;
  2367. int id;
  2368. ARR_FIND( 0, MAX_MAP_SERVERS, id, server[id].fd == fd );
  2369. if(id == MAX_MAP_SERVERS)
  2370. set_eof(fd);
  2371. if(session[fd]->flag.eof) {
  2372. if (id < MAX_MAP_SERVERS) {
  2373. unsigned char buf[16384];
  2374. ShowStatus("Map-server %d (session #%d) has disconnected.\n", id, fd);
  2375. //Notify other map servers that this one is gone. [Skotlex]
  2376. WBUFW(buf,0) = 0x2b20;
  2377. WBUFL(buf,4) = htonl(server[id].ip);
  2378. WBUFW(buf,8) = htons(server[id].port);
  2379. j = 0;
  2380. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2381. if (server[id].map[i])
  2382. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2383. if (j > 0) {
  2384. WBUFW(buf,2) = j * 4 + 10;
  2385. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2386. }
  2387. server[id].fd = -1;
  2388. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2389. }
  2390. do_close(fd);
  2391. create_online_files();
  2392. return 0;
  2393. }
  2394. while(RFIFOREST(fd) >= 2)
  2395. {
  2396. //ShowDebug("Received packet 0x%4x (%d bytes) from map-server (connection %d)\n", RFIFOW(fd, 0), RFIFOREST(fd), fd);
  2397. switch(RFIFOW(fd,0))
  2398. {
  2399. case 0x2afa: // Receiving map names list from the map-server
  2400. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2401. return 0;
  2402. memset(server[id].map, 0, sizeof(server[id].map));
  2403. j = 0;
  2404. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2405. server[id].map[j] = RFIFOW(fd,i);
  2406. j++;
  2407. }
  2408. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2409. id, j, CONVIP(server[id].ip), server[id].port);
  2410. ShowStatus("Map-server %d loading complete.\n", id);
  2411. char_log("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d. Map-server %d loading complete.\n",
  2412. id, j, CONVIP(server[id].ip), server[id].port, id);
  2413. // send name for wisp to player
  2414. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  2415. WFIFOW(fd,0) = 0x2afb;
  2416. WFIFOB(fd,2) = 0;
  2417. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  2418. WFIFOSET(fd,3+NAME_LENGTH);
  2419. char_send_fame_list(fd); //Send fame list.
  2420. {
  2421. unsigned char buf[16384];
  2422. int x;
  2423. if (j == 0) {
  2424. ShowWarning("Map-server %d has NO maps.\n", id);
  2425. char_log("WARNING: Map-server %d has NO maps.\n", id);
  2426. } else {
  2427. // Transmitting maps information to the other map-servers
  2428. WBUFW(buf,0) = 0x2b04;
  2429. WBUFW(buf,2) = j * 4 + 10;
  2430. WBUFL(buf,4) = htonl(server[id].ip);
  2431. WBUFW(buf,8) = htons(server[id].port);
  2432. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2433. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2434. }
  2435. // Transmitting the maps of the other map-servers to the new map-server
  2436. for(x = 0; x < MAX_MAP_SERVERS; x++) {
  2437. if (server[x].fd > 0 && x != id) {
  2438. WFIFOHEAD(fd,10 +4*MAX_MAP_PER_SERVER);
  2439. WFIFOW(fd,0) = 0x2b04;
  2440. WFIFOL(fd,4) = htonl(server[x].ip);
  2441. WFIFOW(fd,8) = htons(server[x].port);
  2442. j = 0;
  2443. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2444. if (server[x].map[i])
  2445. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2446. if (j > 0) {
  2447. WFIFOW(fd,2) = j * 4 + 10;
  2448. WFIFOSET(fd,WFIFOW(fd,2));
  2449. }
  2450. }
  2451. }
  2452. }
  2453. RFIFOSKIP(fd,RFIFOW(fd,2));
  2454. break;
  2455. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  2456. if (RFIFOREST(fd) < 10)
  2457. return 0;
  2458. {
  2459. #ifdef ENABLE_SC_SAVING
  2460. int aid, cid;
  2461. struct scdata *data;
  2462. aid = RFIFOL(fd,2);
  2463. cid = RFIFOL(fd,6);
  2464. data = status_search_scdata(aid, cid);
  2465. if (data->count > 0)
  2466. { //Deliver status change data.
  2467. WFIFOW(fd,0) = 0x2b1d;
  2468. WFIFOW(fd,2) = 14 + data->count*sizeof(struct status_change_data);
  2469. WFIFOL(fd,4) = aid;
  2470. WFIFOL(fd,8) = cid;
  2471. WFIFOW(fd,12) = data->count;
  2472. for (i = 0; i < data->count; i++)
  2473. memcpy(WFIFOP(fd,14+i*sizeof(struct status_change_data)), &data->data[i], sizeof(struct status_change_data));
  2474. WFIFOSET(fd, WFIFOW(fd,2));
  2475. status_delete_scdata(aid, cid); //Data sent, so it needs be discarded now.
  2476. }
  2477. #endif
  2478. RFIFOSKIP(fd, 10);
  2479. }
  2480. break;
  2481. case 0x2afe: //set MAP user count
  2482. if (RFIFOREST(fd) < 4)
  2483. return 0;
  2484. if (RFIFOW(fd,2) != server[id].users) {
  2485. server[id].users = RFIFOW(fd,2);
  2486. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2487. }
  2488. RFIFOSKIP(fd, 4);
  2489. break;
  2490. case 0x2aff: //set MAP users
  2491. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2492. return 0;
  2493. //TODO: When data mismatches memory, update guild/party online/offline states.
  2494. server[id].users = RFIFOW(fd,4);
  2495. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2496. for(i = 0; i < server[id].users; i++) {
  2497. int aid, cid;
  2498. struct online_char_data* character;
  2499. aid = RFIFOL(fd,6+i*8);
  2500. cid = RFIFOL(fd,6+i*8+4);
  2501. character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data);
  2502. if( character->server > -1 && character->server != id )
  2503. {
  2504. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2505. character->account_id, character->char_id, character->server, id, aid, cid);
  2506. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2507. }
  2508. character->char_id = cid;
  2509. character->server = id;
  2510. }
  2511. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2512. RFIFOSKIP(fd,6+i*8);
  2513. break;
  2514. case 0x2b01: // Receive character data from map-server for saving
  2515. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2516. return 0;
  2517. ARR_FIND( 0, char_num, i, char_dat[i].status.account_id == RFIFOL(fd,4) && char_dat[i].status.char_id == RFIFOL(fd,8) );
  2518. if( i < char_num )
  2519. {
  2520. memcpy(&char_dat[i].status, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2521. storage_save(char_dat[i].status.account_id, &char_dat[i].status.storage);
  2522. }
  2523. if (RFIFOB(fd,12))
  2524. { //Flag, set character offline. [Skotlex]
  2525. set_char_offline(RFIFOL(fd,8),RFIFOL(fd,4));
  2526. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2527. WFIFOL(fd,2) = RFIFOL(fd,4);
  2528. WFIFOL(fd,6) = RFIFOL(fd,8);
  2529. WFIFOSET(fd,10);
  2530. }
  2531. RFIFOSKIP(fd,RFIFOW(fd,2));
  2532. break;
  2533. case 0x2b02: // req char selection
  2534. if( RFIFOREST(fd) < 18 )
  2535. return 0;
  2536. {
  2537. struct auth_node* node;
  2538. int account_id = RFIFOL(fd,2);
  2539. uint32 login_id1 = RFIFOL(fd,6);
  2540. uint32 login_id2 = RFIFOL(fd,10);
  2541. uint32 ip = RFIFOL(fd,14);
  2542. RFIFOSKIP(fd,18);
  2543. // create temporary auth entry
  2544. CREATE(node, struct auth_node, 1);
  2545. node->account_id = account_id;
  2546. node->char_id = 0;
  2547. node->login_id1 = login_id1;
  2548. node->login_id2 = login_id2;
  2549. //node->sex = 0;
  2550. node->ip = ntohl(ip);
  2551. node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2552. idb_put(auth_db, account_id, node);
  2553. //Set char to "@ char select" in online db [Kevin]
  2554. set_char_charselect(account_id);
  2555. WFIFOHEAD(fd,7);
  2556. WFIFOW(fd,0) = 0x2b03;
  2557. WFIFOL(fd,2) = account_id;
  2558. WFIFOB(fd,6) = 0;
  2559. WFIFOSET(fd,7);
  2560. }
  2561. break;
  2562. case 0x2b05: // request "change map server"
  2563. if (RFIFOREST(fd) < 35)
  2564. return 0;
  2565. {
  2566. unsigned short name;
  2567. int map_id, map_fd = -1;
  2568. struct online_char_data* data;
  2569. struct mmo_charstatus* char_data;
  2570. name = RFIFOW(fd,18);
  2571. map_id = search_mapserver(name, ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2572. if (map_id >= 0)
  2573. map_fd = server[map_id].fd;
  2574. for(i = 0; i < char_num; i++) {
  2575. if (char_dat[i].status.account_id == RFIFOL(fd,2) &&
  2576. char_dat[i].status.char_id == RFIFOL(fd,14))
  2577. break;
  2578. }
  2579. char_data = i < char_num ? &char_dat[i].status : NULL;
  2580. if (map_fd >= 0 && session[map_fd] && char_data)
  2581. { //Send the map server the auth of this player.
  2582. struct auth_node* node;
  2583. //Update the "last map" as this is where the player must be spawned on the new map server.
  2584. char_data->last_point.map = RFIFOW(fd,18);
  2585. char_data->last_point.x = RFIFOW(fd,20);
  2586. char_data->last_point.y = RFIFOW(fd,22);
  2587. char_data->sex = RFIFOB(fd,30);
  2588. // create temporary auth entry
  2589. CREATE(node, struct auth_node, 1);
  2590. node->account_id = RFIFOL(fd,2);
  2591. node->char_id = RFIFOL(fd,14);
  2592. node->login_id1 = RFIFOL(fd,6);
  2593. node->login_id2 = RFIFOL(fd,10);
  2594. node->sex = RFIFOB(fd,30);
  2595. node->expiration_time = 0; // FIXME
  2596. node->ip = ntohl(RFIFOL(fd,31));
  2597. idb_put(auth_db, RFIFOL(fd,2), node);
  2598. data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2599. data->char_id = char_data->char_id;
  2600. data->server = map_id; //Update server where char is.
  2601. //Reply with an ack.
  2602. WFIFOHEAD(fd,30);
  2603. WFIFOW(fd,0) = 0x2b06;
  2604. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2605. WFIFOSET(fd,30);
  2606. } else { //Reply with nak
  2607. WFIFOHEAD(fd,30);
  2608. WFIFOW(fd,0) = 0x2b06;
  2609. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2610. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2611. WFIFOSET(fd,30);
  2612. }
  2613. RFIFOSKIP(fd,35);
  2614. }
  2615. break;
  2616. case 0x2b08: // char name request
  2617. if (RFIFOREST(fd) < 6)
  2618. return 0;
  2619. WFIFOHEAD(fd,30);
  2620. WFIFOW(fd,0) = 0x2b09;
  2621. WFIFOL(fd,2) = RFIFOL(fd,2);
  2622. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2623. WFIFOSET(fd,30);
  2624. RFIFOSKIP(fd,6);
  2625. break;
  2626. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2627. if (RFIFOREST(fd) < 86)
  2628. return 0;
  2629. if (login_fd > 0) { // don't send request if no login-server
  2630. WFIFOHEAD(login_fd,86);
  2631. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2632. WFIFOW(login_fd,0) = 0x2722;
  2633. WFIFOSET(login_fd,86);
  2634. }
  2635. RFIFOSKIP(fd, 86);
  2636. break;
  2637. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  2638. if (RFIFOREST(fd) < 44)
  2639. return 0;
  2640. {
  2641. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  2642. char character_name[NAME_LENGTH];
  2643. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  2644. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  2645. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2646. short year = RFIFOW(fd,32);
  2647. short month = RFIFOW(fd,34);
  2648. short day = RFIFOW(fd,36);
  2649. short hour = RFIFOW(fd,38);
  2650. short minute = RFIFOW(fd,40);
  2651. short second = RFIFOW(fd,42);
  2652. RFIFOSKIP(fd,44);
  2653. safestrncpy(character_name, name, NAME_LENGTH);
  2654. i = search_character_index(character_name);
  2655. if( i < 0 )
  2656. {
  2657. result = 1; // 1-player not found
  2658. }
  2659. else
  2660. {
  2661. char name[NAME_LENGTH];
  2662. int account_id;
  2663. account_id = char_dat[i].status.account_id;
  2664. safestrncpy(name, char_dat[i].status.name, NAME_LENGTH);
  2665. if( login_fd <= 0 )
  2666. result = 3; // 3-login-server offline
  2667. // else
  2668. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  2669. // result = 2; // 2-gm level too low
  2670. else
  2671. switch( type ) {
  2672. case 1: // block
  2673. WFIFOHEAD(login_fd,10);
  2674. WFIFOW(login_fd,0) = 0x2724;
  2675. WFIFOL(login_fd,2) = account_id;
  2676. WFIFOL(login_fd,6) = 5; // new account status
  2677. WFIFOSET(login_fd,10);
  2678. break;
  2679. case 2: // ban
  2680. WFIFOHEAD(login_fd,18);
  2681. WFIFOW(login_fd, 0) = 0x2725;
  2682. WFIFOL(login_fd, 2) = account_id;
  2683. WFIFOW(login_fd, 6) = year;
  2684. WFIFOW(login_fd, 8) = month;
  2685. WFIFOW(login_fd,10) = day;
  2686. WFIFOW(login_fd,12) = hour;
  2687. WFIFOW(login_fd,14) = minute;
  2688. WFIFOW(login_fd,16) = second;
  2689. WFIFOSET(login_fd,18);
  2690. break;
  2691. case 3: // unblock
  2692. WFIFOHEAD(login_fd,10);
  2693. WFIFOW(login_fd,0) = 0x2724;
  2694. WFIFOL(login_fd,2) = account_id;
  2695. WFIFOL(login_fd,6) = 0; // new account status
  2696. WFIFOSET(login_fd,10);
  2697. break;
  2698. case 4: // unban
  2699. WFIFOHEAD(login_fd,6);
  2700. WFIFOW(login_fd,0) = 0x272a;
  2701. WFIFOL(login_fd,2) = account_id;
  2702. WFIFOSET(login_fd,6);
  2703. break;
  2704. case 5: // changesex
  2705. WFIFOHEAD(login_fd,6);
  2706. WFIFOW(login_fd,0) = 0x2727;
  2707. WFIFOL(login_fd,2) = account_id;
  2708. WFIFOSET(login_fd,6);
  2709. break;
  2710. }
  2711. }
  2712. // send answer if a player ask, not if the server ask
  2713. if( acc != -1 && type != 5) { // Don't send answer for changesex
  2714. WFIFOHEAD(fd,34);
  2715. WFIFOW(fd, 0) = 0x2b0f;
  2716. WFIFOL(fd, 2) = acc;
  2717. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  2718. WFIFOW(fd,30) = type;
  2719. WFIFOW(fd,32) = result;
  2720. WFIFOSET(fd,34);
  2721. }
  2722. }
  2723. break;
  2724. case 0x2b10: // Update and send fame ranking list
  2725. if (RFIFOREST(fd) < 11)
  2726. return 0;
  2727. {
  2728. int cid = RFIFOL(fd, 2);
  2729. int fame = RFIFOL(fd, 6);
  2730. char type = RFIFOB(fd, 10);
  2731. int size;
  2732. struct fame_list* list;
  2733. int player_pos;
  2734. int fame_pos;
  2735. switch(type)
  2736. {
  2737. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  2738. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  2739. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  2740. default: size = 0; list = NULL; break;
  2741. }
  2742. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  2743. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  2744. if( player_pos == size && fame_pos == size )
  2745. ;// not on list and not enough fame to get on it
  2746. else if( fame_pos == player_pos )
  2747. {// same position
  2748. list[player_pos].fame = fame;
  2749. char_update_fame_list(type, player_pos, fame);
  2750. }
  2751. else
  2752. {// move in the list
  2753. if( player_pos == size )
  2754. {// new ranker - not in the list
  2755. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  2756. list[fame_pos].id = cid;
  2757. list[fame_pos].fame = fame;
  2758. char_loadName(cid, list[fame_pos].name);
  2759. }
  2760. else
  2761. {// already in the list
  2762. if( fame_pos == size )
  2763. --fame_pos;// move to the end of the list
  2764. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  2765. list[fame_pos].fame = fame;
  2766. }
  2767. char_send_fame_list(-1);
  2768. }
  2769. RFIFOSKIP(fd,11);
  2770. }
  2771. break;
  2772. case 0x2b16: // Receive rates [Wizputer]
  2773. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8))
  2774. return 0;
  2775. // Txt doesn't need this packet, so just skip it
  2776. RFIFOSKIP(fd,RFIFOW(fd,8));
  2777. break;
  2778. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  2779. if (RFIFOREST(fd) < 6)
  2780. return 0;
  2781. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2782. RFIFOSKIP(fd,10);
  2783. break;
  2784. case 0x2b18: // Reset all chars to offline [Wizputer]
  2785. set_all_offline(id);
  2786. RFIFOSKIP(fd,2);
  2787. break;
  2788. case 0x2b19: // Character set online [Wizputer]
  2789. if (RFIFOREST(fd) < 10)
  2790. return 0;
  2791. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2792. RFIFOSKIP(fd,10);
  2793. break;
  2794. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  2795. if (RFIFOREST(fd) < 2)
  2796. return 0;
  2797. char_read_fame_list();
  2798. char_send_fame_list(-1);
  2799. RFIFOSKIP(fd,2);
  2800. break;
  2801. case 0x2b1c: //Request to save status change data. [Skotlex]
  2802. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2803. return 0;
  2804. {
  2805. #ifdef ENABLE_SC_SAVING
  2806. int count, aid, cid;
  2807. struct scdata *data;
  2808. aid = RFIFOL(fd, 4);
  2809. cid = RFIFOL(fd, 8);
  2810. count = RFIFOW(fd, 12);
  2811. data = status_search_scdata(aid, cid);
  2812. if (data->count != count)
  2813. {
  2814. data->count = count;
  2815. data->data = (struct status_change_data*)aRealloc(data->data, count*sizeof(struct status_change_data));
  2816. }
  2817. for (i = 0; i < count; i++)
  2818. memcpy (&data->data[i], RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2819. #endif
  2820. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2821. }
  2822. break;
  2823. case 0x2b23: // map-server alive packet
  2824. WFIFOHEAD(fd,2);
  2825. WFIFOW(fd,0) = 0x2b24;
  2826. WFIFOSET(fd,2);
  2827. RFIFOSKIP(fd,2);
  2828. break;
  2829. case 0x2b26: // auth request from map-server
  2830. if (RFIFOREST(fd) < 19)
  2831. return 0;
  2832. {
  2833. int account_id;
  2834. int char_id;
  2835. int login_id1;
  2836. char sex;
  2837. uint32 ip;
  2838. struct auth_node* node;
  2839. struct mmo_charstatus* cd;
  2840. account_id = RFIFOL(fd,2);
  2841. char_id = RFIFOL(fd,6);
  2842. login_id1 = RFIFOL(fd,10);
  2843. sex = RFIFOB(fd,14);
  2844. ip = ntohl(RFIFOL(fd,15));
  2845. RFIFOSKIP(fd,19);
  2846. node = (struct auth_node*)idb_get(auth_db, account_id);
  2847. cd = search_character(account_id, char_id);
  2848. if( node != NULL && cd != NULL &&
  2849. node->account_id == account_id &&
  2850. node->char_id == char_id &&
  2851. node->login_id1 == login_id1 &&
  2852. node->sex == sex /*&&
  2853. node->ip == ip*/ )
  2854. {// auth ok
  2855. cd->sex = sex;
  2856. WFIFOHEAD(fd,24 + sizeof(struct mmo_charstatus));
  2857. WFIFOW(fd,0) = 0x2afd;
  2858. WFIFOW(fd,2) = 24 + sizeof(struct mmo_charstatus);
  2859. WFIFOL(fd,4) = account_id;
  2860. WFIFOL(fd,8) = node->login_id1;
  2861. WFIFOL(fd,12) = node->login_id2;
  2862. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  2863. WFIFOL(fd,20) = node->gmlevel;
  2864. storage_load(cd->account_id, &cd->storage); //FIXME: storage is used as a temp buffer here
  2865. memcpy(WFIFOP(fd,24), cd, sizeof(struct mmo_charstatus));
  2866. WFIFOSET(fd, WFIFOW(fd,2));
  2867. // only use the auth once and mark user online
  2868. idb_remove(auth_db, account_id);
  2869. set_char_online(id, char_id, account_id);
  2870. }
  2871. else
  2872. {// auth failed
  2873. WFIFOHEAD(fd,19);
  2874. WFIFOW(fd,0) = 0x2b27;
  2875. WFIFOL(fd,2) = account_id;
  2876. WFIFOL(fd,6) = char_id;
  2877. WFIFOL(fd,10) = login_id1;
  2878. WFIFOB(fd,14) = sex;
  2879. WFIFOL(fd,15) = htonl(ip);
  2880. WFIFOSET(fd,19);
  2881. }
  2882. }
  2883. break;
  2884. case 0x2736: // ip address update
  2885. if (RFIFOREST(fd) < 6) return 0;
  2886. server[id].ip = ntohl(RFIFOL(fd, 2));
  2887. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  2888. RFIFOSKIP(fd,6);
  2889. break;
  2890. default:
  2891. {
  2892. // inter server - packet
  2893. int r = inter_parse_frommap(fd);
  2894. if (r == 1) break; // processed
  2895. if (r == 2) return 0; // need more packet
  2896. // no inter server packet. no char server packet -> disconnect
  2897. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2898. set_eof(fd);
  2899. return 0;
  2900. }
  2901. } // switch
  2902. } // while
  2903. return 0;
  2904. }
  2905. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  2906. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  2907. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  2908. {
  2909. int i, j;
  2910. for(i = 0; i < MAX_MAP_SERVERS; i++)
  2911. {
  2912. if (server[i].fd > 0
  2913. && (ip == (uint32)-1 || server[i].ip == ip)
  2914. && (port == (uint16)-1 || server[i].port == port))
  2915. {
  2916. for (j = 0; server[i].map[j]; j++)
  2917. if (server[i].map[j] == map)
  2918. return i;
  2919. }
  2920. }
  2921. return -1;
  2922. }
  2923. // char_mapifの初期化処理(現在はinter_mapif初期化のみ)
  2924. static int char_mapif_init(int fd)
  2925. {
  2926. return inter_mapif_init(fd);
  2927. }
  2928. //--------------------------------------------
  2929. // Test to know if an IP come from LAN or WAN.
  2930. //--------------------------------------------
  2931. int lan_subnetcheck(uint32 ip)
  2932. {
  2933. int i;
  2934. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  2935. if ( i < subnet_count ) {
  2936. 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));
  2937. return subnet[i].char_ip;
  2938. } else {
  2939. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  2940. return 0;
  2941. }
  2942. }
  2943. int parse_char(int fd)
  2944. {
  2945. int i, ch;
  2946. char email[40];
  2947. unsigned short cmd;
  2948. int map_fd;
  2949. struct char_session_data* sd;
  2950. uint32 ipl = session[fd]->client_addr;
  2951. sd = (struct char_session_data*)session[fd]->session_data;
  2952. // disconnect any player if no login-server.
  2953. if(login_fd < 0)
  2954. set_eof(fd);
  2955. if(session[fd]->flag.eof)
  2956. {
  2957. if( sd != NULL && sd->auth )
  2958. {
  2959. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  2960. if( data != NULL && data->fd == fd)
  2961. data->fd = -1;
  2962. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  2963. set_char_offline(-1,sd->account_id);
  2964. }
  2965. do_close(fd);
  2966. return 0;
  2967. }
  2968. while( RFIFOREST(fd) >= 2 )
  2969. {
  2970. //For use in packets that depend on an sd being present [Skotlex]
  2971. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  2972. cmd = RFIFOW(fd,0);
  2973. switch( cmd )
  2974. {
  2975. // request to connect
  2976. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  2977. case 0x65:
  2978. if( RFIFOREST(fd) < 17 )
  2979. return 0;
  2980. {
  2981. struct auth_node* node;
  2982. int account_id = RFIFOL(fd,2);
  2983. uint32 login_id1 = RFIFOL(fd,6);
  2984. uint32 login_id2 = RFIFOL(fd,10);
  2985. int sex = RFIFOB(fd,16);
  2986. RFIFOSKIP(fd,17);
  2987. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  2988. if (sd) {
  2989. //Received again auth packet for already authentified account?? Discard it.
  2990. //TODO: Perhaps log this as a hack attempt?
  2991. //TODO: and perhaps send back a reply?
  2992. break;
  2993. }
  2994. CREATE(session[fd]->session_data, struct char_session_data, 1);
  2995. sd = (struct char_session_data*)session[fd]->session_data;
  2996. sd->account_id = account_id;
  2997. sd->login_id1 = login_id1;
  2998. sd->login_id2 = login_id2;
  2999. sd->sex = sex;
  3000. sd->auth = false; // not authed yet
  3001. // send back account_id
  3002. WFIFOHEAD(fd,4);
  3003. WFIFOL(fd,0) = account_id;
  3004. WFIFOSET(fd,4);
  3005. // search authentification
  3006. node = (struct auth_node*)idb_get(auth_db, account_id);
  3007. if( node != NULL &&
  3008. node->account_id == account_id &&
  3009. node->login_id1 == login_id1 &&
  3010. node->login_id2 == login_id2 /*&&
  3011. node->ip == ipl*/ )
  3012. {// authentication found (coming from map server)
  3013. idb_remove(auth_db, account_id);
  3014. char_auth_ok(fd, sd);
  3015. }
  3016. else
  3017. {// authentication not found (coming from login server)
  3018. if (login_fd > 0) { // don't send request if no login-server
  3019. WFIFOHEAD(login_fd,23);
  3020. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  3021. WFIFOL(login_fd,2) = sd->account_id;
  3022. WFIFOL(login_fd,6) = sd->login_id1;
  3023. WFIFOL(login_fd,10) = sd->login_id2;
  3024. WFIFOB(login_fd,14) = sd->sex;
  3025. WFIFOL(login_fd,15) = htonl(ipl);
  3026. WFIFOL(login_fd,19) = fd;
  3027. WFIFOSET(login_fd,23);
  3028. } else { // if no login-server, we must refuse connection
  3029. WFIFOHEAD(fd,3);
  3030. WFIFOW(fd,0) = 0x6c;
  3031. WFIFOB(fd,2) = 0;
  3032. WFIFOSET(fd,3);
  3033. }
  3034. }
  3035. }
  3036. break;
  3037. // char select
  3038. case 0x66:
  3039. FIFOSD_CHECK(3);
  3040. {
  3041. struct mmo_charstatus *cd;
  3042. uint32 subnet_map_ip;
  3043. struct auth_node* node;
  3044. int slot = RFIFOB(fd,2);
  3045. RFIFOSKIP(fd,3);
  3046. // if we activated email creation and email is default email
  3047. if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online
  3048. WFIFOHEAD(fd,3);
  3049. WFIFOW(fd,0) = 0x70;
  3050. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3051. WFIFOSET(fd,3);
  3052. break;
  3053. }
  3054. // otherwise, load the character
  3055. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] >= 0 && char_dat[sd->found_char[ch]].status.slot == slot );
  3056. if (ch == MAX_CHARS)
  3057. { //Not found?? May be forged packet.
  3058. break;
  3059. }
  3060. cd = &char_dat[sd->found_char[ch]].status;
  3061. char_log("Character Selected, Account ID: %d, Character Slot: %d, Character Name: %s.\n", sd->account_id, slot, cd->name);
  3062. cd->sex = sd->sex;
  3063. // searching map server
  3064. i = search_mapserver(cd->last_point.map,-1,-1);
  3065. // if map is not found, we check major cities
  3066. if (i < 0) {
  3067. unsigned short j;
  3068. //First check that there's actually a map server online.
  3069. ARR_FIND( 0, MAX_MAP_SERVERS, j, server[j].fd >= 0 && server[j].map[0] );
  3070. if (j == MAX_MAP_SERVERS) {
  3071. ShowInfo("Connection Closed. No map servers available.\n");
  3072. WFIFOHEAD(fd,3);
  3073. WFIFOW(fd,0) = 0x81;
  3074. WFIFOB(fd,2) = 1; // 01 = Server closed
  3075. WFIFOSET(fd,3);
  3076. break;
  3077. }
  3078. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  3079. cd->last_point.x = 273;
  3080. cd->last_point.y = 354;
  3081. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  3082. cd->last_point.x = 120;
  3083. cd->last_point.y = 100;
  3084. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  3085. cd->last_point.x = 160;
  3086. cd->last_point.y = 94;
  3087. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  3088. cd->last_point.x = 116;
  3089. cd->last_point.y = 57;
  3090. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  3091. cd->last_point.x = 87;
  3092. cd->last_point.y = 117;
  3093. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  3094. cd->last_point.x = 94;
  3095. cd->last_point.y = 103;
  3096. } else {
  3097. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(cd->last_point.map));
  3098. WFIFOHEAD(fd,3);
  3099. WFIFOW(fd,0) = 0x81;
  3100. WFIFOB(fd,2) = 1; // 01 = Server closed
  3101. WFIFOSET(fd,3);
  3102. break;
  3103. }
  3104. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(cd->last_point.map), mapindex_id2name(j));
  3105. cd->last_point.map = j;
  3106. }
  3107. //FIXME: is this case even possible? [ultramage]
  3108. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  3109. {
  3110. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  3111. server[i].fd = -1;
  3112. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3113. //Send server closed.
  3114. WFIFOHEAD(fd,3);
  3115. WFIFOW(fd,0) = 0x81;
  3116. WFIFOB(fd,2) = 1; // 01 = Server closed
  3117. WFIFOSET(fd,3);
  3118. break;
  3119. }
  3120. //Send player to map
  3121. WFIFOHEAD(fd,28);
  3122. WFIFOW(fd,0) = 0x71;
  3123. WFIFOL(fd,2) = cd->char_id;
  3124. mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
  3125. subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
  3126. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  3127. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  3128. WFIFOSET(fd,28);
  3129. ShowInfo("Character selection '%s' (account: %d, slot: %d).\n", cd->name, sd->account_id, ch);
  3130. // create temporary auth entry
  3131. CREATE(node, struct auth_node, 1);
  3132. node->account_id = sd->account_id;
  3133. node->char_id = cd->char_id;
  3134. node->login_id1 = sd->login_id1;
  3135. node->login_id2 = sd->login_id2;
  3136. node->sex = sd->sex;
  3137. node->expiration_time = sd->expiration_time;
  3138. node->gmlevel = sd->gmlevel;
  3139. node->ip = ipl;
  3140. idb_put(auth_db, sd->account_id, node);
  3141. }
  3142. break;
  3143. // create new char
  3144. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  3145. case 0x67:
  3146. FIFOSD_CHECK(37);
  3147. if( !char_new ) //turn character creation on/off [Kevin]
  3148. i = -2;
  3149. else
  3150. i = make_new_char(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  3151. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  3152. if (i < 0)
  3153. {
  3154. WFIFOHEAD(fd,3);
  3155. WFIFOW(fd,0) = 0x6e;
  3156. switch (i) {
  3157. case -1: WFIFOB(fd,2) = 0x00; break;
  3158. case -2: WFIFOB(fd,2) = 0xFF; break;
  3159. case -3: WFIFOB(fd,2) = 0x01; break;
  3160. }
  3161. WFIFOSET(fd,3);
  3162. }
  3163. else
  3164. {
  3165. int len;
  3166. // send to player
  3167. WFIFOHEAD(fd,MAX_CHAR_BUF+2);
  3168. WFIFOW(fd,0) = 0x6d;
  3169. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat[i].status);
  3170. WFIFOSET(fd,len);
  3171. // add new entry to the chars list
  3172. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  3173. if( ch < MAX_CHARS )
  3174. sd->found_char[ch] = i; // position of the new char in the char_dat[] array
  3175. }
  3176. RFIFOSKIP(fd,37);
  3177. break;
  3178. // delete char
  3179. case 0x68:
  3180. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  3181. case 0x1fb:
  3182. if (cmd == 0x68) FIFOSD_CHECK(46);
  3183. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  3184. {
  3185. int cid = RFIFOL(fd,2);
  3186. struct mmo_charstatus* cs = NULL;
  3187. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  3188. memcpy(email, RFIFOP(fd,6), 40);
  3189. RFIFOSKIP(fd,RFIFOREST(fd)); // hack to make the other deletion packet work
  3190. if (e_mail_check(email) == 0)
  3191. strncpy(email, "a@a.com", 40); // default e-mail
  3192. // BEGIN HACK: "change email using the char deletion 'confirm email' menu"
  3193. // if we activated email creation and email is default email
  3194. if (email_creation != 0 && strcmp(sd->email, "a@a.com") == 0 && login_fd > 0) { // to modify an e-mail, login-server must be online
  3195. // if sended email is incorrect e-mail
  3196. if (strcmp(email, "a@a.com") == 0) {
  3197. WFIFOHEAD(fd,3);
  3198. WFIFOW(fd,0) = 0x70;
  3199. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3200. WFIFOSET(fd,3);
  3201. break;
  3202. }
  3203. // we change the packet to set it like selection.
  3204. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid );
  3205. if( i < MAX_CHARS )
  3206. {
  3207. // we save new e-mail
  3208. memcpy(sd->email, email, 40);
  3209. // we send new e-mail to login-server ('online' login-server is checked before)
  3210. WFIFOHEAD(login_fd,46);
  3211. WFIFOW(login_fd,0) = 0x2715;
  3212. WFIFOL(login_fd,2) = sd->account_id;
  3213. memcpy(WFIFOP(login_fd, 6), email, 40);
  3214. WFIFOSET(login_fd,46);
  3215. // change value to put new packet (char selection)
  3216. RFIFOSKIP(fd,-3); //FIXME: Will this work? Messing with the received buffer is ugly anyway...
  3217. RFIFOW(fd,0) = 0x66;
  3218. RFIFOB(fd,2) = char_dat[sd->found_char[i]].status.slot;
  3219. // not send packet, it's modify of actual packet
  3220. } else {
  3221. WFIFOHEAD(fd,3);
  3222. WFIFOW(fd,0) = 0x70;
  3223. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3224. WFIFOSET(fd,3);
  3225. }
  3226. break;
  3227. }
  3228. // END HACK
  3229. // otherwise, we delete the character
  3230. if (strcmpi(email, sd->email) != 0) { // if it's an invalid email
  3231. WFIFOHEAD(fd,3);
  3232. WFIFOW(fd,0) = 0x70;
  3233. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  3234. WFIFOSET(fd,3);
  3235. break;
  3236. }
  3237. // check if this char exists
  3238. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] != -1 && char_dat[sd->found_char[i]].status.char_id == cid );
  3239. if( i == MAX_CHARS )
  3240. { // Such a character does not exist in the account
  3241. WFIFOHEAD(fd,3);
  3242. WFIFOW(fd,0) = 0x70;
  3243. WFIFOB(fd,2) = 0;
  3244. WFIFOSET(fd,3);
  3245. break;
  3246. }
  3247. // deletion process
  3248. cs = &char_dat[sd->found_char[i]].status;
  3249. char_delete(cs);
  3250. if (sd->found_char[i] != char_num - 1) {
  3251. int j, k;
  3252. struct char_session_data *sd2;
  3253. memcpy(&char_dat[sd->found_char[i]], &char_dat[char_num-1], sizeof(struct mmo_charstatus));
  3254. // Correct moved character reference in the character's owner
  3255. for (j = 0; j < fd_max; j++) {
  3256. if (session[j] && (sd2 = (struct char_session_data*)session[j]->session_data) &&
  3257. sd2->account_id == char_dat[char_num-1].status.account_id) {
  3258. for (k = 0; k < MAX_CHARS; k++) {
  3259. if (sd2->found_char[k] == char_num-1) {
  3260. sd2->found_char[k] = sd->found_char[i];
  3261. break;
  3262. }
  3263. }
  3264. break;
  3265. }
  3266. }
  3267. }
  3268. char_num--;
  3269. // remove char from list and compact it
  3270. for(ch = i; ch < MAX_CHARS-1; ch++)
  3271. sd->found_char[ch] = sd->found_char[ch+1];
  3272. sd->found_char[MAX_CHARS-1] = -1;
  3273. /* Char successfully deleted.*/
  3274. WFIFOHEAD(fd,2);
  3275. WFIFOW(fd,0) = 0x6f;
  3276. WFIFOSET(fd,2);
  3277. }
  3278. break;
  3279. // client keep-alive packet (every 12 seconds)
  3280. // R 0187 <account ID>.l
  3281. case 0x187:
  3282. if (RFIFOREST(fd) < 6)
  3283. return 0;
  3284. RFIFOSKIP(fd,6);
  3285. break;
  3286. // char rename request
  3287. // R 028d <account ID>.l <char ID>.l <new name>.24B
  3288. case 0x28d:
  3289. FIFOSD_CHECK(34);
  3290. //not implemented
  3291. RFIFOSKIP(fd,34);
  3292. break;
  3293. // captcha code request (not implemented)
  3294. // R 07e5 <?>.w <aid>.l
  3295. case 0x7e5:
  3296. WFIFOHEAD(fd,5);
  3297. WFIFOW(fd,0) = 0x7e9;
  3298. WFIFOW(fd,2) = 5;
  3299. WFIFOB(fd,4) = 1;
  3300. WFIFOSET(fd,5);
  3301. RFIFOSKIP(fd,8);
  3302. break;
  3303. // captcha code check (not implemented)
  3304. // R 07e7 <len>.w <aid>.l <code>.b10 <?>.b14
  3305. case 0x7e7:
  3306. WFIFOHEAD(fd,5);
  3307. WFIFOW(fd,0) = 0x7e9;
  3308. WFIFOW(fd,2) = 5;
  3309. WFIFOB(fd,4) = 1;
  3310. WFIFOSET(fd,5);
  3311. RFIFOSKIP(fd,32);
  3312. break;
  3313. // login as map-server
  3314. case 0x2af8:
  3315. if (RFIFOREST(fd) < 60)
  3316. return 0;
  3317. {
  3318. char* l_user = (char*)RFIFOP(fd,2);
  3319. char* l_pass = (char*)RFIFOP(fd,26);
  3320. l_user[23] = '\0';
  3321. l_pass[23] = '\0';
  3322. ARR_FIND( 0, MAX_MAP_SERVERS, i, server[i].fd <= 0 );
  3323. if (i == MAX_MAP_SERVERS || strcmp(l_user, userid) || strcmp(l_pass, passwd)) {
  3324. WFIFOHEAD(fd,3);
  3325. WFIFOW(fd,0) = 0x2af9;
  3326. WFIFOB(fd,2) = 3;
  3327. WFIFOSET(fd,3);
  3328. } else {
  3329. WFIFOHEAD(fd,3);
  3330. WFIFOW(fd,0) = 0x2af9;
  3331. WFIFOB(fd,2) = 0;
  3332. WFIFOSET(fd,3);
  3333. server[i].fd = fd;
  3334. server[i].ip = ntohl(RFIFOL(fd,54));
  3335. server[i].port = ntohs(RFIFOW(fd,58));
  3336. server[i].users = 0;
  3337. memset(server[i].map, 0, sizeof(server[i].map));
  3338. session[fd]->func_parse = parse_frommap;
  3339. session[fd]->flag.server = 1;
  3340. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3341. char_mapif_init(fd);
  3342. }
  3343. RFIFOSKIP(fd,60);
  3344. }
  3345. return 0; // avoid processing of followup packets here
  3346. // Athena info get
  3347. case 0x7530:
  3348. WFIFOHEAD(fd,10);
  3349. WFIFOW(fd,0) = 0x7531;
  3350. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  3351. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  3352. WFIFOB(fd,4) = ATHENA_REVISION;
  3353. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  3354. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  3355. WFIFOB(fd,7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  3356. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  3357. WFIFOSET(fd,10);
  3358. RFIFOSKIP(fd,2);
  3359. break;
  3360. // unknown packet received
  3361. default:
  3362. 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));
  3363. set_eof(fd);
  3364. return 0;
  3365. }
  3366. }
  3367. RFIFOFLUSH(fd);
  3368. return 0;
  3369. }
  3370. // Console Command Parser [Wizputer]
  3371. int parse_console(char* buf)
  3372. {
  3373. char command[256];
  3374. memset(command, 0, sizeof(command));
  3375. sscanf(buf, "%[^\n]", command);
  3376. //login_log("Console command :%s\n", command);
  3377. if( strcmpi("shutdown", command) == 0 ||
  3378. strcmpi("exit", command) == 0 ||
  3379. strcmpi("quit", command) == 0 ||
  3380. strcmpi("end", command) == 0 )
  3381. runflag = 0;
  3382. else if( strcmpi("alive", command) == 0 ||
  3383. strcmpi("status", command) == 0 )
  3384. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  3385. else if( strcmpi("help", command) == 0 ){
  3386. ShowInfo(CL_BOLD"Help of commands:"CL_RESET"\n");
  3387. ShowInfo(" To shutdown the server:\n");
  3388. ShowInfo(" 'shutdown|exit|qui|end'\n");
  3389. ShowInfo(" To know if server is alive:\n");
  3390. ShowInfo(" 'alive|status'\n");
  3391. }
  3392. return 0;
  3393. }
  3394. // 全てのMAPサーバーにデータ送信(送信したmap鯖の数を返す)
  3395. int mapif_sendall(unsigned char *buf, unsigned int len)
  3396. {
  3397. int i, c;
  3398. c = 0;
  3399. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3400. int fd;
  3401. if ((fd = server[i].fd) > 0) {
  3402. WFIFOHEAD(fd,len);
  3403. memcpy(WFIFOP(fd,0), buf, len);
  3404. WFIFOSET(fd,len);
  3405. c++;
  3406. }
  3407. }
  3408. return c;
  3409. }
  3410. // 自分以外の全てのMAPサーバーにデータ送信(送信したmap鯖の数を返す)
  3411. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  3412. {
  3413. int i, c;
  3414. c = 0;
  3415. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3416. int fd;
  3417. if ((fd = server[i].fd) > 0 && fd != sfd) {
  3418. WFIFOHEAD(fd,len);
  3419. memcpy(WFIFOP(fd,0), buf, len);
  3420. WFIFOSET(fd,len);
  3421. c++;
  3422. }
  3423. }
  3424. return c;
  3425. }
  3426. // MAPサーバーにデータ送信(map鯖生存確認有り)
  3427. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  3428. {
  3429. int i;
  3430. if (fd >= 0) {
  3431. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3432. if (fd == server[i].fd) {
  3433. WFIFOHEAD(fd,len);
  3434. memcpy(WFIFOP(fd,0), buf, len);
  3435. WFIFOSET(fd,len);
  3436. return 1;
  3437. }
  3438. }
  3439. }
  3440. return 0;
  3441. }
  3442. int broadcast_user_count(int tid, unsigned int tick, int id, intptr data)
  3443. {
  3444. uint8 buf[6];
  3445. int users = count_users();
  3446. // only send an update when needed
  3447. static int prev_users = 0;
  3448. if( prev_users == users )
  3449. return 0;
  3450. prev_users = users;
  3451. if( login_fd > 0 && session[login_fd] )
  3452. {
  3453. // send number of user to login server
  3454. WFIFOHEAD(login_fd,6);
  3455. WFIFOW(login_fd,0) = 0x2714;
  3456. WFIFOL(login_fd,2) = users;
  3457. WFIFOSET(login_fd,6);
  3458. }
  3459. // send number of players to all map-servers
  3460. WBUFW(buf,0) = 0x2b00;
  3461. WBUFL(buf,2) = users;
  3462. mapif_sendall(buf,6);
  3463. // refresh online files (txt and html)
  3464. create_online_files();
  3465. return 0;
  3466. }
  3467. /// load this char's account id into the 'online accounts' packet
  3468. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap)
  3469. {
  3470. struct online_char_data* character = (struct online_char_data*)data;
  3471. int* i = va_arg(ap, int*);
  3472. if(character->server > -1)
  3473. {
  3474. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  3475. (*i)++;
  3476. return 1;
  3477. }
  3478. return 0;
  3479. }
  3480. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data)
  3481. {
  3482. if (login_fd > 0 && session[login_fd])
  3483. {
  3484. // send account list to login server
  3485. int users = online_char_db->size(online_char_db);
  3486. int i = 0;
  3487. WFIFOHEAD(login_fd,8+users*4);
  3488. WFIFOW(login_fd,0) = 0x272d;
  3489. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  3490. WFIFOW(login_fd,2) = 8+ i*4;
  3491. WFIFOL(login_fd,4) = i;
  3492. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  3493. }
  3494. return 0;
  3495. }
  3496. int check_connect_login_server(int tid, unsigned int tick, int id, intptr data)
  3497. {
  3498. if (login_fd > 0 && session[login_fd] != NULL)
  3499. return 0;
  3500. ShowInfo("Attempt to connect to login-server...\n");
  3501. login_fd = make_connection(login_ip, login_port);
  3502. if (login_fd == -1)
  3503. { //Try again later. [Skotlex]
  3504. login_fd = 0;
  3505. return 0;
  3506. }
  3507. session[login_fd]->func_parse = parse_fromlogin;
  3508. session[login_fd]->flag.server = 1;
  3509. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3510. WFIFOHEAD(login_fd,86);
  3511. WFIFOW(login_fd,0) = 0x2710;
  3512. memcpy(WFIFOP(login_fd,2), userid, 24);
  3513. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3514. WFIFOL(login_fd,50) = 0;
  3515. WFIFOL(login_fd,54) = htonl(char_ip);
  3516. WFIFOL(login_fd,58) = htons(char_port);
  3517. memcpy(WFIFOP(login_fd,60), server_name, 20);
  3518. WFIFOW(login_fd,80) = 0;
  3519. WFIFOW(login_fd,82) = char_maintenance;
  3520. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3521. WFIFOSET(login_fd,86);
  3522. return 1;
  3523. }
  3524. // sends a ping packet to login server (will receive pong 0x2718)
  3525. int ping_login_server(int tid, unsigned int tick, int id, intptr data)
  3526. {
  3527. if (login_fd > 0 && session[login_fd] != NULL)
  3528. {
  3529. WFIFOHEAD(login_fd,2);
  3530. WFIFOW(login_fd,0) = 0x2719;
  3531. WFIFOSET(login_fd,2);
  3532. }
  3533. return 0;
  3534. }
  3535. //------------------------------------------------
  3536. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3537. //replies/disconnect the player we tried to kick. [Skotlex]
  3538. //------------------------------------------------
  3539. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data)
  3540. {
  3541. struct online_char_data* character;
  3542. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  3543. { //Mark it offline due to timeout.
  3544. character->waiting_disconnect = -1;
  3545. set_char_offline(character->char_id, character->account_id);
  3546. }
  3547. return 0;
  3548. }
  3549. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3550. {
  3551. struct online_char_data *character= (struct online_char_data*)data;
  3552. if (character->fd != -1)
  3553. return 0; //Character still connected
  3554. if (character->server == -2) //Unknown server.. set them offline
  3555. set_char_offline(character->char_id, character->account_id);
  3556. if (character->server < 0)
  3557. //Free data from players that have not been online for a while.
  3558. db_remove(online_char_db, key);
  3559. return 0;
  3560. }
  3561. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr data)
  3562. {
  3563. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3564. return 0;
  3565. }
  3566. //----------------------------------
  3567. // Reading Lan Support configuration
  3568. // Rewrote: Anvanced subnet check [LuzZza]
  3569. //----------------------------------
  3570. int char_lan_config_read(const char *lancfgName)
  3571. {
  3572. FILE *fp;
  3573. int line_num = 0;
  3574. char line[1024], w1[64], w2[64], w3[64], w4[64];
  3575. if((fp = fopen(lancfgName, "r")) == NULL) {
  3576. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3577. return 1;
  3578. }
  3579. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  3580. while(fgets(line, sizeof(line), fp))
  3581. {
  3582. line_num++;
  3583. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3584. continue;
  3585. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  3586. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3587. continue;
  3588. }
  3589. remove_control_chars(w1);
  3590. remove_control_chars(w2);
  3591. remove_control_chars(w3);
  3592. remove_control_chars(w4);
  3593. if( strcmpi(w1, "subnet") == 0 )
  3594. {
  3595. subnet[subnet_count].mask = str2ip(w2);
  3596. subnet[subnet_count].char_ip = str2ip(w3);
  3597. subnet[subnet_count].map_ip = str2ip(w4);
  3598. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  3599. {
  3600. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  3601. continue;
  3602. }
  3603. subnet_count++;
  3604. }
  3605. }
  3606. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  3607. fclose(fp);
  3608. return 0;
  3609. }
  3610. #endif //TXT_SQL_CONVERT
  3611. int char_config_read(const char *cfgName)
  3612. {
  3613. char line[1024], w1[1024], w2[1024];
  3614. FILE* fp = fopen(cfgName, "r");
  3615. if (fp == NULL) {
  3616. ShowError("Configuration file not found: %s.\n", cfgName);
  3617. return 1;
  3618. }
  3619. ShowInfo("Reading configuration file %s...\n", cfgName);
  3620. while(fgets(line, sizeof(line), fp))
  3621. {
  3622. if (line[0] == '/' && line[1] == '/')
  3623. continue;
  3624. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3625. continue;
  3626. remove_control_chars(w1);
  3627. remove_control_chars(w2);
  3628. if(strcmpi(w1,"timestamp_format") == 0) {
  3629. strncpy(timestamp_format, w2, 20);
  3630. } else if(strcmpi(w1,"console_silent")==0){
  3631. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3632. msg_silent = atoi(w2);
  3633. #ifndef TXT_SQL_CONVERT
  3634. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  3635. stdout_with_ansisequence = config_switch(w2);
  3636. } else if (strcmpi(w1, "userid") == 0) {
  3637. strncpy(userid, w2, 24);
  3638. } else if (strcmpi(w1, "passwd") == 0) {
  3639. strncpy(passwd, w2, 24);
  3640. } else if (strcmpi(w1, "server_name") == 0) {
  3641. strncpy(server_name, w2, 20);
  3642. server_name[sizeof(server_name) - 1] = '\0';
  3643. ShowStatus("%s server has been initialized\n", w2);
  3644. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3645. if (strlen(w2) >= 4) {
  3646. memcpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3647. wisp_server_name[sizeof(wisp_server_name) - 1] = '\0';
  3648. }
  3649. } else if (strcmpi(w1, "login_ip") == 0) {
  3650. char ip_str[16];
  3651. login_ip = host2ip(w2);
  3652. if (login_ip) {
  3653. strncpy(login_ip_str, w2, sizeof(login_ip_str));
  3654. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  3655. }
  3656. } else if (strcmpi(w1, "login_port") == 0) {
  3657. login_port = atoi(w2);
  3658. } else if (strcmpi(w1, "char_ip") == 0) {
  3659. char ip_str[16];
  3660. char_ip = host2ip(w2);
  3661. if (char_ip){
  3662. strncpy(char_ip_str, w2, sizeof(char_ip_str));
  3663. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  3664. }
  3665. } else if (strcmpi(w1, "bind_ip") == 0) {
  3666. char ip_str[16];
  3667. bind_ip = host2ip(w2);
  3668. if (bind_ip) {
  3669. strncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  3670. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  3671. }
  3672. } else if (strcmpi(w1, "char_port") == 0) {
  3673. char_port = atoi(w2);
  3674. } else if (strcmpi(w1, "char_maintenance") == 0) {
  3675. char_maintenance = atoi(w2);
  3676. } else if (strcmpi(w1, "char_new") == 0) {
  3677. char_new = (bool)atoi(w2);
  3678. } else if (strcmpi(w1, "char_new_display") == 0) {
  3679. char_new_display = atoi(w2);
  3680. } else if (strcmpi(w1, "email_creation") == 0) {
  3681. email_creation = config_switch(w2);
  3682. } else if (strcmpi(w1, "scdata_txt") == 0) { //By Skotlex
  3683. strcpy(scdata_txt, w2);
  3684. #endif
  3685. } else if (strcmpi(w1, "char_txt") == 0) {
  3686. strcpy(char_txt, w2);
  3687. } else if (strcmpi(w1, "friends_txt") == 0) { //By davidsiaw
  3688. strcpy(friends_txt, w2);
  3689. } else if (strcmpi(w1, "hotkeys_txt") == 0) { //By davidsiaw
  3690. strcpy(hotkeys_txt, w2);
  3691. #ifndef TXT_SQL_CONVERT
  3692. } else if (strcmpi(w1, "max_connect_user") == 0) {
  3693. max_connect_user = atoi(w2);
  3694. if (max_connect_user < 0)
  3695. max_connect_user = 0; // unlimited online players
  3696. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  3697. gm_allow_level = atoi(w2);
  3698. if(gm_allow_level < 0)
  3699. gm_allow_level = 99;
  3700. } else if (strcmpi(w1, "autosave_time") == 0) {
  3701. autosave_interval = atoi(w2)*1000;
  3702. if (autosave_interval <= 0)
  3703. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  3704. } else if (strcmpi(w1, "save_log") == 0) {
  3705. save_log = config_switch(w2);
  3706. } else if (strcmpi(w1, "start_point") == 0) {
  3707. char map[MAP_NAME_LENGTH_EXT];
  3708. int x, y;
  3709. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  3710. continue;
  3711. start_point.map = mapindex_name2id(map);
  3712. if (!start_point.map) {
  3713. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  3714. start_point.map = 0;
  3715. }
  3716. start_point.x = x;
  3717. start_point.y = y;
  3718. } else if (strcmpi(w1, "start_zeny") == 0) {
  3719. start_zeny = atoi(w2);
  3720. if (start_zeny < 0)
  3721. start_zeny = 0;
  3722. } else if (strcmpi(w1, "start_weapon") == 0) {
  3723. start_weapon = atoi(w2);
  3724. if (start_weapon < 0)
  3725. start_weapon = 0;
  3726. } else if (strcmpi(w1, "start_armor") == 0) {
  3727. start_armor = atoi(w2);
  3728. if (start_armor < 0)
  3729. start_armor = 0;
  3730. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  3731. log_char = atoi(w2);
  3732. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  3733. strcpy(unknown_char_name, w2);
  3734. unknown_char_name[NAME_LENGTH-1] = '\0';
  3735. } else if (strcmpi(w1, "char_log_filename") == 0) {
  3736. strcpy(char_log_filename, w2);
  3737. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  3738. name_ignoring_case = (bool)config_switch(w2);
  3739. } else if (strcmpi(w1, "char_name_option") == 0) {
  3740. char_name_option = atoi(w2);
  3741. } else if (strcmpi(w1, "char_name_letters") == 0) {
  3742. strcpy(char_name_letters, w2);
  3743. // online files options
  3744. } else if (strcmpi(w1, "online_txt_filename") == 0) {
  3745. strcpy(online_txt_filename, w2);
  3746. } else if (strcmpi(w1, "online_html_filename") == 0) {
  3747. strcpy(online_html_filename, w2);
  3748. } else if (strcmpi(w1, "online_sorting_option") == 0) {
  3749. online_sorting_option = atoi(w2);
  3750. } else if (strcmpi(w1, "online_display_option") == 0) {
  3751. online_display_option = atoi(w2);
  3752. } else if (strcmpi(w1, "online_gm_display_min_level") == 0) { // minimum GM level to display 'GM' when we want to display it
  3753. online_gm_display_min_level = atoi(w2);
  3754. if (online_gm_display_min_level < 5) // send online file every 5 seconds to player is enough
  3755. online_gm_display_min_level = 5;
  3756. } else if (strcmpi(w1, "online_refresh_html") == 0) {
  3757. online_refresh_html = atoi(w2);
  3758. if (online_refresh_html < 1)
  3759. online_refresh_html = 1;
  3760. } else if(strcmpi(w1,"db_path")==0) {
  3761. strcpy(db_path,w2);
  3762. } else if (strcmpi(w1, "console") == 0) {
  3763. console = config_switch(w2);
  3764. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  3765. fame_list_size_chemist = atoi(w2);
  3766. if (fame_list_size_chemist > MAX_FAME_LIST) {
  3767. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  3768. fame_list_size_chemist = MAX_FAME_LIST;
  3769. }
  3770. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  3771. fame_list_size_smith = atoi(w2);
  3772. if (fame_list_size_smith > MAX_FAME_LIST) {
  3773. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  3774. fame_list_size_smith = MAX_FAME_LIST;
  3775. }
  3776. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  3777. fame_list_size_taekwon = atoi(w2);
  3778. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  3779. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  3780. fame_list_size_taekwon = MAX_FAME_LIST;
  3781. }
  3782. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  3783. guild_exp_rate = atoi(w2);
  3784. #endif //TXT_SQL_CONVERT
  3785. } else if (strcmpi(w1, "import") == 0) {
  3786. char_config_read(w2);
  3787. }
  3788. }
  3789. fclose(fp);
  3790. ShowInfo("Done reading %s.\n", cfgName);
  3791. return 0;
  3792. }
  3793. #ifndef TXT_SQL_CONVERT
  3794. int chardb_final(int key, void* data, va_list va)
  3795. {
  3796. aFree(data);
  3797. return 0;
  3798. }
  3799. void do_final(void)
  3800. {
  3801. ShowStatus("Terminating server.\n");
  3802. mmo_char_sync();
  3803. inter_save();
  3804. set_all_offline(-1);
  3805. flush_fifos();
  3806. // write online players files with no player
  3807. online_char_db->clear(online_char_db, NULL); //clean the db...
  3808. create_online_files();
  3809. online_char_db->destroy(online_char_db, NULL); //dispose the db...
  3810. auth_db->destroy(auth_db, NULL);
  3811. if(char_dat) aFree(char_dat);
  3812. if (login_fd > 0)
  3813. do_close(login_fd);
  3814. if (char_fd > 0)
  3815. do_close(char_fd);
  3816. #ifdef ENABLE_SC_SAVING
  3817. status_final();
  3818. #endif
  3819. inter_final();
  3820. mapindex_final();
  3821. char_log("----End of char-server (normal end with closing of all files).\n");
  3822. }
  3823. //------------------------------
  3824. // Function called when the server
  3825. // has received a crash signal.
  3826. //------------------------------
  3827. void do_abort(void)
  3828. {
  3829. }
  3830. void set_server_type(void)
  3831. {
  3832. SERVER_TYPE = ATHENA_SERVER_CHAR;
  3833. }
  3834. int do_init(int argc, char **argv)
  3835. {
  3836. int i;
  3837. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3838. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3839. server[i].fd = -1;
  3840. }
  3841. //Read map indexes
  3842. mapindex_init();
  3843. start_point.map = mapindex_name2id("new_zone01");
  3844. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  3845. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  3846. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  3847. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  3848. ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n");
  3849. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  3850. }
  3851. ShowInfo("Finished reading the char-server configuration.\n");
  3852. // a newline in the log...
  3853. char_log("");
  3854. char_log("The char-server starting...\n");
  3855. ShowInfo("Initializing char server.\n");
  3856. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  3857. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  3858. mmo_char_init();
  3859. char_read_fame_list(); //Read fame lists.
  3860. #ifdef ENABLE_SC_SAVING
  3861. status_init();
  3862. #endif
  3863. inter_init_txt((argc > 2) ? argv[2] : inter_cfgName); // inter server 初期化
  3864. ShowInfo("char server initialized.\n");
  3865. set_defaultparse(parse_char);
  3866. if ((naddr_ != 0) && (!login_ip || !char_ip))
  3867. {
  3868. char ip_str[16];
  3869. ip2str(addr_[0], ip_str);
  3870. if (naddr_ > 1)
  3871. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  3872. else
  3873. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  3874. if (!login_ip) {
  3875. strcpy(login_ip_str, ip_str);
  3876. login_ip = str2ip(login_ip_str);
  3877. }
  3878. if (!char_ip) {
  3879. strcpy(char_ip_str, ip_str);
  3880. char_ip = str2ip(char_ip_str);
  3881. }
  3882. }
  3883. // establish char-login connection if not present
  3884. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  3885. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  3886. // keep the char-login connection alive
  3887. add_timer_func_list(ping_login_server, "ping_login_server");
  3888. add_timer_interval(gettick() + 1000, ping_login_server, 0, 0, ((int)stall_time-2) * 1000);
  3889. // periodically update the overall user count on all mapservers + login server
  3890. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  3891. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  3892. // send a list of all online account IDs to login server
  3893. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  3894. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour
  3895. // ???
  3896. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  3897. // ???
  3898. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  3899. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  3900. // periodic flush of all saved data to disk
  3901. add_timer_func_list(mmo_char_sync_timer, "mmo_char_sync_timer");
  3902. add_timer_interval(gettick() + 1000, mmo_char_sync_timer, 0, 0, autosave_interval);
  3903. if( console )
  3904. {
  3905. //##TODO invoke a CONSOLE_START plugin event
  3906. }
  3907. char_fd = make_listen_bind(bind_ip, char_port);
  3908. char_log("The char-server is ready (Server is listening on the port %d).\n", char_port);
  3909. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  3910. return 0;
  3911. }
  3912. #endif //TXT_SQL_CONVERT