CommunicationsManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 IInventoryServices m_inventoryService;
  52. public IInventoryServices InventoryService
  53. {
  54. get { return m_inventoryService; }
  55. }
  56. protected IInterRegionCommunications m_interRegion;
  57. public IInterRegionCommunications InterRegion
  58. {
  59. get { return m_interRegion; }
  60. }
  61. protected UserProfileCacheService m_userProfileCacheService;
  62. public UserProfileCacheService UserProfileCacheService
  63. {
  64. get { return m_userProfileCacheService; }
  65. }
  66. // protected AgentAssetTransactionsManager m_transactionsManager;
  67. // public AgentAssetTransactionsManager TransactionsManager
  68. // {
  69. // get { return m_transactionsManager; }
  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. public void doCreate(string[] cmmdParams)
  90. {
  91. switch (cmmdParams[0])
  92. {
  93. case "user":
  94. string firstName;
  95. string lastName;
  96. string password;
  97. uint regX = 1000;
  98. uint regY = 1000;
  99. if (cmmdParams.Length < 2)
  100. {
  101. firstName = MainConsole.Instance.CmdPrompt("First name", "Default");
  102. lastName = MainConsole.Instance.CmdPrompt("Last name", "User");
  103. password = MainConsole.Instance.PasswdPrompt("Password");
  104. regX = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region X", "1000"));
  105. regY = Convert.ToUInt32(MainConsole.Instance.CmdPrompt("Start Region Y", "1000"));
  106. }
  107. else
  108. {
  109. firstName = cmmdParams[1];
  110. lastName = cmmdParams[2];
  111. password = cmmdParams[3];
  112. regX = Convert.ToUInt32(cmmdParams[4]);
  113. regY = Convert.ToUInt32(cmmdParams[5]);
  114. }
  115. if (null == m_userService.GetUserProfile(firstName, lastName))
  116. {
  117. AddUser(firstName, lastName, password, regX, regY);
  118. }
  119. else
  120. {
  121. m_log.ErrorFormat("[USERS]: A user with the name {0} {1} already exists!", firstName, lastName);
  122. }
  123. break;
  124. }
  125. }
  126. /// <summary>
  127. /// Persistently adds a user to OpenSim.
  128. /// </summary>
  129. /// <param name="firstName"></param>
  130. /// <param name="lastName"></param>
  131. /// <param name="password"></param>
  132. /// <param name="regX"></param>
  133. /// <param name="regY"></param>
  134. /// <returns>The UUID of the added user. Returns null if the add was unsuccessful</returns>
  135. public LLUUID AddUser(string firstName, string lastName, string password, uint regX, uint regY)
  136. {
  137. string md5PasswdHash = Util.Md5Hash(Util.Md5Hash(password) + ":" + String.Empty);
  138. m_userService.AddUserProfile(firstName, lastName, md5PasswdHash, regX, regY);
  139. UserProfileData userProf = UserService.GetUserProfile(firstName, lastName);
  140. if (userProf == null)
  141. {
  142. return LLUUID.Zero;
  143. }
  144. else
  145. {
  146. m_inventoryService.CreateNewUserInventory(userProf.ID);
  147. m_log.Info("[USERS]: Created new inventory set for " + firstName + " " + lastName);
  148. return userProf.ID;
  149. }
  150. }
  151. #region Friend Methods
  152. /// <summary>
  153. /// Adds a new friend to the database for XUser
  154. /// </summary>
  155. /// <param name="friendlistowner">The agent that who's friends list is being added to</param>
  156. /// <param name="friend">The agent that being added to the friends list of the friends list owner</param>
  157. /// <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>
  158. public void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms)
  159. {
  160. m_userService.AddNewUserFriend(friendlistowner, friend, perms);
  161. }
  162. /// <summary>
  163. /// Logs off a user and does the appropriate communications
  164. /// </summary>
  165. /// <param name="userid"></param>
  166. /// <param name="regionid"></param>
  167. /// <param name="regionhandle"></param>
  168. /// <param name="posx"></param>
  169. /// <param name="posy"></param>
  170. /// <param name="posz"></param>
  171. public void LogOffUser(LLUUID userid, LLUUID regionid, ulong regionhandle, float posx, float posy, float posz)
  172. {
  173. m_userService.LogOffUser(userid, regionid, regionhandle, posx, posy, posz);
  174. }
  175. /// <summary>
  176. /// Delete friend on friendlistowner's friendlist.
  177. /// </summary>
  178. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  179. /// <param name="friend">The Ex-friend agent</param>
  180. public void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend)
  181. {
  182. m_userService.RemoveUserFriend(friendlistowner, friend);
  183. }
  184. /// <summary>
  185. /// Update permissions for friend on friendlistowner's friendlist.
  186. /// </summary>
  187. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  188. /// <param name="friend">The agent that is getting or loosing permissions</param>
  189. /// <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>
  190. public void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms)
  191. {
  192. m_userService.UpdateUserFriendPerms(friendlistowner, friend, perms);
  193. }
  194. /// <summary>
  195. /// Returns a list of FriendsListItems that describe the friends and permissions in the friend relationship for LLUUID friendslistowner
  196. /// </summary>
  197. /// <param name="friendlistowner">The agent that we're retreiving the friends Data.</param>
  198. public List<FriendListItem> GetUserFriendList(LLUUID friendlistowner)
  199. {
  200. return m_userService.GetUserFriendList(friendlistowner);
  201. }
  202. #endregion
  203. #region Packet Handlers
  204. public void UpdateAvatarPropertiesRequest(IClientAPI remote_client, UserProfileData UserProfile)
  205. {
  206. m_userService.UpdateUserProfileProperties(UserProfile);
  207. return;
  208. }
  209. public void HandleUUIDNameRequest(LLUUID uuid, IClientAPI remote_client)
  210. {
  211. if (uuid == m_userProfileCacheService.libraryRoot.Owner)
  212. {
  213. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  214. }
  215. else
  216. {
  217. string[] names = doUUIDNameRequest(uuid);
  218. if (names.Length == 2)
  219. {
  220. remote_client.SendNameReply(uuid, names[0], names[1]);
  221. }
  222. }
  223. }
  224. private string[] doUUIDNameRequest(LLUUID uuid)
  225. {
  226. string[] returnstring = new string[0];
  227. bool doLookup = false;
  228. lock (m_nameRequestCache)
  229. {
  230. if (m_nameRequestCache.ContainsKey(uuid))
  231. {
  232. returnstring = m_nameRequestCache[uuid];
  233. }
  234. else
  235. {
  236. // we don't want to lock the dictionary while we're doing the lookup
  237. doLookup = true;
  238. }
  239. }
  240. if (doLookup) {
  241. UserProfileData profileData = m_userService.GetUserProfile(uuid);
  242. if (profileData != null)
  243. {
  244. returnstring = new string[2];
  245. LLUUID profileId = profileData.ID;
  246. returnstring[0] = profileData.FirstName;
  247. returnstring[1] = profileData.SurName;
  248. lock (m_nameRequestCache)
  249. {
  250. if (!m_nameRequestCache.ContainsKey(uuid))
  251. m_nameRequestCache.Add(uuid, returnstring);
  252. }
  253. }
  254. }
  255. return returnstring;
  256. }
  257. public bool UUIDNameCachedTest(LLUUID uuid)
  258. {
  259. lock (m_nameRequestCache)
  260. return m_nameRequestCache.ContainsKey(uuid);
  261. }
  262. public string UUIDNameRequestString(LLUUID uuid)
  263. {
  264. string[] names = doUUIDNameRequest(uuid);
  265. if (names.Length == 2)
  266. {
  267. string firstname = names[0];
  268. string lastname = names[1];
  269. return firstname + " " + lastname;
  270. }
  271. return "(hippos)";
  272. }
  273. public List<AvatarPickerAvatar> GenerateAgentPickerRequestResponse(LLUUID queryID, string query)
  274. {
  275. List<AvatarPickerAvatar> pickerlist = m_userService.GenerateAgentPickerRequestResponse(queryID, query);
  276. return pickerlist;
  277. }
  278. #endregion
  279. }
  280. }