UserProfileModule.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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.RegionLocX*Constants.RegionSize + avaPos.X,
  582. remoteClient.Scene.RegionInfo.RegionLocY*Constants.RegionSize + 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.TargetId = remoteClient.AgentId;
  664. UUID.TryParse(args[0], out note.UserId);
  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. Stream dataStream = webRequest.GetRequestStream();
  1112. dataStream.Write(content, 0, content.Length);
  1113. dataStream.Close();
  1114. WebResponse webResponse = null;
  1115. try
  1116. {
  1117. webResponse = webRequest.GetResponse();
  1118. }
  1119. catch (WebException e)
  1120. {
  1121. Console.WriteLine("Web Error" + e.Message);
  1122. Console.WriteLine ("Please check input");
  1123. return false;
  1124. }
  1125. Stream rstream = webResponse.GetResponseStream();
  1126. OSDMap mret = new OSDMap();
  1127. try
  1128. {
  1129. mret = (OSDMap)OSDParser.DeserializeJson(rstream);
  1130. }
  1131. catch (Exception e)
  1132. {
  1133. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1134. return false;
  1135. }
  1136. if (mret.ContainsKey("error"))
  1137. return false;
  1138. // get params...
  1139. OSD.DeserializeMembers(ref parameters, (OSDMap) mret["result"]);
  1140. return true;
  1141. }
  1142. /// <summary>
  1143. /// Sends json-rpc request with OSD parameter.
  1144. /// </summary>
  1145. /// <returns>
  1146. /// The rpc request.
  1147. /// </returns>
  1148. /// <param name='data'>
  1149. /// data - incoming as parameters, outgong as result/error
  1150. /// </param>
  1151. /// <param name='method'>
  1152. /// Json-rpc method to call.
  1153. /// </param>
  1154. /// <param name='uri'>
  1155. /// URI of json-rpc service.
  1156. /// </param>
  1157. /// <param name='jsonId'>
  1158. /// If set to <c>true</c> json identifier.
  1159. /// </param>
  1160. bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  1161. {
  1162. OSDMap map = new OSDMap();
  1163. map["jsonrpc"] = "2.0";
  1164. if(string.IsNullOrEmpty(jsonId))
  1165. map["id"] = UUID.Random().ToString();
  1166. else
  1167. map["id"] = jsonId;
  1168. map["method"] = method;
  1169. map["params"] = data;
  1170. string jsonRequestData = OSDParser.SerializeJsonString(map);
  1171. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1172. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1173. webRequest.ContentType = "application/json-rpc";
  1174. webRequest.Method = "POST";
  1175. Stream dataStream = webRequest.GetRequestStream();
  1176. dataStream.Write(content, 0, content.Length);
  1177. dataStream.Close();
  1178. WebResponse webResponse = null;
  1179. try
  1180. {
  1181. webResponse = webRequest.GetResponse();
  1182. }
  1183. catch (WebException e)
  1184. {
  1185. Console.WriteLine("Web Error" + e.Message);
  1186. Console.WriteLine ("Please check input");
  1187. return false;
  1188. }
  1189. Stream rstream = webResponse.GetResponseStream();
  1190. OSDMap response = new OSDMap();
  1191. try
  1192. {
  1193. response = (OSDMap)OSDParser.DeserializeJson(rstream);
  1194. }
  1195. catch (Exception e)
  1196. {
  1197. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1198. return false;
  1199. }
  1200. if(response.ContainsKey("error"))
  1201. {
  1202. data = response["error"];
  1203. return false;
  1204. }
  1205. data = response;
  1206. return true;
  1207. }
  1208. #endregion Web Util
  1209. }
  1210. }