MSSQLUserData.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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 MSSQL
  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. using (IDbCommand cmd = database.Query("select top 1 * from " + m_usersTableName, new Dictionary<string, string>()))
  92. {
  93. try
  94. {
  95. cmd.ExecuteNonQuery();
  96. }
  97. catch
  98. {
  99. database.ExecuteResourceSql("Mssql-users.sql");
  100. }
  101. }
  102. using (IDbCommand cmd = database.Query("select top 1 * from " + m_agentsTableName, new Dictionary<string, string>()))
  103. {
  104. try
  105. {
  106. cmd.ExecuteNonQuery();
  107. }
  108. catch
  109. {
  110. database.ExecuteResourceSql("Mssql-agents.sql");
  111. }
  112. }
  113. using (IDbCommand cmd = database.Query("select top 1 * from " + m_userFriendsTableName, new Dictionary<string, string>()))
  114. {
  115. try
  116. {
  117. cmd.ExecuteNonQuery();
  118. }
  119. catch
  120. {
  121. database.ExecuteResourceSql("CreateUserFriendsTable.sql");
  122. }
  123. }
  124. return true;
  125. }
  126. /// <summary>
  127. /// Searches the database for a specified user profile by name components
  128. /// </summary>
  129. /// <param name="user">The first part of the account name</param>
  130. /// <param name="last">The second part of the account name</param>
  131. /// <returns>A user profile</returns>
  132. override public UserProfileData GetUserByName(string user, string last)
  133. {
  134. try
  135. {
  136. Dictionary<string, string> param = new Dictionary<string, string>();
  137. param["first"] = user;
  138. param["second"] = last;
  139. using (IDbCommand result = database.Query("SELECT * FROM " + m_usersTableName + " WHERE username = @first AND lastname = @second", param))
  140. using (IDataReader reader = result.ExecuteReader())
  141. {
  142. return database.readUserRow(reader);
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. m_log.Error(e.ToString());
  148. return null;
  149. }
  150. }
  151. #region User Friends List Data
  152. /// <summary>
  153. /// Add a new friend in the friendlist
  154. /// </summary>
  155. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  156. /// <param name="friend">Friend's UUID</param>
  157. /// <param name="perms">Permission flag</param>
  158. override public void AddNewUserFriend(LLUUID friendlistowner, LLUUID friend, uint perms)
  159. {
  160. int dtvalue = Util.UnixTimeSinceEpoch();
  161. Dictionary<string, string> param = new Dictionary<string, string>();
  162. param["@ownerID"] = friendlistowner.UUID.ToString();
  163. param["@friendID"] = friend.UUID.ToString();
  164. param["@friendPerms"] = perms.ToString();
  165. param["@datetimestamp"] = dtvalue.ToString();
  166. try
  167. {
  168. using (IDbCommand adder =
  169. database.Query(
  170. "INSERT INTO " + m_userFriendsTableName + " " +
  171. "(ownerID,friendID,friendPerms,datetimestamp) " +
  172. "VALUES " +
  173. "(@ownerID,@friendID,@friendPerms,@datetimestamp)",
  174. param))
  175. {
  176. adder.ExecuteNonQuery();
  177. }
  178. using (IDbCommand adder =
  179. database.Query(
  180. "INSERT INTO " + m_userFriendsTableName + " " +
  181. "(ownerID,friendID,friendPerms,datetimestamp) " +
  182. "VALUES " +
  183. "(@friendID,@ownerID,@friendPerms,@datetimestamp)",
  184. param))
  185. {
  186. adder.ExecuteNonQuery();
  187. }
  188. }
  189. catch (Exception e)
  190. {
  191. m_log.Error(e.ToString());
  192. return;
  193. }
  194. }
  195. /// <summary>
  196. /// Remove an friend from the friendlist
  197. /// </summary>
  198. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  199. /// <param name="friend">UUID of the not-so-friendly user to remove from the list</param>
  200. override public void RemoveUserFriend(LLUUID friendlistowner, LLUUID friend)
  201. {
  202. Dictionary<string, string> param = new Dictionary<string, string>();
  203. param["@ownerID"] = friendlistowner.UUID.ToString();
  204. param["@friendID"] = friend.UUID.ToString();
  205. try
  206. {
  207. using (IDbCommand updater =
  208. database.Query(
  209. "delete from " + m_userFriendsTableName + " where ownerID = @ownerID and friendID = @friendID",
  210. param))
  211. {
  212. updater.ExecuteNonQuery();
  213. }
  214. using (IDbCommand updater =
  215. database.Query(
  216. "delete from " + m_userFriendsTableName + " where ownerID = @friendID and friendID = @ownerID",
  217. param))
  218. {
  219. updater.ExecuteNonQuery();
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. m_log.Error(e.ToString());
  225. }
  226. }
  227. /// <summary>
  228. /// Update friendlist permission flag for a friend
  229. /// </summary>
  230. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  231. /// <param name="friend">UUID of the friend</param>
  232. /// <param name="perms">new permission flag</param>
  233. override public void UpdateUserFriendPerms(LLUUID friendlistowner, LLUUID friend, uint perms)
  234. {
  235. Dictionary<string, string> param = new Dictionary<string, string>();
  236. param["@ownerID"] = friendlistowner.UUID.ToString();
  237. param["@friendID"] = friend.UUID.ToString();
  238. param["@friendPerms"] = perms.ToString();
  239. try
  240. {
  241. using (IDbCommand updater =
  242. database.Query(
  243. "update " + m_userFriendsTableName +
  244. " SET friendPerms = @friendPerms " +
  245. "where ownerID = @ownerID and friendID = @friendID",
  246. param))
  247. {
  248. updater.ExecuteNonQuery();
  249. }
  250. }
  251. catch (Exception e)
  252. {
  253. m_log.Error(e.ToString());
  254. }
  255. }
  256. /// <summary>
  257. /// Get (fetch?) the user's friendlist
  258. /// </summary>
  259. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  260. /// <returns>Friendlist list</returns>
  261. override public List<FriendListItem> GetUserFriendList(LLUUID friendlistowner)
  262. {
  263. List<FriendListItem> Lfli = new List<FriendListItem>();
  264. Dictionary<string, string> param = new Dictionary<string, string>();
  265. param["@ownerID"] = friendlistowner.UUID.ToString();
  266. try
  267. {
  268. //Left Join userfriends to itself
  269. using (IDbCommand result =
  270. database.Query(
  271. "select a.ownerID,a.friendID,a.friendPerms,b.friendPerms as ownerperms from " + m_userFriendsTableName + " as a, " + m_userFriendsTableName + " as b" +
  272. " where a.ownerID = @ownerID and b.ownerID = a.friendID and b.friendID = a.ownerID",
  273. param))
  274. using (IDataReader reader = result.ExecuteReader())
  275. {
  276. while (reader.Read())
  277. {
  278. FriendListItem fli = new FriendListItem();
  279. fli.FriendListOwner = new LLUUID((string)reader["ownerID"]);
  280. fli.Friend = new LLUUID((string)reader["friendID"]);
  281. fli.FriendPerms = (uint)Convert.ToInt32(reader["friendPerms"]);
  282. // This is not a real column in the database table, it's a joined column from the opposite record
  283. fli.FriendListOwnerPerms = (uint)Convert.ToInt32(reader["ownerperms"]);
  284. Lfli.Add(fli);
  285. }
  286. }
  287. }
  288. catch (Exception e)
  289. {
  290. m_log.Error(e.ToString());
  291. }
  292. return Lfli;
  293. }
  294. #endregion
  295. /// <summary>
  296. /// STUB ! Update current region
  297. /// </summary>
  298. /// <param name="avatarid">avatar uuid</param>
  299. /// <param name="regionuuid">region uuid</param>
  300. /// <param name="regionhandle">region handle</param>
  301. override public void UpdateUserCurrentRegion(LLUUID avatarid, LLUUID regionuuid, ulong regionhandle)
  302. {
  303. //m_log.Info("[USER]: Stub UpdateUserCUrrentRegion called");
  304. }
  305. /// <summary>
  306. ///
  307. /// </summary>
  308. /// <param name="queryID"></param>
  309. /// <param name="query"></param>
  310. /// <returns></returns>
  311. override public List<AvatarPickerAvatar> GeneratePickerResults(LLUUID queryID, string query)
  312. {
  313. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  314. string[] querysplit;
  315. querysplit = query.Split(' ');
  316. if (querysplit.Length == 2)
  317. {
  318. try
  319. {
  320. Dictionary<string, string> param = new Dictionary<string, string>();
  321. param["first"] = querysplit[0];
  322. param["second"] = querysplit[1];
  323. using (IDbCommand result = database.Query("SELECT UUID,username,lastname FROM " + m_usersTableName + " WHERE username = @first AND lastname = @second", param))
  324. using (IDataReader reader = result.ExecuteReader())
  325. {
  326. while (reader.Read())
  327. {
  328. AvatarPickerAvatar user = new AvatarPickerAvatar();
  329. user.AvatarID = new LLUUID((string)reader["UUID"]);
  330. user.firstName = (string)reader["username"];
  331. user.lastName = (string)reader["lastname"];
  332. returnlist.Add(user);
  333. }
  334. }
  335. }
  336. catch (Exception e)
  337. {
  338. m_log.Error(e.ToString());
  339. }
  340. }
  341. else if (querysplit.Length == 1)
  342. {
  343. try
  344. {
  345. Dictionary<string, string> param = new Dictionary<string, string>();
  346. param["first"] = querysplit[0];
  347. using (IDbCommand result = database.Query("SELECT UUID,username,lastname FROM " + m_usersTableName + " WHERE username = @first OR lastname = @first", param))
  348. using (IDataReader reader = result.ExecuteReader())
  349. {
  350. while (reader.Read())
  351. {
  352. AvatarPickerAvatar user = new AvatarPickerAvatar();
  353. user.AvatarID = new LLUUID((string)reader["UUID"]);
  354. user.firstName = (string)reader["username"];
  355. user.lastName = (string)reader["lastname"];
  356. returnlist.Add(user);
  357. }
  358. }
  359. }
  360. catch (Exception e)
  361. {
  362. m_log.Error(e.ToString());
  363. }
  364. }
  365. return returnlist;
  366. }
  367. /// <summary>
  368. /// See IUserData
  369. /// </summary>
  370. /// <param name="uuid"></param>
  371. /// <returns></returns>
  372. override public UserProfileData GetUserByUUID(LLUUID uuid)
  373. {
  374. try
  375. {
  376. Dictionary<string, string> param = new Dictionary<string, string>();
  377. param["uuid"] = uuid.ToString();
  378. using (IDbCommand result = database.Query("SELECT * FROM " + m_usersTableName + " WHERE UUID = @uuid", param))
  379. using (IDataReader reader = result.ExecuteReader())
  380. {
  381. return database.readUserRow(reader);
  382. }
  383. }
  384. catch (Exception e)
  385. {
  386. m_log.Error(e.ToString());
  387. return null;
  388. }
  389. }
  390. /// <summary>
  391. /// Returns a user session searching by name
  392. /// </summary>
  393. /// <param name="name">The account name</param>
  394. /// <returns>The users session</returns>
  395. override public UserAgentData GetAgentByName(string name)
  396. {
  397. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  398. }
  399. /// <summary>
  400. /// Returns a user session by account name
  401. /// </summary>
  402. /// <param name="user">First part of the users account name</param>
  403. /// <param name="last">Second part of the users account name</param>
  404. /// <returns>The users session</returns>
  405. override public UserAgentData GetAgentByName(string user, string last)
  406. {
  407. UserProfileData profile = GetUserByName(user, last);
  408. return GetAgentByUUID(profile.ID);
  409. }
  410. /// <summary>
  411. /// Returns an agent session by account UUID
  412. /// </summary>
  413. /// <param name="uuid">The accounts UUID</param>
  414. /// <returns>The users session</returns>
  415. override public UserAgentData GetAgentByUUID(LLUUID uuid)
  416. {
  417. try
  418. {
  419. Dictionary<string, string> param = new Dictionary<string, string>();
  420. param["uuid"] = uuid.ToString();
  421. using (IDbCommand result = database.Query("SELECT * FROM " + m_agentsTableName + " WHERE UUID = @uuid", param))
  422. using (IDataReader reader = result.ExecuteReader())
  423. {
  424. return database.readAgentRow(reader);
  425. }
  426. }
  427. catch (Exception e)
  428. {
  429. m_log.Error(e.ToString());
  430. return null;
  431. }
  432. }
  433. /// <summary>
  434. /// Store a weblogin key
  435. /// </summary>
  436. /// <param name="AgentID">The agent UUID</param>
  437. /// <param name="WebLoginKey">the WebLogin Key</param>
  438. /// <remarks>unused ?</remarks>
  439. override public void StoreWebLoginKey(LLUUID AgentID, LLUUID WebLoginKey)
  440. {
  441. UserProfileData user = GetUserByUUID(AgentID);
  442. user.WebLoginKey = WebLoginKey;
  443. UpdateUserProfile(user);
  444. }
  445. /// <summary>
  446. /// Creates a new users profile
  447. /// </summary>
  448. /// <param name="user">The user profile to create</param>
  449. override public void AddNewUserProfile(UserProfileData user)
  450. {
  451. try
  452. {
  453. InsertUserRow(user.ID, user.FirstName, user.SurName, user.PasswordHash, user.PasswordSalt,
  454. user.HomeRegion, user.HomeLocation.X, user.HomeLocation.Y,
  455. user.HomeLocation.Z,
  456. user.HomeLookAt.X, user.HomeLookAt.Y, user.HomeLookAt.Z, user.Created,
  457. user.LastLogin, user.UserInventoryURI, user.UserAssetURI,
  458. user.CanDoMask, user.WantDoMask,
  459. user.AboutText, user.FirstLifeAboutText, user.Image,
  460. user.FirstLifeImage, user.WebLoginKey);
  461. }
  462. catch (Exception e)
  463. {
  464. m_log.Error(e.ToString());
  465. }
  466. }
  467. /// <summary>
  468. /// Creates a new user and inserts it into the database
  469. /// </summary>
  470. /// <param name="uuid">User ID</param>
  471. /// <param name="username">First part of the login</param>
  472. /// <param name="lastname">Second part of the login</param>
  473. /// <param name="passwordHash">A salted hash of the users password</param>
  474. /// <param name="passwordSalt">The salt used for the password hash</param>
  475. /// <param name="homeRegion">A regionHandle of the users home region</param>
  476. /// <param name="homeLocX">Home region position vector</param>
  477. /// <param name="homeLocY">Home region position vector</param>
  478. /// <param name="homeLocZ">Home region position vector</param>
  479. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  480. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  481. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  482. /// <param name="created">Account created (unix timestamp)</param>
  483. /// <param name="lastlogin">Last login (unix timestamp)</param>
  484. /// <param name="inventoryURI">Users inventory URI</param>
  485. /// <param name="assetURI">Users asset URI</param>
  486. /// <param name="canDoMask">I can do mask</param>
  487. /// <param name="wantDoMask">I want to do mask</param>
  488. /// <param name="aboutText">Profile text</param>
  489. /// <param name="firstText">Firstlife text</param>
  490. /// <param name="profileImage">UUID for profile image</param>
  491. /// <param name="firstImage">UUID for firstlife image</param>
  492. /// <returns>Success?</returns>
  493. private bool InsertUserRow(LLUUID uuid, string username, string lastname, string passwordHash,
  494. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  495. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  496. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  497. string aboutText, string firstText,
  498. LLUUID profileImage, LLUUID firstImage, LLUUID webLoginKey)
  499. {
  500. string sql = "INSERT INTO "+m_usersTableName;
  501. sql += " ([UUID], [username], [lastname], [passwordHash], [passwordSalt], [homeRegion], ";
  502. sql +=
  503. "[homeLocationX], [homeLocationY], [homeLocationZ], [homeLookAtX], [homeLookAtY], [homeLookAtZ], [created], ";
  504. sql +=
  505. "[lastLogin], [userInventoryURI], [userAssetURI], [profileCanDoMask], [profileWantDoMask], [profileAboutText], ";
  506. sql += "[profileFirstText], [profileImage], [profileFirstImage], [webLoginKey]) VALUES ";
  507. sql += "(@UUID, @username, @lastname, @passwordHash, @passwordSalt, @homeRegion, ";
  508. sql +=
  509. "@homeLocationX, @homeLocationY, @homeLocationZ, @homeLookAtX, @homeLookAtY, @homeLookAtZ, @created, ";
  510. sql +=
  511. "@lastLogin, @userInventoryURI, @userAssetURI, @profileCanDoMask, @profileWantDoMask, @profileAboutText, ";
  512. sql += "@profileFirstText, @profileImage, @profileFirstImage, @webLoginKey);";
  513. Dictionary<string, string> parameters = new Dictionary<string, string>();
  514. parameters["UUID"] = uuid.ToString();
  515. parameters["username"] = username.ToString();
  516. parameters["lastname"] = lastname.ToString();
  517. parameters["passwordHash"] = passwordHash.ToString();
  518. parameters["passwordSalt"] = passwordSalt.ToString();
  519. parameters["homeRegion"] = homeRegion.ToString();
  520. parameters["homeLocationX"] = homeLocX.ToString();
  521. parameters["homeLocationY"] = homeLocY.ToString();
  522. parameters["homeLocationZ"] = homeLocZ.ToString();
  523. parameters["homeLookAtX"] = homeLookAtX.ToString();
  524. parameters["homeLookAtY"] = homeLookAtY.ToString();
  525. parameters["homeLookAtZ"] = homeLookAtZ.ToString();
  526. parameters["created"] = created.ToString();
  527. parameters["lastLogin"] = lastlogin.ToString();
  528. parameters["userInventoryURI"] = String.Empty;
  529. parameters["userAssetURI"] = String.Empty;
  530. parameters["profileCanDoMask"] = "0";
  531. parameters["profileWantDoMask"] = "0";
  532. parameters["profileAboutText"] = aboutText;
  533. parameters["profileFirstText"] = firstText;
  534. parameters["profileImage"] = profileImage.ToString();
  535. parameters["profileFirstImage"] = firstImage.ToString();
  536. parameters["webLoginKey"] = LLUUID.Random().ToString();
  537. try
  538. {
  539. using (IDbCommand result = database.Query(sql, parameters))
  540. {
  541. return (result.ExecuteNonQuery() == 1);
  542. }
  543. }
  544. catch (Exception e)
  545. {
  546. m_log.Error(e.ToString());
  547. return false;
  548. }
  549. }
  550. /// <summary>
  551. /// Creates a new agent
  552. /// </summary>
  553. /// <param name="agent">The agent to create</param>
  554. override public void AddNewUserAgent(UserAgentData agent)
  555. {
  556. // Do nothing.
  557. }
  558. /// <summary>
  559. /// update a user profile
  560. /// </summary>
  561. /// <param name="user">the profile to update</param>
  562. /// <returns></returns>
  563. override public bool UpdateUserProfile(UserProfileData user)
  564. {
  565. using (IDbCommand command = database.Query("UPDATE " + m_usersTableName + " set UUID = @uuid, " +
  566. "username = @username, " +
  567. "lastname = @lastname," +
  568. "passwordHash = @passwordHash," +
  569. "passwordSalt = @passwordSalt," +
  570. "homeRegion = @homeRegion," +
  571. "homeLocationX = @homeLocationX," +
  572. "homeLocationY = @homeLocationY," +
  573. "homeLocationZ = @homeLocationZ," +
  574. "homeLookAtX = @homeLookAtX," +
  575. "homeLookAtY = @homeLookAtY," +
  576. "homeLookAtZ = @homeLookAtZ," +
  577. "created = @created," +
  578. "lastLogin = @lastLogin," +
  579. "userInventoryURI = @userInventoryURI," +
  580. "userAssetURI = @userAssetURI," +
  581. "profileCanDoMask = @profileCanDoMask," +
  582. "profileWantDoMask = @profileWantDoMask," +
  583. "profileAboutText = @profileAboutText," +
  584. "profileFirstText = @profileFirstText," +
  585. "profileImage = @profileImage," +
  586. "profileFirstImage = @profileFirstImage, " +
  587. "webLoginKey = @webLoginKey where " +
  588. "UUID = @keyUUUID;"))
  589. {
  590. SqlParameter param1 = new SqlParameter("@uuid", user.ID.ToString());
  591. SqlParameter param2 = new SqlParameter("@username", user.FirstName);
  592. SqlParameter param3 = new SqlParameter("@lastname", user.SurName);
  593. SqlParameter param4 = new SqlParameter("@passwordHash", user.PasswordHash);
  594. SqlParameter param5 = new SqlParameter("@passwordSalt", user.PasswordSalt);
  595. SqlParameter param6 = new SqlParameter("@homeRegion", Convert.ToInt64(user.HomeRegion));
  596. SqlParameter param7 = new SqlParameter("@homeLocationX", user.HomeLocation.X);
  597. SqlParameter param8 = new SqlParameter("@homeLocationY", user.HomeLocation.Y);
  598. SqlParameter param9 = new SqlParameter("@homeLocationZ", user.HomeLocation.Y);
  599. SqlParameter param10 = new SqlParameter("@homeLookAtX", user.HomeLookAt.X);
  600. SqlParameter param11 = new SqlParameter("@homeLookAtY", user.HomeLookAt.Y);
  601. SqlParameter param12 = new SqlParameter("@homeLookAtZ", user.HomeLookAt.Z);
  602. SqlParameter param13 = new SqlParameter("@created", Convert.ToInt32(user.Created));
  603. SqlParameter param14 = new SqlParameter("@lastLogin", Convert.ToInt32(user.LastLogin));
  604. SqlParameter param15 = new SqlParameter("@userInventoryURI", user.UserInventoryURI);
  605. SqlParameter param16 = new SqlParameter("@userAssetURI", user.UserAssetURI);
  606. SqlParameter param17 = new SqlParameter("@profileCanDoMask", Convert.ToInt32(user.CanDoMask));
  607. SqlParameter param18 = new SqlParameter("@profileWantDoMask", Convert.ToInt32(user.WantDoMask));
  608. SqlParameter param19 = new SqlParameter("@profileAboutText", user.AboutText);
  609. SqlParameter param20 = new SqlParameter("@profileFirstText", user.FirstLifeAboutText);
  610. SqlParameter param21 = new SqlParameter("@profileImage", user.Image.ToString());
  611. SqlParameter param22 = new SqlParameter("@profileFirstImage", user.FirstLifeImage.ToString());
  612. SqlParameter param23 = new SqlParameter("@keyUUUID", user.ID.ToString());
  613. SqlParameter param24 = new SqlParameter("@webLoginKey", user.WebLoginKey.UUID.ToString());
  614. command.Parameters.Add(param1);
  615. command.Parameters.Add(param2);
  616. command.Parameters.Add(param3);
  617. command.Parameters.Add(param4);
  618. command.Parameters.Add(param5);
  619. command.Parameters.Add(param6);
  620. command.Parameters.Add(param7);
  621. command.Parameters.Add(param8);
  622. command.Parameters.Add(param9);
  623. command.Parameters.Add(param10);
  624. command.Parameters.Add(param11);
  625. command.Parameters.Add(param12);
  626. command.Parameters.Add(param13);
  627. command.Parameters.Add(param14);
  628. command.Parameters.Add(param15);
  629. command.Parameters.Add(param16);
  630. command.Parameters.Add(param17);
  631. command.Parameters.Add(param18);
  632. command.Parameters.Add(param19);
  633. command.Parameters.Add(param20);
  634. command.Parameters.Add(param21);
  635. command.Parameters.Add(param22);
  636. command.Parameters.Add(param23);
  637. command.Parameters.Add(param24);
  638. try
  639. {
  640. int affected = command.ExecuteNonQuery();
  641. return (affected != 0);
  642. }
  643. catch (Exception e)
  644. {
  645. m_log.Error(e.ToString());
  646. }
  647. }
  648. return false;
  649. }
  650. /// <summary>
  651. /// Performs a money transfer request between two accounts
  652. /// </summary>
  653. /// <param name="from">The senders account ID</param>
  654. /// <param name="to">The receivers account ID</param>
  655. /// <param name="amount">The amount to transfer</param>
  656. /// <returns>false</returns>
  657. override public bool MoneyTransferRequest(LLUUID from, LLUUID to, uint amount)
  658. {
  659. return false;
  660. }
  661. /// <summary>
  662. /// Performs an inventory transfer request between two accounts
  663. /// </summary>
  664. /// <remarks>TODO: Move to inventory server</remarks>
  665. /// <param name="from">The senders account ID</param>
  666. /// <param name="to">The receivers account ID</param>
  667. /// <param name="item">The item to transfer</param>
  668. /// <returns>false</returns>
  669. override public bool InventoryTransferRequest(LLUUID from, LLUUID to, LLUUID item)
  670. {
  671. return false;
  672. }
  673. /// Appearance
  674. /// TODO: stubs for now to get us to a compiling state gently
  675. override public AvatarAppearance GetUserAppearance(LLUUID user)
  676. {
  677. // return new AvatarAppearance();
  678. try
  679. {
  680. Dictionary<string, string> param = new Dictionary<string, string>();
  681. param["@UUID"] = user.ToString();
  682. using (IDbCommand result =
  683. database.Query("SELECT * FROM avatarappearance WHERE owner = @UUID", param))
  684. using (IDataReader reader = result.ExecuteReader())
  685. {
  686. AvatarAppearance item = null;
  687. if (reader.Read())
  688. item = readUserAppearance(reader);
  689. return item;
  690. }
  691. }
  692. catch (Exception e)
  693. {
  694. m_log.Error(e.ToString());
  695. }
  696. return null;
  697. }
  698. /// <summary>
  699. /// Reads a one item from an SQL result
  700. /// </summary>
  701. /// <param name="reader">The SQL Result</param>
  702. /// <returns>the item read</returns>
  703. private static AvatarAppearance readUserAppearance(IDataReader reader)
  704. {
  705. try
  706. {
  707. AvatarAppearance appearance = new AvatarAppearance();
  708. appearance.Owner = new LLUUID((string)reader["owner"]);
  709. appearance.Serial = Convert.ToInt32(reader["serial"]);
  710. appearance.VisualParams = (byte[])reader["visual_params"];
  711. appearance.Texture = new LLObject.TextureEntry((byte[])reader["texture"], 0, ((byte[])reader["texture"]).Length);
  712. appearance.AvatarHeight = (float)Convert.ToDouble(reader["avatar_height"]);
  713. appearance.BodyItem = new LLUUID((string)reader["body_item"]);
  714. appearance.BodyAsset = new LLUUID((string)reader["body_asset"]);
  715. appearance.SkinItem = new LLUUID((string)reader["skin_item"]);
  716. appearance.SkinAsset = new LLUUID((string)reader["skin_asset"]);
  717. appearance.HairItem = new LLUUID((string)reader["hair_item"]);
  718. appearance.HairAsset = new LLUUID((string)reader["hair_asset"]);
  719. appearance.EyesItem = new LLUUID((string)reader["eyes_item"]);
  720. appearance.EyesAsset = new LLUUID((string)reader["eyes_asset"]);
  721. appearance.ShirtItem = new LLUUID((string)reader["shirt_item"]);
  722. appearance.ShirtAsset = new LLUUID((string)reader["shirt_asset"]);
  723. appearance.PantsItem = new LLUUID((string)reader["pants_item"]);
  724. appearance.PantsAsset = new LLUUID((string)reader["pants_asset"]);
  725. appearance.ShoesItem = new LLUUID((string)reader["shoes_item"]);
  726. appearance.ShoesAsset = new LLUUID((string)reader["shoes_asset"]);
  727. appearance.SocksItem = new LLUUID((string)reader["socks_item"]);
  728. appearance.SocksAsset = new LLUUID((string)reader["socks_asset"]);
  729. appearance.JacketItem = new LLUUID((string)reader["jacket_item"]);
  730. appearance.JacketAsset = new LLUUID((string)reader["jacket_asset"]);
  731. appearance.GlovesItem = new LLUUID((string)reader["gloves_item"]);
  732. appearance.GlovesAsset = new LLUUID((string)reader["gloves_asset"]);
  733. appearance.UnderShirtItem = new LLUUID((string)reader["undershirt_item"]);
  734. appearance.UnderShirtAsset = new LLUUID((string)reader["undershirt_asset"]);
  735. appearance.UnderPantsItem = new LLUUID((string)reader["underpants_item"]);
  736. appearance.UnderPantsAsset = new LLUUID((string)reader["underpants_asset"]);
  737. appearance.SkirtItem = new LLUUID((string)reader["skirt_item"]);
  738. appearance.SkirtAsset = new LLUUID((string)reader["skirt_asset"]);
  739. return appearance;
  740. }
  741. catch (SqlException e)
  742. {
  743. m_log.Error(e.ToString());
  744. }
  745. return null;
  746. }
  747. /// <summary>
  748. /// Update a user appearence into database
  749. /// </summary>
  750. /// <param name="user">the used UUID</param>
  751. /// <param name="appearance">the appearence</param>
  752. override public void UpdateUserAppearance(LLUUID user, AvatarAppearance appearance)
  753. {
  754. string sql = String.Empty;
  755. sql += "DELETE FROM avatarappearance WHERE owner=@owner ";
  756. sql += "INSERT INTO avatarappearance ";
  757. sql += "(owner, serial, visual_params, texture, avatar_height, ";
  758. sql += "body_item, body_asset, skin_item, skin_asset, hair_item, hair_asset, eyes_item, eyes_asset, ";
  759. sql += "shirt_item, shirt_asset, pants_item, pants_asset, shoes_item, shoes_asset, socks_item, socks_asset, ";
  760. sql += "jacket_item, jacket_asset, gloves_item, gloves_asset, undershirt_item, undershirt_asset, underpants_item, underpants_asset, ";
  761. sql += "skirt_item, skirt_asset) values (";
  762. sql += "@owner, @serial, @visual_params, @texture, @avatar_height, ";
  763. sql += "@body_item, @body_asset, @skin_item, @skin_asset, @hair_item, @hair_asset, @eyes_item, @eyes_asset, ";
  764. sql += "@shirt_item, @shirt_asset, @pants_item, @pants_asset, @shoes_item, @shoes_asset, @socks_item, @socks_asset, ";
  765. sql += "@jacket_item, @jacket_asset, @gloves_item, @gloves_asset, @undershirt_item, @undershirt_asset, @underpants_item, @underpants_asset, ";
  766. sql += "@skirt_item, @skirt_asset)";
  767. using (AutoClosingSqlCommand cmd = database.Query(sql))
  768. {
  769. cmd.Parameters.AddWithValue("@owner", appearance.Owner.ToString());
  770. cmd.Parameters.AddWithValue("@serial", appearance.Serial);
  771. cmd.Parameters.AddWithValue("@visual_params", appearance.VisualParams);
  772. cmd.Parameters.AddWithValue("@texture", appearance.Texture.ToBytes());
  773. cmd.Parameters.AddWithValue("@avatar_height", appearance.AvatarHeight);
  774. cmd.Parameters.AddWithValue("@body_item", appearance.BodyItem.ToString());
  775. cmd.Parameters.AddWithValue("@body_asset", appearance.BodyAsset.ToString());
  776. cmd.Parameters.AddWithValue("@skin_item", appearance.SkinItem.ToString());
  777. cmd.Parameters.AddWithValue("@skin_asset", appearance.SkinAsset.ToString());
  778. cmd.Parameters.AddWithValue("@hair_item", appearance.HairItem.ToString());
  779. cmd.Parameters.AddWithValue("@hair_asset", appearance.HairAsset.ToString());
  780. cmd.Parameters.AddWithValue("@eyes_item", appearance.EyesItem.ToString());
  781. cmd.Parameters.AddWithValue("@eyes_asset", appearance.EyesAsset.ToString());
  782. cmd.Parameters.AddWithValue("@shirt_item", appearance.ShirtItem.ToString());
  783. cmd.Parameters.AddWithValue("@shirt_asset", appearance.ShirtAsset.ToString());
  784. cmd.Parameters.AddWithValue("@pants_item", appearance.PantsItem.ToString());
  785. cmd.Parameters.AddWithValue("@pants_asset", appearance.PantsAsset.ToString());
  786. cmd.Parameters.AddWithValue("@shoes_item", appearance.ShoesItem.ToString());
  787. cmd.Parameters.AddWithValue("@shoes_asset", appearance.ShoesAsset.ToString());
  788. cmd.Parameters.AddWithValue("@socks_item", appearance.SocksItem.ToString());
  789. cmd.Parameters.AddWithValue("@socks_asset", appearance.SocksAsset.ToString());
  790. cmd.Parameters.AddWithValue("@jacket_item", appearance.JacketItem.ToString());
  791. cmd.Parameters.AddWithValue("@jacket_asset", appearance.JacketAsset.ToString());
  792. cmd.Parameters.AddWithValue("@gloves_item", appearance.GlovesItem.ToString());
  793. cmd.Parameters.AddWithValue("@gloves_asset", appearance.GlovesAsset.ToString());
  794. cmd.Parameters.AddWithValue("@undershirt_item", appearance.UnderShirtItem.ToString());
  795. cmd.Parameters.AddWithValue("@undershirt_asset", appearance.UnderShirtAsset.ToString());
  796. cmd.Parameters.AddWithValue("@underpants_item", appearance.UnderPantsItem.ToString());
  797. cmd.Parameters.AddWithValue("@underpants_asset", appearance.UnderPantsAsset.ToString());
  798. cmd.Parameters.AddWithValue("@skirt_item", appearance.SkirtItem.ToString());
  799. cmd.Parameters.AddWithValue("@skirt_asset", appearance.SkirtAsset.ToString());
  800. try
  801. {
  802. cmd.ExecuteNonQuery();
  803. }
  804. catch (Exception e)
  805. {
  806. m_log.Error(e.ToString());
  807. }
  808. }
  809. }
  810. /// <summary>
  811. /// add an attachement to an avatar
  812. /// </summary>
  813. /// <param name="user">the avatar UUID</param>
  814. /// <param name="item">the item UUID</param>
  815. override public void AddAttachment(LLUUID user, LLUUID item)
  816. {
  817. // TBI?
  818. }
  819. /// <summary>
  820. /// Remove an attachement from an avatar
  821. /// </summary>
  822. /// <param name="user">the avatar UUID</param>
  823. /// <param name="item">the item UUID</param>
  824. override public void RemoveAttachment(LLUUID user, LLUUID item)
  825. {
  826. // TBI?
  827. }
  828. /// <summary>
  829. /// get (fetch?) all attached item to an avatar
  830. /// </summary>
  831. /// <param name="user">the avatar UUID</param>
  832. /// <returns>List of attached item</returns>
  833. /// <remarks>return an empty list</remarks>
  834. override public List<LLUUID> GetAttachments(LLUUID user)
  835. {
  836. return new List<LLUUID>();
  837. }
  838. /// <summary>
  839. /// Database provider name
  840. /// </summary>
  841. /// <returns>Provider name</returns>
  842. override public string Name
  843. {
  844. get {return "MSSQL Userdata Interface";}
  845. }
  846. /// <summary>
  847. /// Database provider version
  848. /// </summary>
  849. /// <returns>provider version</returns>
  850. override public string Version
  851. {
  852. get {return database.getVersion();}
  853. }
  854. /// <summary>
  855. /// Not implemented
  856. /// </summary>
  857. /// <param name="query"></param>
  858. public void runQuery(string query)
  859. {
  860. }
  861. }
  862. }