MSSQLUserData.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Data;
  30. using System.Data.SqlClient;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using OpenSim.Framework;
  35. namespace OpenSim.Data.MSSQL
  36. {
  37. /// <summary>
  38. /// A database interface class to a user profile storage system
  39. /// </summary>
  40. public class MSSQLUserData : UserDataBase
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// Database manager for MySQL
  45. /// </summary>
  46. public MSSQLManager database;
  47. private string m_agentsTableName;
  48. private string m_usersTableName;
  49. private string m_userFriendsTableName;
  50. /// <summary>
  51. /// Loads and initialises the MSSQL storage plugin
  52. /// </summary>
  53. /// <param name="connect">TODO: do something with the connect string instead of ignoring it.</param>
  54. /// <remarks>use mssql_connection.ini</remarks>
  55. override public void Initialise(string connect)
  56. {
  57. // TODO: do something with the connect string instead of
  58. // ignoring it.
  59. IniFile iniFile = new IniFile("mssql_connection.ini");
  60. string settingDataSource = iniFile.ParseFileReadValue("data_source");
  61. string settingInitialCatalog = iniFile.ParseFileReadValue("initial_catalog");
  62. string settingPersistSecurityInfo = iniFile.ParseFileReadValue("persist_security_info");
  63. string settingUserId = iniFile.ParseFileReadValue("user_id");
  64. string settingPassword = iniFile.ParseFileReadValue("password");
  65. m_usersTableName = iniFile.ParseFileReadValue("userstablename");
  66. if (m_usersTableName == null)
  67. {
  68. m_usersTableName = "users";
  69. }
  70. m_userFriendsTableName = iniFile.ParseFileReadValue("userfriendstablename");
  71. if (m_userFriendsTableName == null)
  72. {
  73. m_userFriendsTableName = "userfriends";
  74. }
  75. m_agentsTableName = iniFile.ParseFileReadValue("agentstablename");
  76. if (m_agentsTableName == null)
  77. {
  78. m_agentsTableName = "agents";
  79. }
  80. database =
  81. new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId,
  82. settingPassword);
  83. TestTables();
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <returns></returns>
  89. private bool TestTables()
  90. {
  91. IDbCommand cmd;
  92. cmd = database.Query("select top 1 * from " + m_usersTableName, new Dictionary<string, string>());
  93. try
  94. {
  95. cmd.ExecuteNonQuery();
  96. }
  97. catch
  98. {
  99. database.ExecuteResourceSql("Mssql-users.sql");
  100. }
  101. cmd = database.Query("select top 1 * from " + m_agentsTableName, new Dictionary<string, string>());
  102. try
  103. {
  104. cmd.ExecuteNonQuery();
  105. }
  106. catch
  107. {
  108. database.ExecuteResourceSql("Mssql-agents.sql");
  109. }
  110. cmd = database.Query("select top 1 * from " + m_userFriendsTableName, new Dictionary<string, string>());
  111. try
  112. {
  113. cmd.ExecuteNonQuery();
  114. }
  115. catch
  116. {
  117. database.ExecuteResourceSql("CreateUserFriendsTable.sql");
  118. }
  119. return true;
  120. }
  121. /// <summary>
  122. /// Searches the database for a specified user profile by name components
  123. /// </summary>
  124. /// <param name="user">The first part of the account name</param>
  125. /// <param name="last">The second part of the account name</param>
  126. /// <returns>A user profile</returns>
  127. override public UserProfileData GetUserByName(string user, string last)
  128. {
  129. try
  130. {
  131. lock (database)
  132. {
  133. Dictionary<string, string> param = new Dictionary<string, string>();
  134. param["first"] = user;
  135. param["second"] = last;
  136. IDbCommand result =
  137. database.Query("SELECT * FROM " + m_usersTableName + " WHERE username = @first AND lastname = @second", param);
  138. IDataReader reader = result.ExecuteReader();
  139. UserProfileData row = database.readUserRow(reader);
  140. reader.Close();
  141. result.Dispose();
  142. return row;
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. database.Reconnect();
  148. m_log.Error(e.ToString());
  149. return null;
  150. }
  151. }
  152. #region User Friends List Data
  153. /// <summary>
  154. /// Add a new friend in the friendlist
  155. /// </summary>
  156. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  157. /// <param name="friend">Friend's UUID</param>
  158. /// <param name="perms">Permission flag</param>
  159. override public void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms)
  160. {
  161. int dtvalue = Util.UnixTimeSinceEpoch();
  162. Dictionary<string, string> param = new Dictionary<string, string>();
  163. param["@ownerID"] = friendlistowner.UUID.ToString();
  164. param["@friendID"] = friend.UUID.ToString();
  165. param["@friendPerms"] = perms.ToString();
  166. param["@datetimestamp"] = dtvalue.ToString();
  167. try
  168. {
  169. lock (database)
  170. {
  171. IDbCommand adder =
  172. database.Query(
  173. "INSERT INTO " + m_userFriendsTableName + " " +
  174. "(ownerID,friendID,friendPerms,datetimestamp) " +
  175. "VALUES " +
  176. "(@ownerID,@friendID,@friendPerms,@datetimestamp)",
  177. param);
  178. adder.ExecuteNonQuery();
  179. adder =
  180. database.Query(
  181. "INSERT INTO " + m_userFriendsTableName + " " +
  182. "(ownerID,friendID,friendPerms,datetimestamp) " +
  183. "VALUES " +
  184. "(@friendID,@ownerID,@friendPerms,@datetimestamp)",
  185. param);
  186. adder.ExecuteNonQuery();
  187. }
  188. }
  189. catch (Exception e)
  190. {
  191. database.Reconnect();
  192. m_log.Error(e.ToString());
  193. return;
  194. }
  195. }
  196. /// <summary>
  197. /// Remove an friend from the friendlist
  198. /// </summary>
  199. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  200. /// <param name="friend">UUID of the not-so-friendly user to remove from the list</param>
  201. override public void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend)
  202. {
  203. Dictionary<string, string> param = new Dictionary<string, string>();
  204. param["@ownerID"] = friendlistowner.UUID.ToString();
  205. param["@friendID"] = friend.UUID.ToString();
  206. try
  207. {
  208. lock (database)
  209. {
  210. IDbCommand updater =
  211. database.Query(
  212. "delete from " + m_userFriendsTableName + " where ownerID = @ownerID and friendID = @friendID",
  213. param);
  214. updater.ExecuteNonQuery();
  215. updater =
  216. database.Query(
  217. "delete from " + m_userFriendsTableName + " where ownerID = @friendID and friendID = @ownerID",
  218. param);
  219. updater.ExecuteNonQuery();
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. database.Reconnect();
  225. m_log.Error(e.ToString());
  226. return;
  227. }
  228. }
  229. /// <summary>
  230. /// Update friendlist permission flag for a friend
  231. /// </summary>
  232. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  233. /// <param name="friend">UUID of the friend</param>
  234. /// <param name="perms">new permission flag</param>
  235. override public void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms)
  236. {
  237. Dictionary<string, string> param = new Dictionary<string, string>();
  238. param["@ownerID"] = friendlistowner.UUID.ToString();
  239. param["@friendID"] = friend.UUID.ToString();
  240. param["@friendPerms"] = perms.ToString();
  241. try
  242. {
  243. lock (database)
  244. {
  245. IDbCommand updater =
  246. database.Query(
  247. "update " + m_userFriendsTableName +
  248. " SET friendPerms = @friendPerms " +
  249. "where ownerID = @ownerID and friendID = @friendID",
  250. param);
  251. updater.ExecuteNonQuery();
  252. }
  253. }
  254. catch (Exception e)
  255. {
  256. database.Reconnect();
  257. m_log.Error(e.ToString());
  258. return;
  259. }
  260. }
  261. /// <summary>
  262. /// Get (fetch?) the user's friendlist
  263. /// </summary>
  264. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  265. /// <returns>Friendlist list</returns>
  266. override public List<FriendListItem> GetUserFriendList(LLUUID friendlistowner)
  267. {
  268. List<FriendListItem> Lfli = new List<FriendListItem>();
  269. Dictionary<string, string> param = new Dictionary<string, string>();
  270. param["@ownerID"] = friendlistowner.UUID.ToString();
  271. try
  272. {
  273. lock (database)
  274. {
  275. //Left Join userfriends to itself
  276. IDbCommand result =
  277. database.Query(
  278. "select a.ownerID,a.friendID,a.friendPerms,b.friendPerms as ownerperms from " + m_userFriendsTableName + " as a, " + m_userFriendsTableName + " as b" +
  279. " where a.ownerID = @ownerID and b.ownerID = a.friendID and b.friendID = a.ownerID",
  280. param);
  281. IDataReader reader = result.ExecuteReader();
  282. while (reader.Read())
  283. {
  284. FriendListItem fli = new FriendListItem();
  285. fli.FriendListOwner = new LLUUID((string)reader["ownerID"]);
  286. fli.Friend = new LLUUID((string)reader["friendID"]);
  287. fli.FriendPerms = (uint)Convert.ToInt32(reader["friendPerms"]);
  288. // This is not a real column in the database table, it's a joined column from the opposite record
  289. fli.FriendListOwnerPerms = (uint)Convert.ToInt32(reader["ownerperms"]);
  290. Lfli.Add(fli);
  291. }
  292. reader.Close();
  293. result.Dispose();
  294. }
  295. }
  296. catch (Exception e)
  297. {
  298. database.Reconnect();
  299. m_log.Error(e.ToString());
  300. return Lfli;
  301. }
  302. return Lfli;
  303. }
  304. #endregion
  305. /// <summary>
  306. /// STUB ! Update current region
  307. /// </summary>
  308. /// <param name="avatarid">avatar uuid</param>
  309. /// <param name="regionuuid">region uuid</param>
  310. /// <param name="regionhandle">region handle</param>
  311. override public void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid, ulong regionhandle)
  312. {
  313. //m_log.Info("[USER]: Stub UpdateUserCUrrentRegion called");
  314. }
  315. /// <summary>
  316. ///
  317. /// </summary>
  318. /// <param name="queryID"></param>
  319. /// <param name="query"></param>
  320. /// <returns></returns>
  321. override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  322. {
  323. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  324. string[] querysplit;
  325. querysplit = query.Split(' ');
  326. if (querysplit.Length == 2)
  327. {
  328. try
  329. {
  330. lock (database)
  331. {
  332. Dictionary<string, string> param = new Dictionary<string, string>();
  333. param["first"] = querysplit[0];
  334. param["second"] = querysplit[1];
  335. IDbCommand result =
  336. database.Query(
  337. "SELECT UUID,username,lastname FROM " + m_usersTableName + " WHERE username = @first AND lastname = @second",
  338. param);
  339. IDataReader reader = result.ExecuteReader();
  340. while (reader.Read())
  341. {
  342. AvatarPickerAvatar user = new AvatarPickerAvatar();
  343. user.AvatarID = new LLUUID((string)reader["UUID"]);
  344. user.firstName = (string)reader["username"];
  345. user.lastName = (string)reader["lastname"];
  346. returnlist.Add(user);
  347. }
  348. reader.Close();
  349. result.Dispose();
  350. }
  351. }
  352. catch (Exception e)
  353. {
  354. database.Reconnect();
  355. m_log.Error(e.ToString());
  356. return returnlist;
  357. }
  358. }
  359. else if (querysplit.Length == 1)
  360. {
  361. try
  362. {
  363. lock (database)
  364. {
  365. Dictionary<string, string> param = new Dictionary<string, string>();
  366. param["first"] = querysplit[0];
  367. IDbCommand result =
  368. database.Query(
  369. "SELECT UUID,username,lastname FROM " + m_usersTableName + " WHERE username = @first OR lastname = @first",
  370. param);
  371. IDataReader reader = result.ExecuteReader();
  372. while (reader.Read())
  373. {
  374. AvatarPickerAvatar user = new AvatarPickerAvatar();
  375. user.AvatarID = new LLUUID((string)reader["UUID"]);
  376. user.firstName = (string)reader["username"];
  377. user.lastName = (string)reader["lastname"];
  378. returnlist.Add(user);
  379. }
  380. reader.Close();
  381. result.Dispose();
  382. }
  383. }
  384. catch (Exception e)
  385. {
  386. database.Reconnect();
  387. m_log.Error(e.ToString());
  388. return returnlist;
  389. }
  390. }
  391. return returnlist;
  392. }
  393. /// <summary>
  394. /// See IUserData
  395. /// </summary>
  396. /// <param name="uuid"></param>
  397. /// <returns></returns>
  398. override public UserProfileData GetUserByUUID(LLUUID uuid)
  399. {
  400. try
  401. {
  402. lock (database)
  403. {
  404. Dictionary<string, string> param = new Dictionary<string, string>();
  405. param["uuid"] = uuid.ToString();
  406. IDbCommand result = database.Query("SELECT * FROM " + m_usersTableName + " WHERE UUID = @uuid", param);
  407. IDataReader reader = result.ExecuteReader();
  408. UserProfileData row = database.readUserRow(reader);
  409. reader.Close();
  410. result.Dispose();
  411. return row;
  412. }
  413. }
  414. catch (Exception e)
  415. {
  416. database.Reconnect();
  417. m_log.Error(e.ToString());
  418. return null;
  419. }
  420. }
  421. /// <summary>
  422. /// Returns a user session searching by name
  423. /// </summary>
  424. /// <param name="name">The account name</param>
  425. /// <returns>The users session</returns>
  426. override public UserAgentData GetAgentByName(string name)
  427. {
  428. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  429. }
  430. /// <summary>
  431. /// Returns a user session by account name
  432. /// </summary>
  433. /// <param name="user">First part of the users account name</param>
  434. /// <param name="last">Second part of the users account name</param>
  435. /// <returns>The users session</returns>
  436. override public UserAgentData GetAgentByName(string user, string last)
  437. {
  438. UserProfileData profile = GetUserByName(user, last);
  439. return GetAgentByUUID(profile.ID);
  440. }
  441. /// <summary>
  442. /// Returns an agent session by account UUID
  443. /// </summary>
  444. /// <param name="uuid">The accounts UUID</param>
  445. /// <returns>The users session</returns>
  446. override public UserAgentData GetAgentByUUID(LLUUID uuid)
  447. {
  448. try
  449. {
  450. lock (database)
  451. {
  452. Dictionary<string, string> param = new Dictionary<string, string>();
  453. param["uuid"] = uuid.ToString();
  454. IDbCommand result = database.Query("SELECT * FROM " + m_agentsTableName + " WHERE UUID = @uuid", param);
  455. IDataReader reader = result.ExecuteReader();
  456. UserAgentData row = database.readAgentRow(reader);
  457. reader.Close();
  458. result.Dispose();
  459. return row;
  460. }
  461. }
  462. catch (Exception e)
  463. {
  464. database.Reconnect();
  465. m_log.Error(e.ToString());
  466. return null;
  467. }
  468. }
  469. /// <summary>
  470. /// Store a weblogin key
  471. /// </summary>
  472. /// <param name="AgentID">The agent UUID</param>
  473. /// <param name="WebLoginKey">the WebLogin Key</param>
  474. /// <remarks>unused ?</remarks>
  475. override public void StoreWebLoginKey(LLUUID AgentID, LLUUID WebLoginKey)
  476. {
  477. UserProfileData user = GetUserByUUID(AgentID);
  478. user.WebLoginKey = WebLoginKey;
  479. UpdateUserProfile(user);
  480. }
  481. /// <summary>
  482. /// Creates a new users profile
  483. /// </summary>
  484. /// <param name="user">The user profile to create</param>
  485. override public void AddNewUserProfile(UserProfileData user)
  486. {
  487. try
  488. {
  489. lock (database)
  490. {
  491. InsertUserRow(user.ID, user.FirstName, user.SurName, user.PasswordHash, user.PasswordSalt,
  492. user.HomeRegion, user.HomeLocation.X, user.HomeLocation.Y,
  493. user.HomeLocation.Z,
  494. user.HomeLookAt.X, user.HomeLookAt.Y, user.HomeLookAt.Z, user.Created,
  495. user.LastLogin, user.UserInventoryURI, user.UserAssetURI,
  496. user.CanDoMask, user.WantDoMask,
  497. user.AboutText, user.FirstLifeAboutText, user.Image,
  498. user.FirstLifeImage, user.WebLoginKey);
  499. }
  500. }
  501. catch (Exception e)
  502. {
  503. database.Reconnect();
  504. m_log.Error(e.ToString());
  505. }
  506. }
  507. /// <summary>
  508. /// Creates a new user and inserts it into the database
  509. /// </summary>
  510. /// <param name="uuid">User ID</param>
  511. /// <param name="username">First part of the login</param>
  512. /// <param name="lastname">Second part of the login</param>
  513. /// <param name="passwordHash">A salted hash of the users password</param>
  514. /// <param name="passwordSalt">The salt used for the password hash</param>
  515. /// <param name="homeRegion">A regionHandle of the users home region</param>
  516. /// <param name="homeLocX">Home region position vector</param>
  517. /// <param name="homeLocY">Home region position vector</param>
  518. /// <param name="homeLocZ">Home region position vector</param>
  519. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  520. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  521. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  522. /// <param name="created">Account created (unix timestamp)</param>
  523. /// <param name="lastlogin">Last login (unix timestamp)</param>
  524. /// <param name="inventoryURI">Users inventory URI</param>
  525. /// <param name="assetURI">Users asset URI</param>
  526. /// <param name="canDoMask">I can do mask</param>
  527. /// <param name="wantDoMask">I want to do mask</param>
  528. /// <param name="aboutText">Profile text</param>
  529. /// <param name="firstText">Firstlife text</param>
  530. /// <param name="profileImage">UUID for profile image</param>
  531. /// <param name="firstImage">UUID for firstlife image</param>
  532. /// <returns>Success?</returns>
  533. private bool InsertUserRow(LLUUID uuid, string username, string lastname, string passwordHash,
  534. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  535. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  536. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  537. string aboutText, string firstText,
  538. LLUUID profileImage, LLUUID firstImage, LLUUID webLoginKey)
  539. {
  540. string sql = "INSERT INTO "+m_usersTableName;
  541. sql += " ([UUID], [username], [lastname], [passwordHash], [passwordSalt], [homeRegion], ";
  542. sql +=
  543. "[homeLocationX], [homeLocationY], [homeLocationZ], [homeLookAtX], [homeLookAtY], [homeLookAtZ], [created], ";
  544. sql +=
  545. "[lastLogin], [userInventoryURI], [userAssetURI], [profileCanDoMask], [profileWantDoMask], [profileAboutText], ";
  546. sql += "[profileFirstText], [profileImage], [profileFirstImage], [webLoginKey]) VALUES ";
  547. sql += "(@UUID, @username, @lastname, @passwordHash, @passwordSalt, @homeRegion, ";
  548. sql +=
  549. "@homeLocationX, @homeLocationY, @homeLocationZ, @homeLookAtX, @homeLookAtY, @homeLookAtZ, @created, ";
  550. sql +=
  551. "@lastLogin, @userInventoryURI, @userAssetURI, @profileCanDoMask, @profileWantDoMask, @profileAboutText, ";
  552. sql += "@profileFirstText, @profileImage, @profileFirstImage, @webLoginKey);";
  553. Dictionary<string, string> parameters = new Dictionary<string, string>();
  554. parameters["UUID"] = uuid.ToString();
  555. parameters["username"] = username.ToString();
  556. parameters["lastname"] = lastname.ToString();
  557. parameters["passwordHash"] = passwordHash.ToString();
  558. parameters["passwordSalt"] = passwordSalt.ToString();
  559. parameters["homeRegion"] = homeRegion.ToString();
  560. parameters["homeLocationX"] = homeLocX.ToString();
  561. parameters["homeLocationY"] = homeLocY.ToString();
  562. parameters["homeLocationZ"] = homeLocZ.ToString();
  563. parameters["homeLookAtX"] = homeLookAtX.ToString();
  564. parameters["homeLookAtY"] = homeLookAtY.ToString();
  565. parameters["homeLookAtZ"] = homeLookAtZ.ToString();
  566. parameters["created"] = created.ToString();
  567. parameters["lastLogin"] = lastlogin.ToString();
  568. parameters["userInventoryURI"] = String.Empty;
  569. parameters["userAssetURI"] = String.Empty;
  570. parameters["profileCanDoMask"] = "0";
  571. parameters["profileWantDoMask"] = "0";
  572. parameters["profileAboutText"] = aboutText;
  573. parameters["profileFirstText"] = firstText;
  574. parameters["profileImage"] = profileImage.ToString();
  575. parameters["profileFirstImage"] = firstImage.ToString();
  576. parameters["webLoginKey"] = LLUUID.Random().ToString();
  577. bool returnval = false;
  578. try
  579. {
  580. IDbCommand result = database.Query(sql, parameters);
  581. if (result.ExecuteNonQuery() == 1)
  582. returnval = true;
  583. result.Dispose();
  584. }
  585. catch (Exception e)
  586. {
  587. m_log.Error(e.ToString());
  588. return false;
  589. }
  590. return returnval;
  591. }
  592. /// <summary>
  593. /// Creates a new agent
  594. /// </summary>
  595. /// <param name="agent">The agent to create</param>
  596. override public void AddNewUserAgent(UserAgentData agent)
  597. {
  598. // Do nothing.
  599. }
  600. /// <summary>
  601. /// update a user profile
  602. /// </summary>
  603. /// <param name="user">the profile to update</param>
  604. /// <returns></returns>
  605. override public bool UpdateUserProfile(UserProfileData user)
  606. {
  607. SqlCommand command = new SqlCommand("UPDATE " + m_usersTableName + " set UUID = @uuid, " +
  608. "username = @username, " +
  609. "lastname = @lastname," +
  610. "passwordHash = @passwordHash," +
  611. "passwordSalt = @passwordSalt," +
  612. "homeRegion = @homeRegion," +
  613. "homeLocationX = @homeLocationX," +
  614. "homeLocationY = @homeLocationY," +
  615. "homeLocationZ = @homeLocationZ," +
  616. "homeLookAtX = @homeLookAtX," +
  617. "homeLookAtY = @homeLookAtY," +
  618. "homeLookAtZ = @homeLookAtZ," +
  619. "created = @created," +
  620. "lastLogin = @lastLogin," +
  621. "userInventoryURI = @userInventoryURI," +
  622. "userAssetURI = @userAssetURI," +
  623. "profileCanDoMask = @profileCanDoMask," +
  624. "profileWantDoMask = @profileWantDoMask," +
  625. "profileAboutText = @profileAboutText," +
  626. "profileFirstText = @profileFirstText," +
  627. "profileImage = @profileImage," +
  628. "profileFirstImage = @profileFirstImage, " +
  629. "webLoginKey = @webLoginKey where " +
  630. "UUID = @keyUUUID;", database.getConnection());
  631. SqlParameter param1 = new SqlParameter("@uuid", user.ID.ToString());
  632. SqlParameter param2 = new SqlParameter("@username", user.FirstName);
  633. SqlParameter param3 = new SqlParameter("@lastname", user.SurName);
  634. SqlParameter param4 = new SqlParameter("@passwordHash", user.PasswordHash);
  635. SqlParameter param5 = new SqlParameter("@passwordSalt", user.PasswordSalt);
  636. SqlParameter param6 = new SqlParameter("@homeRegion", Convert.ToInt64(user.HomeRegion));
  637. SqlParameter param7 = new SqlParameter("@homeLocationX", user.HomeLocation.X);
  638. SqlParameter param8 = new SqlParameter("@homeLocationY", user.HomeLocation.Y);
  639. SqlParameter param9 = new SqlParameter("@homeLocationZ", user.HomeLocation.Y);
  640. SqlParameter param10 = new SqlParameter("@homeLookAtX", user.HomeLookAt.X);
  641. SqlParameter param11 = new SqlParameter("@homeLookAtY", user.HomeLookAt.Y);
  642. SqlParameter param12 = new SqlParameter("@homeLookAtZ", user.HomeLookAt.Z);
  643. SqlParameter param13 = new SqlParameter("@created", Convert.ToInt32(user.Created));
  644. SqlParameter param14 = new SqlParameter("@lastLogin", Convert.ToInt32(user.LastLogin));
  645. SqlParameter param15 = new SqlParameter("@userInventoryURI", user.UserInventoryURI);
  646. SqlParameter param16 = new SqlParameter("@userAssetURI", user.UserAssetURI);
  647. SqlParameter param17 = new SqlParameter("@profileCanDoMask", Convert.ToInt32(user.CanDoMask));
  648. SqlParameter param18 = new SqlParameter("@profileWantDoMask", Convert.ToInt32(user.WantDoMask));
  649. SqlParameter param19 = new SqlParameter("@profileAboutText", user.AboutText);
  650. SqlParameter param20 = new SqlParameter("@profileFirstText", user.FirstLifeAboutText);
  651. SqlParameter param21 = new SqlParameter("@profileImage", user.Image.ToString());
  652. SqlParameter param22 = new SqlParameter("@profileFirstImage", user.FirstLifeImage.ToString());
  653. SqlParameter param23 = new SqlParameter("@keyUUUID", user.ID.ToString());
  654. SqlParameter param24 = new SqlParameter("@webLoginKey", user.WebLoginKey.UUID.ToString());
  655. command.Parameters.Add(param1);
  656. command.Parameters.Add(param2);
  657. command.Parameters.Add(param3);
  658. command.Parameters.Add(param4);
  659. command.Parameters.Add(param5);
  660. command.Parameters.Add(param6);
  661. command.Parameters.Add(param7);
  662. command.Parameters.Add(param8);
  663. command.Parameters.Add(param9);
  664. command.Parameters.Add(param10);
  665. command.Parameters.Add(param11);
  666. command.Parameters.Add(param12);
  667. command.Parameters.Add(param13);
  668. command.Parameters.Add(param14);
  669. command.Parameters.Add(param15);
  670. command.Parameters.Add(param16);
  671. command.Parameters.Add(param17);
  672. command.Parameters.Add(param18);
  673. command.Parameters.Add(param19);
  674. command.Parameters.Add(param20);
  675. command.Parameters.Add(param21);
  676. command.Parameters.Add(param22);
  677. command.Parameters.Add(param23);
  678. command.Parameters.Add(param24);
  679. try
  680. {
  681. int affected = command.ExecuteNonQuery();
  682. if (affected != 0)
  683. {
  684. return true;
  685. }
  686. else
  687. {
  688. return false;
  689. }
  690. }
  691. catch (Exception e)
  692. {
  693. m_log.Error(e.ToString());
  694. }
  695. return false;
  696. }
  697. /// <summary>
  698. /// Performs a money transfer request between two accounts
  699. /// </summary>
  700. /// <param name="from">The senders account ID</param>
  701. /// <param name="to">The receivers account ID</param>
  702. /// <param name="amount">The amount to transfer</param>
  703. /// <returns>false</returns>
  704. override public bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount)
  705. {
  706. return false;
  707. }
  708. /// <summary>
  709. /// Performs an inventory transfer request between two accounts
  710. /// </summary>
  711. /// <remarks>TODO: Move to inventory server</remarks>
  712. /// <param name="from">The senders account ID</param>
  713. /// <param name="to">The receivers account ID</param>
  714. /// <param name="item">The item to transfer</param>
  715. /// <returns>false</returns>
  716. override public bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID item)
  717. {
  718. return false;
  719. }
  720. /// Appearance
  721. /// TODO: stubs for now to get us to a compiling state gently
  722. override public AvatarAppearance GetUserAppearance(LLUUID user)
  723. {
  724. // return new AvatarAppearance();
  725. try
  726. {
  727. lock (database)
  728. {
  729. Dictionary<string, string> param = new Dictionary<string, string>();
  730. param["@UUID"] = user.ToString();
  731. IDbCommand result =
  732. database.Query("SELECT * FROM avatarappearance WHERE owner = @UUID", param);
  733. IDataReader reader = result.ExecuteReader();
  734. AvatarAppearance item = null;
  735. if (reader.Read())
  736. item = readUserAppearance(reader);
  737. reader.Close();
  738. result.Dispose();
  739. return item;
  740. }
  741. }
  742. catch (Exception e)
  743. {
  744. database.Reconnect();
  745. m_log.Error(e.ToString());
  746. }
  747. return null;
  748. }
  749. /// <summary>
  750. /// Reads a one item from an SQL result
  751. /// </summary>
  752. /// <param name="reader">The SQL Result</param>
  753. /// <returns>the item read</returns>
  754. private static AvatarAppearance readUserAppearance(IDataReader reader)
  755. {
  756. try
  757. {
  758. AvatarAppearance appearance = new AvatarAppearance();
  759. appearance.Owner = new LLUUID((string)reader["owner"]);
  760. appearance.Serial = Convert.ToInt32(reader["serial"]);
  761. appearance.VisualParams = (byte[])reader["visual_params"];
  762. appearance.Texture = new LLObject.TextureEntry((byte[])reader["texture"], 0, ((byte[])reader["texture"]).Length);
  763. appearance.AvatarHeight = (float)Convert.ToDouble(reader["avatar_height"]);
  764. appearance.BodyItem = new LLUUID((string)reader["body_item"]);
  765. appearance.BodyAsset = new LLUUID((string)reader["body_asset"]);
  766. appearance.SkinItem = new LLUUID((string)reader["skin_item"]);
  767. appearance.SkinAsset = new LLUUID((string)reader["skin_asset"]);
  768. appearance.HairItem = new LLUUID((string)reader["hair_item"]);
  769. appearance.HairAsset = new LLUUID((string)reader["hair_asset"]);
  770. appearance.EyesItem = new LLUUID((string)reader["eyes_item"]);
  771. appearance.EyesAsset = new LLUUID((string)reader["eyes_asset"]);
  772. appearance.ShirtItem = new LLUUID((string)reader["shirt_item"]);
  773. appearance.ShirtAsset = new LLUUID((string)reader["shirt_asset"]);
  774. appearance.PantsItem = new LLUUID((string)reader["pants_item"]);
  775. appearance.PantsAsset = new LLUUID((string)reader["pants_asset"]);
  776. appearance.ShoesItem = new LLUUID((string)reader["shoes_item"]);
  777. appearance.ShoesAsset = new LLUUID((string)reader["shoes_asset"]);
  778. appearance.SocksItem = new LLUUID((string)reader["socks_item"]);
  779. appearance.SocksAsset = new LLUUID((string)reader["socks_asset"]);
  780. appearance.JacketItem = new LLUUID((string)reader["jacket_item"]);
  781. appearance.JacketAsset = new LLUUID((string)reader["jacket_asset"]);
  782. appearance.GlovesItem = new LLUUID((string)reader["gloves_item"]);
  783. appearance.GlovesAsset = new LLUUID((string)reader["gloves_asset"]);
  784. appearance.UnderShirtItem = new LLUUID((string)reader["undershirt_item"]);
  785. appearance.UnderShirtAsset = new LLUUID((string)reader["undershirt_asset"]);
  786. appearance.UnderPantsItem = new LLUUID((string)reader["underpants_item"]);
  787. appearance.UnderPantsAsset = new LLUUID((string)reader["underpants_asset"]);
  788. appearance.SkirtItem = new LLUUID((string)reader["skirt_item"]);
  789. appearance.SkirtAsset = new LLUUID((string)reader["skirt_asset"]);
  790. return appearance;
  791. }
  792. catch (SqlException e)
  793. {
  794. m_log.Error(e.ToString());
  795. }
  796. return null;
  797. }
  798. /// <summary>
  799. /// Update a user appearence into database
  800. /// </summary>
  801. /// <param name="user">the used UUID</param>
  802. /// <param name="appearance">the appearence</param>
  803. override public void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance)
  804. {
  805. string sql = String.Empty;
  806. sql += "DELETE FROM avatarappearance WHERE owner=@owner ";
  807. sql += "INSERT INTO avatarappearance ";
  808. sql += "(owner, serial, visual_params, texture, avatar_height, ";
  809. sql += "body_item, body_asset, skin_item, skin_asset, hair_item, hair_asset, eyes_item, eyes_asset, ";
  810. sql += "shirt_item, shirt_asset, pants_item, pants_asset, shoes_item, shoes_asset, socks_item, socks_asset, ";
  811. sql += "jacket_item, jacket_asset, gloves_item, gloves_asset, undershirt_item, undershirt_asset, underpants_item, underpants_asset, ";
  812. sql += "skirt_item, skirt_asset) values (";
  813. sql += "@owner, @serial, @visual_params, @texture, @avatar_height, ";
  814. sql += "@body_item, @body_asset, @skin_item, @skin_asset, @hair_item, @hair_asset, @eyes_item, @eyes_asset, ";
  815. sql += "@shirt_item, @shirt_asset, @pants_item, @pants_asset, @shoes_item, @shoes_asset, @socks_item, @socks_asset, ";
  816. sql += "@jacket_item, @jacket_asset, @gloves_item, @gloves_asset, @undershirt_item, @undershirt_asset, @underpants_item, @underpants_asset, ";
  817. sql += "@skirt_item, @skirt_asset)";
  818. SqlCommand cmd = new SqlCommand(sql, database.getConnection());
  819. cmd.Parameters.AddWithValue("@owner", appearance.Owner.ToString());
  820. cmd.Parameters.AddWithValue("@serial", appearance.Serial);
  821. cmd.Parameters.AddWithValue("@visual_params", appearance.VisualParams);
  822. cmd.Parameters.AddWithValue("@texture", appearance.Texture.ToBytes());
  823. cmd.Parameters.AddWithValue("@avatar_height", appearance.AvatarHeight);
  824. cmd.Parameters.AddWithValue("@body_item", appearance.BodyItem.ToString());
  825. cmd.Parameters.AddWithValue("@body_asset", appearance.BodyAsset.ToString());
  826. cmd.Parameters.AddWithValue("@skin_item", appearance.SkinItem.ToString());
  827. cmd.Parameters.AddWithValue("@skin_asset", appearance.SkinAsset.ToString());
  828. cmd.Parameters.AddWithValue("@hair_item", appearance.HairItem.ToString());
  829. cmd.Parameters.AddWithValue("@hair_asset", appearance.HairAsset.ToString());
  830. cmd.Parameters.AddWithValue("@eyes_item", appearance.EyesItem.ToString());
  831. cmd.Parameters.AddWithValue("@eyes_asset", appearance.EyesAsset.ToString());
  832. cmd.Parameters.AddWithValue("@shirt_item", appearance.ShirtItem.ToString());
  833. cmd.Parameters.AddWithValue("@shirt_asset", appearance.ShirtAsset.ToString());
  834. cmd.Parameters.AddWithValue("@pants_item", appearance.PantsItem.ToString());
  835. cmd.Parameters.AddWithValue("@pants_asset", appearance.PantsAsset.ToString());
  836. cmd.Parameters.AddWithValue("@shoes_item", appearance.ShoesItem.ToString());
  837. cmd.Parameters.AddWithValue("@shoes_asset", appearance.ShoesAsset.ToString());
  838. cmd.Parameters.AddWithValue("@socks_item", appearance.SocksItem.ToString());
  839. cmd.Parameters.AddWithValue("@socks_asset", appearance.SocksAsset.ToString());
  840. cmd.Parameters.AddWithValue("@jacket_item", appearance.JacketItem.ToString());
  841. cmd.Parameters.AddWithValue("@jacket_asset", appearance.JacketAsset.ToString());
  842. cmd.Parameters.AddWithValue("@gloves_item", appearance.GlovesItem.ToString());
  843. cmd.Parameters.AddWithValue("@gloves_asset", appearance.GlovesAsset.ToString());
  844. cmd.Parameters.AddWithValue("@undershirt_item", appearance.UnderShirtItem.ToString());
  845. cmd.Parameters.AddWithValue("@undershirt_asset", appearance.UnderShirtAsset.ToString());
  846. cmd.Parameters.AddWithValue("@underpants_item", appearance.UnderPantsItem.ToString());
  847. cmd.Parameters.AddWithValue("@underpants_asset", appearance.UnderPantsAsset.ToString());
  848. cmd.Parameters.AddWithValue("@skirt_item", appearance.SkirtItem.ToString());
  849. cmd.Parameters.AddWithValue("@skirt_asset", appearance.SkirtAsset.ToString());
  850. try
  851. {
  852. cmd.ExecuteNonQuery();
  853. cmd.Dispose();
  854. }
  855. catch (Exception e)
  856. {
  857. m_log.Error(e.ToString());
  858. }
  859. return;
  860. }
  861. /// <summary>
  862. /// add an attachement to an avatar
  863. /// </summary>
  864. /// <param name="user">the avatar UUID</param>
  865. /// <param name="item">the item UUID</param>
  866. override public void AddAttachment(LLUUID user, LLUUID item)
  867. {
  868. return;
  869. }
  870. /// <summary>
  871. /// Remove an attachement from an avatar
  872. /// </summary>
  873. /// <param name="user">the avatar UUID</param>
  874. /// <param name="item">the item UUID</param>
  875. override public void RemoveAttachment(LLUUID user, LLUUID item)
  876. {
  877. return;
  878. }
  879. /// <summary>
  880. /// get (fetch?) all attached item to an avatar
  881. /// </summary>
  882. /// <param name="user">the avatar UUID</param>
  883. /// <returns>List of attached item</returns>
  884. /// <remarks>return an empty list</remarks>
  885. override public List<LLUUID> GetAttachments(LLUUID user)
  886. {
  887. return new List<LLUUID>();
  888. }
  889. /// <summary>
  890. /// Database provider name
  891. /// </summary>
  892. /// <returns>Provider name</returns>
  893. override public string Name
  894. {
  895. get {return "MSSQL Userdata Interface";}
  896. }
  897. /// <summary>
  898. /// Database provider version
  899. /// </summary>
  900. /// <returns>provider version</returns>
  901. override public string Version
  902. {
  903. get {return database.getVersion();}
  904. }
  905. /// <summary>
  906. /// Not implemented
  907. /// </summary>
  908. /// <param name="query"></param>
  909. public void runQuery(string query)
  910. {
  911. }
  912. }
  913. }