UserProfileModule.cs 73 KB

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