UserManagementModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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.IO;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Region.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Services.Connectors.Hypergrid;
  38. using OpenMetaverse;
  39. using log4net;
  40. using Nini.Config;
  41. namespace OpenSim.Region.CoreModules.Framework.UserManagement
  42. {
  43. struct UserData
  44. {
  45. public UUID Id;
  46. public string FirstName;
  47. public string LastName;
  48. public string HomeURL;
  49. public Dictionary<string, object> ServerURLs;
  50. }
  51. public class UserManagementModule : ISharedRegionModule, IUserManagement
  52. {
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. private List<Scene> m_Scenes = new List<Scene>();
  55. // The cache
  56. Dictionary<UUID, UserData> m_UserCache = new Dictionary<UUID, UserData>();
  57. #region ISharedRegionModule
  58. public void Initialise(IConfigSource config)
  59. {
  60. //m_Enabled = config.Configs["Modules"].GetBoolean("LibraryModule", m_Enabled);
  61. //if (m_Enabled)
  62. //{
  63. // IConfig libConfig = config.Configs["LibraryService"];
  64. // if (libConfig != null)
  65. // {
  66. // string dllName = libConfig.GetString("LocalServiceModule", string.Empty);
  67. // m_log.Debug("[LIBRARY MODULE]: Library service dll is " + dllName);
  68. // if (dllName != string.Empty)
  69. // {
  70. // Object[] args = new Object[] { config };
  71. // m_Library = ServerUtils.LoadPlugin<ILibraryService>(dllName, args);
  72. // }
  73. // }
  74. //}
  75. MainConsole.Instance.Commands.AddCommand("grid", true,
  76. "show names",
  77. "show names",
  78. "Show the bindings between user UUIDs and user names",
  79. String.Empty,
  80. HandleShowUsers);
  81. }
  82. public bool IsSharedModule
  83. {
  84. get { return true; }
  85. }
  86. public string Name
  87. {
  88. get { return "UserManagement Module"; }
  89. }
  90. public Type ReplaceableInterface
  91. {
  92. get { return null; }
  93. }
  94. public void AddRegion(Scene scene)
  95. {
  96. m_Scenes.Add(scene);
  97. scene.RegisterModuleInterface<IUserManagement>(this);
  98. scene.EventManager.OnNewClient += new EventManager.OnNewClientDelegate(EventManager_OnNewClient);
  99. scene.EventManager.OnPrimsLoaded += new EventManager.PrimsLoaded(EventManager_OnPrimsLoaded);
  100. }
  101. public void RemoveRegion(Scene scene)
  102. {
  103. scene.UnregisterModuleInterface<IUserManagement>(this);
  104. m_Scenes.Remove(scene);
  105. }
  106. public void RegionLoaded(Scene s)
  107. {
  108. }
  109. public void PostInitialise()
  110. {
  111. }
  112. public void Close()
  113. {
  114. m_Scenes.Clear();
  115. m_UserCache.Clear();
  116. }
  117. #endregion ISharedRegionModule
  118. #region Event Handlers
  119. void EventManager_OnPrimsLoaded(Scene s)
  120. {
  121. // let's sniff all the user names referenced by objects in the scene
  122. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Caching creators' data from {0} ({1} objects)...", s.RegionInfo.RegionName, s.GetEntities().Length);
  123. s.ForEachSOG(delegate(SceneObjectGroup sog) { CacheCreators(sog); });
  124. }
  125. void EventManager_OnNewClient(IClientAPI client)
  126. {
  127. client.OnNameFromUUIDRequest += new UUIDNameRequest(HandleUUIDNameRequest);
  128. }
  129. void HandleUUIDNameRequest(UUID uuid, IClientAPI remote_client)
  130. {
  131. if (m_Scenes[0].LibraryService != null && (m_Scenes[0].LibraryService.LibraryRootFolder.Owner == uuid))
  132. {
  133. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  134. }
  135. else
  136. {
  137. string[] names = GetUserNames(uuid);
  138. if (names.Length == 2)
  139. {
  140. //m_log.DebugFormat("[XXX] HandleUUIDNameRequest {0} is {1} {2}", uuid, names[0], names[1]);
  141. remote_client.SendNameReply(uuid, names[0], names[1]);
  142. }
  143. }
  144. }
  145. #endregion Event Handlers
  146. private void CacheCreators(SceneObjectGroup sog)
  147. {
  148. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: processing {0} {1}; {2}", sog.RootPart.Name, sog.RootPart.CreatorData, sog.RootPart.CreatorIdentification);
  149. AddUser(sog.RootPart.CreatorID, sog.RootPart.CreatorData);
  150. foreach (SceneObjectPart sop in sog.Parts)
  151. {
  152. AddUser(sop.CreatorID, sop.CreatorData);
  153. foreach (TaskInventoryItem item in sop.TaskInventory.Values)
  154. AddUser(item.CreatorID, item.CreatorData);
  155. }
  156. }
  157. private string[] GetUserNames(UUID uuid)
  158. {
  159. string[] returnstring = new string[2];
  160. if (m_UserCache.ContainsKey(uuid))
  161. {
  162. returnstring[0] = m_UserCache[uuid].FirstName;
  163. returnstring[1] = m_UserCache[uuid].LastName;
  164. return returnstring;
  165. }
  166. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(UUID.Zero, uuid);
  167. if (account != null)
  168. {
  169. returnstring[0] = account.FirstName;
  170. returnstring[1] = account.LastName;
  171. UserData user = new UserData();
  172. user.FirstName = account.FirstName;
  173. user.LastName = account.LastName;
  174. lock (m_UserCache)
  175. m_UserCache[uuid] = user;
  176. }
  177. else
  178. {
  179. returnstring[0] = "Unknown";
  180. returnstring[1] = "User";
  181. }
  182. return returnstring;
  183. }
  184. #region IUserManagement
  185. public string GetUserName(UUID uuid)
  186. {
  187. //m_log.DebugFormat("[XXX] GetUserName {0}", uuid);
  188. string[] names = GetUserNames(uuid);
  189. if (names.Length == 2)
  190. {
  191. string firstname = names[0];
  192. string lastname = names[1];
  193. return firstname + " " + lastname;
  194. }
  195. return "(hippos)";
  196. }
  197. public string GetUserHomeURL(UUID userID)
  198. {
  199. if (m_UserCache.ContainsKey(userID))
  200. return m_UserCache[userID].HomeURL;
  201. return string.Empty;
  202. }
  203. public string GetUserServerURL(UUID userID, string serverType)
  204. {
  205. if (m_UserCache.ContainsKey(userID))
  206. {
  207. UserData userdata = m_UserCache[userID];
  208. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  209. return userdata.ServerURLs[serverType].ToString();
  210. if (userdata.HomeURL != string.Empty)
  211. {
  212. UserAgentServiceConnector uConn = new UserAgentServiceConnector(userdata.HomeURL);
  213. userdata.ServerURLs = uConn.GetServerURLs(userID);
  214. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  215. return userdata.ServerURLs[serverType].ToString();
  216. }
  217. }
  218. return string.Empty;
  219. }
  220. public string GetUserUUI(UUID userID)
  221. {
  222. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, userID);
  223. if (account != null)
  224. return userID.ToString();
  225. if (m_UserCache.ContainsKey(userID))
  226. {
  227. UserData ud = m_UserCache[userID];
  228. string homeURL = ud.HomeURL;
  229. string first = ud.FirstName, last = ud.LastName;
  230. if (ud.LastName.StartsWith("@"))
  231. {
  232. string[] parts = ud.FirstName.Split('.');
  233. if (parts.Length >= 2)
  234. {
  235. first = parts[0];
  236. last = parts[1];
  237. }
  238. return userID + ";" + homeURL + ";" + first + " " + last;
  239. }
  240. }
  241. return userID.ToString();
  242. }
  243. public void AddUser(UUID uuid, string first, string last)
  244. {
  245. if (m_UserCache.ContainsKey(uuid))
  246. return;
  247. UserData user = new UserData();
  248. user.Id = uuid;
  249. user.FirstName = first;
  250. user.LastName = last;
  251. // user.ProfileURL = we should initialize this to the default
  252. AddUserInternal(user);
  253. }
  254. public void AddUser(UUID uuid, string first, string last, string profileURL)
  255. {
  256. AddUser(uuid, profileURL + ";" + first + " " + last);
  257. }
  258. public void AddUser(UUID id, string creatorData)
  259. {
  260. if (m_UserCache.ContainsKey(id))
  261. return;
  262. // m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, craetorData {1}", id, creatorData);
  263. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, id);
  264. if (account != null)
  265. {
  266. AddUser(id, account.FirstName, account.LastName);
  267. }
  268. else
  269. {
  270. UserData user = new UserData();
  271. user.Id = id;
  272. if (creatorData != null && creatorData != string.Empty)
  273. {
  274. //creatorData = <endpoint>;<name>
  275. string[] parts = creatorData.Split(';');
  276. if (parts.Length >= 1)
  277. {
  278. user.HomeURL = parts[0];
  279. try
  280. {
  281. Uri uri = new Uri(parts[0]);
  282. user.LastName = "@" + uri.Authority;
  283. }
  284. catch (UriFormatException)
  285. {
  286. m_log.DebugFormat("[SCENE]: Unable to parse Uri {0}", parts[0]);
  287. user.LastName = "@unknown";
  288. }
  289. }
  290. if (parts.Length >= 2)
  291. user.FirstName = parts[1].Replace(' ', '.');
  292. }
  293. else
  294. {
  295. user.FirstName = "Unknown";
  296. user.LastName = "User";
  297. }
  298. AddUserInternal(user);
  299. }
  300. }
  301. void AddUserInternal(UserData user)
  302. {
  303. lock (m_UserCache)
  304. m_UserCache[user.Id] = user;
  305. // m_log.DebugFormat(
  306. // "[USER MANAGEMENT MODULE]: Added user {0} {1} {2} {3}",
  307. // user.Id, user.FirstName, user.LastName, user.HomeURL);
  308. }
  309. //public void AddUser(UUID uuid, string userData)
  310. //{
  311. // if (m_UserCache.ContainsKey(uuid))
  312. // return;
  313. // UserData user = new UserData();
  314. // user.Id = uuid;
  315. // // userData = <profile url>;<name>
  316. // string[] parts = userData.Split(';');
  317. // if (parts.Length >= 1)
  318. // user.ProfileURL = parts[0].Trim();
  319. // if (parts.Length >= 2)
  320. // {
  321. // string[] name = parts[1].Trim().Split(' ');
  322. // if (name.Length >= 1)
  323. // user.FirstName = name[0];
  324. // if (name.Length >= 2)
  325. // user.LastName = name[1];
  326. // else
  327. // user.LastName = "?";
  328. // }
  329. // lock (m_UserCache)
  330. // m_UserCache.Add(uuid, user);
  331. // m_log.DebugFormat("[USER MANAGEMENT MODULE]: Added user {0} {1} {2} {3}", user.Id, user.FirstName, user.LastName, user.ProfileURL);
  332. //}
  333. #endregion IUserManagement
  334. private void HandleShowUsers(string module, string[] cmd)
  335. {
  336. if (m_UserCache.Count == 0)
  337. {
  338. MainConsole.Instance.Output("No users not found");
  339. return;
  340. }
  341. MainConsole.Instance.Output("UUID User Name");
  342. MainConsole.Instance.Output("-----------------------------------------------------------------------------");
  343. foreach (KeyValuePair<UUID, UserData> kvp in m_UserCache)
  344. {
  345. MainConsole.Instance.Output(String.Format("{0} {1} {2}",
  346. kvp.Key, kvp.Value.FirstName, kvp.Value.LastName));
  347. }
  348. return;
  349. }
  350. }
  351. }