SimianProfiles.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.Collections.Generic;
  29. using System.Collections.Specialized;
  30. using System.Reflection;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Client;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Services.Interfaces;
  41. namespace OpenSim.Services.Connectors.SimianGrid
  42. {
  43. /// <summary>
  44. /// Avatar profile flags
  45. /// </summary>
  46. [Flags]
  47. public enum ProfileFlags : uint
  48. {
  49. AllowPublish = 1,
  50. MaturePublish = 2,
  51. Identified = 4,
  52. Transacted = 8,
  53. Online = 16
  54. }
  55. /// <summary>
  56. /// Connects avatar profile and classified queries to the SimianGrid
  57. /// backend
  58. /// </summary>
  59. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimianProfiles")]
  60. public class SimianProfiles : INonSharedRegionModule
  61. {
  62. private static readonly ILog m_log =
  63. LogManager.GetLogger(
  64. MethodBase.GetCurrentMethod().DeclaringType);
  65. private string m_serverUrl = String.Empty;
  66. private bool m_Enabled = false;
  67. #region INonSharedRegionModule
  68. public Type ReplaceableInterface { get { return null; } }
  69. public void RegionLoaded(Scene scene) { }
  70. public void Close() { }
  71. public SimianProfiles() { }
  72. public string Name { get { return "SimianProfiles"; } }
  73. public void AddRegion(Scene scene)
  74. {
  75. if (m_Enabled)
  76. {
  77. CheckEstateManager(scene);
  78. scene.EventManager.OnClientConnect += ClientConnectHandler;
  79. }
  80. }
  81. public void RemoveRegion(Scene scene)
  82. {
  83. if (m_Enabled)
  84. {
  85. scene.EventManager.OnClientConnect -= ClientConnectHandler;
  86. }
  87. }
  88. #endregion INonSharedRegionModule
  89. public SimianProfiles(IConfigSource source)
  90. {
  91. Initialise(source);
  92. }
  93. public void Initialise(IConfigSource source)
  94. {
  95. IConfig profileConfig = source.Configs["Profiles"];
  96. if (profileConfig == null)
  97. return;
  98. if (profileConfig.GetString("Module", String.Empty) != Name)
  99. return;
  100. m_log.DebugFormat("[SIMIAN PROFILES] module enabled");
  101. m_Enabled = true;
  102. IConfig gridConfig = source.Configs["UserAccountService"];
  103. if (gridConfig != null)
  104. {
  105. string serviceUrl = gridConfig.GetString("UserAccountServerURI");
  106. if (!String.IsNullOrEmpty(serviceUrl))
  107. {
  108. if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
  109. serviceUrl = serviceUrl + '/';
  110. m_serverUrl = serviceUrl;
  111. }
  112. }
  113. if (String.IsNullOrEmpty(m_serverUrl))
  114. m_log.Info("[SIMIAN PROFILES]: No UserAccountServerURI specified, disabling connector");
  115. }
  116. private void ClientConnectHandler(IClientCore clientCore)
  117. {
  118. if (clientCore is IClientAPI)
  119. {
  120. IClientAPI client = (IClientAPI)clientCore;
  121. // Classifieds
  122. client.AddGenericPacketHandler("avatarclassifiedsrequest", AvatarClassifiedsRequestHandler);
  123. client.OnClassifiedInfoRequest += ClassifiedInfoRequestHandler;
  124. client.OnClassifiedInfoUpdate += ClassifiedInfoUpdateHandler;
  125. client.OnClassifiedDelete += ClassifiedDeleteHandler;
  126. // Picks
  127. client.AddGenericPacketHandler("avatarpicksrequest", HandleAvatarPicksRequest);
  128. client.AddGenericPacketHandler("pickinforequest", HandlePickInfoRequest);
  129. client.OnPickInfoUpdate += PickInfoUpdateHandler;
  130. client.OnPickDelete += PickDeleteHandler;
  131. // Notes
  132. client.AddGenericPacketHandler("avatarnotesrequest", HandleAvatarNotesRequest);
  133. client.OnAvatarNotesUpdate += AvatarNotesUpdateHandler;
  134. // Profiles
  135. client.OnRequestAvatarProperties += RequestAvatarPropertiesHandler;
  136. client.OnUpdateAvatarProperties += UpdateAvatarPropertiesHandler;
  137. client.OnAvatarInterestUpdate += AvatarInterestUpdateHandler;
  138. client.OnUserInfoRequest += UserInfoRequestHandler;
  139. client.OnUpdateUserInfo += UpdateUserInfoHandler;
  140. }
  141. }
  142. #region Classifieds
  143. private void AvatarClassifiedsRequestHandler(Object sender, string method, List<String> args)
  144. {
  145. if (!(sender is IClientAPI))
  146. return;
  147. IClientAPI client = (IClientAPI)sender;
  148. UUID targetAvatarID;
  149. if (args.Count < 1 || !UUID.TryParse(args[0], out targetAvatarID))
  150. {
  151. m_log.Error("[SIMIAN PROFILES]: Unrecognized arguments for " + method);
  152. return;
  153. }
  154. // FIXME: Query the generic key/value store for classifieds
  155. client.SendAvatarClassifiedReply(targetAvatarID, new Dictionary<UUID, string>(0));
  156. }
  157. private void ClassifiedInfoRequestHandler(UUID classifiedID, IClientAPI client)
  158. {
  159. // FIXME: Fetch this info
  160. client.SendClassifiedInfoReply(classifiedID, UUID.Zero, 0, Utils.DateTimeToUnixTime(DateTime.UtcNow + TimeSpan.FromDays(1)),
  161. 0, String.Empty, String.Empty, UUID.Zero, 0, UUID.Zero, String.Empty, Vector3.Zero, String.Empty, 0, 0);
  162. }
  163. private void ClassifiedInfoUpdateHandler(UUID classifiedID, uint category, string name, string description,
  164. UUID parcelID, uint parentEstate, UUID snapshotID, Vector3 globalPos, byte classifiedFlags, int price,
  165. IClientAPI client)
  166. {
  167. // FIXME: Save this info
  168. }
  169. private void ClassifiedDeleteHandler(UUID classifiedID, IClientAPI client)
  170. {
  171. // FIXME: Delete the specified classified ad
  172. }
  173. #endregion Classifieds
  174. #region Picks
  175. private void HandleAvatarPicksRequest(Object sender, string method, List<String> args)
  176. {
  177. if (!(sender is IClientAPI))
  178. return;
  179. IClientAPI client = (IClientAPI)sender;
  180. UUID targetAvatarID;
  181. if (args.Count < 1 || !UUID.TryParse(args[0], out targetAvatarID))
  182. {
  183. m_log.Error("[SIMIAN PROFILES]: Unrecognized arguments for " + method);
  184. return;
  185. }
  186. // FIXME: Fetch these
  187. client.SendAvatarPicksReply(targetAvatarID, new Dictionary<UUID, string>(0));
  188. }
  189. private void HandlePickInfoRequest(Object sender, string method, List<String> args)
  190. {
  191. if (!(sender is IClientAPI))
  192. return;
  193. IClientAPI client = (IClientAPI)sender;
  194. UUID avatarID;
  195. UUID pickID;
  196. if (args.Count < 2 || !UUID.TryParse(args[0], out avatarID) || !UUID.TryParse(args[1], out pickID))
  197. {
  198. m_log.Error("[SIMIAN PROFILES]: Unrecognized arguments for " + method);
  199. return;
  200. }
  201. // FIXME: Fetch this
  202. client.SendPickInfoReply(pickID, avatarID, false, UUID.Zero, String.Empty, String.Empty, UUID.Zero, String.Empty,
  203. String.Empty, String.Empty, Vector3.Zero, 0, false);
  204. }
  205. private void PickInfoUpdateHandler(IClientAPI client, UUID pickID, UUID creatorID, bool topPick, string name,
  206. string desc, UUID snapshotID, int sortOrder, bool enabled)
  207. {
  208. // FIXME: Save this
  209. }
  210. private void PickDeleteHandler(IClientAPI client, UUID pickID)
  211. {
  212. // FIXME: Delete
  213. }
  214. #endregion Picks
  215. #region Notes
  216. private void HandleAvatarNotesRequest(Object sender, string method, List<String> args)
  217. {
  218. if (!(sender is IClientAPI))
  219. return;
  220. IClientAPI client = (IClientAPI)sender;
  221. UUID targetAvatarID;
  222. if (args.Count < 1 || !UUID.TryParse(args[0], out targetAvatarID))
  223. {
  224. m_log.Error("[SIMIAN PROFILES]: Unrecognized arguments for " + method);
  225. return;
  226. }
  227. // FIXME: Fetch this
  228. client.SendAvatarNotesReply(targetAvatarID, String.Empty);
  229. }
  230. private void AvatarNotesUpdateHandler(IClientAPI client, UUID targetID, string notes)
  231. {
  232. // FIXME: Save this
  233. }
  234. #endregion Notes
  235. #region Profiles
  236. private void RequestAvatarPropertiesHandler(IClientAPI client, UUID avatarID)
  237. {
  238. m_log.DebugFormat("[SIMIAN PROFILES]: Request avatar properties for {0}",avatarID);
  239. OSDMap user = FetchUserData(avatarID);
  240. ProfileFlags flags = ProfileFlags.AllowPublish | ProfileFlags.MaturePublish;
  241. if (user != null)
  242. {
  243. OSDMap about = null;
  244. if (user.ContainsKey("LLAbout"))
  245. {
  246. try
  247. {
  248. about = OSDParser.DeserializeJson(user["LLAbout"].AsString()) as OSDMap;
  249. }
  250. catch
  251. {
  252. m_log.WarnFormat("[SIMIAN PROFILES]: Unable to decode LLAbout");
  253. }
  254. }
  255. if (about == null)
  256. about = new OSDMap(0);
  257. // Check if this user is a grid operator
  258. byte[] charterMember;
  259. if (user["AccessLevel"].AsInteger() >= 200)
  260. charterMember = Utils.StringToBytes("Operator");
  261. else
  262. charterMember = Utils.EmptyBytes;
  263. // Check if the user is online
  264. if (client.Scene is Scene)
  265. {
  266. OpenSim.Services.Interfaces.PresenceInfo[] presences = ((Scene)client.Scene).PresenceService.GetAgents(new string[] { avatarID.ToString() });
  267. if (presences != null && presences.Length > 0)
  268. flags |= ProfileFlags.Online;
  269. }
  270. // Check if the user is identified
  271. if (user["Identified"].AsBoolean())
  272. flags |= ProfileFlags.Identified;
  273. client.SendAvatarProperties(avatarID, about["About"].AsString(), user["CreationDate"].AsDate().ToString("M/d/yyyy",
  274. System.Globalization.CultureInfo.InvariantCulture), charterMember, about["FLAbout"].AsString(), (uint)flags,
  275. about["FLImage"].AsUUID(), about["Image"].AsUUID(), about["URL"].AsString(), user["Partner"].AsUUID());
  276. OSDMap interests = null;
  277. if (user.ContainsKey("LLInterests"))
  278. {
  279. try
  280. {
  281. interests = OSDParser.DeserializeJson(user["LLInterests"].AsString()) as OSDMap;
  282. client.SendAvatarInterestsReply(avatarID, interests["WantMask"].AsUInteger(), interests["WantText"].AsString(), interests["SkillsMask"].AsUInteger(), interests["SkillsText"].AsString(), interests["Languages"].AsString());
  283. }
  284. catch { }
  285. }
  286. if (about == null)
  287. about = new OSDMap(0);
  288. }
  289. else
  290. {
  291. m_log.Warn("[SIMIAN PROFILES]: Failed to fetch profile information for " + client.Name + ", returning default values");
  292. client.SendAvatarProperties(avatarID, String.Empty, "1/1/1970", Utils.EmptyBytes,
  293. String.Empty, (uint)flags, UUID.Zero, UUID.Zero, String.Empty, UUID.Zero);
  294. }
  295. }
  296. private void UpdateAvatarPropertiesHandler(IClientAPI client, UserProfileData profileData)
  297. {
  298. OSDMap map = new OSDMap
  299. {
  300. { "About", OSD.FromString(profileData.AboutText) },
  301. { "Image", OSD.FromUUID(profileData.Image) },
  302. { "FLAbout", OSD.FromString(profileData.FirstLifeAboutText) },
  303. { "FLImage", OSD.FromUUID(profileData.FirstLifeImage) },
  304. { "URL", OSD.FromString(profileData.ProfileUrl) }
  305. };
  306. AddUserData(client.AgentId, "LLAbout", map);
  307. }
  308. private void AvatarInterestUpdateHandler(IClientAPI client, uint wantmask, string wanttext, uint skillsmask,
  309. string skillstext, string languages)
  310. {
  311. OSDMap map = new OSDMap
  312. {
  313. { "WantMask", OSD.FromInteger(wantmask) },
  314. { "WantText", OSD.FromString(wanttext) },
  315. { "SkillsMask", OSD.FromInteger(skillsmask) },
  316. { "SkillsText", OSD.FromString(skillstext) },
  317. { "Languages", OSD.FromString(languages) }
  318. };
  319. AddUserData(client.AgentId, "LLInterests", map);
  320. }
  321. private void UserInfoRequestHandler(IClientAPI client)
  322. {
  323. m_log.Error("[SIMIAN PROFILES]: UserInfoRequestHandler");
  324. // Fetch this user's e-mail address
  325. NameValueCollection requestArgs = new NameValueCollection
  326. {
  327. { "RequestMethod", "GetUser" },
  328. { "UserID", client.AgentId.ToString() }
  329. };
  330. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  331. string email = response["Email"].AsString();
  332. if (!response["Success"].AsBoolean())
  333. m_log.Warn("[SIMIAN PROFILES]: GetUser failed during a user info request for " + client.Name);
  334. client.SendUserInfoReply(false, true, email);
  335. }
  336. private void UpdateUserInfoHandler(bool imViaEmail, bool visible, IClientAPI client)
  337. {
  338. m_log.Info("[SIMIAN PROFILES]: Ignoring user info update from " + client.Name);
  339. }
  340. #endregion Profiles
  341. /// <summary>
  342. /// Sanity checks regions for a valid estate owner at startup
  343. /// </summary>
  344. private void CheckEstateManager(Scene scene)
  345. {
  346. EstateSettings estate = scene.RegionInfo.EstateSettings;
  347. if (estate.EstateOwner == UUID.Zero)
  348. {
  349. // Attempt to lookup the grid admin
  350. UserAccount admin = scene.UserAccountService.GetUserAccount(scene.RegionInfo.ScopeID, UUID.Zero);
  351. if (admin != null)
  352. {
  353. m_log.InfoFormat("[SIMIAN PROFILES]: Setting estate {0} (ID: {1}) owner to {2}", estate.EstateName,
  354. estate.EstateID, admin.Name);
  355. estate.EstateOwner = admin.PrincipalID;
  356. scene.EstateDataService.StoreEstateSettings(estate);
  357. }
  358. else
  359. {
  360. m_log.WarnFormat("[SIMIAN PROFILES]: Estate {0} (ID: {1}) does not have an owner", estate.EstateName, estate.EstateID);
  361. }
  362. }
  363. }
  364. private bool AddUserData(UUID userID, string key, OSDMap value)
  365. {
  366. NameValueCollection requestArgs = new NameValueCollection
  367. {
  368. { "RequestMethod", "AddUserData" },
  369. { "UserID", userID.ToString() },
  370. { key, OSDParser.SerializeJsonString(value) }
  371. };
  372. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  373. bool success = response["Success"].AsBoolean();
  374. if (!success)
  375. m_log.WarnFormat("[SIMIAN PROFILES]: Failed to add user data with key {0} for {1}: {2}", key, userID, response["Message"].AsString());
  376. return success;
  377. }
  378. private OSDMap FetchUserData(UUID userID)
  379. {
  380. m_log.DebugFormat("[SIMIAN PROFILES]: Fetch information about {0}",userID);
  381. NameValueCollection requestArgs = new NameValueCollection
  382. {
  383. { "RequestMethod", "GetUser" },
  384. { "UserID", userID.ToString() }
  385. };
  386. OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
  387. if (response["Success"].AsBoolean() && response["User"] is OSDMap)
  388. {
  389. return (OSDMap)response["User"];
  390. }
  391. else
  392. {
  393. m_log.Error("[SIMIAN PROFILES]: Failed to fetch user data for " + userID + ": " + response["Message"].AsString());
  394. }
  395. return null;
  396. }
  397. }
  398. }