Lobby2Message_PGSQL.cpp 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "Lobby2Message_PGSQL.h"
  11. using namespace RakNet;
  12. STATIC_FACTORY_DEFINITIONS(Lobby2MessageFactory_PGSQL,Lobby2MessageFactory_PGSQL);
  13. unsigned int RakNet::GetUserRowFromHandle(RakNet::RakString& userName, PostgreSQLInterface *pgsql)
  14. {
  15. PGresult *result = pgsql->QueryVariadic("SELECT userId_pk,handle from lobby2.users WHERE handleLower=lower(%s)", userName.C_String());
  16. if (result)
  17. {
  18. unsigned int primaryKey;
  19. int numRowsReturned = PQntuples(result);
  20. if (numRowsReturned>0)
  21. {
  22. PostgreSQLInterface::PQGetValueFromBinary(&primaryKey, result, 0, "userId_pk");
  23. PostgreSQLInterface::PQGetValueFromBinary(&userName, result, 0, "handle");
  24. PQclear(result);
  25. return primaryKey;
  26. }
  27. PQclear(result);
  28. return 0;
  29. }
  30. return 0;
  31. }
  32. unsigned int RakNet::GetClanIdFromHandle(RakNet::RakString clanName, PostgreSQLInterface *pgsql)
  33. {
  34. PGresult *result = pgsql->QueryVariadic("SELECT clanId_pk from lobby2.clans WHERE clanHandleLower=lower(%s)", clanName.C_String());
  35. if (result)
  36. {
  37. unsigned int primaryKey;
  38. int numRowsReturned = PQntuples(result);
  39. if (numRowsReturned>0)
  40. {
  41. PostgreSQLInterface::PQGetValueFromBinary(&primaryKey, result, 0, "clanId_pk");
  42. PQclear(result);
  43. return primaryKey;
  44. }
  45. PQclear(result);
  46. return 0;
  47. }
  48. return 0;
  49. }
  50. bool RakNet::IsClanLeader(RakNet::RakString clanName, unsigned int userId, PostgreSQLInterface *pgsql)
  51. {
  52. unsigned int clanId = GetClanIdFromHandle(clanName, pgsql);
  53. if (clanId==0)
  54. return false;
  55. return IsClanLeader(clanId, userId, pgsql);
  56. }
  57. unsigned int RakNet::GetClanLeaderId(unsigned int clanId, PostgreSQLInterface *pgsql)
  58. {
  59. PGresult *result = pgsql->QueryVariadic("SELECT leaderUserId_fk FROM lobby2.clans WHERE clanId_pk=%i", clanId);
  60. if (result==0)
  61. return 0;
  62. int numRowsReturned = PQntuples(result);
  63. if (numRowsReturned==0)
  64. {
  65. PQclear(result);
  66. return 0;
  67. }
  68. int leaderId;
  69. PostgreSQLInterface::PQGetValueFromBinary(&leaderId, result, 0, "leaderUserId_fk");
  70. PQclear(result);
  71. return leaderId;
  72. }
  73. bool RakNet::IsClanLeader(unsigned int clanId, unsigned int userId, PostgreSQLInterface *pgsql)
  74. {
  75. return userId!=0 && GetClanLeaderId(clanId, pgsql)==userId;
  76. }
  77. RakNet::ClanMemberState RakNet::GetClanMemberState(unsigned int clanId, unsigned int userId, bool *isSubleader, PostgreSQLInterface *pgsql)
  78. {
  79. PGresult *result = pgsql->QueryVariadic("SELECT memberState_fk FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", userId, clanId);
  80. if (result==0)
  81. return CMD_UNDEFINED;
  82. int numRowsReturned = PQntuples(result);
  83. if (numRowsReturned==0)
  84. {
  85. PQclear(result);
  86. return CMD_UNDEFINED;
  87. }
  88. int memberState;
  89. PostgreSQLInterface::PQGetValueFromBinary(&memberState, result, 0, "memberState_fk");
  90. PostgreSQLInterface::PQGetValueFromBinary(isSubleader, result, 0, "isSubleader");
  91. PQclear(result);
  92. return (ClanMemberState) memberState;
  93. }
  94. void RakNet::GetClanMembers(unsigned int clanId, DataStructures::List<ClanMemberDescriptor> &clanMembers, PostgreSQLInterface *pgsql)
  95. {
  96. ClanMemberDescriptor cmd;
  97. PGresult *result = pgsql->QueryVariadic(
  98. "SELECT M.userId_fk, M.isSubleader, M.memberState_fk, M.banReason, U.handle "
  99. "FROM lobby2.clanMembers AS M, lobby2.users AS U WHERE M.clanId_fk=%i AND U.userId_pk=M.userId_fk", clanId);
  100. if (result==0)
  101. return;
  102. int numRowsReturned = PQntuples(result);
  103. int idx;
  104. for (idx=0; idx < numRowsReturned; idx++)
  105. {
  106. PostgreSQLInterface::PQGetValueFromBinary(&cmd.userId, result, idx, "userId_fk");
  107. PostgreSQLInterface::PQGetValueFromBinary(&cmd.isSubleader, result, idx, "isSubleader");
  108. int cms;
  109. PostgreSQLInterface::PQGetValueFromBinary(&cms, result, idx, "memberState_fk");
  110. cmd.memberState=(ClanMemberState)cms;
  111. PostgreSQLInterface::PQGetValueFromBinary(&cmd.banReason, result, idx, "banReason");
  112. PostgreSQLInterface::PQGetValueFromBinary(&cmd.name, result, idx, "handle");
  113. clanMembers.Insert(cmd, _FILE_AND_LINE_ );
  114. }
  115. PQclear(result);
  116. }
  117. bool RakNet::IsTitleInUse(RakNet::RakString titleName, PostgreSQLInterface *pgsql)
  118. {
  119. PGresult *result = pgsql->QueryVariadic("SELECT titleName_pk FROM lobby2.titles where titleName_pk=%s", titleName.C_String());
  120. if (result==0)
  121. return false;
  122. int numRowsReturned = PQntuples(result);
  123. PQclear(result);
  124. if (numRowsReturned==0)
  125. return false;
  126. return true;
  127. }
  128. bool RakNet::StringContainsProfanity(RakNet::RakString string, PostgreSQLInterface *pgsql)
  129. {
  130. RakNet::RakString strLower1 = " " + string;
  131. RakNet::RakString strLower2 = string + " ";
  132. RakNet::RakString strLower3 = " " + string + " ";
  133. RakNet::RakString strLower4 = string;
  134. strLower1.ToLower();
  135. strLower2.ToLower();
  136. strLower3.ToLower();
  137. strLower4.ToLower();
  138. PGresult *result = pgsql->QueryVariadic("SELECT wordLower FROM lobby2.profanity WHERE "
  139. "wordLower LIKE %s OR wordLower LIKE %s OR wordLower LIKE %s OR wordLower LIKE %s"
  140. , strLower1.C_String(), strLower2.C_String(), strLower3.C_String(), strLower4.C_String());
  141. if (result==0)
  142. return false;
  143. int numRowsReturned = PQntuples(result);
  144. PQclear(result);
  145. if (numRowsReturned==0)
  146. return false;
  147. return true;
  148. }
  149. bool RakNet::IsValidCountry(RakNet::RakString string, bool *countryHasStates, PostgreSQLInterface *pgsql)
  150. {
  151. PGresult *result = pgsql->QueryVariadic("SELECT country_name,country_has_states FROM lobby2.country where lower(country_name)=lower(%s)", string.C_String());
  152. if (result==0)
  153. return false;
  154. int numRowsReturned = PQntuples(result);
  155. if (countryHasStates && numRowsReturned>0)
  156. PostgreSQLInterface::PQGetValueFromBinary(countryHasStates, result, 0, "country_has_states");
  157. PQclear(result);
  158. if (numRowsReturned==0)
  159. return false;
  160. return true;
  161. }
  162. bool RakNet::IsValidState(RakNet::RakString string, PostgreSQLInterface *pgsql)
  163. {
  164. PGresult *result = pgsql->QueryVariadic("SELECT state_name FROM lobby2.state WHERE lower(state_name)=lower(%s)", string.C_String());
  165. if (result==0)
  166. return false;
  167. if (result==0)
  168. return false;
  169. int numRowsReturned = PQntuples(result);
  170. PQclear(result);
  171. if (numRowsReturned==0)
  172. return false;
  173. return true;
  174. }
  175. bool RakNet::IsValidRace(RakNet::RakString string, PostgreSQLInterface *pgsql)
  176. {
  177. PGresult *result = pgsql->QueryVariadic("SELECT race_text FROM lobby2.race WHERE lower(race_text)=lower(%s)", string.C_String());
  178. if (result==0)
  179. return false;
  180. if (result==0)
  181. return false;
  182. int numRowsReturned = PQntuples(result);
  183. PQclear(result);
  184. if (numRowsReturned==0)
  185. return false;
  186. return true;
  187. }
  188. void RakNet::GetFriendIDs(unsigned int callerUserId, bool excludeIfIgnored, PostgreSQLInterface *pgsql, DataStructures::List<unsigned int> &output)
  189. {
  190. PGresult *result = pgsql->QueryVariadic("SELECT userTwo_fk from lobby2.friends WHERE userOne_fk=%i AND "
  191. "actionId_fk=(SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends');", callerUserId);
  192. if (result==0)
  193. return;
  194. int numRowsReturned = PQntuples(result);
  195. int idx;
  196. unsigned int id;
  197. for (idx=0; idx < numRowsReturned; idx++)
  198. {
  199. PostgreSQLInterface::PQGetValueFromBinary(&id, result, idx, "userTwo_fk");
  200. if (excludeIfIgnored==false || IsIgnoredByTarget(callerUserId, id, pgsql)==false)
  201. output.Insert(id, _FILE_AND_LINE_ );
  202. }
  203. PQclear(result);
  204. }
  205. void RakNet::GetClanMateIDs(unsigned int callerUserId, bool excludeIfIgnored, PostgreSQLInterface *pgsql, DataStructures::List<unsigned int> &output)
  206. {
  207. PGresult *result = pgsql->QueryVariadic(
  208. "select userId_fk from lobby2.clanMembers where clanId_fk="
  209. "(select clanId_fk from lobby2.clanMembers where userId_fk=%i)"
  210. "AND userId_fk !=%i;"
  211. , callerUserId, callerUserId);
  212. if (result==0)
  213. return;
  214. int numRowsReturned = PQntuples(result);
  215. int idx;
  216. unsigned int id;
  217. for (idx=0; idx < numRowsReturned; idx++)
  218. {
  219. PostgreSQLInterface::PQGetValueFromBinary(&id, result, idx, "userId_fk");
  220. if (excludeIfIgnored==false || IsIgnoredByTarget(callerUserId, id, pgsql)==false)
  221. output.Insert(id, _FILE_AND_LINE_ );
  222. }
  223. PQclear(result);
  224. }
  225. bool RakNet::IsIgnoredByTarget(unsigned int callerUserId, unsigned int targetUserId, PostgreSQLInterface *pgsql)
  226. {
  227. PGresult *result = pgsql->QueryVariadic(
  228. "select userMe_fk from lobby2.ignore where userMe_fk=%i AND userOther_fk=%i"
  229. , callerUserId, targetUserId);
  230. if (result==0)
  231. return false;
  232. int numRowsReturned = PQntuples(result);
  233. PQclear(result);
  234. return numRowsReturned>0;
  235. }
  236. void RakNet::OutputFriendsNotification(RakNet::Notification_Friends_StatusChange::Status notificationType, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
  237. {
  238. // Tell all friends about this new login
  239. DataStructures::List<unsigned int> output;
  240. GetFriendIDs(command->callerUserId, true, pgsql, output);
  241. unsigned int idx;
  242. for (idx=0; idx < output.Size(); idx++)
  243. {
  244. Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
  245. RakAssert(command->callingUserName.IsEmpty()==false);
  246. notification->otherHandle=command->callingUserName;
  247. notification->op=notificationType;
  248. notification->resultCode=L2RC_SUCCESS;
  249. command->server->AddOutputFromThread(notification, output[idx], "");
  250. }
  251. }
  252. void RakNet::GetFriendInfosByStatus(unsigned int callerUserId, RakNet::RakString status, PostgreSQLInterface *pgsql, DataStructures::List<FriendInfo> &output, bool callerIsUserOne)
  253. {
  254. RakNet::RakString query;
  255. /*
  256. if (callerIsUserOne)
  257. {
  258. query = "SELECT handle from lobby2.users WHERE userId_pk ="
  259. "(SELECT userTwo_fk from lobby2.friends WHERE userOne_fk=%i AND actionId_fk ="
  260. "(SELECT actionId_pk FROM lobby2.friendActions where description='";
  261. }
  262. else
  263. {
  264. query = "SELECT handle from lobby2.users WHERE userId_pk ="
  265. "(SELECT userOne_fk from lobby2.friends WHERE userTwo_fk=%i AND actionId_fk ="
  266. "(SELECT actionId_pk FROM lobby2.friendActions where description='";
  267. }
  268. query+=status;
  269. query+="'));";
  270. */
  271. if (callerIsUserOne)
  272. {
  273. query = "SELECT handle from lobby2.friends,lobby2.users WHERE userId_pk=userTwo_fk AND userOne_fk=%i";
  274. }
  275. else
  276. {
  277. query = "SELECT handle from lobby2.friends,lobby2.users WHERE userId_pk=userOne_fk AND userTwo_fk=%i";
  278. }
  279. query+=" AND actionId_fk=(SELECT actionId_pk FROM lobby2.friendActions where description='";
  280. query+=status;
  281. query+="');";
  282. PGresult *result = pgsql->QueryVariadic( query.C_String(), callerUserId );
  283. RakAssert(result);
  284. int numRowsReturned = PQntuples(result);
  285. int i;
  286. for (i=0; i < numRowsReturned; i++)
  287. {
  288. FriendInfo fi;
  289. PostgreSQLInterface::PQGetValueFromBinary(&fi.usernameAndStatus.handle, result, i, "handle");
  290. fi.usernameAndStatus.isOnline=false;
  291. output.Insert(fi, _FILE_AND_LINE_ );
  292. }
  293. PQclear(result);
  294. }
  295. void RakNet::SendEmail(DataStructures::List<RakNet::RakString> &recipientNames, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
  296. {
  297. DataStructures::List<unsigned int> targetUserIds;
  298. unsigned int targetUserId;
  299. for (unsigned int i=0; i < recipientNames.Size(); i++)
  300. {
  301. targetUserId = GetUserRowFromHandle(recipientNames[i], pgsql);
  302. if (targetUserId!=0)
  303. targetUserIds.Insert(targetUserId, _FILE_AND_LINE_ );
  304. }
  305. SendEmail(targetUserIds, senderUserId, senderUserName, server, subject, body, binaryData, status, triggerString, pgsql);
  306. }
  307. void RakNet::SendEmail(unsigned int targetUserId, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
  308. {
  309. DataStructures::List<unsigned int> targetUserIds;
  310. targetUserIds.Insert(targetUserId, _FILE_AND_LINE_ );
  311. SendEmail(targetUserIds, senderUserId, senderUserName, server, subject, body, binaryData, status, triggerString, pgsql);
  312. }
  313. void RakNet::SendEmail(DataStructures::List<unsigned int> &targetUserIds, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
  314. {
  315. if (targetUserIds.Size()==0)
  316. return;
  317. PGresult *result=0;
  318. if (binaryData.IsNull()==false)
  319. {
  320. result = pgsql->QueryVariadic(
  321. "INSERT INTO lobby2.emails (subject, body, binaryData, triggerId_fk) VALUES "
  322. "(%s, %s, %a, (SELECT triggerId_pk FROM lobby2.emailTriggers WHERE description=%s) ) RETURNING emailId_pk;"
  323. ,subject.C_String(), body.C_String(), binaryData->binaryData, binaryData->binaryDataLength, triggerString.C_String()
  324. );
  325. }
  326. else
  327. {
  328. result = pgsql->QueryVariadic(
  329. "INSERT INTO lobby2.emails (subject, body, triggerId_fk) VALUES "
  330. "(%s, %s, %a, (SELECT triggerId_pk FROM lobby2.emailTriggers WHERE description=%s) ) RETURNING emailId_pk;"
  331. ,subject.C_String(), body.C_String(), triggerString.C_String()
  332. );
  333. }
  334. RakAssert(result);
  335. unsigned int emailId_pk;
  336. PostgreSQLInterface::PQGetValueFromBinary(&emailId_pk, result, 0, "emailId_pk");
  337. PQclear(result);
  338. unsigned int i;
  339. for (i=0; i < targetUserIds.Size(); i++)
  340. {
  341. // Once in my inbox
  342. result = pgsql->QueryVariadic(
  343. "INSERT INTO lobby2.emailTargets (emailId_fk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail) VALUES "
  344. "(%i, %i, %i, %i, %b, %b);", emailId_pk, senderUserId, targetUserIds[i], status, true, true);
  345. RakAssert(result);
  346. PQclear(result);
  347. // Once in the destination inbox
  348. result = pgsql->QueryVariadic(
  349. "INSERT INTO lobby2.emailTargets (emailId_fk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail) VALUES "
  350. "(%i, %i, %i, %i, %b, %b) RETURNING emailTarget_pk;", emailId_pk, targetUserIds[i], senderUserId, status, false, false);
  351. RakAssert(result);
  352. unsigned int emailTarget_pk;
  353. PostgreSQLInterface::PQGetValueFromBinary(&emailTarget_pk, result, 0, "emailTarget_pk");
  354. PQclear(result);
  355. // Notify recipient that they got an email
  356. Notification_Emails_Received *notification = (Notification_Emails_Received *) server->GetMessageFactory()->Alloc(L2MID_Notification_Emails_Received);
  357. notification->sender=senderUserName;
  358. notification->subject=subject;
  359. notification->emailId=emailTarget_pk;
  360. notification->resultCode=L2RC_SUCCESS;
  361. server->AddOutputFromThread(notification, targetUserIds[i], "");
  362. }
  363. }
  364. int RakNet::GetActiveClanCount(unsigned int userId, PostgreSQLInterface *pgsql)
  365. {
  366. PGresult *result = pgsql->QueryVariadic("SELECT clanMemberId_pk FROM lobby2.clanMembers WHERE userId_fk=%i AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active')", userId);
  367. int numRowsReturned = PQntuples(result);
  368. PQclear(result);
  369. return numRowsReturned;
  370. }
  371. bool RakNet::CreateAccountParametersFailed( CreateAccountParameters &createAccountParameters, RakNet::Lobby2ResultCode &resultCode, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
  372. {
  373. bool hasStates=true;
  374. if (createAccountParameters.homeCountry.IsEmpty()==false)
  375. {
  376. if (IsValidCountry(createAccountParameters.homeCountry, &hasStates, pgsql)==false)
  377. {
  378. resultCode=L2RC_Client_RegisterAccount_INVALID_COUNTRY;
  379. return true;
  380. }
  381. }
  382. if (hasStates==true)
  383. {
  384. if (createAccountParameters.homeState.IsEmpty()==false && IsValidState(createAccountParameters.homeState, pgsql)==false)
  385. {
  386. resultCode=L2RC_Client_RegisterAccount_INVALID_STATE;
  387. return true;
  388. }
  389. }
  390. else
  391. createAccountParameters.homeState.Clear();
  392. if (createAccountParameters.billingCountry.IsEmpty()==false)
  393. {
  394. if (IsValidCountry(createAccountParameters.billingCountry, &hasStates, pgsql)==false)
  395. {
  396. resultCode=L2RC_Client_RegisterAccount_INVALID_COUNTRY;
  397. return true;
  398. }
  399. }
  400. if (hasStates==true)
  401. {
  402. if (createAccountParameters.billingState.IsEmpty()==false && IsValidState(createAccountParameters.billingState, pgsql)==false)
  403. {
  404. resultCode=L2RC_Client_RegisterAccount_INVALID_STATE;
  405. return true;
  406. }
  407. }
  408. else
  409. createAccountParameters.billingState.Clear();
  410. if (createAccountParameters.race.IsEmpty()==false &&
  411. IsValidRace(createAccountParameters.race, pgsql)==false)
  412. {
  413. resultCode=L2RC_Client_RegisterAccount_INVALID_RACE;
  414. return true;
  415. }
  416. unsigned int requiredAgeYears = command->server->GetConfigurationProperties()->accountRegistrationRequiredAgeYears;
  417. if (createAccountParameters.ageInDays < requiredAgeYears*365 )
  418. {
  419. resultCode=L2RC_Client_RegisterAccount_REQUIRED_AGE_NOT_MET;
  420. return true;
  421. }
  422. return false;
  423. }
  424. void RakNet::UpdateAccountFromMissingCreationParameters(CreateAccountParameters &createAccountParameters, unsigned int userPrimaryKey, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
  425. {
  426. (void)command;
  427. PGresult *result=0;
  428. unsigned int key;
  429. if (createAccountParameters.homeState.IsEmpty()==false)
  430. {
  431. result = pgsql->QueryVariadic("SELECT state_id FROM lobby2.state where lower(state_name)=lower(%s)", createAccountParameters.homeState.C_String());
  432. if (result)
  433. {
  434. if (PQntuples(result))
  435. {
  436. PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "state_id");
  437. PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeStateId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  438. }
  439. PQclear(result);
  440. }
  441. }
  442. if (createAccountParameters.homeCountry.IsEmpty()==false)
  443. {
  444. result = pgsql->QueryVariadic("SELECT country_id FROM lobby2.country where lower(country_name)=lower(%s)", createAccountParameters.homeCountry.C_String());
  445. if (result)
  446. {
  447. if (PQntuples(result))
  448. {
  449. PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "country_id");
  450. PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  451. }
  452. PQclear(result);
  453. }
  454. }
  455. if (createAccountParameters.billingState.IsEmpty()==false)
  456. {
  457. result = pgsql->QueryVariadic("SELECT state_id FROM lobby2.state where lower(state_name)=lower(%s)", createAccountParameters.billingState.C_String());
  458. if (result)
  459. {
  460. if (PQntuples(result))
  461. {
  462. PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "state_id");
  463. PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET billingStateId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  464. }
  465. PQclear(result);
  466. }
  467. }
  468. if (createAccountParameters.billingCountry.IsEmpty()==false)
  469. {
  470. result = pgsql->QueryVariadic("SELECT country_id FROM lobby2.country where lower(country_name)=lower(%s)", createAccountParameters.billingCountry.C_String());
  471. if (result)
  472. {
  473. if (PQntuples(result))
  474. {
  475. PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "country_id");
  476. PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET billingCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  477. }
  478. PQclear(result);
  479. }
  480. }
  481. if (createAccountParameters.race.IsEmpty()==false)
  482. {
  483. result = pgsql->QueryVariadic("SELECT race_id FROM lobby2.race where lower(race_text)=lower(%s)", createAccountParameters.race.C_String());
  484. if (result)
  485. {
  486. if (PQntuples(result))
  487. {
  488. PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "race_id");
  489. PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET raceId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  490. }
  491. PQclear(result);
  492. }
  493. }
  494. }
  495. bool RakNet::System_CreateDatabase_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  496. {
  497. (void)command;
  498. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  499. bool success;
  500. PGresult *result=0;
  501. pgsql->ExecuteBlockingCommand("DROP SCHEMA lobby2 CASCADE;", &result, false);
  502. PQclear(result);
  503. pgsql->ExecuteBlockingCommand("DROP LANGUAGE plpgsql;", &result, false);
  504. PQclear(result);
  505. FILE *fp = fopen("Lobby2Schema.txt", "rb");
  506. RakAssert(fp && "Can't find Lobby2Schema.txt");
  507. fseek( fp, 0, SEEK_END );
  508. unsigned int fileSize = ftell( fp );
  509. fseek( fp, 0, SEEK_SET );
  510. char *cmd = (char*) rakMalloc_Ex(fileSize+1, _FILE_AND_LINE_);
  511. fread(cmd, 1, fileSize, fp);
  512. fclose(fp);
  513. cmd[fileSize]=0;
  514. success = pgsql->ExecuteBlockingCommand(cmd, &result, false);
  515. PQclear(result);
  516. if (success)
  517. {
  518. resultCode=L2RC_SUCCESS;
  519. }
  520. else
  521. {
  522. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  523. printf(cmd);
  524. printf(pgsql->GetLastError());
  525. }
  526. rakFree_Ex(cmd, _FILE_AND_LINE_ );
  527. return true;
  528. }
  529. bool RakNet::System_DestroyDatabase_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  530. {
  531. (void)command;
  532. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  533. bool success;
  534. PGresult *result=0;
  535. success=pgsql->ExecuteBlockingCommand("DROP SCHEMA lobby2 CASCADE;", &result, false);
  536. PQclear(result);
  537. if (success)
  538. resultCode=L2RC_SUCCESS;
  539. else
  540. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  541. return true;
  542. }
  543. bool RakNet::System_CreateTitle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  544. {
  545. (void)command;
  546. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  547. PGresult *result = pgsql->QueryVariadic("INSERT INTO lobby2.titles (titleName_pk, titleSecretKey, requiredAge, binaryData) VALUES (%s,%s,%i,%a)",
  548. titleName.C_String(),
  549. titleSecretKey.C_String(),
  550. requiredAge,
  551. binaryData->binaryData,
  552. binaryData->binaryDataLength);
  553. if (result!=0)
  554. {
  555. PQclear(result);
  556. resultCode=L2RC_SUCCESS;
  557. }
  558. else
  559. {
  560. resultCode=L2RC_System_CreateTitle_TITLE_ALREADY_IN_USE;
  561. }
  562. return true;
  563. }
  564. bool RakNet::System_DestroyTitle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  565. {
  566. (void)command;
  567. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  568. PGresult *result=0;
  569. result = pgsql->QueryVariadic("DELETE FROM lobby2.titles WHERE titlename_pk=%s", titleName.C_String());
  570. if (result!=0)
  571. {
  572. PQclear(result);
  573. resultCode=L2RC_SUCCESS;
  574. }
  575. else
  576. {
  577. resultCode=L2RC_System_DestroyTitle_TITLE_NOT_IN_USE;
  578. }
  579. return true;
  580. }
  581. bool RakNet::System_GetTitleRequiredAge_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  582. {
  583. (void)command;
  584. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  585. PGresult *result = pgsql->QueryVariadic("SELECT requiredAge FROM lobby2.titles where titlename_pk=%s", titleName.C_String());
  586. if (result!=0)
  587. {
  588. PostgreSQLInterface::PQGetValueFromBinary(&requiredAge, result, 0, "requiredAge");
  589. PQclear(result);
  590. resultCode=L2RC_SUCCESS;
  591. }
  592. else
  593. {
  594. resultCode=L2RC_System_GetTitleRequiredAge_TITLE_NOT_IN_USE;
  595. }
  596. return true;
  597. }
  598. bool RakNet::System_GetTitleBinaryData_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  599. {
  600. (void)command;
  601. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  602. PGresult *result = pgsql->QueryVariadic("SELECT binaryData FROM lobby2.titles where titlename_pk=%s", titleName.C_String());
  603. if (result!=0)
  604. {
  605. PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
  606. PQclear(result);
  607. resultCode=L2RC_SUCCESS;
  608. }
  609. else
  610. {
  611. resultCode=L2RC_System_GetTitleBinaryData_TITLE_NOT_IN_USE;
  612. }
  613. return true;
  614. }
  615. bool RakNet::System_RegisterProfanity_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  616. {
  617. (void)command;
  618. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  619. // pgsql->PrepareVariadic("INSERT INTO lobby2.profanity (word) VALUES (%s)");
  620. // for (unsigned int i=0; i < profanityWords.Size(); i++)
  621. // pgsql->PrepareVariadicArgs(0, profanityWords[i].C_String());
  622. // PGresult *result = pgsql->ExecutePreparedStatement();
  623. PGresult *result=0;
  624. resultCode=L2RC_SUCCESS;
  625. for (unsigned int i=0; i < profanityWords.Size(); i++)
  626. {
  627. result = pgsql->QueryVariadic("INSERT INTO lobby2.profanity (word) VALUES (%s)", profanityWords[i].C_String());
  628. if (result==0)
  629. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  630. PQclear(result);
  631. if (resultCode==L2RC_DATABASE_CONSTRAINT_FAILURE)
  632. return true;
  633. }
  634. return true;
  635. }
  636. bool RakNet::System_BanUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  637. {
  638. (void)command;
  639. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  640. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  641. if (userRow==0)
  642. {
  643. resultCode=L2RC_UNKNOWN_USER;
  644. return true;
  645. }
  646. PGresult *result = pgsql->QueryVariadic("INSERT INTO lobby2.bannedUsers (userId_fk, description, timeout) VALUES (%i, %s, now () + %i * interval '1 hours')", userRow, banReason.C_String(), durationHours);
  647. if (result!=0)
  648. {
  649. PQclear(result);
  650. result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, description, triggerId_fk) "
  651. "VALUES (%i, %s, (SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Banned'))", userRow, banReason.C_String());
  652. RakAssert(result);
  653. PQclear(result);
  654. resultCode=L2RC_SUCCESS;
  655. }
  656. else
  657. {
  658. resultCode=L2RC_System_BanUser_ALREADY_BANNED;
  659. }
  660. return true;
  661. }
  662. bool RakNet::System_UnbanUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  663. {
  664. (void)command;
  665. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  666. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  667. if (userRow==0)
  668. {
  669. resultCode=L2RC_UNKNOWN_USER;
  670. return true;
  671. }
  672. PGresult *result = pgsql->QueryVariadic("DELETE FROM lobby2.bannedUsers WHERE userId_fk=%i", userRow);
  673. if (result!=0)
  674. {
  675. PQclear(result);
  676. result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, description, triggerId_fk) "
  677. "VALUES (%i, %s, (SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Unbanned'))", userRow, reason.C_String());
  678. RakAssert(result);
  679. PQclear(result);
  680. resultCode=L2RC_SUCCESS;
  681. }
  682. else
  683. {
  684. resultCode=L2RC_System_BanUser_ALREADY_BANNED;
  685. }
  686. return true;
  687. }
  688. bool RakNet::CDKey_Add_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  689. {
  690. (void)command;
  691. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  692. PGresult *result=0;
  693. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  694. {
  695. resultCode=L2RC_CDKey_Add_TITLE_NOT_IN_USE;
  696. return true;
  697. }
  698. unsigned int i;
  699. for (i=0; i < cdKeys.Size(); i++)
  700. {
  701. result = pgsql->QueryVariadic("INSERT INTO lobby2.cdkeys (cdKey, usable, stolen, titleName_fk) VALUES (%s, true, false, %s);", cdKeys[i].C_String(), titleName.C_String());
  702. RakAssert(result);
  703. PQclear(result);
  704. }
  705. resultCode=L2RC_SUCCESS;
  706. return true;
  707. }
  708. bool RakNet::CDKey_GetStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  709. {
  710. (void)command;
  711. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  712. PGresult *result=0;
  713. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  714. {
  715. resultCode=L2RC_CDKey_GetStatus_TITLE_NOT_IN_USE;
  716. return true;
  717. }
  718. result = pgsql->QueryVariadic(
  719. "SELECT lobby2.cdkeys.usable, lobby2.cdkeys.stolen, lobby2.cdkeys.activationDate, lobby2.users.handle "
  720. "FROM lobby2.cdkeys LEFT OUTER JOIN lobby2.users ON lobby2.users.userId_pk=lobby2.cdkeys.userId_fk "
  721. "WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s;"
  722. , cdKey.C_String(), titleName.C_String());
  723. int numRowsReturned = PQntuples(result);
  724. if (numRowsReturned==0)
  725. {
  726. PQclear(result);
  727. resultCode=L2RC_CDKey_GetStatus_UNKNOWN_CD_KEY;
  728. return true;
  729. }
  730. PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "lobby2.cdkeys.usable");
  731. PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "lobby2.cdkeys.stolen");
  732. PostgreSQLInterface::PQGetValueFromBinary(&usedBy, result, 0, "lobby2.users.handle");
  733. PostgreSQLInterface::PQGetValueFromBinary(&activationDate, result, 0, "lobby2.cdkeys.activationDate");
  734. PQclear(result);
  735. resultCode=L2RC_SUCCESS;
  736. return true;
  737. }
  738. bool RakNet::CDKey_Use_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  739. {
  740. (void)command;
  741. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  742. PGresult *result=0;
  743. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  744. {
  745. resultCode=L2RC_CDKey_Use_TITLE_NOT_IN_USE;
  746. return true;
  747. }
  748. result = pgsql->QueryVariadic("SELECT lobby2.cdkeys.usable, lobby2.cdkeys.stolen, lobby2.cdkeys.userId_fk "
  749. "FROM lobby2.cdkeys, lobby2.users WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
  750. cdKey.C_String(), titleName.C_String());
  751. int numRowsReturned = PQntuples(result);
  752. if (numRowsReturned==0)
  753. {
  754. PQclear(result);
  755. resultCode=L2RC_CDKey_Use_UNKNOWN_CD_KEY;
  756. return true;
  757. }
  758. bool usable, wasStolen, alreadyUsed;
  759. unsigned int userId;
  760. PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "lobby2.cdkeys.usable");
  761. PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "lobby2.cdkeys.stolen");
  762. alreadyUsed=PostgreSQLInterface::PQGetValueFromBinary(&userId, result, 0, "lobby2.cdkeys.userId_fk");
  763. PQclear(result);
  764. if (wasStolen)
  765. resultCode=L2RC_CDKey_Use_CD_KEY_STOLEN;
  766. else if (alreadyUsed)
  767. resultCode=L2RC_CDKey_Use_CD_KEY_ALREADY_USED;
  768. else if (usable==false)
  769. resultCode=L2RC_CDKey_Use_NOT_USABLE;
  770. else
  771. {
  772. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  773. if (userRow==0)
  774. {
  775. resultCode=L2RC_UNKNOWN_USER;
  776. return true;
  777. }
  778. result = pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET activationDate=now(),"
  779. "userId_fk=%i WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", userRow, cdKey.C_String(), titleName.C_String());
  780. if (result==0)
  781. {
  782. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  783. return true;
  784. }
  785. PQclear(result);
  786. resultCode=L2RC_SUCCESS;
  787. }
  788. return true;
  789. }
  790. bool RakNet::CDKey_FlagStolen_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  791. {
  792. (void)command;
  793. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  794. PGresult *result=0;
  795. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  796. {
  797. resultCode=L2RC_CDKey_FlagStolen_TITLE_NOT_IN_USE;
  798. return true;
  799. }
  800. result = pgsql->QueryVariadic("SELECT lobby2.cdkeys.cdKey, lobby2.cdkeys.userId_fk FROM lobby2.cdkeys WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
  801. cdKey.C_String(), titleName.C_String());
  802. if (result==0)
  803. {
  804. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  805. return true;
  806. }
  807. int numRowsReturned = PQntuples(result);
  808. if (numRowsReturned==0)
  809. {
  810. PQclear(result);
  811. resultCode=L2RC_CDKey_Use_UNKNOWN_CD_KEY;
  812. return true;
  813. }
  814. unsigned int userId_fk;
  815. if (PostgreSQLInterface::PQGetValueFromBinary(&userId_fk, result, 0, "userId_fk"))
  816. {
  817. PQclear(result);
  818. result = pgsql->QueryVariadic("SELECT handle from lobby2.users WHERE userId_pk=%i", userId_fk);
  819. PostgreSQLInterface::PQGetValueFromBinary(&userUsingThisKey, result, 0, "handle");
  820. PQclear(result);
  821. }
  822. else
  823. PQclear(result);
  824. result = pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET stolen=%b WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", wasStolen, cdKey.C_String(), titleName.C_String());
  825. if (result==0)
  826. {
  827. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  828. return true;
  829. }
  830. PQclear(result);
  831. resultCode=L2RC_SUCCESS;
  832. return true;
  833. }
  834. bool RakNet::Client_Login_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  835. {
  836. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  837. PGresult *result=0;
  838. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  839. if (userRow==0)
  840. {
  841. resultCode=L2RC_Client_Login_HANDLE_NOT_IN_USE_OR_BAD_SECRET_KEY;
  842. return true;
  843. }
  844. result = pgsql->QueryVariadic("SELECT password FROM lobby2.users WHERE userId_pk=%i", userRow);
  845. RakNet::RakString password;
  846. PostgreSQLInterface::PQGetValueFromBinary(&password, result, 0, "password");
  847. PQclear(result);
  848. if (password!=userPassword)
  849. {
  850. resultCode=L2RC_Client_Login_HANDLE_NOT_IN_USE_OR_BAD_SECRET_KEY;
  851. return true;
  852. }
  853. if (command->server->GetConfigurationProperties()->requiresEmailAddressValidationToLogin)
  854. {
  855. result = pgsql->QueryVariadic("SELECT emailAddressValidated FROM lobby2.users WHERE userId_pk=%i", userRow);
  856. bool emailAddressValidated;
  857. PostgreSQLInterface::PQGetValueFromBinary(&emailAddressValidated, result, 0, "emailAddressValidated");
  858. PQclear(result);
  859. if (emailAddressValidated==false)
  860. {
  861. resultCode=L2RC_Client_Login_EMAIL_ADDRESS_NOT_VALIDATED;
  862. return true;
  863. }
  864. }
  865. if (command->server->GetConfigurationProperties()->requiresTitleToLogin)
  866. {
  867. RakNet::RakString titleDBSecretKey;
  868. result = pgsql->QueryVariadic("SELECT titleSecretKey FROM lobby2.titles where titleName_pk=%s", titleName.C_String());
  869. int numRowsReturned = PQntuples(result);
  870. if (numRowsReturned==0)
  871. {
  872. resultCode=L2RC_Client_Login_BAD_TITLE_OR_TITLE_SECRET_KEY;
  873. PQclear(result);
  874. return true;
  875. }
  876. PostgreSQLInterface::PQGetValueFromBinary(&titleDBSecretKey, result, 0, "titleSecretKey");
  877. PQclear(result);
  878. // title can have no secret key, in which case you just have to specify a valid title
  879. if (titleDBSecretKey.IsEmpty()==false && titleDBSecretKey!=titleSecretKey)
  880. {
  881. resultCode=L2RC_Client_Login_BAD_TITLE_OR_TITLE_SECRET_KEY;
  882. return true;
  883. }
  884. }
  885. // Does this user have any stolen CD keys?
  886. result = pgsql->QueryVariadic("SELECT stolen FROM lobby2.cdkeys WHERE userId_fk=%i AND stolen=TRUE", userRow);
  887. int numRowsReturned = PQntuples(result);
  888. PQclear(result);
  889. if (numRowsReturned!=0)
  890. {
  891. resultCode=L2RC_Client_Login_CDKEY_STOLEN;
  892. return true;
  893. }
  894. result = pgsql->QueryVariadic("SELECT description, timeout, creationDate from lobby2.bannedUsers WHERE userId_fk=%i AND now() < timeout", userRow);
  895. numRowsReturned = PQntuples(result);
  896. if (numRowsReturned!=0)
  897. {
  898. PostgreSQLInterface::PQGetValueFromBinary(&bannedReason, result, 0, "description");
  899. PostgreSQLInterface::PQGetValueFromBinary(&bannedExpiration, result, 0, "timeout");
  900. PostgreSQLInterface::PQGetValueFromBinary(&whenBanned, result, 0, "creationDate");
  901. PQclear(result);
  902. resultCode=L2RC_Client_Login_BANNED;
  903. return true;
  904. }
  905. PQclear(result);
  906. result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, triggerId_fk) "
  907. "VALUES "
  908. "(%i,"
  909. "(SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Login'))", userRow);
  910. PQclear(result);
  911. command->callingUserName=userName;
  912. command->callerUserId=userRow;
  913. // Let the user do this if they want. Not all users may want ignore lists
  914. /*
  915. // Trigger GetIgnoreList for this user, so they download the client ignore list used for rooms when they logon
  916. Client_GetIgnoreList *ignoreListRequest = (Client_GetIgnoreList *) command->server->GetMessageFactory()->Alloc(L2MID_Client_GetIgnoreList);
  917. ignoreListRequest->ServerDBImpl(command, databaseInterface);
  918. command->server->AddOutputFromThread(ignoreListRequest, userRow, userName);
  919. */
  920. OutputFriendsNotification(Notification_Friends_StatusChange::FRIEND_LOGGED_IN, command, pgsql);
  921. // Have the server record in memory this new login
  922. command->server->OnLogin(command, true);
  923. resultCode=L2RC_SUCCESS;
  924. return true;
  925. }
  926. bool RakNet::Client_Logoff_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  927. {
  928. if (command->callerUserId==0)
  929. return false;
  930. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  931. PGresult *result=0;
  932. result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, triggerId_fk) "
  933. "VALUES "
  934. "(%i,"
  935. "(SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Logoff'));", command->callerUserId);
  936. PQclear(result);
  937. // Notification is done below
  938. command->server->OnLogoff(command, true);
  939. resultCode=L2RC_SUCCESS;
  940. return true;
  941. }
  942. bool RakNet::Client_RegisterAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  943. {
  944. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  945. PGresult *result=0;
  946. if (StringContainsProfanity(userName, pgsql))
  947. {
  948. resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
  949. return true;
  950. }
  951. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  952. if (userRow!=0)
  953. {
  954. resultCode=L2RC_Client_RegisterAccount_HANDLE_ALREADY_IN_USE;
  955. return true;
  956. }
  957. if (CreateAccountParametersFailed(createAccountParameters, resultCode, command, pgsql))
  958. return true;
  959. if (command->server->GetConfigurationProperties()->accountRegistrationRequiresCDKey)
  960. {
  961. if (cdKey.IsEmpty())
  962. {
  963. resultCode=L2RC_Client_RegisterAccount_REQUIRES_CD_KEY;
  964. return true;
  965. }
  966. if (titleName.IsEmpty())
  967. {
  968. resultCode=L2RC_Client_RegisterAccount_REQUIRES_CD_KEY;
  969. return true;
  970. }
  971. result = pgsql->QueryVariadic("SELECT usable, stolen "
  972. "FROM lobby2.cdkeys WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
  973. cdKey.C_String(), titleName.C_String());
  974. int numRowsReturned = PQntuples(result);
  975. if (numRowsReturned==0)
  976. {
  977. PQclear(result);
  978. resultCode=L2RC_Client_RegisterAccount_CD_KEY_NOT_USABLE;
  979. return true;
  980. }
  981. bool usable;
  982. bool wasStolen;
  983. RakNet::RakString usedBy;
  984. PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "usable");
  985. PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "stolen");
  986. PQclear(result);
  987. if (usable==false)
  988. {
  989. PQclear(result);
  990. resultCode=L2RC_Client_RegisterAccount_CD_KEY_NOT_USABLE;
  991. return true;
  992. }
  993. if (wasStolen)
  994. {
  995. PQclear(result);
  996. resultCode=L2RC_Client_RegisterAccount_CD_KEY_STOLEN;
  997. return true;
  998. }
  999. if (usedBy.IsEmpty()==false)
  1000. {
  1001. PQclear(result);
  1002. resultCode=L2RC_Client_RegisterAccount_CD_KEY_ALREADY_USED;
  1003. return true;
  1004. }
  1005. }
  1006. result = pgsql->QueryVariadic(
  1007. "INSERT INTO lobby2.users ("
  1008. "handle, firstname, middlename, lastname,"
  1009. "sex_male, homeaddress1, homeaddress2, homecity, "
  1010. "homezipcode, billingaddress1, billingaddress2, billingcity,"
  1011. "billingzipcode, emailaddress, password, passwordrecoveryquestion,"
  1012. "passwordrecoveryanswer, caption1, caption2, dateOfBirth, binaryData) "
  1013. "VALUES ("
  1014. "%s, %s, %s, %s,"
  1015. "%b, %s, %s, %s,"
  1016. "%s, %s, %s, %s,"
  1017. "%s, %s, %s, %s,"
  1018. "%s, %s, %s, (select now() - %i * interval '1 day'), %a) RETURNING userId_pk",
  1019. userName.C_String(), createAccountParameters.firstName.C_String(), createAccountParameters.middleName.C_String(), createAccountParameters.lastName.C_String(),
  1020. createAccountParameters.sex_male, createAccountParameters.homeAddress1.C_String(), createAccountParameters.homeAddress2.C_String(), createAccountParameters.homeCity.C_String(),
  1021. createAccountParameters.homeZipCode.C_String(), createAccountParameters.billingAddress1.C_String(), createAccountParameters.billingAddress2.C_String(), createAccountParameters.billingCity.C_String(),
  1022. createAccountParameters.billingZipCode.C_String(), createAccountParameters.emailAddress.C_String(), createAccountParameters.password.C_String(), createAccountParameters.passwordRecoveryQuestion.C_String(),
  1023. createAccountParameters.passwordRecoveryAnswer.C_String(), createAccountParameters.caption1.C_String(),createAccountParameters.caption2.C_String(),
  1024. createAccountParameters.ageInDays, createAccountParameters.binaryData->binaryData, createAccountParameters.binaryData->binaryDataLength);
  1025. if (result==0)
  1026. {
  1027. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1028. return true;
  1029. }
  1030. unsigned int userPrimaryKey;
  1031. PostgreSQLInterface::PQGetValueFromBinary(&userPrimaryKey, result, 0, "userId_pk");
  1032. PQclear(result);
  1033. // Assign the cd key, already validated earlier
  1034. if (command->server->GetConfigurationProperties()->accountRegistrationRequiresCDKey)
  1035. {
  1036. PQclear(pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET activationDate=now(),"
  1037. "userId_fk=%i WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", userPrimaryKey, cdKey.C_String(), titleName.C_String()));
  1038. }
  1039. UpdateAccountFromMissingCreationParameters(createAccountParameters, userPrimaryKey, command, pgsql);
  1040. resultCode=L2RC_SUCCESS;
  1041. return true;
  1042. }
  1043. bool RakNet::System_SetEmailAddressValidated_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1044. {
  1045. (void)command;
  1046. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1047. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1048. if (userRow==0)
  1049. {
  1050. resultCode=L2RC_UNKNOWN_USER;
  1051. return true;
  1052. }
  1053. PGresult *result = pgsql->QueryVariadic("UPDATE lobby2.users SET emailAddressValidated=%b WHERE userId_pk=%i", validated, userRow);
  1054. PQclear(result);
  1055. if (result!=0)
  1056. resultCode=L2RC_SUCCESS;
  1057. else
  1058. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1059. return true;
  1060. }
  1061. bool RakNet::Client_ValidateHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1062. {
  1063. (void)command;
  1064. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1065. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1066. if (userRow!=0)
  1067. {
  1068. resultCode=L2RC_Client_ValidateHandle_HANDLE_ALREADY_IN_USE;
  1069. return true;
  1070. }
  1071. if (StringContainsProfanity(userName, pgsql))
  1072. {
  1073. resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
  1074. return true;
  1075. }
  1076. resultCode=L2RC_SUCCESS;
  1077. return true;
  1078. }
  1079. bool RakNet::System_DeleteAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1080. {
  1081. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1082. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1083. if (userRow==0)
  1084. {
  1085. resultCode=L2RC_UNKNOWN_USER;
  1086. return true;
  1087. }
  1088. PGresult *result = pgsql->QueryVariadic(
  1089. "SELECT password from lobby2.users WHERE userId_pk = %i", userRow);
  1090. if (result==0)
  1091. {
  1092. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1093. return true;
  1094. }
  1095. int numRowsReturned = PQntuples(result);
  1096. if (numRowsReturned==0)
  1097. {
  1098. PQclear(result);
  1099. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1100. return true;
  1101. }
  1102. RakNet::RakString passwordFromDB;
  1103. PostgreSQLInterface::PQGetValueFromBinary(&passwordFromDB, result, 0, "password");
  1104. PQclear(result);
  1105. if (passwordFromDB!=password)
  1106. {
  1107. PQclear(result);
  1108. resultCode=L2RC_System_DeleteAccount_INVALID_PASSWORD;
  1109. return true;
  1110. }
  1111. // Notify friends of account deletion
  1112. command->callingUserName=userName;
  1113. command->callerUserId=userRow;
  1114. OutputFriendsNotification(Notification_Friends_StatusChange::FRIEND_ACCOUNT_WAS_DELETED, command, pgsql);
  1115. // Trigger logoff as well
  1116. Client_Logoff *logoffRequest = (Client_Logoff *) command->server->GetMessageFactory()->Alloc(L2MID_Client_Logoff);
  1117. logoffRequest->ServerDBImpl( command, databaseInterface );
  1118. command->server->AddOutputFromThread(logoffRequest, userRow, userName);
  1119. // Delete the account
  1120. result = pgsql->QueryVariadic("DELETE FROM lobby2.users WHERE userId_pk=%i", userRow);
  1121. PQclear(result);
  1122. if (result!=0)
  1123. resultCode=L2RC_SUCCESS;
  1124. else
  1125. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1126. return true;
  1127. }
  1128. bool RakNet::System_PruneAccounts_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1129. {
  1130. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1131. PGresult *result = pgsql->QueryVariadic(
  1132. "SELECT handle from lobby2.users WHERE userId_pk ="
  1133. "(SELECT userId_fk FROM lobby2.userHistory WHERE creationDate < now() - %i * interval '1 day' ORDER by creationDate DESC LIMIT 1 AND triggerid_fk="
  1134. "(SELECT triggerId_pk from lobby2.userHistoryTriggers where description='Login')"
  1135. ") ;", deleteAccountsNotLoggedInDays);
  1136. if (result==0)
  1137. {
  1138. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1139. return true;
  1140. }
  1141. int numRowsReturned = PQntuples(result);
  1142. if (numRowsReturned==0)
  1143. {
  1144. PQclear(result);
  1145. resultCode=L2RC_SUCCESS;
  1146. return true;
  1147. }
  1148. // Delete all accounts where the user has not logged in deleteAccountsNotLoggedInDays
  1149. System_DeleteAccount *deleteAccount = (System_DeleteAccount *) command->server->GetMessageFactory()->Alloc(L2MID_System_DeleteAccount);
  1150. RakNet::RakString userName;
  1151. for (int i=0; i < numRowsReturned; i++)
  1152. {
  1153. PostgreSQLInterface::PQGetValueFromBinary(&userName, result, i, "handle");
  1154. deleteAccount->userName=userName;
  1155. deleteAccount->ServerDBImpl( command, databaseInterface );
  1156. }
  1157. command->server->GetMessageFactory()->Dealloc(deleteAccount);
  1158. PQclear(result);
  1159. return true;
  1160. }
  1161. bool RakNet::Client_GetEmailAddress_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1162. {
  1163. (void)command;
  1164. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1165. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1166. if (userRow==0)
  1167. {
  1168. resultCode=L2RC_UNKNOWN_USER;
  1169. return true;
  1170. }
  1171. PGresult *result = pgsql->QueryVariadic(
  1172. "SELECT emailaddress, emailAddressValidated from lobby2.users WHERE userId_pk = %i", userRow);
  1173. if (result==0)
  1174. {
  1175. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1176. return true;
  1177. }
  1178. PostgreSQLInterface::PQGetValueFromBinary(&emailAddress, result, 0, "emailAddress");
  1179. PostgreSQLInterface::PQGetValueFromBinary(&emailAddressValidated, result, 0, "emailAddressValidated");
  1180. PQclear(result);
  1181. resultCode=L2RC_SUCCESS;
  1182. return true;
  1183. }
  1184. bool RakNet::Client_GetPasswordRecoveryQuestionByHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1185. {
  1186. (void)command;
  1187. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1188. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1189. if (userRow==0)
  1190. {
  1191. resultCode=L2RC_UNKNOWN_USER;
  1192. return true;
  1193. }
  1194. PGresult *result = pgsql->QueryVariadic(
  1195. "SELECT passwordRecoveryQuestion,emailAddress from lobby2.users WHERE userId_pk = %i", userRow);
  1196. if (result==0)
  1197. {
  1198. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1199. return true;
  1200. }
  1201. int numRowsReturned = PQntuples(result);
  1202. if (numRowsReturned==0)
  1203. {
  1204. PQclear(result);
  1205. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1206. return true;
  1207. }
  1208. PostgreSQLInterface::PQGetValueFromBinary(&emailAddress, result, 0, "emailAddress");
  1209. PostgreSQLInterface::PQGetValueFromBinary(&passwordRecoveryQuestion, result, 0, "passwordRecoveryQuestion");
  1210. PQclear(result);
  1211. resultCode=L2RC_SUCCESS;
  1212. return true;
  1213. }
  1214. bool RakNet::Client_GetPasswordByPasswordRecoveryAnswer_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1215. {
  1216. (void)command;
  1217. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1218. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1219. if (userRow==0)
  1220. {
  1221. resultCode=L2RC_UNKNOWN_USER;
  1222. return true;
  1223. }
  1224. PGresult *result = pgsql->QueryVariadic(
  1225. "SELECT password from lobby2.users WHERE lower(passwordrecoveryanswer) = lower(%s) AND userId_pk = %i", passwordRecoveryAnswer.C_String(), userRow);
  1226. if (result==0)
  1227. {
  1228. resultCode=L2RC_Client_GetPasswordByPasswordRecoveryAnswer_BAD_ANSWER;
  1229. return true;
  1230. }
  1231. int numRowsReturned = PQntuples(result);
  1232. if (numRowsReturned==0)
  1233. {
  1234. PQclear(result);
  1235. resultCode=L2RC_Client_GetPasswordByPasswordRecoveryAnswer_BAD_ANSWER;
  1236. return true;
  1237. }
  1238. PostgreSQLInterface::PQGetValueFromBinary(&password, result, 0, "password");
  1239. PQclear(result);
  1240. resultCode=L2RC_SUCCESS;
  1241. return true;
  1242. }
  1243. bool RakNet::Client_ChangeHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1244. {
  1245. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1246. unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
  1247. if (userRow==0)
  1248. {
  1249. resultCode=L2RC_UNKNOWN_USER;
  1250. return true;
  1251. }
  1252. unsigned int userRow2 = GetUserRowFromHandle(newHandle, pgsql);
  1253. if (userRow2!=0)
  1254. {
  1255. resultCode=L2RC_Client_ChangeHandle_NEW_HANDLE_ALREADY_IN_USE;
  1256. return true;
  1257. }
  1258. if (StringContainsProfanity(newHandle, pgsql))
  1259. {
  1260. resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
  1261. return true;
  1262. }
  1263. if (requiresPasswordToChangeHandle)
  1264. {
  1265. PGresult *result = pgsql->QueryVariadic(
  1266. "SELECT password from lobby2.users WHERE userId_pk = %i", userRow);
  1267. if (result==0)
  1268. {
  1269. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1270. return true;
  1271. }
  1272. int numRowsReturned = PQntuples(result);
  1273. if (numRowsReturned==0)
  1274. {
  1275. PQclear(result);
  1276. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1277. return true;
  1278. }
  1279. RakNet::RakString passwordFromDB;
  1280. PostgreSQLInterface::PQGetValueFromBinary(&passwordFromDB, result, 0, "password");
  1281. PQclear(result);
  1282. if (passwordFromDB!=password)
  1283. {
  1284. PQclear(result);
  1285. resultCode=L2RC_Client_ChangeHandle_INVALID_PASSWORD;
  1286. return true;
  1287. }
  1288. }
  1289. PGresult *result = pgsql->QueryVariadic(
  1290. "UPDATE lobby2.users SET handle=%s WHERE userId_pk=%i", newHandle.C_String(), userRow);
  1291. PQclear(result);
  1292. // Tell all friends and clanMembers
  1293. DataStructures::List<unsigned int> output;
  1294. GetFriendIDs(command->callerUserId, false, pgsql, output);
  1295. GetClanMateIDs(command->callerUserId, false, pgsql, output);
  1296. unsigned int i;
  1297. for (i=0; i < output.Size(); i++)
  1298. {
  1299. Notification_User_ChangedHandle *notification = (Notification_User_ChangedHandle *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_User_ChangedHandle);
  1300. notification->oldHandle=userName;
  1301. notification->newHandle=newHandle;
  1302. notification->resultCode=L2RC_SUCCESS;
  1303. command->server->AddOutputFromThread(notification, output[i], "");
  1304. }
  1305. command->callingUserName=newHandle;
  1306. command->server->OnChangeHandle(command, true);
  1307. resultCode=L2RC_SUCCESS;
  1308. return true;
  1309. }
  1310. bool RakNet::Client_UpdateAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1311. {
  1312. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1313. PGresult *result=0;
  1314. if (CreateAccountParametersFailed(createAccountParameters, resultCode, command, pgsql))
  1315. return true;
  1316. // PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
  1317. result = pgsql->QueryVariadic(
  1318. "UPDATE lobby2.users SET "
  1319. "firstname=%s, middlename=%s, lastname=%s,"
  1320. "sex_male=%b, homeaddress1=%s, homeaddress2=%s, homecity=%s, "
  1321. "homezipcode=%s, billingaddress1=%s, billingaddress2=%s, billingcity=%s,"
  1322. "billingzipcode=%s, emailaddress=%s, password=%s, passwordrecoveryquestion=%s,"
  1323. "passwordrecoveryanswer=%s, caption1=%s, caption2=%s, dateOfBirth=(select now() - %i * interval '1 day'), binaryData=%a "
  1324. "WHERE userId_pk = %i",
  1325. createAccountParameters.firstName.C_String(), createAccountParameters.middleName.C_String(), createAccountParameters.lastName.C_String(),
  1326. createAccountParameters.sex_male, createAccountParameters.homeAddress1.C_String(), createAccountParameters.homeAddress2.C_String(), createAccountParameters.homeCity.C_String(),
  1327. createAccountParameters.homeZipCode.C_String(), createAccountParameters.billingAddress1.C_String(), createAccountParameters.billingAddress2.C_String(), createAccountParameters.billingCity.C_String(),
  1328. createAccountParameters.billingZipCode.C_String(), createAccountParameters.emailAddress.C_String(), createAccountParameters.password.C_String(), createAccountParameters.passwordRecoveryQuestion.C_String(),
  1329. createAccountParameters.passwordRecoveryAnswer.C_String(), createAccountParameters.caption1.C_String(),createAccountParameters.caption2.C_String(),
  1330. createAccountParameters.ageInDays, createAccountParameters.binaryData->binaryData, createAccountParameters.binaryData->binaryDataLength,
  1331. command->callerUserId);
  1332. if (result==0)
  1333. {
  1334. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1335. return true;
  1336. }
  1337. UpdateAccountFromMissingCreationParameters(createAccountParameters, command->callerUserId, command, pgsql);
  1338. resultCode=L2RC_SUCCESS;
  1339. return true;
  1340. }
  1341. bool RakNet::Client_GetAccountDetails_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1342. {
  1343. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1344. PGresult *result=0;
  1345. result = pgsql->QueryVariadic(
  1346. "SELECT firstname, middlename, lastname, (SELECT race_text FROM lobby2.race WHERE "
  1347. "race_id=(SELECT raceid_fk FROM lobby2.users WHERE userId_pk = %i )) as race, sex_male, homeaddress1, "
  1348. "homeaddress2, homecity,(SELECT state_name FROM lobby2.state WHERE state_id=(SELECT homestateid_fk FROM lobby2.users WHERE userId_pk = %i)) as homeState,(SELECT country_name FROM lobby2.country "
  1349. "WHERE country_id=(SELECT homeCountryid_fk FROM lobby2.users WHERE userId_pk = %i)) as homeCountry,homezipcode, billingaddress1, billingaddress2, billingcity,"
  1350. "(SELECT state_name FROM lobby2.state WHERE state_id=(SELECT billingstateid_fk FROM lobby2.users WHERE userId_pk = %i)) as billingState,(SELECT country_name FROM "
  1351. "lobby2.country WHERE country_id=(SELECT billingCountryid_fk FROM lobby2.users WHERE userId_pk = %i)) as billingCountry,billingzipcode, emailaddress, password, passwordrecoveryquestion,"
  1352. "passwordrecoveryanswer, caption1, caption2, (SELECT (EXTRACT(EPOCH FROM now()) - extract(epoch from dateofbirth))/(24*60*60) AS days_old "
  1353. "FROM lobby2.users WHERE userId_pk = %i) as ageInDays, binaryData FROM lobby2.users WHERE userId_pk = %i"
  1354. ,command->callerUserId, command->callerUserId,command->callerUserId, command->callerUserId,command->callerUserId
  1355. ,command->callerUserId, command->callerUserId);
  1356. if (result==0)
  1357. {
  1358. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1359. return true;
  1360. }
  1361. int numRowsReturned = PQntuples(result);
  1362. if (numRowsReturned==0)
  1363. {
  1364. PQclear(result);
  1365. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1366. return true;
  1367. }
  1368. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.firstName, result, 0, "firstname");
  1369. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.middleName, result, 0, "middleName");
  1370. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.lastName, result, 0, "lastName");
  1371. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.race, result, 0, "race");
  1372. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.sex_male, result, 0, "sex_male");
  1373. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeAddress1, result, 0, "homeAddress1");
  1374. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeAddress2, result, 0, "homeAddress2");
  1375. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeCity, result, 0, "homeCity");
  1376. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeState, result, 0, "homeState");
  1377. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeCountry, result, 0, "homeCountry");
  1378. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeZipCode, result, 0, "homeZipCode");
  1379. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingAddress1, result, 0, "billingAddress1");
  1380. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingAddress2, result, 0, "billingAddress2");
  1381. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingCity, result, 0, "billingCity");
  1382. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingState, result, 0, "billingState");
  1383. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingCountry, result, 0, "billingCountry");
  1384. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingZipCode, result, 0, "billingZipCode");
  1385. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.emailAddress, result, 0, "emailAddress");
  1386. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.password, result, 0, "password");
  1387. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.passwordRecoveryQuestion, result, 0, "passwordRecoveryQuestion");
  1388. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.passwordRecoveryAnswer, result, 0, "passwordRecoveryAnswer");
  1389. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.caption1, result, 0, "caption1");
  1390. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.caption2, result, 0, "caption2");
  1391. double d;
  1392. PostgreSQLInterface::PQGetValueFromBinary(&d, result, 0, "ageInDays");
  1393. createAccountParameters.ageInDays=(unsigned int) d;
  1394. // PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.ageInDays, result, 0, "ageInDays");
  1395. PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.binaryData->binaryData, &createAccountParameters.binaryData->binaryDataLength, result, 0, "binaryData");
  1396. PQclear(result);
  1397. resultCode=L2RC_SUCCESS;
  1398. return true;
  1399. }
  1400. bool RakNet::Client_StartIgnore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1401. {
  1402. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1403. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1404. if (targetUserId==0)
  1405. {
  1406. resultCode=L2RC_Client_StartIgnore_UNKNOWN_TARGET_HANDLE;
  1407. return true;
  1408. }
  1409. if (targetUserId==command->callerUserId)
  1410. {
  1411. resultCode=L2RC_Client_StartIgnore_CANNOT_PERFORM_ON_SELF;
  1412. return true;
  1413. }
  1414. PGresult *result = pgsql->QueryVariadic(
  1415. "INSERT INTO lobby2.ignore (userMe_fk, userOther_fk) VALUES (%i, %i)", command->callerUserId, targetUserId);
  1416. if (result==0)
  1417. {
  1418. resultCode=L2RC_Client_StartIgnore_ALREADY_IGNORED;
  1419. return true;
  1420. }
  1421. PQclear(result);
  1422. Notification_Client_IgnoreStatus *notification = (Notification_Client_IgnoreStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Client_IgnoreStatus);
  1423. notification->otherHandle=command->callingUserName;
  1424. notification->nowIgnored=true;
  1425. notification->resultCode=L2RC_SUCCESS;
  1426. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1427. resultCode=L2RC_SUCCESS;
  1428. return true;
  1429. }
  1430. bool RakNet::Client_GetIgnoreList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1431. {
  1432. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1433. PGresult *result = pgsql->QueryVariadic("SELECT handle FROM lobby2.users WHERE userId_pk="
  1434. "(SELECT userOther_fk FROM lobby2.ignore WHERE userMe_fk=%i);", command->callerUserId);
  1435. if (result==0)
  1436. {
  1437. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1438. return true;
  1439. }
  1440. RakNet::RakString handle;
  1441. ignoredHandles.Clear(false, _FILE_AND_LINE_);
  1442. int numRowsReturned = PQntuples(result);
  1443. int i;
  1444. for (i=0; i < numRowsReturned; i++)
  1445. {
  1446. PostgreSQLInterface::PQGetValueFromBinary(&handle, result, i, "handle");
  1447. ignoredHandles.Insert(handle, _FILE_AND_LINE_ );
  1448. }
  1449. PQclear(result);
  1450. resultCode=L2RC_SUCCESS;
  1451. return true;
  1452. }
  1453. bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Write( Lobby2ServerCommand *command, void *databaseInterface )
  1454. {
  1455. PGresult *result=0;
  1456. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1457. result = pgsql->QueryVariadic(
  1458. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g);",
  1459. titleName.C_String(), slotIndex, command->callerUserId, inputValue);
  1460. if (result==0)
  1461. {
  1462. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1463. return true;
  1464. }
  1465. /*
  1466. switch (addConditionForOperation)
  1467. {
  1468. case PTISC_EQUAL:
  1469. result = pgsql->QueryVariadic(
  1470. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value=%g RETURNING value;",
  1471. titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
  1472. break;
  1473. case PTISC_NOT_EQUAL:
  1474. result = pgsql->QueryVariadic(
  1475. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) RETURNING value;",
  1476. titleName.C_String(), slotIndex, command->callerUserId, inputValue);
  1477. break;
  1478. case PTISC_GREATER_THAN:
  1479. result = pgsql->QueryVariadic(
  1480. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value<%g RETURNING value;",
  1481. titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
  1482. break;
  1483. case PTISC_GREATER_OR_EQUAL:
  1484. result = pgsql->QueryVariadic(
  1485. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value<=%g RETURNING value;",
  1486. titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
  1487. break;
  1488. case PTISC_LESS_THAN:
  1489. result = pgsql->QueryVariadic(
  1490. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value>%g RETURNING value;",
  1491. titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
  1492. break;
  1493. case PTISC_LESS_OR_EQUAL:
  1494. result = pgsql->QueryVariadic(
  1495. "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value>=%g RETURNING value;",
  1496. titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
  1497. break;
  1498. }
  1499. int numRowsReturned = PQntuples(result);
  1500. if (numRowsReturned==0)
  1501. {
  1502. PQclear(result);
  1503. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1504. return true;
  1505. }
  1506. PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
  1507. */
  1508. PQclear(result);
  1509. resultCode=L2RC_SUCCESS;
  1510. return true;
  1511. }
  1512. bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Read( Lobby2ServerCommand *command, void *databaseInterface )
  1513. {
  1514. PGresult *result=0;
  1515. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1516. result = pgsql->QueryVariadic("SELECT value FROM lobby2.perTitlePerUserIntegerStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i", titleName.C_String(), slotIndex, command->callerUserId);
  1517. int numRowsReturned = PQntuples(result);
  1518. if (numRowsReturned==0)
  1519. {
  1520. PQclear(result);
  1521. resultCode=L2RC_Client_PerTitleIntegerStorage_ROW_EMPTY;
  1522. return true;
  1523. }
  1524. PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
  1525. PQclear(result);
  1526. resultCode=L2RC_SUCCESS;
  1527. return true;
  1528. }
  1529. bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Delete( Lobby2ServerCommand *command, void *databaseInterface )
  1530. {
  1531. PGresult *result=0;
  1532. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1533. result = pgsql->QueryVariadic("DELETE FROM lobby2.perTitlePerUserIntegerStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i", titleName.C_String(), slotIndex, command->callerUserId);
  1534. PQclear(result);
  1535. resultCode=L2RC_SUCCESS;
  1536. return true;
  1537. }
  1538. bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Add( Lobby2ServerCommand *command, void *databaseInterface )
  1539. {
  1540. // In MySQL I think you can do this:
  1541. // INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,value) VALUES ('tn',1,2) ON DUPLICATE KEY UPDATE titleName_fk='tn2', slotIndex=2, value=3;
  1542. // But not in PostgreSQL
  1543. Read(command, databaseInterface);
  1544. if (resultCode==L2RC_Client_PerTitleIntegerStorage_ROW_EMPTY)
  1545. return Write(command, databaseInterface);
  1546. outputValue+=inputValue;
  1547. PGresult *result=0;
  1548. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1549. switch (addConditionForOperation)
  1550. {
  1551. case PTISC_EQUAL:
  1552. result = pgsql->QueryVariadic(
  1553. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value=%g RETURNING value;",
  1554. titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
  1555. break;
  1556. case PTISC_NOT_EQUAL:
  1557. result = pgsql->QueryVariadic(
  1558. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value!=%g RETURNING value;",
  1559. titleName.C_String(), slotIndex, command->callerUserId, outputValue);
  1560. break;
  1561. case PTISC_GREATER_THAN:
  1562. result = pgsql->QueryVariadic(
  1563. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value<%g RETURNING value;",
  1564. titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
  1565. break;
  1566. case PTISC_GREATER_OR_EQUAL:
  1567. result = pgsql->QueryVariadic(
  1568. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value<=%g RETURNING value;",
  1569. titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
  1570. break;
  1571. case PTISC_LESS_THAN:
  1572. result = pgsql->QueryVariadic(
  1573. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value>%g RETURNING value;",
  1574. titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
  1575. break;
  1576. case PTISC_LESS_OR_EQUAL:
  1577. result = pgsql->QueryVariadic(
  1578. "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value>=%g RETURNING value;",
  1579. titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
  1580. break;
  1581. }
  1582. int numRowsReturned = PQntuples(result);
  1583. if (numRowsReturned==0)
  1584. {
  1585. PQclear(result);
  1586. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1587. return true;
  1588. }
  1589. PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
  1590. PQclear(result);
  1591. resultCode=L2RC_SUCCESS;
  1592. return true;
  1593. }
  1594. bool RakNet::Client_PerTitleIntegerStorage_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1595. {
  1596. (void)command;
  1597. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1598. // Verify title name
  1599. if (IsTitleInUse(titleName, pgsql)==false)
  1600. {
  1601. resultCode=L2RC_Client_PerTitleIntegerStorage_TITLE_NOT_IN_USE;
  1602. return true;
  1603. }
  1604. switch (operationToPerform)
  1605. {
  1606. case PTISO_WRITE:
  1607. return Write(command,databaseInterface);
  1608. break;
  1609. case PTISO_READ:
  1610. return Read(command,databaseInterface);
  1611. break;
  1612. case PTISO_DELETE:
  1613. return Delete(command,databaseInterface);
  1614. break;
  1615. case PTISO_ADD:
  1616. return Add(command,databaseInterface);
  1617. break;
  1618. }
  1619. return true;
  1620. }
  1621. bool RakNet::Client_PerTitleBinaryStorage_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1622. {
  1623. (void)command;
  1624. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1625. PGresult *result=0;
  1626. // Verify title name
  1627. if (IsTitleInUse(titleName, pgsql)==false)
  1628. {
  1629. resultCode=L2RC_Client_PerTitleBinaryStorage_TITLE_NOT_IN_USE;
  1630. return true;
  1631. }
  1632. int numRowsReturned;
  1633. switch (operationToPerform)
  1634. {
  1635. case PTISO_WRITE:
  1636. result = pgsql->QueryVariadic(
  1637. "INSERT INTO lobby2.perTitlePerUserBinaryStorage (titleName_fk,slotIndex,userId_fk,binaryData) VALUES (%s,%i,%i,%a);",
  1638. titleName.C_String(), slotIndex, command->callerUserId, binaryData->binaryData,
  1639. binaryData->binaryDataLength);
  1640. if (result==0)
  1641. {
  1642. resultCode=L2RC_GENERAL_ERROR;
  1643. return true;
  1644. }
  1645. break;
  1646. case PTISO_READ:
  1647. result = pgsql->QueryVariadic(
  1648. "SELECT binaryData FROM lobby2.perTitlePerUserBinaryStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i;",
  1649. titleName.C_String(), slotIndex, command->callerUserId);
  1650. if (result==0)
  1651. {
  1652. resultCode=L2RC_GENERAL_ERROR;
  1653. return true;
  1654. }
  1655. numRowsReturned = PQntuples(result);
  1656. if (numRowsReturned==0)
  1657. {
  1658. resultCode=L2RC_Client_PerTitleBinaryStorage_ROW_EMPTY;
  1659. return true;
  1660. }
  1661. PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
  1662. break;
  1663. case PTISO_DELETE:
  1664. result = pgsql->QueryVariadic("DELETE FROM lobby2.perTitlePerUserBinaryStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i;",
  1665. titleName.C_String(), slotIndex, command->callerUserId);
  1666. break;
  1667. }
  1668. PQclear(result);
  1669. resultCode=L2RC_SUCCESS;
  1670. return true;
  1671. }
  1672. bool RakNet::Client_SetPresence_PGSQL::ServerPreDBMemoryImpl( Lobby2Server *server, RakString userHandle )
  1673. {
  1674. server->SetPresence( presence, userHandle );
  1675. resultCode=L2RC_SUCCESS;
  1676. return true;
  1677. }
  1678. bool RakNet::Client_GetPresence_PGSQL::ServerPreDBMemoryImpl( Lobby2Server *server, RakString userHandle )
  1679. {
  1680. server->GetPresence( presence, userHandle );
  1681. resultCode=L2RC_SUCCESS;
  1682. return true;
  1683. }
  1684. bool RakNet::Client_StopIgnore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1685. {
  1686. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1687. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1688. if (targetUserId==0)
  1689. {
  1690. resultCode=L2RC_Client_StopIgnore_UNKNOWN_TARGET_HANDLE;
  1691. return true;
  1692. }
  1693. if (targetUserId==command->callerUserId)
  1694. {
  1695. resultCode=L2RC_Client_StopIgnore_CANNOT_PERFORM_ON_SELF;
  1696. return true;
  1697. }
  1698. PGresult *result = pgsql->QueryVariadic(
  1699. "DELETE FROM lobby2.ignore WHERE userMe_fk=%i AND userOther_fk=%i", command->callerUserId, targetUserId);
  1700. PQclear(result);
  1701. Notification_Client_IgnoreStatus *notification = (Notification_Client_IgnoreStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Client_IgnoreStatus);
  1702. notification->otherHandle=command->callingUserName;
  1703. notification->nowIgnored=false;
  1704. notification->resultCode=L2RC_SUCCESS;
  1705. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1706. resultCode=L2RC_SUCCESS;
  1707. return true;
  1708. }
  1709. bool RakNet::Friends_SendInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1710. {
  1711. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1712. PGresult *result=0;
  1713. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1714. if (targetUserId==0)
  1715. {
  1716. resultCode=L2RC_Friends_SendInvite_UNKNOWN_TARGET_HANDLE;
  1717. return true;
  1718. }
  1719. if (targetUserId==command->callerUserId)
  1720. {
  1721. resultCode=L2RC_Friends_SendInvite_CANNOT_PERFORM_ON_SELF;
  1722. return true;
  1723. }
  1724. // Don't do if already in friends table (already friends, or already has an invite)
  1725. result = pgsql->QueryVariadic(
  1726. "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
  1727. "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
  1728. , command->callerUserId, targetUserId);
  1729. if (result==0)
  1730. {
  1731. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1732. return true;
  1733. }
  1734. int numRowsReturned = PQntuples(result);
  1735. if (numRowsReturned!=0)
  1736. {
  1737. RakNet::RakString description;
  1738. PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
  1739. if (description=="sentInvite")
  1740. resultCode=L2RC_Friends_SendInvite_ALREADY_SENT_INVITE;
  1741. else if (description=="isFriends")
  1742. resultCode=L2RC_Friends_SendInvite_ALREADY_FRIENDS;
  1743. else
  1744. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1745. PQclear(result);
  1746. return true;
  1747. }
  1748. PQclear(result);
  1749. // Add friend invite
  1750. result = pgsql->QueryVariadic(
  1751. "INSERT INTO lobby2.friends (userOne_fk, userTwo_fk, actionId_fk) VALUES "
  1752. "(%i, %i, (SELECT actionId_pk FROM lobby2.friendActions WHERE description='sentInvite'));"
  1753. , command->callerUserId, targetUserId);
  1754. if (result==0)
  1755. {
  1756. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1757. return true;
  1758. }
  1759. PQclear(result);
  1760. // Notify by email
  1761. SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_SendInvite", pgsql);
  1762. // Tell the other system the invitation was sent
  1763. Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
  1764. RakAssert(command->callingUserName.IsEmpty()==false);
  1765. notification->otherHandle=command->callingUserName;
  1766. notification->subject=subject;
  1767. notification->body=body;
  1768. notification->op=Notification_Friends_StatusChange::GOT_INVITATION_TO_BE_FRIENDS;
  1769. notification->resultCode=L2RC_SUCCESS;
  1770. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1771. resultCode=L2RC_SUCCESS;
  1772. return true;
  1773. }
  1774. bool RakNet::Friends_AcceptInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1775. {
  1776. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1777. PGresult *result=0;
  1778. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1779. if (targetUserId==0)
  1780. {
  1781. resultCode=L2RC_Friends_AcceptInvite_UNKNOWN_TARGET_HANDLE;
  1782. return true;
  1783. }
  1784. if (targetUserId==command->callerUserId)
  1785. {
  1786. resultCode=L2RC_Friends_AcceptInvite_CANNOT_PERFORM_ON_SELF;
  1787. return true;
  1788. }
  1789. // Make sure we have an invite from the other user
  1790. result = pgsql->QueryVariadic(
  1791. "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
  1792. "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
  1793. , targetUserId, command->callerUserId );
  1794. if (result==0)
  1795. {
  1796. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1797. return true;
  1798. }
  1799. int numRowsReturned = PQntuples(result);
  1800. if (numRowsReturned!=0)
  1801. {
  1802. RakNet::RakString description;
  1803. PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
  1804. PQclear(result);
  1805. if (description!=RakNet::RakString("sentInvite"))
  1806. {
  1807. resultCode=L2RC_Friends_AcceptInvite_NO_INVITE;
  1808. return true;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. PQclear(result);
  1814. resultCode=L2RC_Friends_AcceptInvite_NO_INVITE;
  1815. return true;
  1816. }
  1817. // Change from invited to friends, insert twice
  1818. result = pgsql->QueryVariadic(
  1819. "UPDATE lobby2.friends SET actionId_fk=(SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends') WHERE userOne_fk=%i AND userTwo_fk=%i;"
  1820. , targetUserId, command->callerUserId );
  1821. RakAssert(result);
  1822. PQclear(result);
  1823. // Delete any existing invites, etc. if there are any
  1824. result = pgsql->QueryVariadic(
  1825. "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
  1826. , command->callerUserId, targetUserId );
  1827. PQclear(result);
  1828. // Insert as a friend
  1829. result = pgsql->QueryVariadic(
  1830. "INSERT INTO lobby2.friends (userOne_fk, userTwo_fk, actionId_fk) VALUES (%i, %i, (SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends'));"
  1831. ,command->callerUserId, targetUserId);
  1832. RakAssert(result);
  1833. PQclear(result);
  1834. SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_AcceptInvite", (PostgreSQLInterface *) databaseInterface);
  1835. // Tell the other system the invitation was accepted
  1836. Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
  1837. RakAssert(command->callingUserName.IsEmpty()==false);
  1838. notification->otherHandle=command->callingUserName;
  1839. notification->subject=subject;
  1840. notification->body=body;
  1841. notification->op=Notification_Friends_StatusChange::THEY_ACCEPTED_OUR_INVITATION_TO_BE_FRIENDS;
  1842. notification->resultCode=L2RC_SUCCESS;
  1843. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1844. resultCode=L2RC_SUCCESS;
  1845. return true;
  1846. }
  1847. void RakNet::Friends_AcceptInvite_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
  1848. {
  1849. (void)userHandle;
  1850. server->GetPresence(presence,targetHandle);
  1851. }
  1852. bool RakNet::Friends_RejectInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1853. {
  1854. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1855. PGresult *result=0;
  1856. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1857. if (targetUserId==0)
  1858. {
  1859. resultCode=L2RC_Friends_RejectInvite_UNKNOWN_TARGET_HANDLE;
  1860. return true;
  1861. }
  1862. if (targetUserId==command->callerUserId)
  1863. {
  1864. resultCode=L2RC_Friends_RejectInvite_CANNOT_PERFORM_ON_SELF;
  1865. return true;
  1866. }
  1867. // Make sure we have an invite from the other user
  1868. result = pgsql->QueryVariadic(
  1869. "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
  1870. "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
  1871. , targetUserId, command->callerUserId );
  1872. if (result==0)
  1873. {
  1874. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1875. return true;
  1876. }
  1877. int numRowsReturned = PQntuples(result);
  1878. if (numRowsReturned!=0)
  1879. {
  1880. RakNet::RakString description;
  1881. PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
  1882. PQclear(result);
  1883. if (description!=RakNet::RakString("sentInvite"))
  1884. {
  1885. resultCode=L2RC_Friends_RejectInvite_NO_INVITE;
  1886. return true;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. PQclear(result);
  1892. resultCode=L2RC_Friends_RejectInvite_NO_INVITE;
  1893. return true;
  1894. }
  1895. // Delete friend invite (both ways)
  1896. result = pgsql->QueryVariadic(
  1897. "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
  1898. , targetUserId, command->callerUserId );
  1899. PQclear(result);
  1900. result = pgsql->QueryVariadic(
  1901. "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
  1902. , command->callerUserId, targetUserId );
  1903. PQclear(result);
  1904. SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_RejectInvite", (PostgreSQLInterface *) databaseInterface);
  1905. // Tell the other system the invitation was rejected
  1906. Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
  1907. RakAssert(command->callingUserName.IsEmpty()==false);
  1908. notification->otherHandle=command->callingUserName;
  1909. notification->subject=subject;
  1910. notification->body=body;
  1911. notification->op=Notification_Friends_StatusChange::THEY_REJECTED_OUR_INVITATION_TO_BE_FRIENDS;
  1912. notification->resultCode=L2RC_SUCCESS;
  1913. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1914. resultCode=L2RC_SUCCESS;
  1915. return true;
  1916. }
  1917. bool RakNet::Friends_GetInvites_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1918. {
  1919. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1920. GetFriendInfosByStatus(command->callerUserId, "sentInvite", pgsql, invitesSent, true);
  1921. GetFriendInfosByStatus(command->callerUserId, "sentInvite", pgsql, invitesReceived, false);
  1922. resultCode=L2RC_SUCCESS;
  1923. return true;
  1924. }
  1925. void RakNet::Friends_GetInvites_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
  1926. {
  1927. (void)userHandle;
  1928. for (unsigned int i=0; i < invitesSent.Size(); i++)
  1929. server->GetUserOnlineStatus(invitesSent[i].usernameAndStatus);
  1930. for (unsigned int i=0; i < invitesReceived.Size(); i++)
  1931. server->GetUserOnlineStatus(invitesReceived[i].usernameAndStatus);
  1932. }
  1933. bool RakNet::Friends_GetFriends_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1934. {
  1935. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1936. GetFriendInfosByStatus(command->callerUserId, "isFriends", pgsql, myFriends, true);
  1937. resultCode=L2RC_SUCCESS;
  1938. return true;
  1939. }
  1940. void RakNet::Friends_GetFriends_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
  1941. {
  1942. (void)userHandle;
  1943. for (unsigned int i=0; i < myFriends.Size(); i++)
  1944. server->GetUserOnlineStatus(myFriends[i].usernameAndStatus);
  1945. }
  1946. bool RakNet::Friends_Remove_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1947. {
  1948. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1949. PGresult *result=0;
  1950. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1951. if (targetUserId==0)
  1952. {
  1953. resultCode=L2RC_Friends_Remove_UNKNOWN_TARGET_HANDLE;
  1954. return true;
  1955. }
  1956. if (targetUserId==command->callerUserId)
  1957. {
  1958. resultCode=L2RC_Friends_Remove_CANNOT_PERFORM_ON_SELF;
  1959. return true;
  1960. }
  1961. result = pgsql->QueryVariadic(
  1962. "SELECT userOne_fk FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
  1963. , command->callerUserId, targetUserId );
  1964. if (result==0)
  1965. {
  1966. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  1967. return true;
  1968. }
  1969. int numRowsReturned = PQntuples(result);
  1970. PQclear(result);
  1971. if (numRowsReturned==0)
  1972. {
  1973. resultCode=L2RC_Friends_Remove_NOT_FRIENDS;
  1974. return true;
  1975. }
  1976. // Bidirectional delete
  1977. result = pgsql->QueryVariadic("DELETE FROM lobby2.friends WHERE (userOne_fk=%i AND userTwo_fk=%i) OR (userOne_fk=%i AND userTwo_fk=%i)",
  1978. command->callerUserId, targetUserId, targetUserId, command->callerUserId);
  1979. RakAssert(result);
  1980. PQclear(result);
  1981. SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_Remove", (PostgreSQLInterface *) databaseInterface);
  1982. Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
  1983. RakAssert(command->callingUserName.IsEmpty()==false);
  1984. notification->otherHandle=command->callingUserName;
  1985. notification->subject=subject;
  1986. notification->body=body;
  1987. notification->op=Notification_Friends_StatusChange::YOU_WERE_REMOVED_AS_A_FRIEND;
  1988. notification->resultCode=L2RC_SUCCESS;
  1989. command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
  1990. resultCode=L2RC_SUCCESS;
  1991. return true;
  1992. }
  1993. bool RakNet::BookmarkedUsers_Add_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  1994. {
  1995. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  1996. PGresult *result=0;
  1997. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  1998. if (targetUserId==0)
  1999. {
  2000. resultCode=L2RC_BookmarkedUsers_Add_UNKNOWN_TARGET_HANDLE;
  2001. return true;
  2002. }
  2003. if (targetUserId==command->callerUserId)
  2004. {
  2005. resultCode=L2RC_BookmarkedUsers_Add_CANNOT_PERFORM_ON_SELF;
  2006. return true;
  2007. }
  2008. result = pgsql->QueryVariadic(
  2009. "INSERT INTO lobby2.bookmarkedUsers (userMe_fk, userOther_fk, type, description) VALUES (%i, %i, %i, %s)",
  2010. command->callerUserId, targetUserId, type, description.C_String() );
  2011. if (result==0)
  2012. {
  2013. resultCode=L2RC_BookmarkedUsers_Add_ALREADY_BOOKMARKED;
  2014. return true;
  2015. }
  2016. PQclear(result);
  2017. resultCode=L2RC_SUCCESS;
  2018. return true;
  2019. }
  2020. bool RakNet::BookmarkedUsers_Remove_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2021. {
  2022. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2023. PGresult *result=0;
  2024. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2025. if (targetUserId==0)
  2026. {
  2027. resultCode=L2RC_BookmarkedUsers_Remove_UNKNOWN_TARGET_HANDLE;
  2028. return true;
  2029. }
  2030. if (targetUserId==command->callerUserId)
  2031. {
  2032. resultCode=L2RC_BookmarkedUsers_Remove_CANNOT_PERFORM_ON_SELF;
  2033. return true;
  2034. }
  2035. result = pgsql->QueryVariadic(
  2036. "DELETE FROM lobby2.bookmarkedUsers WHERE userOther_fk=%i AND type=%i",
  2037. targetUserId, type);
  2038. if (result==0)
  2039. {
  2040. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2041. return true;
  2042. }
  2043. PQclear(result);
  2044. resultCode=L2RC_SUCCESS;
  2045. return true;
  2046. }
  2047. bool RakNet::BookmarkedUsers_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2048. {
  2049. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2050. PGresult *result=0;
  2051. result = pgsql->QueryVariadic(
  2052. "SELECT (SELECT handle FROM lobby2.users where userId_pk=userOther_fk) as handle, *"
  2053. "FROM (SELECT userOther_fk, type, description, creationDate from lobby2.bookmarkedUsers WHERE userMe_fk=%i) as bm ORDER BY type, creationDate ASC;",
  2054. command->callerUserId);
  2055. if (result==0)
  2056. {
  2057. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2058. return true;
  2059. }
  2060. int numRowsReturned = PQntuples(result);
  2061. int i;
  2062. for (i=0; i < numRowsReturned; i++)
  2063. {
  2064. BookmarkedUser bm;
  2065. PostgreSQLInterface::PQGetValueFromBinary(&bm.targetHandle, result, i, "handle");
  2066. PostgreSQLInterface::PQGetValueFromBinary(&bm.type, result, i, "type");
  2067. PostgreSQLInterface::PQGetValueFromBinary(&bm.description, result, i, "description");
  2068. PostgreSQLInterface::PQGetValueFromBinary(&bm.dateWhenAdded, result, i, "creationDate");
  2069. bookmarkedUsers.Insert(bm, _FILE_AND_LINE_ );
  2070. }
  2071. PQclear(result);
  2072. resultCode=L2RC_SUCCESS;
  2073. return true;
  2074. }
  2075. bool RakNet::Emails_Send_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2076. {
  2077. SendEmail(recipients, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, status, "Emails_Send", (PostgreSQLInterface *) databaseInterface);
  2078. resultCode=L2RC_SUCCESS;
  2079. return true;
  2080. }
  2081. bool RakNet::Emails_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2082. {
  2083. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2084. PGresult *result;
  2085. if (unreadEmailsOnly==true)
  2086. {
  2087. if (emailIdsOnly)
  2088. {
  2089. result = pgsql->QueryVariadic(
  2090. "SELECT tbl2.emailid_fk from lobby2.users, ( "
  2091. "SELECT tbl1.*, lobby2.emails.creationDate FROM "
  2092. "(SELECT emailId_fk, userMe_fk, userOther_fk, isDeleted, wasRead FROM lobby2.emailTargets) as tbl1, lobby2.emails "
  2093. "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE AND tbl1.wasRead=FALSE "
  2094. ") as tbl2 "
  2095. "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
  2096. , command->callerUserId);
  2097. }
  2098. else
  2099. {
  2100. result = pgsql->QueryVariadic(
  2101. "SELECT handle, tbl2.* from lobby2.users, ("
  2102. "SELECT tbl1.*, lobby2.emails.subject, lobby2.emails.body, lobby2.emails.binaryData, lobby2.emails.creationDate FROM"
  2103. "(SELECT emailId_fk, emailTarget_pk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
  2104. "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE AND tbl1.wasRead=FALSE"
  2105. ") as tbl2 "
  2106. "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
  2107. , command->callerUserId);
  2108. }
  2109. }
  2110. else
  2111. {
  2112. if (emailIdsOnly)
  2113. {
  2114. result = pgsql->QueryVariadic(
  2115. "SELECT tbl2.emailid_fk from lobby2.users, ( "
  2116. "SELECT tbl1.*, lobby2.emails.creationDate FROM "
  2117. "(SELECT emailId_fk, userMe_fk, userOther_fk, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
  2118. "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE "
  2119. ") as tbl2 "
  2120. "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
  2121. , command->callerUserId);
  2122. }
  2123. else
  2124. {
  2125. result = pgsql->QueryVariadic(
  2126. "SELECT handle, tbl2.* from lobby2.users, ("
  2127. "SELECT tbl1.*, lobby2.emails.subject, lobby2.emails.body, lobby2.emails.binaryData, lobby2.emails.creationDate FROM"
  2128. "(SELECT emailId_fk, emailTarget_pk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
  2129. "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE"
  2130. ") as tbl2 "
  2131. "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
  2132. , command->callerUserId);
  2133. }
  2134. }
  2135. if (result==0)
  2136. {
  2137. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2138. return true;
  2139. }
  2140. int numRowsReturned = PQntuples(result);
  2141. int i;
  2142. for (i=0; i < numRowsReturned; i++)
  2143. {
  2144. EmailResult emailResult;
  2145. RakNet::RakString otherHandle;
  2146. RakNet::RakString myHandle = command->callingUserName;
  2147. // 4/6/2011 emailTarget_pk is correct, this is used by Emails_Delete and Emails_SetStatus
  2148. // 11/4/2010 - I think this was a copy/paste error
  2149. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.emailID, result, i, "emailTarget_pk");
  2150. // PostgreSQLInterface::PQGetValueFromBinary(&emailResult.emailID, result, i, "emailId_fk");
  2151. bool getThisEmail;
  2152. if (emailsToRetrieve.Size()>0)
  2153. {
  2154. getThisEmail=false;
  2155. for (unsigned int i=0; i < emailsToRetrieve.Size(); i++)
  2156. {
  2157. if (emailsToRetrieve[i]==emailResult.emailID)
  2158. {
  2159. getThisEmail=true;
  2160. break;
  2161. }
  2162. }
  2163. }
  2164. else
  2165. getThisEmail=true;
  2166. if (getThisEmail)
  2167. {
  2168. if (emailIdsOnly==false)
  2169. {
  2170. PostgreSQLInterface::PQGetValueFromBinary(&otherHandle, result, i, "handle");
  2171. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.status, result, i, "status");
  2172. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.wasReadByMe, result, i, "wasRead");
  2173. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.wasSendByMe, result, i, "ISentThisEmail");
  2174. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.subject, result, i, "subject");
  2175. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.body, result, i, "body");
  2176. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.binaryData->binaryData, &emailResult.binaryData->binaryDataLength, result, i, "binaryData");
  2177. PostgreSQLInterface::PQGetValueFromBinary(&emailResult.creationDate, result, i, "creationDate");
  2178. if (emailResult.wasSendByMe)
  2179. {
  2180. emailResult.sender=myHandle;
  2181. emailResult.recipient=otherHandle;
  2182. }
  2183. else
  2184. {
  2185. emailResult.sender=otherHandle;
  2186. emailResult.recipient=myHandle;
  2187. }
  2188. }
  2189. emailResults.Insert(emailResult, __FILE__, __LINE__ );
  2190. }
  2191. emailResults.Insert(emailResult, _FILE_AND_LINE_ );
  2192. }
  2193. resultCode=L2RC_SUCCESS;
  2194. PQclear(result);
  2195. return true;
  2196. }
  2197. bool RakNet::Emails_Delete_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2198. {
  2199. (void)command;
  2200. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2201. PGresult *result = pgsql->QueryVariadic("SELECT isDeleted FROM lobby2.emailTargets WHERE emailTarget_pk = %i", emailId);
  2202. if (result==0)
  2203. {
  2204. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2205. return true;
  2206. }
  2207. int numRowsReturned = PQntuples(result);
  2208. if (numRowsReturned==0)
  2209. {
  2210. PQclear(result);
  2211. resultCode=L2RC_Emails_Delete_UNKNOWN_EMAIL_ID;
  2212. return true;
  2213. }
  2214. bool isDeleted;
  2215. PostgreSQLInterface::PQGetValueFromBinary(&isDeleted, result, 0, "isDeleted");
  2216. PQclear(result);
  2217. if (isDeleted)
  2218. {
  2219. resultCode=L2RC_Emails_Delete_ALREADY_DELETED;
  2220. return true;
  2221. }
  2222. // Don't actually delete, just flag as deleted. This is so the admin can investigate reports of abuse.
  2223. result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET isDeleted=TRUE WHERE emailTarget_pk = %i", emailId);
  2224. if (result==0)
  2225. {
  2226. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2227. return true;
  2228. }
  2229. PQclear(result);
  2230. resultCode=L2RC_SUCCESS;
  2231. return true;
  2232. }
  2233. bool RakNet::Emails_SetStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2234. {
  2235. (void)command;
  2236. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2237. PGresult *result = pgsql->QueryVariadic("SELECT isDeleted FROM lobby2.emailTargets WHERE emailTarget_pk = %i", emailId);
  2238. if (result==0)
  2239. {
  2240. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2241. return true;
  2242. }
  2243. int numRowsReturned = PQntuples(result);
  2244. if (numRowsReturned==0)
  2245. {
  2246. PQclear(result);
  2247. resultCode=L2RC_Emails_SetStatus_UNKNOWN_EMAIL_ID;
  2248. return true;
  2249. }
  2250. bool isDeleted;
  2251. PostgreSQLInterface::PQGetValueFromBinary(&isDeleted, result, 0, "isDeleted");
  2252. PQclear(result);
  2253. if (isDeleted)
  2254. {
  2255. resultCode=L2RC_Emails_SetStatus_WAS_DELETED;
  2256. return true;
  2257. }
  2258. if (updateStatusFlag)
  2259. {
  2260. result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET status=%i WHERE emailTarget_pk = %i", newStatusFlag, emailId);
  2261. if (result==0)
  2262. {
  2263. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2264. return true;
  2265. }
  2266. PQclear(result);
  2267. }
  2268. if (updateMarkedRead)
  2269. {
  2270. result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET wasRead=%b WHERE emailTarget_pk = %i", isNowMarkedRead, emailId);
  2271. if (result==0)
  2272. {
  2273. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2274. return true;
  2275. }
  2276. }
  2277. PQclear(result);
  2278. resultCode=L2RC_SUCCESS;
  2279. return true;
  2280. }
  2281. bool RakNet::Ranking_SubmitMatch_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2282. {
  2283. (void)command;
  2284. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2285. PGresult *result=0;
  2286. // Verify title name
  2287. if (IsTitleInUse(titleName, pgsql)==false)
  2288. {
  2289. resultCode=L2RC_Ranking_SubmitMatch_TITLE_NOT_IN_USE;
  2290. return true;
  2291. }
  2292. // Insert
  2293. result = pgsql->QueryVariadic("INSERT INTO lobby2.matches (gameTypeName, titleName_fk, matchNote, binaryData) VALUES "
  2294. "(%s, %s, %s, %a) RETURNING matchId_pk;",
  2295. gameType.C_String(), titleName.C_String(), submittedMatch.matchNote.C_String(), submittedMatch.binaryData->binaryData, submittedMatch.binaryData->binaryDataLength );
  2296. int numRowsReturned = PQntuples(result);
  2297. if (numRowsReturned==0)
  2298. {
  2299. PQclear(result);
  2300. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2301. return true;
  2302. }
  2303. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result, 0, "matchId_pk");
  2304. PQclear(result);
  2305. // For each match participant, add to lobby2.matchParticipants
  2306. unsigned int i;
  2307. for (i=0; i < submittedMatch.matchParticipants.Size(); i++)
  2308. {
  2309. result = pgsql->QueryVariadic("INSERT INTO lobby2.matchParticipants (matchId_fk, userId_fk, score) VALUES "
  2310. "(%i, (SELECT userId_pk FROM lobby2.users WHERE handleLower=lower(%s)), %f);",
  2311. submittedMatch.matchID, submittedMatch.matchParticipants[i].handle.C_String(), submittedMatch.matchParticipants[i].score);
  2312. // May fail if a user is deleted at the same time this is running
  2313. if (result)
  2314. PQclear(result);
  2315. }
  2316. result = pgsql->QueryVariadic("SELECT EXTRACT(EPOCH FROM now()) as whenSubmittedDate;");
  2317. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.whenSubmittedDate, result, 0, "whenSubmittedDate");
  2318. PQclear(result);
  2319. resultCode=L2RC_SUCCESS;
  2320. return true;
  2321. }
  2322. bool RakNet::Ranking_GetMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2323. {
  2324. (void)command;
  2325. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2326. PGresult *result1, *result2;
  2327. // Verify title name
  2328. if (IsTitleInUse(titleName, pgsql)==false)
  2329. {
  2330. resultCode=L2RC_Ranking_SubmitMatch_TITLE_NOT_IN_USE;
  2331. return true;
  2332. }
  2333. result1 = pgsql->QueryVariadic("SELECT matchId_pk, matchNote, binaryData, EXTRACT(EPOCH FROM creationDate) as creationDate from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
  2334. if (result1==0)
  2335. {
  2336. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2337. return true;
  2338. }
  2339. int numMatchesReturned = PQntuples(result1);
  2340. int i;
  2341. for (i=0; i < numMatchesReturned; i++)
  2342. {
  2343. SubmittedMatch submittedMatch;
  2344. MatchParticipant matchParticipant;
  2345. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result1, i, "matchId_pk");
  2346. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchNote, result1, i, "matchNote");
  2347. // PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.binaryData->binaryData, &submittedMatch.binaryData->binaryDataLength, result1, i, "binaryData");
  2348. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.whenSubmittedDate, result1, i, "creationDate");
  2349. PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result1, i, "matchId_pk");
  2350. result2=pgsql->QueryVariadic("SELECT (SELECT handle FROM lobby2.users where userId_pk=userId_fk) as handle, score from lobby2.matchParticipants where matchId_fk=%i;", submittedMatch.matchID);
  2351. int numParticipants = PQntuples(result2);
  2352. for (int j=0; j < numParticipants; j++)
  2353. {
  2354. PostgreSQLInterface::PQGetValueFromBinary(&matchParticipant.handle, result2, j, "handle");
  2355. PostgreSQLInterface::PQGetValueFromBinary(&matchParticipant.score, result2, j, "score");
  2356. submittedMatch.matchParticipants.Insert(matchParticipant, _FILE_AND_LINE_ );
  2357. }
  2358. PQclear(result2);
  2359. submittedMatches.Insert(submittedMatch, _FILE_AND_LINE_ );
  2360. }
  2361. PQclear(result1);
  2362. resultCode=L2RC_SUCCESS;
  2363. return true;
  2364. }
  2365. bool RakNet::Ranking_GetMatchBinaryData_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2366. {
  2367. (void)command;
  2368. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2369. PGresult *result=0;
  2370. result = pgsql->QueryVariadic("SELECT binaryData from lobby2.matches WHERE matchId_pk=%i", matchID);
  2371. if (result==0)
  2372. {
  2373. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2374. return true;
  2375. }
  2376. int numRowsReturned = PQntuples(result);
  2377. if (numRowsReturned==0)
  2378. {
  2379. PQclear(result);
  2380. resultCode=L2RC_Ranking_GetMatchBinaryData_INVALID_MATCH_ID;
  2381. return false;
  2382. }
  2383. PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
  2384. PQclear(result);
  2385. resultCode=L2RC_SUCCESS;
  2386. return true;
  2387. }
  2388. bool RakNet::Ranking_GetTotalScore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2389. {
  2390. (void)command;
  2391. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2392. PGresult *result=0;
  2393. // Verify title name
  2394. if (IsTitleInUse(titleName, pgsql)==false)
  2395. {
  2396. resultCode=L2RC_Ranking_GetTotalScore_TITLE_NOT_IN_USE;
  2397. return true;
  2398. }
  2399. unsigned int userRow = GetUserRowFromHandle(targetHandle, pgsql);
  2400. if (userRow==0)
  2401. {
  2402. resultCode=L2RC_UNKNOWN_USER;
  2403. return true;
  2404. }
  2405. result = pgsql->QueryVariadic("SELECT COUNT(score) as count, sum(score) as sum from lobby2.matchParticipants WHERE userId_fk=%i AND matchId_fk IN"
  2406. "(SELECT matchId_pk from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s);", userRow, gameType.C_String(), titleName.C_String());
  2407. if (result==0)
  2408. {
  2409. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2410. return true;
  2411. }
  2412. int64_t count;
  2413. PostgreSQLInterface::PQGetValueFromBinary(&count, result, 0, "count");
  2414. numScoresSubmitted = (unsigned int) count;
  2415. if (numScoresSubmitted>0)
  2416. PostgreSQLInterface::PQGetValueFromBinary(&scoreSum, result, 0, "sum");
  2417. else
  2418. scoreSum=0.0f;
  2419. PQclear(result);
  2420. resultCode=L2RC_SUCCESS;
  2421. return true;
  2422. }
  2423. bool RakNet::Ranking_WipeScoresForPlayer_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2424. {
  2425. (void)command;
  2426. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2427. PGresult *result=0;
  2428. // Verify title name
  2429. if (IsTitleInUse(titleName, pgsql)==false)
  2430. {
  2431. resultCode=L2RC_Ranking_WipeScoresForPlayer_TITLE_NOT_IN_USE;
  2432. return true;
  2433. }
  2434. unsigned int userRow = GetUserRowFromHandle(targetHandle, pgsql);
  2435. if (userRow==0)
  2436. {
  2437. resultCode=L2RC_UNKNOWN_USER;
  2438. return true;
  2439. }
  2440. result = pgsql->QueryVariadic("DELETE FROM lobby2.matchParticipants WHERE userId_fk=%i AND matchId_fk IN"
  2441. "(SELECT matchId_pk from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s);", userRow, gameType.C_String(), titleName.C_String());
  2442. if (result==0)
  2443. {
  2444. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2445. return true;
  2446. }
  2447. resultCode=L2RC_SUCCESS;
  2448. return true;
  2449. }
  2450. bool RakNet::Ranking_WipeMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2451. {
  2452. (void)command;
  2453. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2454. PGresult *result=0;
  2455. // Verify title name
  2456. if (IsTitleInUse(titleName, pgsql)==false)
  2457. {
  2458. resultCode=L2RC_Ranking_WipeMatches_TITLE_NOT_IN_USE;
  2459. return true;
  2460. }
  2461. result = pgsql->QueryVariadic("DELETE FROM lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
  2462. if (result==0)
  2463. {
  2464. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2465. return true;
  2466. }
  2467. resultCode=L2RC_SUCCESS;
  2468. return true;
  2469. }
  2470. bool RakNet::Ranking_PruneMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2471. {
  2472. (void)command;
  2473. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2474. PGresult *result=0;
  2475. result = pgsql->QueryVariadic("DELETE FROM lobby2.matches WHERE creationDate < (select now() - %i * interval '1 day')", pruneTimeDays);
  2476. if (result==0)
  2477. {
  2478. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2479. return true;
  2480. }
  2481. resultCode=L2RC_SUCCESS;
  2482. return true;
  2483. }
  2484. bool RakNet::Ranking_UpdateRating_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2485. {
  2486. (void)command;
  2487. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2488. PGresult *result=0;
  2489. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  2490. {
  2491. resultCode=L2RC_Ranking_UpdateRating_TITLE_NOT_IN_USE;
  2492. return true;
  2493. }
  2494. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2495. if (targetUserId==0)
  2496. {
  2497. resultCode=L2RC_Ranking_UpdateRating_UNKNOWN_TARGET_HANDLE;
  2498. return true;
  2499. }
  2500. result = pgsql->QueryVariadic("INSERT INTO lobby2.ratings (userId_fk, gameTypeName, titleName_fk, userRating) VALUES (%i, %s, %s, %f)", targetUserId, gameType.C_String(), titleName.C_String(), targetRating);
  2501. if (result==0)
  2502. {
  2503. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2504. return true;
  2505. }
  2506. resultCode=L2RC_SUCCESS;
  2507. return true;
  2508. }
  2509. bool RakNet::Ranking_WipeRatings_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2510. {
  2511. (void)command;
  2512. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2513. PGresult *result=0;
  2514. // Verify title name
  2515. if (IsTitleInUse(titleName, pgsql)==false)
  2516. {
  2517. resultCode=L2RC_Ranking_WipeRatings_TITLE_NOT_IN_USE;
  2518. return true;
  2519. }
  2520. result = pgsql->QueryVariadic("DELETE FROM lobby2.ratings WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
  2521. if (result==0)
  2522. {
  2523. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2524. return true;
  2525. }
  2526. resultCode=L2RC_SUCCESS;
  2527. return true;
  2528. }
  2529. bool RakNet::Ranking_GetRating_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2530. {
  2531. (void)command;
  2532. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2533. PGresult *result=0;
  2534. if (RakNet::IsTitleInUse(titleName, pgsql)==false)
  2535. {
  2536. resultCode=L2RC_Ranking_GetRating_TITLE_NOT_IN_USE;
  2537. return true;
  2538. }
  2539. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2540. if (targetUserId==0)
  2541. {
  2542. resultCode=L2RC_Ranking_GetRating_UNKNOWN_TARGET_HANDLE;
  2543. return true;
  2544. }
  2545. result = pgsql->QueryVariadic("SELECT userRating, creationDate FROM lobby2.ratings WHERE gameTypeName=%s AND titleName_fk=%s and userId_fk=%i ORDER by creationDate LIMIT 1;", gameType.C_String(), titleName.C_String(), targetUserId);
  2546. if (result==0)
  2547. {
  2548. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2549. return true;
  2550. }
  2551. int numRowsReturned = PQntuples(result);
  2552. if (numRowsReturned==0)
  2553. {
  2554. PQclear(result);
  2555. resultCode=L2RC_Ranking_GetRating_NO_RATING;
  2556. return true;
  2557. }
  2558. PostgreSQLInterface::PQGetValueFromBinary(&currentRating, result, 0, "userRating");
  2559. PQclear(result);
  2560. resultCode=L2RC_SUCCESS;
  2561. return true;
  2562. }
  2563. bool RakNet::Clans_Create_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2564. {
  2565. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2566. PGresult *result=0;
  2567. if (StringContainsProfanity(clanHandle, pgsql))
  2568. {
  2569. resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
  2570. return true;
  2571. }
  2572. if (GetClanIdFromHandle(clanHandle, pgsql))
  2573. {
  2574. resultCode=L2RC_Clans_Create_CLAN_HANDLE_IN_USE;
  2575. return true;
  2576. }
  2577. result = pgsql->QueryVariadic(
  2578. "INSERT INTO lobby2.clans (leaderUserId_fk, clanHandle, requiresInvitationsToJoin, description, binaryData) VALUES "
  2579. "(%i, %s, %b, %s, %a) RETURNING clanId_pk;", command->callerUserId, clanHandle.C_String(), requiresInvitationsToJoin, description.C_String(), binaryData->binaryData, binaryData->binaryDataLength );
  2580. if (result==0)
  2581. {
  2582. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2583. return true;
  2584. }
  2585. unsigned int clanId_pk;
  2586. PostgreSQLInterface::PQGetValueFromBinary(&clanId_pk, result, 0, "clanId_pk");
  2587. PQclear(result);
  2588. if (failIfAlreadyInClan)
  2589. {
  2590. // Checking after rather than before in case this user creates a clan in another thread at the same time
  2591. result = pgsql->QueryVariadic("SELECT COUNT(*) as count from lobby2.clans WHERE leaderUserId_fk=%i", command->callerUserId);
  2592. long long count;
  2593. PostgreSQLInterface::PQGetValueFromBinary(&count, result, 0, "count");
  2594. PQclear(result);
  2595. if (count>1)
  2596. {
  2597. result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE (clanId_pk=%i);", clanId_pk);
  2598. PQclear(result);
  2599. resultCode=L2RC_Clans_Create_ALREADY_IN_A_CLAN;
  2600. return true;
  2601. }
  2602. }
  2603. // Add yourself as a clan member
  2604. result = pgsql->QueryVariadic(
  2605. "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
  2606. "(%i, %i, false, (SELECT stateId_Pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active'));", command->callerUserId, clanId_pk );
  2607. if (result==0)
  2608. {
  2609. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2610. return true;
  2611. }
  2612. PQclear(result);
  2613. // Tell all friends about this new clan
  2614. DataStructures::List<unsigned int> output;
  2615. GetFriendIDs(command->callerUserId, true, pgsql, output);
  2616. unsigned int idx;
  2617. for (idx=0; idx < output.Size(); idx++)
  2618. {
  2619. Notification_Friends_CreatedClan *notification = (Notification_Friends_CreatedClan *)command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_CreatedClan);
  2620. RakAssert(command->callingUserName.IsEmpty()==false);
  2621. notification->otherHandle=command->callingUserName;
  2622. notification->clanName=clanHandle;
  2623. notification->resultCode=L2RC_SUCCESS;
  2624. command->server->AddOutputFromThread(notification, output[idx], "");
  2625. }
  2626. resultCode=L2RC_SUCCESS;
  2627. return true;
  2628. }
  2629. bool RakNet::Clans_SetProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2630. {
  2631. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2632. PGresult *result=0;
  2633. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2634. if (clanId==0)
  2635. {
  2636. resultCode=L2RC_Clans_SetProperties_UNKNOWN_CLAN;
  2637. return true;
  2638. }
  2639. if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
  2640. {
  2641. resultCode=L2RC_Clans_SetProperties_MUST_BE_LEADER;
  2642. return true;
  2643. }
  2644. result = pgsql->QueryVariadic("UPDATE lobby2.clans SET description=%s, binaryData=%a WHERE clanId_pk=%i", description.C_String(), binaryData->binaryData, binaryData->binaryDataLength, clanId);
  2645. if (result==0)
  2646. {
  2647. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2648. return true;
  2649. }
  2650. PQclear(result);
  2651. resultCode=L2RC_SUCCESS;
  2652. return true;
  2653. }
  2654. bool RakNet::Clans_GetProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2655. {
  2656. (void)command;
  2657. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2658. PGresult *result=0;
  2659. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2660. if (clanId==0)
  2661. {
  2662. resultCode=L2RC_Clans_GetProperties_UNKNOWN_CLAN;
  2663. return true;
  2664. }
  2665. result = pgsql->QueryVariadic("SELECT description, binaryData FROM lobby2.clans WHERE clanId_pk=%i", clanId);
  2666. if (result==0)
  2667. {
  2668. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2669. return true;
  2670. }
  2671. PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
  2672. PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
  2673. PQclear(result);
  2674. resultCode=L2RC_SUCCESS;
  2675. return true;
  2676. }
  2677. bool RakNet::Clans_SetMyMemberProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2678. {
  2679. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2680. PGresult *result=0;
  2681. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2682. if (clanId==0)
  2683. {
  2684. resultCode=L2RC_Clans_SetMyMemberProperties_UNKNOWN_CLAN;
  2685. return true;
  2686. }
  2687. bool isSubleader;
  2688. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  2689. if (clanMemberState!=CMD_ACTIVE)
  2690. {
  2691. resultCode=L2RC_Clans_SetMyMemberProperties_NOT_IN_CLAN;
  2692. return true;
  2693. }
  2694. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET description=%s, binaryData=%a WHERE userId_fk=%i AND clanId_fk=%i", description.C_String(), binaryData->binaryData, binaryData->binaryDataLength, command->callerUserId, clanId);
  2695. if (result==0)
  2696. {
  2697. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2698. return true;
  2699. }
  2700. PQclear(result);
  2701. resultCode=L2RC_SUCCESS;
  2702. return true;
  2703. }
  2704. bool RakNet::Clans_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2705. {
  2706. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2707. // Gets all clans that I am in, as active
  2708. PGresult *result = pgsql->QueryVariadic(
  2709. "SELECT U.handle, C.clanHandle, C.description, C.binaryData, C.clanId_fk, C.leaderUserId_fk "
  2710. "FROM lobby2.users AS U, (SELECT clanHandle, description, binaryData, leaderUserId_fk, myClans.* FROM lobby2.clans, "
  2711. "(SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i "
  2712. "AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active')) AS myClans "
  2713. "WHERE clanId_pk=myClans.clanId_fk) AS C WHERE U.userId_pk=C.leaderUserId_fk;", command->callerUserId);
  2714. if (result==0)
  2715. {
  2716. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2717. return true;
  2718. }
  2719. int numRowsReturned = PQntuples(result);
  2720. if (numRowsReturned==0)
  2721. {
  2722. PQclear(result);
  2723. resultCode=L2RC_SUCCESS;
  2724. return true;
  2725. }
  2726. for (int i=0; i < numRowsReturned; i++)
  2727. {
  2728. ClanInfo ci;
  2729. unsigned int clanId;
  2730. int leaderId;
  2731. PostgreSQLInterface::PQGetValueFromBinary(&ci.clanName, result, i, "clanHandle");
  2732. PostgreSQLInterface::PQGetValueFromBinary(&ci.description, result, i, "description");
  2733. PostgreSQLInterface::PQGetValueFromBinary(&ci.clanLeader, result, i, "handle");
  2734. PostgreSQLInterface::PQGetValueFromBinary(&ci.binaryData->binaryData, &ci.binaryData->binaryDataLength, result, i, "binaryData");
  2735. PostgreSQLInterface::PQGetValueFromBinary(&clanId, result, i, "clanId_fk");
  2736. PostgreSQLInterface::PQGetValueFromBinary(&leaderId, result, i, "leaderUserId_fk");
  2737. // Get the names of all other active members in this clan
  2738. PGresult *result2 = pgsql->QueryVariadic(
  2739. "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
  2740. "WHERE M.clanId_fk=%i AND M.userId_fk!=%i AND U.userId_pk=M.userId_fk "
  2741. "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active');",
  2742. clanId, leaderId);
  2743. int numRowsReturned2 = PQntuples(result2);
  2744. RakNet::RakString memberHandle;
  2745. for (int j=0; j < numRowsReturned2; j++)
  2746. {
  2747. PostgreSQLInterface::PQGetValueFromBinary(&memberHandle, result2, j, "handle");
  2748. ci.clanMembersOtherThanLeader.Insert(memberHandle, _FILE_AND_LINE_ );
  2749. }
  2750. PQclear(result2);
  2751. clans.Insert(ci, _FILE_AND_LINE_ );
  2752. }
  2753. PQclear(result);
  2754. resultCode=L2RC_SUCCESS;
  2755. return true;
  2756. };
  2757. bool RakNet::Clans_GrantLeader_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2758. {
  2759. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2760. PGresult *result=0;
  2761. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2762. if (clanId==0)
  2763. {
  2764. resultCode=L2RC_Clans_GrantLeader_UNKNOWN_CLAN;
  2765. return true;
  2766. }
  2767. if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
  2768. {
  2769. resultCode=L2RC_Clans_GrantLeader_MUST_BE_LEADER;
  2770. return true;
  2771. }
  2772. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2773. if (targetUserId==0)
  2774. {
  2775. resultCode=L2RC_Clans_GrantLeader_UNKNOWN_TARGET_HANDLE;
  2776. return true;
  2777. }
  2778. if (targetUserId==command->callerUserId)
  2779. {
  2780. resultCode=L2RC_Clans_GrantLeader_CANNOT_PERFORM_ON_SELF;
  2781. return true;
  2782. }
  2783. bool isSubleader;
  2784. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
  2785. if (clanMemberState!=CMD_ACTIVE)
  2786. {
  2787. resultCode=L2RC_Clans_GrantLeader_TARGET_NOT_IN_CLAN;
  2788. return true;
  2789. }
  2790. result = pgsql->QueryVariadic("UPDATE lobby2.clans SET leaderUserId_fk=%i WHERE clanId_pk=%i;",targetUserId, clanId);
  2791. if (result==0)
  2792. {
  2793. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2794. return true;
  2795. }
  2796. PQclear(result);
  2797. resultCode=L2RC_SUCCESS;
  2798. Notification_Clans_GrantLeader *notification;
  2799. // Tell all clan members of the new leader
  2800. DataStructures::List<ClanMemberDescriptor> clanMembers;
  2801. GetClanMembers(clanId, clanMembers, pgsql);
  2802. // Tell all clan members, except the command originator, about the new clan leader
  2803. for (unsigned int i=0; i < clanMembers.Size(); i++)
  2804. {
  2805. if (clanMembers[i].memberState!=CMD_ACTIVE)
  2806. continue;
  2807. if (clanMembers[i].userId==command->callerUserId)
  2808. continue;
  2809. notification = (Notification_Clans_GrantLeader *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_GrantLeader);
  2810. notification->clanHandle=clanHandle;
  2811. notification->oldLeader=command->callingUserName;
  2812. notification->newLeader=targetHandle;
  2813. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  2814. }
  2815. return true;
  2816. }
  2817. bool RakNet::Clans_SetSubleaderStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2818. {
  2819. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2820. PGresult *result=0;
  2821. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2822. if (clanId==0)
  2823. {
  2824. resultCode=L2RC_Clans_SetSubleaderStatus_UNKNOWN_CLAN;
  2825. return true;
  2826. }
  2827. if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
  2828. {
  2829. resultCode=L2RC_Clans_SetProperties_MUST_BE_LEADER;
  2830. return true;
  2831. }
  2832. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2833. if (targetUserId==0)
  2834. {
  2835. resultCode=L2RC_Clans_SetSubleaderStatus_UNKNOWN_TARGET_HANDLE;
  2836. return true;
  2837. }
  2838. if (targetUserId==command->callerUserId)
  2839. {
  2840. resultCode=L2RC_Clans_SetSubleaderStatus_CANNOT_PERFORM_ON_SELF;
  2841. return true;
  2842. }
  2843. bool isSubleader;
  2844. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
  2845. if (clanMemberState!=CMD_ACTIVE)
  2846. {
  2847. resultCode=L2RC_Clans_SetSubleaderStatus_TARGET_NOT_IN_CLAN;
  2848. return true;
  2849. }
  2850. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET isSubleader=%b WHERE clanId_fk=%i AND userId_fk=%i;",setToSubleader,clanId,targetUserId);
  2851. if (result==0)
  2852. {
  2853. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2854. return true;
  2855. }
  2856. PQclear(result);
  2857. Notification_Clans_SetSubleaderStatus *notification;
  2858. // Tell all clan members of the new leader
  2859. DataStructures::List<ClanMemberDescriptor> clanMembers;
  2860. GetClanMembers(clanId, clanMembers, pgsql);
  2861. // Tell all clan members, except the command originator, about the new clan leader
  2862. for (unsigned int i=0; i < clanMembers.Size(); i++)
  2863. {
  2864. if (clanMembers[i].memberState!=CMD_ACTIVE)
  2865. continue;
  2866. if (clanMembers[i].userId==command->callerUserId)
  2867. continue;
  2868. notification = (Notification_Clans_SetSubleaderStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_SetSubleaderStatus);
  2869. notification->clanHandle=clanHandle;
  2870. notification->leaderHandle=command->callingUserName;
  2871. notification->targetHandle=targetHandle;
  2872. notification->setToSubleader=setToSubleader;
  2873. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  2874. }
  2875. resultCode=L2RC_SUCCESS;
  2876. return true;
  2877. }
  2878. bool RakNet::Clans_SetMemberRank_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2879. {
  2880. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2881. PGresult *result=0;
  2882. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2883. if (clanId==0)
  2884. {
  2885. resultCode=L2RC_Clans_SetMemberRank_UNKNOWN_CLAN;
  2886. return true;
  2887. }
  2888. if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
  2889. {
  2890. resultCode=L2RC_Clans_SetMemberRank_MUST_BE_LEADER;
  2891. return true;
  2892. }
  2893. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2894. if (targetUserId==0)
  2895. {
  2896. resultCode=L2RC_Clans_SetMemberRank_UNKNOWN_TARGET_HANDLE;
  2897. return true;
  2898. }
  2899. if (targetUserId==command->callerUserId)
  2900. {
  2901. resultCode=L2RC_Clans_SetMemberRank_CANNOT_PERFORM_ON_SELF;
  2902. return true;
  2903. }
  2904. bool isSubleader;
  2905. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
  2906. if (clanMemberState!=CMD_ACTIVE)
  2907. {
  2908. resultCode=L2RC_Clans_SetMemberRank_TARGET_NOT_IN_CLAN;
  2909. return true;
  2910. }
  2911. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET rank=%i WHERE clanId_fk=%i AND userId_fk=%i;",newRank,clanId,targetUserId);
  2912. if (result==0)
  2913. {
  2914. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2915. return true;
  2916. }
  2917. PQclear(result);
  2918. Notification_Clans_SetMemberRank *notification;
  2919. // Tell all clan members of the new leader
  2920. DataStructures::List<ClanMemberDescriptor> clanMembers;
  2921. GetClanMembers(clanId, clanMembers, pgsql);
  2922. // Tell all clan members, except the command originator, about the new clan leader
  2923. for (unsigned int i=0; i < clanMembers.Size(); i++)
  2924. {
  2925. if (clanMembers[i].memberState!=CMD_ACTIVE)
  2926. continue;
  2927. if (clanMembers[i].userId==command->callerUserId)
  2928. continue;
  2929. notification = (Notification_Clans_SetMemberRank *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_SetMemberRank);
  2930. notification->clanHandle=clanHandle;
  2931. notification->leaderHandle=command->callingUserName;
  2932. notification->targetHandle=targetHandle;
  2933. notification->newRank=newRank;
  2934. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  2935. }
  2936. resultCode=L2RC_SUCCESS;
  2937. return true;
  2938. }
  2939. bool RakNet::Clans_GetMemberProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2940. {
  2941. (void)command;
  2942. PGresult *result=0;
  2943. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2944. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  2945. if (clanId==0)
  2946. {
  2947. resultCode=L2RC_Clans_GetMemberProperties_UNKNOWN_CLAN;
  2948. return true;
  2949. }
  2950. unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
  2951. if (targetUserId==0)
  2952. {
  2953. resultCode=L2RC_Clans_GetMemberProperties_UNKNOWN_TARGET_HANDLE;
  2954. return true;
  2955. }
  2956. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
  2957. if (clanMemberState==CMD_UNDEFINED)
  2958. {
  2959. resultCode=L2RC_Clans_GetMemberProperties_TARGET_NOT_IN_CLAN;
  2960. return true;
  2961. }
  2962. result = pgsql->QueryVariadic("SELECT description, binaryData, isSubleader, rank, memberState_fk, banReason FROM lobby2.clanMembers where userId_fk=%i AND clanId_fk=%i",
  2963. targetUserId, clanId);
  2964. if (result==0)
  2965. {
  2966. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  2967. return true;
  2968. }
  2969. PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
  2970. PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
  2971. PostgreSQLInterface::PQGetValueFromBinary(&isSubleader, result, 0, "isSubleader");
  2972. PostgreSQLInterface::PQGetValueFromBinary(&rank, result, 0, "rank");
  2973. int cms;
  2974. PostgreSQLInterface::PQGetValueFromBinary(&cms, result, 0, "memberState_fk");
  2975. clanMemberState=(ClanMemberState)cms;
  2976. PostgreSQLInterface::PQGetValueFromBinary(&banReason, result, 0, "banReason");
  2977. PQclear(result);
  2978. resultCode=L2RC_SUCCESS;
  2979. return true;
  2980. }
  2981. bool RakNet::Clans_ChangeHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  2982. {
  2983. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  2984. PGresult *result=0;
  2985. unsigned int clanId = GetClanIdFromHandle(oldClanHandle, pgsql);
  2986. if (clanId==0)
  2987. {
  2988. resultCode=L2RC_Clans_ChangeHandle_UNKNOWN_CLAN;
  2989. return true;
  2990. }
  2991. if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
  2992. {
  2993. resultCode=L2RC_Clans_ChangeHandle_MUST_BE_LEADER;
  2994. return true;
  2995. }
  2996. result = pgsql->QueryVariadic("UPDATE lobby2.clans SET clanHandle=%s WHERE clanId_pk=%i;", newClanHandle.C_String(), clanId);
  2997. if (result==0)
  2998. {
  2999. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3000. return true;
  3001. }
  3002. PQclear(result);
  3003. Notification_Clans_ChangeHandle *notification;
  3004. // Tell all clan members of the new leader
  3005. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3006. GetClanMembers(clanId, clanMembers, pgsql);
  3007. // Tell all clan members, except the command originator, about the new clan name
  3008. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3009. {
  3010. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3011. continue;
  3012. if (clanMembers[i].userId==command->callerUserId)
  3013. continue;
  3014. notification = (Notification_Clans_ChangeHandle *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_ChangeHandle);
  3015. notification->oldClanHandle=oldClanHandle;
  3016. notification->leaderHandle=command->callingUserName;
  3017. notification->newClanHandle=newClanHandle;
  3018. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3019. }
  3020. resultCode=L2RC_SUCCESS;
  3021. return true;
  3022. }
  3023. bool RakNet::Clans_Leave_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3024. {
  3025. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3026. PGresult *result=0;
  3027. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3028. if (clanId==0)
  3029. {
  3030. resultCode=L2RC_Clans_Leave_UNKNOWN_CLAN;
  3031. return true;
  3032. }
  3033. bool isSubleader;
  3034. ClanMemberState state = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3035. if (state!=CMD_ACTIVE)
  3036. {
  3037. resultCode=L2RC_Clans_Leave_NOT_IN_CLAN;
  3038. return true;
  3039. }
  3040. bool isClanLeader = IsClanLeader(clanId, command->callerUserId, pgsql);
  3041. // Remove from the clanMembers table
  3042. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
  3043. if (result==0)
  3044. {
  3045. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3046. return true;
  3047. }
  3048. PQclear(result);
  3049. // Tell all clan members of the new leader
  3050. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3051. GetClanMembers(clanId, clanMembers, pgsql);
  3052. if (subject.IsEmpty()==false || body.IsEmpty()==false)
  3053. {
  3054. // Send this email to the members
  3055. DataStructures::List<unsigned int> targetUserIds;
  3056. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3057. {
  3058. if (clanMembers[i].memberState==CMD_ACTIVE)
  3059. targetUserIds.Insert(clanMembers[i].userId, _FILE_AND_LINE_ );
  3060. }
  3061. SendEmail(targetUserIds, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_Leave", pgsql);
  3062. }
  3063. unsigned int validUserCount=0;
  3064. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3065. {
  3066. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3067. continue;
  3068. validUserCount++;
  3069. }
  3070. wasDissolved = false;
  3071. if (isClanLeader)
  3072. {
  3073. if (dissolveIfClanLeader || validUserCount==0)
  3074. {
  3075. wasDissolved = true;
  3076. // Send notification to clan members that the clan was destroyed, then destroy the clan
  3077. Notification_Clans_Destroyed *notification;
  3078. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3079. {
  3080. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3081. continue;
  3082. notification = (Notification_Clans_Destroyed *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Destroyed);
  3083. notification->clanHandle=clanHandle;
  3084. notification->oldClanLeader=command->callingUserName;
  3085. notification->resultCode=L2RC_SUCCESS;
  3086. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3087. }
  3088. // Tell the former leader the clan was destroyed too
  3089. notification = (Notification_Clans_Destroyed *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Destroyed);
  3090. notification->clanHandle=clanHandle;
  3091. notification->oldClanLeader=command->callingUserName;
  3092. notification->resultCode=L2RC_SUCCESS;
  3093. command->server->AddOutputFromThread(notification, command->callerUserId, command->callingUserName);
  3094. // Destroy the clan
  3095. result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE clanId_pk=%i", clanId);
  3096. PQclear(result);
  3097. resultCode=L2RC_SUCCESS;
  3098. return true;
  3099. }
  3100. else
  3101. {
  3102. // Choose the oldest subleader to lead, or if no subleaders, the oldest member
  3103. result = pgsql->QueryVariadic("SELECT handle, userId_pk FROM lobby2.users WHERE userId_pk=(SELECT userId_fk FROM lobby2.clanMembers WHERE clanId_fk=%i ORDER BY isSubleader, creationDate DESC LIMIT 1)",
  3104. clanId);
  3105. int numRowsReturned = PQntuples(result);
  3106. if (numRowsReturned==0)
  3107. {
  3108. // Destroy the clan if no possible leader (due to asynch)
  3109. wasDissolved = true;
  3110. PQclear(result);
  3111. result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE clanId_pk=%i", clanId);
  3112. PQclear(result);
  3113. resultCode=L2RC_SUCCESS;
  3114. return true;
  3115. }
  3116. unsigned int newLeaderId;
  3117. PostgreSQLInterface::PQGetValueFromBinary(&newClanLeader, result, 0, "handle");
  3118. PostgreSQLInterface::PQGetValueFromBinary(&newLeaderId, result, 0, "userId_pk");
  3119. PQclear(result);
  3120. // Promote this member to the leader
  3121. result = pgsql->QueryVariadic("UPDATE lobby2.clans SET leaderUserId_fk=%i WHERE clanId_pk = %i", newLeaderId, clanId);
  3122. PQclear(result);
  3123. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET isSubleader=FALSE WHERE userId_fk = %i AND clanId_fk = %i", newLeaderId, clanId);
  3124. PQclear(result);
  3125. // Notify users of new leader
  3126. Notification_Clans_GrantLeader *notification;
  3127. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3128. {
  3129. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3130. continue;
  3131. if (clanMembers[i].userId==command->callerUserId)
  3132. continue;
  3133. notification = (Notification_Clans_GrantLeader *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_GrantLeader);
  3134. notification->clanHandle=clanHandle;
  3135. notification->newLeader=newClanLeader;
  3136. notification->oldLeader=command->callingUserName;
  3137. notification->resultCode=L2RC_SUCCESS;
  3138. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3139. }
  3140. }
  3141. }
  3142. // Send notification to clan members that the member has left the clan
  3143. Notification_Clans_Leave *notification;
  3144. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3145. {
  3146. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3147. continue;
  3148. if (clanMembers[i].userId==command->callerUserId)
  3149. continue;
  3150. notification = (Notification_Clans_Leave *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Leave);
  3151. notification->clanHandle=clanHandle;
  3152. notification->targetHandle=command->callingUserName;
  3153. notification->resultCode=L2RC_SUCCESS;
  3154. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3155. }
  3156. resultCode=L2RC_SUCCESS;
  3157. return true;
  3158. }
  3159. bool RakNet::Clans_SendJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3160. {
  3161. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3162. PGresult *result=0;
  3163. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3164. if (clanId==0)
  3165. {
  3166. resultCode=L2RC_Clans_SendJoinInvitation_UNKNOWN_CLAN;
  3167. return true;
  3168. }
  3169. bool isSubleader;
  3170. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3171. if (clanMemberState!=CMD_ACTIVE)
  3172. {
  3173. resultCode=L2RC_Clans_SendJoinInvitation_NOT_IN_CLAN;
  3174. return true;
  3175. }
  3176. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3177. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3178. {
  3179. resultCode=L2RC_Clans_SendJoinInvitation_MUST_BE_LEADER_OR_SUBLEADER;
  3180. return true;
  3181. }
  3182. // Does target already have an entry?
  3183. unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
  3184. if (targetId==0)
  3185. {
  3186. resultCode=L2RC_Clans_SendJoinInvitation_UNKNOWN_TARGET_HANDLE;
  3187. return true;
  3188. }
  3189. if (targetId==command->callerUserId)
  3190. {
  3191. resultCode=L2RC_Clans_SendJoinInvitation_CANNOT_PERFORM_ON_SELF;
  3192. return true;
  3193. }
  3194. bool isTargetSubleader;
  3195. RakNet::ClanMemberState targetClanMemberState = GetClanMemberState(clanId, targetId, &isTargetSubleader, pgsql);
  3196. if (targetClanMemberState==CMD_ACTIVE)
  3197. {
  3198. // active member
  3199. resultCode=L2RC_Clans_SendJoinInvitation_TARGET_ALREADY_IN_CLAN;
  3200. return true;
  3201. }
  3202. if (targetClanMemberState==CMD_BANNED)
  3203. {
  3204. // banned
  3205. resultCode=L2RC_Clans_SendJoinInvitation_TARGET_IS_BANNED;
  3206. return true;
  3207. }
  3208. if (targetClanMemberState==CMD_JOIN_INVITED)
  3209. {
  3210. // already invited
  3211. resultCode=L2RC_Clans_SendJoinInvitation_REQUEST_ALREADY_PENDING;
  3212. return true;
  3213. }
  3214. if (targetClanMemberState==CMD_JOIN_REQUESTED)
  3215. {
  3216. resultCode=L2RC_Clans_SendJoinInvitation_TARGET_ALREADY_REQUESTED;
  3217. // already requested
  3218. return true;
  3219. }
  3220. // Add row to lobby2.clanMembers
  3221. result = pgsql->QueryVariadic(
  3222. "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
  3223. "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') );"
  3224. ,targetId, clanId);
  3225. PQclear(result);
  3226. // Send email to targetman
  3227. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_SendJoinInvitation", pgsql);
  3228. // Send notification to target, leader, subleaders about this invite
  3229. Notification_Clans_PendingJoinStatus *notification;
  3230. notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3231. notification->clanHandle=clanHandle;
  3232. notification->targetHandle=targetHandle;
  3233. notification->sourceHandle=command->callingUserName;
  3234. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
  3235. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
  3236. command->server->AddOutputFromThread(notification, targetId, targetHandle); // target
  3237. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3238. GetClanMembers(clanId, clanMembers, pgsql);
  3239. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3240. {
  3241. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3242. continue;
  3243. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3244. continue;
  3245. notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3246. notification->clanHandle=clanHandle;
  3247. notification->targetHandle=targetHandle;
  3248. notification->sourceHandle=command->callingUserName;
  3249. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
  3250. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
  3251. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3252. }
  3253. resultCode=L2RC_SUCCESS;
  3254. return true;
  3255. }
  3256. bool RakNet::Clans_WithdrawJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3257. {
  3258. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3259. PGresult *result=0;
  3260. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3261. if (clanId==0)
  3262. {
  3263. resultCode=L2RC_Clans_WithdrawJoinInvitation_UNKNOWN_CLAN;
  3264. return true;
  3265. }
  3266. // Does target already have an entry?
  3267. unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
  3268. if (targetId==0)
  3269. {
  3270. resultCode=L2RC_Clans_WithdrawJoinInvitation_UNKNOWN_TARGET_HANDLE;
  3271. return true;
  3272. }
  3273. if (targetId==command->callerUserId)
  3274. {
  3275. resultCode=L2RC_Clans_WithdrawJoinInvitation_CANNOT_PERFORM_ON_SELF;
  3276. return true;
  3277. }
  3278. bool isSubleader;
  3279. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetId, &isSubleader, pgsql);
  3280. if (clanMemberState!=CMD_JOIN_INVITED)
  3281. {
  3282. resultCode=L2RC_Clans_WithdrawJoinInvitation_NO_SUCH_INVITATION_EXISTS;
  3283. return true;
  3284. }
  3285. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3286. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3287. {
  3288. resultCode=L2RC_Clans_WithdrawJoinInvitation_MUST_BE_LEADER_OR_SUBLEADER;
  3289. return true;
  3290. }
  3291. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
  3292. PQclear(result);
  3293. // Send email to target
  3294. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_WithdrawJoinInvitation", pgsql);
  3295. // Send notification to target, leader, subleaders
  3296. Notification_Clans_PendingJoinStatus *notification;
  3297. notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3298. notification->clanHandle=clanHandle;
  3299. notification->targetHandle=targetHandle;
  3300. notification->sourceHandle=command->callingUserName;
  3301. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
  3302. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
  3303. command->server->AddOutputFromThread(notification, targetId, targetHandle); // target
  3304. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3305. GetClanMembers(clanId, clanMembers, pgsql);
  3306. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3307. {
  3308. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3309. continue;
  3310. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3311. continue;
  3312. if (command->callerUserId==clanMembers[i].userId)
  3313. continue;
  3314. notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3315. notification->clanHandle=clanHandle;
  3316. notification->targetHandle=targetHandle;
  3317. notification->sourceHandle=command->callingUserName;
  3318. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
  3319. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
  3320. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3321. }
  3322. resultCode=L2RC_SUCCESS;
  3323. return true;
  3324. }
  3325. bool RakNet::Clans_AcceptJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3326. {
  3327. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3328. PGresult *result=0;
  3329. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3330. if (clanId==0)
  3331. {
  3332. resultCode=L2RC_Clans_AcceptJoinInvitation_UNKNOWN_CLAN;
  3333. return true;
  3334. }
  3335. bool isSubleader;
  3336. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3337. if (clanMemberState!=CMD_JOIN_INVITED)
  3338. {
  3339. resultCode=L2RC_Clans_AcceptJoinInvitation_NO_SUCH_INVITATION_EXISTS;
  3340. return true;
  3341. }
  3342. // Change status from invited to clan member
  3343. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
  3344. PQclear(result);
  3345. // Do AFTER the update in case another thread also added to a clan
  3346. if (failIfAlreadyInClan)
  3347. {
  3348. int count = GetActiveClanCount(command->callerUserId, pgsql);
  3349. if (count>1)
  3350. {
  3351. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i;", command->callerUserId, clanId);
  3352. PQclear(result);
  3353. resultCode=L2RC_Clans_AcceptJoinInvitation_ALREADY_IN_DIFFERENT_CLAN;
  3354. return true;
  3355. }
  3356. }
  3357. // Notify all members about this new member
  3358. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3359. GetClanMembers(clanId, clanMembers, pgsql);
  3360. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3361. {
  3362. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3363. continue;
  3364. if (clanMembers[i].userId==command->callerUserId )
  3365. continue;
  3366. Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
  3367. notification->clanHandle=clanHandle;
  3368. notification->targetHandle=command->callingUserName;
  3369. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3370. }
  3371. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3372. // Send email to leader
  3373. SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_AcceptJoinInvitation", pgsql);
  3374. resultCode=L2RC_SUCCESS;
  3375. return true;
  3376. }
  3377. bool RakNet::Clans_RejectJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3378. {
  3379. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3380. PGresult *result=0;
  3381. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3382. if (clanId==0)
  3383. {
  3384. resultCode=L2RC_Clans_RejectJoinInvitation_UNKNOWN_CLAN;
  3385. return true;
  3386. }
  3387. bool isSubleader;
  3388. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3389. if (clanMemberState!=CMD_JOIN_INVITED)
  3390. {
  3391. resultCode=L2RC_Clans_RejectJoinInvitation_NO_SUCH_INVITATION_EXISTS;
  3392. return true;
  3393. }
  3394. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
  3395. PQclear(result);
  3396. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3397. // Send email to leader
  3398. SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_RejectJoinInvitation", pgsql);
  3399. // Subleader and leader notification
  3400. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3401. GetClanMembers(clanId, clanMembers, pgsql);
  3402. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3403. {
  3404. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3405. continue;
  3406. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3407. continue;
  3408. Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3409. notification->clanHandle=clanHandle;
  3410. notification->sourceHandle=command->callingUserName;
  3411. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
  3412. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
  3413. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3414. }
  3415. resultCode=L2RC_SUCCESS;
  3416. return true;
  3417. }
  3418. bool RakNet::Clans_DownloadInvitationList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3419. {
  3420. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3421. PGresult *result = pgsql->QueryVariadic(
  3422. " SELECT clanHandle FROM lobby2.clans INNER JOIN "
  3423. " (SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i AND memberState_fk = "
  3424. " (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') ) as tbl1 "
  3425. " ON tbl1.clanId_fk = lobby2.clans.clanId_pk;", command->callerUserId);
  3426. if (result==0)
  3427. {
  3428. PQclear(result);
  3429. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3430. return true;
  3431. }
  3432. int numRowsReturned = PQntuples(result);
  3433. int i;
  3434. for (i=0; i < numRowsReturned; i++)
  3435. {
  3436. OpenInvite oi;
  3437. PostgreSQLInterface::PQGetValueFromBinary(&oi.clanHandle, result, i, "clanHandle");
  3438. invitationsSentToMe.Insert(oi, _FILE_AND_LINE_ );
  3439. }
  3440. PQclear(result);
  3441. // Gets all users in clans that I am in, as active or leader, where an invitation has been sent
  3442. result = pgsql->QueryVariadic(
  3443. "SELECT creationDate, handle, clanHandle FROM "
  3444. "( "
  3445. "SELECT userId_fk, creationDate, myClans.clanId_fk FROM lobby2.clanMembers INNER JOIN (SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i) as myClans ON "
  3446. "myClans.clanId_fk=lobby2.clanMembers.clanId_fk AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') "
  3447. ") as t1 "
  3448. "INNER JOIN lobby2.users as t2 on (t1.userId_fk=t2.userId_pk) "
  3449. "INNER JOIN lobby2.clans as t3 on (t1.clanId_fk=t3.clanId_pk) ", command->callerUserId);
  3450. if (result==0)
  3451. {
  3452. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3453. return true;
  3454. }
  3455. numRowsReturned = PQntuples(result);
  3456. if (numRowsReturned==0)
  3457. {
  3458. PQclear(result);
  3459. resultCode=L2RC_SUCCESS;
  3460. return true;
  3461. }
  3462. for (i=0; i < PQntuples(result); i++)
  3463. {
  3464. ClanJoinInvite cjr;
  3465. PostgreSQLInterface::PQGetValueFromBinary(&cjr.sourceClan, result, i, "clanHandle");
  3466. PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
  3467. PostgreSQLInterface::PQGetValueFromBinary(&cjr.joinRequestTarget, result, i, "joinRequestTarget");
  3468. usersThatHaveAnInvitationFromClansThatIAmAMemberOf.Insert(cjr, _FILE_AND_LINE_ );
  3469. }
  3470. PQclear(result);
  3471. resultCode=L2RC_SUCCESS;
  3472. return true;
  3473. }
  3474. bool RakNet::Clans_SendJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3475. {
  3476. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3477. PGresult *result=0;
  3478. clanJoined=false;
  3479. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3480. if (clanId==0)
  3481. {
  3482. resultCode=L2RC_Clans_SendJoinRequest_UNKNOWN_CLAN;
  3483. return true;
  3484. }
  3485. bool isSubleader;
  3486. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3487. if (clanMemberState==CMD_ACTIVE)
  3488. {
  3489. resultCode=L2RC_Clans_SendJoinRequest_ALREADY_IN_CLAN;
  3490. return true;
  3491. }
  3492. if (clanMemberState==CMD_BANNED)
  3493. {
  3494. resultCode=L2RC_Clans_SendJoinRequest_BANNED;
  3495. return true;
  3496. }
  3497. if (clanMemberState==CMD_JOIN_REQUESTED)
  3498. {
  3499. resultCode=L2RC_Clans_SendJoinRequest_REQUEST_ALREADY_PENDING;
  3500. return true;
  3501. }
  3502. if (clanMemberState==CMD_JOIN_INVITED)
  3503. {
  3504. resultCode=L2RC_Clans_SendJoinRequest_ALREADY_INVITED;
  3505. return true;
  3506. }
  3507. result = pgsql->QueryVariadic("SELECT requiresInvitationsToJoin FROM lobby2.clans WHERE clanId_pk=%i",clanId);
  3508. bool requiresInvitationsToJoin;
  3509. PostgreSQLInterface::PQGetValueFromBinary(&requiresInvitationsToJoin, result, 0, "requiresInvitationsToJoin");
  3510. PQclear(result);
  3511. if (requiresInvitationsToJoin==false)
  3512. {
  3513. result = pgsql->QueryVariadic(
  3514. "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
  3515. "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') );"
  3516. ,command->callerUserId, clanId);
  3517. PQclear(result);
  3518. // Send notification all members about the new member
  3519. // Notify all members about this new member
  3520. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3521. GetClanMembers(clanId, clanMembers, pgsql);
  3522. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3523. {
  3524. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3525. continue;
  3526. if (clanMembers[i].userId==command->callerUserId)
  3527. continue;
  3528. Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
  3529. notification->clanHandle=clanHandle;
  3530. notification->targetHandle=command->callingUserName;
  3531. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3532. }
  3533. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3534. // Send email to leader
  3535. SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_SendJoinRequest", pgsql);
  3536. clanJoined=true;
  3537. }
  3538. else
  3539. {
  3540. // Add row to lobby2.clanMembers
  3541. result = pgsql->QueryVariadic(
  3542. "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
  3543. "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested') );"
  3544. ,command->callerUserId, clanId);
  3545. PQclear(result);
  3546. // Send notification to leader, subleaders about this invite
  3547. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3548. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3549. GetClanMembers(clanId, clanMembers, pgsql);
  3550. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3551. {
  3552. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3553. continue;
  3554. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3555. continue;
  3556. Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3557. notification->clanHandle=clanHandle;
  3558. notification->targetHandle=clanMembers[i].name;
  3559. notification->sourceHandle=command->callingUserName;
  3560. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
  3561. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
  3562. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3563. }
  3564. }
  3565. resultCode=L2RC_SUCCESS;
  3566. return true;
  3567. }
  3568. bool RakNet::Clans_WithdrawJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3569. {
  3570. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3571. PGresult *result=0;
  3572. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3573. if (clanId==0)
  3574. {
  3575. resultCode=L2RC_Clans_WithdrawJoinRequest_UNKNOWN_CLAN;
  3576. return true;
  3577. }
  3578. bool isSubleader;
  3579. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3580. if (clanMemberState==CMD_ACTIVE)
  3581. {
  3582. resultCode=L2RC_Clans_WithdrawJoinRequest_ALREADY_IN_CLAN;
  3583. return true;
  3584. }
  3585. if (clanMemberState!=CMD_JOIN_REQUESTED)
  3586. {
  3587. resultCode=L2RC_Clans_WithdrawJoinRequest_NO_SUCH_INVITATION_EXISTS;
  3588. return true;
  3589. }
  3590. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
  3591. if (result==0)
  3592. {
  3593. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3594. return true;
  3595. }
  3596. PQclear(result);
  3597. // Send email to leader
  3598. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3599. SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_WithdrawJoinRequest", pgsql);
  3600. // Send notification to leader, subleaders
  3601. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3602. GetClanMembers(clanId, clanMembers, pgsql);
  3603. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3604. {
  3605. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3606. continue;
  3607. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3608. continue;
  3609. Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3610. notification->clanHandle=clanHandle;
  3611. notification->targetHandle=clanMembers[i].name;
  3612. notification->sourceHandle=command->callingUserName;
  3613. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
  3614. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
  3615. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3616. }
  3617. resultCode=L2RC_SUCCESS;
  3618. return true;
  3619. }
  3620. bool RakNet::Clans_AcceptJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3621. {
  3622. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3623. PGresult *result=0;
  3624. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3625. if (clanId==0)
  3626. {
  3627. resultCode=L2RC_Clans_AcceptJoinRequest_UNKNOWN_CLAN;
  3628. return true;
  3629. }
  3630. bool isSubleader;
  3631. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
  3632. if (clanMemberState!=CMD_ACTIVE)
  3633. {
  3634. resultCode=L2RC_Clans_AcceptJoinRequest_NOT_IN_CLAN;
  3635. return true;
  3636. }
  3637. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3638. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3639. {
  3640. resultCode=L2RC_Clans_AcceptJoinRequest_MUST_BE_LEADER_OR_SUBLEADER;
  3641. return true;
  3642. }
  3643. // Does target already have an entry?
  3644. unsigned int targetId = RakNet::GetUserRowFromHandle(requestingUserHandle, pgsql);
  3645. if (targetId==0)
  3646. {
  3647. resultCode=L2RC_Clans_AcceptJoinRequest_UNKNOWN_TARGET_HANDLE;
  3648. return true;
  3649. }
  3650. if (targetId==command->callerUserId)
  3651. {
  3652. resultCode=L2RC_Clans_AcceptJoinRequest_CANNOT_PERFORM_ON_SELF;
  3653. return true;
  3654. }
  3655. bool isTargetSubleader;
  3656. RakNet::ClanMemberState targetClanMemberState = GetClanMemberState(clanId, targetId, &isTargetSubleader, pgsql);
  3657. if (targetClanMemberState==CMD_ACTIVE)
  3658. {
  3659. // active member
  3660. resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_ALREADY_IN_CLAN;
  3661. return true;
  3662. }
  3663. if (targetClanMemberState==CMD_BANNED)
  3664. {
  3665. resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_IS_BANNED;
  3666. return true;
  3667. }
  3668. if (targetClanMemberState!=CMD_JOIN_REQUESTED)
  3669. {
  3670. resultCode=L2RC_Clans_AcceptJoinRequest_REQUEST_NOT_PENDING;
  3671. return true;
  3672. }
  3673. // Change status to clan member
  3674. result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
  3675. PQclear(result);
  3676. // Do AFTER the update in case another thread also added to a clan
  3677. if (failIfAlreadyInClan)
  3678. {
  3679. int count = GetActiveClanCount(targetId, pgsql);
  3680. if (count>1)
  3681. {
  3682. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i;", targetId, clanId);
  3683. PQclear(result);
  3684. resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_ALREADY_IN_DIFFERENT_CLAN;
  3685. return true;
  3686. }
  3687. }
  3688. // Notify all members about this new member
  3689. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3690. GetClanMembers(clanId, clanMembers, pgsql);
  3691. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3692. {
  3693. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3694. continue;
  3695. if (clanMembers[i].userId==command->callerUserId )
  3696. continue;
  3697. Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
  3698. notification->clanHandle=clanHandle;
  3699. notification->targetHandle=requestingUserHandle;
  3700. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
  3701. }
  3702. // Send email to member
  3703. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_AcceptJoinRequest", pgsql);
  3704. resultCode=L2RC_SUCCESS;
  3705. return true;
  3706. }
  3707. bool RakNet::Clans_RejectJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3708. {
  3709. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3710. PGresult *result=0;
  3711. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3712. if (clanId==0)
  3713. {
  3714. resultCode=L2RC_Clans_RejectJoinRequest_UNKNOWN_CLAN;
  3715. return true;
  3716. }
  3717. bool isSubleader;
  3718. if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
  3719. {
  3720. resultCode=L2RC_Clans_RejectJoinRequest_NOT_IN_CLAN;
  3721. return true;
  3722. }
  3723. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3724. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3725. {
  3726. resultCode=L2RC_Clans_RejectJoinRequest_MUST_BE_LEADER_OR_SUBLEADER;
  3727. return true;
  3728. }
  3729. unsigned int targetId = RakNet::GetUserRowFromHandle(requestingUserHandle, pgsql);
  3730. if (targetId==0)
  3731. {
  3732. resultCode=L2RC_Clans_RejectJoinRequest_REQUESTING_USER_HANDLE_UNKNOWN;
  3733. return true;
  3734. }
  3735. if (GetClanMemberState(clanId, targetId, &isSubleader, pgsql)!=CMD_JOIN_REQUESTED)
  3736. {
  3737. resultCode=L2RC_Clans_RejectJoinRequest_NO_SUCH_INVITATION_EXISTS;
  3738. return true;
  3739. }
  3740. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
  3741. PQclear(result);
  3742. // Subleader and leader notification
  3743. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3744. GetClanMembers(clanId, clanMembers, pgsql);
  3745. // requestingUserHandle will be notified too, insert a fake record
  3746. ClanMemberDescriptor cmd;
  3747. cmd.memberState=CMD_ACTIVE;
  3748. cmd.isSubleader=true;
  3749. cmd.userId=targetId;
  3750. clanMembers.Insert(cmd, _FILE_AND_LINE_ );
  3751. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3752. {
  3753. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3754. continue;
  3755. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3756. continue;
  3757. Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
  3758. notification->clanHandle=clanHandle;
  3759. notification->sourceHandle=command->callingUserName;
  3760. notification->targetHandle=requestingUserHandle;
  3761. notification->clanMemberHandle=command->callingUserName;
  3762. notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
  3763. notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_REJECTED;
  3764. notification->resultCode=L2RC_SUCCESS;
  3765. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3766. }
  3767. // Send email to member
  3768. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_RejectJoinRequest", pgsql);
  3769. resultCode=L2RC_SUCCESS;
  3770. return true;
  3771. };
  3772. bool RakNet::Clans_DownloadRequestList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3773. {
  3774. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3775. PGresult *result=0;
  3776. int i;
  3777. // Get all clanMembers that are in state requested of all clans where I am the leader or subleader
  3778. result = pgsql->QueryVariadic(
  3779. "SELECT C.clanHandle, creationDate, U.handle FROM lobby2.clanMembers INNER JOIN "
  3780. "(SELECT clanId_pk as clanId FROM lobby2.clans WHERE leaderUserId_fk=%i "
  3781. "UNION ALL "
  3782. "SELECT clanId_fk as clanId FROM lobby2.clanMembers WHERE isSubleader=TRUE AND userId_fk=%i) AS clansWhereIAmLeaderOrSubleader "
  3783. "ON clansWhereIAmLeaderOrSubleader.clanId=lobby2.clanMembers.clanId_fk "
  3784. "INNER JOIN lobby2.users U ON U.userId_pk = userId_fk "
  3785. "INNER JOIN lobby2.clans C ON C.clanId_pk = clanId "
  3786. "WHERE lobby2.clanMembers.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested');",
  3787. command->callerUserId, command->callerUserId);
  3788. if (result==0)
  3789. {
  3790. PQclear(result);
  3791. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3792. return true;
  3793. }
  3794. for (i=0; i < PQntuples(result); i++)
  3795. {
  3796. ClanJoinRequest cjr;
  3797. PostgreSQLInterface::PQGetValueFromBinary(&cjr.targetClan, result, i, "clanHandle");
  3798. PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
  3799. PostgreSQLInterface::PQGetValueFromBinary(&cjr.joinRequestSender, result, i, "handle");
  3800. joinRequestsToMyClan.Insert(cjr, _FILE_AND_LINE_ );
  3801. }
  3802. PQclear(result);
  3803. // Get all clanMembers where I am in state requested
  3804. result = pgsql->QueryVariadic(
  3805. "SELECT C.clanHandle, M.creationDate FROM lobby2.clanMembers AS M, lobby2.clans AS C WHERE "
  3806. "M.userId_fk=%i AND C.clanId_pk=M.clanId_fk AND M.memberState_fk = "
  3807. "(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested');",
  3808. command->callerUserId);
  3809. if (result==0)
  3810. {
  3811. PQclear(result);
  3812. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  3813. return true;
  3814. }
  3815. for (i=0; i < PQntuples(result); i++)
  3816. {
  3817. ClanJoinRequest cjr;
  3818. PostgreSQLInterface::PQGetValueFromBinary(&cjr.targetClan, result, i, "clanHandle");
  3819. PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
  3820. joinRequestsFromMe.Insert(cjr, _FILE_AND_LINE_ );
  3821. }
  3822. PQclear(result);
  3823. resultCode=L2RC_SUCCESS;
  3824. return true;
  3825. }
  3826. bool RakNet::Clans_KickAndBlacklistUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3827. {
  3828. (void)command;
  3829. (void)databaseInterface;
  3830. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3831. PGresult *result=0;
  3832. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3833. if (clanId==0)
  3834. {
  3835. resultCode=L2RC_Clans_KickAndBlacklistUser_UNKNOWN_CLAN;
  3836. return true;
  3837. }
  3838. bool isSubleader;
  3839. if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
  3840. {
  3841. resultCode=L2RC_Clans_KickAndBlacklistUser_NOT_IN_CLAN;
  3842. return true;
  3843. }
  3844. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3845. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3846. {
  3847. resultCode=L2RC_Clans_KickAndBlacklistUser_MUST_BE_LEADER_OR_SUBLEADER;
  3848. return true;
  3849. }
  3850. unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
  3851. if (targetId==0)
  3852. {
  3853. resultCode=L2RC_Clans_KickAndBlacklistUser_UNKNOWN_TARGET_HANDLE;
  3854. return true;
  3855. }
  3856. if (targetId==command->callerUserId)
  3857. {
  3858. resultCode=L2RC_Clans_KickAndBlacklistUser_CANNOT_PERFORM_ON_SELF;
  3859. return true;
  3860. }
  3861. if (targetId==clanLeaderId)
  3862. {
  3863. resultCode=L2RC_Clans_KickAndBlacklistUser_CANNOT_PERFORM_ON_LEADER;
  3864. return true;
  3865. }
  3866. RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetId, &isSubleader, pgsql);
  3867. if (clanMemberState==CMD_BANNED)
  3868. {
  3869. resultCode=L2RC_Clans_KickAndBlacklistUser_ALREADY_BLACKLISTED;
  3870. return true;
  3871. }
  3872. if (blacklist)
  3873. {
  3874. if (clanMemberState!=CMD_UNDEFINED)
  3875. {
  3876. // Change status to banned
  3877. result = pgsql->QueryVariadic(
  3878. "UPDATE lobby2.clanMembers "
  3879. "SET banReason=%s, memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned') "
  3880. "WHERE userId_fk=%i AND clanId_fk=%i;", reason.C_String(), targetId, clanId);
  3881. }
  3882. else
  3883. {
  3884. // Add row as banned
  3885. result = pgsql->QueryVariadic(
  3886. "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk, banReason) VALUES "
  3887. "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned'), %s );"
  3888. , targetId, clanId, reason.C_String());
  3889. }
  3890. PQclear(result);
  3891. }
  3892. else if (kick)
  3893. {
  3894. // Remove from the clanMembers table
  3895. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
  3896. PQclear(result);
  3897. }
  3898. // Subleader and leader notification
  3899. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3900. GetClanMembers(clanId, clanMembers, pgsql);
  3901. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3902. {
  3903. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3904. continue;
  3905. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3906. continue;
  3907. if (clanMembers[i].userId==targetId)
  3908. continue;
  3909. Notification_Clans_KickAndBlacklistUser* notification = (Notification_Clans_KickAndBlacklistUser*) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_KickAndBlacklistUser);
  3910. notification->clanHandle=clanHandle;
  3911. notification->targetHandle=targetHandle;
  3912. notification->blacklistingUserHandle=command->callingUserName;
  3913. notification->targetHandleWasKicked=(clanMemberState!=CMD_UNDEFINED);
  3914. notification->reason=reason;
  3915. notification->resultCode=L2RC_SUCCESS;
  3916. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3917. }
  3918. // If user was in the clan, send Notification_Clans_Leave to them.
  3919. if (clanMemberState!=CMD_UNDEFINED)
  3920. {
  3921. Notification_Clans_Leave *notification = (Notification_Clans_Leave *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Leave);
  3922. notification->clanHandle=clanHandle;
  3923. notification->targetHandle=targetHandle;
  3924. notification->resultCode=L2RC_SUCCESS;
  3925. command->server->AddOutputFromThread(notification, targetId, targetHandle);
  3926. }
  3927. // Send email to member
  3928. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_KickAndBlacklistUser", pgsql);
  3929. resultCode=L2RC_SUCCESS;
  3930. return true;
  3931. }
  3932. bool RakNet::Clans_UnblacklistUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3933. {
  3934. (void)command;
  3935. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3936. PGresult *result=0;
  3937. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3938. if (clanId==0)
  3939. {
  3940. resultCode=L2RC_Clans_UnblacklistUser_UNKNOWN_CLAN;
  3941. return true;
  3942. }
  3943. bool isSubleader;
  3944. if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
  3945. {
  3946. resultCode=L2RC_Clans_UnblacklistUser_NOT_IN_CLAN;
  3947. return true;
  3948. }
  3949. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  3950. if (isSubleader==false && clanLeaderId!=command->callerUserId)
  3951. {
  3952. resultCode=L2RC_Clans_UnblacklistUser_MUST_BE_LEADER_OR_SUBLEADER;
  3953. return true;
  3954. }
  3955. unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
  3956. if (targetId==0)
  3957. {
  3958. resultCode=L2RC_Clans_UnblacklistUser_UNKNOWN_TARGET_HANDLE;
  3959. return true;
  3960. }
  3961. if (GetClanMemberState(clanId, targetId, &isSubleader, pgsql)!=CMD_BANNED)
  3962. {
  3963. resultCode=L2RC_Clans_UnblacklistUser_NOT_BLACKLISTED;
  3964. return true;
  3965. }
  3966. // Remove from the clanMembers table
  3967. result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
  3968. PQclear(result);
  3969. // Subleader and leader notification
  3970. DataStructures::List<ClanMemberDescriptor> clanMembers;
  3971. GetClanMembers(clanId, clanMembers, pgsql);
  3972. for (unsigned int i=0; i < clanMembers.Size(); i++)
  3973. {
  3974. if (clanMembers[i].memberState!=CMD_ACTIVE)
  3975. continue;
  3976. if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
  3977. continue;
  3978. Notification_Clans_UnblacklistUser *notification = (Notification_Clans_UnblacklistUser *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_UnblacklistUser);
  3979. notification->clanHandle=clanHandle;
  3980. notification->targetHandle=targetHandle;
  3981. notification->unblacklistingUserHandle=command->callingUserName;
  3982. notification->resultCode=L2RC_SUCCESS;
  3983. command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
  3984. }
  3985. // Send email to member
  3986. SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_UnblacklistUser", pgsql);
  3987. resultCode=L2RC_SUCCESS;
  3988. return true;
  3989. }
  3990. bool RakNet::Clans_GetBlacklist_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  3991. {
  3992. (void)command;
  3993. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  3994. PGresult *result=0;
  3995. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  3996. if (clanId==0)
  3997. {
  3998. resultCode=L2RC_Clans_GetBlacklist_UNKNOWN_CLAN;
  3999. return true;
  4000. }
  4001. // Get the names of all banned members in this clan
  4002. result = pgsql->QueryVariadic(
  4003. "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
  4004. "WHERE M.clanId_fk=%i AND M.userId_fk=U.userId_pk "
  4005. "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned');",
  4006. clanId );
  4007. if (result==0)
  4008. {
  4009. PQclear(result);
  4010. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  4011. return true;
  4012. }
  4013. RakNet::RakString memberName;
  4014. for (int i=0; i < PQntuples(result); i++)
  4015. {
  4016. PostgreSQLInterface::PQGetValueFromBinary(&memberName, result, i, "handle");
  4017. blacklistedUsers.Insert(memberName, _FILE_AND_LINE_ );
  4018. }
  4019. PQclear(result);
  4020. resultCode=L2RC_SUCCESS;
  4021. return true;
  4022. }
  4023. bool RakNet::Clans_GetMembers_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  4024. {
  4025. (void)command;
  4026. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  4027. PGresult *result=0;
  4028. unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
  4029. if (clanId==0)
  4030. {
  4031. resultCode=L2RC_Clans_GetMembers_UNKNOWN_CLAN;
  4032. return true;
  4033. }
  4034. unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
  4035. result = pgsql->QueryVariadic("SELECT handle from lobby2.users WHERE userId_pk=%i", clanLeaderId);
  4036. if (result==0)
  4037. {
  4038. PQclear(result);
  4039. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  4040. return true;
  4041. }
  4042. if (PQntuples(result)!=0)
  4043. {
  4044. PostgreSQLInterface::PQGetValueFromBinary(&clanLeader, result, 0, "handle");
  4045. }
  4046. PQclear(result);
  4047. // Get the names of all other active members in this clan
  4048. result = pgsql->QueryVariadic(
  4049. "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
  4050. "WHERE M.clanId_fk=%i AND M.userId_fk!=%i AND M.userId_fk=U.userId_pk "
  4051. "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active');",
  4052. clanId, clanLeaderId);
  4053. if (result==0)
  4054. {
  4055. PQclear(result);
  4056. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  4057. return true;
  4058. }
  4059. RakNet::RakString memberName;
  4060. for (int i=0; i < PQntuples(result); i++)
  4061. {
  4062. PostgreSQLInterface::PQGetValueFromBinary(&memberName, result, i, "handle");
  4063. clanMembersOtherThanLeader.Insert(memberName, _FILE_AND_LINE_ );
  4064. }
  4065. PQclear(result);
  4066. resultCode=L2RC_SUCCESS;
  4067. return true;
  4068. }
  4069. bool RakNet::Clans_GetList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
  4070. {
  4071. (void)command;
  4072. PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
  4073. PGresult *result = pgsql->QueryVariadic("SELECT clanhandle from lobby2.clans");
  4074. if (result==0)
  4075. {
  4076. resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
  4077. return true;
  4078. }
  4079. RakNet::RakString clanName;
  4080. for (int i=0; i < PQntuples(result); i++)
  4081. {
  4082. PostgreSQLInterface::PQGetValueFromBinary(&clanName, result, i, "clanhandle");
  4083. clanNames.Insert(clanName, _FILE_AND_LINE_ );
  4084. }
  4085. PQclear(result);
  4086. resultCode=L2RC_SUCCESS;
  4087. return true;
  4088. }
粤ICP备19079148号