UserProfileModule.cs 81 KB

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