UserProfileModule.cs 71 KB

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