UserProfileModule.cs 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.IO;
  29. using System.Text;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.Concurrent;
  33. using System.Globalization;
  34. using System.Linq;
  35. using System.Net;
  36. using System.Reflection;
  37. using System.Threading;
  38. using System.Xml;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using log4net;
  42. using Nini.Config;
  43. using OpenSim.Framework;
  44. using OpenSim.Region.Framework.Interfaces;
  45. using OpenSim.Region.Framework.Scenes;
  46. using OpenSim.Services.Interfaces;
  47. using Mono.Addins;
  48. using OpenSim.Services.Connectors.Hypergrid;
  49. using OpenSim.Framework.Servers.HttpServer;
  50. using OpenSim.Services.UserProfilesService;
  51. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  52. using Microsoft.CSharp;
  53. namespace OpenSim.Region.CoreModules.Avatar.UserProfiles
  54. {
  55. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserProfilesModule")]
  56. public class UserProfileModule : IProfileModule, INonSharedRegionModule
  57. {
  58. const double PROFILECACHEEXPIRE = 300;
  59. /// <summary>
  60. /// Logging
  61. /// </summary>
  62. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  63. // The pair of Dictionaries are used to handle the switching of classified ads
  64. // by maintaining a cache of classified id to creator id mappings and an interest
  65. // count. The entries are removed when the interest count reaches 0.
  66. readonly Dictionary<UUID, UUID> m_classifiedCache = new Dictionary<UUID, UUID>();
  67. readonly Dictionary<UUID, int> m_classifiedInterest = new Dictionary<UUID, int>();
  68. readonly ExpiringCacheOS<UUID, UserProfileCacheEntry> m_profilesCache = new ExpiringCacheOS<UUID, UserProfileCacheEntry>(60000);
  69. IAssetCache m_assetCache;
  70. IGroupsModule m_groupsModule = null;
  71. private JsonRpcRequestManager rpc = new JsonRpcRequestManager();
  72. private bool m_allowUserProfileWebURLs = true;
  73. struct AsyncPropsRequest
  74. {
  75. public IClientAPI client;
  76. public ScenePresence presence;
  77. public UUID agent;
  78. public int reqtype;
  79. }
  80. private ConcurrentStack<AsyncPropsRequest> m_asyncRequests = new ConcurrentStack<AsyncPropsRequest>();
  81. private object m_asyncRequestsLock = new object();
  82. private bool m_asyncRequestsRunning = false;
  83. private void ProcessRequests()
  84. {
  85. lock(m_asyncRequestsLock)
  86. {
  87. while (m_asyncRequests.TryPop(out AsyncPropsRequest req))
  88. {
  89. try
  90. {
  91. IClientAPI client = req.client;
  92. if(!client.IsActive)
  93. continue;
  94. if(req.reqtype == 0)
  95. {
  96. UUID avatarID = req.agent;
  97. ScenePresence p = req.presence;
  98. string serverURI = string.Empty;
  99. bool ok = true;
  100. bool foreign = GetUserProfileServerURI(avatarID, out serverURI);
  101. if(serverURI.Length == 0)
  102. ok = false;
  103. Byte[] membershipType = new Byte[1];
  104. string born = string.Empty;
  105. uint flags = 0x00;
  106. if (ok && GetUserAccountData(avatarID, out UserAccount acc))
  107. {
  108. int val_flags = acc.UserFlags;
  109. flags = (uint)(val_flags & 0xff);
  110. if (acc.UserTitle.Length == 0)
  111. membershipType[0] = (byte)((val_flags & 0x0f00) >> 8);
  112. else
  113. membershipType = Utils.StringToBytes(acc.UserTitle);
  114. int val_born = acc.Created;
  115. if (val_born != 0)
  116. born = Util.ToDateTime(val_born).ToString("M/d/yyyy", CultureInfo.InvariantCulture);
  117. // picky, picky
  118. }
  119. else
  120. ok = false;
  121. UserProfileProperties props = new UserProfileProperties();
  122. props.UserId = avatarID;
  123. if(ok)
  124. {
  125. string result = string.Empty;
  126. if (!GetProfileData(ref props, foreign, serverURI, out result))
  127. ok = false;
  128. }
  129. if (!ok)
  130. props.AboutText = "Profile not available at this time. User may still be unknown to this grid";
  131. if (!m_allowUserProfileWebURLs)
  132. props.WebUrl = "";
  133. GroupMembershipData[] agentGroups = null;
  134. if(ok && m_groupsModule != null)
  135. agentGroups = m_groupsModule.GetMembershipData(avatarID);
  136. HashSet<IClientAPI> clients;
  137. lock (m_profilesCache)
  138. {
  139. if (!m_profilesCache.TryGetValue(props.UserId, out UserProfileCacheEntry uce) || uce == null)
  140. uce = new UserProfileCacheEntry();
  141. uce.props = props;
  142. uce.born = born;
  143. uce.membershipType = membershipType;
  144. uce.flags = flags;
  145. clients = uce.ClientsWaitingProps;
  146. uce.ClientsWaitingProps = null;
  147. uce.avatarGroups = agentGroups;
  148. m_profilesCache.AddOrUpdate(props.UserId, uce, PROFILECACHEEXPIRE);
  149. }
  150. if (IsFriendOnline(req.client.AgentId, req.agent))
  151. flags |= (uint)ProfileFlags.Online;
  152. else
  153. flags &= (uint)~ProfileFlags.Online;
  154. if (clients == null)
  155. {
  156. client.SendAvatarProperties(props.UserId, props.AboutText, born, membershipType, props.FirstLifeText, flags,
  157. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  158. client.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask, props.WantToText,
  159. (uint)props.SkillsMask, props.SkillsText, props.Language);
  160. if (agentGroups != null)
  161. client.SendAvatarGroupsReply(avatarID, agentGroups);
  162. }
  163. else
  164. {
  165. if (!clients.Contains(client) && client.IsActive)
  166. {
  167. client.SendAvatarProperties(props.UserId, props.AboutText, born, membershipType, props.FirstLifeText, flags,
  168. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  169. client.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask, props.WantToText,
  170. (uint)props.SkillsMask, props.SkillsText, props.Language);
  171. if (agentGroups != null)
  172. client.SendAvatarGroupsReply(avatarID, agentGroups);
  173. }
  174. foreach (IClientAPI cli in clients)
  175. {
  176. if (!cli.IsActive)
  177. continue;
  178. cli.SendAvatarProperties(props.UserId, props.AboutText, born, membershipType, props.FirstLifeText, flags,
  179. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  180. cli.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask, props.WantToText,
  181. (uint)props.SkillsMask, props.SkillsText, props.Language);
  182. if (agentGroups != null)
  183. cli.SendAvatarGroupsReply(avatarID, agentGroups);
  184. }
  185. }
  186. }
  187. }
  188. catch (Exception e)
  189. {
  190. m_log.ErrorFormat("[UserProfileModule]: Process fail {0} : {1}", e.Message, e.StackTrace);
  191. }
  192. }
  193. m_asyncRequestsRunning = false;
  194. }
  195. }
  196. public Scene Scene
  197. {
  198. get; private set;
  199. }
  200. /// <summary>
  201. /// Gets or sets the ConfigSource.
  202. /// </summary>
  203. /// <value>
  204. /// The configuration
  205. /// </value>
  206. public IConfigSource Config
  207. {
  208. get;
  209. set;
  210. }
  211. /// <summary>
  212. /// Gets or sets the URI to the profile server.
  213. /// </summary>
  214. /// <value>
  215. /// The profile server URI.
  216. /// </value>
  217. public string ProfileServerUri
  218. {
  219. get;
  220. set;
  221. }
  222. IProfileModule ProfileModule
  223. {
  224. get; set;
  225. }
  226. IUserManagement UserManagementModule
  227. {
  228. get; set;
  229. }
  230. /// <summary>
  231. /// Gets or sets a value indicating whether this
  232. /// <see cref="OpenSim.Region.Coremodules.UserProfiles.UserProfileModule"/> is enabled.
  233. /// </summary>
  234. /// <value>
  235. /// <c>true</c> if enabled; otherwise, <c>false</c>.
  236. /// </value>
  237. public bool Enabled
  238. {
  239. get;
  240. set;
  241. }
  242. private GridInfo m_thisGridInfo;
  243. #region IRegionModuleBase implementation
  244. /// <summary>
  245. /// This is called to initialize the region module. For shared modules, this is called exactly once, after
  246. /// creating the single (shared) instance. For non-shared modules, this is called once on each instance, after
  247. /// the instace for the region has been created.
  248. /// </summary>
  249. /// <param name='source'>
  250. /// Source.
  251. /// </param>
  252. public void Initialise(IConfigSource source)
  253. {
  254. Config = source;
  255. ReplaceableInterface = typeof(IProfileModule);
  256. IConfig profileConfig = Config.Configs["UserProfiles"];
  257. if (profileConfig == null)
  258. {
  259. //m_log.Debug("[PROFILES]: UserProfiles disabled, no configuration");
  260. Enabled = false;
  261. return;
  262. }
  263. // If we find ProfileURL then we configure for FULL support
  264. // else we setup for BASIC support
  265. ProfileServerUri = profileConfig.GetString("ProfileServiceURL", "");
  266. if (string.IsNullOrEmpty(ProfileServerUri))
  267. {
  268. Enabled = false;
  269. return;
  270. }
  271. OSHTTPURI tmp = new OSHTTPURI(ProfileServerUri, true);
  272. if (!tmp.IsResolvedHost)
  273. {
  274. m_log.ErrorFormat("[UserProfileModule: {0}", tmp.IsValidHost ? "Could not resolve ProfileServiceURL" : "ProfileServiceURL is a invalid host");
  275. throw new Exception("UserProfileModule init error");
  276. }
  277. ProfileServerUri = tmp.URI;
  278. m_allowUserProfileWebURLs = profileConfig.GetBoolean("AllowUserProfileWebURLs", m_allowUserProfileWebURLs);
  279. m_log.Debug("[UserProfileModule]: Full Profiles Enabled");
  280. ReplaceableInterface = null;
  281. Enabled = true;
  282. }
  283. /// <summary>
  284. /// Adds the region.
  285. /// </summary>
  286. /// <param name='scene'>
  287. /// Scene.
  288. /// </param>
  289. public void AddRegion(Scene scene)
  290. {
  291. if(!Enabled)
  292. return;
  293. Scene = scene;
  294. if(m_thisGridInfo == null)
  295. m_thisGridInfo = scene.SceneGridInfo;
  296. Scene.RegisterModuleInterface<IProfileModule>(this);
  297. Scene.EventManager.OnNewClient += OnNewClient;
  298. Scene.EventManager.OnClientClosed += OnClientClosed;
  299. UserManagementModule = Scene.RequestModuleInterface<IUserManagement>();
  300. }
  301. /// <summary>
  302. /// Removes the region.
  303. /// </summary>
  304. /// <param name='scene'>
  305. /// Scene.
  306. /// </param>
  307. public void RemoveRegion(Scene scene)
  308. {
  309. if(!Enabled)
  310. return;
  311. m_profilesCache.Clear();
  312. m_classifiedCache.Clear();
  313. m_classifiedInterest.Clear();
  314. }
  315. /// <summary>
  316. /// This will be called once for every scene loaded. In a shared module this will be multiple times in one
  317. /// instance, while a nonshared module instance will only be called once. This method is called after AddRegion
  318. /// has been called in all modules for that scene, providing an opportunity to request another module's
  319. /// interface, or hook an event from another module.
  320. /// </summary>
  321. /// <param name='scene'>
  322. /// Scene.
  323. /// </param>
  324. public void RegionLoaded(Scene scene)
  325. {
  326. if(!Enabled)
  327. return;
  328. m_assetCache = Scene.RequestModuleInterface<IAssetCache>();
  329. m_groupsModule = Scene.RequestModuleInterface<IGroupsModule>();
  330. }
  331. /// <summary>
  332. /// If this returns non-null, it is the type of an interface that this module intends to register. This will
  333. /// cause the loader to defer loading of this module until all other modules have been loaded. If no other
  334. /// module has registered the interface by then, this module will be activated, else it will remain inactive,
  335. /// letting the other module take over. This should return non-null ONLY in modules that are intended to be
  336. /// easily replaceable, e.g. stub implementations that the developer expects to be replaced by third party
  337. /// provided modules.
  338. /// </summary>
  339. /// <value>
  340. /// The replaceable interface.
  341. /// </value>
  342. public Type ReplaceableInterface
  343. {
  344. get; private set;
  345. }
  346. /// <summary>
  347. /// Called as the instance is closed.
  348. /// </summary>
  349. public void Close()
  350. {
  351. m_thisGridInfo = null;
  352. }
  353. /// <value>
  354. /// The name of the module
  355. /// </value>
  356. /// <summary>
  357. /// Gets the module name.
  358. /// </summary>
  359. public string Name
  360. {
  361. get { return "UserProfileModule"; }
  362. }
  363. #endregion IRegionModuleBase implementation
  364. #region Region Event Handlers
  365. /// <summary>
  366. /// Raises the new client event.
  367. /// </summary>
  368. /// <param name='client'>
  369. /// Client.
  370. /// </param>
  371. void OnNewClient(IClientAPI client)
  372. {
  373. //Profile
  374. client.OnRequestAvatarProperties += RequestAvatarProperties;
  375. client.OnUpdateAvatarProperties += AvatarPropertiesUpdate;
  376. client.OnAvatarInterestUpdate += AvatarInterestsUpdate;
  377. // Classifieds
  378. client.AddGenericPacketHandler("avatarclassifiedsrequest", ClassifiedsRequest);
  379. client.OnClassifiedInfoUpdate += ClassifiedInfoUpdate;
  380. client.OnClassifiedInfoRequest += ClassifiedInfoRequest;
  381. client.OnClassifiedDelete += ClassifiedDelete;
  382. // Picks
  383. client.AddGenericPacketHandler("avatarpicksrequest", PicksRequest);
  384. client.AddGenericPacketHandler("pickinforequest", PickInfoRequest);
  385. client.OnPickInfoUpdate += PickInfoUpdate;
  386. client.OnPickDelete += PickDelete;
  387. // Notes
  388. client.AddGenericPacketHandler("avatarnotesrequest", NotesRequest);
  389. client.OnAvatarNotesUpdate += NotesUpdate;
  390. // Preferences
  391. client.OnUserInfoRequest += UserPreferencesRequest;
  392. client.OnUpdateUserInfo += UpdateUserPreferences;
  393. }
  394. void OnClientClosed(UUID AgentId, Scene scene)
  395. {
  396. ScenePresence sp = scene.GetScenePresence(AgentId);
  397. IClientAPI client = sp.ControllingClient;
  398. if (client == null)
  399. return;
  400. //Profile
  401. client.OnRequestAvatarProperties -= RequestAvatarProperties;
  402. client.OnUpdateAvatarProperties -= AvatarPropertiesUpdate;
  403. client.OnAvatarInterestUpdate -= AvatarInterestsUpdate;
  404. // Classifieds
  405. // client.r GenericPacketHandler("avatarclassifiedsrequest", ClassifiedsRequest);
  406. client.OnClassifiedInfoUpdate -= ClassifiedInfoUpdate;
  407. client.OnClassifiedInfoRequest -= ClassifiedInfoRequest;
  408. client.OnClassifiedDelete -= ClassifiedDelete;
  409. // Picks
  410. // client.AddGenericPacketHandler("avatarpicksrequest", PicksRequest);
  411. // client.AddGenericPacketHandler("pickinforequest", PickInfoRequest);
  412. client.OnPickInfoUpdate -= PickInfoUpdate;
  413. client.OnPickDelete -= PickDelete;
  414. // Notes
  415. // client.AddGenericPacketHandler("avatarnotesrequest", NotesRequest);
  416. client.OnAvatarNotesUpdate -= NotesUpdate;
  417. // Preferences
  418. client.OnUserInfoRequest -= UserPreferencesRequest;
  419. client.OnUpdateUserInfo -= UpdateUserPreferences;
  420. }
  421. #endregion Region Event Handlers
  422. #region Classified
  423. ///
  424. /// <summary>
  425. /// Handles the avatar classifieds request.
  426. /// </summary>
  427. /// <param name='sender'>
  428. /// Sender.
  429. /// </param>
  430. /// <param name='method'>
  431. /// Method.
  432. /// </param>
  433. /// <param name='args'>
  434. /// Arguments.
  435. /// </param>
  436. public void ClassifiedsRequest(Object sender, string method, List<String> args)
  437. {
  438. if (!(sender is IClientAPI))
  439. return;
  440. IClientAPI remoteClient = (IClientAPI)sender;
  441. Dictionary<UUID, string> classifieds = new Dictionary<UUID, string>();
  442. UUID targetID;
  443. if(!UUID.TryParse(args[0], out targetID) || targetID.IsZero())
  444. return;
  445. if (targetID == Constants.m_MrOpenSimID)
  446. {
  447. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  448. return;
  449. }
  450. ScenePresence p = FindPresence(targetID);
  451. if (p != null && p.IsNPC)
  452. {
  453. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  454. return;
  455. }
  456. lock(m_profilesCache)
  457. {
  458. if(m_profilesCache.TryGetValue(targetID, out UserProfileCacheEntry uce) && uce != null)
  459. {
  460. if(uce.classifiedsLists != null)
  461. {
  462. foreach(KeyValuePair<UUID,string> kvp in uce.classifiedsLists)
  463. {
  464. UUID kvpkey = kvp.Key;
  465. classifieds[kvpkey] = kvp.Value;
  466. lock (m_classifiedCache)
  467. {
  468. if (!m_classifiedCache.ContainsKey(kvpkey))
  469. {
  470. m_classifiedCache.Add(kvpkey,targetID);
  471. m_classifiedInterest.Add(kvpkey, 0);
  472. }
  473. m_classifiedInterest[kvpkey]++;
  474. }
  475. }
  476. remoteClient.SendAvatarClassifiedReply(targetID, uce.classifiedsLists);
  477. return;
  478. }
  479. }
  480. }
  481. string serverURI = string.Empty;
  482. GetUserProfileServerURI(targetID, out serverURI);
  483. if(string.IsNullOrWhiteSpace(serverURI))
  484. {
  485. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  486. return;
  487. }
  488. OSDMap parameters= new OSDMap() {{"creatorId", OSD.FromUUID(targetID) }};
  489. OSD osdtmp = parameters;
  490. if(!rpc.JsonRpcRequest(ref osdtmp, "avatarclassifiedsrequest", serverURI, UUID.Random().ToString()))
  491. {
  492. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  493. return;
  494. }
  495. parameters = (OSDMap)osdtmp;
  496. if(!parameters.TryGetValue("result", out osdtmp) || !(osdtmp is OSDArray))
  497. {
  498. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  499. return;
  500. }
  501. OSDArray list = (OSDArray)osdtmp;
  502. foreach(OSD map in list)
  503. {
  504. OSDMap m = (OSDMap)map;
  505. UUID cid = m["classifieduuid"].AsUUID();
  506. string name = m["name"].AsString();
  507. classifieds[cid] = name;
  508. lock (m_classifiedCache)
  509. {
  510. if (!m_classifiedCache.ContainsKey(cid))
  511. {
  512. m_classifiedCache.Add(cid,targetID);
  513. m_classifiedInterest.Add(cid, 0);
  514. }
  515. m_classifiedInterest[cid]++;
  516. }
  517. }
  518. lock(m_profilesCache)
  519. {
  520. if(!m_profilesCache.TryGetValue(targetID, out UserProfileCacheEntry uce) || uce == null)
  521. uce = new UserProfileCacheEntry();
  522. uce.classifiedsLists = classifieds;
  523. m_profilesCache.AddOrUpdate(targetID, uce, PROFILECACHEEXPIRE);
  524. }
  525. remoteClient.SendAvatarClassifiedReply(targetID, classifieds);
  526. }
  527. public void ClassifiedInfoRequest(UUID queryClassifiedID, IClientAPI remoteClient)
  528. {
  529. UUID target = remoteClient.AgentId;
  530. UserClassifiedAdd ad = new UserClassifiedAdd();
  531. ad.ClassifiedId = queryClassifiedID;
  532. lock (m_classifiedCache)
  533. {
  534. if (m_classifiedCache.ContainsKey(queryClassifiedID))
  535. {
  536. target = m_classifiedCache[queryClassifiedID];
  537. m_classifiedInterest[queryClassifiedID] --;
  538. if (m_classifiedInterest[queryClassifiedID] == 0)
  539. {
  540. m_classifiedInterest.Remove(queryClassifiedID);
  541. m_classifiedCache.Remove(queryClassifiedID);
  542. }
  543. }
  544. }
  545. UserProfileCacheEntry uce = null;
  546. lock(m_profilesCache)
  547. {
  548. if(m_profilesCache.TryGetValue(target, out uce) && uce != null)
  549. {
  550. if(uce.classifieds != null && uce.classifieds.ContainsKey(queryClassifiedID))
  551. {
  552. ad = uce.classifieds[queryClassifiedID];
  553. Vector3 gPos = new Vector3();
  554. Vector3.TryParse(ad.GlobalPos, out gPos);
  555. remoteClient.SendClassifiedInfoReply(ad.ClassifiedId, ad.CreatorId, (uint)ad.CreationDate,
  556. (uint)ad.ExpirationDate, (uint)ad.Category, ad.Name, ad.Description,
  557. ad.ParcelId, (uint)ad.ParentEstate, ad.SnapshotId, ad.SimName,
  558. gPos, ad.ParcelName, ad.Flags, ad.Price);
  559. return;
  560. }
  561. }
  562. }
  563. string serverURI = string.Empty;
  564. bool foreign = GetUserProfileServerURI(target, out serverURI);
  565. if(string.IsNullOrWhiteSpace(serverURI))
  566. {
  567. return;
  568. }
  569. object Adobject = ad;
  570. if(!rpc.JsonRpcRequest(ref Adobject, "classifieds_info_query", serverURI, UUID.Random().ToString()))
  571. {
  572. remoteClient.SendAgentAlertMessage(
  573. "Error getting classified info", false);
  574. return;
  575. }
  576. ad = (UserClassifiedAdd) Adobject;
  577. if(ad.CreatorId.IsZero())
  578. return;
  579. if(foreign)
  580. cacheForeignImage(target, ad.SnapshotId);
  581. lock(m_profilesCache)
  582. {
  583. if(!m_profilesCache.TryGetValue(target, out uce) || uce == null)
  584. uce = new UserProfileCacheEntry();
  585. if(uce.classifieds == null)
  586. uce.classifieds = new Dictionary<UUID, UserClassifiedAdd>();
  587. uce.classifieds[ad.ClassifiedId] = ad;
  588. m_profilesCache.AddOrUpdate(target, uce, PROFILECACHEEXPIRE);
  589. }
  590. Vector3 globalPos = new Vector3();
  591. Vector3.TryParse(ad.GlobalPos, out globalPos);
  592. remoteClient.SendClassifiedInfoReply(ad.ClassifiedId, ad.CreatorId, (uint)ad.CreationDate, (uint)ad.ExpirationDate,
  593. (uint)ad.Category, ad.Name, ad.Description, ad.ParcelId, (uint)ad.ParentEstate,
  594. ad.SnapshotId, ad.SimName, globalPos, ad.ParcelName, ad.Flags, ad.Price);
  595. }
  596. /// <summary>
  597. /// Classifieds info update.
  598. /// </summary>
  599. /// <param name='queryclassifiedID'>
  600. /// Queryclassified I.
  601. /// </param>
  602. /// <param name='queryCategory'>
  603. /// Query category.
  604. /// </param>
  605. /// <param name='queryName'>
  606. /// Query name.
  607. /// </param>
  608. /// <param name='queryDescription'>
  609. /// Query description.
  610. /// </param>
  611. /// <param name='queryParcelID'>
  612. /// Query parcel I.
  613. /// </param>
  614. /// <param name='queryParentEstate'>
  615. /// Query parent estate.
  616. /// </param>
  617. /// <param name='querySnapshotID'>
  618. /// Query snapshot I.
  619. /// </param>
  620. /// <param name='queryGlobalPos'>
  621. /// Query global position.
  622. /// </param>
  623. /// <param name='queryclassifiedFlags'>
  624. /// Queryclassified flags.
  625. /// </param>
  626. /// <param name='queryclassifiedPrice'>
  627. /// Queryclassified price.
  628. /// </param>
  629. /// <param name='remoteClient'>
  630. /// Remote client.
  631. /// </param>
  632. public void ClassifiedInfoUpdate(UUID queryclassifiedID, uint queryCategory, string queryName, string queryDescription, UUID queryParcelID,
  633. uint queryParentEstate, UUID querySnapshotID, Vector3 queryGlobalPos, byte queryclassifiedFlags,
  634. int queryclassifiedPrice, IClientAPI remoteClient)
  635. {
  636. Scene s = (Scene)remoteClient.Scene;
  637. Vector3 pos = remoteClient.SceneAgent.AbsolutePosition;
  638. ILandObject land = s.LandChannel.GetLandObject(pos.X, pos.Y);
  639. UUID creatorId = remoteClient.AgentId;
  640. ScenePresence p = FindPresence(creatorId);
  641. UserProfileCacheEntry uce = null;
  642. lock(m_profilesCache)
  643. m_profilesCache.TryGetValue(remoteClient.AgentId, out uce);
  644. string serverURI = string.Empty;
  645. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  646. if(string.IsNullOrWhiteSpace(serverURI))
  647. {
  648. return;
  649. }
  650. if(foreign)
  651. {
  652. remoteClient.SendAgentAlertMessage("Please change classifieds on your home grid", true);
  653. if(uce != null && uce.classifiedsLists != null)
  654. remoteClient.SendAvatarClassifiedReply(remoteClient.AgentId, uce.classifiedsLists);
  655. return;
  656. }
  657. OSDMap parameters = new OSDMap {{"creatorId", OSD.FromUUID(creatorId)}};
  658. OSD osdtmp = parameters;
  659. if (!rpc.JsonRpcRequest(ref osdtmp, "avatarclassifiedsrequest", serverURI, UUID.Random().ToString()))
  660. {
  661. remoteClient.SendAgentAlertMessage("Error fetching classifieds", false);
  662. return;
  663. }
  664. parameters = (OSDMap)osdtmp;
  665. OSDArray list = (OSDArray)parameters["result"];
  666. bool exists = list.Cast<OSDMap>().Where(map => map.ContainsKey("classifieduuid"))
  667. .Any(map => map["classifieduuid"].AsUUID().Equals(queryclassifiedID));
  668. IMoneyModule money = null;
  669. if (!exists)
  670. {
  671. money = s.RequestModuleInterface<IMoneyModule>();
  672. if (money != null)
  673. {
  674. if (!money.AmountCovered(remoteClient.AgentId, queryclassifiedPrice))
  675. {
  676. remoteClient.SendAgentAlertMessage("You do not have enough money to create this classified.", false);
  677. if(uce != null && uce.classifiedsLists != null)
  678. remoteClient.SendAvatarClassifiedReply(remoteClient.AgentId, uce.classifiedsLists);
  679. return;
  680. }
  681. }
  682. }
  683. UserClassifiedAdd ad = new UserClassifiedAdd();
  684. ad.ParcelName = land == null ? string.Empty : land.LandData.Name;
  685. ad.CreatorId = remoteClient.AgentId;
  686. ad.ClassifiedId = queryclassifiedID;
  687. ad.Category = Convert.ToInt32(queryCategory);
  688. ad.Name = queryName;
  689. ad.Description = queryDescription;
  690. ad.ParentEstate = Convert.ToInt32(queryParentEstate);
  691. ad.SnapshotId = querySnapshotID;
  692. ad.SimName = remoteClient.Scene.RegionInfo.RegionName;
  693. ad.GlobalPos = queryGlobalPos.ToString ();
  694. ad.Flags = queryclassifiedFlags;
  695. ad.Price = queryclassifiedPrice;
  696. ad.ParcelId = p.currentParcelUUID;
  697. object Ad = ad;
  698. if(!rpc.JsonRpcRequest(ref Ad, "classified_update", serverURI, UUID.Random().ToString()))
  699. {
  700. remoteClient.SendAgentAlertMessage("Error updating classified", false);
  701. if(uce != null && uce.classifiedsLists != null)
  702. remoteClient.SendAvatarClassifiedReply(remoteClient.AgentId, uce.classifiedsLists);
  703. return;
  704. }
  705. // only charge if it worked
  706. if (money != null)
  707. money.ApplyCharge(remoteClient.AgentId, queryclassifiedPrice, MoneyTransactionType.ClassifiedCharge);
  708. // just flush cache for now
  709. lock(m_profilesCache)
  710. {
  711. if(m_profilesCache.TryGetValue(remoteClient.AgentId, out uce) && uce != null)
  712. {
  713. uce.classifieds = null;
  714. uce.classifiedsLists = null;
  715. }
  716. }
  717. }
  718. /// <summary>
  719. /// Classifieds delete.
  720. /// </summary>
  721. /// <param name='queryClassifiedID'>
  722. /// Query classified I.
  723. /// </param>
  724. /// <param name='remoteClient'>
  725. /// Remote client.
  726. /// </param>
  727. public void ClassifiedDelete(UUID queryClassifiedID, IClientAPI remoteClient)
  728. {
  729. string serverURI = string.Empty;
  730. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  731. if(string.IsNullOrWhiteSpace(serverURI))
  732. return;
  733. if(foreign)
  734. {
  735. remoteClient.SendAgentAlertMessage("Please change classifieds on your home grid", true);
  736. return;
  737. }
  738. UUID classifiedId;
  739. if(!UUID.TryParse(queryClassifiedID.ToString(), out classifiedId))
  740. return;
  741. OSD Params = new OSDMap() {{ "classifiedId", OSD.FromUUID(classifiedId) }};
  742. if(!rpc.JsonRpcRequest(ref Params, "classified_delete", serverURI, UUID.Random().ToString()))
  743. {
  744. remoteClient.SendAgentAlertMessage(
  745. "Error deleting classified", false);
  746. return;
  747. }
  748. // flush cache
  749. UserProfileCacheEntry uce = null;
  750. lock(m_profilesCache)
  751. {
  752. if(m_profilesCache.TryGetValue(remoteClient.AgentId, out uce) && uce != null)
  753. {
  754. uce.classifieds = null;
  755. uce.classifiedsLists = null;
  756. }
  757. }
  758. }
  759. #endregion Classified
  760. #region Picks
  761. /// <summary>
  762. /// Handles the avatar picks request.
  763. /// </summary>
  764. /// <param name='sender'>
  765. /// Sender.
  766. /// </param>
  767. /// <param name='method'>
  768. /// Method.
  769. /// </param>
  770. /// <param name='args'>
  771. /// Arguments.
  772. /// </param>
  773. public void PicksRequest(Object sender, string method, List<String> args)
  774. {
  775. if (!(sender is IClientAPI))
  776. return;
  777. IClientAPI remoteClient = (IClientAPI)sender;
  778. UUID targetId;
  779. if(!UUID.TryParse(args[0], out targetId))
  780. return;
  781. Dictionary<UUID, string> picks = new Dictionary<UUID, string>();
  782. if (targetId == Constants.m_MrOpenSimID)
  783. {
  784. remoteClient.SendAvatarPicksReply(targetId, picks);
  785. return;
  786. }
  787. ScenePresence p = FindPresence(targetId);
  788. if (p != null && p.IsNPC)
  789. {
  790. remoteClient.SendAvatarPicksReply(targetId, picks);
  791. return;
  792. }
  793. UserProfileCacheEntry uce = null;
  794. lock(m_profilesCache)
  795. {
  796. if(m_profilesCache.TryGetValue(targetId, out uce) && uce != null)
  797. {
  798. if(uce != null && uce.picksList != null)
  799. {
  800. remoteClient.SendAvatarPicksReply(targetId, uce.picksList);
  801. return;
  802. }
  803. }
  804. }
  805. string serverURI = string.Empty;
  806. GetUserProfileServerURI(targetId, out serverURI);
  807. if(string.IsNullOrWhiteSpace(serverURI))
  808. {
  809. remoteClient.SendAvatarPicksReply(targetId, picks);
  810. return;
  811. }
  812. OSDMap parameters= new OSDMap() {{"creatorId", OSD.FromUUID(targetId)}};
  813. OSD osdtmp = parameters;
  814. if(!rpc.JsonRpcRequest(ref osdtmp, "avatarpicksrequest", serverURI, UUID.Random().ToString()))
  815. {
  816. remoteClient.SendAvatarPicksReply(targetId, picks);
  817. return;
  818. }
  819. parameters = (OSDMap)osdtmp;
  820. if(!parameters.TryGetValue("result", out osdtmp) || !(osdtmp is OSDArray))
  821. {
  822. remoteClient.SendAvatarPicksReply(targetId, picks);
  823. return;
  824. }
  825. OSDArray list = (OSDArray)osdtmp;
  826. foreach(OSD map in list)
  827. {
  828. OSDMap m = (OSDMap)map;
  829. UUID cid = m["pickuuid"].AsUUID();
  830. string name = m["name"].AsString();
  831. picks[cid] = name;
  832. }
  833. lock(m_profilesCache)
  834. {
  835. if(!m_profilesCache.TryGetValue(targetId, out uce) || uce == null)
  836. uce = new UserProfileCacheEntry();
  837. uce.picksList = picks;
  838. m_profilesCache.AddOrUpdate(targetId, uce, PROFILECACHEEXPIRE);
  839. }
  840. remoteClient.SendAvatarPicksReply(targetId, picks);
  841. }
  842. /// <summary>
  843. /// Handles the pick info request.
  844. /// </summary>
  845. /// <param name='sender'>
  846. /// Sender.
  847. /// </param>
  848. /// <param name='method'>
  849. /// Method.
  850. /// </param>
  851. /// <param name='args'>
  852. /// Arguments.
  853. /// </param>
  854. public void PickInfoRequest(Object sender, string method, List<String> args)
  855. {
  856. if (!(sender is IClientAPI))
  857. return;
  858. UserProfilePick pick = new UserProfilePick ();
  859. UUID targetID;
  860. if(!UUID.TryParse(args [0], out targetID))
  861. return;
  862. pick.CreatorId = targetID;
  863. if(!UUID.TryParse (args [1], out pick.PickId))
  864. return;
  865. IClientAPI remoteClient = (IClientAPI)sender;
  866. UserProfileCacheEntry uce = null;
  867. lock(m_profilesCache)
  868. {
  869. if(m_profilesCache.TryGetValue(targetID, out uce) && uce != null)
  870. {
  871. if(uce != null && uce.picks != null && uce.picks.ContainsKey(pick.PickId))
  872. {
  873. pick = uce.picks[pick.PickId];
  874. Vector3 gPos = new Vector3(Vector3.Zero);
  875. Vector3.TryParse(pick.GlobalPos, out gPos);
  876. remoteClient.SendPickInfoReply(pick.PickId,pick.CreatorId,pick.TopPick,pick.ParcelId,pick.Name,
  877. pick.Desc,pick.SnapshotId,pick.ParcelName,pick.OriginalName,pick.SimName,
  878. gPos,pick.SortOrder,pick.Enabled);
  879. return;
  880. }
  881. }
  882. }
  883. string serverURI = string.Empty;
  884. bool foreign = GetUserProfileServerURI (targetID, out serverURI);
  885. if(string.IsNullOrWhiteSpace(serverURI))
  886. {
  887. return;
  888. }
  889. object Pick = (object)pick;
  890. if (!rpc.JsonRpcRequest (ref Pick, "pickinforequest", serverURI, UUID.Random ().ToString ())) {
  891. remoteClient.SendAgentAlertMessage ("Error selecting pick", false);
  892. return;
  893. }
  894. pick = (UserProfilePick)Pick;
  895. if(foreign)
  896. cacheForeignImage(targetID, pick.SnapshotId);
  897. Vector3 globalPos = new Vector3(Vector3.Zero);
  898. Vector3.TryParse(pick.GlobalPos, out globalPos);
  899. if (m_thisGridInfo.IsLocalGrid(pick.Gatekeeper, true) == 0)
  900. {
  901. // Setup the illusion
  902. string region = string.Format("{0} {1}",pick.Gatekeeper,pick.SimName);
  903. GridRegion target = Scene.GridService.GetRegionByName(Scene.RegionInfo.ScopeID, region);
  904. if(target == null)
  905. {
  906. // This is a unreachable region
  907. }
  908. else
  909. {
  910. // we have a proxy on map
  911. ulong oriHandle;
  912. uint oriX;
  913. uint oriY;
  914. if(Util.ParseFakeParcelID(pick.ParcelId, out oriHandle, out oriX, out oriY))
  915. {
  916. pick.ParcelId = Util.BuildFakeParcelID(target.RegionHandle, oriX, oriY);
  917. globalPos.X = target.RegionLocX + oriX;
  918. globalPos.Y = target.RegionLocY + oriY;
  919. pick.GlobalPos = globalPos.ToString();
  920. }
  921. else
  922. {
  923. // this is a fail on large regions
  924. uint gtmp = (uint)globalPos.X >> 8;
  925. globalPos.X -= (gtmp << 8);
  926. gtmp = (uint)globalPos.Y >> 8;
  927. globalPos.Y -= (gtmp << 8);
  928. pick.ParcelId = Util.BuildFakeParcelID(target.RegionHandle, (uint)globalPos.X, (uint)globalPos.Y);
  929. globalPos.X += target.RegionLocX;
  930. globalPos.Y += target.RegionLocY;
  931. pick.GlobalPos = globalPos.ToString();
  932. }
  933. }
  934. }
  935. //m_log.DebugFormat("[PROFILES]: PickInfoRequest: {0} : {1}", pick.Name.ToString(), pick.SnapshotId.ToString());
  936. lock(m_profilesCache)
  937. {
  938. if(!m_profilesCache.TryGetValue(targetID, out uce) || uce == null)
  939. uce = new UserProfileCacheEntry();
  940. if(uce.picks == null)
  941. uce.picks = new Dictionary<UUID, UserProfilePick>();
  942. uce.picks[pick.PickId] = pick;
  943. m_profilesCache.AddOrUpdate(targetID, uce, PROFILECACHEEXPIRE);
  944. }
  945. // Pull the rabbit out of the hat
  946. remoteClient.SendPickInfoReply(pick.PickId,pick.CreatorId,pick.TopPick,pick.ParcelId,pick.Name,
  947. pick.Desc,pick.SnapshotId,pick.ParcelName,pick.OriginalName,pick.SimName,
  948. globalPos,pick.SortOrder,pick.Enabled);
  949. }
  950. /// <summary>
  951. /// Updates the userpicks
  952. /// </summary>
  953. /// <param name='remoteClient'>
  954. /// Remote client.
  955. /// </param>
  956. /// <param name='pickID'>
  957. /// Pick I.
  958. /// </param>
  959. /// <param name='creatorID'>
  960. /// the creator of the pick
  961. /// </param>
  962. /// <param name='topPick'>
  963. /// Top pick.
  964. /// </param>
  965. /// <param name='name'>
  966. /// Name.
  967. /// </param>
  968. /// <param name='desc'>
  969. /// Desc.
  970. /// </param>
  971. /// <param name='snapshotID'>
  972. /// Snapshot I.
  973. /// </param>
  974. /// <param name='sortOrder'>
  975. /// Sort order.
  976. /// </param>
  977. /// <param name='enabled'>
  978. /// Enabled.
  979. /// </param>
  980. public void PickInfoUpdate(IClientAPI remoteClient, UUID pickID, UUID creatorID, bool topPick, string name, string desc, UUID snapshotID, int sortOrder, bool enabled)
  981. {
  982. //m_log.DebugFormat("[PROFILES]: Start PickInfoUpdate Name: {0} PickId: {1} SnapshotId: {2}", name, pickID.ToString(), snapshotID.ToString());
  983. UserProfilePick pick = new UserProfilePick();
  984. string serverURI = string.Empty;
  985. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  986. if(string.IsNullOrWhiteSpace(serverURI))
  987. {
  988. return;
  989. }
  990. ScenePresence p = FindPresence(remoteClient.AgentId);
  991. Vector3 avaPos = p.AbsolutePosition;
  992. // Getting the global position for the Avatar
  993. Vector3 posGlobal = new Vector3(remoteClient.Scene.RegionInfo.WorldLocX + avaPos.X,
  994. remoteClient.Scene.RegionInfo.WorldLocY + avaPos.Y,
  995. avaPos.Z);
  996. string landParcelName = "My Parcel";
  997. // UUID landParcelID = p.currentParcelUUID;
  998. // to locate parcels we use a fake id that encodes the region handle
  999. // since we do not have a global locator
  1000. // this fails on HG
  1001. UUID landParcelID = Util.BuildFakeParcelID(remoteClient.Scene.RegionInfo.RegionHandle, (uint)avaPos.X, (uint)avaPos.Y);
  1002. ILandObject land = p.Scene.LandChannel.GetLandObject(avaPos.X, avaPos.Y);
  1003. if (land != null)
  1004. {
  1005. // If land found, use parcel uuid from here because the value from SP will be blank if the avatar hasnt moved
  1006. landParcelName = land.LandData.Name;
  1007. // landParcelID = land.LandData.GlobalID;
  1008. }
  1009. else
  1010. {
  1011. m_log.WarnFormat(
  1012. "[PROFILES]: PickInfoUpdate found no parcel info at {0},{1} in {2}",
  1013. avaPos.X, avaPos.Y, p.Scene.Name);
  1014. }
  1015. pick.PickId = pickID;
  1016. pick.CreatorId = creatorID;
  1017. pick.TopPick = topPick;
  1018. pick.Name = name;
  1019. pick.Desc = desc;
  1020. pick.ParcelId = landParcelID;
  1021. pick.SnapshotId = snapshotID;
  1022. pick.ParcelName = landParcelName;
  1023. pick.SimName = remoteClient.Scene.RegionInfo.RegionName;
  1024. pick.Gatekeeper = m_thisGridInfo.GateKeeperURLNoEndSlash;
  1025. pick.GlobalPos = posGlobal.ToString();
  1026. pick.SortOrder = sortOrder;
  1027. pick.Enabled = enabled;
  1028. object Pick = (object)pick;
  1029. if(!rpc.JsonRpcRequest(ref Pick, "picks_update", serverURI, UUID.Random().ToString()))
  1030. {
  1031. remoteClient.SendAgentAlertMessage(
  1032. "Error updating pick", false);
  1033. return;
  1034. }
  1035. UserProfileCacheEntry uce = null;
  1036. lock(m_profilesCache)
  1037. {
  1038. if(!m_profilesCache.TryGetValue(remoteClient.AgentId, out uce) || uce == null)
  1039. uce = new UserProfileCacheEntry();
  1040. if(uce.picks == null)
  1041. uce.picks = new Dictionary<UUID, UserProfilePick>();
  1042. if(uce.picksList == null)
  1043. uce.picksList = new Dictionary<UUID, string>();
  1044. uce.picks[pick.PickId] = pick;
  1045. uce.picksList[pick.PickId] = pick.Name;
  1046. m_profilesCache.AddOrUpdate(remoteClient.AgentId, uce, PROFILECACHEEXPIRE);
  1047. }
  1048. remoteClient.SendAvatarPicksReply(remoteClient.AgentId, uce.picksList);
  1049. remoteClient.SendPickInfoReply(pick.PickId,pick.CreatorId,pick.TopPick,pick.ParcelId,pick.Name,
  1050. pick.Desc,pick.SnapshotId,pick.ParcelName,pick.OriginalName,pick.SimName,
  1051. posGlobal,pick.SortOrder,pick.Enabled);
  1052. //m_log.DebugFormat("[PROFILES]: Finish PickInfoUpdate {0} {1}", pick.Name, pick.PickId.ToString());
  1053. }
  1054. /// <summary>
  1055. /// Delete a Pick
  1056. /// </summary>
  1057. /// <param name='remoteClient'>
  1058. /// Remote client.
  1059. /// </param>
  1060. /// <param name='queryPickID'>
  1061. /// Query pick I.
  1062. /// </param>
  1063. public void PickDelete(IClientAPI remoteClient, UUID queryPickID)
  1064. {
  1065. string serverURI = string.Empty;
  1066. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  1067. if(string.IsNullOrWhiteSpace(serverURI))
  1068. {
  1069. return;
  1070. }
  1071. OSDMap parameters= new OSDMap();
  1072. parameters.Add("pickId", OSD.FromUUID(queryPickID));
  1073. OSD Params = (OSD)parameters;
  1074. if(!rpc.JsonRpcRequest(ref Params, "picks_delete", serverURI, UUID.Random().ToString()))
  1075. {
  1076. remoteClient.SendAgentAlertMessage(
  1077. "Error picks delete", false);
  1078. return;
  1079. }
  1080. UserProfileCacheEntry uce = null;
  1081. lock(m_profilesCache)
  1082. {
  1083. if(m_profilesCache.TryGetValue(remoteClient.AgentId, out uce) && uce != null)
  1084. {
  1085. if(uce.picks != null && uce.picks.ContainsKey(queryPickID))
  1086. uce.picks.Remove(queryPickID);
  1087. if(uce.picksList != null && uce.picksList.ContainsKey(queryPickID))
  1088. uce.picksList.Remove(queryPickID);
  1089. m_profilesCache.AddOrUpdate(remoteClient.AgentId, uce, PROFILECACHEEXPIRE);
  1090. }
  1091. }
  1092. if(uce != null && uce.picksList != null)
  1093. remoteClient.SendAvatarPicksReply(remoteClient.AgentId, uce.picksList);
  1094. else
  1095. remoteClient.SendAvatarPicksReply(remoteClient.AgentId, new Dictionary<UUID, string>());
  1096. }
  1097. #endregion Picks
  1098. #region Notes
  1099. /// <summary>
  1100. /// Handles the avatar notes request.
  1101. /// </summary>
  1102. /// <param name='sender'>
  1103. /// Sender.
  1104. /// </param>
  1105. /// <param name='method'>
  1106. /// Method.
  1107. /// </param>
  1108. /// <param name='args'>
  1109. /// Arguments.
  1110. /// </param>
  1111. public void NotesRequest(Object sender, string method, List<String> args)
  1112. {
  1113. UserProfileNotes note = new UserProfileNotes();
  1114. if (!(sender is IClientAPI))
  1115. return;
  1116. if(!UUID.TryParse(args[0], out note.TargetId))
  1117. return;
  1118. IClientAPI remoteClient = (IClientAPI)sender;
  1119. note.UserId = remoteClient.AgentId;
  1120. string serverURI = string.Empty;
  1121. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  1122. if(string.IsNullOrWhiteSpace(serverURI))
  1123. {
  1124. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  1125. return;
  1126. }
  1127. object Note = (object)note;
  1128. if(!rpc.JsonRpcRequest(ref Note, "avatarnotesrequest", serverURI, UUID.Random().ToString()))
  1129. {
  1130. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  1131. return;
  1132. }
  1133. note = (UserProfileNotes) Note;
  1134. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  1135. }
  1136. /// <summary>
  1137. /// Avatars the notes update.
  1138. /// </summary>
  1139. /// <param name='remoteClient'>
  1140. /// Remote client.
  1141. /// </param>
  1142. /// <param name='queryTargetID'>
  1143. /// Query target I.
  1144. /// </param>
  1145. /// <param name='queryNotes'>
  1146. /// Query notes.
  1147. /// </param>
  1148. public void NotesUpdate(IClientAPI remoteClient, UUID queryTargetID, string queryNotes)
  1149. {
  1150. if (queryTargetID == Constants.m_MrOpenSimID)
  1151. return;
  1152. ScenePresence p = FindPresence(queryTargetID);
  1153. if (p != null && p.IsNPC)
  1154. {
  1155. remoteClient.SendAgentAlertMessage(
  1156. "Notes for NPCs not available", false);
  1157. return;
  1158. }
  1159. UserProfileNotes note = new UserProfileNotes();
  1160. note.UserId = remoteClient.AgentId;
  1161. note.TargetId = queryTargetID;
  1162. note.Notes = queryNotes;
  1163. string serverURI = string.Empty;
  1164. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  1165. if(string.IsNullOrWhiteSpace(serverURI))
  1166. return;
  1167. object Note = note;
  1168. if(!rpc.JsonRpcRequest(ref Note, "avatar_notes_update", serverURI, UUID.Random().ToString()))
  1169. {
  1170. remoteClient.SendAgentAlertMessage(
  1171. "Error updating note", false);
  1172. return;
  1173. }
  1174. }
  1175. #endregion Notes
  1176. #region User Preferences
  1177. /// <summary>
  1178. /// Updates the user preferences.
  1179. /// </summary>
  1180. /// <param name='imViaEmail'>
  1181. /// Im via email.
  1182. /// </param>
  1183. /// <param name='visible'>
  1184. /// Visible.
  1185. /// </param>
  1186. /// <param name='remoteClient'>
  1187. /// Remote client.
  1188. /// </param>
  1189. public void UpdateUserPreferences(bool imViaEmail, bool visible, IClientAPI remoteClient)
  1190. {
  1191. UserPreferences pref = new UserPreferences();
  1192. pref.UserId = remoteClient.AgentId;
  1193. pref.IMViaEmail = imViaEmail;
  1194. pref.Visible = visible;
  1195. string serverURI = string.Empty;
  1196. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  1197. if(string.IsNullOrWhiteSpace(serverURI))
  1198. return;
  1199. object Pref = pref;
  1200. if(!rpc.JsonRpcRequest(ref Pref, "user_preferences_update", serverURI, UUID.Random().ToString()))
  1201. {
  1202. m_log.InfoFormat("[PROFILES]: UserPreferences update error");
  1203. remoteClient.SendAgentAlertMessage("Error updating preferences", false);
  1204. return;
  1205. }
  1206. }
  1207. /// <summary>
  1208. /// Users the preferences request.
  1209. /// </summary>
  1210. /// <param name='remoteClient'>
  1211. /// Remote client.
  1212. /// </param>
  1213. public void UserPreferencesRequest(IClientAPI remoteClient)
  1214. {
  1215. UserPreferences pref = new UserPreferences();
  1216. pref.UserId = remoteClient.AgentId;
  1217. string serverURI = string.Empty;
  1218. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  1219. if(string.IsNullOrWhiteSpace(serverURI))
  1220. return;
  1221. object Pref = (object)pref;
  1222. if(!rpc.JsonRpcRequest(ref Pref, "user_preferences_request", serverURI, UUID.Random().ToString()))
  1223. {
  1224. // m_log.InfoFormat("[PROFILES]: UserPreferences request error");
  1225. // remoteClient.SendAgentAlertMessage("Error requesting preferences", false);
  1226. return;
  1227. }
  1228. pref = (UserPreferences) Pref;
  1229. remoteClient.SendUserInfoReply(pref.IMViaEmail, pref.Visible, pref.EMail);
  1230. }
  1231. #endregion User Preferences
  1232. #region Avatar Properties
  1233. /// <summary>
  1234. /// Update the avatars interests .
  1235. /// </summary>
  1236. /// <param name='remoteClient'>
  1237. /// Remote client.
  1238. /// </param>
  1239. /// <param name='wantmask'>
  1240. /// Wantmask.
  1241. /// </param>
  1242. /// <param name='wanttext'>
  1243. /// Wanttext.
  1244. /// </param>
  1245. /// <param name='skillsmask'>
  1246. /// Skillsmask.
  1247. /// </param>
  1248. /// <param name='skillstext'>
  1249. /// Skillstext.
  1250. /// </param>
  1251. /// <param name='languages'>
  1252. /// Languages.
  1253. /// </param>
  1254. public void AvatarInterestsUpdate(IClientAPI remoteClient, uint wantmask, string wanttext, uint skillsmask, string skillstext, string languages)
  1255. {
  1256. GetUserProfileServerURI(remoteClient.AgentId, out string serverURI);
  1257. if (string.IsNullOrWhiteSpace(serverURI))
  1258. return;
  1259. object Param = new UserProfileProperties()
  1260. {
  1261. UserId = remoteClient.AgentId,
  1262. WantToMask = (int)wantmask,
  1263. WantToText = wanttext,
  1264. SkillsMask = (int)skillsmask,
  1265. SkillsText = skillstext,
  1266. Language = languages
  1267. };
  1268. if(!rpc.JsonRpcRequest(ref Param, "avatar_interests_update", serverURI, UUID.Random().ToString()))
  1269. {
  1270. remoteClient.SendAgentAlertMessage(
  1271. "Error updating interests", false);
  1272. return;
  1273. }
  1274. // flush cache
  1275. lock(m_profilesCache)
  1276. {
  1277. if(m_profilesCache.TryGetValue(remoteClient.AgentId, out UserProfileCacheEntry uce) && uce != null)
  1278. {
  1279. uce.props = null;
  1280. uce.ClientsWaitingProps = null;
  1281. }
  1282. }
  1283. RequestAvatarProperties(remoteClient, remoteClient.AgentId);
  1284. }
  1285. public void RequestAvatarProperties(IClientAPI remoteClient, UUID avatarID)
  1286. {
  1287. if (avatarID.IsZero())
  1288. {
  1289. // Looking for a reason that some viewers are sending null Id's
  1290. m_log.Debug("[PROFILES]: got request of null ID");
  1291. return;
  1292. }
  1293. if (avatarID.Equals(Constants.m_MrOpenSimID))
  1294. {
  1295. remoteClient.SendAvatarProperties(avatarID, "Creator of OpenSimulator shared assets library", Constants.m_MrOpenSimBorn.ToString(),
  1296. Utils.StringToBytes("System agent"), "MrOpenSim has no life", 0x10,
  1297. UUID.Zero, UUID.Zero, "", UUID.Zero);
  1298. remoteClient.SendAvatarInterestsReply(avatarID, 0, "",
  1299. 0, "Getting into trouble", "Droidspeak");
  1300. return;
  1301. }
  1302. ScenePresence p = FindPresence(avatarID);
  1303. if (p != null && p.IsNPC)
  1304. {
  1305. remoteClient.SendAvatarProperties(avatarID, ((INPC)(p.ControllingClient)).profileAbout, ((INPC)(p.ControllingClient)).Born,
  1306. Utils.StringToBytes("Non Player Character (NPC)"), "NPCs have no life", 0x10,
  1307. UUID.Zero, ((INPC)(p.ControllingClient)).profileImage, "", UUID.Zero);
  1308. remoteClient.SendAvatarInterestsReply(avatarID, 0, "",
  1309. 0, "Getting into trouble", "Droidspeak");
  1310. return;
  1311. }
  1312. UserProfileProperties props;
  1313. lock(m_profilesCache)
  1314. {
  1315. if(m_profilesCache.TryGetValue(avatarID, out UserProfileCacheEntry uce) && uce != null)
  1316. {
  1317. if(uce.props != null)
  1318. {
  1319. props = uce.props;
  1320. uint cflags = uce.flags;
  1321. if (IsFriendOnline(remoteClient.AgentId, avatarID))
  1322. cflags = (uint)ProfileFlags.Online;
  1323. else
  1324. cflags &= (uint)~ProfileFlags.Online;
  1325. remoteClient.SendAvatarProperties(props.UserId, props.AboutText,
  1326. uce.born, uce.membershipType , props.FirstLifeText, cflags,
  1327. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  1328. remoteClient.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask,
  1329. props.WantToText, (uint)props.SkillsMask,
  1330. props.SkillsText, props.Language);
  1331. if(uce.avatarGroups != null)
  1332. remoteClient.SendAvatarGroupsReply(avatarID, uce.avatarGroups);
  1333. return;
  1334. }
  1335. else
  1336. {
  1337. if(uce.ClientsWaitingProps == null)
  1338. uce.ClientsWaitingProps = new HashSet<IClientAPI>();
  1339. else if(uce.ClientsWaitingProps.Contains(remoteClient))
  1340. return;
  1341. uce.ClientsWaitingProps.Add(remoteClient);
  1342. }
  1343. }
  1344. else
  1345. {
  1346. uce = new UserProfileCacheEntry();
  1347. uce.ClientsWaitingProps = new HashSet<IClientAPI>();
  1348. uce.ClientsWaitingProps.Add(remoteClient);
  1349. m_profilesCache.AddOrUpdate(avatarID, uce, PROFILECACHEEXPIRE);
  1350. }
  1351. }
  1352. AsyncPropsRequest req = new AsyncPropsRequest();
  1353. req.client = remoteClient;
  1354. req.presence = p;
  1355. req.agent = avatarID;
  1356. req.reqtype = 0;
  1357. m_asyncRequests.Push(req);
  1358. if (Monitor.TryEnter(m_asyncRequestsLock))
  1359. {
  1360. if (!m_asyncRequestsRunning)
  1361. {
  1362. m_asyncRequestsRunning = true;
  1363. Util.FireAndForget(x => ProcessRequests());
  1364. }
  1365. Monitor.Exit(m_asyncRequestsLock);
  1366. }
  1367. }
  1368. /// <summary>
  1369. /// Updates the avatar properties.
  1370. /// </summary>
  1371. /// <param name='remoteClient'>
  1372. /// Remote client.
  1373. /// </param>
  1374. /// <param name='newProfile'>
  1375. /// New profile.
  1376. /// </param>
  1377. public void AvatarPropertiesUpdate(IClientAPI remoteClient, UserProfileProperties newProfile)
  1378. {
  1379. GetUserProfileServerURI(remoteClient.AgentId, out string serverURI);
  1380. if (string.IsNullOrWhiteSpace(serverURI))
  1381. return;
  1382. if (!m_allowUserProfileWebURLs)
  1383. newProfile.WebUrl = string.Empty;
  1384. object Prop = newProfile;
  1385. if(!rpc.JsonRpcRequest(ref Prop, "avatar_properties_update", serverURI, UUID.Random().ToString()))
  1386. {
  1387. remoteClient.SendAgentAlertMessage("Error updating properties", false);
  1388. return;
  1389. }
  1390. // flush cache
  1391. lock(m_profilesCache)
  1392. {
  1393. if(m_profilesCache.TryGetValue(remoteClient.AgentId, out UserProfileCacheEntry uce) && uce != null)
  1394. {
  1395. uce.props = null;
  1396. uce.ClientsWaitingProps = null;
  1397. }
  1398. }
  1399. RequestAvatarProperties(remoteClient, remoteClient.AgentId);
  1400. }
  1401. /// <summary>
  1402. /// Gets the profile data.
  1403. /// </summary>
  1404. /// <returns>
  1405. /// The profile data.
  1406. /// </returns>
  1407. bool GetProfileData(ref UserProfileProperties properties, bool foreign, string serverURI, out string message)
  1408. {
  1409. if (String.IsNullOrEmpty(serverURI))
  1410. {
  1411. message = "User profile service unknown at this time";
  1412. return false;
  1413. }
  1414. object Prop = properties;
  1415. if (!rpc.JsonRpcRequest(ref Prop, "avatar_properties_request", serverURI, UUID.Random().ToString()))
  1416. {
  1417. // If it's a foreign user then try again using OpenProfile, in case that's what the grid is using
  1418. bool secondChanceSuccess = false;
  1419. if (foreign)
  1420. {
  1421. try
  1422. {
  1423. OpenProfileClient client = new OpenProfileClient(serverURI);
  1424. if (client.RequestAvatarPropertiesUsingOpenProfile(ref properties))
  1425. secondChanceSuccess = true;
  1426. }
  1427. catch (Exception e)
  1428. {
  1429. m_log.Debug(
  1430. string.Format(
  1431. "[PROFILES]: Request using the OpenProfile API for user {0} to {1} failed",
  1432. properties.UserId, serverURI),
  1433. e);
  1434. // Allow the return 'message' to say "JsonRpcRequest" and not "OpenProfile", because
  1435. // the most likely reason that OpenProfile failed is that the remote server
  1436. // doesn't support OpenProfile, and that's not very interesting.
  1437. }
  1438. }
  1439. if (!secondChanceSuccess)
  1440. {
  1441. message = string.Format("JsonRpcRequest for user {0} to {1} failed", properties.UserId, serverURI);
  1442. m_log.DebugFormat("[PROFILES]: {0}", message);
  1443. return false;
  1444. }
  1445. }
  1446. properties = (UserProfileProperties)Prop;
  1447. if(foreign)
  1448. {
  1449. cacheForeignImage(properties.UserId, properties.ImageId);
  1450. cacheForeignImage(properties.UserId, properties.FirstLifeImageId);
  1451. }
  1452. message = "Success";
  1453. return true;
  1454. }
  1455. #endregion Avatar Properties
  1456. #region Utils
  1457. /// <summary>
  1458. /// Gets the user account data.
  1459. /// </summary>
  1460. /// <returns>
  1461. /// The user profile data.
  1462. /// </returns>
  1463. /// <param name='userID'>
  1464. /// If set to <c>true</c> user I.
  1465. /// </param>
  1466. /// <param name='userInfo'>
  1467. /// If set to <c>true</c> user info.
  1468. /// </param>
  1469. bool GetUserAccountData(UUID userID, out UserAccount account)
  1470. {
  1471. account = null;
  1472. if (UserManagementModule.IsLocalGridUser(userID))
  1473. {
  1474. // Is local
  1475. IUserAccountService uas = Scene.UserAccountService;
  1476. account = uas.GetUserAccount(Scene.RegionInfo.ScopeID, userID);
  1477. return account != null;
  1478. }
  1479. else
  1480. {
  1481. // Is Foreign
  1482. string home_url = UserManagementModule.GetUserServerURL(userID, "HomeURI", out bool recentFailedWeb);
  1483. if (recentFailedWeb || String.IsNullOrEmpty(home_url))
  1484. return false;
  1485. UserAgentServiceConnector uConn = new UserAgentServiceConnector(home_url);
  1486. Dictionary<string, object> info;
  1487. try
  1488. {
  1489. info = uConn.GetUserInfo(userID);
  1490. }
  1491. catch (Exception e)
  1492. {
  1493. m_log.Debug("[PROFILES]: GetUserInfo call failed ", e);
  1494. UserManagementModule.UserWebFailed(userID);
  1495. return false;
  1496. }
  1497. if (info.Count == 0)
  1498. return false;
  1499. account = new UserAccount();
  1500. if (info.ContainsKey("user_flags"))
  1501. account.UserFlags = (int)info["user_flags"];
  1502. if (info.ContainsKey("user_created"))
  1503. account.Created = (int)info["user_created"];
  1504. account.UserTitle = "HG Visitor";
  1505. return true;
  1506. }
  1507. }
  1508. /// <summary>
  1509. /// Gets the user profile server UR.
  1510. /// </summary>
  1511. /// <returns>
  1512. /// The user profile server UR.
  1513. /// </returns>
  1514. /// <param name='userID'>
  1515. /// If set to <c>true</c> user I.
  1516. /// </param>
  1517. /// <param name='serverURI'>
  1518. /// If set to <c>true</c> server UR.
  1519. /// </param>
  1520. bool GetUserProfileServerURI(UUID userID, out string serverURI)
  1521. {
  1522. if (!UserManagementModule.IsLocalGridUser(userID))
  1523. {
  1524. serverURI = UserManagementModule.GetUserServerURL(userID, "ProfileServerURI", out bool failed);
  1525. if(failed)
  1526. serverURI = string.Empty;
  1527. // Is Foreign
  1528. return true;
  1529. }
  1530. else
  1531. {
  1532. serverURI = ProfileServerUri;
  1533. // Is local
  1534. return false;
  1535. }
  1536. }
  1537. void cacheForeignImage(UUID agent, UUID imageID)
  1538. {
  1539. if(imageID.IsZero())
  1540. return;
  1541. string assetServerURI = UserManagementModule.GetUserServerURL(agent, "AssetServerURI");
  1542. if(string.IsNullOrWhiteSpace(assetServerURI))
  1543. return;
  1544. Scene.AssetService.Get(imageID.ToString(), assetServerURI, false);
  1545. }
  1546. /// <summary>
  1547. /// Finds the presence.
  1548. /// </summary>
  1549. /// <returns>
  1550. /// The presence.
  1551. /// </returns>
  1552. /// <param name='clientID'>
  1553. /// Client I.
  1554. /// </param>
  1555. ScenePresence FindPresence(UUID clientID)
  1556. {
  1557. ScenePresence p;
  1558. p = Scene.GetScenePresence(clientID);
  1559. if (p != null && !p.IsChildAgent)
  1560. return p;
  1561. return null;
  1562. }
  1563. public virtual bool IsFriendOnline(UUID client, UUID agent)
  1564. {
  1565. // if on same region force online
  1566. ScenePresence p = Scene.GetScenePresence(agent);
  1567. if (p != null && !p.IsDeleted)
  1568. return true;
  1569. IFriendsModule friendsModule = Scene.RequestModuleInterface<IFriendsModule>();
  1570. if (friendsModule != null)
  1571. {
  1572. int friendPerms = friendsModule.GetRightsGrantedByFriend(client, agent);
  1573. if((friendPerms & (int) FriendRights.CanSeeOnline) != 0)
  1574. {
  1575. Services.Interfaces.PresenceInfo[] pi = Scene.PresenceService?.GetAgents(new string[] { agent.ToString() });
  1576. return pi != null && pi.Length > 0;
  1577. }
  1578. }
  1579. return false;
  1580. }
  1581. #endregion Util
  1582. #region Web Util
  1583. /// <summary>
  1584. /// Sends json-rpc request with a serializable type.
  1585. /// </summary>
  1586. /// <returns>
  1587. /// OSD Map.
  1588. /// </returns>
  1589. /// <param name='parameters'>
  1590. /// Serializable type .
  1591. /// </param>
  1592. /// <param name='method'>
  1593. /// Json-rpc method to call.
  1594. /// </param>
  1595. /// <param name='uri'>
  1596. /// URI of json-rpc service.
  1597. /// </param>
  1598. /// <param name='jsonId'>
  1599. /// Id for our call.
  1600. /// </param>
  1601. bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
  1602. {
  1603. if (jsonId == null)
  1604. throw new ArgumentNullException ("jsonId");
  1605. if (uri == null)
  1606. throw new ArgumentNullException ("uri");
  1607. if (method == null)
  1608. throw new ArgumentNullException ("method");
  1609. if (parameters == null)
  1610. throw new ArgumentNullException ("parameters");
  1611. // Prep our payload
  1612. OSDMap json = new OSDMap();
  1613. json.Add("jsonrpc", OSD.FromString("2.0"));
  1614. json.Add("id", OSD.FromString(jsonId));
  1615. json.Add("method", OSD.FromString(method));
  1616. json.Add("params", OSD.SerializeMembers(parameters));
  1617. string jsonRequestData = OSDParser.SerializeJsonString(json);
  1618. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1619. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1620. webRequest.ContentType = "application/json-rpc";
  1621. webRequest.Method = "POST";
  1622. WebResponse webResponse = null;
  1623. try
  1624. {
  1625. using(Stream dataStream = webRequest.GetRequestStream())
  1626. dataStream.Write(content,0,content.Length);
  1627. webResponse = webRequest.GetResponse();
  1628. }
  1629. catch (WebException e)
  1630. {
  1631. Console.WriteLine("Web Error" + e.Message);
  1632. Console.WriteLine ("Please check input");
  1633. return false;
  1634. }
  1635. OSDMap mret = new OSDMap();
  1636. using (Stream rstream = webResponse.GetResponseStream())
  1637. {
  1638. try
  1639. {
  1640. mret = (OSDMap)OSDParser.DeserializeJson(rstream);
  1641. }
  1642. catch (Exception e)
  1643. {
  1644. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1645. if (webResponse != null)
  1646. webResponse.Close();
  1647. return false;
  1648. }
  1649. }
  1650. if (webResponse != null)
  1651. webResponse.Close();
  1652. if (mret.ContainsKey("error"))
  1653. return false;
  1654. // get params...
  1655. OSD.DeserializeMembers(ref parameters, (OSDMap) mret["result"]);
  1656. return true;
  1657. }
  1658. /// <summary>
  1659. /// Sends json-rpc request with OSD parameter.
  1660. /// </summary>
  1661. /// <returns>
  1662. /// The rpc request.
  1663. /// </returns>
  1664. /// <param name='data'>
  1665. /// data - incoming as parameters, outgong as result/error
  1666. /// </param>
  1667. /// <param name='method'>
  1668. /// Json-rpc method to call.
  1669. /// </param>
  1670. /// <param name='uri'>
  1671. /// URI of json-rpc service.
  1672. /// </param>
  1673. /// <param name='jsonId'>
  1674. /// If set to <c>true</c> json identifier.
  1675. /// </param>
  1676. bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  1677. {
  1678. OSDMap map = new OSDMap();
  1679. map["jsonrpc"] = "2.0";
  1680. if(string.IsNullOrEmpty(jsonId))
  1681. map["id"] = UUID.Random().ToString();
  1682. else
  1683. map["id"] = jsonId;
  1684. map["method"] = method;
  1685. map["params"] = data;
  1686. string jsonRequestData = OSDParser.SerializeJsonString(map);
  1687. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1688. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1689. webRequest.ContentType = "application/json-rpc";
  1690. webRequest.Method = "POST";
  1691. WebResponse webResponse = null;
  1692. try
  1693. {
  1694. using(Stream dataStream = webRequest.GetRequestStream())
  1695. dataStream.Write(content,0,content.Length);
  1696. webResponse = webRequest.GetResponse();
  1697. }
  1698. catch (WebException e)
  1699. {
  1700. Console.WriteLine("Web Error" + e.Message);
  1701. Console.WriteLine ("Please check input");
  1702. return false;
  1703. }
  1704. OSDMap response = new OSDMap();
  1705. using (Stream rstream = webResponse.GetResponseStream())
  1706. {
  1707. try
  1708. {
  1709. response = (OSDMap)OSDParser.DeserializeJson(rstream);
  1710. }
  1711. catch (Exception e)
  1712. {
  1713. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1714. if (webResponse != null)
  1715. webResponse.Close();
  1716. return false;
  1717. }
  1718. }
  1719. if (webResponse != null)
  1720. webResponse.Close();
  1721. if(response.ContainsKey("error"))
  1722. {
  1723. data = response["error"];
  1724. return false;
  1725. }
  1726. data = response;
  1727. return true;
  1728. }
  1729. #endregion Web Util
  1730. }
  1731. }