CommunicationsManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 OpenSim 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.Reflection;
  30. using libsecondlife;
  31. using log4net;
  32. using OpenSim.Framework.Communications.Cache;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Servers;
  35. namespace OpenSim.Framework.Communications
  36. {
  37. public class CommunicationsManager
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. protected IUserService m_userService;
  41. protected Dictionary<LLUUID, string[]> m_nameRequestCache = new Dictionary<LLUUID, string[]>();
  42. public IUserService UserService
  43. {
  44. get { return m_userService; }
  45. }
  46. protected IGridServices m_gridService;
  47. public IGridServices GridService
  48. {
  49. get { return m_gridService; }
  50. }
  51. protected IInterRegionCommunications m_interRegion;
  52. public IInterRegionCommunications InterRegion
  53. {
  54. get { return m_interRegion; }
  55. }
  56. protected UserProfileCacheService m_userProfileCacheService;
  57. public UserProfileCacheService UserProfileCacheService
  58. {
  59. get { return m_userProfileCacheService; }
  60. }
  61. // protected AgentAssetTransactionsManager m_transactionsManager;
  62. // public AgentAssetTransactionsManager TransactionsManager
  63. // {
  64. // get { return m_transactionsManager; }
  65. // }
  66. protected IAvatarService m_avatarService;
  67. public IAvatarService AvatarService
  68. {
  69. get { return m_avatarService; }
  70. }
  71. protected AssetCache m_assetCache;
  72. public AssetCache AssetCache
  73. {
  74. get { return m_assetCache; }
  75. }
  76. protected NetworkServersInfo m_networkServersInfo;
  77. public NetworkServersInfo NetworkServersInfo
  78. {
  79. get { return m_networkServersInfo; }
  80. }
  81. public CommunicationsManager(NetworkServersInfo serversInfo, BaseHttpServer httpServer, AssetCache assetCache,
  82. bool dumpAssetsToFile)
  83. {
  84. m_networkServersInfo = serversInfo;
  85. m_assetCache = assetCache;
  86. m_userProfileCacheService = new UserProfileCacheService(this);
  87. // m_transactionsManager = new AgentAssetTransactionsManager(this, dumpAssetsToFile);
  88. }
  89. #region Inventory
  90. protected string m_defaultInventoryHost = "default";
  91. protected List<IInventoryServices> m_inventoryServices = new List<IInventoryServices>();
  92. // protected IInventoryServices m_inventoryService;
  93. public IInventoryServices InventoryService
  94. {
  95. get
  96. {
  97. if (m_inventoryServices.Count > 0)
  98. {
  99. // return m_inventoryServices[0];
  100. IInventoryServices invService;
  101. if (TryGetInventoryService(m_defaultInventoryHost, out invService))
  102. {
  103. return invService;
  104. }
  105. }
  106. return null;
  107. }
  108. }
  109. public bool TryGetInventoryService(string host, out IInventoryServices inventoryService)
  110. {
  111. if ((host == string.Empty) || (host == "default"))
  112. {
  113. host = m_defaultInventoryHost;
  114. }
  115. lock (m_inventoryServices)
  116. {
  117. foreach (IInventoryServices service in m_inventoryServices)
  118. {
  119. if (service.Host == host)
  120. {
  121. inventoryService = service;
  122. return true;
  123. }
  124. }
  125. }
  126. inventoryService = null;
  127. return false;
  128. }
  129. public virtual void AddInventoryService(string hostUrl)
  130. {
  131. }
  132. public virtual void AddInventoryService(IInventoryServices service)
  133. {
  134. lock (m_inventoryServices)
  135. {
  136. m_inventoryServices.Add(service);
  137. }
  138. }
  139. #endregion
  140. public void doCreate(string[] cmmdParams)
  141. {
  142. switch (cmmdParams[0])
  143. {
  144. case "user":
  145. string firstName;
  146. string lastName;
  147. string password;
  148. uint regX = 1000;
  149. uint regY = 1000;
  150. if (cmmdParams.Length < 2)
  151. firstName = MainConsole.Instance.CmdPrompt("First name", "Default");
  152. else firstName = cmmdParams[1];
  153. if ( cmmdParams.Length < 3 )
  154. lastName = MainConsole.Instance.CmdPrompt("Last name", "User");
  155. else lastName = cmmdParams[2];
  156. if ( cmmdParams.Length < 4 )
  157. password = MainConsole.Instance.PasswdPrompt("Password");
  158. else password = cmmdParams[3];
  159. if ( cmmdParams.Length < 5 )
  160. regX = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region X", regX.ToString()));
  161. else regX = Convert.ToUInt32(cmmdParams[4]);
  162. if ( cmmdParams.Length < 6 )
  163. regY = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region Y", regY.ToString()));
  164. else regY = Convert.ToUInt32(cmmdParams[5]);
  165. if (null == m_userService.GetUserProfile(firstName, lastName))
  166. {
  167. AddUser(firstName, lastName, password, regX, regY);
  168. }
  169. else
  170. {
  171. m_log.ErrorFormat("[USERS]: A user with the name {0} {1} already exists!", firstName, lastName);
  172. }
  173. break;
  174. }
  175. }
  176. /// <summary>
  177. /// Persistently adds a user to OpenSim.
  178. /// </summary>
  179. /// <param name="firstName"></param>
  180. /// <param name="lastName"></param>
  181. /// <param name="password"></param>
  182. /// <param name="regX"></param>
  183. /// <param name="regY"></param>
  184. /// <returns>The UUID of the added user. Returns null if the add was unsuccessful</returns>
  185. public LLUUID AddUser(string firstName, string lastName, string password, uint regX, uint regY)
  186. {
  187. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + String.Empty);
  188. m_userService.AddUserProfile(firstName, lastName, md5PasswdHash, regX, regY);
  189. UserProfileData userProf = UserService.GetUserProfile(firstName, lastName);
  190. if (userProf == null)
  191. {
  192. return LLUUID.Zero;
  193. }
  194. else
  195. {
  196. InventoryService.CreateNewUserInventory(userProf.ID);
  197. m_log.Info("[USERS]: Created new inventory set for " + firstName + " " + lastName);
  198. return userProf.ID;
  199. }
  200. }
  201. #region Friend Methods
  202. /// <summary>
  203. /// Adds a new friend to the database for XUser
  204. /// </summary>
  205. /// <param name="friendlistowner">The agent that who's friends list is being added to</param>
  206. /// <param name="friend">The agent that being added to the friends list of the friends list owner</param>
  207. /// <param name="perms">A uint bit vector for set perms that the friend being added has; 0 = none, 1=This friend can see when they sign on, 2 = map, 4 edit objects </param>
  208. public void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms)
  209. {
  210. m_userService.AddNewUserFriend(friendlistowner, friend, perms);
  211. }
  212. /// <summary>
  213. /// Logs off a user and does the appropriate communications
  214. /// </summary>
  215. /// <param name="userid"></param>
  216. /// <param name="regionid"></param>
  217. /// <param name="regionhandle"></param>
  218. /// <param name="posx"></param>
  219. /// <param name="posy"></param>
  220. /// <param name="posz"></param>
  221. public void LogOffUser(LLUUID userid, LLUUID regionid, ulong regionhandle, float posx, float posy, float posz)
  222. {
  223. m_userService.LogOffUser(userid, regionid, regionhandle, posx, posy, posz);
  224. }
  225. /// <summary>
  226. /// Delete friend on friendlistowner's friendlist.
  227. /// </summary>
  228. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  229. /// <param name="friend">The Ex-friend agent</param>
  230. public void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend)
  231. {
  232. m_userService.RemoveUserFriend(friendlistowner, friend);
  233. }
  234. /// <summary>
  235. /// Update permissions for friend on friendlistowner's friendlist.
  236. /// </summary>
  237. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  238. /// <param name="friend">The agent that is getting or loosing permissions</param>
  239. /// <param name="perms">A uint bit vector for set perms that the friend being added has; 0 = none, 1=This friend can see when they sign on, 2 = map, 4 edit objects </param>
  240. public void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms)
  241. {
  242. m_userService.UpdateUserFriendPerms(friendlistowner, friend, perms);
  243. }
  244. /// <summary>
  245. /// Returns a list of FriendsListItems that describe the friends and permissions in the friend relationship for LLUUID friendslistowner
  246. /// </summary>
  247. /// <param name="friendlistowner">The agent that we're retreiving the friends Data.</param>
  248. public List<FriendListItem> GetUserFriendList(LLUUID friendlistowner)
  249. {
  250. return m_userService.GetUserFriendList(friendlistowner);
  251. }
  252. #endregion
  253. #region Packet Handlers
  254. public void UpdateAvatarPropertiesRequest(IClientAPI remote_client, UserProfileData UserProfile)
  255. {
  256. m_userService.UpdateUserProfileProperties(UserProfile);
  257. return;
  258. }
  259. public void HandleUUIDNameRequest(LLUUID uuid, IClientAPI remote_client)
  260. {
  261. if (uuid == m_userProfileCacheService.libraryRoot.Owner)
  262. {
  263. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  264. }
  265. else
  266. {
  267. string[] names = doUUIDNameRequest(uuid);
  268. if (names.Length == 2)
  269. {
  270. remote_client.SendNameReply(uuid, names[0], names[1]);
  271. }
  272. }
  273. }
  274. private string[] doUUIDNameRequest(LLUUID uuid)
  275. {
  276. string[] returnstring = new string[0];
  277. bool doLookup = false;
  278. lock (m_nameRequestCache)
  279. {
  280. if (m_nameRequestCache.ContainsKey(uuid))
  281. {
  282. returnstring = m_nameRequestCache[uuid];
  283. }
  284. else
  285. {
  286. // we don't want to lock the dictionary while we're doing the lookup
  287. doLookup = true;
  288. }
  289. }
  290. if (doLookup) {
  291. UserProfileData profileData = m_userService.GetUserProfile(uuid);
  292. if (profileData != null)
  293. {
  294. returnstring = new string[2];
  295. // LLUUID profileId = profileData.ID;
  296. returnstring[0] = profileData.FirstName;
  297. returnstring[1] = profileData.SurName;
  298. lock (m_nameRequestCache)
  299. {
  300. if (!m_nameRequestCache.ContainsKey(uuid))
  301. m_nameRequestCache.Add(uuid, returnstring);
  302. }
  303. }
  304. }
  305. return returnstring;
  306. }
  307. public bool UUIDNameCachedTest(LLUUID uuid)
  308. {
  309. lock (m_nameRequestCache)
  310. return m_nameRequestCache.ContainsKey(uuid);
  311. }
  312. public string UUIDNameRequestString(LLUUID uuid)
  313. {
  314. string[] names = doUUIDNameRequest(uuid);
  315. if (names.Length == 2)
  316. {
  317. string firstname = names[0];
  318. string lastname = names[1];
  319. return firstname + " " + lastname;
  320. }
  321. return "(hippos)";
  322. }
  323. public List<AvatarPickerAvatar> GenerateAgentPickerRequestResponse(LLUUID queryID, string query)
  324. {
  325. List<AvatarPickerAvatar> pickerlist = m_userService.GenerateAgentPickerRequestResponse(queryID, query);
  326. return pickerlist;
  327. }
  328. #endregion
  329. }
  330. }