llimmgr.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594
  1. /**
  2. * @file llimmgr.cpp
  3. * @brief Instant Messaging management
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "boost/tokenizer.hpp"
  34. #include "llimmgr.h"
  35. #include "llcachename.h"
  36. #include "llcorehttputil.h"
  37. #include "llfloater.h"
  38. #include "llhttpnode.h"
  39. #include "llnotifications.h"
  40. #include "llsdserialize.h" // For LLSDSerialize::toPrettyXML()
  41. #include "llsdutil_math.h" // For ll_vector3_from_sd()
  42. #include "lltabcontainer.h"
  43. #include "lltrans.h"
  44. #include "lluistring.h"
  45. #include "llwindow.h"
  46. #include "llagent.h"
  47. #include "llappviewer.h" // For gFrameTimeSeconds and gDisconnected
  48. #include "llavatartracker.h"
  49. #include "llchat.h"
  50. #include "llfloaterchat.h"
  51. #include "llfloaterchatterbox.h"
  52. #include "llfloatergroupinfo.h"
  53. #include "llfloaterim.h"
  54. #include "llfloaternewim.h"
  55. #include "llinventorymodel.h"
  56. #include "llmutelist.h"
  57. #include "lloverlaybar.h"
  58. //MK
  59. #include "mkrlinterface.h"
  60. //mk
  61. #include "hbviewerautomation.h"
  62. #include "llviewercontrol.h"
  63. #include "llviewermenu.h"
  64. #include "llviewermessage.h"
  65. #include "llviewerobjectlist.h"
  66. #include "llviewerregion.h"
  67. #include "llvoavatarself.h"
  68. #include "llvoicechannel.h"
  69. LLIMMgr* gIMMgrp = NULL;
  70. // This name is used by (and reserved for) the menus: floater_im.xml,
  71. // floater_instant_message.xml, floater_instant_message_group.xml and
  72. // floater_instant_message_ad_hoc.xml. If you change it here, change it
  73. // there ! HB
  74. const std::string gIMFloaterName = "im session";
  75. typedef boost::tokenizer<boost::char_separator<char> > tok_t;
  76. static const boost::char_separator<char> sSeparators("|", "",
  77. boost::keep_empty_tokens);
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Friendship offer callback (was formerly in llviewermessage.cpp, but since
  80. // the OfferFriendship and OfferFriendshipNoMessage notifications are initiated
  81. // from here, it makes more sense to keep the corresponding callback here too).
  82. ///////////////////////////////////////////////////////////////////////////////
  83. bool accept_friendship_udp(const LLSD& payload)
  84. {
  85. LLMessageSystem* msg = gMessageSystemp;
  86. if (!msg) // Went offline ?
  87. {
  88. return false;
  89. }
  90. LL_DEBUGS("InstantMessaging") << "Accepting friendship offer via UDP messaging"
  91. << LL_ENDL;
  92. LLAvatarTracker::formFriendship(payload["from_id"]);
  93. const LLUUID& fid =
  94. gInventory.findCategoryUUIDForType(LLFolderType::FT_CALLINGCARD);
  95. // This will also trigger an onlinenotification if the user is online
  96. msg->newMessageFast(_PREHASH_AcceptFriendship);
  97. msg->nextBlockFast(_PREHASH_AgentData);
  98. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  99. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  100. msg->nextBlockFast(_PREHASH_TransactionBlock);
  101. msg->addUUIDFast(_PREHASH_TransactionID, payload["session_id"]);
  102. msg->nextBlockFast(_PREHASH_FolderData);
  103. msg->addUUIDFast(_PREHASH_FolderID, fid);
  104. msg->sendReliable(LLHost(payload["sender"].asString()));
  105. return true;
  106. }
  107. bool decline_friendship_udp(const LLSD& payload)
  108. {
  109. LLMessageSystem* msg = gMessageSystemp;
  110. if (!msg) // Went offline ?
  111. {
  112. return false;
  113. }
  114. LL_DEBUGS("InstantMessaging") << "Declining friendship offer via UDP messaging"
  115. << LL_ENDL;
  116. // We no longer notify other viewers, but we DO still send the rejection to
  117. // the simulator to delete the pending userop.
  118. msg->newMessageFast(_PREHASH_DeclineFriendship);
  119. msg->nextBlockFast(_PREHASH_AgentData);
  120. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  121. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  122. msg->nextBlockFast(_PREHASH_TransactionBlock);
  123. msg->addUUIDFast(_PREHASH_TransactionID, payload["session_id"]);
  124. msg->sendReliable(LLHost(payload["sender"].asString()));
  125. return true;
  126. }
  127. void accept_friendship_coro(std::string url, LLSD payload)
  128. {
  129. LL_DEBUGS("InstantMessaging") << "Accepting friendship offer via capability"
  130. << LL_ENDL;
  131. url += "?from=" + payload["from_id"].asString() + "&agent_name=\"" +
  132. LLURI::escape(gAgentAvatarp->getFullname(true)) + "\"";
  133. LLSD data;
  134. LLCoreHttpUtil::HttpCoroutineAdapter adapter("AcceptFriendshipOffer");
  135. LLSD result = adapter.postAndSuspend(url, data);
  136. LLCore::HttpStatus status =
  137. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  138. if (!status || !result.has("success") || !result["success"].asBoolean())
  139. {
  140. llwarns << "Error accepting frienship offer via capability. Error: "
  141. << status.toString() << llendl;
  142. if (accept_friendship_udp(payload))
  143. {
  144. llinfos << "Sent frienship acceptance via legacy UDP messaging"
  145. << llendl;
  146. }
  147. else
  148. {
  149. llwarns << "Failed to send frienship acceptance via legacy UDP messaging"
  150. << llendl;
  151. }
  152. return;
  153. }
  154. LLAvatarTracker::formFriendship(payload["from_id"]);
  155. }
  156. void decline_friendship_coro(std::string url, LLSD payload)
  157. {
  158. LL_DEBUGS("InstantMessaging") << "Declining friendship offer via capability"
  159. << LL_ENDL;
  160. url += "?from=" + payload["from_id"].asString();
  161. LLCoreHttpUtil::HttpCoroutineAdapter adapter("DeclineFriendshipOffer");
  162. LLSD result = adapter.deleteAndSuspend(url);
  163. LLCore::HttpStatus status =
  164. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  165. if (!status || !result.has("success") || !result["success"].asBoolean())
  166. {
  167. llwarns << "Error declining frienship offer via capability. Error: "
  168. << status.toString() << llendl;
  169. if (decline_friendship_udp(payload))
  170. {
  171. llinfos << "Sent frienship declining via legacy UDP messaging"
  172. << llendl;
  173. }
  174. else
  175. {
  176. llwarns << "Failed to send frienship declining via legacy UDP messaging"
  177. << llendl;
  178. }
  179. }
  180. }
  181. bool friendship_offer_callback(const LLSD& notification, const LLSD& response)
  182. {
  183. const LLSD& payload = notification["payload"];
  184. bool online = payload.has("online") && payload["online"].asBoolean();
  185. S32 option = LLNotification::getSelectedOption(notification, response);
  186. if (option == 0) // Accept
  187. {
  188. const std::string& url =
  189. gAgent.getRegionCapability("AcceptFriendship");
  190. if (url.empty() || online)
  191. {
  192. accept_friendship_udp(payload);
  193. return false;
  194. }
  195. gCoros.launch("acceptFriendshipOffer",
  196. boost::bind(&accept_friendship_coro, url, payload));
  197. }
  198. else if (option == 1) // Decline
  199. {
  200. const std::string& url =
  201. gAgent.getRegionCapability("DeclineFriendship");
  202. if (url.empty() || online)
  203. {
  204. decline_friendship_udp(payload);
  205. return false;
  206. }
  207. gCoros.launch("declineFriendshipOffer",
  208. boost::bind(&decline_friendship_coro, url, payload));
  209. }
  210. return false;
  211. }
  212. static LLNotificationFunctorRegistration friend_offer_cb_reg("OfferFriendship",
  213. friendship_offer_callback);
  214. static LLNotificationFunctorRegistration friend_offer_nm_cb_reg("OfferFriendshipNoMessage",
  215. friendship_offer_callback);
  216. ///////////////////////////////////////////////////////////////////////////////
  217. // LLIMMgrFriendObserver class
  218. // Bridge to suport knowing when the friends list has changed.
  219. ///////////////////////////////////////////////////////////////////////////////
  220. class LLIMMgrFriendObserver final : public LLFriendObserver
  221. {
  222. public:
  223. LLIMMgrFriendObserver()
  224. {
  225. }
  226. ~LLIMMgrFriendObserver() override
  227. {
  228. }
  229. void changed(U32 mask) override
  230. {
  231. if (gIMMgrp &&
  232. (mask & (LLFriendObserver::ADD | LLFriendObserver::REMOVE |
  233. LLFriendObserver::ONLINE)) != 0)
  234. {
  235. gIMMgrp->refresh();
  236. }
  237. }
  238. };
  239. ///////////////////////////////////////////////////////////////////////////////
  240. // LLIMMgr class
  241. ///////////////////////////////////////////////////////////////////////////////
  242. LLIMMgr::LLIMMgr()
  243. : mIMsReceived(0),
  244. mPrivateIMReceived(false)
  245. {
  246. llassert_always(gIMMgrp == NULL); // Only one instance allowed
  247. mFriendObserver = new LLIMMgrFriendObserver();
  248. gAvatarTracker.addObserver(mFriendObserver);
  249. // *HACK: use floater to initialize string constants from xml file then
  250. // delete it right away
  251. LLFloaterIM* dummy_floaterp = new LLFloaterIM();
  252. delete dummy_floaterp;
  253. mPendingInvitations = LLSD::emptyMap();
  254. mPendingAgentListUpdates = LLSD::emptyMap();
  255. gIMMgrp = this;
  256. }
  257. LLIMMgr::~LLIMMgr()
  258. {
  259. gAvatarTracker.removeObserver(mFriendObserver);
  260. delete mFriendObserver;
  261. gIMMgrp = NULL;
  262. }
  263. // NOTE: the other_participant_id is either an agent_id, a group_id, or an
  264. // inventory folder item_id (collection of calling cards)
  265. //static
  266. LLUUID LLIMMgr::computeSessionID(EInstantMessage dialog,
  267. const LLUUID& other_participant_id)
  268. {
  269. LLUUID session_id;
  270. if (dialog == IM_SESSION_GROUP_START || dialog == IM_SESSION_INVITE)
  271. {
  272. // Slam group session_id to the group_id (other_participant_id)
  273. // or the provided session id for invites (which includes group
  274. // session invites).
  275. session_id = other_participant_id;
  276. }
  277. else if (dialog == IM_SESSION_CONFERENCE_START)
  278. {
  279. session_id.generate();
  280. }
  281. else
  282. {
  283. LLUUID agent_id = gAgentID;
  284. if (other_participant_id == agent_id)
  285. {
  286. // If we try to send an IM to ourselves then the XOR would be null
  287. // so we just make the session_id the same as the agent_id
  288. session_id = agent_id;
  289. }
  290. else
  291. {
  292. // Peer-to-peer or peer-to-asset session_id is the XOR
  293. session_id = other_participant_id ^ agent_id;
  294. }
  295. }
  296. return session_id;
  297. }
  298. //static
  299. void LLIMMgr::chatterBoxInvitationCoro(const std::string& url,
  300. LLUUID session_id,
  301. LLIMMgr::EInvitationType type)
  302. {
  303. LLSD data;
  304. data["method"] = "accept invitation";
  305. data["session-id"] = session_id;
  306. LLSD params;
  307. params["voice_server_type"] = gSavedSettings.getString("VoiceServerType");
  308. data["alt_params"] = params;
  309. LLCoreHttpUtil::HttpCoroutineAdapter adapter("ChatterBoxInvitation");
  310. LLSD result = adapter.postAndSuspend(url, data);
  311. if (!gIMMgrp) return; // Viewer is closing down !
  312. gIMMgrp->clearPendingAgentListUpdates(session_id);
  313. gIMMgrp->clearPendingInvitation(session_id);
  314. LLFloaterIMSession* floaterp =
  315. LLFloaterIMSession::findInstance(session_id);
  316. if (!floaterp)
  317. {
  318. llinfos << "Received a reply for closed session Id: " << session_id
  319. << ". Ignored." << llendl;
  320. return;
  321. }
  322. LLCore::HttpStatus status =
  323. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  324. if (!status)
  325. {
  326. llwarns << "Failed to start session Id: " << session_id
  327. << ". Error: " << status.toString() << llendl;
  328. if (status == gStatusNotFound)
  329. {
  330. floaterp->showSessionStartError("does not exist");
  331. }
  332. return;
  333. }
  334. // We have accepted our invitation and received a list of agents that were
  335. // currently in the session when the reply was sent to us. Now, it is
  336. // possible that there were some agents to slip in/out between when that
  337. // message was sent to us and now.
  338. // The agent list updates we have received have been accurate from the time
  339. // we were added to the session but unfortunately our base that we are
  340. // receiving here may not be the most up to date. It was accurate at some
  341. // point in time though.
  342. result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS);
  343. floaterp->setSpeakers(result);
  344. // We now have our base of users in the session that was accurate at some
  345. // point, but maybe not now so now we apply all of the udpates we have
  346. // received in case of race conditions
  347. floaterp->updateSpeakersList(gIMMgrp->getPendingAgentListUpdates(session_id));
  348. if (type == LLIMMgr::INVITATION_TYPE_VOICE)
  349. {
  350. floaterp->requestAutoConnect();
  351. LLFloaterIMSession::onClickStartCall(floaterp);
  352. // Always open IM window when connecting to voice
  353. LLFloaterChatterBox::showInstance(true);
  354. }
  355. else if (type == LLIMMgr::INVITATION_TYPE_IMMEDIATE)
  356. {
  357. LLFloaterChatterBox::showInstance(true);
  358. }
  359. }
  360. //static
  361. bool LLIMMgr::inviteUserResponse(const LLSD& notification,
  362. const LLSD& response)
  363. {
  364. if (!gIMMgrp) return false;
  365. LLSD payload = notification["payload"];
  366. LLUUID session_id = payload["session_id"].asUUID();
  367. EInstantMessage type = (EInstantMessage)payload["type"].asInteger();
  368. LLIMMgr::EInvitationType inv_type =
  369. (LLIMMgr::EInvitationType)payload["inv_type"].asInteger();
  370. std::string session_name = payload["session_name"].asString();
  371. S32 option = LLNotification::getSelectedOption(notification, response);
  372. switch (option)
  373. {
  374. case 0: // Accept
  375. {
  376. if (type == IM_SESSION_P2P_INVITE)
  377. {
  378. if (payload.has("voice_channel_info"))
  379. {
  380. LLSD& chan_info = payload["voice_channel_info"];
  381. if (chan_info.isMap() && chan_info.size() > 0)
  382. {
  383. chan_info["incoming"] = true;
  384. }
  385. }
  386. if (session_name.empty() && payload.has("caller_name"))
  387. {
  388. session_name = payload["caller_name"].asString();
  389. }
  390. // Create a normal IM session
  391. session_id =
  392. gIMMgrp->addP2PSession(session_name,
  393. payload["caller_id"].asUUID(),
  394. payload["voice_channel_info"]);
  395. LLFloaterIMSession* floaterp =
  396. LLFloaterIMSession::findInstance(session_id);
  397. if (floaterp)
  398. {
  399. floaterp->requestAutoConnect();
  400. LLFloaterIMSession::onClickStartCall(floaterp);
  401. // Always open IM window when connecting to voice
  402. LLFloaterChatterBox::showInstance(session_id);
  403. }
  404. gIMMgrp->clearPendingAgentListUpdates(session_id);
  405. gIMMgrp->clearPendingInvitation(session_id);
  406. }
  407. else
  408. {
  409. const std::string& url =
  410. gAgent.getRegionCapability("ChatSessionRequest");
  411. if (!url.empty())
  412. {
  413. if (inv_type == INVITATION_TYPE_VOICE &&
  414. payload.has("voice_channel_info"))
  415. {
  416. LLSD& chan_info = payload["voice_channel_info"];
  417. if (chan_info.isMap() && chan_info.size() > 0)
  418. {
  419. chan_info["incoming"] = true;
  420. }
  421. }
  422. gIMMgrp->addSession(session_name, type, session_id,
  423. payload["voice_channel_info"]);
  424. gCoros.launch("chatterBoxInvitationCoro",
  425. boost::bind(&LLIMMgr::chatterBoxInvitationCoro,
  426. url, session_id, inv_type));
  427. }
  428. }
  429. break;
  430. }
  431. // Mute (also implies ignore, so this falls through to the "ignore" case
  432. // below)
  433. case 2: // Mute
  434. {
  435. // Mute the sender of this invite
  436. if (!LLMuteList::isMuted(payload["caller_id"].asUUID()))
  437. {
  438. LLMute mute(payload["caller_id"].asUUID(),
  439. payload["caller_name"].asString(), LLMute::AGENT);
  440. LLMuteList::add(mute);
  441. }
  442. // FALLTHROUGH to decline
  443. }
  444. case 1: // Decline
  445. {
  446. static const std::string declined = "Invitation declined.";
  447. static const std::string failed =
  448. "Failed to send decline invitation message.";
  449. using namespace LLCoreHttpUtil;
  450. if (type == IM_SESSION_P2P_INVITE)
  451. {
  452. U32 server_type =
  453. gVoiceClient.getVoiceServerType(payload["voice_channel_info"]);
  454. if (server_type == LLVoiceClient::VIVOX_SERVER)
  455. {
  456. gVoiceClient.declineInvite(payload["voice_channel_info"]);
  457. }
  458. else if (server_type == LLVoiceClient::WEBRTC_SERVER)
  459. {
  460. const std::string& url =
  461. gAgent.getRegionCapability("ChatSessionRequest");
  462. if (!url.empty())
  463. {
  464. LLSD data;
  465. data["method"] = "decline p2p voice";
  466. data["session-id"] = session_id;
  467. HttpCoroutineAdapter::messageHttpPost(url, data, declined,
  468. failed);
  469. }
  470. }
  471. }
  472. else
  473. {
  474. const std::string& url =
  475. gAgent.getRegionCapability("ChatSessionRequest");
  476. if (!url.empty())
  477. {
  478. LLSD data;
  479. data["method"] = "decline invitation";
  480. data["session-id"] = session_id;
  481. HttpCoroutineAdapter::messageHttpPost(url, data, declined,
  482. failed);
  483. }
  484. }
  485. gIMMgrp->clearPendingAgentListUpdates(session_id);
  486. gIMMgrp->clearPendingInvitation(session_id);
  487. }
  488. }
  489. return false;
  490. }
  491. // Helper function
  492. void session_starter_helper(const LLUUID& temp_session_id,
  493. const LLUUID& other_participant_id,
  494. EInstantMessage im_type)
  495. {
  496. LLMessageSystem* msg = gMessageSystemp;
  497. msg->newMessageFast(_PREHASH_ImprovedInstantMessage);
  498. msg->nextBlockFast(_PREHASH_AgentData);
  499. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  500. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  501. msg->nextBlockFast(_PREHASH_MessageBlock);
  502. msg->addBoolFast(_PREHASH_FromGroup, false);
  503. msg->addUUIDFast(_PREHASH_ToAgentID, other_participant_id);
  504. msg->addU8Fast(_PREHASH_Offline, IM_ONLINE);
  505. msg->addU8Fast(_PREHASH_Dialog, im_type);
  506. msg->addUUIDFast(_PREHASH_ID, temp_session_id);
  507. // No timestamp necessary
  508. msg->addU32Fast(_PREHASH_Timestamp, NO_TIMESTAMP);
  509. std::string name;
  510. gAgent.buildFullname(name);
  511. msg->addStringFast(_PREHASH_FromAgentName, name);
  512. msg->addStringFast(_PREHASH_Message, LLStringUtil::null);
  513. msg->addU32Fast(_PREHASH_ParentEstateID, 0);
  514. msg->addUUIDFast(_PREHASH_RegionID, LLUUID::null);
  515. msg->addVector3Fast(_PREHASH_Position, gAgent.getPositionAgent());
  516. }
  517. //static
  518. void LLIMMgr::startConferenceCoro(const std::string& url,
  519. LLUUID temp_session_id, LLUUID creator_id,
  520. LLUUID other_participant_id, LLSD agents)
  521. {
  522. LLSD data;
  523. data["method"] = "start conference";
  524. data["session-id"] = temp_session_id;
  525. data["params"] = agents;
  526. LLSD params;
  527. params["voice_server_type"] = gSavedSettings.getString("VoiceServerType");
  528. data["alt_params"] = params;
  529. LLCoreHttpUtil::HttpCoroutineAdapter adapter("StartConference");
  530. LLSD result = adapter.postAndSuspend(url, data);
  531. LLCore::HttpStatus status =
  532. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  533. if (!status)
  534. {
  535. if (status == gStatusBadRequest)
  536. {
  537. startDeprecatedConference(temp_session_id, creator_id,
  538. other_participant_id, agents);
  539. }
  540. else
  541. {
  542. // Throw an error back to the client ?
  543. // In theory we should have just have these error strings set up in
  544. // this file as opposed to the IMMgr, but the error string were
  545. // unneeded here previously and it is not worth the effort
  546. // switching over all the possible different language translations.
  547. llwarns << "Failed to start conference: " << status.toString()
  548. << llendl;
  549. }
  550. }
  551. }
  552. //static
  553. void LLIMMgr::startP2PVoiceCoro(const std::string& url, LLUUID temp_session_id,
  554. LLUUID creator_id, LLUUID other_participant_id)
  555. {
  556. LLSD data;
  557. data["method"] = "start p2p voice";
  558. data["session-id"] = temp_session_id;
  559. data["params"] = other_participant_id;
  560. LLSD params;
  561. params["voice_server_type"] = gSavedSettings.getString("VoiceServerType");
  562. data["alt_params"] = params;
  563. LLCoreHttpUtil::HttpCoroutineAdapter adapter("startP2PVoiceCoro");
  564. LLSD result = adapter.postAndSuspend(url, data);
  565. LLCore::HttpStatus status =
  566. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  567. if (!status)
  568. {
  569. llwarns << "Failed to start conference: " << status.toString()
  570. << llendl;
  571. if (status == gStatusBadRequest)
  572. {
  573. // Throw an error dialog and close the temp session's floater
  574. LLFloaterIMSession* floaterp =
  575. LLFloaterIMSession::findInstance(temp_session_id);
  576. if (floaterp)
  577. {
  578. floaterp->showSessionStartError("does not exist");
  579. }
  580. }
  581. }
  582. }
  583. // Returns true if any messages were sent, false otherwise. Is sort of
  584. // equivalent to "does the server need to do anything ?"
  585. //static
  586. bool LLIMMgr::sendStartSessionMessages(const LLUUID& temp_session_id,
  587. const LLUUID& other_participant_id,
  588. const uuid_vec_t& ids,
  589. EInstantMessage dialog, bool use_p2p)
  590. {
  591. if (dialog == IM_SESSION_GROUP_START)
  592. {
  593. session_starter_helper(temp_session_id, other_participant_id, dialog);
  594. gMessageSystemp->addBinaryDataFast(_PREHASH_BinaryBucket,
  595. EMPTY_BINARY_BUCKET,
  596. EMPTY_BINARY_BUCKET_SIZE);
  597. gAgent.sendReliableMessage();
  598. return true;
  599. }
  600. // We have a new way of starting conference calls now
  601. const std::string& url = gAgent.getRegionCapability("ChatSessionRequest");
  602. if (dialog == IM_SESSION_CONFERENCE_START)
  603. {
  604. LLSD agents;
  605. for (S32 i = 0, count = ids.size(); i < count; ++i)
  606. {
  607. agents.append(ids[i]);
  608. }
  609. if (url.empty())
  610. {
  611. startDeprecatedConference(temp_session_id, gAgentID,
  612. other_participant_id, agents);
  613. }
  614. else
  615. {
  616. gCoros.launch("startConferenceCoro",
  617. boost::bind(&LLIMMgr::startConferenceCoro, url,
  618. temp_session_id, gAgentID,
  619. other_participant_id, agents));
  620. }
  621. // We need to wait for the server reply; in case of ad-hoc chat, we
  622. // will get a new session Id.
  623. return true;
  624. }
  625. if (!use_p2p)
  626. {
  627. // Nothing to ask or tell to the server. HB
  628. return false;
  629. }
  630. if (url.empty())
  631. {
  632. llwarns << "Missing ChatSessionRequest capability: cannot open sesion."
  633. << llendl;
  634. }
  635. else
  636. {
  637. gCoros.launch("startP2PVoiceCoro",
  638. boost::bind(&LLIMMgr::startP2PVoiceCoro, url,
  639. temp_session_id, gAgentID,
  640. other_participant_id));
  641. }
  642. return true;
  643. }
  644. //static
  645. void LLIMMgr::startDeprecatedConference(const LLUUID& temp_session_id,
  646. const LLUUID& creator_id,
  647. const LLUUID& other_participant_id,
  648. const LLSD& agents_to_invite)
  649. {
  650. // This method is also called on return of coroutines, and the viewer could
  651. // be closing down when it happens...
  652. if (!gMessageSystemp)
  653. {
  654. return;
  655. }
  656. S32 count = agents_to_invite.size();
  657. if (count == 0)
  658. {
  659. return; // No one to invite...
  660. }
  661. S32 bucket_size = UUID_BYTES * count;
  662. U8* bucket = new U8[bucket_size];
  663. // *FIX: this could suffer from endian issues
  664. U8* pos = bucket;
  665. LLUUID agent_id;
  666. for (S32 i = 0; i < count; ++i)
  667. {
  668. agent_id = agents_to_invite[i].asUUID();
  669. memcpy(pos, &agent_id, UUID_BYTES);
  670. pos += UUID_BYTES;
  671. }
  672. session_starter_helper(temp_session_id, other_participant_id,
  673. IM_SESSION_CONFERENCE_START);
  674. gMessageSystemp->addBinaryDataFast(_PREHASH_BinaryBucket, bucket,
  675. bucket_size);
  676. gAgent.sendReliableMessage();
  677. delete[] bucket;
  678. }
  679. // This is a helper function to determine what kind of IM session should be
  680. // used for the given agent.
  681. //static
  682. EInstantMessage LLIMMgr::defaultIMTypeForAgent(const LLUUID& agent_id)
  683. {
  684. EInstantMessage type = IM_NOTHING_SPECIAL;
  685. if (LLAvatarTracker::isAgentFriend(agent_id))
  686. {
  687. if (gAvatarTracker.isBuddyOnline(agent_id))
  688. {
  689. type = IM_SESSION_CONFERENCE_START;
  690. }
  691. }
  692. return type;
  693. }
  694. //static
  695. void LLIMMgr::toggle(void*)
  696. {
  697. // Hide the button and show the floater or vice versa.
  698. if (gIMMgrp)
  699. {
  700. gIMMgrp->setFloaterOpen(!gIMMgrp->getFloaterOpen());
  701. }
  702. }
  703. // Helper function
  704. static void get_extended_text_color(const LLUUID& session_id,
  705. const LLUUID& other_participant_id,
  706. const std::string& msg,
  707. LLColor4& color)
  708. {
  709. if (other_participant_id.notNull() &&
  710. gSavedSettings.getBool("HighlightOwnNameInIM"))
  711. {
  712. for (std::vector<LLGroupData>::iterator i = gAgent.mGroups.begin(),
  713. end = gAgent.mGroups.end();
  714. i != end; ++i)
  715. {
  716. if (i->mID == session_id)
  717. {
  718. if (LLFloaterChat::isOwnNameInText(msg))
  719. {
  720. color = gSavedSettings.getColor4("OwnNameChatColor");
  721. }
  722. break;
  723. }
  724. }
  725. }
  726. }
  727. // Add a message to a session.
  728. void LLIMMgr::addMessage(const LLUUID& session_id, const LLUUID& target_id,
  729. const std::string& from, const std::string& msg,
  730. const std::string& session_name,
  731. EInstantMessage dialog, U32 parent_estate_id,
  732. const LLUUID& region_id, const LLVector3& position,
  733. bool link_name)
  734. {
  735. LLUUID other_participant_id = target_id;
  736. bool private_im = from != SYSTEM_FROM &&
  737. !gAgent.isInGroup(session_id, true);
  738. // Replace interactive system message marker with correct from string value
  739. std::string from_name = from;
  740. if (from == INCOMING_IM)
  741. {
  742. from_name = SYSTEM_FROM;
  743. }
  744. else if (from == INTERACTIVE_SYSTEM_FROM)
  745. {
  746. from_name = SYSTEM_FROM;
  747. private_im = false;
  748. }
  749. // Do not process muted IMs
  750. if (LLMuteList::isMuted(other_participant_id, LLMute::flagTextChat) &&
  751. !LLMuteList::isLinden(from_name))
  752. {
  753. return;
  754. }
  755. if (session_id.notNull() &&
  756. LLMuteList::isMuted(session_id, LLMute::flagTextChat))
  757. {
  758. // Muted group
  759. return;
  760. }
  761. size_t i = session_name.find(" Conference");
  762. if (i != std::string::npos)
  763. {
  764. std::string initiator = session_name.substr(0, i);
  765. if (LLMuteList::isMuted(LLUUID::null, initiator, LLMute::flagTextChat,
  766. LLMute::AGENT))
  767. {
  768. // Conference initiated by a muted agent
  769. return;
  770. }
  771. }
  772. #if 1 // *TODO: check that this is still needed...
  773. // Not sure why...but if it is from ourselves we set the target_id to be
  774. // NULL
  775. if (other_participant_id == gAgentID)
  776. {
  777. other_participant_id.setNull();
  778. }
  779. #endif
  780. LL_DEBUGS("InstantMessaging") << "IM type: " << dialog
  781. << " - session name: " << session_name
  782. << " - From: " << from_name << LL_ENDL;
  783. LLUUID new_session_id = session_id;
  784. if (new_session_id.isNull())
  785. {
  786. // No session ID... Compute a new one
  787. new_session_id = computeSessionID(dialog, other_participant_id);
  788. }
  789. LLFloaterIMSession* floaterp =
  790. LLFloaterIMSession::findInstance(new_session_id);
  791. if (!floaterp)
  792. {
  793. floaterp = LLFloaterIMSession::findInstance(other_participant_id);
  794. if (floaterp)
  795. {
  796. llinfos << "Found the IM session " << session_id
  797. << " by participant " << other_participant_id << llendl;
  798. }
  799. }
  800. // Create IM window as necessary
  801. if (!floaterp)
  802. {
  803. LL_DEBUGS("InstantMessaging") << "Creating a new window" << LL_ENDL;
  804. std::string name = from_name;
  805. if (!session_name.empty() && session_name.size() > 1)
  806. {
  807. name = session_name;
  808. }
  809. if (LLAvatarName::sOmitResidentAsLastName)
  810. {
  811. name = LLCacheName::cleanFullName(name);
  812. from_name = LLCacheName::cleanFullName(from_name);
  813. }
  814. floaterp = createFloater(new_session_id, other_participant_id, name,
  815. dialog, false);
  816. // When we get a new IM, and if you are a god, display a bit of
  817. // information about the source. This is to help liaisons when
  818. // answering questions.
  819. if (gAgent.isGodlike())
  820. {
  821. // *TODO:translate (low priority, god ability)
  822. std::ostringstream bonus_info;
  823. bonus_info << "*** parent estate: " << parent_estate_id
  824. << (parent_estate_id == 1 ? ", mainland" : "")
  825. << (parent_estate_id == 5 ? ", teen" : "");
  826. // Once we have web-services (or something) which returns
  827. // information about a region id, we can print this out and even
  828. // have it link to map-teleport or something.
  829. // << "*** region_id: " << region_id << std::endl
  830. // << "*** position: " << position << std::endl;
  831. floaterp->addHistoryLine(bonus_info.str(),
  832. gSavedSettings.getColor4("SystemChatColor"));
  833. }
  834. if (private_im ||
  835. gSavedSettings.getBool("UISndNewIncomingPlayForGroup"))
  836. {
  837. make_ui_sound("UISndNewIncomingIMSession");
  838. }
  839. }
  840. // Now add message to floater
  841. bool is_from_system = target_id.isNull() || from_name == SYSTEM_FROM;
  842. LLColor4 color;
  843. if (is_from_system)
  844. {
  845. color = gSavedSettings.getColor4("SystemChatColor");
  846. }
  847. else
  848. {
  849. std::string new_line = std::string(msg);
  850. if (new_line.find(": ") == 0)
  851. {
  852. new_line = new_line.substr(2);
  853. }
  854. else
  855. {
  856. new_line = new_line.substr(1);
  857. }
  858. color = gSavedSettings.getColor("IMChatColor");
  859. get_extended_text_color(session_id, other_participant_id, new_line,
  860. color);
  861. }
  862. if (!link_name)
  863. {
  864. // No name to prepend, so just add the message normally
  865. floaterp->addHistoryLine(msg, color);
  866. }
  867. else
  868. {
  869. // Insert linked name to front of message
  870. floaterp->addHistoryLine(msg, color, true, other_participant_id,
  871. from_name);
  872. }
  873. LLFloaterChatterBox* chat_floaterp =
  874. LLFloaterChatterBox::getInstance(LLSD());
  875. if (!chat_floaterp->getVisible() && !floaterp->getVisible())
  876. {
  877. LL_DEBUGS("InstantMessaging") << "Adding the IM to the non-visible window"
  878. << LL_ENDL;
  879. // If the IM window is not open and the floater is not visible (i.e.
  880. // not torn off)
  881. LLFloater* old_active = chat_floaterp->getActiveFloater();
  882. // Select the newly added floater (or the floater with the new line
  883. // added to it). It should be there.
  884. chat_floaterp->selectFloater(floaterp);
  885. // There was a previously unseen IM, make that old tab flashing it is
  886. // assumed that the most recently unseen IM tab is the one current
  887. // selected/active
  888. if (old_active && mIMsReceived > 0)
  889. {
  890. chat_floaterp->setFloaterFlashing(old_active, true);
  891. }
  892. // Notify of a new IM (for the overlay bar button)
  893. // *BUG: in fact, this counts the number of sessions that received new,
  894. // unread IMs, and not the number of unread IMs... The floater code
  895. // above is apparently changing the getVisible() flags even though the
  896. // corresponding windows are not visible...
  897. ++mIMsReceived;
  898. if (private_im)
  899. {
  900. mPrivateIMReceived = true;
  901. }
  902. if (gOverlayBarp)
  903. {
  904. gOverlayBarp->setDirty();
  905. }
  906. LL_DEBUGS("InstantMessaging") << "Unread IMs: " << mIMsReceived
  907. << LL_ENDL;
  908. }
  909. }
  910. void LLIMMgr::addSystemMessage(const LLUUID& session_id,
  911. const std::string& message_name,
  912. const LLSD& args)
  913. {
  914. LLUIString message;
  915. // Null session id means near me (chat history)
  916. if (session_id.isNull())
  917. {
  918. LLFloaterChat* chat_floaterp = LLFloaterChat::getInstance();
  919. message = chat_floaterp->getString(message_name);
  920. message.setArgs(args);
  921. LLChat chat(message);
  922. chat.mSourceType = CHAT_SOURCE_SYSTEM;
  923. chat_floaterp->addChatHistory(chat);
  924. return;
  925. }
  926. // Going to IM session
  927. LLFloaterIMSession* floaterp =
  928. LLFloaterIMSession::findInstance(session_id);
  929. if (floaterp)
  930. {
  931. message = floaterp->getString(message_name);
  932. message.setArgs(args);
  933. addMessage(session_id, LLUUID::null, SYSTEM_FROM, message.getString());
  934. }
  935. }
  936. // This adds a session to the talk view. The name is the local name of the
  937. // session, dialog specifies the type of session. If the session exists, it is
  938. // brought forward. Specifying id = NULL results in an IM session to everyone.
  939. // Returns the UUID of the session.
  940. LLUUID LLIMMgr::addSession(const std::string& name, EInstantMessage dialog,
  941. const LLUUID& other_participant_id,
  942. const LLSD& voice_call_info)
  943. {
  944. LLUUID session_id = computeSessionID(dialog, other_participant_id);
  945. LLFloaterIMSession* floaterp =
  946. LLFloaterIMSession::findInstance(session_id);
  947. if (!floaterp)
  948. {
  949. uuid_vec_t ids;
  950. ids.emplace_back(other_participant_id);
  951. //MK
  952. if (gRLenabled && !gRLInterface.canStartIM(other_participant_id))
  953. {
  954. return LLUUID::null;
  955. }
  956. //mk
  957. floaterp = createFloater(session_id, other_participant_id, name, ids,
  958. voice_call_info, dialog, true);
  959. noteOfflineUsers(floaterp, ids);
  960. LLFloaterChatterBox::showInstance(session_id);
  961. // Only warn for regular IMs - not group IMs
  962. if (dialog == IM_NOTHING_SPECIAL)
  963. {
  964. noteMutedUsers(floaterp, ids);
  965. }
  966. else
  967. {
  968. snoozed_map_t::iterator it = mSnoozedSessions.find(session_id);
  969. if (it != mSnoozedSessions.end())
  970. {
  971. LL_DEBUGS("InstantMessaging") << "Removing session Id "
  972. << session_id
  973. << " from snoozes map."
  974. << LL_ENDL;
  975. mSnoozedSessions.erase(it);
  976. }
  977. }
  978. LLFloaterChatterBox::getInstance(LLSD())->showFloater(floaterp);
  979. }
  980. else
  981. {
  982. floaterp->open();
  983. }
  984. floaterp->setInputFocus(true);
  985. return floaterp->getSessionID();
  986. }
  987. // Adds a session using the given session_id. If the session already exists the
  988. // dialog type is assumed correct. Returns the uuid of the session.
  989. LLUUID LLIMMgr::addSession(const std::string& name, EInstantMessage dialog,
  990. const LLUUID& other_participant_id,
  991. const uuid_vec_t& ids, const LLSD& voice_call_info)
  992. {
  993. if (ids.empty())
  994. {
  995. return LLUUID::null;
  996. }
  997. LLUUID session_id = computeSessionID(dialog, other_participant_id);
  998. LLFloaterIMSession* floaterp =
  999. LLFloaterIMSession::findInstance(session_id);
  1000. if (!floaterp)
  1001. {
  1002. // On creation, use the first element of Ids as the
  1003. // "other_participant_id"
  1004. floaterp = createFloater(session_id, other_participant_id, name, ids,
  1005. voice_call_info, dialog, true);
  1006. if (!floaterp) return LLUUID::null; // Paranoia
  1007. noteOfflineUsers(floaterp, ids);
  1008. LLFloaterChatterBox::showInstance(session_id);
  1009. // Only warn for regular IMs, not group IMs
  1010. if (dialog == IM_NOTHING_SPECIAL)
  1011. {
  1012. noteMutedUsers(floaterp, ids);
  1013. }
  1014. }
  1015. else
  1016. {
  1017. floaterp->open();
  1018. }
  1019. floaterp->setInputFocus(true);
  1020. return floaterp->getSessionID();
  1021. }
  1022. void LLIMMgr::removeSession(const LLUUID& session_id,
  1023. const LLUUID& other_participant_id,
  1024. U32 snooze_duration)
  1025. {
  1026. if (session_id.notNull())
  1027. {
  1028. if (snooze_duration)
  1029. {
  1030. F32 unsnooze_after = gFrameTimeSeconds +
  1031. (F32)snooze_duration * 60.f;
  1032. LL_DEBUGS("InstantMessaging") << "Snoozing session Id: "
  1033. << session_id << LL_ENDL;
  1034. mSnoozedSessions[session_id] = unsnooze_after;
  1035. }
  1036. else // Close the session server-side
  1037. {
  1038. std::string name;
  1039. gAgent.buildFullname(name);
  1040. pack_instant_message(gAgentID, false, gAgentSessionID,
  1041. other_participant_id, name,
  1042. LLStringUtil::null, IM_ONLINE,
  1043. IM_SESSION_LEAVE, session_id);
  1044. gAgent.sendReliableMessage();
  1045. }
  1046. }
  1047. LLFloaterIMSession* floaterp =
  1048. LLFloaterIMSession::findInstance(session_id);
  1049. if (floaterp)
  1050. {
  1051. LLFloaterChatterBox::getInstance(LLSD())->removeFloater(floaterp);
  1052. clearPendingInvitation(session_id);
  1053. clearPendingAgentListUpdates(session_id);
  1054. }
  1055. }
  1056. void LLIMMgr::inviteToSession(const LLUUID& session_id,
  1057. const std::string& session_name,
  1058. const LLUUID& caller_id,
  1059. const std::string& caller_name,
  1060. EInstantMessage type,
  1061. EInvitationType inv_type,
  1062. const LLSD& voice_channel_info)
  1063. {
  1064. if (caller_id == gAgentID)
  1065. {
  1066. return; // Ignore invites from ourself.
  1067. }
  1068. // Ignore invites from muted residents
  1069. bool is_linden = LLMuteList::isLinden(caller_name);
  1070. if (!is_linden && LLMuteList::isMuted(caller_id))
  1071. {
  1072. llinfos << "Ignoring session invite from fully muted resident: "
  1073. << caller_name << llendl;
  1074. return;
  1075. }
  1076. std::string notify_box_type;
  1077. bool ad_hoc_invite = false;
  1078. bool voice_invite = false;
  1079. if (type == IM_SESSION_P2P_INVITE)
  1080. {
  1081. // P2P is different... they only have voice invitations
  1082. notify_box_type = "VoiceInviteP2P";
  1083. voice_invite = true;
  1084. }
  1085. else if (gAgent.isInGroup(session_id, true))
  1086. {
  1087. // Only really old school groups have voice invitations
  1088. notify_box_type = "VoiceInviteGroup";
  1089. voice_invite = true;
  1090. }
  1091. else if (inv_type == INVITATION_TYPE_VOICE)
  1092. {
  1093. // Else it is an ad-hoc and a voice ad-hoc
  1094. notify_box_type = "VoiceInviteAdHoc";
  1095. ad_hoc_invite = true;
  1096. voice_invite = true;
  1097. }
  1098. else if (inv_type == INVITATION_TYPE_IMMEDIATE)
  1099. {
  1100. notify_box_type = "InviteAdHoc";
  1101. ad_hoc_invite = true;
  1102. }
  1103. if (voice_invite && LLMuteList::isMuted(caller_id, LLMute::flagVoiceChat))
  1104. {
  1105. llinfos << "Ignoring voice session invite from voice-muted resident: "
  1106. << caller_name << llendl;
  1107. return;
  1108. }
  1109. LLSD payload;
  1110. payload["session_id"] = session_id;
  1111. payload["session_name"] = session_name;
  1112. payload["caller_id"] = caller_id;
  1113. payload["caller_name"] = caller_name;
  1114. payload["type"] = type;
  1115. payload["inv_type"] = inv_type;
  1116. payload["voice_channel_info"] = voice_channel_info;
  1117. payload["notify_box_type"] = notify_box_type;
  1118. LLVoiceChannel* channelp = LLVoiceChannel::getChannelByID(session_id);
  1119. if (channelp && channelp->callStarted())
  1120. {
  1121. // You have already started a call to the other user, so just accept
  1122. // the invite
  1123. gNotifications.forceResponse(LLNotification::Params("VoiceInviteP2P").payload(payload),
  1124. 0);
  1125. return;
  1126. }
  1127. if (type == IM_SESSION_P2P_INVITE || ad_hoc_invite)
  1128. {
  1129. // Is the inviter a friend ?
  1130. if (gAvatarTracker.getBuddyInfo(caller_id) == NULL)
  1131. {
  1132. // If not, and we are ignoring voice invites from non-friends
  1133. // then silently decline
  1134. if (gSavedSettings.getBool("VoiceCallsFriendsOnly"))
  1135. {
  1136. // Invite is not from a friend, so decline
  1137. gNotifications.forceResponse(LLNotification::Params("VoiceInviteP2P").payload(payload),
  1138. 1);
  1139. return;
  1140. }
  1141. }
  1142. }
  1143. if (!mPendingInvitations.has(session_id.asString()))
  1144. {
  1145. if (caller_name.empty())
  1146. {
  1147. if (gCacheNamep)
  1148. {
  1149. gCacheNamep->get(caller_id, false,
  1150. boost::bind(&LLIMMgr::onInviteNameLookup,
  1151. _1, _2, _3, payload));
  1152. }
  1153. }
  1154. else
  1155. {
  1156. LLSD args;
  1157. args["NAME"] = caller_name;
  1158. args["GROUP"] = session_name;
  1159. gNotifications.add(notify_box_type, args, payload,
  1160. &inviteUserResponse);
  1161. }
  1162. mPendingInvitations[session_id.asString()] = LLSD();
  1163. }
  1164. }
  1165. //static
  1166. void LLIMMgr::onInviteNameLookup(const LLUUID& id,
  1167. const std::string& full_name,
  1168. bool is_group, LLSD payload)
  1169. {
  1170. std::string name = full_name;
  1171. if (LLAvatarName::sOmitResidentAsLastName)
  1172. {
  1173. name = LLCacheName::cleanFullName(name);
  1174. }
  1175. payload["caller_name"] = name;
  1176. payload["session_name"] = name;
  1177. LLSD args;
  1178. args["NAME"] = name;
  1179. gNotifications.add(payload["notify_box_type"].asString(), args, payload,
  1180. &inviteUserResponse);
  1181. }
  1182. void LLIMMgr::refresh()
  1183. {
  1184. static const EInstantMessage group_session = IM_SESSION_GROUP_START;
  1185. static const EInstantMessage default_session = IM_NOTHING_SPECIAL;
  1186. LLFloaterNewIM* floaterp =
  1187. LLFloaterChatterBox::getInstance(LLSD())->getFloaterNewIM();
  1188. if (!floaterp) return;
  1189. S32 old_group_scroll_pos = floaterp->getGroupScrollPos();
  1190. S32 old_agent_scroll_pos = floaterp->getAgentScrollPos();
  1191. floaterp->clearAllTargets();
  1192. // Add groups
  1193. for (S32 i = 0, count = gAgent.mGroups.size(); i < count; ++i)
  1194. {
  1195. LLGroupData* group = &(gAgent.mGroups[i]);
  1196. floaterp->addGroup(group->mID, (void*)(&group_session));
  1197. }
  1198. // Build a set of buddies in the current buddy list.
  1199. LLCollectAllBuddies collector;
  1200. gAvatarTracker.applyFunctor(collector);
  1201. LLCollectAllBuddies::buddy_map_t::iterator it;
  1202. LLCollectAllBuddies::buddy_map_t::iterator end;
  1203. it = collector.mOnline.begin();
  1204. end = collector.mOnline.end();
  1205. for ( ; it != end; ++it)
  1206. {
  1207. floaterp->addAgent(it->second, (void*)(&default_session), true);
  1208. }
  1209. it = collector.mOffline.begin();
  1210. end = collector.mOffline.end();
  1211. for ( ; it != end; ++it)
  1212. {
  1213. floaterp->addAgent(it->second, (void*)(&default_session), false);
  1214. }
  1215. floaterp->setGroupScrollPos(old_group_scroll_pos);
  1216. floaterp->setAgentScrollPos(old_agent_scroll_pos);
  1217. }
  1218. void LLIMMgr::setFloaterOpen(bool set_open)
  1219. {
  1220. if (set_open)
  1221. {
  1222. LLFloaterChatterBox::showInstance();
  1223. LLFloaterChatterBox* floater_chatterbox = getFloater();
  1224. LLFloater* floater_current = floater_chatterbox->getActiveFloater();
  1225. LLFloater* floater_new_im = floater_chatterbox->getFloaterNewIM();
  1226. bool active_is_im = floater_current &&
  1227. (floater_current->getName() == gIMFloaterName ||
  1228. floater_current == floater_new_im);
  1229. LLFloater* floater_to_show = active_is_im ? floater_current : NULL;
  1230. LLTabContainer* tabs =
  1231. floater_chatterbox->getChild<LLTabContainer>("Preview Tabs");
  1232. for (S32 i = 0; i < floater_chatterbox->getFloaterCount(); ++i)
  1233. {
  1234. LLPanel* panelp = tabs->getPanelByIndex(i);
  1235. if (panelp->getName() == gIMFloaterName)
  1236. {
  1237. // This cast is safe here because in such tabs, only an
  1238. // LLFloaterIMSessions can be called gIMFloaterName.
  1239. LLFloaterIMSession* floaterp = (LLFloaterIMSession*)panelp;
  1240. if (floaterp &&
  1241. (!floater_to_show ||
  1242. floater_chatterbox->isFloaterFlashing(floaterp)))
  1243. {
  1244. // The first floaterp or the flashing floaterp
  1245. floater_to_show = floaterp;
  1246. }
  1247. }
  1248. }
  1249. if (floater_to_show)
  1250. {
  1251. floater_to_show->open();
  1252. }
  1253. else if (floater_chatterbox && floater_chatterbox->getFloaterNewIM())
  1254. {
  1255. floater_chatterbox->getFloaterNewIM()->open();
  1256. }
  1257. }
  1258. else
  1259. {
  1260. LLFloaterChatterBox::hideInstance();
  1261. }
  1262. }
  1263. bool LLIMMgr::getFloaterOpen()
  1264. {
  1265. return LLFloaterChatterBox::instanceVisible(LLSD());
  1266. }
  1267. LLFloaterChatterBox* LLIMMgr::getFloater()
  1268. {
  1269. return LLFloaterChatterBox::getInstance(LLSD());
  1270. }
  1271. void LLIMMgr::disconnectAllSessions()
  1272. {
  1273. LLFloaterIMSession::closeAllInstances();
  1274. }
  1275. void LLIMMgr::clearPendingInvitation(const LLUUID& session_id)
  1276. {
  1277. if (mPendingInvitations.has(session_id.asString()))
  1278. {
  1279. mPendingInvitations.erase(session_id.asString());
  1280. }
  1281. }
  1282. LLSD LLIMMgr::getPendingAgentListUpdates(const LLUUID& session_id)
  1283. {
  1284. if (mPendingAgentListUpdates.has(session_id.asString()))
  1285. {
  1286. return mPendingAgentListUpdates[session_id.asString()];
  1287. }
  1288. return LLSD();
  1289. }
  1290. void LLIMMgr::addPendingAgentListUpdates(const LLUUID& session_id,
  1291. const LLSD& updates)
  1292. {
  1293. LLSD::map_const_iterator iter;
  1294. if (!mPendingAgentListUpdates.has(session_id.asString()))
  1295. {
  1296. // This is a new agent list update for this session
  1297. mPendingAgentListUpdates[session_id.asString()] = LLSD::emptyMap();
  1298. }
  1299. if (updates.has("agent_updates") && updates["agent_updates"].isMap() &&
  1300. updates.has("updates") && updates["updates"].isMap())
  1301. {
  1302. // New school update
  1303. LLSD update_types = LLSD::emptyArray();
  1304. LLSD::array_iterator array_iter;
  1305. update_types.append("agent_updates");
  1306. update_types.append("updates");
  1307. for (array_iter = update_types.beginArray();
  1308. array_iter != update_types.endArray();
  1309. ++array_iter)
  1310. {
  1311. // We only want to include the last update for a given agent
  1312. for (iter = updates[array_iter->asString()].beginMap();
  1313. iter != updates[array_iter->asString()].endMap();
  1314. ++iter)
  1315. {
  1316. mPendingAgentListUpdates[session_id.asString()][array_iter->asString()][iter->first] =
  1317. iter->second;
  1318. }
  1319. }
  1320. }
  1321. else if (updates.has("updates") && updates["updates"].isMap())
  1322. {
  1323. // Old school update where the SD contained just mappings of
  1324. // agent_id -> "LEAVE"/"ENTER"
  1325. // Only want to keep last update for each agent
  1326. for (iter = updates["updates"].beginMap();
  1327. iter != updates["updates"].endMap(); ++iter)
  1328. {
  1329. mPendingAgentListUpdates[session_id.asString()]["updates"][iter->first] =
  1330. iter->second;
  1331. }
  1332. }
  1333. }
  1334. void LLIMMgr::clearPendingAgentListUpdates(const LLUUID& session_id)
  1335. {
  1336. if (mPendingAgentListUpdates.has(session_id.asString()))
  1337. {
  1338. mPendingAgentListUpdates.erase(session_id.asString());
  1339. }
  1340. }
  1341. void LLIMMgr::processAgentListUpdates(const LLUUID& session_id,
  1342. const LLSD& body)
  1343. {
  1344. if (body.has("agent_updates"))
  1345. {
  1346. // WebRTC-specific agent updates on leaving a P2P channel.
  1347. const LLSD& updates = body["agent_updates"];
  1348. if (updates.isMap())
  1349. {
  1350. for (LLSD::map_const_iterator it = updates.beginMap(),
  1351. end = updates.endMap();
  1352. it != end; ++it)
  1353. {
  1354. const LLSD& agent_data = it->second;
  1355. if (!agent_data.isMap())
  1356. {
  1357. continue;
  1358. }
  1359. // Ignore actual leaves as those will be handled separately.
  1360. if (agent_data.has("transition") &&
  1361. agent_data["transition"].asString() == "LEAVE")
  1362. {
  1363. continue;
  1364. }
  1365. LLUUID agent_id(it->first, false);
  1366. if (agent_id == gAgentID || !agent_data.has("info"))
  1367. {
  1368. continue;
  1369. }
  1370. const LLSD& info = agent_data["info"];
  1371. if (!info.isMap())
  1372. {
  1373. continue;
  1374. }
  1375. if (info.has("can_voice_chat") &&
  1376. !info["can_voice_chat"].asBoolean())
  1377. {
  1378. LLVoiceChannel* channelp =
  1379. LLVoiceChannel::getChannelByID(session_id);
  1380. if (channelp && channelp->isP2P())
  1381. {
  1382. // It is an adhoc-style P2P channel, and the peer has
  1383. // declined voice. Notify the user and shut down the
  1384. // voice channel.
  1385. gNotifications.add("P2PCallDeclined",
  1386. channelp->getNotifyArgs());
  1387. channelp->deactivate();
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. }
  1393. }
  1394. LLFloaterIMSession* floaterp =
  1395. LLFloaterIMSession::findInstance(session_id);
  1396. if (floaterp)
  1397. {
  1398. floaterp->updateSpeakersList(body);
  1399. }
  1400. else
  1401. {
  1402. // We do not have a floater yet: something went wrong and we are
  1403. // probably receiving an update here before a start or an acceptance
  1404. // of an invitation. Race condition.
  1405. addPendingAgentListUpdates(session_id, body);
  1406. }
  1407. }
  1408. // Creates a floater and updates internal representation for consistency.
  1409. // Returns the pointer, caller (the class instance since it is a private
  1410. // method) is not responsible for deleting the pointer. Add the floater to
  1411. // this but do not select it.
  1412. LLFloaterIMSession* LLIMMgr::createFloater(const LLUUID& session_id,
  1413. const LLUUID& other_participant_id,
  1414. const std::string& session_label,
  1415. EInstantMessage dialog,
  1416. bool user_initiated)
  1417. {
  1418. if (session_id.isNull())
  1419. {
  1420. llwarns << "Creating floater with null session Id" << llendl;
  1421. }
  1422. llinfos << "Created from " << other_participant_id << " in session "
  1423. << session_id << llendl;
  1424. LLFloaterIMSession* floaterp = new LLFloaterIMSession(session_label,
  1425. session_id,
  1426. other_participant_id,
  1427. dialog);
  1428. LLTabContainer::eInsertionPoint i_pt = user_initiated ?
  1429. LLTabContainer::RIGHT_OF_CURRENT :
  1430. LLTabContainer::END;
  1431. LLFloaterChatterBox::getInstance(LLSD())->addFloater(floaterp, false,
  1432. i_pt);
  1433. return floaterp;
  1434. }
  1435. LLFloaterIMSession* LLIMMgr::createFloater(const LLUUID& session_id,
  1436. const LLUUID& other_participant_id,
  1437. const std::string& session_label,
  1438. const uuid_vec_t& ids,
  1439. const LLSD& voice_call_info,
  1440. EInstantMessage dialog,
  1441. bool user_initiated)
  1442. {
  1443. if (session_id.isNull())
  1444. {
  1445. llwarns << "Creating with null session Id !" << llendl;
  1446. }
  1447. llinfos << "Creating floater for " << other_participant_id
  1448. << " in session " << session_id << llendl;
  1449. LLFloaterIMSession* floaterp = new LLFloaterIMSession(session_label,
  1450. session_id,
  1451. other_participant_id,
  1452. ids, voice_call_info,
  1453. dialog);
  1454. LLTabContainer::eInsertionPoint i_pt = user_initiated ?
  1455. LLTabContainer::RIGHT_OF_CURRENT :
  1456. LLTabContainer::END;
  1457. LLFloaterChatterBox::getInstance(LLSD())->addFloater(floaterp, false,
  1458. i_pt);
  1459. return floaterp;
  1460. }
  1461. void LLIMMgr::noteOfflineUsers(LLFloaterIMSession* floaterp,
  1462. const uuid_vec_t& ids)
  1463. {
  1464. S32 count = ids.size();
  1465. if (!count)
  1466. {
  1467. floaterp->addHistoryLine(LLFloaterIM::sOnlyUserMessage,
  1468. gSavedSettings.getColor4("SystemChatColor"));
  1469. return;
  1470. }
  1471. const LLRelationship* info = NULL;
  1472. LLAvatarTracker& at = gAvatarTracker;
  1473. LLColor4 color = gSavedSettings.getColor4("SystemChatColor");
  1474. for (S32 i = 0; i < count; ++i)
  1475. {
  1476. info = at.getBuddyInfo(ids[i]);
  1477. std::string first, last;
  1478. if (info && !info->isOnline() && gCacheNamep &&
  1479. gCacheNamep->getName(ids[i], first, last))
  1480. {
  1481. LLUIString offline = LLFloaterIM::sOfflineMessage;
  1482. offline.setArg("[FIRST]", first);
  1483. offline.setArg("[LAST]", last);
  1484. floaterp->addHistoryLine(offline, color);
  1485. }
  1486. }
  1487. }
  1488. void LLIMMgr::noteMutedUsers(LLFloaterIMSession* floaterp,
  1489. const uuid_vec_t& ids)
  1490. {
  1491. S32 count = ids.size();
  1492. if (count > 0)
  1493. {
  1494. for (S32 i = 0; i < count; ++i)
  1495. {
  1496. if (LLMuteList::isMuted(ids[i]))
  1497. {
  1498. LLUIString muted = LLFloaterIM::sMutedMessage;
  1499. floaterp->addHistoryLine(muted);
  1500. break;
  1501. }
  1502. }
  1503. }
  1504. }
  1505. void LLIMMgr::processNewMessage(const LLUUID& from_id, bool from_group,
  1506. const LLUUID& to_id, U8 offline,
  1507. EInstantMessage dialog,
  1508. const LLUUID& session_id, U32 timestamp,
  1509. std::string name, std::string message,
  1510. U32 parent_estate_id,const LLUUID& region_id,
  1511. const LLVector3& position, U8* binary_bucket,
  1512. S32 bucket_size, const LLHost& sender,
  1513. const LLUUID& aux_id)
  1514. {
  1515. LLViewerRegion* regionp = gAgent.getRegion();
  1516. if (!regionp)
  1517. {
  1518. return; // Viewer is likely disconnected or closing down !
  1519. }
  1520. // Make sure that we do not have an empty or all-whitespace name
  1521. LLStringUtil::trim(name);
  1522. if (name.empty())
  1523. {
  1524. name = LLTrans::getString("Unnamed");
  1525. }
  1526. bool is_busy = gAgent.getBusy();
  1527. bool is_away = gAgent.getAFK() &&
  1528. gSavedPerAccountSettings.getBool("BusyResponseWhenAway");
  1529. bool auto_reply = gAgent.getAutoReply();
  1530. bool is_muted = LLMuteList::isMuted(from_id, name, LLMute::flagTextChat);
  1531. bool is_linden = LLMuteList::isLinden(name);
  1532. bool is_owned_by_me = false;
  1533. // session_id is probably the wrong thing...
  1534. LLViewerObject* source = gObjectList.findObject(session_id);
  1535. if (source)
  1536. {
  1537. is_owned_by_me = source->permYouOwner();
  1538. }
  1539. LLChat chat;
  1540. chat.mMuted = is_muted && !is_linden;
  1541. chat.mFromID = from_id;
  1542. chat.mFromName = name;
  1543. if (from_id.isNull() || name == SYSTEM_FROM)
  1544. {
  1545. chat.mSourceType = CHAT_SOURCE_SYSTEM;
  1546. }
  1547. else if (dialog == IM_FROM_TASK || dialog == IM_FROM_TASK_AS_ALERT)
  1548. {
  1549. chat.mSourceType = CHAT_SOURCE_OBJECT;
  1550. // Keep track of the owner's Id for the source object.
  1551. if (source && source->mOwnerID.isNull())
  1552. {
  1553. source->mOwnerID = from_id;
  1554. }
  1555. }
  1556. else
  1557. {
  1558. chat.mSourceType = CHAT_SOURCE_AGENT;
  1559. }
  1560. std::string separator_string(": ");
  1561. size_t message_offset = 0;
  1562. // Handle IRC styled /me messages.
  1563. std::string prefix = message.substr(0, 4);
  1564. if (prefix == "/me " || prefix == "/me'")
  1565. {
  1566. separator_string = "";
  1567. message_offset = 3;
  1568. }
  1569. LL_DEBUGS("InstantMessaging") << "IM type: " << dialog << " from: "
  1570. << (is_owned_by_me ?
  1571. "agent-owned object" :
  1572. (source ?
  1573. "other resident object" :
  1574. (from_group ? "group" : "resident")))
  1575. << LL_ENDL;
  1576. std::string buffer;
  1577. LLSD args;
  1578. switch (dialog)
  1579. {
  1580. case IM_CONSOLE_AND_CHAT_HISTORY:
  1581. {
  1582. // These are used for system messages, hence do not need the name,
  1583. // as it is always "Second Life". *TODO: translate
  1584. //MK
  1585. if (gRLenabled)
  1586. {
  1587. if (gRLInterface.mContainsShowloc)
  1588. {
  1589. // Hide every occurrence of the Region and Parcel names if
  1590. // the location restriction is active
  1591. message = gRLInterface.getCensoredLocation(message);
  1592. }
  1593. if (gRLInterface.mContainsShownames ||
  1594. gRLInterface.mContainsShownametags)
  1595. {
  1596. // Censor object IMs but not avatar IMs
  1597. message = gRLInterface.getCensoredMessage(message);
  1598. }
  1599. }
  1600. //mk
  1601. args["MESSAGE"] = message;
  1602. // Note: don't put the message in the IM history, even though was
  1603. // sent via the IM mechanism.
  1604. gNotifications.add("SystemMessageTip", args);
  1605. break;
  1606. }
  1607. case IM_NOTHING_SPECIAL:
  1608. {
  1609. // Do not show dialog, just do IM
  1610. if (to_id.isNull() && !gAgent.isGodlike() && regionp &&
  1611. regionp->isPrelude())
  1612. {
  1613. // Do not distract newbies in Prelude with global IMs
  1614. break;
  1615. }
  1616. //MK
  1617. else if (gRLenabled && !is_muted &&
  1618. (message == "@version" || message == "@getblacklist" ||
  1619. message == "@list" || message == "@stopim"))
  1620. {
  1621. bool close_session = false;
  1622. std::string my_name;
  1623. gAgent.buildFullname(my_name);
  1624. std::string response;
  1625. if (message == "@version")
  1626. {
  1627. // Return the version message
  1628. response = gRLInterface.getVersion();
  1629. }
  1630. else if (message == "@getblacklist")
  1631. {
  1632. // Return the list of the blacklisted RLV commands
  1633. response = RLInterface::sBlackList;
  1634. }
  1635. else if (message == "@list")
  1636. {
  1637. // Return the list of the RLV restrictions in force
  1638. response = gRLInterface.getRlvRestrictions();
  1639. }
  1640. else if (gRLInterface.canStartIM(from_id))
  1641. {
  1642. response =
  1643. "*** The other party is not under @startim restriction.";
  1644. }
  1645. // @stopim
  1646. else
  1647. {
  1648. close_session = true;
  1649. response =
  1650. "*** Session has been ended for the other party.";
  1651. }
  1652. // The message may be very long, so we might need to chop it
  1653. // into chunks of 1023 characters and send several IMs in a row
  1654. // or else it will be truncated by the server.
  1655. std::string chunk;
  1656. while (response.length())
  1657. {
  1658. if (response.length() > 1023)
  1659. {
  1660. chunk = response.substr(0, 1023);
  1661. // Try to break out at the end of a text line, if
  1662. // possible...
  1663. size_t i = chunk.rfind('\n');
  1664. if (i > 1)
  1665. {
  1666. chunk = chunk.substr(0, i);
  1667. }
  1668. else
  1669. {
  1670. i = 1023;
  1671. }
  1672. response = response.substr(i);
  1673. }
  1674. else
  1675. {
  1676. chunk = response;
  1677. response.clear();
  1678. }
  1679. pack_instant_message(gAgentID, false, gAgentSessionID,
  1680. from_id, my_name.c_str(),
  1681. chunk.c_str(), IM_ONLINE,
  1682. IM_BUSY_AUTO_RESPONSE, session_id);
  1683. gAgent.sendReliableMessage();
  1684. }
  1685. if (close_session)
  1686. {
  1687. LLFloaterIMSession* floaterp =
  1688. LLFloaterIMSession::findInstance(session_id);
  1689. if (floaterp)
  1690. {
  1691. LLChat chat("*** IM session with " + name +
  1692. " has been ended remotely.");
  1693. LLFloaterChat::addChat(chat, true, false);
  1694. floaterp->close();
  1695. }
  1696. }
  1697. // Remove the "XXX is typing..." label from the IM window
  1698. processIMTypingCore(dialog, from_id, name, false);
  1699. }
  1700. //mk
  1701. else if ((is_busy || is_away || auto_reply) &&
  1702. //MK
  1703. (!gRLenabled ||
  1704. // Agent is not forbidden to receive IMs or the sender is
  1705. // an exception => send Busy response
  1706. gRLInterface.canReceiveIM(from_id)) &&
  1707. //mk
  1708. offline == IM_ONLINE && !is_linden && name != SYSTEM_FROM)
  1709. {
  1710. // Return a standard "busy" message, but only do it to online
  1711. // IM (i.e. not other auto responses and not store-and-forward
  1712. // IM)
  1713. if (!LLFloaterIMSession::findInstance(session_id))
  1714. {
  1715. // There is no panel for this conversation (i.e. it is a
  1716. // new IM conversation initiated by the other party)
  1717. std::string my_name;
  1718. gAgent.buildFullname(my_name);
  1719. std::string response;
  1720. if (is_away)
  1721. {
  1722. response = "Away mode auto-response: ";
  1723. }
  1724. else if (is_busy)
  1725. {
  1726. response = "Busy mode auto-response: ";
  1727. }
  1728. else
  1729. {
  1730. response = "Auto-response: ";
  1731. }
  1732. response +=
  1733. gSavedPerAccountSettings.getText("BusyModeResponse");
  1734. pack_instant_message(gAgentID, false, gAgentSessionID,
  1735. from_id, my_name, response, IM_ONLINE,
  1736. IM_BUSY_AUTO_RESPONSE, session_id);
  1737. gAgent.sendReliableMessage();
  1738. }
  1739. // Now store incoming IM in chat history
  1740. buffer = separator_string + message.substr(message_offset);
  1741. llinfos << "IM_NOTHING_SPECIAL session_id(" << session_id
  1742. << "), from_id(" << from_id << ")" << llendl;
  1743. // Add to IM panel, but do not bother the user
  1744. addMessage(session_id, from_id, name, buffer,
  1745. LLStringUtil::null, dialog, parent_estate_id,
  1746. region_id, position, true);
  1747. // Pretend this is chat generated by self, so it does not show
  1748. // up on screen
  1749. chat.mText = "IM: " + name + separator_string +
  1750. message.substr(message_offset);
  1751. LLFloaterChat::addChat(chat, true, true);
  1752. if (gAutomationp)
  1753. {
  1754. gAutomationp->onInstantMsg(session_id, from_id, name,
  1755. chat.mText);
  1756. }
  1757. }
  1758. else if (from_id.isNull())
  1759. {
  1760. // Messages from "Second Life" ID don't go to IM history
  1761. // messages which should be routed to IM window come from a
  1762. // user ID with name = SYSTEM_NAME:
  1763. chat.mText = name + ": " + message;
  1764. //MK
  1765. if (gRLenabled)
  1766. {
  1767. if (gRLInterface.mContainsShowloc)
  1768. {
  1769. // Hide every occurrence of the Region and Parcel names if
  1770. // the location restriction is active
  1771. chat.mText = gRLInterface.getCensoredLocation(chat.mText);
  1772. }
  1773. if (gRLInterface.mContainsShownames)
  1774. {
  1775. // censor that message
  1776. chat.mText = gRLInterface.getCensoredMessage(chat.mText);
  1777. }
  1778. }
  1779. //mk
  1780. LLFloaterChat::addChat(chat, false, false);
  1781. if (gAutomationp)
  1782. {
  1783. gAutomationp->onReceivedChat(chat.mChatType, from_id, name,
  1784. chat.mText);
  1785. }
  1786. }
  1787. else if (to_id.isNull())
  1788. {
  1789. // Message to everyone from GOD
  1790. args["NAME"] = name;
  1791. args["MESSAGE"] = message;
  1792. gNotifications.add("GodMessage", args);
  1793. // Treat like a system message and put in chat history. Claim
  1794. // to be from a local agent so it doesn't go into console.
  1795. chat.mText = name + separator_string +
  1796. message.substr(message_offset);
  1797. bool local_agent = true;
  1798. LLFloaterChat::addChat(chat, false, local_agent);
  1799. if (gAutomationp)
  1800. {
  1801. gAutomationp->onReceivedChat(chat.mChatType, from_id, name,
  1802. chat.mText);
  1803. }
  1804. }
  1805. else
  1806. {
  1807. // Standard message, not from system
  1808. std::string saved;
  1809. if (offline == IM_OFFLINE)
  1810. {
  1811. saved = llformat("(Saved %s) ",
  1812. formatted_time(timestamp).c_str());
  1813. }
  1814. buffer = separator_string + saved +
  1815. message.substr(message_offset);
  1816. //MK
  1817. bool forbid = gRLenabled &&
  1818. !gRLInterface.canReceiveIM(from_id);
  1819. if (forbid)
  1820. {
  1821. // Agent is forbidden to receive IMs and the sender is no
  1822. // exception
  1823. buffer = separator_string + saved +
  1824. "*** IM blocked by your viewer";
  1825. // Tell the sender the avatar could not read them
  1826. std::string my_name;
  1827. gAgent.buildFullname(my_name);
  1828. my_name += " using viewer " +
  1829. gRLInterface.getVersion();
  1830. std::string response = RLInterface::sRecvimMessage;
  1831. pack_instant_message(gAgentID, false, gAgentSessionID,
  1832. from_id, my_name.c_str(),
  1833. response.c_str(), IM_ONLINE,
  1834. IM_BUSY_AUTO_RESPONSE, session_id);
  1835. gAgent.sendReliableMessage();
  1836. }
  1837. //mk
  1838. llinfos << "IM_NOTHING_SPECIAL session_id(" << session_id
  1839. << "), from_id(" << from_id << ")" << llendl;
  1840. if (!is_muted || is_linden)
  1841. {
  1842. addMessage(session_id, from_id, name, buffer,
  1843. LLStringUtil::null, dialog, parent_estate_id,
  1844. region_id, position, true);
  1845. if (gAutomationp)
  1846. {
  1847. gAutomationp->onInstantMsg(session_id, from_id, name,
  1848. buffer);
  1849. }
  1850. //MK
  1851. // When agent is not forbidden to receive IMs or the sender
  1852. // is an exception, duplicate in chat box.
  1853. if (!forbid)
  1854. //mk
  1855. {
  1856. chat.mText = "IM: " + name + separator_string + saved +
  1857. message.substr(message_offset);
  1858. bool local_agent = false;
  1859. LLFloaterChat::addChat(chat, true, local_agent);
  1860. }
  1861. }
  1862. else
  1863. {
  1864. // Muted user, so do not start an IM session, just record
  1865. // line in chat history. Pretend the chat is from a local
  1866. // agent, so it will go into the history but not be shown
  1867. // on screen.
  1868. chat.mText = buffer;
  1869. bool local_agent = true;
  1870. LLFloaterChat::addChat(chat, true, local_agent);
  1871. }
  1872. }
  1873. break;
  1874. }
  1875. case IM_TYPING_START:
  1876. case IM_TYPING_STOP:
  1877. {
  1878. bool typing_start = dialog == IM_TYPING_START;
  1879. bool ok = processIMTypingCore(dialog, from_id, name, typing_start);
  1880. if (!ok && typing_start && (!is_muted || is_linden) &&
  1881. // Do not announce when busy/away/auto-replying
  1882. !is_busy && !is_away && !auto_reply &&
  1883. gSavedSettings.getBool("IMOpenSessionOnIncoming") &&
  1884. //MK
  1885. (!gRLenabled || gRLInterface.canReceiveIM(from_id)))
  1886. //mk
  1887. {
  1888. addMessage(computeSessionID(dialog, from_id), from_id,
  1889. INCOMING_IM, LLTrans::getString("im_incoming"),
  1890. name, IM_NOTHING_SPECIAL, parent_estate_id,
  1891. region_id, position, false);
  1892. }
  1893. break;
  1894. }
  1895. case IM_MESSAGEBOX:
  1896. {
  1897. // This is a block, modeless dialog.
  1898. //*TODO:translate
  1899. //MK
  1900. if (gRLenabled)
  1901. {
  1902. if (gRLInterface.mContainsShowloc)
  1903. {
  1904. // Hide every occurrence of the Region and Parcel names if
  1905. // thelocation restriction is active
  1906. message = gRLInterface.getCensoredLocation(message);
  1907. }
  1908. if (gRLInterface.mContainsShownames ||
  1909. gRLInterface.mContainsShownametags)
  1910. {
  1911. message = gRLInterface.getCensoredMessage(message);
  1912. }
  1913. }
  1914. //mk
  1915. args["MESSAGE"] = message;
  1916. gNotifications.add("SystemMessage", args);
  1917. break;
  1918. }
  1919. case IM_GROUP_NOTICE:
  1920. case IM_GROUP_NOTICE_REQUESTED:
  1921. {
  1922. llinfos << "Received IM_GROUP_NOTICE message." << llendl;
  1923. U8 has_inventory = 0;
  1924. U8 asset_type = 0;
  1925. LLUUID group_id;
  1926. std::string item_name;
  1927. if (aux_id.notNull())
  1928. {
  1929. // aux_id contains group id, binary bucket contains name and
  1930. // asset type
  1931. from_group = true;
  1932. group_id = aux_id;
  1933. has_inventory = bucket_size > 1 ? 1 : 0;
  1934. if (has_inventory)
  1935. {
  1936. std::string str_bucket =
  1937. ll_safe_string((char*)binary_bucket, bucket_size);
  1938. tok_t tokens(str_bucket, sSeparators);
  1939. tok_t::iterator iter = tokens.begin();
  1940. if (iter != tokens.end())
  1941. {
  1942. asset_type =
  1943. (LLAssetType::EType)(atoi((iter++)->c_str()));
  1944. if (++iter != tokens.end())
  1945. {
  1946. item_name.assign(iter->c_str());
  1947. }
  1948. }
  1949. }
  1950. }
  1951. else
  1952. {
  1953. // Read the binary bucket for more information.
  1954. struct notice_bucket_header_t
  1955. {
  1956. U8 has_inventory;
  1957. U8 asset_type;
  1958. LLUUID group_id;
  1959. };
  1960. struct notice_bucket_full_t
  1961. {
  1962. struct notice_bucket_header_t header;
  1963. U8 item_name[DB_INV_ITEM_NAME_BUF_SIZE];
  1964. }* notice_bin_bucket;
  1965. // Make sure the binary bucket is big enough to hold the header
  1966. // and a nul terminated item name.
  1967. if (bucket_size <
  1968. (S32)(sizeof(notice_bucket_header_t) + sizeof(U8)) ||
  1969. binary_bucket[bucket_size - 1] != '\0')
  1970. {
  1971. llwarns << "Malformed group notice binary bucket"
  1972. << llendl;
  1973. break;
  1974. }
  1975. notice_bin_bucket =
  1976. (struct notice_bucket_full_t*)&binary_bucket[0];
  1977. has_inventory = notice_bin_bucket->header.has_inventory;
  1978. asset_type = notice_bin_bucket->header.asset_type;
  1979. group_id = notice_bin_bucket->header.group_id;
  1980. item_name =
  1981. ll_safe_string((const char*)notice_bin_bucket->item_name);
  1982. }
  1983. // If there is inventory, give the user the inventory offer.
  1984. LLOfferInfo* info = NULL;
  1985. is_muted = LLMuteList::isMuted(LLUUID::null, name, 0,
  1986. LLMute::AGENT);
  1987. if (has_inventory && !is_muted)
  1988. {
  1989. info = new LLOfferInfo;
  1990. info->mIM = IM_GROUP_NOTICE;
  1991. info->mFromID = from_id;
  1992. info->mFromObject = false;
  1993. info->mFromGroup = from_group;
  1994. info->mTransactionID = session_id;
  1995. info->mType = (LLAssetType::EType)asset_type;
  1996. info->mFolderID =
  1997. gInventory.findCategoryUUIDForType(LLFolderType::assetTypeToFolderType(info->mType));
  1998. info->mFromName = "A group member named " + name;
  1999. info->mDesc = item_name;
  2000. info->mHost = sender;
  2001. // For requested notices, we do not want a chat decline message
  2002. // logged (it would appear each time you select another group
  2003. // notice).
  2004. if (dialog == IM_GROUP_NOTICE_REQUESTED || is_muted)
  2005. {
  2006. info->mLogInChat = false;
  2007. }
  2008. }
  2009. std::string str = message;
  2010. // Tokenize the string. *TODO: Support escaped tokens ("||" -> "|")
  2011. tok_t tokens(str, sSeparators);
  2012. tok_t::iterator iter = tokens.begin();
  2013. std::string subj(*iter++);
  2014. std::string mes(*iter++);
  2015. // Send the notification down the new path.
  2016. // For requested notices, we do not want to send the popups.
  2017. if (dialog != IM_GROUP_NOTICE_REQUESTED && !is_muted)
  2018. {
  2019. LLSD payload;
  2020. payload["subject"] = subj;
  2021. payload["message"] = mes;
  2022. payload["sender_name"] = name;
  2023. payload["group_id"] = group_id;
  2024. payload["inventory_name"] = item_name;
  2025. payload["inventory_offer"] = info ? info->asLLSD() : LLSD();
  2026. LLSD args;
  2027. args["SUBJECT"] = subj;
  2028. args["MESSAGE"] = mes;
  2029. gNotifications.add(LLNotification::Params("GroupNotice").substitutions(args).payload(payload).timestamp(timestamp));
  2030. }
  2031. // Also send down the old path for now.
  2032. if (IM_GROUP_NOTICE_REQUESTED == dialog)
  2033. {
  2034. LLFloaterGroupInfo::showNotice(subj, mes, group_id,
  2035. has_inventory, item_name, info);
  2036. }
  2037. break;
  2038. }
  2039. case IM_GROUP_INVITATION:
  2040. {
  2041. //if (!is_linden && (is_busy || is_muted))
  2042. if (is_busy || is_muted)
  2043. {
  2044. busy_message(from_id);
  2045. }
  2046. else
  2047. {
  2048. llinfos << "Received IM_GROUP_INVITATION message." << llendl;
  2049. //MK
  2050. if (gRLenabled && gRLInterface.contains("setgroup"))
  2051. {
  2052. llinfos << "Invitation ignored due to RestrainedLove restrictions."
  2053. << llendl;
  2054. break;
  2055. }
  2056. //mk
  2057. // Read the binary bucket for more information.
  2058. struct invite_bucket_t
  2059. {
  2060. S32 membership_fee;
  2061. LLUUID role_id;
  2062. }* invite_bucket;
  2063. // Make sure the binary bucket is the correct size.
  2064. if (bucket_size != sizeof(invite_bucket_t))
  2065. {
  2066. llwarns << "Malformed group invite binary bucket"
  2067. << llendl;
  2068. break;
  2069. }
  2070. invite_bucket = (struct invite_bucket_t*)&binary_bucket[0];
  2071. S32 membership_fee = ntohl(invite_bucket->membership_fee);
  2072. LLSD payload;
  2073. payload["transaction_id"] = session_id;
  2074. payload["group_id"] = from_group ? from_id : aux_id;
  2075. payload["name"] = name;
  2076. payload["message"] = message;
  2077. payload["fee"] = membership_fee;
  2078. payload["use_offline_cap"] = session_id.isNull() &&
  2079. offline == IM_OFFLINE;
  2080. LLSD args;
  2081. args["MESSAGE"] = message;
  2082. gNotifications.add("JoinGroup", args, payload);
  2083. }
  2084. break;
  2085. }
  2086. case IM_INVENTORY_OFFERED:
  2087. case IM_TASK_INVENTORY_OFFERED:
  2088. {
  2089. // Someone has offered us some inventory.
  2090. LLOfferInfo* info = new LLOfferInfo;
  2091. if (dialog == IM_INVENTORY_OFFERED)
  2092. {
  2093. struct offer_agent_bucket_t
  2094. {
  2095. S8 asset_type;
  2096. LLUUID object_id;
  2097. }* bucketp;
  2098. if (bucket_size != sizeof(offer_agent_bucket_t))
  2099. {
  2100. llwarns << "Malformed inventory offer from agent"
  2101. << llendl;
  2102. break;
  2103. }
  2104. bucketp = (struct offer_agent_bucket_t*)&binary_bucket[0];
  2105. info->mType = (LLAssetType::EType)bucketp->asset_type;
  2106. info->mObjectID = bucketp->object_id;
  2107. info->mFromObject = false;
  2108. }
  2109. // IM_TASK_INVENTORY_OFFERED
  2110. else
  2111. {
  2112. if (bucket_size == sizeof(S8))
  2113. {
  2114. info->mType = (LLAssetType::EType)binary_bucket[0];
  2115. }
  2116. else
  2117. {
  2118. // Rider - The previous version of the protocol returned
  2119. // the wrong binary bucket... We still might be able to
  2120. // figure out the type even though the offer is not
  2121. // retrievable.
  2122. std::string str(reinterpret_cast<char*>(binary_bucket));
  2123. std::string str_type = str.substr(0, str.find('|'));
  2124. std::stringstream type_convert(str_type);
  2125. S32 type;
  2126. type_convert >> type;
  2127. // We could try AT_UNKNOWN which would be more accurate,
  2128. // but it would cause an auto decline.
  2129. info->mType = (LLAssetType::EType)type;
  2130. // Do not break in the case of a bad binary bucket. Go
  2131. // ahead and show the accept/decline popup even though it
  2132. // will not do anything.
  2133. llwarns << "Malformed inventory offer from object, type might be: "
  2134. << info->mType
  2135. << ". The offer will likely be impossible to process."
  2136. << llendl;
  2137. }
  2138. info->mObjectID.setNull();
  2139. info->mFromObject = true;
  2140. }
  2141. // In the case of an offline message, the transaction Id is in
  2142. // aux_id and the session_id is null
  2143. info->mTransactionID = session_id.notNull() ? session_id : aux_id;
  2144. info->mIM = dialog;
  2145. info->mFromID = from_id;
  2146. info->mFromGroup = from_group;
  2147. //MK
  2148. std::string folder_name(message);
  2149. if (gRLenabled &&
  2150. !gSavedSettings.getBool("RestrainedLoveForbidGiveToRLV") &&
  2151. info->mType == LLAssetType::AT_CATEGORY &&
  2152. gRLInterface.getRlvShare() &&
  2153. folder_name.find(RL_RLV_REDIR_FOLDER_PREFIX) == 1)
  2154. {
  2155. info->mFolderID = gRLInterface.getRlvShare()->getUUID();
  2156. }
  2157. else
  2158. //mk
  2159. {
  2160. info->mFolderID =
  2161. gInventory.findCategoryUUIDForType(LLFolderType::assetTypeToFolderType(info->mType));
  2162. }
  2163. if (dialog == IM_TASK_INVENTORY_OFFERED)
  2164. {
  2165. info->mFromObject = true;
  2166. }
  2167. else
  2168. {
  2169. info->mFromObject = false;
  2170. }
  2171. info->mFromName = name;
  2172. //MK
  2173. if (gRLenabled && gRLInterface.mContainsShowloc)
  2174. {
  2175. // Hide every occurrence of the Region and Parcel names if the
  2176. // location restriction is active
  2177. message = gRLInterface.getCensoredLocation(message);
  2178. }
  2179. //mk
  2180. info->mDesc = message;
  2181. info->extractSLURL();
  2182. info->mHost = sender;
  2183. is_muted = LLMuteList::isMuted(from_id, name);
  2184. if (is_muted)
  2185. {
  2186. static F32 last_notification = 0.f;
  2187. // Do not spam with such messages...
  2188. llinfos_once << "Declining inventory offer from muted object/agent: "
  2189. << info->mFromName << llendl;
  2190. if (gFrameTimeSeconds - last_notification > 30.f)
  2191. {
  2192. LLSD args;
  2193. args["NAME"] = info->mFromName;
  2194. gNotifications.add("MutedObjectOfferDeclined", args);
  2195. last_notification = gFrameTimeSeconds;
  2196. }
  2197. // Same as closing window
  2198. info->forceResponse(IOR_MUTED);
  2199. }
  2200. else if (is_busy && dialog != IM_TASK_INVENTORY_OFFERED &&
  2201. gSavedSettings.getBool("RejectNewInventoryWhenBusy"))
  2202. {
  2203. // Until throttling is implemented, busy mode should reject
  2204. // inventory instead of silently accepting it. SEE SL-39554
  2205. info->forceResponse(IOR_BUSY);
  2206. }
  2207. else
  2208. {
  2209. info->inventoryOfferHandler();
  2210. }
  2211. break;
  2212. }
  2213. case IM_INVENTORY_ACCEPTED:
  2214. {
  2215. args["NAME"] = name;
  2216. gNotifications.add("InventoryAccepted", args);
  2217. break;
  2218. }
  2219. case IM_INVENTORY_DECLINED:
  2220. {
  2221. args["NAME"] = name;
  2222. gNotifications.add("InventoryDeclined", args);
  2223. break;
  2224. }
  2225. case IM_GROUP_VOTE:
  2226. {
  2227. llwarns << "Received deprecated IM event: IM_GROUP_VOTE" << llendl;
  2228. break;
  2229. }
  2230. case IM_GROUP_ELECTION_DEPRECATED:
  2231. {
  2232. llwarns << "Received deprecated IM event: IM_GROUP_ELECTION_DEPRECATED"
  2233. << llendl;
  2234. break;
  2235. }
  2236. case IM_SESSION_SEND:
  2237. {
  2238. if (!is_linden && is_busy)
  2239. {
  2240. return;
  2241. }
  2242. LLFloaterIMSession* floaterp =
  2243. LLFloaterIMSession::findInstance(session_id);
  2244. // Only show messages if we have a session open (which should
  2245. // happen after you get an "invitation"
  2246. if (!floaterp)
  2247. {
  2248. // Check to see if this was a snoozed session, and whether the
  2249. // snooze delay expired or not.
  2250. snoozed_map_t::iterator it = mSnoozedSessions.find(session_id);
  2251. if (it == mSnoozedSessions.end())
  2252. {
  2253. return; // Unexpected message for a closed session: ignore.
  2254. }
  2255. if (it->second > gFrameTimeSeconds)
  2256. {
  2257. LL_DEBUGS("InstantMessaging") << "Ignoring message for snoozed session Id: "
  2258. << session_id << LL_ENDL;
  2259. return;
  2260. }
  2261. mSnoozedSessions.erase(it);
  2262. LL_DEBUGS("InstantMessaging") << "Restoring snoozed session Id: "
  2263. << session_id << LL_ENDL;
  2264. }
  2265. //MK
  2266. if (gRLenabled)
  2267. {
  2268. if (!gRLInterface.canReceiveIM(from_id))
  2269. {
  2270. // Agent is forbidden to receive IMs
  2271. return;
  2272. }
  2273. // Group session ?
  2274. if (floaterp && floaterp->isGroupSession() &&
  2275. !gRLInterface.canSendGroupIM(floaterp->getSessionName()))
  2276. {
  2277. // Agent is forbidden to receive group IMs
  2278. return;
  2279. }
  2280. }
  2281. if (gRLenabled && !gRLInterface.canReceiveIM(from_id))
  2282. {
  2283. // Agent is forbidden to receive IMs
  2284. return;
  2285. }
  2286. //mk
  2287. // Standard message, not from system
  2288. std::string saved;
  2289. if (offline == IM_OFFLINE)
  2290. {
  2291. saved = llformat("(Saved %s) ", formatted_time(timestamp).c_str());
  2292. }
  2293. buffer = separator_string + saved + message.substr(message_offset);
  2294. addMessage(session_id, from_id, name, buffer,
  2295. ll_safe_string((char*)binary_bucket), IM_SESSION_INVITE,
  2296. parent_estate_id, region_id, position, true);
  2297. if (gAutomationp)
  2298. {
  2299. gAutomationp->onInstantMsg(session_id, from_id, name, buffer);
  2300. }
  2301. chat.mText = "IM: " + name + separator_string + saved +
  2302. message.substr(message_offset);
  2303. LLFloaterChat::addChat(chat, true, from_id == gAgentID);
  2304. break;
  2305. }
  2306. case IM_FROM_TASK:
  2307. {
  2308. LL_DEBUGS("InstantMessaging") << "IM_FROM_TASK: owner: " << from_id
  2309. << " - Object name: " << name
  2310. << " - Object Id: " << session_id
  2311. << LL_ENDL;
  2312. if (from_id == gAgentID)
  2313. {
  2314. is_owned_by_me = true;
  2315. }
  2316. if ((is_busy && !is_owned_by_me) ||
  2317. LLMuteList::isMuted(from_id, LLMute::flagTextChat) ||
  2318. LLMuteList::isMuted(session_id, name, LLMute::flagTextChat))
  2319. {
  2320. return;
  2321. }
  2322. chat.mFromName = name;
  2323. // Build a link to open the object IM info window.
  2324. std::string location = ll_safe_string((char*)binary_bucket,
  2325. bucket_size);
  2326. LLSD query_string;
  2327. query_string["owner"] = from_id;
  2328. query_string["slurl"] = location.c_str();
  2329. query_string["name"] = name;
  2330. if (from_group)
  2331. {
  2332. query_string["groupowned"] = "true";
  2333. }
  2334. if (session_id.notNull())
  2335. {
  2336. chat.mFromID = session_id;
  2337. }
  2338. else
  2339. {
  2340. // This message originated on a region without the updated code
  2341. // for task id and slurl information. We just need a unique ID
  2342. // for this object that is not the owner ID. If it is the owner
  2343. // ID, it will overwrite the style that contains the link to
  2344. // that owner's profile. This is not ideal: it will make one
  2345. // style for all objects owned by the the same person/group.
  2346. // This works because the only thing we can really do in this
  2347. // case is show the owner name and link to their profile.
  2348. chat.mFromID = from_id ^ gAgentSessionID;
  2349. }
  2350. std::ostringstream link;
  2351. link << "secondlife:///app/objectim/" << session_id
  2352. << LLURI::mapToQueryString(query_string);
  2353. chat.mURL = link.str();
  2354. //MK
  2355. if (gRLenabled)
  2356. {
  2357. if (gRLInterface.mContainsShowloc)
  2358. {
  2359. // hide the url
  2360. chat.mURL = "";
  2361. // Hide every occurrence of the Region and Parcel names if
  2362. // the/ location restriction is active
  2363. message = gRLInterface.getCensoredLocation(message);
  2364. }
  2365. if (gRLInterface.mContainsShownames)
  2366. {
  2367. message = gRLInterface.getCensoredMessage(message);
  2368. }
  2369. }
  2370. //mk
  2371. chat.mText = name + separator_string +
  2372. message.substr(message_offset);
  2373. // Note: lie to LLFloaterChat::addChat(), pretending that this is
  2374. // NOT an IM, because IMs from objects do not open IM sessions.
  2375. // However, display it like a direct chat from object.
  2376. chat.mChatType = CHAT_TYPE_DIRECT;
  2377. chat.mOwnerID = from_id;
  2378. if (is_owned_by_me &&
  2379. HBViewerAutomation::checkLuaCommand(message, from_id, name))
  2380. {
  2381. return;
  2382. }
  2383. if (gAutomationp)
  2384. {
  2385. gAutomationp->onReceivedChat(chat.mChatType, from_id, name,
  2386. chat.mText);
  2387. }
  2388. LLFloaterChat::addChat(chat, false, false);
  2389. break;
  2390. }
  2391. case IM_FROM_TASK_AS_ALERT:
  2392. {
  2393. if (is_busy && !is_owned_by_me)
  2394. {
  2395. return;
  2396. }
  2397. // Construct a viewer alert for this message.
  2398. //MK
  2399. if (gRLenabled)
  2400. {
  2401. if (gRLInterface.mContainsShowloc)
  2402. {
  2403. // Hide every occurrence of the Region and Parcel names if
  2404. // the location restriction is active
  2405. message = gRLInterface.getCensoredLocation(message);
  2406. }
  2407. if (gRLInterface.mContainsShownames ||
  2408. gRLInterface.mContainsShownametags)
  2409. {
  2410. // Censor object IMs but not avatar IMs
  2411. message = gRLInterface.getCensoredMessage(message);
  2412. }
  2413. }
  2414. //mk
  2415. args["NAME"] = name;
  2416. args["MESSAGE"] = message;
  2417. gNotifications.add("ObjectMessage", args);
  2418. break;
  2419. }
  2420. case IM_BUSY_AUTO_RESPONSE:
  2421. {
  2422. if (is_muted)
  2423. {
  2424. LL_DEBUGS("InstantMessaging") << "Ignoring busy response from "
  2425. << from_id << LL_ENDL;
  2426. return;
  2427. }
  2428. else
  2429. {
  2430. // *TODO: translate.
  2431. buffer = llformat("%s (%s): %s", name.c_str(), "busy response",
  2432. message.substr(message_offset).c_str());
  2433. addMessage(session_id, from_id, name, buffer);
  2434. }
  2435. break;
  2436. }
  2437. case IM_LURE_USER:
  2438. case IM_TELEPORT_REQUEST:
  2439. {
  2440. if (LLMuteList::isMuted(from_id, name))
  2441. {
  2442. return;
  2443. }
  2444. //MK
  2445. bool auto_accept = false;
  2446. if (gRLenabled)
  2447. {
  2448. std::string behav = dialog == IM_LURE_USER ? "accepttp"
  2449. : "accepttprequest";
  2450. auto_accept = gRLInterface.contains(behav) ||
  2451. gRLInterface.contains(behav + ":" +
  2452. from_id.asString());
  2453. }
  2454. //mk
  2455. if (is_busy
  2456. //MK
  2457. // Even in busy mode, accept if we are forced to
  2458. && !auto_accept)
  2459. //mk
  2460. {
  2461. busy_message(from_id);
  2462. }
  2463. else
  2464. {
  2465. //MK
  2466. if (gRLenabled && dialog == IM_LURE_USER)
  2467. {
  2468. if (gRLInterface.containsWithoutException("tplure",
  2469. from_id.asString()) ||
  2470. (gRLInterface.mContainsUnsit &&
  2471. isAgentAvatarValid() && gAgentAvatarp->mIsSitting))
  2472. {
  2473. std::string response =
  2474. "The Resident you invited is prevented from accepting teleport offers. Please try again later.";
  2475. pack_instant_message(gAgentID, false, gAgentSessionID,
  2476. from_id, SYSTEM_FROM,
  2477. response.c_str(), IM_ONLINE,
  2478. IM_BUSY_AUTO_RESPONSE);
  2479. gAgent.sendReliableMessage();
  2480. return;
  2481. }
  2482. }
  2483. if (gRLenabled && dialog == IM_TELEPORT_REQUEST)
  2484. {
  2485. if (gRLInterface.containsWithoutException("tprequest",
  2486. from_id.asString()))
  2487. {
  2488. std::string response =
  2489. "The Resident you invited is prevented from accepting teleport requests. Please try again later.";
  2490. pack_instant_message(gAgentID, false, gAgentSessionID,
  2491. from_id, SYSTEM_FROM,
  2492. response.c_str(), IM_ONLINE,
  2493. IM_BUSY_AUTO_RESPONSE);
  2494. gAgent.sendReliableMessage();
  2495. return;
  2496. }
  2497. }
  2498. if (gRLenabled &&
  2499. (gRLInterface.mContainsShowloc ||
  2500. !gRLInterface.canReceiveIM(from_id)))
  2501. {
  2502. message = "(Hidden)";
  2503. }
  2504. if (gRLenabled && dialog == IM_LURE_USER && auto_accept)
  2505. {
  2506. // accepttp => the viewer acts like it was teleported by a god
  2507. gRLInterface.setAllowCancelTp(false);
  2508. LLSD payload;
  2509. payload["from_id"] = from_id;
  2510. payload["lure_id"] = session_id;
  2511. payload["godlike"] = true;
  2512. // do not show a message box, because you're about to be
  2513. // teleported.
  2514. gNotifications.forceResponse(LLNotification::Params("TeleportOffered").payload(payload), 0);
  2515. }
  2516. else if (gRLenabled && dialog == IM_TELEPORT_REQUEST && auto_accept)
  2517. {
  2518. // accepttprequest => the viewer automatically sends the TP
  2519. LLSD dummy_notification, dummy_response;
  2520. dummy_notification["payload"]["ids"][0] = from_id;
  2521. dummy_response["message"] = "Automatic teleport offer";
  2522. send_lures(dummy_notification, dummy_response);
  2523. }
  2524. else
  2525. //mk
  2526. {
  2527. LLSD args;
  2528. // *TODO:translate -> [FIRST] [LAST] (maybe)
  2529. args["NAME"] = name;
  2530. args["MESSAGE"] = message;
  2531. LLSD payload;
  2532. payload["from_id"] = from_id;
  2533. payload["lure_id"] = session_id;
  2534. payload["godlike"] = false;
  2535. if (dialog == IM_TELEPORT_REQUEST)
  2536. {
  2537. gNotifications.add("TeleportRequest", args, payload);
  2538. }
  2539. else
  2540. {
  2541. gNotifications.add("TeleportOffered", args, payload);
  2542. }
  2543. }
  2544. }
  2545. break;
  2546. }
  2547. case IM_GODLIKE_LURE_USER:
  2548. {
  2549. LLSD payload;
  2550. payload["from_id"] = from_id;
  2551. payload["lure_id"] = session_id;
  2552. payload["godlike"] = true;
  2553. // Do not show a message box, because you're about to be
  2554. // teleported.
  2555. gNotifications.forceResponse(LLNotification::Params("TeleportOffered").payload(payload), 0);
  2556. break;
  2557. }
  2558. case IM_GOTO_URL:
  2559. {
  2560. LLSD args;
  2561. // N.B.: this is for URLs sent by the system, not for URLs sent by
  2562. // scripts (i.e. llLoadURL)
  2563. if (bucket_size <= 0)
  2564. {
  2565. llwarns << "bad bucket_size: " << bucket_size
  2566. << " - aborting function." << llendl;
  2567. return;
  2568. }
  2569. std::string url;
  2570. url.assign((char*)binary_bucket, bucket_size - 1);
  2571. args["MESSAGE"] = message;
  2572. args["URL"] = url;
  2573. LLSD payload;
  2574. payload["url"] = url;
  2575. gNotifications.add("GotoURL", args, payload);
  2576. break;
  2577. }
  2578. case IM_FRIENDSHIP_OFFERED:
  2579. {
  2580. LLSD payload;
  2581. payload["from_id"] = from_id;
  2582. payload["session_id"] = session_id;;
  2583. payload["online"] = offline == IM_ONLINE;
  2584. payload["sender"] = sender.getIPandPort();
  2585. if (is_busy)
  2586. {
  2587. busy_message(from_id);
  2588. gNotifications.forceResponse(LLNotification::Params("OfferFriendship").payload(payload), 1);
  2589. }
  2590. else if (LLMuteList::isMuted(from_id, name))
  2591. {
  2592. gNotifications.forceResponse(LLNotification::Params("OfferFriendship").payload(payload), 1);
  2593. }
  2594. else
  2595. {
  2596. args["[NAME]"] = name;
  2597. //MK
  2598. if (gRLenabled && !gRLInterface.canReceiveIM(from_id))
  2599. {
  2600. message = "(Hidden)";
  2601. }
  2602. //mk
  2603. if (message.empty())
  2604. {
  2605. // Support for frienship offers from clients before 07/2008
  2606. gNotifications.add("OfferFriendshipNoMessage", args,
  2607. payload);
  2608. }
  2609. else
  2610. {
  2611. args["[MESSAGE]"] = message;
  2612. gNotifications.add("OfferFriendship", args, payload);
  2613. }
  2614. }
  2615. break;
  2616. }
  2617. case IM_FRIENDSHIP_ACCEPTED:
  2618. {
  2619. // In the case of an offline IM, the formFriendship() may be
  2620. // extraneous as the database should already include the
  2621. // relationship. But it does not hurt for dupes.
  2622. LLAvatarTracker::formFriendship(from_id);
  2623. std::vector<std::string> strings;
  2624. strings.emplace_back(from_id.asString());
  2625. send_generic_message("requestonlinenotification", strings);
  2626. args["NAME"] = name;
  2627. gNotifications.add("FriendshipAccepted", args);
  2628. break;
  2629. }
  2630. case IM_FRIENDSHIP_DECLINED_DEPRECATED:
  2631. default:
  2632. {
  2633. llwarns << "Instant message calling for unknown dialog "
  2634. << (S32)dialog << llendl;
  2635. }
  2636. }
  2637. if (gWindowp && gWindowp->getMinimized())
  2638. {
  2639. F32 flash_time = gSavedSettings.getF32("TaskBarButtonFlashTime");
  2640. if (flash_time > 0.f)
  2641. {
  2642. gWindowp->flashIcon(flash_time);
  2643. }
  2644. }
  2645. }
  2646. bool LLIMMgr::processIMTypingCore(EInstantMessage dialog,
  2647. const LLUUID& from_id,
  2648. const std::string& from_name, bool typing)
  2649. {
  2650. LLUUID session_id = computeSessionID(dialog, from_id);
  2651. LLFloaterIMSession* floaterp =
  2652. LLFloaterIMSession::findInstance(session_id);
  2653. if (floaterp)
  2654. {
  2655. floaterp->processIMTyping(from_id, from_name, typing);
  2656. return true;
  2657. }
  2658. return false;
  2659. }
  2660. void LLIMMgr::updateFloaterSessionID(const LLUUID& old_session_id,
  2661. const LLUUID& new_session_id)
  2662. {
  2663. LLFloaterIMSession* floaterp =
  2664. LLFloaterIMSession::findInstance(old_session_id);
  2665. if (floaterp)
  2666. {
  2667. floaterp->sessionInitReplyReceived(new_session_id);
  2668. }
  2669. }
  2670. // LLFloaterIMSession::sessionInitReplyReceived() above will call back this
  2671. // method:
  2672. //static
  2673. void LLIMMgr::deliverMessage(const std::string& utf8_text,
  2674. const LLUUID& im_session_id,
  2675. const LLUUID& other_participant_id,
  2676. EInstantMessage dialog)
  2677. {
  2678. std::string name;
  2679. gAgent.buildFullname(name);
  2680. const LLRelationship* info =
  2681. gAvatarTracker.getBuddyInfo(other_participant_id);
  2682. U8 offline = (!info || info->isOnline()) ? IM_ONLINE : IM_OFFLINE;
  2683. // Send message normally. Default to IM_SESSION_SEND unless it is nothing
  2684. // special, in which case it is probably an IM to everyone.
  2685. U8 new_dialog = dialog;
  2686. if (dialog != IM_NOTHING_SPECIAL)
  2687. {
  2688. new_dialog = IM_SESSION_SEND;
  2689. }
  2690. pack_instant_message(gAgentID, false, gAgentSessionID,
  2691. other_participant_id, name.c_str(), utf8_text.c_str(),
  2692. offline, (EInstantMessage)new_dialog, im_session_id);
  2693. gAgent.sendReliableMessage();
  2694. // If there is a mute list and this is not a group chat the target should
  2695. // not be in our mute list for some message types. Auto-remove them if
  2696. // present.
  2697. switch (dialog)
  2698. {
  2699. #if 0 // Enabling this makes it impossible to mute permanently a resident who
  2700. // initiated a group IM session (posting in the group chat would unmute
  2701. // them)
  2702. case IM_SESSION_INVITE:
  2703. #endif
  2704. case IM_NOTHING_SPECIAL:
  2705. case IM_GROUP_INVITATION:
  2706. case IM_INVENTORY_OFFERED:
  2707. case IM_SESSION_P2P_INVITE:
  2708. case IM_SESSION_CONFERENCE_START:
  2709. case IM_SESSION_SEND: // Marginal: erring on the side of hearing.
  2710. case IM_LURE_USER:
  2711. case IM_GODLIKE_LURE_USER:
  2712. case IM_FRIENDSHIP_OFFERED:
  2713. LLMuteList::autoRemove(other_participant_id, LLMuteList::AR_IM);
  2714. break;
  2715. default: ; // do nothing
  2716. }
  2717. }
  2718. //static
  2719. bool LLIMMgr::requestOfflineMessages()
  2720. {
  2721. LLMessageSystem* msg = gMessageSystemp;
  2722. if (!msg || gDisconnected)
  2723. {
  2724. return false;
  2725. }
  2726. if (!gAgent.regionCapabilitiesReceived())
  2727. {
  2728. return false;
  2729. }
  2730. if (!gSavedSettings.getBool("UseOfflineIMsCapability"))
  2731. {
  2732. return requestOfflineMessagesLegacy();
  2733. }
  2734. const std::string& cap_url = gAgent.getRegionCapability("ReadOfflineMsgs");
  2735. if (cap_url.empty() ||
  2736. // NOTE: Offline messages capability provides no session/transaction
  2737. // Ids for message AcceptFriendship and IM_GROUP_INVITATION to work,
  2738. // so make sure we have the necessary caps before using it.
  2739. !gAgent.hasRegionCapability("AcceptFriendship") ||
  2740. !gAgent.hasRegionCapability("AcceptGroupInvite"))
  2741. {
  2742. return requestOfflineMessagesLegacy();
  2743. }
  2744. LL_DEBUGS("InstantMessaging") << "Using capability for offline instant messages request"
  2745. << LL_ENDL;
  2746. gCoros.launch("requestOfflineMessagesCoro",
  2747. boost::bind(&LLIMMgr::requestOfflineMessagesCoro, cap_url));
  2748. return true;
  2749. }
  2750. //static
  2751. bool LLIMMgr::requestOfflineMessagesLegacy()
  2752. {
  2753. LLMessageSystem* msg = gMessageSystemp;
  2754. if (!msg)
  2755. {
  2756. return false;
  2757. }
  2758. LL_DEBUGS("InstantMessaging") << "Using UDP messaging for offline instant messages request"
  2759. << LL_ENDL;
  2760. msg->newMessageFast(_PREHASH_RetrieveInstantMessages);
  2761. msg->nextBlockFast(_PREHASH_AgentData);
  2762. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  2763. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  2764. gAgent.sendReliableMessage();
  2765. return true;
  2766. }
  2767. //static
  2768. void LLIMMgr::requestOfflineMessagesCoro(const std::string& url)
  2769. {
  2770. LLCoreHttpUtil::HttpCoroutineAdapter adapter("requestOfflineMessages");
  2771. LLSD result = adapter.getAndSuspend(url);
  2772. LLViewerRegion* regionp = gAgent.getRegion();
  2773. if (!regionp || !gIMMgrp)
  2774. {
  2775. return; // Viewer is likely disconnected or closing down !
  2776. }
  2777. LLCore::HttpStatus status =
  2778. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  2779. if (!status)
  2780. {
  2781. llwarns << "Error requesting offline messages via capability. Error: "
  2782. << status.toString() << llendl;
  2783. if (requestOfflineMessagesLegacy())
  2784. {
  2785. llinfos << "Sent offline messages request via legacy UDP messaging"
  2786. << llendl;
  2787. }
  2788. else
  2789. {
  2790. llwarns << "Failed to send offline messages request via legacy UDP messaging"
  2791. << llendl;
  2792. }
  2793. return;
  2794. }
  2795. const LLSD& contents =
  2796. result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT];
  2797. if (!contents.size())
  2798. {
  2799. llinfos << "No contents received for offline messages via capability"
  2800. << llendl;
  2801. return;
  2802. }
  2803. LLSD messages;
  2804. if (contents.isArray())
  2805. {
  2806. messages = *contents.beginArray();
  2807. }
  2808. else if (contents.has("messages"))
  2809. {
  2810. messages = contents["messages"];
  2811. }
  2812. else
  2813. {
  2814. llwarns << "Malformed contents received for offline messages via capability"
  2815. << llendl;
  2816. return;
  2817. }
  2818. if (!messages.isArray())
  2819. {
  2820. llwarns << "Malformed contents received for offline messages via capability"
  2821. << llendl;
  2822. return;
  2823. }
  2824. if (!messages.size())
  2825. {
  2826. // Nothing to process
  2827. return;
  2828. }
  2829. std::vector<U8> data;
  2830. LLVector3 position;
  2831. std::string message, from_name;
  2832. LLUUID session_id;
  2833. LLHost sender = regionp->getHost();
  2834. for (LLSD::array_iterator it = messages.beginArray(),
  2835. end = messages.endArray();
  2836. it != end; ++it)
  2837. {
  2838. const LLSD& message_data(*it);
  2839. LL_DEBUGS("InstantMessaging") << "Processing offline message:\n";
  2840. std::stringstream str;
  2841. LLSDSerialize::toPrettyXML(message_data, str);
  2842. LL_CONT << "\n" << str.str() << LL_ENDL;
  2843. EInstantMessage dialog =
  2844. (EInstantMessage)message_data["dialog"].asInteger();
  2845. if (message_data.has("message"))
  2846. {
  2847. message = message_data["message"].asString();
  2848. LL_DEBUGS("InstantMessaging") << "Found 'message'" << LL_ENDL;
  2849. }
  2850. else
  2851. {
  2852. message.clear();
  2853. LL_DEBUGS("InstantMessaging") << "No message !" << LL_ENDL;
  2854. }
  2855. U32 parent_estate_id = 1; // 1 = Main land
  2856. if (message_data.has("parent_estate_id"))
  2857. {
  2858. parent_estate_id = message_data["parent_estate_id"].asInteger();
  2859. LL_DEBUGS("InstantMessaging") << "Found 'parent_estate_id': "
  2860. << parent_estate_id << LL_ENDL;
  2861. }
  2862. if (message_data.has("position"))
  2863. {
  2864. position.setValue(message_data["position"]);
  2865. LL_DEBUGS("InstantMessaging") << "Found 'position'" << LL_ENDL;
  2866. }
  2867. else if (message_data.has("local_x"))
  2868. {
  2869. position.set(message_data["local_x"].asReal(),
  2870. message_data["local_y"].asReal(),
  2871. message_data["local_z"].asReal());
  2872. LL_DEBUGS("InstantMessaging") << "Found 'local_x/y/z'" << LL_ENDL;
  2873. }
  2874. else
  2875. {
  2876. position.clear();
  2877. LL_DEBUGS("InstantMessaging") << "No position !" << LL_ENDL;
  2878. }
  2879. data.clear();
  2880. if (message_data.has("binary_bucket"))
  2881. {
  2882. data = message_data["binary_bucket"].asBinary();
  2883. LL_DEBUGS("InstantMessaging") << "Found 'binary_bucket'"
  2884. << LL_ENDL;
  2885. }
  2886. else
  2887. {
  2888. data.push_back(0);
  2889. }
  2890. bool from_group;
  2891. if (message_data["from_group"].isInteger())
  2892. {
  2893. from_group = message_data["from_group"].asInteger();
  2894. }
  2895. else
  2896. {
  2897. from_group = message_data["from_group"].asString() == "Y";
  2898. }
  2899. if (message_data.has("transaction-id"))
  2900. {
  2901. session_id = message_data["transaction-id"].asUUID();
  2902. LL_DEBUGS("InstantMessaging") << "Found 'transaction-id': "
  2903. << session_id << LL_ENDL;
  2904. }
  2905. // Fallbacks, in case LL changes this field name for something more
  2906. // coherent (no dash is ever used in other names but underline is) or
  2907. // meaningful (this actually is a session Id) in the future... HB
  2908. else if (message_data.has("transaction_id"))
  2909. {
  2910. session_id = message_data["transaction_id"].asUUID();
  2911. LL_DEBUGS("InstantMessaging") << "Found 'transaction_id': "
  2912. << session_id << LL_ENDL;
  2913. }
  2914. else if (message_data.has("session_id"))
  2915. {
  2916. session_id = message_data["session_id"].asUUID();
  2917. LL_DEBUGS("InstantMessaging") << "Found 'session_id': "
  2918. << session_id << LL_ENDL;
  2919. }
  2920. else
  2921. {
  2922. session_id.setNull();
  2923. LL_DEBUGS("InstantMessaging") << "No session/transaction id !"
  2924. << LL_ENDL;
  2925. }
  2926. if (session_id.isNull() && dialog == IM_FROM_TASK)
  2927. {
  2928. session_id = message_data["asset_id"].asUUID();
  2929. LL_DEBUGS("InstantMessaging") << "IM_FROM_TASK: using the asset Id for the session Id"
  2930. << LL_ENDL;
  2931. }
  2932. U8 im_type = IM_OFFLINE;
  2933. if (message_data.has("offline"))
  2934. {
  2935. im_type = (U8)message_data["offline"].asInteger();
  2936. LL_DEBUGS("InstantMessaging") << "Found 'offline': "
  2937. << (S32)im_type << LL_ENDL;
  2938. }
  2939. if (message_data.has("from_agent_name"))
  2940. {
  2941. from_name = message_data["from_agent_name"].asString();
  2942. LL_DEBUGS("InstantMessaging") << "Found 'from_agent_name': "
  2943. << from_name << LL_ENDL;
  2944. }
  2945. else if (message_data.has("from_name"))
  2946. {
  2947. from_name = message_data["from_name"].asString();
  2948. LL_DEBUGS("InstantMessaging") << "Found 'from_name': "
  2949. << from_name << LL_ENDL;
  2950. }
  2951. else
  2952. {
  2953. from_name.clear();
  2954. LL_DEBUGS("InstantMessaging") << "No originator name !" << LL_ENDL;
  2955. }
  2956. gIMMgrp->processNewMessage(message_data["from_agent_id"].asUUID(),
  2957. from_group,
  2958. message_data["to_agent_id"].asUUID(),
  2959. im_type, dialog, session_id,
  2960. (U32)message_data["timestamp"].asInteger(),
  2961. from_name, message, parent_estate_id,
  2962. message_data["region_id"].asUUID(),
  2963. position, data.data(), data.size(), sender,
  2964. // Not necessarily an asset
  2965. message_data["asset_id"].asUUID());
  2966. }
  2967. }
  2968. class LLViewerChatterBoxSessionStartReply final : public LLHTTPNode
  2969. {
  2970. public:
  2971. void describe(Description& desc) const override
  2972. {
  2973. desc.shortInfo("Used for receiving a reply to a request to initialize an ChatterBox session");
  2974. desc.postAPI();
  2975. desc.input("{\"client_session_id\": UUID, \"session_id\": UUID, \"success\" boolean, \"reason\": string");
  2976. desc.source(__FILE__, __LINE__);
  2977. }
  2978. void post(ResponsePtr response, const LLSD& context,
  2979. const LLSD& input) const override
  2980. {
  2981. LLSD body;
  2982. LLUUID temp_session_id;
  2983. LLUUID session_id;
  2984. bool success;
  2985. if (!gIMMgrp) return;
  2986. body = input["body"];
  2987. success = body["success"].asBoolean();
  2988. temp_session_id = body["temp_session_id"].asUUID();
  2989. if (success)
  2990. {
  2991. session_id = body["session_id"].asUUID();
  2992. gIMMgrp->updateFloaterSessionID(temp_session_id, session_id);
  2993. LLFloaterIMSession* floaterp =
  2994. LLFloaterIMSession::findInstance(session_id);
  2995. if (floaterp)
  2996. {
  2997. floaterp->setSpeakers(body);
  2998. // Apply updates we have possibly received previously
  2999. floaterp->updateSpeakersList(gIMMgrp->getPendingAgentListUpdates(session_id));
  3000. if (body.has("session_info"))
  3001. {
  3002. floaterp->processSessionUpdate(body["session_info"]);
  3003. }
  3004. // Apply updates we have possibly received previously
  3005. floaterp->updateSpeakersList(gIMMgrp->getPendingAgentListUpdates(session_id));
  3006. }
  3007. gIMMgrp->clearPendingAgentListUpdates(session_id);
  3008. }
  3009. else
  3010. {
  3011. // Throw an error dialog and close the temp session's floater
  3012. LLFloaterIMSession* floaterp =
  3013. LLFloaterIMSession::findInstance(temp_session_id);
  3014. if (floaterp)
  3015. {
  3016. floaterp->showSessionStartError(body["error"].asString());
  3017. }
  3018. }
  3019. gIMMgrp->clearPendingAgentListUpdates(session_id);
  3020. }
  3021. };
  3022. class LLViewerChatterBoxSessionEventReply final : public LLHTTPNode
  3023. {
  3024. public:
  3025. void describe(Description& desc) const override
  3026. {
  3027. desc.shortInfo("Used for receiving a reply to a ChatterBox session event");
  3028. desc.postAPI();
  3029. desc.input("{\"event\": string, \"reason\": string, \"success\": boolean, \"session_id\": UUID");
  3030. desc.source(__FILE__, __LINE__);
  3031. }
  3032. void post(ResponsePtr response, const LLSD& context,
  3033. const LLSD& input) const override
  3034. {
  3035. LLUUID session_id;
  3036. bool success;
  3037. LLSD body = input["body"];
  3038. success = body["success"].asBoolean();
  3039. session_id = body["session_id"].asUUID();
  3040. if (!success)
  3041. {
  3042. // Throw an error dialog
  3043. LLFloaterIMSession* floaterp =
  3044. LLFloaterIMSession::findInstance(session_id);
  3045. if (floaterp)
  3046. {
  3047. floaterp->showSessionEventError(body["event"].asString(),
  3048. body["error"].asString());
  3049. }
  3050. }
  3051. }
  3052. };
  3053. class LLViewerForceCloseChatterBoxSession final : public LLHTTPNode
  3054. {
  3055. public:
  3056. void post(ResponsePtr response, const LLSD& context,
  3057. const LLSD& input) const override
  3058. {
  3059. LLUUID session_id = input["body"]["session_id"].asUUID();
  3060. LLFloaterIMSession* floaterp =
  3061. LLFloaterIMSession::findInstance(session_id);
  3062. if (floaterp)
  3063. {
  3064. std::string reason = input["body"]["reason"].asString();
  3065. floaterp->showSessionForceClose(reason);
  3066. }
  3067. }
  3068. };
  3069. class LLViewerChatterBoxSessionAgentListUpdates final : public LLHTTPNode
  3070. {
  3071. public:
  3072. void post(ResponsePtr responder, const LLSD& context,
  3073. const LLSD& input) const override
  3074. {
  3075. if (!gIMMgrp) return;
  3076. const LLSD& body = input["body"];
  3077. if (body.isMap())
  3078. {
  3079. LLUUID session_id = body["session_id"].asUUID();
  3080. gIMMgrp->processAgentListUpdates(session_id, body);
  3081. }
  3082. }
  3083. };
  3084. class LLViewerChatterBoxSessionUpdate final : public LLHTTPNode
  3085. {
  3086. public:
  3087. void post(ResponsePtr responder, const LLSD& context,
  3088. const LLSD& input) const override
  3089. {
  3090. const LLSD& body = input["body"];
  3091. if (!body.isMap())
  3092. {
  3093. return;
  3094. }
  3095. LLFloaterIMSession* floaterp =
  3096. LLFloaterIMSession::findInstance(body["session_id"].asUUID());
  3097. if (floaterp)
  3098. {
  3099. floaterp->processSessionUpdate(body["info"]);
  3100. }
  3101. }
  3102. };
  3103. class LLViewerChatterBoxInvitation final : public LLHTTPNode
  3104. {
  3105. public:
  3106. void post(ResponsePtr response, const LLSD& context,
  3107. const LLSD& input) const override
  3108. {
  3109. if (!gIMMgrp) return;
  3110. // For backwards compatiblity reasons... we need to still check for
  3111. // 'text' or 'voice' invitations... bleh
  3112. if (input["body"].has("instantmessage"))
  3113. {
  3114. LLSD message_params =
  3115. input["body"]["instantmessage"]["message_params"];
  3116. // Do something here to have the IM invite behave just like a
  3117. // normal IM; this is just replicated code from process_improved_im
  3118. // and should really go in its own function - jwolk
  3119. std::string message = message_params["message"].asString();
  3120. std::string name = message_params["from_name"].asString();
  3121. if (LLAvatarName::sOmitResidentAsLastName)
  3122. {
  3123. name = LLCacheName::cleanFullName(name);
  3124. }
  3125. LLUUID from_id = message_params["from_id"].asUUID();
  3126. LLUUID session_id = message_params["id"].asUUID();
  3127. const LLSD::Binary& bin_bucket =
  3128. message_params["data"]["binary_bucket"].asBinary();
  3129. U8 offline = (U8)message_params["offline"].asInteger();
  3130. time_t timestamp = (time_t)message_params["timestamp"].asInteger();
  3131. bool is_busy = gAgent.getBusy();
  3132. bool is_muted = LLMuteList::isMuted(from_id, name,
  3133. LLMute::flagTextChat);
  3134. bool is_linden = LLMuteList::isLinden(name);
  3135. std::string separator_string(": ");
  3136. size_t message_offset = 0;
  3137. // Handle IRC styled /me messages.
  3138. std::string prefix = message.substr(0, 4);
  3139. if (prefix == "/me " || prefix == "/me'")
  3140. {
  3141. separator_string.clear();
  3142. message_offset = 3;
  3143. }
  3144. LLChat chat;
  3145. chat.mMuted = is_muted && !is_linden;
  3146. chat.mFromID = from_id;
  3147. chat.mFromName = name;
  3148. if (!is_linden && (is_busy || is_muted))
  3149. {
  3150. return;
  3151. }
  3152. //MK
  3153. if (gRLenabled && !gRLInterface.canReceiveIM(from_id))
  3154. {
  3155. return;
  3156. }
  3157. //mk
  3158. // Standard message, not from system
  3159. std::string saved;
  3160. if (offline == IM_OFFLINE)
  3161. {
  3162. saved = llformat("(Saved %s) ",
  3163. formatted_time(timestamp).c_str());
  3164. }
  3165. std::string buffer = separator_string + saved +
  3166. message.substr(message_offset);
  3167. bool is_this_agent = false;
  3168. if (from_id == gAgentID)
  3169. {
  3170. is_this_agent = true;
  3171. }
  3172. // Do not process muted IMs
  3173. if (!is_this_agent && !LLMuteList::isLinden(name) &&
  3174. LLMuteList::isMuted(from_id, LLMute::flagTextChat))
  3175. {
  3176. // Muted agent
  3177. return;
  3178. }
  3179. else if (session_id.notNull() &&
  3180. LLMuteList::isMuted(session_id, LLMute::flagTextChat))
  3181. {
  3182. // Muted group
  3183. return;
  3184. }
  3185. gIMMgrp->addMessage(session_id, from_id, name, buffer,
  3186. std::string((char*)&bin_bucket[0]),
  3187. IM_SESSION_INVITE,
  3188. message_params["parent_estate_id"].asInteger(),
  3189. message_params["region_id"].asUUID(),
  3190. ll_vector3_from_sd(message_params["position"]),
  3191. true);
  3192. if (gAutomationp)
  3193. {
  3194. gAutomationp->onInstantMsg(session_id, from_id, name, buffer);
  3195. if (!LLFloaterIMSession::findInstance(session_id))
  3196. {
  3197. // If the automation script OnInstantMsg() callback closed
  3198. // the session as a result of this IM, abort now.
  3199. return;
  3200. }
  3201. }
  3202. chat.mText = "IM: " + name + separator_string + saved +
  3203. message.substr(message_offset);
  3204. LLFloaterChat::addChat(chat, true, is_this_agent);
  3205. // OK, now we want to accept the invitation
  3206. const std::string& url =
  3207. gAgent.getRegionCapability("ChatSessionRequest");
  3208. if (!url.empty())
  3209. {
  3210. gCoros.launch("chatterBoxInvitationCoro",
  3211. boost::bind(&LLIMMgr::chatterBoxInvitationCoro,
  3212. url, session_id,
  3213. LLIMMgr::INVITATION_TYPE_INSTANT_MESSAGE));
  3214. }
  3215. }
  3216. else if (input["body"].has("voice"))
  3217. {
  3218. if (!gVoiceClient.isVoiceWorking())
  3219. {
  3220. // Do not display voice invites unless the user has voice
  3221. // enabled
  3222. return;
  3223. }
  3224. const LLSD& voice = input["body"]["voice"];
  3225. constexpr S32 P2P_SESSION = 2; // Note: WebRTC P2P voice only.
  3226. bool p2p = voice.has("invitation_type") &&
  3227. voice["invitation_type"].asInteger() == P2P_SESSION;
  3228. gIMMgrp->inviteToSession(input["body"]["session_id"].asUUID(),
  3229. input["body"]["session_name"].asString(),
  3230. input["body"]["from_id"].asUUID(),
  3231. input["body"]["from_name"].asString(),
  3232. p2p ? IM_SESSION_P2P_INVITE
  3233. : IM_SESSION_INVITE,
  3234. LLIMMgr::INVITATION_TYPE_VOICE);
  3235. }
  3236. else if (input["body"].has("immediate"))
  3237. {
  3238. gIMMgrp->inviteToSession(input["body"]["session_id"].asUUID(),
  3239. input["body"]["session_name"].asString(),
  3240. input["body"]["from_id"].asUUID(),
  3241. input["body"]["from_name"].asString(),
  3242. IM_SESSION_INVITE,
  3243. LLIMMgr::INVITATION_TYPE_IMMEDIATE);
  3244. }
  3245. }
  3246. };
  3247. LLHTTPRegistration<LLViewerChatterBoxSessionStartReply>
  3248. gHTTPRegistrationMessageChatterboxsessionstartreply(
  3249. "/message/ChatterBoxSessionStartReply");
  3250. LLHTTPRegistration<LLViewerChatterBoxSessionEventReply>
  3251. gHTTPRegistrationMessageChatterboxsessioneventreply(
  3252. "/message/ChatterBoxSessionEventReply");
  3253. LLHTTPRegistration<LLViewerForceCloseChatterBoxSession>
  3254. gHTTPRegistrationMessageForceclosechatterboxsession(
  3255. "/message/ForceCloseChatterBoxSession");
  3256. LLHTTPRegistration<LLViewerChatterBoxSessionAgentListUpdates>
  3257. gHTTPRegistrationMessageChatterboxsessionagentlistupdates(
  3258. "/message/ChatterBoxSessionAgentListUpdates");
  3259. LLHTTPRegistration<LLViewerChatterBoxSessionUpdate>
  3260. gHTTPRegistrationMessageChatterBoxSessionUpdate(
  3261. "/message/ChatterBoxSessionUpdate");
  3262. LLHTTPRegistration<LLViewerChatterBoxInvitation>
  3263. gHTTPRegistrationMessageChatterBoxInvitation(
  3264. "/message/ChatterBoxInvitation");