UserProfileModule.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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.Net;
  34. using System.Net.Sockets;
  35. using System.Reflection;
  36. using System.Xml;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. using log4net;
  40. using Nini.Config;
  41. using Nwc.XmlRpc;
  42. using OpenSim.Framework;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Region.Framework.Scenes;
  45. using OpenSim.Services.Interfaces;
  46. using Mono.Addins;
  47. using OpenSim.Services.Connectors.Hypergrid;
  48. namespace OpenSim.Region.OptionalModules.Avatar.UserProfiles
  49. {
  50. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserProfilesModule")]
  51. public class UserProfileModule : IProfileModule, INonSharedRegionModule
  52. {
  53. /// <summary>
  54. /// Logging
  55. /// </summary>
  56. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  57. // The pair of Dictionaries are used to handle the switching of classified ads
  58. // by maintaining a cache of classified id to creator id mappings and an interest
  59. // count. The entries are removed when the interest count reaches 0.
  60. Dictionary<UUID, UUID> m_classifiedCache = new Dictionary<UUID, UUID>();
  61. Dictionary<UUID, int> m_classifiedInterest = new Dictionary<UUID, int>();
  62. public Scene Scene
  63. {
  64. get; private set;
  65. }
  66. /// <summary>
  67. /// Gets or sets the ConfigSource.
  68. /// </summary>
  69. /// <value>
  70. /// The configuration
  71. /// </value>
  72. public IConfigSource Config {
  73. get;
  74. set;
  75. }
  76. /// <summary>
  77. /// Gets or sets the URI to the profile server.
  78. /// </summary>
  79. /// <value>
  80. /// The profile server URI.
  81. /// </value>
  82. public string ProfileServerUri {
  83. get;
  84. set;
  85. }
  86. IProfileModule ProfileModule
  87. {
  88. get; set;
  89. }
  90. IUserManagement UserManagementModule
  91. {
  92. get; set;
  93. }
  94. /// <summary>
  95. /// Gets or sets a value indicating whether this
  96. /// <see cref="OpenSim.Region.Coremodules.UserProfiles.UserProfileModule"/> is enabled.
  97. /// </summary>
  98. /// <value>
  99. /// <c>true</c> if enabled; otherwise, <c>false</c>.
  100. /// </value>
  101. public bool Enabled {
  102. get;
  103. set;
  104. }
  105. #region IRegionModuleBase implementation
  106. /// <summary>
  107. /// This is called to initialize the region module. For shared modules, this is called exactly once, after
  108. /// creating the single (shared) instance. For non-shared modules, this is called once on each instance, after
  109. /// the instace for the region has been created.
  110. /// </summary>
  111. /// <param name='source'>
  112. /// Source.
  113. /// </param>
  114. public void Initialise(IConfigSource source)
  115. {
  116. Config = source;
  117. ReplaceableInterface = typeof(IProfileModule);
  118. IConfig profileConfig = Config.Configs["UserProfiles"];
  119. if (profileConfig == null)
  120. {
  121. m_log.Debug("[PROFILES]: UserProfiles disabled, no configuration");
  122. Enabled = false;
  123. return;
  124. }
  125. // If we find ProfileURL then we configure for FULL support
  126. // else we setup for BASIC support
  127. ProfileServerUri = profileConfig.GetString("ProfileServiceURL", "");
  128. if (ProfileServerUri == "")
  129. {
  130. Enabled = false;
  131. return;
  132. }
  133. m_log.Debug("[PROFILES]: Full Profiles Enabled");
  134. ReplaceableInterface = null;
  135. Enabled = true;
  136. }
  137. /// <summary>
  138. /// Adds the region.
  139. /// </summary>
  140. /// <param name='scene'>
  141. /// Scene.
  142. /// </param>
  143. public void AddRegion(Scene scene)
  144. {
  145. if(!Enabled)
  146. return;
  147. Scene = scene;
  148. Scene.RegisterModuleInterface<IProfileModule>(this);
  149. Scene.EventManager.OnNewClient += OnNewClient;
  150. Scene.EventManager.OnMakeRootAgent += HandleOnMakeRootAgent;
  151. UserManagementModule = Scene.RequestModuleInterface<IUserManagement>();
  152. }
  153. void HandleOnMakeRootAgent (ScenePresence obj)
  154. {
  155. if(obj.PresenceType == PresenceType.Npc)
  156. return;
  157. Util.FireAndForget(delegate
  158. {
  159. GetImageAssets(((IScenePresence)obj).UUID);
  160. });
  161. }
  162. /// <summary>
  163. /// Removes the region.
  164. /// </summary>
  165. /// <param name='scene'>
  166. /// Scene.
  167. /// </param>
  168. public void RemoveRegion(Scene scene)
  169. {
  170. if(!Enabled)
  171. return;
  172. }
  173. /// <summary>
  174. /// This will be called once for every scene loaded. In a shared module this will be multiple times in one
  175. /// instance, while a nonshared module instance will only be called once. This method is called after AddRegion
  176. /// has been called in all modules for that scene, providing an opportunity to request another module's
  177. /// interface, or hook an event from another module.
  178. /// </summary>
  179. /// <param name='scene'>
  180. /// Scene.
  181. /// </param>
  182. public void RegionLoaded(Scene scene)
  183. {
  184. if(!Enabled)
  185. return;
  186. }
  187. /// <summary>
  188. /// If this returns non-null, it is the type of an interface that this module intends to register. This will
  189. /// cause the loader to defer loading of this module until all other modules have been loaded. If no other
  190. /// module has registered the interface by then, this module will be activated, else it will remain inactive,
  191. /// letting the other module take over. This should return non-null ONLY in modules that are intended to be
  192. /// easily replaceable, e.g. stub implementations that the developer expects to be replaced by third party
  193. /// provided modules.
  194. /// </summary>
  195. /// <value>
  196. /// The replaceable interface.
  197. /// </value>
  198. public Type ReplaceableInterface
  199. {
  200. get; private set;
  201. }
  202. /// <summary>
  203. /// Called as the instance is closed.
  204. /// </summary>
  205. public void Close()
  206. {
  207. }
  208. /// <value>
  209. /// The name of the module
  210. /// </value>
  211. /// <summary>
  212. /// Gets the module name.
  213. /// </summary>
  214. public string Name
  215. {
  216. get { return "UserProfileModule"; }
  217. }
  218. #endregion IRegionModuleBase implementation
  219. #region Region Event Handlers
  220. /// <summary>
  221. /// Raises the new client event.
  222. /// </summary>
  223. /// <param name='client'>
  224. /// Client.
  225. /// </param>
  226. void OnNewClient(IClientAPI client)
  227. {
  228. //Profile
  229. client.OnRequestAvatarProperties += RequestAvatarProperties;
  230. client.OnUpdateAvatarProperties += AvatarPropertiesUpdate;
  231. client.OnAvatarInterestUpdate += AvatarInterestsUpdate;
  232. // Classifieds
  233. client.AddGenericPacketHandler("avatarclassifiedsrequest", ClassifiedsRequest);
  234. client.OnClassifiedInfoUpdate += ClassifiedInfoUpdate;
  235. client.OnClassifiedInfoRequest += ClassifiedInfoRequest;
  236. client.OnClassifiedDelete += ClassifiedDelete;
  237. // Picks
  238. client.AddGenericPacketHandler("avatarpicksrequest", PicksRequest);
  239. client.AddGenericPacketHandler("pickinforequest", PickInfoRequest);
  240. client.OnPickInfoUpdate += PickInfoUpdate;
  241. client.OnPickDelete += PickDelete;
  242. // Notes
  243. client.AddGenericPacketHandler("avatarnotesrequest", NotesRequest);
  244. client.OnAvatarNotesUpdate += NotesUpdate;
  245. // Preferences
  246. client.OnUserInfoRequest += UserPreferencesRequest;
  247. client.OnUpdateUserInfo += UpdateUserPreferences;
  248. }
  249. #endregion Region Event Handlers
  250. #region Classified
  251. ///
  252. /// <summary>
  253. /// Handles the avatar classifieds request.
  254. /// </summary>
  255. /// <param name='sender'>
  256. /// Sender.
  257. /// </param>
  258. /// <param name='method'>
  259. /// Method.
  260. /// </param>
  261. /// <param name='args'>
  262. /// Arguments.
  263. /// </param>
  264. public void ClassifiedsRequest(Object sender, string method, List<String> args)
  265. {
  266. if (!(sender is IClientAPI))
  267. return;
  268. IClientAPI remoteClient = (IClientAPI)sender;
  269. UUID targetID;
  270. UUID.TryParse(args[0], out targetID);
  271. // Can't handle NPC yet...
  272. ScenePresence p = FindPresence(targetID);
  273. if (null != p)
  274. {
  275. if (p.PresenceType == PresenceType.Npc)
  276. return;
  277. }
  278. string serverURI = string.Empty;
  279. GetUserProfileServerURI(targetID, out serverURI);
  280. UUID creatorId = UUID.Zero;
  281. Dictionary<UUID, string> classifieds = new Dictionary<UUID, string>();
  282. OSDMap parameters= new OSDMap();
  283. UUID.TryParse(args[0], out creatorId);
  284. parameters.Add("creatorId", OSD.FromUUID(creatorId));
  285. OSD Params = (OSD)parameters;
  286. if(!JsonRpcRequest(ref Params, "avatarclassifiedsrequest", serverURI, UUID.Random().ToString()))
  287. {
  288. remoteClient.SendAvatarClassifiedReply(new UUID(args[0]), classifieds);
  289. return;
  290. }
  291. parameters = (OSDMap)Params;
  292. OSDArray list = (OSDArray)parameters["result"];
  293. foreach(OSD map in list)
  294. {
  295. OSDMap m = (OSDMap)map;
  296. UUID cid = m["classifieduuid"].AsUUID();
  297. string name = m["name"].AsString();
  298. classifieds[cid] = name;
  299. lock (m_classifiedCache)
  300. {
  301. if (!m_classifiedCache.ContainsKey(cid))
  302. {
  303. m_classifiedCache.Add(cid,creatorId);
  304. m_classifiedInterest.Add(cid, 0);
  305. }
  306. m_classifiedInterest[cid]++;
  307. }
  308. }
  309. remoteClient.SendAvatarClassifiedReply(new UUID(args[0]), classifieds);
  310. }
  311. public void ClassifiedInfoRequest(UUID queryClassifiedID, IClientAPI remoteClient)
  312. {
  313. UUID target = remoteClient.AgentId;
  314. UserClassifiedAdd ad = new UserClassifiedAdd();
  315. ad.ClassifiedId = queryClassifiedID;
  316. lock (m_classifiedCache)
  317. {
  318. if (m_classifiedCache.ContainsKey(queryClassifiedID))
  319. {
  320. target = m_classifiedCache[queryClassifiedID];
  321. m_classifiedInterest[queryClassifiedID] --;
  322. if (m_classifiedInterest[queryClassifiedID] == 0)
  323. {
  324. m_classifiedInterest.Remove(queryClassifiedID);
  325. m_classifiedCache.Remove(queryClassifiedID);
  326. }
  327. }
  328. }
  329. string serverURI = string.Empty;
  330. GetUserProfileServerURI(target, out serverURI);
  331. object Ad = (object)ad;
  332. if(!JsonRpcRequest(ref Ad, "classifieds_info_query", serverURI, UUID.Random().ToString()))
  333. {
  334. remoteClient.SendAgentAlertMessage(
  335. "Error getting classified info", false);
  336. return;
  337. }
  338. ad = (UserClassifiedAdd) Ad;
  339. if(ad.CreatorId == UUID.Zero)
  340. return;
  341. Vector3 globalPos = new Vector3();
  342. Vector3.TryParse(ad.GlobalPos, out globalPos);
  343. remoteClient.SendClassifiedInfoReply(ad.ClassifiedId, ad.CreatorId, (uint)ad.CreationDate, (uint)ad.ExpirationDate,
  344. (uint)ad.Category, ad.Name, ad.Description, ad.ParcelId, (uint)ad.ParentEstate,
  345. ad.SnapshotId, ad.SimName, globalPos, ad.ParcelName, ad.Flags, ad.Price);
  346. }
  347. /// <summary>
  348. /// Classifieds info update.
  349. /// </summary>
  350. /// <param name='queryclassifiedID'>
  351. /// Queryclassified I.
  352. /// </param>
  353. /// <param name='queryCategory'>
  354. /// Query category.
  355. /// </param>
  356. /// <param name='queryName'>
  357. /// Query name.
  358. /// </param>
  359. /// <param name='queryDescription'>
  360. /// Query description.
  361. /// </param>
  362. /// <param name='queryParcelID'>
  363. /// Query parcel I.
  364. /// </param>
  365. /// <param name='queryParentEstate'>
  366. /// Query parent estate.
  367. /// </param>
  368. /// <param name='querySnapshotID'>
  369. /// Query snapshot I.
  370. /// </param>
  371. /// <param name='queryGlobalPos'>
  372. /// Query global position.
  373. /// </param>
  374. /// <param name='queryclassifiedFlags'>
  375. /// Queryclassified flags.
  376. /// </param>
  377. /// <param name='queryclassifiedPrice'>
  378. /// Queryclassified price.
  379. /// </param>
  380. /// <param name='remoteClient'>
  381. /// Remote client.
  382. /// </param>
  383. public void ClassifiedInfoUpdate(UUID queryclassifiedID, uint queryCategory, string queryName, string queryDescription, UUID queryParcelID,
  384. uint queryParentEstate, UUID querySnapshotID, Vector3 queryGlobalPos, byte queryclassifiedFlags,
  385. int queryclassifiedPrice, IClientAPI remoteClient)
  386. {
  387. UserClassifiedAdd ad = new UserClassifiedAdd();
  388. Scene s = (Scene) remoteClient.Scene;
  389. Vector3 pos = remoteClient.SceneAgent.AbsolutePosition;
  390. ILandObject land = s.LandChannel.GetLandObject(pos.X, pos.Y);
  391. ScenePresence p = FindPresence(remoteClient.AgentId);
  392. string serverURI = string.Empty;
  393. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  394. if (land == null)
  395. {
  396. ad.ParcelName = string.Empty;
  397. }
  398. else
  399. {
  400. ad.ParcelName = land.LandData.Name;
  401. }
  402. ad.CreatorId = remoteClient.AgentId;
  403. ad.ClassifiedId = queryclassifiedID;
  404. ad.Category = Convert.ToInt32(queryCategory);
  405. ad.Name = queryName;
  406. ad.Description = queryDescription;
  407. ad.ParentEstate = Convert.ToInt32(queryParentEstate);
  408. ad.SnapshotId = querySnapshotID;
  409. ad.SimName = remoteClient.Scene.RegionInfo.RegionName;
  410. ad.GlobalPos = queryGlobalPos.ToString ();
  411. ad.Flags = queryclassifiedFlags;
  412. ad.Price = queryclassifiedPrice;
  413. ad.ParcelId = p.currentParcelUUID;
  414. object Ad = ad;
  415. OSD.SerializeMembers(Ad);
  416. if(!JsonRpcRequest(ref Ad, "classified_update", serverURI, UUID.Random().ToString()))
  417. {
  418. remoteClient.SendAgentAlertMessage(
  419. "Error updating classified", false);
  420. }
  421. }
  422. /// <summary>
  423. /// Classifieds delete.
  424. /// </summary>
  425. /// <param name='queryClassifiedID'>
  426. /// Query classified I.
  427. /// </param>
  428. /// <param name='remoteClient'>
  429. /// Remote client.
  430. /// </param>
  431. public void ClassifiedDelete(UUID queryClassifiedID, IClientAPI remoteClient)
  432. {
  433. string serverURI = string.Empty;
  434. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  435. UUID classifiedId;
  436. OSDMap parameters= new OSDMap();
  437. UUID.TryParse(queryClassifiedID.ToString(), out classifiedId);
  438. parameters.Add("classifiedId", OSD.FromUUID(classifiedId));
  439. OSD Params = (OSD)parameters;
  440. if(!JsonRpcRequest(ref Params, "classified_delete", serverURI, UUID.Random().ToString()))
  441. {
  442. remoteClient.SendAgentAlertMessage(
  443. "Error classified delete", false);
  444. }
  445. parameters = (OSDMap)Params;
  446. }
  447. #endregion Classified
  448. #region Picks
  449. /// <summary>
  450. /// Handles the avatar picks request.
  451. /// </summary>
  452. /// <param name='sender'>
  453. /// Sender.
  454. /// </param>
  455. /// <param name='method'>
  456. /// Method.
  457. /// </param>
  458. /// <param name='args'>
  459. /// Arguments.
  460. /// </param>
  461. public void PicksRequest(Object sender, string method, List<String> args)
  462. {
  463. if (!(sender is IClientAPI))
  464. return;
  465. IClientAPI remoteClient = (IClientAPI)sender;
  466. UUID targetId;
  467. UUID.TryParse(args[0], out targetId);
  468. // Can't handle NPC yet...
  469. ScenePresence p = FindPresence(targetId);
  470. if (null != p)
  471. {
  472. if (p.PresenceType == PresenceType.Npc)
  473. return;
  474. }
  475. string serverURI = string.Empty;
  476. GetUserProfileServerURI(targetId, out serverURI);
  477. Dictionary<UUID, string> picks = new Dictionary<UUID, string>();
  478. OSDMap parameters= new OSDMap();
  479. parameters.Add("creatorId", OSD.FromUUID(targetId));
  480. OSD Params = (OSD)parameters;
  481. if(!JsonRpcRequest(ref Params, "avatarpicksrequest", serverURI, UUID.Random().ToString()))
  482. {
  483. remoteClient.SendAvatarPicksReply(new UUID(args[0]), picks);
  484. return;
  485. }
  486. parameters = (OSDMap)Params;
  487. OSDArray list = (OSDArray)parameters["result"];
  488. foreach(OSD map in list)
  489. {
  490. OSDMap m = (OSDMap)map;
  491. UUID cid = m["pickuuid"].AsUUID();
  492. string name = m["name"].AsString();
  493. m_log.DebugFormat("[PROFILES]: PicksRequest {0}", name);
  494. picks[cid] = name;
  495. }
  496. remoteClient.SendAvatarPicksReply(new UUID(args[0]), picks);
  497. }
  498. /// <summary>
  499. /// Handles the pick info request.
  500. /// </summary>
  501. /// <param name='sender'>
  502. /// Sender.
  503. /// </param>
  504. /// <param name='method'>
  505. /// Method.
  506. /// </param>
  507. /// <param name='args'>
  508. /// Arguments.
  509. /// </param>
  510. public void PickInfoRequest(Object sender, string method, List<String> args)
  511. {
  512. if (!(sender is IClientAPI))
  513. return;
  514. UUID targetID;
  515. UUID.TryParse(args[0], out targetID);
  516. string serverURI = string.Empty;
  517. GetUserProfileServerURI(targetID, out serverURI);
  518. IClientAPI remoteClient = (IClientAPI)sender;
  519. UserProfilePick pick = new UserProfilePick();
  520. UUID.TryParse(args[0], out pick.CreatorId);
  521. UUID.TryParse(args[1], out pick.PickId);
  522. object Pick = (object)pick;
  523. if(!JsonRpcRequest(ref Pick, "pickinforequest", serverURI, UUID.Random().ToString()))
  524. {
  525. remoteClient.SendAgentAlertMessage(
  526. "Error selecting pick", false);
  527. }
  528. pick = (UserProfilePick) Pick;
  529. if(pick.SnapshotId == UUID.Zero)
  530. {
  531. // In case of a new UserPick, the data may not be ready and we would send wrong data, skip it...
  532. m_log.DebugFormat("[PROFILES]: PickInfoRequest: SnapshotID is {0}", UUID.Zero.ToString());
  533. return;
  534. }
  535. Vector3 globalPos;
  536. Vector3.TryParse(pick.GlobalPos,out globalPos);
  537. m_log.DebugFormat("[PROFILES]: PickInfoRequest: {0} : {1}", pick.Name.ToString(), pick.SnapshotId.ToString());
  538. remoteClient.SendPickInfoReply(pick.PickId,pick.CreatorId,pick.TopPick,pick.ParcelId,pick.Name,
  539. pick.Desc,pick.SnapshotId,pick.User,pick.OriginalName,pick.SimName,
  540. globalPos,pick.SortOrder,pick.Enabled);
  541. }
  542. /// <summary>
  543. /// Updates the userpicks
  544. /// </summary>
  545. /// <param name='remoteClient'>
  546. /// Remote client.
  547. /// </param>
  548. /// <param name='pickID'>
  549. /// Pick I.
  550. /// </param>
  551. /// <param name='creatorID'>
  552. /// the creator of the pick
  553. /// </param>
  554. /// <param name='topPick'>
  555. /// Top pick.
  556. /// </param>
  557. /// <param name='name'>
  558. /// Name.
  559. /// </param>
  560. /// <param name='desc'>
  561. /// Desc.
  562. /// </param>
  563. /// <param name='snapshotID'>
  564. /// Snapshot I.
  565. /// </param>
  566. /// <param name='sortOrder'>
  567. /// Sort order.
  568. /// </param>
  569. /// <param name='enabled'>
  570. /// Enabled.
  571. /// </param>
  572. public void PickInfoUpdate(IClientAPI remoteClient, UUID pickID, UUID creatorID, bool topPick, string name, string desc, UUID snapshotID, int sortOrder, bool enabled)
  573. {
  574. m_log.DebugFormat("[PROFILES]: Start PickInfoUpdate Name: {0} PickId: {1} SnapshotId: {2}", name, pickID.ToString(), snapshotID.ToString());
  575. UserProfilePick pick = new UserProfilePick();
  576. string serverURI = string.Empty;
  577. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  578. ScenePresence p = FindPresence(remoteClient.AgentId);
  579. Vector3 avaPos = p.AbsolutePosition;
  580. // Getting the global position for the Avatar
  581. Vector3 posGlobal = new Vector3(remoteClient.Scene.RegionInfo.WorldLocX + avaPos.X,
  582. remoteClient.Scene.RegionInfo.WorldLocY + avaPos.Y,
  583. avaPos.Z);
  584. string landOwnerName = string.Empty;
  585. ILandObject land = p.Scene.LandChannel.GetLandObject(avaPos.X, avaPos.Y);
  586. if(land.LandData.IsGroupOwned)
  587. {
  588. IGroupsModule groupMod = p.Scene.RequestModuleInterface<IGroupsModule>();
  589. UUID groupId = land.LandData.GroupID;
  590. GroupRecord groupRecord = groupMod.GetGroupRecord(groupId);
  591. landOwnerName = groupRecord.GroupName;
  592. }
  593. else
  594. {
  595. IUserAccountService accounts = p.Scene.RequestModuleInterface<IUserAccountService>();
  596. UserAccount user = accounts.GetUserAccount(p.Scene.RegionInfo.ScopeID, land.LandData.OwnerID);
  597. landOwnerName = user.Name;
  598. }
  599. pick.PickId = pickID;
  600. pick.CreatorId = creatorID;
  601. pick.TopPick = topPick;
  602. pick.Name = name;
  603. pick.Desc = desc;
  604. pick.ParcelId = p.currentParcelUUID;
  605. pick.SnapshotId = snapshotID;
  606. pick.User = landOwnerName;
  607. pick.SimName = remoteClient.Scene.RegionInfo.RegionName;
  608. pick.GlobalPos = posGlobal.ToString();
  609. pick.SortOrder = sortOrder;
  610. pick.Enabled = enabled;
  611. object Pick = (object)pick;
  612. if(!JsonRpcRequest(ref Pick, "picks_update", serverURI, UUID.Random().ToString()))
  613. {
  614. remoteClient.SendAgentAlertMessage(
  615. "Error updating pick", false);
  616. }
  617. m_log.DebugFormat("[PROFILES]: Finish PickInfoUpdate {0} {1}", pick.Name, pick.PickId.ToString());
  618. }
  619. /// <summary>
  620. /// Delete a Pick
  621. /// </summary>
  622. /// <param name='remoteClient'>
  623. /// Remote client.
  624. /// </param>
  625. /// <param name='queryPickID'>
  626. /// Query pick I.
  627. /// </param>
  628. public void PickDelete(IClientAPI remoteClient, UUID queryPickID)
  629. {
  630. string serverURI = string.Empty;
  631. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  632. OSDMap parameters= new OSDMap();
  633. parameters.Add("pickId", OSD.FromUUID(queryPickID));
  634. OSD Params = (OSD)parameters;
  635. if(!JsonRpcRequest(ref Params, "picks_delete", serverURI, UUID.Random().ToString()))
  636. {
  637. remoteClient.SendAgentAlertMessage(
  638. "Error picks delete", false);
  639. }
  640. }
  641. #endregion Picks
  642. #region Notes
  643. /// <summary>
  644. /// Handles the avatar notes request.
  645. /// </summary>
  646. /// <param name='sender'>
  647. /// Sender.
  648. /// </param>
  649. /// <param name='method'>
  650. /// Method.
  651. /// </param>
  652. /// <param name='args'>
  653. /// Arguments.
  654. /// </param>
  655. public void NotesRequest(Object sender, string method, List<String> args)
  656. {
  657. UserProfileNotes note = new UserProfileNotes();
  658. if (!(sender is IClientAPI))
  659. return;
  660. IClientAPI remoteClient = (IClientAPI)sender;
  661. string serverURI = string.Empty;
  662. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  663. note.UserId = remoteClient.AgentId;
  664. UUID.TryParse(args[0], out note.TargetId);
  665. object Note = (object)note;
  666. if(!JsonRpcRequest(ref Note, "avatarnotesrequest", serverURI, UUID.Random().ToString()))
  667. {
  668. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  669. return;
  670. }
  671. note = (UserProfileNotes) Note;
  672. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  673. }
  674. /// <summary>
  675. /// Avatars the notes update.
  676. /// </summary>
  677. /// <param name='remoteClient'>
  678. /// Remote client.
  679. /// </param>
  680. /// <param name='queryTargetID'>
  681. /// Query target I.
  682. /// </param>
  683. /// <param name='queryNotes'>
  684. /// Query notes.
  685. /// </param>
  686. public void NotesUpdate(IClientAPI remoteClient, UUID queryTargetID, string queryNotes)
  687. {
  688. UserProfileNotes note = new UserProfileNotes();
  689. note.UserId = remoteClient.AgentId;
  690. note.TargetId = queryTargetID;
  691. note.Notes = queryNotes;
  692. string serverURI = string.Empty;
  693. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  694. object Note = note;
  695. if(!JsonRpcRequest(ref Note, "avatar_notes_update", serverURI, UUID.Random().ToString()))
  696. {
  697. return;
  698. }
  699. }
  700. #endregion Notes
  701. #region User Preferences
  702. /// <summary>
  703. /// Updates the user preferences.
  704. /// </summary>
  705. /// <param name='imViaEmail'>
  706. /// Im via email.
  707. /// </param>
  708. /// <param name='visible'>
  709. /// Visible.
  710. /// </param>
  711. /// <param name='remoteClient'>
  712. /// Remote client.
  713. /// </param>
  714. public void UpdateUserPreferences(bool imViaEmail, bool visible, IClientAPI remoteClient)
  715. {
  716. UserPreferences pref = new UserPreferences();
  717. pref.UserId = remoteClient.AgentId;
  718. pref.IMViaEmail = imViaEmail;
  719. pref.Visible = visible;
  720. string serverURI = string.Empty;
  721. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  722. object Pref = pref;
  723. if(!JsonRpcRequest(ref Pref, "user_preferences_update", serverURI, UUID.Random().ToString()))
  724. {
  725. m_log.InfoFormat("[PROFILES]: UserPreferences update error");
  726. remoteClient.SendAgentAlertMessage("Error updating preferences", false);
  727. return;
  728. }
  729. }
  730. /// <summary>
  731. /// Users the preferences request.
  732. /// </summary>
  733. /// <param name='remoteClient'>
  734. /// Remote client.
  735. /// </param>
  736. public void UserPreferencesRequest(IClientAPI remoteClient)
  737. {
  738. UserPreferences pref = new UserPreferences();
  739. pref.UserId = remoteClient.AgentId;
  740. string serverURI = string.Empty;
  741. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  742. object Pref = (object)pref;
  743. if(!JsonRpcRequest(ref Pref, "user_preferences_request", serverURI, UUID.Random().ToString()))
  744. {
  745. m_log.InfoFormat("[PROFILES]: UserPreferences request error");
  746. remoteClient.SendAgentAlertMessage("Error requesting preferences", false);
  747. return;
  748. }
  749. pref = (UserPreferences) Pref;
  750. remoteClient.SendUserInfoReply(pref.IMViaEmail, pref.Visible, pref.EMail);
  751. }
  752. #endregion User Preferences
  753. #region Avatar Properties
  754. /// <summary>
  755. /// Update the avatars interests .
  756. /// </summary>
  757. /// <param name='remoteClient'>
  758. /// Remote client.
  759. /// </param>
  760. /// <param name='wantmask'>
  761. /// Wantmask.
  762. /// </param>
  763. /// <param name='wanttext'>
  764. /// Wanttext.
  765. /// </param>
  766. /// <param name='skillsmask'>
  767. /// Skillsmask.
  768. /// </param>
  769. /// <param name='skillstext'>
  770. /// Skillstext.
  771. /// </param>
  772. /// <param name='languages'>
  773. /// Languages.
  774. /// </param>
  775. public void AvatarInterestsUpdate(IClientAPI remoteClient, uint wantmask, string wanttext, uint skillsmask, string skillstext, string languages)
  776. {
  777. UserProfileProperties prop = new UserProfileProperties();
  778. prop.UserId = remoteClient.AgentId;
  779. prop.WantToMask = (int)wantmask;
  780. prop.WantToText = wanttext;
  781. prop.SkillsMask = (int)skillsmask;
  782. prop.SkillsText = skillstext;
  783. prop.Language = languages;
  784. string serverURI = string.Empty;
  785. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  786. object Param = prop;
  787. if(!JsonRpcRequest(ref Param, "avatar_interests_update", serverURI, UUID.Random().ToString()))
  788. {
  789. remoteClient.SendAgentAlertMessage(
  790. "Error updating interests", false);
  791. }
  792. }
  793. public void RequestAvatarProperties(IClientAPI remoteClient, UUID avatarID)
  794. {
  795. if ( String.IsNullOrEmpty(avatarID.ToString()) || String.IsNullOrEmpty(remoteClient.AgentId.ToString()))
  796. {
  797. // Looking for a reason that some viewers are sending null Id's
  798. m_log.DebugFormat("[PROFILES]: This should not happen remoteClient.AgentId {0} - avatarID {1}", remoteClient.AgentId, avatarID);
  799. return;
  800. }
  801. // Can't handle NPC yet...
  802. ScenePresence p = FindPresence(avatarID);
  803. if (null != p)
  804. {
  805. if (p.PresenceType == PresenceType.Npc)
  806. return;
  807. }
  808. string serverURI = string.Empty;
  809. bool foreign = GetUserProfileServerURI(avatarID, out serverURI);
  810. UserAccount account = null;
  811. Dictionary<string,object> userInfo;
  812. if (!foreign)
  813. {
  814. account = Scene.UserAccountService.GetUserAccount(Scene.RegionInfo.ScopeID, avatarID);
  815. }
  816. else
  817. {
  818. userInfo = new Dictionary<string, object>();
  819. }
  820. Byte[] charterMember = new Byte[1];
  821. string born = String.Empty;
  822. uint flags = 0x00;
  823. if (null != account)
  824. {
  825. if (account.UserTitle == "")
  826. {
  827. charterMember[0] = (Byte)((account.UserFlags & 0xf00) >> 8);
  828. }
  829. else
  830. {
  831. charterMember = Utils.StringToBytes(account.UserTitle);
  832. }
  833. born = Util.ToDateTime(account.Created).ToString(
  834. "M/d/yyyy", CultureInfo.InvariantCulture);
  835. flags = (uint)(account.UserFlags & 0xff);
  836. }
  837. else
  838. {
  839. if (GetUserAccountData(avatarID, out userInfo) == true)
  840. {
  841. if ((string)userInfo["user_title"] == "")
  842. {
  843. charterMember[0] = (Byte)(((Byte)userInfo["user_flags"] & 0xf00) >> 8);
  844. }
  845. else
  846. {
  847. charterMember = Utils.StringToBytes((string)userInfo["user_title"]);
  848. }
  849. int val_born = (int)userInfo["user_created"];
  850. born = Util.ToDateTime(val_born).ToString(
  851. "M/d/yyyy", CultureInfo.InvariantCulture);
  852. // picky, picky
  853. int val_flags = (int)userInfo["user_flags"];
  854. flags = (uint)(val_flags & 0xff);
  855. }
  856. }
  857. UserProfileProperties props = new UserProfileProperties();
  858. string result = string.Empty;
  859. props.UserId = avatarID;
  860. GetProfileData(ref props, out result);
  861. remoteClient.SendAvatarProperties(props.UserId, props.AboutText, born, charterMember , props.FirstLifeText, flags,
  862. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  863. remoteClient.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask, props.WantToText, (uint)props.SkillsMask,
  864. props.SkillsText, props.Language);
  865. }
  866. /// <summary>
  867. /// Updates the avatar properties.
  868. /// </summary>
  869. /// <param name='remoteClient'>
  870. /// Remote client.
  871. /// </param>
  872. /// <param name='newProfile'>
  873. /// New profile.
  874. /// </param>
  875. public void AvatarPropertiesUpdate(IClientAPI remoteClient, UserProfileData newProfile)
  876. {
  877. if (remoteClient.AgentId == newProfile.ID)
  878. {
  879. UserProfileProperties prop = new UserProfileProperties();
  880. prop.UserId = remoteClient.AgentId;
  881. prop.WebUrl = newProfile.ProfileUrl;
  882. prop.ImageId = newProfile.Image;
  883. prop.AboutText = newProfile.AboutText;
  884. prop.FirstLifeImageId = newProfile.FirstLifeImage;
  885. prop.FirstLifeText = newProfile.FirstLifeAboutText;
  886. string serverURI = string.Empty;
  887. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  888. object Prop = prop;
  889. if(!JsonRpcRequest(ref Prop, "avatar_properties_update", serverURI, UUID.Random().ToString()))
  890. {
  891. remoteClient.SendAgentAlertMessage(
  892. "Error updating properties", false);
  893. }
  894. RequestAvatarProperties(remoteClient, newProfile.ID);
  895. }
  896. }
  897. /// <summary>
  898. /// Gets the profile data.
  899. /// </summary>
  900. /// <returns>
  901. /// The profile data.
  902. /// </returns>
  903. /// <param name='userID'>
  904. /// User I.
  905. /// </param>
  906. bool GetProfileData(ref UserProfileProperties properties, out string message)
  907. {
  908. // Can't handle NPC yet...
  909. ScenePresence p = FindPresence(properties.UserId);
  910. if (null != p)
  911. {
  912. if (p.PresenceType == PresenceType.Npc)
  913. {
  914. message = "Id points to NPC";
  915. return false;
  916. }
  917. }
  918. string serverURI = string.Empty;
  919. GetUserProfileServerURI(properties.UserId, out serverURI);
  920. // This is checking a friend on the home grid
  921. // Not HG friend
  922. if ( String.IsNullOrEmpty(serverURI))
  923. {
  924. message = "No Presence - foreign friend";
  925. return false;
  926. }
  927. object Prop = (object)properties;
  928. JsonRpcRequest(ref Prop, "avatar_properties_request", serverURI, UUID.Random().ToString());
  929. properties = (UserProfileProperties)Prop;
  930. message = "Success";
  931. return true;
  932. }
  933. #endregion Avatar Properties
  934. #region Utils
  935. bool GetImageAssets(UUID avatarId)
  936. {
  937. string profileServerURI = string.Empty;
  938. string assetServerURI = string.Empty;
  939. bool foreign = GetUserProfileServerURI(avatarId, out profileServerURI);
  940. if(!foreign)
  941. return true;
  942. assetServerURI = UserManagementModule.GetUserServerURL(avatarId, "AssetServerURI");
  943. OSDMap parameters= new OSDMap();
  944. parameters.Add("avatarId", OSD.FromUUID(avatarId));
  945. OSD Params = (OSD)parameters;
  946. if(!JsonRpcRequest(ref Params, "image_assets_request", profileServerURI, UUID.Random().ToString()))
  947. {
  948. return false;
  949. }
  950. parameters = (OSDMap)Params;
  951. OSDArray list = (OSDArray)parameters["result"];
  952. foreach(OSD asset in list)
  953. {
  954. OSDString assetId = (OSDString)asset;
  955. Scene.AssetService.Get(string.Format("{0}/{1}",assetServerURI, assetId.AsString()));
  956. }
  957. return true;
  958. }
  959. /// <summary>
  960. /// Gets the user account data.
  961. /// </summary>
  962. /// <returns>
  963. /// The user profile data.
  964. /// </returns>
  965. /// <param name='userID'>
  966. /// If set to <c>true</c> user I.
  967. /// </param>
  968. /// <param name='userInfo'>
  969. /// If set to <c>true</c> user info.
  970. /// </param>
  971. bool GetUserAccountData(UUID userID, out Dictionary<string, object> userInfo)
  972. {
  973. Dictionary<string,object> info = new Dictionary<string, object>();
  974. if (UserManagementModule.IsLocalGridUser(userID))
  975. {
  976. // Is local
  977. IUserAccountService uas = Scene.UserAccountService;
  978. UserAccount account = uas.GetUserAccount(Scene.RegionInfo.ScopeID, userID);
  979. info["user_flags"] = account.UserFlags;
  980. info["user_created"] = account.Created;
  981. if (!String.IsNullOrEmpty(account.UserTitle))
  982. info["user_title"] = account.UserTitle;
  983. else
  984. info["user_title"] = "";
  985. userInfo = info;
  986. return false;
  987. }
  988. else
  989. {
  990. // Is Foreign
  991. string home_url = UserManagementModule.GetUserServerURL(userID, "HomeURI");
  992. if (String.IsNullOrEmpty(home_url))
  993. {
  994. info["user_flags"] = 0;
  995. info["user_created"] = 0;
  996. info["user_title"] = "Unavailable";
  997. userInfo = info;
  998. return true;
  999. }
  1000. UserAgentServiceConnector uConn = new UserAgentServiceConnector(home_url);
  1001. Dictionary<string, object> account = uConn.GetUserInfo(userID);
  1002. if (account.Count > 0)
  1003. {
  1004. if (account.ContainsKey("user_flags"))
  1005. info["user_flags"] = account["user_flags"];
  1006. else
  1007. info["user_flags"] = "";
  1008. if (account.ContainsKey("user_created"))
  1009. info["user_created"] = account["user_created"];
  1010. else
  1011. info["user_created"] = "";
  1012. info["user_title"] = "HG Visitor";
  1013. }
  1014. else
  1015. {
  1016. info["user_flags"] = 0;
  1017. info["user_created"] = 0;
  1018. info["user_title"] = "HG Visitor";
  1019. }
  1020. userInfo = info;
  1021. return true;
  1022. }
  1023. }
  1024. /// <summary>
  1025. /// Gets the user profile server UR.
  1026. /// </summary>
  1027. /// <returns>
  1028. /// The user profile server UR.
  1029. /// </returns>
  1030. /// <param name='userID'>
  1031. /// If set to <c>true</c> user I.
  1032. /// </param>
  1033. /// <param name='serverURI'>
  1034. /// If set to <c>true</c> server UR.
  1035. /// </param>
  1036. bool GetUserProfileServerURI(UUID userID, out string serverURI)
  1037. {
  1038. bool local;
  1039. local = UserManagementModule.IsLocalGridUser(userID);
  1040. if (!local)
  1041. {
  1042. serverURI = UserManagementModule.GetUserServerURL(userID, "ProfileServerURI");
  1043. // Is Foreign
  1044. return true;
  1045. }
  1046. else
  1047. {
  1048. serverURI = ProfileServerUri;
  1049. // Is local
  1050. return false;
  1051. }
  1052. }
  1053. /// <summary>
  1054. /// Finds the presence.
  1055. /// </summary>
  1056. /// <returns>
  1057. /// The presence.
  1058. /// </returns>
  1059. /// <param name='clientID'>
  1060. /// Client I.
  1061. /// </param>
  1062. ScenePresence FindPresence(UUID clientID)
  1063. {
  1064. ScenePresence p;
  1065. p = Scene.GetScenePresence(clientID);
  1066. if (p != null && !p.IsChildAgent)
  1067. return p;
  1068. return null;
  1069. }
  1070. #endregion Util
  1071. #region Web Util
  1072. /// <summary>
  1073. /// Sends json-rpc request with a serializable type.
  1074. /// </summary>
  1075. /// <returns>
  1076. /// OSD Map.
  1077. /// </returns>
  1078. /// <param name='parameters'>
  1079. /// Serializable type .
  1080. /// </param>
  1081. /// <param name='method'>
  1082. /// Json-rpc method to call.
  1083. /// </param>
  1084. /// <param name='uri'>
  1085. /// URI of json-rpc service.
  1086. /// </param>
  1087. /// <param name='jsonId'>
  1088. /// Id for our call.
  1089. /// </param>
  1090. bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
  1091. {
  1092. if (jsonId == null)
  1093. throw new ArgumentNullException ("jsonId");
  1094. if (uri == null)
  1095. throw new ArgumentNullException ("uri");
  1096. if (method == null)
  1097. throw new ArgumentNullException ("method");
  1098. if (parameters == null)
  1099. throw new ArgumentNullException ("parameters");
  1100. // Prep our payload
  1101. OSDMap json = new OSDMap();
  1102. json.Add("jsonrpc", OSD.FromString("2.0"));
  1103. json.Add("id", OSD.FromString(jsonId));
  1104. json.Add("method", OSD.FromString(method));
  1105. json.Add("params", OSD.SerializeMembers(parameters));
  1106. string jsonRequestData = OSDParser.SerializeJsonString(json);
  1107. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1108. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1109. webRequest.ContentType = "application/json-rpc";
  1110. webRequest.Method = "POST";
  1111. using (Stream dataStream = webRequest.GetRequestStream())
  1112. dataStream.Write(content, 0, content.Length);
  1113. WebResponse webResponse = null;
  1114. try
  1115. {
  1116. webResponse = webRequest.GetResponse();
  1117. }
  1118. catch (WebException e)
  1119. {
  1120. Console.WriteLine("Web Error" + e.Message);
  1121. Console.WriteLine ("Please check input");
  1122. return false;
  1123. }
  1124. using (webResponse)
  1125. using (Stream rstream = webResponse.GetResponseStream())
  1126. {
  1127. OSDMap mret = (OSDMap)OSDParser.DeserializeJson(rstream);
  1128. if (mret.ContainsKey("error"))
  1129. return false;
  1130. // get params...
  1131. OSD.DeserializeMembers(ref parameters, (OSDMap)mret["result"]);
  1132. return true;
  1133. }
  1134. }
  1135. /// <summary>
  1136. /// Sends json-rpc request with OSD parameter.
  1137. /// </summary>
  1138. /// <returns>
  1139. /// The rpc request.
  1140. /// </returns>
  1141. /// <param name='data'>
  1142. /// data - incoming as parameters, outgong as result/error
  1143. /// </param>
  1144. /// <param name='method'>
  1145. /// Json-rpc method to call.
  1146. /// </param>
  1147. /// <param name='uri'>
  1148. /// URI of json-rpc service.
  1149. /// </param>
  1150. /// <param name='jsonId'>
  1151. /// If set to <c>true</c> json identifier.
  1152. /// </param>
  1153. bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  1154. {
  1155. OSDMap map = new OSDMap();
  1156. map["jsonrpc"] = "2.0";
  1157. if(string.IsNullOrEmpty(jsonId))
  1158. map["id"] = UUID.Random().ToString();
  1159. else
  1160. map["id"] = jsonId;
  1161. map["method"] = method;
  1162. map["params"] = data;
  1163. string jsonRequestData = OSDParser.SerializeJsonString(map);
  1164. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1165. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1166. webRequest.ContentType = "application/json-rpc";
  1167. webRequest.Method = "POST";
  1168. using (Stream dataStream = webRequest.GetRequestStream())
  1169. dataStream.Write(content, 0, content.Length);
  1170. WebResponse webResponse = null;
  1171. try
  1172. {
  1173. webResponse = webRequest.GetResponse();
  1174. }
  1175. catch (WebException e)
  1176. {
  1177. Console.WriteLine("Web Error" + e.Message);
  1178. Console.WriteLine ("Please check input");
  1179. return false;
  1180. }
  1181. using (webResponse)
  1182. using (Stream rstream = webResponse.GetResponseStream())
  1183. {
  1184. OSDMap response = new OSDMap();
  1185. try
  1186. {
  1187. response = (OSDMap)OSDParser.DeserializeJson(rstream);
  1188. }
  1189. catch (Exception e)
  1190. {
  1191. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1192. return false;
  1193. }
  1194. if (response.ContainsKey("error"))
  1195. {
  1196. data = response["error"];
  1197. return false;
  1198. }
  1199. data = response;
  1200. return true;
  1201. }
  1202. }
  1203. #endregion Web Util
  1204. }
  1205. }