UserProfileModule.cs 78 KB

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