UserProfileModule.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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.CoreModules.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 != null)
  587. {
  588. if (land.LandData.IsGroupOwned)
  589. {
  590. IGroupsModule groupMod = p.Scene.RequestModuleInterface<IGroupsModule>();
  591. UUID groupId = land.LandData.GroupID;
  592. GroupRecord groupRecord = groupMod.GetGroupRecord(groupId);
  593. landOwnerName = groupRecord.GroupName;
  594. }
  595. else
  596. {
  597. IUserAccountService accounts = p.Scene.RequestModuleInterface<IUserAccountService>();
  598. UserAccount user = accounts.GetUserAccount(p.Scene.RegionInfo.ScopeID, land.LandData.OwnerID);
  599. landOwnerName = user.Name;
  600. }
  601. }
  602. else
  603. {
  604. m_log.WarnFormat(
  605. "[PROFILES]: PickInfoUpdate found no parcel info at {0},{1} in {2}",
  606. avaPos.X, avaPos.Y, p.Scene.Name);
  607. }
  608. pick.PickId = pickID;
  609. pick.CreatorId = creatorID;
  610. pick.TopPick = topPick;
  611. pick.Name = name;
  612. pick.Desc = desc;
  613. pick.ParcelId = p.currentParcelUUID;
  614. pick.SnapshotId = snapshotID;
  615. pick.User = landOwnerName;
  616. pick.SimName = remoteClient.Scene.RegionInfo.RegionName;
  617. pick.GlobalPos = posGlobal.ToString();
  618. pick.SortOrder = sortOrder;
  619. pick.Enabled = enabled;
  620. object Pick = (object)pick;
  621. if(!JsonRpcRequest(ref Pick, "picks_update", serverURI, UUID.Random().ToString()))
  622. {
  623. remoteClient.SendAgentAlertMessage(
  624. "Error updating pick", false);
  625. }
  626. m_log.DebugFormat("[PROFILES]: Finish PickInfoUpdate {0} {1}", pick.Name, pick.PickId.ToString());
  627. }
  628. /// <summary>
  629. /// Delete a Pick
  630. /// </summary>
  631. /// <param name='remoteClient'>
  632. /// Remote client.
  633. /// </param>
  634. /// <param name='queryPickID'>
  635. /// Query pick I.
  636. /// </param>
  637. public void PickDelete(IClientAPI remoteClient, UUID queryPickID)
  638. {
  639. string serverURI = string.Empty;
  640. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  641. OSDMap parameters= new OSDMap();
  642. parameters.Add("pickId", OSD.FromUUID(queryPickID));
  643. OSD Params = (OSD)parameters;
  644. if(!JsonRpcRequest(ref Params, "picks_delete", serverURI, UUID.Random().ToString()))
  645. {
  646. remoteClient.SendAgentAlertMessage(
  647. "Error picks delete", false);
  648. }
  649. }
  650. #endregion Picks
  651. #region Notes
  652. /// <summary>
  653. /// Handles the avatar notes request.
  654. /// </summary>
  655. /// <param name='sender'>
  656. /// Sender.
  657. /// </param>
  658. /// <param name='method'>
  659. /// Method.
  660. /// </param>
  661. /// <param name='args'>
  662. /// Arguments.
  663. /// </param>
  664. public void NotesRequest(Object sender, string method, List<String> args)
  665. {
  666. UserProfileNotes note = new UserProfileNotes();
  667. if (!(sender is IClientAPI))
  668. return;
  669. IClientAPI remoteClient = (IClientAPI)sender;
  670. string serverURI = string.Empty;
  671. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  672. note.UserId = remoteClient.AgentId;
  673. UUID.TryParse(args[0], out note.TargetId);
  674. object Note = (object)note;
  675. if(!JsonRpcRequest(ref Note, "avatarnotesrequest", serverURI, UUID.Random().ToString()))
  676. {
  677. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  678. return;
  679. }
  680. note = (UserProfileNotes) Note;
  681. remoteClient.SendAvatarNotesReply(note.TargetId, note.Notes);
  682. }
  683. /// <summary>
  684. /// Avatars the notes update.
  685. /// </summary>
  686. /// <param name='remoteClient'>
  687. /// Remote client.
  688. /// </param>
  689. /// <param name='queryTargetID'>
  690. /// Query target I.
  691. /// </param>
  692. /// <param name='queryNotes'>
  693. /// Query notes.
  694. /// </param>
  695. public void NotesUpdate(IClientAPI remoteClient, UUID queryTargetID, string queryNotes)
  696. {
  697. UserProfileNotes note = new UserProfileNotes();
  698. note.UserId = remoteClient.AgentId;
  699. note.TargetId = queryTargetID;
  700. note.Notes = queryNotes;
  701. string serverURI = string.Empty;
  702. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  703. object Note = note;
  704. if(!JsonRpcRequest(ref Note, "avatar_notes_update", serverURI, UUID.Random().ToString()))
  705. {
  706. return;
  707. }
  708. }
  709. #endregion Notes
  710. #region User Preferences
  711. /// <summary>
  712. /// Updates the user preferences.
  713. /// </summary>
  714. /// <param name='imViaEmail'>
  715. /// Im via email.
  716. /// </param>
  717. /// <param name='visible'>
  718. /// Visible.
  719. /// </param>
  720. /// <param name='remoteClient'>
  721. /// Remote client.
  722. /// </param>
  723. public void UpdateUserPreferences(bool imViaEmail, bool visible, IClientAPI remoteClient)
  724. {
  725. UserPreferences pref = new UserPreferences();
  726. pref.UserId = remoteClient.AgentId;
  727. pref.IMViaEmail = imViaEmail;
  728. pref.Visible = visible;
  729. string serverURI = string.Empty;
  730. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  731. object Pref = pref;
  732. if(!JsonRpcRequest(ref Pref, "user_preferences_update", serverURI, UUID.Random().ToString()))
  733. {
  734. m_log.InfoFormat("[PROFILES]: UserPreferences update error");
  735. remoteClient.SendAgentAlertMessage("Error updating preferences", false);
  736. return;
  737. }
  738. }
  739. /// <summary>
  740. /// Users the preferences request.
  741. /// </summary>
  742. /// <param name='remoteClient'>
  743. /// Remote client.
  744. /// </param>
  745. public void UserPreferencesRequest(IClientAPI remoteClient)
  746. {
  747. UserPreferences pref = new UserPreferences();
  748. pref.UserId = remoteClient.AgentId;
  749. string serverURI = string.Empty;
  750. bool foreign = GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  751. object Pref = (object)pref;
  752. if(!JsonRpcRequest(ref Pref, "user_preferences_request", serverURI, UUID.Random().ToString()))
  753. {
  754. m_log.InfoFormat("[PROFILES]: UserPreferences request error");
  755. remoteClient.SendAgentAlertMessage("Error requesting preferences", false);
  756. return;
  757. }
  758. pref = (UserPreferences) Pref;
  759. remoteClient.SendUserInfoReply(pref.IMViaEmail, pref.Visible, pref.EMail);
  760. }
  761. #endregion User Preferences
  762. #region Avatar Properties
  763. /// <summary>
  764. /// Update the avatars interests .
  765. /// </summary>
  766. /// <param name='remoteClient'>
  767. /// Remote client.
  768. /// </param>
  769. /// <param name='wantmask'>
  770. /// Wantmask.
  771. /// </param>
  772. /// <param name='wanttext'>
  773. /// Wanttext.
  774. /// </param>
  775. /// <param name='skillsmask'>
  776. /// Skillsmask.
  777. /// </param>
  778. /// <param name='skillstext'>
  779. /// Skillstext.
  780. /// </param>
  781. /// <param name='languages'>
  782. /// Languages.
  783. /// </param>
  784. public void AvatarInterestsUpdate(IClientAPI remoteClient, uint wantmask, string wanttext, uint skillsmask, string skillstext, string languages)
  785. {
  786. UserProfileProperties prop = new UserProfileProperties();
  787. prop.UserId = remoteClient.AgentId;
  788. prop.WantToMask = (int)wantmask;
  789. prop.WantToText = wanttext;
  790. prop.SkillsMask = (int)skillsmask;
  791. prop.SkillsText = skillstext;
  792. prop.Language = languages;
  793. string serverURI = string.Empty;
  794. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  795. object Param = prop;
  796. if(!JsonRpcRequest(ref Param, "avatar_interests_update", serverURI, UUID.Random().ToString()))
  797. {
  798. remoteClient.SendAgentAlertMessage(
  799. "Error updating interests", false);
  800. }
  801. }
  802. public void RequestAvatarProperties(IClientAPI remoteClient, UUID avatarID)
  803. {
  804. if ( String.IsNullOrEmpty(avatarID.ToString()) || String.IsNullOrEmpty(remoteClient.AgentId.ToString()))
  805. {
  806. // Looking for a reason that some viewers are sending null Id's
  807. m_log.DebugFormat("[PROFILES]: This should not happen remoteClient.AgentId {0} - avatarID {1}", remoteClient.AgentId, avatarID);
  808. return;
  809. }
  810. // Can't handle NPC yet...
  811. ScenePresence p = FindPresence(avatarID);
  812. if (null != p)
  813. {
  814. if (p.PresenceType == PresenceType.Npc)
  815. return;
  816. }
  817. string serverURI = string.Empty;
  818. bool foreign = GetUserProfileServerURI(avatarID, out serverURI);
  819. UserAccount account = null;
  820. Dictionary<string,object> userInfo;
  821. if (!foreign)
  822. {
  823. account = Scene.UserAccountService.GetUserAccount(Scene.RegionInfo.ScopeID, avatarID);
  824. }
  825. else
  826. {
  827. userInfo = new Dictionary<string, object>();
  828. }
  829. Byte[] charterMember = new Byte[1];
  830. string born = String.Empty;
  831. uint flags = 0x00;
  832. if (null != account)
  833. {
  834. if (account.UserTitle == "")
  835. {
  836. charterMember[0] = (Byte)((account.UserFlags & 0xf00) >> 8);
  837. }
  838. else
  839. {
  840. charterMember = Utils.StringToBytes(account.UserTitle);
  841. }
  842. born = Util.ToDateTime(account.Created).ToString(
  843. "M/d/yyyy", CultureInfo.InvariantCulture);
  844. flags = (uint)(account.UserFlags & 0xff);
  845. }
  846. else
  847. {
  848. if (GetUserAccountData(avatarID, out userInfo) == true)
  849. {
  850. if ((string)userInfo["user_title"] == "")
  851. {
  852. charterMember[0] = (Byte)(((Byte)userInfo["user_flags"] & 0xf00) >> 8);
  853. }
  854. else
  855. {
  856. charterMember = Utils.StringToBytes((string)userInfo["user_title"]);
  857. }
  858. int val_born = (int)userInfo["user_created"];
  859. born = Util.ToDateTime(val_born).ToString(
  860. "M/d/yyyy", CultureInfo.InvariantCulture);
  861. // picky, picky
  862. int val_flags = (int)userInfo["user_flags"];
  863. flags = (uint)(val_flags & 0xff);
  864. }
  865. }
  866. UserProfileProperties props = new UserProfileProperties();
  867. string result = string.Empty;
  868. props.UserId = avatarID;
  869. try
  870. {
  871. GetProfileData(ref props, out result);
  872. }
  873. catch (Exception e)
  874. {
  875. if (foreign)
  876. {
  877. // Check if the foreign grid is using OpenProfile.
  878. // If any error occurs then discard it, and report the original error.
  879. try
  880. {
  881. OpenProfileClient client = new OpenProfileClient(serverURI);
  882. if (!client.RequestAvatarPropertiesUsingOpenProfile(ref props))
  883. throw e;
  884. }
  885. catch (Exception)
  886. {
  887. throw e;
  888. }
  889. }
  890. else
  891. throw;
  892. }
  893. remoteClient.SendAvatarProperties(props.UserId, props.AboutText, born, charterMember , props.FirstLifeText, flags,
  894. props.FirstLifeImageId, props.ImageId, props.WebUrl, props.PartnerId);
  895. remoteClient.SendAvatarInterestsReply(props.UserId, (uint)props.WantToMask, props.WantToText, (uint)props.SkillsMask,
  896. props.SkillsText, props.Language);
  897. }
  898. /// <summary>
  899. /// Updates the avatar properties.
  900. /// </summary>
  901. /// <param name='remoteClient'>
  902. /// Remote client.
  903. /// </param>
  904. /// <param name='newProfile'>
  905. /// New profile.
  906. /// </param>
  907. public void AvatarPropertiesUpdate(IClientAPI remoteClient, UserProfileData newProfile)
  908. {
  909. if (remoteClient.AgentId == newProfile.ID)
  910. {
  911. UserProfileProperties prop = new UserProfileProperties();
  912. prop.UserId = remoteClient.AgentId;
  913. prop.WebUrl = newProfile.ProfileUrl;
  914. prop.ImageId = newProfile.Image;
  915. prop.AboutText = newProfile.AboutText;
  916. prop.FirstLifeImageId = newProfile.FirstLifeImage;
  917. prop.FirstLifeText = newProfile.FirstLifeAboutText;
  918. string serverURI = string.Empty;
  919. GetUserProfileServerURI(remoteClient.AgentId, out serverURI);
  920. object Prop = prop;
  921. if(!JsonRpcRequest(ref Prop, "avatar_properties_update", serverURI, UUID.Random().ToString()))
  922. {
  923. remoteClient.SendAgentAlertMessage(
  924. "Error updating properties", false);
  925. }
  926. RequestAvatarProperties(remoteClient, newProfile.ID);
  927. }
  928. }
  929. /// <summary>
  930. /// Gets the profile data.
  931. /// </summary>
  932. /// <returns>
  933. /// The profile data.
  934. /// </returns>
  935. /// <param name='userID'>
  936. /// User I.
  937. /// </param>
  938. bool GetProfileData(ref UserProfileProperties properties, out string message)
  939. {
  940. // Can't handle NPC yet...
  941. ScenePresence p = FindPresence(properties.UserId);
  942. if (null != p)
  943. {
  944. if (p.PresenceType == PresenceType.Npc)
  945. {
  946. message = "Id points to NPC";
  947. return false;
  948. }
  949. }
  950. string serverURI = string.Empty;
  951. GetUserProfileServerURI(properties.UserId, out serverURI);
  952. // This is checking a friend on the home grid
  953. // Not HG friend
  954. if ( String.IsNullOrEmpty(serverURI))
  955. {
  956. message = "No Presence - foreign friend";
  957. return false;
  958. }
  959. object Prop = (object)properties;
  960. JsonRpcRequest(ref Prop, "avatar_properties_request", serverURI, UUID.Random().ToString());
  961. properties = (UserProfileProperties)Prop;
  962. message = "Success";
  963. return true;
  964. }
  965. #endregion Avatar Properties
  966. #region Utils
  967. bool GetImageAssets(UUID avatarId)
  968. {
  969. string profileServerURI = string.Empty;
  970. string assetServerURI = string.Empty;
  971. bool foreign = GetUserProfileServerURI(avatarId, out profileServerURI);
  972. if(!foreign)
  973. return true;
  974. assetServerURI = UserManagementModule.GetUserServerURL(avatarId, "AssetServerURI");
  975. OSDMap parameters= new OSDMap();
  976. parameters.Add("avatarId", OSD.FromUUID(avatarId));
  977. OSD Params = (OSD)parameters;
  978. if(!JsonRpcRequest(ref Params, "image_assets_request", profileServerURI, UUID.Random().ToString()))
  979. {
  980. return false;
  981. }
  982. parameters = (OSDMap)Params;
  983. OSDArray list = (OSDArray)parameters["result"];
  984. foreach(OSD asset in list)
  985. {
  986. OSDString assetId = (OSDString)asset;
  987. Scene.AssetService.Get(string.Format("{0}/{1}",assetServerURI, assetId.AsString()));
  988. }
  989. return true;
  990. }
  991. /// <summary>
  992. /// Gets the user account data.
  993. /// </summary>
  994. /// <returns>
  995. /// The user profile data.
  996. /// </returns>
  997. /// <param name='userID'>
  998. /// If set to <c>true</c> user I.
  999. /// </param>
  1000. /// <param name='userInfo'>
  1001. /// If set to <c>true</c> user info.
  1002. /// </param>
  1003. bool GetUserAccountData(UUID userID, out Dictionary<string, object> userInfo)
  1004. {
  1005. Dictionary<string,object> info = new Dictionary<string, object>();
  1006. if (UserManagementModule.IsLocalGridUser(userID))
  1007. {
  1008. // Is local
  1009. IUserAccountService uas = Scene.UserAccountService;
  1010. UserAccount account = uas.GetUserAccount(Scene.RegionInfo.ScopeID, userID);
  1011. info["user_flags"] = account.UserFlags;
  1012. info["user_created"] = account.Created;
  1013. if (!String.IsNullOrEmpty(account.UserTitle))
  1014. info["user_title"] = account.UserTitle;
  1015. else
  1016. info["user_title"] = "";
  1017. userInfo = info;
  1018. return false;
  1019. }
  1020. else
  1021. {
  1022. // Is Foreign
  1023. string home_url = UserManagementModule.GetUserServerURL(userID, "HomeURI");
  1024. if (String.IsNullOrEmpty(home_url))
  1025. {
  1026. info["user_flags"] = 0;
  1027. info["user_created"] = 0;
  1028. info["user_title"] = "Unavailable";
  1029. userInfo = info;
  1030. return true;
  1031. }
  1032. UserAgentServiceConnector uConn = new UserAgentServiceConnector(home_url);
  1033. Dictionary<string, object> account;
  1034. try
  1035. {
  1036. account = uConn.GetUserInfo(userID);
  1037. }
  1038. catch (Exception e)
  1039. {
  1040. m_log.Debug("[PROFILES]: GetUserInfo call failed ", e);
  1041. account = new Dictionary<string, object>();
  1042. }
  1043. if (account.Count > 0)
  1044. {
  1045. if (account.ContainsKey("user_flags"))
  1046. info["user_flags"] = account["user_flags"];
  1047. else
  1048. info["user_flags"] = "";
  1049. if (account.ContainsKey("user_created"))
  1050. info["user_created"] = account["user_created"];
  1051. else
  1052. info["user_created"] = "";
  1053. info["user_title"] = "HG Visitor";
  1054. }
  1055. else
  1056. {
  1057. info["user_flags"] = 0;
  1058. info["user_created"] = 0;
  1059. info["user_title"] = "HG Visitor";
  1060. }
  1061. userInfo = info;
  1062. return true;
  1063. }
  1064. }
  1065. /// <summary>
  1066. /// Gets the user profile server UR.
  1067. /// </summary>
  1068. /// <returns>
  1069. /// The user profile server UR.
  1070. /// </returns>
  1071. /// <param name='userID'>
  1072. /// If set to <c>true</c> user I.
  1073. /// </param>
  1074. /// <param name='serverURI'>
  1075. /// If set to <c>true</c> server UR.
  1076. /// </param>
  1077. bool GetUserProfileServerURI(UUID userID, out string serverURI)
  1078. {
  1079. bool local;
  1080. local = UserManagementModule.IsLocalGridUser(userID);
  1081. if (!local)
  1082. {
  1083. serverURI = UserManagementModule.GetUserServerURL(userID, "ProfileServerURI");
  1084. // Is Foreign
  1085. return true;
  1086. }
  1087. else
  1088. {
  1089. serverURI = ProfileServerUri;
  1090. // Is local
  1091. return false;
  1092. }
  1093. }
  1094. /// <summary>
  1095. /// Finds the presence.
  1096. /// </summary>
  1097. /// <returns>
  1098. /// The presence.
  1099. /// </returns>
  1100. /// <param name='clientID'>
  1101. /// Client I.
  1102. /// </param>
  1103. ScenePresence FindPresence(UUID clientID)
  1104. {
  1105. ScenePresence p;
  1106. p = Scene.GetScenePresence(clientID);
  1107. if (p != null && !p.IsChildAgent)
  1108. return p;
  1109. return null;
  1110. }
  1111. #endregion Util
  1112. #region Web Util
  1113. /// <summary>
  1114. /// Sends json-rpc request with a serializable type.
  1115. /// </summary>
  1116. /// <returns>
  1117. /// OSD Map.
  1118. /// </returns>
  1119. /// <param name='parameters'>
  1120. /// Serializable type .
  1121. /// </param>
  1122. /// <param name='method'>
  1123. /// Json-rpc method to call.
  1124. /// </param>
  1125. /// <param name='uri'>
  1126. /// URI of json-rpc service.
  1127. /// </param>
  1128. /// <param name='jsonId'>
  1129. /// Id for our call.
  1130. /// </param>
  1131. bool JsonRpcRequest(ref object parameters, string method, string uri, string jsonId)
  1132. {
  1133. if (jsonId == null)
  1134. throw new ArgumentNullException ("jsonId");
  1135. if (uri == null)
  1136. throw new ArgumentNullException ("uri");
  1137. if (method == null)
  1138. throw new ArgumentNullException ("method");
  1139. if (parameters == null)
  1140. throw new ArgumentNullException ("parameters");
  1141. // Prep our payload
  1142. OSDMap json = new OSDMap();
  1143. json.Add("jsonrpc", OSD.FromString("2.0"));
  1144. json.Add("id", OSD.FromString(jsonId));
  1145. json.Add("method", OSD.FromString(method));
  1146. json.Add("params", OSD.SerializeMembers(parameters));
  1147. string jsonRequestData = OSDParser.SerializeJsonString(json);
  1148. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1149. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1150. webRequest.ContentType = "application/json-rpc";
  1151. webRequest.Method = "POST";
  1152. using (Stream dataStream = webRequest.GetRequestStream())
  1153. dataStream.Write(content, 0, content.Length);
  1154. WebResponse webResponse = null;
  1155. try
  1156. {
  1157. webResponse = webRequest.GetResponse();
  1158. }
  1159. catch (WebException e)
  1160. {
  1161. Console.WriteLine("Web Error" + e.Message);
  1162. Console.WriteLine ("Please check input");
  1163. return false;
  1164. }
  1165. using (webResponse)
  1166. using (Stream rstream = webResponse.GetResponseStream())
  1167. {
  1168. OSDMap mret = (OSDMap)OSDParser.DeserializeJson(rstream);
  1169. if (mret.ContainsKey("error"))
  1170. return false;
  1171. // get params...
  1172. OSD.DeserializeMembers(ref parameters, (OSDMap)mret["result"]);
  1173. return true;
  1174. }
  1175. }
  1176. /// <summary>
  1177. /// Sends json-rpc request with OSD parameter.
  1178. /// </summary>
  1179. /// <returns>
  1180. /// The rpc request.
  1181. /// </returns>
  1182. /// <param name='data'>
  1183. /// data - incoming as parameters, outgong as result/error
  1184. /// </param>
  1185. /// <param name='method'>
  1186. /// Json-rpc method to call.
  1187. /// </param>
  1188. /// <param name='uri'>
  1189. /// URI of json-rpc service.
  1190. /// </param>
  1191. /// <param name='jsonId'>
  1192. /// If set to <c>true</c> json identifier.
  1193. /// </param>
  1194. bool JsonRpcRequest(ref OSD data, string method, string uri, string jsonId)
  1195. {
  1196. OSDMap map = new OSDMap();
  1197. map["jsonrpc"] = "2.0";
  1198. if(string.IsNullOrEmpty(jsonId))
  1199. map["id"] = UUID.Random().ToString();
  1200. else
  1201. map["id"] = jsonId;
  1202. map["method"] = method;
  1203. map["params"] = data;
  1204. string jsonRequestData = OSDParser.SerializeJsonString(map);
  1205. byte[] content = Encoding.UTF8.GetBytes(jsonRequestData);
  1206. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  1207. webRequest.ContentType = "application/json-rpc";
  1208. webRequest.Method = "POST";
  1209. using (Stream dataStream = webRequest.GetRequestStream())
  1210. dataStream.Write(content, 0, content.Length);
  1211. WebResponse webResponse = null;
  1212. try
  1213. {
  1214. webResponse = webRequest.GetResponse();
  1215. }
  1216. catch (WebException e)
  1217. {
  1218. Console.WriteLine("Web Error" + e.Message);
  1219. Console.WriteLine ("Please check input");
  1220. return false;
  1221. }
  1222. using (webResponse)
  1223. using (Stream rstream = webResponse.GetResponseStream())
  1224. {
  1225. OSDMap response = new OSDMap();
  1226. try
  1227. {
  1228. response = (OSDMap)OSDParser.DeserializeJson(rstream);
  1229. }
  1230. catch (Exception e)
  1231. {
  1232. m_log.DebugFormat("[PROFILES]: JsonRpcRequest Error {0} - remote user with legacy profiles?", e.Message);
  1233. return false;
  1234. }
  1235. if (response.ContainsKey("error"))
  1236. {
  1237. data = response["error"];
  1238. return false;
  1239. }
  1240. data = response;
  1241. return true;
  1242. }
  1243. }
  1244. #endregion Web Util
  1245. }
  1246. }