MySQLUserData.cs 33 KB

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