MySQLUserData.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Reflection;
  32. using System.Text.RegularExpressions;
  33. using System.Threading;
  34. using log4net;
  35. using MySql.Data.MySqlClient;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. namespace OpenSim.Data.MySQL
  39. {
  40. /// <summary>
  41. /// A database interface class to a user profile storage system
  42. /// </summary>
  43. public class MySQLUserData : UserDataBase
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private MySQLManager m_database;
  47. private string m_connectionString;
  48. private object m_dbLock = new object();
  49. public int m_maxConnections = 10;
  50. public int m_lastConnect;
  51. private string m_agentsTableName = "agents";
  52. private string m_usersTableName = "users";
  53. private string m_userFriendsTableName = "userfriends";
  54. private string m_appearanceTableName = "avatarappearance";
  55. private string m_attachmentsTableName = "avatarattachments";
  56. public override void Initialise()
  57. {
  58. m_log.Info("[MySQLUserData]: " + Name + " cannot be default-initialized!");
  59. throw new PluginNotInitialisedException(Name);
  60. }
  61. /// <summary>
  62. /// Initialise User Interface
  63. /// Loads and initialises the MySQL storage plugin
  64. /// Warns and uses the obsolete mysql_connection.ini if connect string is empty.
  65. /// Checks for migration
  66. /// </summary>
  67. /// <param name="connect">connect string.</param>
  68. public override void Initialise(string connect)
  69. {
  70. m_connectionString = connect;
  71. m_database = new MySQLManager(connect);
  72. // This actually does the roll forward assembly stuff
  73. Assembly assem = GetType().Assembly;
  74. using (MySql.Data.MySqlClient.MySqlConnection dbcon = new MySql.Data.MySqlClient.MySqlConnection(m_connectionString))
  75. {
  76. dbcon.Open();
  77. Migration m = new Migration(dbcon, assem, "UserStore");
  78. m.Update();
  79. }
  80. }
  81. public override void Dispose()
  82. {
  83. }
  84. // see IUserDataPlugin
  85. public override UserProfileData GetUserByName(string user, string last)
  86. {
  87. try
  88. {
  89. Dictionary<string, object> param = new Dictionary<string, object>();
  90. param["?first"] = user;
  91. param["?second"] = last;
  92. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  93. {
  94. dbcon.Open();
  95. using (IDbCommand result = m_database.Query(dbcon,
  96. "SELECT * FROM " + m_usersTableName + " WHERE username = ?first AND lastname = ?second", param))
  97. {
  98. using (IDataReader reader = result.ExecuteReader())
  99. {
  100. UserProfileData row = m_database.readUserRow(reader);
  101. return row;
  102. }
  103. }
  104. }
  105. }
  106. catch (Exception e)
  107. {
  108. m_log.Error(e.Message, e);
  109. return null;
  110. }
  111. }
  112. #region User Friends List Data
  113. public override void AddNewUserFriend(UUID friendlistowner, UUID friend, uint perms)
  114. {
  115. int dtvalue = Util.UnixTimeSinceEpoch();
  116. Dictionary<string, object> param = new Dictionary<string, object>();
  117. param["?ownerID"] = friendlistowner.ToString();
  118. param["?friendID"] = friend.ToString();
  119. param["?friendPerms"] = perms.ToString();
  120. param["?datetimestamp"] = dtvalue.ToString();
  121. try
  122. {
  123. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  124. {
  125. dbcon.Open();
  126. using (IDbCommand adder = m_database.Query(dbcon,
  127. "INSERT INTO `" + m_userFriendsTableName + "` " +
  128. "(`ownerID`,`friendID`,`friendPerms`,`datetimestamp`) " +
  129. "VALUES " +
  130. "(?ownerID,?friendID,?friendPerms,?datetimestamp)",
  131. param))
  132. {
  133. adder.ExecuteNonQuery();
  134. }
  135. using (IDbCommand adder = m_database.Query(dbcon,
  136. "INSERT INTO `" + m_userFriendsTableName + "` " +
  137. "(`ownerID`,`friendID`,`friendPerms`,`datetimestamp`) " +
  138. "VALUES " +
  139. "(?friendID,?ownerID,?friendPerms,?datetimestamp)",
  140. param))
  141. {
  142. adder.ExecuteNonQuery();
  143. }
  144. }
  145. }
  146. catch (Exception e)
  147. {
  148. m_log.Error(e.Message, e);
  149. return;
  150. }
  151. }
  152. public override void RemoveUserFriend(UUID friendlistowner, UUID friend)
  153. {
  154. Dictionary<string, object> param = new Dictionary<string, object>();
  155. param["?ownerID"] = friendlistowner.ToString();
  156. param["?friendID"] = friend.ToString();
  157. try
  158. {
  159. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  160. {
  161. dbcon.Open();
  162. using (IDbCommand updater = m_database.Query(dbcon,
  163. "delete from " + m_userFriendsTableName + " where ownerID = ?ownerID and friendID = ?friendID",
  164. param))
  165. {
  166. updater.ExecuteNonQuery();
  167. }
  168. using (IDbCommand updater = m_database.Query(dbcon,
  169. "delete from " + m_userFriendsTableName + " where ownerID = ?friendID and friendID = ?ownerID",
  170. param))
  171. {
  172. updater.ExecuteNonQuery();
  173. }
  174. }
  175. }
  176. catch (Exception e)
  177. {
  178. m_log.Error(e.Message, e);
  179. return;
  180. }
  181. }
  182. public override void UpdateUserFriendPerms(UUID friendlistowner, UUID friend, uint perms)
  183. {
  184. Dictionary<string, object> param = new Dictionary<string, object>();
  185. param["?ownerID"] = friendlistowner.ToString();
  186. param["?friendID"] = friend.ToString();
  187. param["?friendPerms"] = perms.ToString();
  188. try
  189. {
  190. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  191. {
  192. dbcon.Open();
  193. using (IDbCommand updater = m_database.Query(dbcon,
  194. "update " + m_userFriendsTableName +
  195. " SET friendPerms = ?friendPerms " +
  196. "where ownerID = ?ownerID and friendID = ?friendID",
  197. param))
  198. {
  199. updater.ExecuteNonQuery();
  200. }
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. m_log.Error(e.Message, e);
  206. return;
  207. }
  208. }
  209. public override List<FriendListItem> GetUserFriendList(UUID friendlistowner)
  210. {
  211. List<FriendListItem> Lfli = new List<FriendListItem>();
  212. Dictionary<string, object> param = new Dictionary<string, object>();
  213. param["?ownerID"] = friendlistowner.ToString();
  214. try
  215. {
  216. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  217. {
  218. dbcon.Open();
  219. //Left Join userfriends to itself
  220. using (IDbCommand result = m_database.Query(dbcon,
  221. "select a.ownerID,a.friendID,a.friendPerms,b.friendPerms as ownerperms from " +
  222. m_userFriendsTableName + " as a, " + m_userFriendsTableName + " as b" +
  223. " where a.ownerID = ?ownerID and b.ownerID = a.friendID and b.friendID = a.ownerID",
  224. param))
  225. {
  226. using (IDataReader reader = result.ExecuteReader())
  227. {
  228. while (reader.Read())
  229. {
  230. FriendListItem fli = new FriendListItem();
  231. fli.FriendListOwner = new UUID((string)reader["ownerID"]);
  232. fli.Friend = new UUID((string)reader["friendID"]);
  233. fli.FriendPerms = (uint)Convert.ToInt32(reader["friendPerms"]);
  234. // This is not a real column in the database table, it's a joined column from the opposite record
  235. fli.FriendListOwnerPerms = (uint)Convert.ToInt32(reader["ownerperms"]);
  236. Lfli.Add(fli);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. catch (Exception e)
  243. {
  244. m_log.Error(e.Message, e);
  245. return Lfli;
  246. }
  247. return Lfli;
  248. }
  249. override public Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos (List<UUID> uuids)
  250. {
  251. Dictionary<UUID, FriendRegionInfo> infos = new Dictionary<UUID,FriendRegionInfo>();
  252. try
  253. {
  254. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  255. {
  256. dbcon.Open();
  257. foreach (UUID uuid in uuids)
  258. {
  259. Dictionary<string, object> param = new Dictionary<string, object>();
  260. param["?uuid"] = uuid.ToString();
  261. using (IDbCommand result = m_database.Query(dbcon, "select agentOnline,currentHandle from " + m_agentsTableName +
  262. " where UUID = ?uuid", param))
  263. {
  264. using (IDataReader reader = result.ExecuteReader())
  265. {
  266. while (reader.Read())
  267. {
  268. FriendRegionInfo fri = new FriendRegionInfo();
  269. fri.isOnline = (sbyte)reader["agentOnline"] != 0;
  270. fri.regionHandle = (ulong)reader["currentHandle"];
  271. infos[uuid] = fri;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. catch (Exception e)
  279. {
  280. m_log.Warn("[MYSQL]: Got exception on trying to find friends regions:", e);
  281. m_log.Error(e.Message, e);
  282. }
  283. return infos;
  284. }
  285. #endregion
  286. public override List<AvatarPickerAvatar> GeneratePickerResults(UUID queryID, string query)
  287. {
  288. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  289. Regex objAlphaNumericPattern = new Regex("[^a-zA-Z0-9]");
  290. string[] querysplit;
  291. querysplit = query.Split(' ');
  292. if (querysplit.Length > 1 && querysplit[1].Trim() != String.Empty)
  293. {
  294. Dictionary<string, object> param = new Dictionary<string, object>();
  295. param["?first"] = objAlphaNumericPattern.Replace(querysplit[0], String.Empty) + "%";
  296. param["?second"] = objAlphaNumericPattern.Replace(querysplit[1], String.Empty) + "%";
  297. try
  298. {
  299. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  300. {
  301. dbcon.Open();
  302. using (IDbCommand result = m_database.Query(dbcon,
  303. "SELECT UUID,username,lastname FROM " + m_usersTableName +
  304. " WHERE username like ?first AND lastname like ?second LIMIT 100",
  305. param))
  306. {
  307. using (IDataReader reader = result.ExecuteReader())
  308. {
  309. while (reader.Read())
  310. {
  311. AvatarPickerAvatar user = new AvatarPickerAvatar();
  312. user.AvatarID = new UUID((string)reader["UUID"]);
  313. user.firstName = (string)reader["username"];
  314. user.lastName = (string)reader["lastname"];
  315. returnlist.Add(user);
  316. }
  317. }
  318. }
  319. }
  320. }
  321. catch (Exception e)
  322. {
  323. m_log.Error(e.Message, e);
  324. return returnlist;
  325. }
  326. }
  327. else
  328. {
  329. try
  330. {
  331. Dictionary<string, object> param = new Dictionary<string, object>();
  332. param["?first"] = objAlphaNumericPattern.Replace(querysplit[0], String.Empty) + "%";
  333. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  334. {
  335. dbcon.Open();
  336. using (IDbCommand result = m_database.Query(dbcon,
  337. "SELECT UUID,username,lastname FROM " + m_usersTableName +
  338. " WHERE username like ?first OR lastname like ?first LIMIT 100",
  339. param))
  340. {
  341. using (IDataReader reader = result.ExecuteReader())
  342. {
  343. while (reader.Read())
  344. {
  345. AvatarPickerAvatar user = new AvatarPickerAvatar();
  346. user.AvatarID = new UUID((string)reader["UUID"]);
  347. user.firstName = (string)reader["username"];
  348. user.lastName = (string)reader["lastname"];
  349. returnlist.Add(user);
  350. }
  351. }
  352. }
  353. }
  354. }
  355. catch (Exception e)
  356. {
  357. m_log.Error(e.Message, e);
  358. return returnlist;
  359. }
  360. }
  361. return returnlist;
  362. }
  363. /// <summary>
  364. /// See IUserDataPlugin
  365. /// </summary>
  366. /// <param name="uuid">User UUID</param>
  367. /// <returns>User profile data</returns>
  368. public override UserProfileData GetUserByUUID(UUID uuid)
  369. {
  370. try
  371. {
  372. Dictionary<string, object> param = new Dictionary<string, object>();
  373. param["?uuid"] = uuid.ToString();
  374. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  375. {
  376. dbcon.Open();
  377. using (IDbCommand result = m_database.Query(dbcon, "SELECT * FROM " + m_usersTableName + " WHERE UUID = ?uuid", param))
  378. {
  379. using (IDataReader reader = result.ExecuteReader())
  380. {
  381. UserProfileData row = m_database.readUserRow(reader);
  382. return row;
  383. }
  384. }
  385. }
  386. }
  387. catch (Exception e)
  388. {
  389. m_log.Error(e.Message, e);
  390. return null;
  391. }
  392. }
  393. /// <summary>
  394. /// Returns a user session searching by name
  395. /// </summary>
  396. /// <param name="name">The account name : "Username Lastname"</param>
  397. /// <returns>The users session</returns>
  398. public override UserAgentData GetAgentByName(string name)
  399. {
  400. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  401. }
  402. /// <summary>
  403. /// Returns a user session by account name
  404. /// </summary>
  405. /// <param name="user">First part of the users account name</param>
  406. /// <param name="last">Second part of the users account name</param>
  407. /// <returns>The users session</returns>
  408. public override UserAgentData GetAgentByName(string user, string last)
  409. {
  410. UserProfileData profile = GetUserByName(user, last);
  411. return GetAgentByUUID(profile.ID);
  412. }
  413. /// <summary>
  414. /// </summary>
  415. /// <param name="AgentID"></param>
  416. /// <param name="WebLoginKey"></param>
  417. /// <remarks>is it still used ?</remarks>
  418. public override void StoreWebLoginKey(UUID AgentID, UUID WebLoginKey)
  419. {
  420. Dictionary<string, string> param = new Dictionary<string, string>();
  421. param["?UUID"] = AgentID.ToString();
  422. param["?webLoginKey"] = WebLoginKey.ToString();
  423. try
  424. {
  425. m_database.ExecuteParameterizedSql(
  426. "update " + m_usersTableName + " SET webLoginKey = ?webLoginKey " +
  427. "where UUID = ?UUID",
  428. param);
  429. }
  430. catch (Exception e)
  431. {
  432. m_log.Error(e.Message, e);
  433. return;
  434. }
  435. }
  436. /// <summary>
  437. /// Returns an agent session by account UUID
  438. /// </summary>
  439. /// <param name="uuid">The accounts UUID</param>
  440. /// <returns>The users session</returns>
  441. public override UserAgentData GetAgentByUUID(UUID uuid)
  442. {
  443. try
  444. {
  445. Dictionary<string, object> param = new Dictionary<string, object>();
  446. param["?uuid"] = uuid.ToString();
  447. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  448. {
  449. dbcon.Open();
  450. using (IDbCommand result = m_database.Query(dbcon, "SELECT * FROM " + m_agentsTableName + " WHERE UUID = ?uuid", param))
  451. {
  452. using (IDataReader reader = result.ExecuteReader())
  453. {
  454. UserAgentData row = m_database.readAgentRow(reader);
  455. return row;
  456. }
  457. }
  458. }
  459. }
  460. catch (Exception e)
  461. {
  462. m_log.Error(e.Message, e);
  463. return null;
  464. }
  465. }
  466. /// <summary>
  467. /// Creates a new users profile
  468. /// </summary>
  469. /// <param name="user">The user profile to create</param>
  470. public override void AddNewUserProfile(UserProfileData user)
  471. {
  472. UUID zero = UUID.Zero;
  473. if (user.ID == zero)
  474. {
  475. return;
  476. }
  477. try
  478. {
  479. m_database.insertUserRow(
  480. user.ID, user.FirstName, user.SurName, user.Email, user.PasswordHash, user.PasswordSalt,
  481. user.HomeRegion, user.HomeRegionID, user.HomeLocation.X, user.HomeLocation.Y,
  482. user.HomeLocation.Z,
  483. user.HomeLookAt.X, user.HomeLookAt.Y, user.HomeLookAt.Z, user.Created,
  484. user.LastLogin, user.UserInventoryURI, user.UserAssetURI,
  485. user.CanDoMask, user.WantDoMask,
  486. user.AboutText, user.FirstLifeAboutText, user.Image,
  487. user.FirstLifeImage, user.WebLoginKey, user.UserFlags, user.GodLevel, user.CustomType, user.Partner);
  488. }
  489. catch (Exception e)
  490. {
  491. m_log.Error(e.Message, e);
  492. }
  493. }
  494. /// <summary>
  495. /// Creates a new agent
  496. /// </summary>
  497. /// <param name="agent">The agent to create</param>
  498. public override void AddNewUserAgent(UserAgentData agent)
  499. {
  500. UUID zero = UUID.Zero;
  501. if (agent.ProfileID == zero || agent.SessionID == zero)
  502. return;
  503. try
  504. {
  505. m_database.insertAgentRow(agent);
  506. }
  507. catch (Exception e)
  508. {
  509. m_log.Error(e.Message, e);
  510. }
  511. }
  512. /// <summary>
  513. /// Updates a user profile stored in the DB
  514. /// </summary>
  515. /// <param name="user">The profile data to use to update the DB</param>
  516. public override bool UpdateUserProfile(UserProfileData user)
  517. {
  518. try
  519. {
  520. m_database.updateUserRow(
  521. user.ID, user.FirstName, user.SurName, user.Email, user.PasswordHash, user.PasswordSalt,
  522. user.HomeRegion, user.HomeRegionID, user.HomeLocation.X, user.HomeLocation.Y,
  523. user.HomeLocation.Z, user.HomeLookAt.X,
  524. user.HomeLookAt.Y, user.HomeLookAt.Z, user.Created, user.LastLogin,
  525. user.UserInventoryURI,
  526. user.UserAssetURI, user.CanDoMask, user.WantDoMask, user.AboutText,
  527. user.FirstLifeAboutText, user.Image, user.FirstLifeImage, user.WebLoginKey,
  528. user.UserFlags, user.GodLevel, user.CustomType, user.Partner);
  529. return true;
  530. }
  531. catch
  532. {
  533. return false;
  534. }
  535. }
  536. /// <summary>
  537. /// Performs a money transfer request between two accounts
  538. /// </summary>
  539. /// <param name="from">The senders account ID</param>
  540. /// <param name="to">The receivers account ID</param>
  541. /// <param name="amount">The amount to transfer</param>
  542. /// <returns>Success?</returns>
  543. public override bool MoneyTransferRequest(UUID from, UUID to, uint amount)
  544. {
  545. return false;
  546. }
  547. /// <summary>
  548. /// Performs an inventory transfer request between two accounts
  549. /// </summary>
  550. /// <remarks>TODO: Move to inventory server</remarks>
  551. /// <param name="from">The senders account ID</param>
  552. /// <param name="to">The receivers account ID</param>
  553. /// <param name="item">The item to transfer</param>
  554. /// <returns>Success?</returns>
  555. public override bool InventoryTransferRequest(UUID from, UUID to, UUID item)
  556. {
  557. return false;
  558. }
  559. public override AvatarAppearance GetUserAppearance(UUID user)
  560. {
  561. try
  562. {
  563. Dictionary<string, object> param = new Dictionary<string, object>();
  564. param["?owner"] = user.ToString();
  565. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  566. {
  567. dbcon.Open();
  568. using (IDbCommand result = m_database.Query(dbcon, "SELECT * FROM " + m_appearanceTableName + " WHERE owner = ?owner", param))
  569. {
  570. using (IDataReader reader = result.ExecuteReader())
  571. {
  572. AvatarAppearance appearance = m_database.readAppearanceRow(reader);
  573. if (appearance == null)
  574. {
  575. m_log.WarnFormat("[USER DB] No appearance found for user {0}", user.ToString());
  576. return null;
  577. }
  578. else
  579. {
  580. appearance.SetAttachments(GetUserAttachments(user));
  581. return appearance;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. catch (Exception e)
  588. {
  589. m_log.Error(e.Message, e);
  590. return null;
  591. }
  592. }
  593. /// <summary>
  594. /// Updates an avatar appearence
  595. /// </summary>
  596. /// <param name="user">The user UUID</param>
  597. /// <param name="appearance">The avatar appearance</param>
  598. // override
  599. public override void UpdateUserAppearance(UUID user, AvatarAppearance appearance)
  600. {
  601. try
  602. {
  603. appearance.Owner = user;
  604. m_database.insertAppearanceRow(appearance);
  605. UpdateUserAttachments(user, appearance.GetAttachments());
  606. }
  607. catch (Exception e)
  608. {
  609. m_log.Error(e.Message, e);
  610. }
  611. }
  612. /// <summary>
  613. /// Database provider name
  614. /// </summary>
  615. /// <returns>Provider name</returns>
  616. public override string Name
  617. {
  618. get { return "MySQL Userdata Interface"; }
  619. }
  620. /// <summary>
  621. /// Database provider version
  622. /// </summary>
  623. /// <returns>provider version</returns>
  624. public override string Version
  625. {
  626. get { return "0.1"; }
  627. }
  628. public Hashtable GetUserAttachments(UUID agentID)
  629. {
  630. Dictionary<string, object> param = new Dictionary<string, object>();
  631. param["?uuid"] = agentID.ToString();
  632. try
  633. {
  634. using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
  635. {
  636. dbcon.Open();
  637. using (IDbCommand result = m_database.Query(dbcon,
  638. "SELECT attachpoint, item, asset from " + m_attachmentsTableName + " WHERE UUID = ?uuid", param))
  639. {
  640. using (IDataReader reader = result.ExecuteReader())
  641. {
  642. Hashtable ret = m_database.readAttachments(reader);
  643. return ret;
  644. }
  645. }
  646. }
  647. }
  648. catch (Exception e)
  649. {
  650. m_log.Error(e.Message, e);
  651. return null;
  652. }
  653. }
  654. public void UpdateUserAttachments(UUID agentID, Hashtable data)
  655. {
  656. m_database.writeAttachments(agentID, data);
  657. }
  658. public override void ResetAttachments(UUID userID)
  659. {
  660. Dictionary<string, string> param = new Dictionary<string, string>();
  661. param["?uuid"] = userID.ToString();
  662. m_database.ExecuteParameterizedSql(
  663. "UPDATE " + m_attachmentsTableName +
  664. " SET asset = '00000000-0000-0000-0000-000000000000' WHERE UUID = ?uuid",
  665. param);
  666. }
  667. public override void LogoutUsers(UUID regionID)
  668. {
  669. Dictionary<string, string> param = new Dictionary<string, string>();
  670. param["?regionID"] = regionID.ToString();
  671. try
  672. {
  673. m_database.ExecuteParameterizedSql(
  674. "update " + m_agentsTableName + " SET agentOnline = 0 " +
  675. "where currentRegion = ?regionID",
  676. param);
  677. }
  678. catch (Exception e)
  679. {
  680. m_log.Error(e.Message, e);
  681. return;
  682. }
  683. }
  684. }
  685. }