UserManagementModule.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 System.Threading;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Monitoring;
  35. using OpenSim.Region.ClientStack.LindenUDP;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Services.Connectors.Hypergrid;
  41. using OpenMetaverse;
  42. using OpenMetaverse.Packets;
  43. using log4net;
  44. using Nini.Config;
  45. using Mono.Addins;
  46. using DirFindFlags = OpenMetaverse.DirectoryManager.DirFindFlags;
  47. namespace OpenSim.Region.CoreModules.Framework.UserManagement
  48. {
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "UserManagementModule")]
  50. public class UserManagementModule : ISharedRegionModule, IUserManagement, IPeople
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected bool m_Enabled;
  54. protected List<Scene> m_Scenes = new List<Scene>();
  55. protected IServiceThrottleModule m_ServiceThrottle;
  56. // The cache
  57. protected Dictionary<UUID, UserData> m_UserCache = new Dictionary<UUID, UserData>();
  58. protected bool m_DisplayChangingHomeURI = false;
  59. #region ISharedRegionModule
  60. public void Initialise(IConfigSource config)
  61. {
  62. string umanmod = config.Configs["Modules"].GetString("UserManagementModule", Name);
  63. if (umanmod == Name)
  64. {
  65. m_Enabled = true;
  66. Init();
  67. m_log.DebugFormat("[USER MANAGEMENT MODULE]: {0} is enabled", Name);
  68. }
  69. if(!m_Enabled)
  70. {
  71. return;
  72. }
  73. IConfig userManagementConfig = config.Configs["UserManagement"];
  74. if (userManagementConfig == null)
  75. return;
  76. m_DisplayChangingHomeURI = userManagementConfig.GetBoolean("DisplayChangingHomeURI", false);
  77. }
  78. public bool IsSharedModule
  79. {
  80. get { return true; }
  81. }
  82. public virtual string Name
  83. {
  84. get { return "BasicUserManagementModule"; }
  85. }
  86. public Type ReplaceableInterface
  87. {
  88. get { return null; }
  89. }
  90. public void AddRegion(Scene scene)
  91. {
  92. if (m_Enabled)
  93. {
  94. lock (m_Scenes)
  95. {
  96. m_Scenes.Add(scene);
  97. }
  98. scene.RegisterModuleInterface<IUserManagement>(this);
  99. scene.RegisterModuleInterface<IPeople>(this);
  100. scene.EventManager.OnNewClient += new EventManager.OnNewClientDelegate(EventManager_OnNewClient);
  101. scene.EventManager.OnPrimsLoaded += new EventManager.PrimsLoaded(EventManager_OnPrimsLoaded);
  102. }
  103. }
  104. public void RemoveRegion(Scene scene)
  105. {
  106. if (m_Enabled)
  107. {
  108. scene.UnregisterModuleInterface<IUserManagement>(this);
  109. lock (m_Scenes)
  110. {
  111. m_Scenes.Remove(scene);
  112. }
  113. }
  114. }
  115. public void RegionLoaded(Scene s)
  116. {
  117. if (m_Enabled && m_ServiceThrottle == null)
  118. m_ServiceThrottle = s.RequestModuleInterface<IServiceThrottleModule>();
  119. }
  120. public void PostInitialise()
  121. {
  122. }
  123. public void Close()
  124. {
  125. lock (m_Scenes)
  126. {
  127. m_Scenes.Clear();
  128. }
  129. lock (m_UserCache)
  130. m_UserCache.Clear();
  131. }
  132. #endregion ISharedRegionModule
  133. #region Event Handlers
  134. void EventManager_OnPrimsLoaded(Scene s)
  135. {
  136. // let's sniff all the user names referenced by objects in the scene
  137. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Caching creators' data from {0} ({1} objects)...", s.RegionInfo.RegionName, s.GetEntities().Length);
  138. s.ForEachSOG(delegate(SceneObjectGroup sog) { CacheCreators(sog); });
  139. }
  140. void EventManager_OnNewClient(IClientAPI client)
  141. {
  142. client.OnConnectionClosed += new Action<IClientAPI>(HandleConnectionClosed);
  143. client.OnNameFromUUIDRequest += new UUIDNameRequest(HandleUUIDNameRequest);
  144. client.OnAvatarPickerRequest += new AvatarPickerRequest(HandleAvatarPickerRequest);
  145. }
  146. void HandleConnectionClosed(IClientAPI client)
  147. {
  148. client.OnNameFromUUIDRequest -= new UUIDNameRequest(HandleUUIDNameRequest);
  149. client.OnAvatarPickerRequest -= new AvatarPickerRequest(HandleAvatarPickerRequest);
  150. }
  151. void HandleUUIDNameRequest(UUID uuid, IClientAPI client)
  152. {
  153. // m_log.DebugFormat(
  154. // "[USER MANAGEMENT MODULE]: Handling request for name binding of UUID {0} from {1}",
  155. // uuid, remote_client.Name);
  156. if (m_Scenes[0].LibraryService != null && (m_Scenes[0].LibraryService.LibraryRootFolder.Owner == uuid))
  157. {
  158. client.SendNameReply(uuid, "Mr", "OpenSim");
  159. }
  160. else
  161. {
  162. UserData user;
  163. /* bypass that continuation here when entry is already available */
  164. lock (m_UserCache)
  165. {
  166. if (m_UserCache.TryGetValue(uuid, out user))
  167. {
  168. if (!user.IsUnknownUser && user.HasGridUserTried)
  169. {
  170. client.SendNameReply(uuid, user.FirstName, user.LastName);
  171. return;
  172. }
  173. }
  174. }
  175. // Not found in cache, queue continuation
  176. m_ServiceThrottle.Enqueue("name", uuid.ToString(), delegate
  177. {
  178. //m_log.DebugFormat("[YYY]: Name request {0}", uuid);
  179. // As least upto September 2013, clients permanently cache UUID -> Name bindings. Some clients
  180. // appear to clear this when the user asks it to clear the cache, but others may not.
  181. //
  182. // So to avoid clients
  183. // (particularly Hypergrid clients) permanently binding "Unknown User" to a given UUID, we will
  184. // instead drop the request entirely.
  185. if (GetUser(uuid, out user))
  186. {
  187. client.SendNameReply(uuid, user.FirstName, user.LastName);
  188. }
  189. // else
  190. // m_log.DebugFormat(
  191. // "[USER MANAGEMENT MODULE]: No bound name for {0} found, ignoring request from {1}",
  192. // uuid, client.Name);
  193. });
  194. }
  195. }
  196. public void HandleAvatarPickerRequest(IClientAPI client, UUID avatarID, UUID RequestID, string query)
  197. {
  198. //EventManager.TriggerAvatarPickerRequest();
  199. m_log.DebugFormat("[USER MANAGEMENT MODULE]: HandleAvatarPickerRequest for {0}", query);
  200. List<UserData> users = GetUserData(query, 500, 1);
  201. AvatarPickerReplyPacket replyPacket = (AvatarPickerReplyPacket)PacketPool.Instance.GetPacket(PacketType.AvatarPickerReply);
  202. // TODO: don't create new blocks if recycling an old packet
  203. AvatarPickerReplyPacket.DataBlock[] searchData =
  204. new AvatarPickerReplyPacket.DataBlock[users.Count];
  205. AvatarPickerReplyPacket.AgentDataBlock agentData = new AvatarPickerReplyPacket.AgentDataBlock();
  206. agentData.AgentID = avatarID;
  207. agentData.QueryID = RequestID;
  208. replyPacket.AgentData = agentData;
  209. //byte[] bytes = new byte[AvatarResponses.Count*32];
  210. int i = 0;
  211. foreach (UserData item in users)
  212. {
  213. UUID translatedIDtem = item.Id;
  214. searchData[i] = new AvatarPickerReplyPacket.DataBlock();
  215. searchData[i].AvatarID = translatedIDtem;
  216. searchData[i].FirstName = Utils.StringToBytes((string)item.FirstName);
  217. searchData[i].LastName = Utils.StringToBytes((string)item.LastName);
  218. i++;
  219. }
  220. if (users.Count == 0)
  221. {
  222. searchData = new AvatarPickerReplyPacket.DataBlock[0];
  223. }
  224. replyPacket.Data = searchData;
  225. AvatarPickerReplyAgentDataArgs agent_data = new AvatarPickerReplyAgentDataArgs();
  226. agent_data.AgentID = replyPacket.AgentData.AgentID;
  227. agent_data.QueryID = replyPacket.AgentData.QueryID;
  228. List<AvatarPickerReplyDataArgs> data_args = new List<AvatarPickerReplyDataArgs>();
  229. for (i = 0; i < replyPacket.Data.Length; i++)
  230. {
  231. AvatarPickerReplyDataArgs data_arg = new AvatarPickerReplyDataArgs();
  232. data_arg.AvatarID = replyPacket.Data[i].AvatarID;
  233. data_arg.FirstName = replyPacket.Data[i].FirstName;
  234. data_arg.LastName = replyPacket.Data[i].LastName;
  235. data_args.Add(data_arg);
  236. }
  237. client.SendAvatarPickerReply(agent_data, data_args);
  238. }
  239. protected virtual void AddAdditionalUsers(string query, List<UserData> users)
  240. {
  241. }
  242. #endregion Event Handlers
  243. #region IPeople
  244. public List<UserData> GetUserData(string query, int page_size, int page_number)
  245. {
  246. // search the user accounts service
  247. List<UserAccount> accs = m_Scenes[0].UserAccountService.GetUserAccounts(m_Scenes[0].RegionInfo.ScopeID, query);
  248. List<UserData> users = new List<UserData>();
  249. if (accs != null)
  250. {
  251. foreach (UserAccount acc in accs)
  252. {
  253. UserData ud = new UserData();
  254. ud.FirstName = acc.FirstName;
  255. ud.LastName = acc.LastName;
  256. ud.Id = acc.PrincipalID;
  257. ud.HasGridUserTried = true;
  258. ud.IsUnknownUser = false;
  259. users.Add(ud);
  260. }
  261. }
  262. // search the local cache
  263. foreach (UserData data in m_UserCache.Values)
  264. {
  265. if (data.Id != UUID.Zero && !data.IsUnknownUser &&
  266. users.Find(delegate(UserData d) { return d.Id == data.Id; }) == null &&
  267. (data.FirstName.ToLower().StartsWith(query.ToLower()) || data.LastName.ToLower().StartsWith(query.ToLower())))
  268. users.Add(data);
  269. }
  270. AddAdditionalUsers(query, users);
  271. return users;
  272. }
  273. #endregion IPeople
  274. private void CacheCreators(SceneObjectGroup sog)
  275. {
  276. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: processing {0} {1}; {2}", sog.RootPart.Name, sog.RootPart.CreatorData, sog.RootPart.CreatorIdentification);
  277. AddUser(sog.RootPart.CreatorID, sog.RootPart.CreatorData);
  278. foreach (SceneObjectPart sop in sog.Parts)
  279. {
  280. AddUser(sop.CreatorID, sop.CreatorData);
  281. foreach (TaskInventoryItem item in sop.TaskInventory.Values)
  282. AddUser(item.CreatorID, item.CreatorData);
  283. }
  284. }
  285. #region IUserManagement
  286. public UUID GetUserIdByName(string name)
  287. {
  288. string[] parts = name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  289. if (parts.Length < 2)
  290. throw new Exception("Name must have 2 components");
  291. return GetUserIdByName(parts[0], parts[1]);
  292. }
  293. public UUID GetUserIdByName(string firstName, string lastName)
  294. {
  295. // TODO: Optimize for reverse lookup if this gets used by non-console commands.
  296. lock (m_UserCache)
  297. {
  298. foreach (UserData user in m_UserCache.Values)
  299. {
  300. if (user.FirstName == firstName && user.LastName == lastName)
  301. return user.Id;
  302. }
  303. }
  304. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(UUID.Zero, firstName, lastName);
  305. if (account != null)
  306. return account.PrincipalID;
  307. return UUID.Zero;
  308. }
  309. public string GetUserName(UUID uuid)
  310. {
  311. UserData user;
  312. GetUser(uuid, out user);
  313. return user.FirstName + " " + user.LastName;
  314. }
  315. public string GetUserHomeURL(UUID userID)
  316. {
  317. UserData user;
  318. if(GetUser(userID, out user))
  319. {
  320. return user.HomeURL;
  321. }
  322. return string.Empty;
  323. }
  324. public string GetUserServerURL(UUID userID, string serverType)
  325. {
  326. UserData userdata;
  327. if(!GetUser(userID, out userdata))
  328. {
  329. return string.Empty;
  330. }
  331. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  332. {
  333. return userdata.ServerURLs[serverType].ToString();
  334. }
  335. if (!string.IsNullOrEmpty(userdata.HomeURL))
  336. {
  337. // m_log.DebugFormat("[USER MANAGEMENT MODULE]: Requested url type {0} for {1}", serverType, userID);
  338. UserAgentServiceConnector uConn = new UserAgentServiceConnector(userdata.HomeURL);
  339. try
  340. {
  341. userdata.ServerURLs = uConn.GetServerURLs(userID);
  342. }
  343. catch(System.Net.WebException e)
  344. {
  345. m_log.DebugFormat("[USER MANAGEMENT MODULE]: GetServerURLs call failed {0}", e.Message);
  346. userdata.ServerURLs = new Dictionary<string, object>();
  347. }
  348. catch (Exception e)
  349. {
  350. m_log.Debug("[USER MANAGEMENT MODULE]: GetServerURLs call failed ", e);
  351. userdata.ServerURLs = new Dictionary<string, object>();
  352. }
  353. if (userdata.ServerURLs != null && userdata.ServerURLs.ContainsKey(serverType) && userdata.ServerURLs[serverType] != null)
  354. return userdata.ServerURLs[serverType].ToString();
  355. }
  356. return string.Empty;
  357. }
  358. public string GetUserUUI(UUID userID)
  359. {
  360. string uui;
  361. GetUserUUI(userID, out uui);
  362. return uui;
  363. }
  364. public bool GetUserUUI(UUID userID, out string uui)
  365. {
  366. UserData ud;
  367. bool result = GetUser(userID, out ud);
  368. if (ud != null)
  369. {
  370. string homeURL = ud.HomeURL;
  371. string first = ud.FirstName, last = ud.LastName;
  372. if (ud.LastName.StartsWith("@"))
  373. {
  374. string[] parts = ud.FirstName.Split('.');
  375. if (parts.Length >= 2)
  376. {
  377. first = parts[0];
  378. last = parts[1];
  379. }
  380. uui = userID + ";" + homeURL + ";" + first + " " + last;
  381. }
  382. }
  383. uui = userID.ToString();
  384. return result;
  385. }
  386. #region Cache Management
  387. public bool GetUser(UUID uuid, out UserData userdata)
  388. {
  389. lock (m_UserCache)
  390. {
  391. if (m_UserCache.TryGetValue(uuid, out userdata))
  392. {
  393. if (userdata.HasGridUserTried)
  394. {
  395. return true;
  396. }
  397. }
  398. else
  399. {
  400. userdata = new UserData();
  401. userdata.HasGridUserTried = false;
  402. userdata.Id = uuid;
  403. userdata.FirstName = "Unknown";
  404. userdata.LastName = "UserUMMAU42";
  405. userdata.HomeURL = string.Empty;
  406. userdata.IsUnknownUser = true;
  407. userdata.HasGridUserTried = false;
  408. }
  409. }
  410. /* BEGIN: do not wrap this code in any lock here
  411. * There are HTTP calls in here.
  412. */
  413. if (!userdata.HasGridUserTried)
  414. {
  415. /* rewrite here */
  416. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  417. if (account != null)
  418. {
  419. userdata.FirstName = account.FirstName;
  420. userdata.LastName = account.LastName;
  421. userdata.HomeURL = string.Empty;
  422. userdata.IsUnknownUser = false;
  423. userdata.HasGridUserTried = true;
  424. }
  425. }
  426. if (!userdata.HasGridUserTried)
  427. {
  428. GridUserInfo uInfo = null;
  429. if (null != m_Scenes[0].GridUserService)
  430. {
  431. uInfo = m_Scenes[0].GridUserService.GetGridUserInfo(uuid.ToString());
  432. }
  433. if (uInfo != null)
  434. {
  435. string url, first, last, tmp;
  436. UUID u;
  437. if(uInfo.UserID.Length <= 36)
  438. {
  439. /* not a UUI */
  440. }
  441. else if (Util.ParseUniversalUserIdentifier(uInfo.UserID, out u, out url, out first, out last, out tmp))
  442. {
  443. if (url != string.Empty)
  444. {
  445. userdata.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  446. userdata.HomeURL = url;
  447. try
  448. {
  449. userdata.LastName = "@" + new Uri(url).Authority;
  450. userdata.IsUnknownUser = false;
  451. }
  452. catch
  453. {
  454. userdata.LastName = "@unknown";
  455. }
  456. userdata.HasGridUserTried = true;
  457. }
  458. }
  459. else
  460. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Unable to parse UUI {0}", uInfo.UserID);
  461. }
  462. }
  463. /* END: do not wrap this code in any lock here */
  464. lock (m_UserCache)
  465. {
  466. m_UserCache[uuid] = userdata;
  467. }
  468. return !userdata.IsUnknownUser;
  469. }
  470. public void AddUser(UUID uuid, string first, string last)
  471. {
  472. lock(m_UserCache)
  473. {
  474. if(!m_UserCache.ContainsKey(uuid))
  475. {
  476. UserData user = new UserData();
  477. user.Id = uuid;
  478. user.FirstName = first;
  479. user.LastName = last;
  480. user.IsUnknownUser = false;
  481. user.HasGridUserTried = false;
  482. m_UserCache.Add(uuid, user);
  483. }
  484. }
  485. }
  486. public void AddUser(UUID uuid, string first, string last, string homeURL)
  487. {
  488. //m_log.DebugFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, first {1}, last {2}, url {3}", uuid, first, last, homeURL);
  489. UserData oldUser;
  490. lock (m_UserCache)
  491. {
  492. if (m_UserCache.TryGetValue(uuid, out oldUser))
  493. {
  494. if (!oldUser.IsUnknownUser)
  495. {
  496. if (homeURL != oldUser.HomeURL && m_DisplayChangingHomeURI)
  497. {
  498. m_log.DebugFormat("[USER MANAGEMENT MODULE]: Different HomeURI for {0} {1} ({2}): {3} and {4}",
  499. first, last, uuid.ToString(), homeURL, oldUser.HomeURL);
  500. }
  501. /* no update needed */
  502. return;
  503. }
  504. }
  505. else if(!m_UserCache.ContainsKey(uuid))
  506. {
  507. oldUser = new UserData();
  508. oldUser.HasGridUserTried = false;
  509. oldUser.IsUnknownUser = false;
  510. if (homeURL != string.Empty)
  511. {
  512. oldUser.FirstName = first.Replace(" ", ".") + "." + last.Replace(" ", ".");
  513. try
  514. {
  515. oldUser.LastName = "@" + new Uri(homeURL).Authority;
  516. oldUser.IsUnknownUser = false;
  517. }
  518. catch
  519. {
  520. oldUser.LastName = "@unknown";
  521. }
  522. }
  523. else
  524. {
  525. oldUser.FirstName = first;
  526. oldUser.LastName = last;
  527. }
  528. oldUser.HomeURL = homeURL;
  529. oldUser.Id = uuid;
  530. m_UserCache.Add(uuid, oldUser);
  531. }
  532. }
  533. }
  534. public void AddUser(UUID id, string creatorData)
  535. {
  536. // m_log.InfoFormat("[USER MANAGEMENT MODULE]: Adding user with id {0}, creatorData {1}", id, creatorData);
  537. if(string.IsNullOrEmpty(creatorData))
  538. {
  539. AddUser(id, string.Empty, string.Empty, string.Empty);
  540. }
  541. else
  542. {
  543. string homeURL;
  544. string firstname = string.Empty;
  545. string lastname = string.Empty;
  546. //creatorData = <endpoint>;<name>
  547. string[] parts = creatorData.Split(';');
  548. if(parts.Length > 1)
  549. {
  550. string[] nameparts = parts[1].Split(' ');
  551. firstname = nameparts[0];
  552. for(int xi = 1; xi < nameparts.Length; ++xi)
  553. {
  554. if(xi != 1)
  555. {
  556. lastname += " ";
  557. }
  558. lastname += nameparts[xi];
  559. }
  560. }
  561. else
  562. {
  563. firstname = "Unknown";
  564. lastname = "UserUMMAU5";
  565. }
  566. if (parts.Length >= 1)
  567. {
  568. homeURL = parts[0];
  569. if(Uri.IsWellFormedUriString(homeURL, UriKind.Absolute))
  570. {
  571. AddUser(id, firstname, lastname, homeURL);
  572. }
  573. else
  574. {
  575. m_log.DebugFormat("[SCENE]: Unable to parse Uri {0} for CreatorID {1}", parts[0], creatorData);
  576. lock (m_UserCache)
  577. {
  578. if(!m_UserCache.ContainsKey(id))
  579. {
  580. UserData newUser = new UserData();
  581. newUser.Id = id;
  582. newUser.FirstName = firstname + "." + lastname.Replace(' ', '.');
  583. newUser.LastName = "@unknown";
  584. newUser.HomeURL = string.Empty;
  585. newUser.HasGridUserTried = false;
  586. newUser.IsUnknownUser = true; /* we mark those users as Unknown user so a re-retrieve may be activated */
  587. m_UserCache.Add(id, newUser);
  588. }
  589. }
  590. }
  591. }
  592. else
  593. {
  594. lock(m_UserCache)
  595. {
  596. if(!m_UserCache.ContainsKey(id))
  597. {
  598. UserData newUser = new UserData();
  599. newUser.Id = id;
  600. newUser.FirstName = "Unknown";
  601. newUser.LastName = "UserUMMAU4";
  602. newUser.HomeURL = string.Empty;
  603. newUser.IsUnknownUser = true;
  604. newUser.HasGridUserTried = false;
  605. m_UserCache.Add(id, newUser);
  606. }
  607. }
  608. }
  609. }
  610. }
  611. #endregion
  612. public bool IsLocalGridUser(UUID uuid)
  613. {
  614. UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, uuid);
  615. if (account == null || (account != null && !account.LocalToGrid))
  616. return false;
  617. return true;
  618. }
  619. #endregion IUserManagement
  620. protected void Init()
  621. {
  622. AddUser(UUID.Zero, "Unknown", "User");
  623. RegisterConsoleCmds();
  624. }
  625. protected void RegisterConsoleCmds()
  626. {
  627. MainConsole.Instance.Commands.AddCommand("Users", true,
  628. "show name",
  629. "show name <uuid>",
  630. "Show the bindings between a single user UUID and a user name",
  631. String.Empty,
  632. HandleShowUser);
  633. MainConsole.Instance.Commands.AddCommand("Users", true,
  634. "show names",
  635. "show names",
  636. "Show the bindings between user UUIDs and user names",
  637. String.Empty,
  638. HandleShowUsers);
  639. MainConsole.Instance.Commands.AddCommand("Users", true,
  640. "reset user cache",
  641. "reset user cache",
  642. "reset user cache to allow changed settings to be applied",
  643. String.Empty,
  644. HandleResetUserCache);
  645. }
  646. private void HandleResetUserCache(string module, string[] cmd)
  647. {
  648. lock(m_UserCache)
  649. {
  650. m_UserCache.Clear();
  651. }
  652. }
  653. private void HandleShowUser(string module, string[] cmd)
  654. {
  655. if (cmd.Length < 3)
  656. {
  657. MainConsole.Instance.OutputFormat("Usage: show name <uuid>");
  658. return;
  659. }
  660. UUID userId;
  661. if (!ConsoleUtil.TryParseConsoleUuid(MainConsole.Instance, cmd[2], out userId))
  662. return;
  663. UserData ud;
  664. if(!GetUser(userId, out ud))
  665. {
  666. MainConsole.Instance.OutputFormat("No name known for user with id {0}", userId);
  667. return;
  668. }
  669. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  670. cdt.AddColumn("UUID", 36);
  671. cdt.AddColumn("Name", 30);
  672. cdt.AddColumn("HomeURL", 40);
  673. cdt.AddRow(userId, string.Format("{0} {1}", ud.FirstName, ud.LastName), ud.HomeURL);
  674. MainConsole.Instance.Output(cdt.ToString());
  675. }
  676. private void HandleShowUsers(string module, string[] cmd)
  677. {
  678. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  679. cdt.AddColumn("UUID", 36);
  680. cdt.AddColumn("Name", 30);
  681. cdt.AddColumn("HomeURL", 40);
  682. cdt.AddColumn("Checked", 10);
  683. Dictionary<UUID, UserData> copyDict;
  684. lock(m_UserCache)
  685. {
  686. copyDict = new Dictionary<UUID, UserData>(m_UserCache);
  687. }
  688. foreach(KeyValuePair<UUID, UserData> kvp in copyDict)
  689. {
  690. cdt.AddRow(kvp.Key, string.Format("{0} {1}", kvp.Value.FirstName, kvp.Value.LastName), kvp.Value.HomeURL, kvp.Value.HasGridUserTried ? "yes" : "no");
  691. }
  692. MainConsole.Instance.Output(cdt.ToString());
  693. }
  694. }
  695. }