MSSQLUserData.cs 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.Data.SqlClient;
  32. using System.Reflection;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Data.MSSQL
  37. {
  38. /// <summary>
  39. /// A database interface class to a user profile storage system
  40. /// </summary>
  41. public class MSSQLUserData : UserDataBase
  42. {
  43. private const string _migrationStore = "UserStore";
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// Database manager for MSSQL
  47. /// </summary>
  48. public MSSQLManager database;
  49. private const string m_agentsTableName = "agents";
  50. private const string m_usersTableName = "users";
  51. private const string m_userFriendsTableName = "userfriends";
  52. // [Obsolete("Cannot be default-initialized!")]
  53. override public void Initialise()
  54. {
  55. m_log.Info("[MSSQLUserData]: " + Name + " cannot be default-initialized!");
  56. throw new PluginNotInitialisedException(Name);
  57. }
  58. /// <summary>
  59. /// Loads and initialises the MSSQL storage plugin
  60. /// </summary>
  61. /// <param name="connect">connectionstring</param>
  62. /// <remarks>use mssql_connection.ini</remarks>
  63. override public void Initialise(string connect)
  64. {
  65. if (!string.IsNullOrEmpty(connect))
  66. {
  67. database = new MSSQLManager(connect);
  68. }
  69. else
  70. {
  71. IniFile iniFile = new IniFile("mssql_connection.ini");
  72. string settingDataSource = iniFile.ParseFileReadValue("data_source");
  73. string settingInitialCatalog = iniFile.ParseFileReadValue("initial_catalog");
  74. string settingPersistSecurityInfo = iniFile.ParseFileReadValue("persist_security_info");
  75. string settingUserId = iniFile.ParseFileReadValue("user_id");
  76. string settingPassword = iniFile.ParseFileReadValue("password");
  77. database = new MSSQLManager(settingDataSource, settingInitialCatalog, settingPersistSecurityInfo, settingUserId, settingPassword);
  78. }
  79. //Check migration on DB
  80. database.CheckMigration(_migrationStore);
  81. }
  82. /// <summary>
  83. /// Releases unmanaged and - optionally - managed resources
  84. /// </summary>
  85. override public void Dispose() { }
  86. #region User table methods
  87. /// <summary>
  88. /// Searches the database for a specified user profile by name components
  89. /// </summary>
  90. /// <param name="user">The first part of the account name</param>
  91. /// <param name="last">The second part of the account name</param>
  92. /// <returns>A user profile</returns>
  93. override public UserProfileData GetUserByName(string user, string last)
  94. {
  95. string sql = string.Format(@"SELECT * FROM {0}
  96. WHERE username = @first AND lastname = @second", m_usersTableName);
  97. using (AutoClosingSqlCommand command = database.Query(sql))
  98. {
  99. command.Parameters.Add(database.CreateParameter("first", user));
  100. command.Parameters.Add(database.CreateParameter("second", last));
  101. try
  102. {
  103. using (SqlDataReader reader = command.ExecuteReader())
  104. {
  105. return ReadUserRow(reader);
  106. }
  107. }
  108. catch (Exception e)
  109. {
  110. m_log.ErrorFormat("[USER DB] Error getting user profile for {0} {1}: {2}", user, last, e.Message);
  111. return null;
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// See IUserDataPlugin
  117. /// </summary>
  118. /// <param name="uuid"></param>
  119. /// <returns></returns>
  120. override public UserProfileData GetUserByUUID(UUID uuid)
  121. {
  122. string sql = string.Format("SELECT * FROM {0} WHERE UUID = @uuid", m_usersTableName);
  123. using (AutoClosingSqlCommand command = database.Query(sql))
  124. {
  125. command.Parameters.Add(database.CreateParameter("uuid", uuid));
  126. try
  127. {
  128. using (SqlDataReader reader = command.ExecuteReader())
  129. {
  130. return ReadUserRow(reader);
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.ErrorFormat("[USER DB] Error getting user profile by UUID {0}, error: {1}", uuid, e.Message);
  136. return null;
  137. }
  138. }
  139. }
  140. /// <summary>
  141. /// Creates a new users profile
  142. /// </summary>
  143. /// <param name="user">The user profile to create</param>
  144. override public void AddNewUserProfile(UserProfileData user)
  145. {
  146. try
  147. {
  148. InsertUserRow(user.ID, user.FirstName, user.SurName, user.Email, user.PasswordHash, user.PasswordSalt,
  149. user.HomeRegion, user.HomeLocation.X, user.HomeLocation.Y,
  150. user.HomeLocation.Z,
  151. user.HomeLookAt.X, user.HomeLookAt.Y, user.HomeLookAt.Z, user.Created,
  152. user.LastLogin, user.UserInventoryURI, user.UserAssetURI,
  153. user.CanDoMask, user.WantDoMask,
  154. user.AboutText, user.FirstLifeAboutText, user.Image,
  155. user.FirstLifeImage, user.WebLoginKey, user.HomeRegionID,
  156. user.GodLevel, user.UserFlags, user.CustomType, user.Partner);
  157. }
  158. catch (Exception e)
  159. {
  160. m_log.ErrorFormat("[USER DB] Error adding new profile, error: {0}", e.Message);
  161. }
  162. }
  163. /// <summary>
  164. /// update a user profile
  165. /// </summary>
  166. /// <param name="user">the profile to update</param>
  167. /// <returns></returns>
  168. override public bool UpdateUserProfile(UserProfileData user)
  169. {
  170. string sql = string.Format(@"UPDATE {0}
  171. SET UUID = @uuid,
  172. username = @username,
  173. lastname = @lastname,
  174. email = @email,
  175. passwordHash = @passwordHash,
  176. passwordSalt = @passwordSalt,
  177. homeRegion = @homeRegion,
  178. homeLocationX = @homeLocationX,
  179. homeLocationY = @homeLocationY,
  180. homeLocationZ = @homeLocationZ,
  181. homeLookAtX = @homeLookAtX,
  182. homeLookAtY = @homeLookAtY,
  183. homeLookAtZ = @homeLookAtZ,
  184. created = @created,
  185. lastLogin = @lastLogin,
  186. userInventoryURI = @userInventoryURI,
  187. userAssetURI = @userAssetURI,
  188. profileCanDoMask = @profileCanDoMask,
  189. profileWantDoMask = @profileWantDoMask,
  190. profileAboutText = @profileAboutText,
  191. profileFirstText = @profileFirstText,
  192. profileImage = @profileImage,
  193. profileFirstImage = @profileFirstImage,
  194. webLoginKey = @webLoginKey,
  195. homeRegionID = @homeRegionID,
  196. userFlags = @userFlags,
  197. godLevel = @godLevel,
  198. customType = @customType,
  199. partner = @partner WHERE UUID = @keyUUUID;",m_usersTableName);
  200. using (AutoClosingSqlCommand command = database.Query(sql))
  201. {
  202. command.Parameters.Add(database.CreateParameter("uuid", user.ID));
  203. command.Parameters.Add(database.CreateParameter("username", user.FirstName));
  204. command.Parameters.Add(database.CreateParameter("lastname", user.SurName));
  205. command.Parameters.Add(database.CreateParameter("email", user.Email));
  206. command.Parameters.Add(database.CreateParameter("passwordHash", user.PasswordHash));
  207. command.Parameters.Add(database.CreateParameter("passwordSalt", user.PasswordSalt));
  208. command.Parameters.Add(database.CreateParameter("homeRegion", user.HomeRegion));
  209. command.Parameters.Add(database.CreateParameter("homeLocationX", user.HomeLocation.X));
  210. command.Parameters.Add(database.CreateParameter("homeLocationY", user.HomeLocation.Y));
  211. command.Parameters.Add(database.CreateParameter("homeLocationZ", user.HomeLocation.Z));
  212. command.Parameters.Add(database.CreateParameter("homeLookAtX", user.HomeLookAt.X));
  213. command.Parameters.Add(database.CreateParameter("homeLookAtY", user.HomeLookAt.Y));
  214. command.Parameters.Add(database.CreateParameter("homeLookAtZ", user.HomeLookAt.Z));
  215. command.Parameters.Add(database.CreateParameter("created", user.Created));
  216. command.Parameters.Add(database.CreateParameter("lastLogin", user.LastLogin));
  217. command.Parameters.Add(database.CreateParameter("userInventoryURI", user.UserInventoryURI));
  218. command.Parameters.Add(database.CreateParameter("userAssetURI", user.UserAssetURI));
  219. command.Parameters.Add(database.CreateParameter("profileCanDoMask", user.CanDoMask));
  220. command.Parameters.Add(database.CreateParameter("profileWantDoMask", user.WantDoMask));
  221. command.Parameters.Add(database.CreateParameter("profileAboutText", user.AboutText));
  222. command.Parameters.Add(database.CreateParameter("profileFirstText", user.FirstLifeAboutText));
  223. command.Parameters.Add(database.CreateParameter("profileImage", user.Image));
  224. command.Parameters.Add(database.CreateParameter("profileFirstImage", user.FirstLifeImage));
  225. command.Parameters.Add(database.CreateParameter("webLoginKey", user.WebLoginKey));
  226. command.Parameters.Add(database.CreateParameter("homeRegionID", user.HomeRegionID));
  227. command.Parameters.Add(database.CreateParameter("userFlags", user.UserFlags));
  228. command.Parameters.Add(database.CreateParameter("godLevel", user.GodLevel));
  229. command.Parameters.Add(database.CreateParameter("customType", user.CustomType));
  230. command.Parameters.Add(database.CreateParameter("partner", user.Partner));
  231. command.Parameters.Add(database.CreateParameter("keyUUUID", user.ID));
  232. try
  233. {
  234. int affected = command.ExecuteNonQuery();
  235. return (affected != 0);
  236. }
  237. catch (Exception e)
  238. {
  239. m_log.ErrorFormat("[USER DB] Error updating profile, error: {0}", e.Message);
  240. }
  241. }
  242. return false;
  243. }
  244. #endregion
  245. #region Agent table methods
  246. /// <summary>
  247. /// Returns a user session searching by name
  248. /// </summary>
  249. /// <param name="name">The account name</param>
  250. /// <returns>The users session</returns>
  251. override public UserAgentData GetAgentByName(string name)
  252. {
  253. return GetAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  254. }
  255. /// <summary>
  256. /// Returns a user session by account name
  257. /// </summary>
  258. /// <param name="user">First part of the users account name</param>
  259. /// <param name="last">Second part of the users account name</param>
  260. /// <returns>The users session</returns>
  261. override public UserAgentData GetAgentByName(string user, string last)
  262. {
  263. UserProfileData profile = GetUserByName(user, last);
  264. return GetAgentByUUID(profile.ID);
  265. }
  266. /// <summary>
  267. /// Returns an agent session by account UUID
  268. /// </summary>
  269. /// <param name="uuid">The accounts UUID</param>
  270. /// <returns>The users session</returns>
  271. override public UserAgentData GetAgentByUUID(UUID uuid)
  272. {
  273. string sql = string.Format("SELECT * FROM {0} WHERE UUID = @uuid", m_agentsTableName);
  274. using (AutoClosingSqlCommand command = database.Query(sql))
  275. {
  276. command.Parameters.Add(database.CreateParameter("uuid", uuid));
  277. try
  278. {
  279. using (SqlDataReader reader = command.ExecuteReader())
  280. {
  281. return readAgentRow(reader);
  282. }
  283. }
  284. catch (Exception e)
  285. {
  286. m_log.ErrorFormat("[USER DB] Error updating agentdata by UUID, error: {0}", e.Message);
  287. return null;
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// Creates a new agent
  293. /// </summary>
  294. /// <param name="agent">The agent to create</param>
  295. override public void AddNewUserAgent(UserAgentData agent)
  296. {
  297. try
  298. {
  299. InsertUpdateAgentRow(agent);
  300. }
  301. catch (Exception e)
  302. {
  303. m_log.ErrorFormat("[USER DB] Error adding new agentdata, error: {0}", e.Message);
  304. }
  305. }
  306. #endregion
  307. #region User Friends List Data
  308. /// <summary>
  309. /// Add a new friend in the friendlist
  310. /// </summary>
  311. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  312. /// <param name="friend">Friend's UUID</param>
  313. /// <param name="perms">Permission flag</param>
  314. override public void AddNewUserFriend(UUID friendlistowner, UUID friend, uint perms)
  315. {
  316. int dtvalue = Util.UnixTimeSinceEpoch();
  317. string sql = string.Format(@"INSERT INTO {0}
  318. (ownerID,friendID,friendPerms,datetimestamp)
  319. VALUES
  320. (@ownerID,@friendID,@friendPerms,@datetimestamp)", m_userFriendsTableName);
  321. using (AutoClosingSqlCommand command = database.Query(sql))
  322. {
  323. command.Parameters.Add(database.CreateParameter("ownerID", friendlistowner));
  324. command.Parameters.Add(database.CreateParameter("friendID", friend));
  325. command.Parameters.Add(database.CreateParameter("friendPerms", perms));
  326. command.Parameters.Add(database.CreateParameter("datetimestamp", dtvalue));
  327. command.ExecuteNonQuery();
  328. try
  329. {
  330. sql = string.Format(@"INSERT INTO {0}
  331. (ownerID,friendID,friendPerms,datetimestamp)
  332. VALUES
  333. (@friendID,@ownerID,@friendPerms,@datetimestamp)", m_userFriendsTableName);
  334. command.CommandText = sql;
  335. command.ExecuteNonQuery();
  336. }
  337. catch (Exception e)
  338. {
  339. m_log.ErrorFormat("[USER DB] Error adding new userfriend, error: {0}", e.Message);
  340. return;
  341. }
  342. }
  343. }
  344. /// <summary>
  345. /// Remove an friend from the friendlist
  346. /// </summary>
  347. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  348. /// <param name="friend">UUID of the not-so-friendly user to remove from the list</param>
  349. override public void RemoveUserFriend(UUID friendlistowner, UUID friend)
  350. {
  351. string sql = string.Format(@"DELETE from {0}
  352. WHERE ownerID = @ownerID
  353. AND friendID = @friendID", m_userFriendsTableName);
  354. using (AutoClosingSqlCommand command = database.Query(sql))
  355. {
  356. command.Parameters.Add(database.CreateParameter("@ownerID", friendlistowner));
  357. command.Parameters.Add(database.CreateParameter("@friendID", friend));
  358. command.ExecuteNonQuery();
  359. sql = string.Format(@"DELETE from {0}
  360. WHERE ownerID = @friendID
  361. AND friendID = @ownerID", m_userFriendsTableName);
  362. command.CommandText = sql;
  363. try
  364. {
  365. command.ExecuteNonQuery();
  366. }
  367. catch (Exception e)
  368. {
  369. m_log.ErrorFormat("[USER DB] Error removing userfriend, error: {0}", e.Message);
  370. }
  371. }
  372. }
  373. /// <summary>
  374. /// Update friendlist permission flag for a friend
  375. /// </summary>
  376. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  377. /// <param name="friend">UUID of the friend</param>
  378. /// <param name="perms">new permission flag</param>
  379. override public void UpdateUserFriendPerms(UUID friendlistowner, UUID friend, uint perms)
  380. {
  381. string sql = string.Format(@"UPDATE {0} SET friendPerms = @friendPerms
  382. WHERE ownerID = @ownerID
  383. AND friendID = @friendID", m_userFriendsTableName);
  384. using (AutoClosingSqlCommand command = database.Query(sql))
  385. {
  386. command.Parameters.Add(database.CreateParameter("@ownerID", friendlistowner));
  387. command.Parameters.Add(database.CreateParameter("@friendID", friend));
  388. command.Parameters.Add(database.CreateParameter("@friendPerms", perms));
  389. try
  390. {
  391. command.ExecuteNonQuery();
  392. }
  393. catch (Exception e)
  394. {
  395. m_log.ErrorFormat("[USER DB] Error updating userfriend, error: {0}", e.Message);
  396. }
  397. }
  398. }
  399. /// <summary>
  400. /// Get (fetch?) the user's friendlist
  401. /// </summary>
  402. /// <param name="friendlistowner">UUID of the friendlist owner</param>
  403. /// <returns>Friendlist list</returns>
  404. override public List<FriendListItem> GetUserFriendList(UUID friendlistowner)
  405. {
  406. List<FriendListItem> friendList = new List<FriendListItem>();
  407. //Left Join userfriends to itself
  408. string sql = string.Format(@"SELECT a.ownerID, a.friendID, a.friendPerms, b.friendPerms AS ownerperms
  409. FROM {0} as a, {0} as b
  410. WHERE a.ownerID = @ownerID
  411. AND b.ownerID = a.friendID
  412. AND b.friendID = a.ownerID", m_userFriendsTableName);
  413. using (AutoClosingSqlCommand command = database.Query(sql))
  414. {
  415. command.Parameters.Add(database.CreateParameter("@ownerID", friendlistowner));
  416. try
  417. {
  418. using (SqlDataReader reader = command.ExecuteReader())
  419. {
  420. while (reader.Read())
  421. {
  422. FriendListItem fli = new FriendListItem();
  423. fli.FriendListOwner = new UUID((Guid)reader["ownerID"]);
  424. fli.Friend = new UUID((Guid)reader["friendID"]);
  425. fli.FriendPerms = (uint)Convert.ToInt32(reader["friendPerms"]);
  426. // This is not a real column in the database table, it's a joined column from the opposite record
  427. fli.FriendListOwnerPerms = (uint)Convert.ToInt32(reader["ownerperms"]);
  428. friendList.Add(fli);
  429. }
  430. }
  431. }
  432. catch (Exception e)
  433. {
  434. m_log.ErrorFormat("[USER DB] Error updating userfriend, error: {0}", e.Message);
  435. }
  436. }
  437. return friendList;
  438. }
  439. override public Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos (List<UUID> uuids)
  440. {
  441. Dictionary<UUID, FriendRegionInfo> infos = new Dictionary<UUID,FriendRegionInfo>();
  442. try
  443. {
  444. foreach (UUID uuid in uuids)
  445. {
  446. string sql = string.Format(@"SELECT agentOnline,currentHandle
  447. FROM {0} WHERE UUID = @uuid", m_agentsTableName);
  448. using (AutoClosingSqlCommand command = database.Query(sql))
  449. {
  450. command.Parameters.Add(database.CreateParameter("@uuid", uuid));
  451. using (SqlDataReader reader = command.ExecuteReader())
  452. {
  453. while (reader.Read())
  454. {
  455. FriendRegionInfo fri = new FriendRegionInfo();
  456. fri.isOnline = (byte)reader["agentOnline"] != 0;
  457. fri.regionHandle = Convert.ToUInt64(reader["currentHandle"].ToString());
  458. infos[uuid] = fri;
  459. }
  460. }
  461. }
  462. }
  463. }
  464. catch (Exception e)
  465. {
  466. m_log.Warn("[MSSQL]: Got exception on trying to find friends regions:", e);
  467. }
  468. return infos;
  469. }
  470. #endregion
  471. #region Money functions (not used)
  472. /// <summary>
  473. /// Performs a money transfer request between two accounts
  474. /// </summary>
  475. /// <param name="from">The senders account ID</param>
  476. /// <param name="to">The receivers account ID</param>
  477. /// <param name="amount">The amount to transfer</param>
  478. /// <returns>false</returns>
  479. override public bool MoneyTransferRequest(UUID from, UUID to, uint amount)
  480. {
  481. return false;
  482. }
  483. /// <summary>
  484. /// Performs an inventory transfer request between two accounts
  485. /// </summary>
  486. /// <remarks>TODO: Move to inventory server</remarks>
  487. /// <param name="from">The senders account ID</param>
  488. /// <param name="to">The receivers account ID</param>
  489. /// <param name="item">The item to transfer</param>
  490. /// <returns>false</returns>
  491. override public bool InventoryTransferRequest(UUID from, UUID to, UUID item)
  492. {
  493. return false;
  494. }
  495. #endregion
  496. #region Appearance methods
  497. /// <summary>
  498. /// Gets the user appearance.
  499. /// </summary>
  500. /// <param name="user">The user.</param>
  501. /// <returns></returns>
  502. override public AvatarAppearance GetUserAppearance(UUID user)
  503. {
  504. try
  505. {
  506. AvatarAppearance appearance = new AvatarAppearance();
  507. string sql = "SELECT * FROM avatarappearance WHERE owner = @UUID";
  508. using (AutoClosingSqlCommand command = database.Query(sql))
  509. {
  510. command.Parameters.Add(database.CreateParameter("@UUID", user));
  511. using (SqlDataReader reader = command.ExecuteReader())
  512. {
  513. if (reader.Read())
  514. appearance = readUserAppearance(reader);
  515. else
  516. {
  517. m_log.WarnFormat("[USER DB] No appearance found for user {0}", user.ToString());
  518. return null;
  519. }
  520. }
  521. }
  522. appearance.SetAttachments(GetUserAttachments(user));
  523. return appearance;
  524. }
  525. catch (Exception e)
  526. {
  527. m_log.ErrorFormat("[USER DB] Error updating userfriend, error: {0}", e.Message);
  528. }
  529. return null;
  530. }
  531. /// <summary>
  532. /// Update a user appearence into database
  533. /// </summary>
  534. /// <param name="user">the used UUID</param>
  535. /// <param name="appearance">the appearence</param>
  536. override public void UpdateUserAppearance(UUID user, AvatarAppearance appearance)
  537. {
  538. string sql = @"DELETE FROM avatarappearance WHERE owner=@owner;
  539. INSERT INTO avatarappearance
  540. (owner, serial, visual_params, texture, avatar_height,
  541. body_item, body_asset, skin_item, skin_asset, hair_item,
  542. hair_asset, eyes_item, eyes_asset, shirt_item, shirt_asset,
  543. pants_item, pants_asset, shoes_item, shoes_asset, socks_item,
  544. socks_asset, jacket_item, jacket_asset, gloves_item, gloves_asset,
  545. undershirt_item, undershirt_asset, underpants_item, underpants_asset,
  546. skirt_item, skirt_asset)
  547. VALUES
  548. (@owner, @serial, @visual_params, @texture, @avatar_height,
  549. @body_item, @body_asset, @skin_item, @skin_asset, @hair_item,
  550. @hair_asset, @eyes_item, @eyes_asset, @shirt_item, @shirt_asset,
  551. @pants_item, @pants_asset, @shoes_item, @shoes_asset, @socks_item,
  552. @socks_asset, @jacket_item, @jacket_asset, @gloves_item, @gloves_asset,
  553. @undershirt_item, @undershirt_asset, @underpants_item, @underpants_asset,
  554. @skirt_item, @skirt_asset)";
  555. using (AutoClosingSqlCommand cmd = database.Query(sql))
  556. {
  557. cmd.Parameters.Add(database.CreateParameter("@owner", appearance.Owner));
  558. cmd.Parameters.Add(database.CreateParameter("@serial", appearance.Serial));
  559. cmd.Parameters.Add(database.CreateParameter("@visual_params", appearance.VisualParams));
  560. cmd.Parameters.Add(database.CreateParameter("@texture", appearance.Texture.GetBytes()));
  561. cmd.Parameters.Add(database.CreateParameter("@avatar_height", appearance.AvatarHeight));
  562. cmd.Parameters.Add(database.CreateParameter("@body_item", appearance.BodyItem));
  563. cmd.Parameters.Add(database.CreateParameter("@body_asset", appearance.BodyAsset));
  564. cmd.Parameters.Add(database.CreateParameter("@skin_item", appearance.SkinItem));
  565. cmd.Parameters.Add(database.CreateParameter("@skin_asset", appearance.SkinAsset));
  566. cmd.Parameters.Add(database.CreateParameter("@hair_item", appearance.HairItem));
  567. cmd.Parameters.Add(database.CreateParameter("@hair_asset", appearance.HairAsset));
  568. cmd.Parameters.Add(database.CreateParameter("@eyes_item", appearance.EyesItem));
  569. cmd.Parameters.Add(database.CreateParameter("@eyes_asset", appearance.EyesAsset));
  570. cmd.Parameters.Add(database.CreateParameter("@shirt_item", appearance.ShirtItem));
  571. cmd.Parameters.Add(database.CreateParameter("@shirt_asset", appearance.ShirtAsset));
  572. cmd.Parameters.Add(database.CreateParameter("@pants_item", appearance.PantsItem));
  573. cmd.Parameters.Add(database.CreateParameter("@pants_asset", appearance.PantsAsset));
  574. cmd.Parameters.Add(database.CreateParameter("@shoes_item", appearance.ShoesItem));
  575. cmd.Parameters.Add(database.CreateParameter("@shoes_asset", appearance.ShoesAsset));
  576. cmd.Parameters.Add(database.CreateParameter("@socks_item", appearance.SocksItem));
  577. cmd.Parameters.Add(database.CreateParameter("@socks_asset", appearance.SocksAsset));
  578. cmd.Parameters.Add(database.CreateParameter("@jacket_item", appearance.JacketItem));
  579. cmd.Parameters.Add(database.CreateParameter("@jacket_asset", appearance.JacketAsset));
  580. cmd.Parameters.Add(database.CreateParameter("@gloves_item", appearance.GlovesItem));
  581. cmd.Parameters.Add(database.CreateParameter("@gloves_asset", appearance.GlovesAsset));
  582. cmd.Parameters.Add(database.CreateParameter("@undershirt_item", appearance.UnderShirtItem));
  583. cmd.Parameters.Add(database.CreateParameter("@undershirt_asset", appearance.UnderShirtAsset));
  584. cmd.Parameters.Add(database.CreateParameter("@underpants_item", appearance.UnderPantsItem));
  585. cmd.Parameters.Add(database.CreateParameter("@underpants_asset", appearance.UnderPantsAsset));
  586. cmd.Parameters.Add(database.CreateParameter("@skirt_item", appearance.SkirtItem));
  587. cmd.Parameters.Add(database.CreateParameter("@skirt_asset", appearance.SkirtAsset));
  588. try
  589. {
  590. cmd.ExecuteNonQuery();
  591. }
  592. catch (Exception e)
  593. {
  594. m_log.ErrorFormat("[USER DB] Error updating user appearance, error: {0}", e.Message);
  595. }
  596. }
  597. UpdateUserAttachments(user, appearance.GetAttachments());
  598. }
  599. #endregion
  600. #region Attachment methods
  601. /// <summary>
  602. /// Gets all attachment of a agent.
  603. /// </summary>
  604. /// <param name="agentID">agent ID.</param>
  605. /// <returns></returns>
  606. public Hashtable GetUserAttachments(UUID agentID)
  607. {
  608. Hashtable returnTable = new Hashtable();
  609. string sql = "select attachpoint, item, asset from avatarattachments where UUID = @uuid";
  610. using (AutoClosingSqlCommand command = database.Query(sql, database.CreateParameter("@uuid", agentID)))
  611. {
  612. using (SqlDataReader reader = command.ExecuteReader())
  613. {
  614. while (reader.Read())
  615. {
  616. int attachpoint = Convert.ToInt32(reader["attachpoint"]);
  617. if (returnTable.ContainsKey(attachpoint))
  618. continue;
  619. Hashtable item = new Hashtable();
  620. item.Add("item", reader["item"].ToString());
  621. item.Add("asset", reader["asset"].ToString());
  622. returnTable.Add(attachpoint, item);
  623. }
  624. }
  625. }
  626. return returnTable;
  627. }
  628. /// <summary>
  629. /// Updates all attachments of the agent.
  630. /// </summary>
  631. /// <param name="agentID">agentID.</param>
  632. /// <param name="data">data with all items on attachmentpoints</param>
  633. public void UpdateUserAttachments(UUID agentID, Hashtable data)
  634. {
  635. string sql = "DELETE FROM avatarattachments WHERE UUID = @uuid";
  636. using (AutoClosingSqlCommand command = database.Query(sql))
  637. {
  638. command.Parameters.Add(database.CreateParameter("uuid", agentID));
  639. command.ExecuteNonQuery();
  640. }
  641. if (data == null)
  642. return;
  643. sql = @"INSERT INTO avatarattachments (UUID, attachpoint, item, asset)
  644. VALUES (@uuid, @attachpoint, @item, @asset)";
  645. using (AutoClosingSqlCommand command = database.Query(sql))
  646. {
  647. bool firstTime = true;
  648. foreach (DictionaryEntry e in data)
  649. {
  650. int attachpoint = Convert.ToInt32(e.Key);
  651. Hashtable item = (Hashtable)e.Value;
  652. if (firstTime)
  653. {
  654. command.Parameters.Add(database.CreateParameter("@uuid", agentID));
  655. command.Parameters.Add(database.CreateParameter("@attachpoint", attachpoint));
  656. command.Parameters.Add(database.CreateParameter("@item", new UUID(item["item"].ToString())));
  657. command.Parameters.Add(database.CreateParameter("@asset", new UUID(item["asset"].ToString())));
  658. firstTime = false;
  659. }
  660. command.Parameters["@uuid"].Value = agentID.Guid; //.ToString();
  661. command.Parameters["@attachpoint"].Value = attachpoint;
  662. command.Parameters["@item"].Value = new Guid(item["item"].ToString());
  663. command.Parameters["@asset"].Value = new Guid(item["asset"].ToString());
  664. try
  665. {
  666. command.ExecuteNonQuery();
  667. }
  668. catch (Exception ex)
  669. {
  670. m_log.DebugFormat("[USER DB] : Error adding user attachment. {0}", ex.Message);
  671. }
  672. }
  673. }
  674. }
  675. /// <summary>
  676. /// Resets all attachments of a agent in the database.
  677. /// </summary>
  678. /// <param name="agentID">agentID.</param>
  679. override public void ResetAttachments(UUID agentID)
  680. {
  681. string sql = "UPDATE avatarattachments SET asset = '00000000-0000-0000-0000-000000000000' WHERE UUID = @uuid";
  682. using (AutoClosingSqlCommand command = database.Query(sql))
  683. {
  684. command.Parameters.Add(database.CreateParameter("uuid", agentID));
  685. command.ExecuteNonQuery();
  686. }
  687. }
  688. override public void LogoutUsers(UUID regionID)
  689. {
  690. }
  691. #endregion
  692. #region Other public methods
  693. /// <summary>
  694. ///
  695. /// </summary>
  696. /// <param name="queryID"></param>
  697. /// <param name="query"></param>
  698. /// <returns></returns>
  699. override public List<AvatarPickerAvatar> GeneratePickerResults(UUID queryID, string query)
  700. {
  701. List<AvatarPickerAvatar> returnlist = new List<AvatarPickerAvatar>();
  702. string[] querysplit = query.Split(' ');
  703. if (querysplit.Length == 2)
  704. {
  705. try
  706. {
  707. string sql = string.Format(@"SELECT UUID,username,lastname FROM {0}
  708. WHERE username LIKE @first AND lastname LIKE @second", m_usersTableName);
  709. using (AutoClosingSqlCommand command = database.Query(sql))
  710. {
  711. //Add wildcard to the search
  712. command.Parameters.Add(database.CreateParameter("first", querysplit[0] + "%"));
  713. command.Parameters.Add(database.CreateParameter("second", querysplit[1] + "%"));
  714. using (SqlDataReader reader = command.ExecuteReader())
  715. {
  716. while (reader.Read())
  717. {
  718. AvatarPickerAvatar user = new AvatarPickerAvatar();
  719. user.AvatarID = new UUID((Guid)reader["UUID"]);
  720. user.firstName = (string)reader["username"];
  721. user.lastName = (string)reader["lastname"];
  722. returnlist.Add(user);
  723. }
  724. }
  725. }
  726. }
  727. catch (Exception e)
  728. {
  729. m_log.Error(e.ToString());
  730. }
  731. }
  732. else if (querysplit.Length == 1)
  733. {
  734. try
  735. {
  736. string sql = string.Format(@"SELECT UUID,username,lastname FROM {0}
  737. WHERE username LIKE @first OR lastname LIKE @first", m_usersTableName);
  738. using (AutoClosingSqlCommand command = database.Query(sql))
  739. {
  740. command.Parameters.Add(database.CreateParameter("first", querysplit[0] + "%"));
  741. using (SqlDataReader reader = command.ExecuteReader())
  742. {
  743. while (reader.Read())
  744. {
  745. AvatarPickerAvatar user = new AvatarPickerAvatar();
  746. user.AvatarID = new UUID((Guid)reader["UUID"]);
  747. user.firstName = (string)reader["username"];
  748. user.lastName = (string)reader["lastname"];
  749. returnlist.Add(user);
  750. }
  751. }
  752. }
  753. }
  754. catch (Exception e)
  755. {
  756. m_log.Error(e.ToString());
  757. }
  758. }
  759. return returnlist;
  760. }
  761. /// <summary>
  762. /// Store a weblogin key
  763. /// </summary>
  764. /// <param name="AgentID">The agent UUID</param>
  765. /// <param name="WebLoginKey">the WebLogin Key</param>
  766. /// <remarks>unused ?</remarks>
  767. override public void StoreWebLoginKey(UUID AgentID, UUID WebLoginKey)
  768. {
  769. UserProfileData user = GetUserByUUID(AgentID);
  770. user.WebLoginKey = WebLoginKey;
  771. UpdateUserProfile(user);
  772. }
  773. /// <summary>
  774. /// Database provider name
  775. /// </summary>
  776. /// <returns>Provider name</returns>
  777. override public string Name
  778. {
  779. get { return "MSSQL Userdata Interface"; }
  780. }
  781. /// <summary>
  782. /// Database provider version
  783. /// </summary>
  784. /// <returns>provider version</returns>
  785. override public string Version
  786. {
  787. get { return database.getVersion(); }
  788. }
  789. #endregion
  790. #region Private functions
  791. /// <summary>
  792. /// Reads a one item from an SQL result
  793. /// </summary>
  794. /// <param name="reader">The SQL Result</param>
  795. /// <returns>the item read</returns>
  796. private static AvatarAppearance readUserAppearance(SqlDataReader reader)
  797. {
  798. try
  799. {
  800. AvatarAppearance appearance = new AvatarAppearance();
  801. appearance.Owner = new UUID((Guid)reader["owner"]);
  802. appearance.Serial = Convert.ToInt32(reader["serial"]);
  803. appearance.VisualParams = (byte[])reader["visual_params"];
  804. appearance.Texture = new Primitive.TextureEntry((byte[])reader["texture"], 0, ((byte[])reader["texture"]).Length);
  805. appearance.AvatarHeight = (float)Convert.ToDouble(reader["avatar_height"]);
  806. appearance.BodyItem = new UUID((Guid)reader["body_item"]);
  807. appearance.BodyAsset = new UUID((Guid)reader["body_asset"]);
  808. appearance.SkinItem = new UUID((Guid)reader["skin_item"]);
  809. appearance.SkinAsset = new UUID((Guid)reader["skin_asset"]);
  810. appearance.HairItem = new UUID((Guid)reader["hair_item"]);
  811. appearance.HairAsset = new UUID((Guid)reader["hair_asset"]);
  812. appearance.EyesItem = new UUID((Guid)reader["eyes_item"]);
  813. appearance.EyesAsset = new UUID((Guid)reader["eyes_asset"]);
  814. appearance.ShirtItem = new UUID((Guid)reader["shirt_item"]);
  815. appearance.ShirtAsset = new UUID((Guid)reader["shirt_asset"]);
  816. appearance.PantsItem = new UUID((Guid)reader["pants_item"]);
  817. appearance.PantsAsset = new UUID((Guid)reader["pants_asset"]);
  818. appearance.ShoesItem = new UUID((Guid)reader["shoes_item"]);
  819. appearance.ShoesAsset = new UUID((Guid)reader["shoes_asset"]);
  820. appearance.SocksItem = new UUID((Guid)reader["socks_item"]);
  821. appearance.SocksAsset = new UUID((Guid)reader["socks_asset"]);
  822. appearance.JacketItem = new UUID((Guid)reader["jacket_item"]);
  823. appearance.JacketAsset = new UUID((Guid)reader["jacket_asset"]);
  824. appearance.GlovesItem = new UUID((Guid)reader["gloves_item"]);
  825. appearance.GlovesAsset = new UUID((Guid)reader["gloves_asset"]);
  826. appearance.UnderShirtItem = new UUID((Guid)reader["undershirt_item"]);
  827. appearance.UnderShirtAsset = new UUID((Guid)reader["undershirt_asset"]);
  828. appearance.UnderPantsItem = new UUID((Guid)reader["underpants_item"]);
  829. appearance.UnderPantsAsset = new UUID((Guid)reader["underpants_asset"]);
  830. appearance.SkirtItem = new UUID((Guid)reader["skirt_item"]);
  831. appearance.SkirtAsset = new UUID((Guid)reader["skirt_asset"]);
  832. return appearance;
  833. }
  834. catch (SqlException e)
  835. {
  836. m_log.Error(e.ToString());
  837. }
  838. return null;
  839. }
  840. /// <summary>
  841. /// Insert/Update a agent row in the DB.
  842. /// </summary>
  843. /// <param name="agentdata">agentdata.</param>
  844. private void InsertUpdateAgentRow(UserAgentData agentdata)
  845. {
  846. string sql = @"
  847. IF EXISTS (SELECT * FROM agents WHERE UUID = @UUID)
  848. BEGIN
  849. UPDATE agents SET UUID = @UUID, sessionID = @sessionID, secureSessionID = @secureSessionID, agentIP = @agentIP, agentPort = @agentPort, agentOnline = @agentOnline, loginTime = @loginTime, logoutTime = @logoutTime, currentRegion = @currentRegion, currentHandle = @currentHandle, currentPos = @currentPos
  850. WHERE UUID = @UUID
  851. END
  852. ELSE
  853. BEGIN
  854. INSERT INTO
  855. agents (UUID, sessionID, secureSessionID, agentIP, agentPort, agentOnline, loginTime, logoutTime, currentRegion, currentHandle, currentPos) VALUES
  856. (@UUID, @sessionID, @secureSessionID, @agentIP, @agentPort, @agentOnline, @loginTime, @logoutTime, @currentRegion, @currentHandle, @currentPos)
  857. END
  858. ";
  859. using (AutoClosingSqlCommand command = database.Query(sql))
  860. {
  861. command.Parameters.Add(database.CreateParameter("@UUID", agentdata.ProfileID));
  862. command.Parameters.Add(database.CreateParameter("@sessionID", agentdata.SessionID));
  863. command.Parameters.Add(database.CreateParameter("@secureSessionID", agentdata.SecureSessionID));
  864. command.Parameters.Add(database.CreateParameter("@agentIP", agentdata.AgentIP));
  865. command.Parameters.Add(database.CreateParameter("@agentPort", agentdata.AgentPort));
  866. command.Parameters.Add(database.CreateParameter("@agentOnline", agentdata.AgentOnline));
  867. command.Parameters.Add(database.CreateParameter("@loginTime", agentdata.LoginTime));
  868. command.Parameters.Add(database.CreateParameter("@logoutTime", agentdata.LogoutTime));
  869. command.Parameters.Add(database.CreateParameter("@currentRegion", agentdata.Region));
  870. command.Parameters.Add(database.CreateParameter("@currentHandle", agentdata.Handle));
  871. command.Parameters.Add(database.CreateParameter("@currentPos", "<" + ((int)agentdata.Position.X) + "," + ((int)agentdata.Position.Y) + "," + ((int)agentdata.Position.Z) + ">"));
  872. command.Transaction = command.Connection.BeginTransaction(IsolationLevel.Serializable);
  873. try
  874. {
  875. if (command.ExecuteNonQuery() > 0)
  876. {
  877. command.Transaction.Commit();
  878. return;
  879. }
  880. command.Transaction.Rollback();
  881. return;
  882. }
  883. catch (Exception e)
  884. {
  885. command.Transaction.Rollback();
  886. m_log.Error(e.ToString());
  887. return;
  888. }
  889. }
  890. }
  891. /// <summary>
  892. /// Reads an agent row from a database reader
  893. /// </summary>
  894. /// <param name="reader">An active database reader</param>
  895. /// <returns>A user session agent</returns>
  896. private UserAgentData readAgentRow(SqlDataReader reader)
  897. {
  898. UserAgentData retval = new UserAgentData();
  899. if (reader.Read())
  900. {
  901. // Agent IDs
  902. retval.ProfileID = new UUID((Guid)reader["UUID"]);
  903. retval.SessionID = new UUID((Guid)reader["sessionID"]);
  904. retval.SecureSessionID = new UUID((Guid)reader["secureSessionID"]);
  905. // Agent Who?
  906. retval.AgentIP = (string)reader["agentIP"];
  907. retval.AgentPort = Convert.ToUInt32(reader["agentPort"].ToString());
  908. retval.AgentOnline = Convert.ToInt32(reader["agentOnline"].ToString()) != 0;
  909. // Login/Logout times (UNIX Epoch)
  910. retval.LoginTime = Convert.ToInt32(reader["loginTime"].ToString());
  911. retval.LogoutTime = Convert.ToInt32(reader["logoutTime"].ToString());
  912. // Current position
  913. retval.Region = new UUID((Guid)reader["currentRegion"]);
  914. retval.Handle = Convert.ToUInt64(reader["currentHandle"].ToString());
  915. Vector3 tmp_v;
  916. Vector3.TryParse((string)reader["currentPos"], out tmp_v);
  917. retval.Position = tmp_v;
  918. }
  919. else
  920. {
  921. return null;
  922. }
  923. return retval;
  924. }
  925. /// <summary>
  926. /// Creates a new user and inserts it into the database
  927. /// </summary>
  928. /// <param name="uuid">User ID</param>
  929. /// <param name="username">First part of the login</param>
  930. /// <param name="lastname">Second part of the login</param>
  931. /// <param name="email">Email of person</param>
  932. /// <param name="passwordHash">A salted hash of the users password</param>
  933. /// <param name="passwordSalt">The salt used for the password hash</param>
  934. /// <param name="homeRegion">A regionHandle of the users home region</param>
  935. /// <param name="homeLocX">Home region position vector</param>
  936. /// <param name="homeLocY">Home region position vector</param>
  937. /// <param name="homeLocZ">Home region position vector</param>
  938. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  939. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  940. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  941. /// <param name="created">Account created (unix timestamp)</param>
  942. /// <param name="lastlogin">Last login (unix timestamp)</param>
  943. /// <param name="inventoryURI">Users inventory URI</param>
  944. /// <param name="assetURI">Users asset URI</param>
  945. /// <param name="canDoMask">I can do mask</param>
  946. /// <param name="wantDoMask">I want to do mask</param>
  947. /// <param name="aboutText">Profile text</param>
  948. /// <param name="firstText">Firstlife text</param>
  949. /// <param name="profileImage">UUID for profile image</param>
  950. /// <param name="firstImage">UUID for firstlife image</param>
  951. /// <param name="webLoginKey">web login key</param>
  952. /// <param name="homeRegionID">homeregion UUID</param>
  953. /// <param name="godLevel">has the user godlevel</param>
  954. /// <param name="userFlags">unknown</param>
  955. /// <param name="customType">unknown</param>
  956. /// <param name="partnerID">UUID of partner</param>
  957. /// <returns>Success?</returns>
  958. private void InsertUserRow(UUID uuid, string username, string lastname, string email, string passwordHash,
  959. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  960. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  961. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  962. string aboutText, string firstText,
  963. UUID profileImage, UUID firstImage, UUID webLoginKey, UUID homeRegionID,
  964. int godLevel, int userFlags, string customType, UUID partnerID)
  965. {
  966. string sql = string.Format(@"INSERT INTO {0}
  967. ([UUID], [username], [lastname], [email], [passwordHash], [passwordSalt],
  968. [homeRegion], [homeLocationX], [homeLocationY], [homeLocationZ], [homeLookAtX],
  969. [homeLookAtY], [homeLookAtZ], [created], [lastLogin], [userInventoryURI],
  970. [userAssetURI], [profileCanDoMask], [profileWantDoMask], [profileAboutText],
  971. [profileFirstText], [profileImage], [profileFirstImage], [webLoginKey],
  972. [homeRegionID], [userFlags], [godLevel], [customType], [partner])
  973. VALUES
  974. (@UUID, @username, @lastname, @email, @passwordHash, @passwordSalt,
  975. @homeRegion, @homeLocationX, @homeLocationY, @homeLocationZ, @homeLookAtX,
  976. @homeLookAtY, @homeLookAtZ, @created, @lastLogin, @userInventoryURI,
  977. @userAssetURI, @profileCanDoMask, @profileWantDoMask, @profileAboutText,
  978. @profileFirstText, @profileImage, @profileFirstImage, @webLoginKey,
  979. @homeRegionID, @userFlags, @godLevel, @customType, @partner)", m_usersTableName);
  980. try
  981. {
  982. using (AutoClosingSqlCommand command = database.Query(sql))
  983. {
  984. command.Parameters.Add(database.CreateParameter("UUID", uuid));
  985. command.Parameters.Add(database.CreateParameter("username", username));
  986. command.Parameters.Add(database.CreateParameter("lastname", lastname));
  987. command.Parameters.Add(database.CreateParameter("email", email));
  988. command.Parameters.Add(database.CreateParameter("passwordHash", passwordHash));
  989. command.Parameters.Add(database.CreateParameter("passwordSalt", passwordSalt));
  990. command.Parameters.Add(database.CreateParameter("homeRegion", homeRegion));
  991. command.Parameters.Add(database.CreateParameter("homeLocationX", homeLocX));
  992. command.Parameters.Add(database.CreateParameter("homeLocationY", homeLocY));
  993. command.Parameters.Add(database.CreateParameter("homeLocationZ", homeLocZ));
  994. command.Parameters.Add(database.CreateParameter("homeLookAtX", homeLookAtX));
  995. command.Parameters.Add(database.CreateParameter("homeLookAtY", homeLookAtY));
  996. command.Parameters.Add(database.CreateParameter("homeLookAtZ", homeLookAtZ));
  997. command.Parameters.Add(database.CreateParameter("created", created));
  998. command.Parameters.Add(database.CreateParameter("lastLogin", lastlogin));
  999. command.Parameters.Add(database.CreateParameter("userInventoryURI", inventoryURI));
  1000. command.Parameters.Add(database.CreateParameter("userAssetURI", assetURI));
  1001. command.Parameters.Add(database.CreateParameter("profileCanDoMask", canDoMask));
  1002. command.Parameters.Add(database.CreateParameter("profileWantDoMask", wantDoMask));
  1003. command.Parameters.Add(database.CreateParameter("profileAboutText", aboutText));
  1004. command.Parameters.Add(database.CreateParameter("profileFirstText", firstText));
  1005. command.Parameters.Add(database.CreateParameter("profileImage", profileImage));
  1006. command.Parameters.Add(database.CreateParameter("profileFirstImage", firstImage));
  1007. command.Parameters.Add(database.CreateParameter("webLoginKey", webLoginKey));
  1008. command.Parameters.Add(database.CreateParameter("homeRegionID", homeRegionID));
  1009. command.Parameters.Add(database.CreateParameter("userFlags", userFlags));
  1010. command.Parameters.Add(database.CreateParameter("godLevel", godLevel));
  1011. command.Parameters.Add(database.CreateParameter("customType", customType));
  1012. command.Parameters.Add(database.CreateParameter("partner", partnerID));
  1013. command.ExecuteNonQuery();
  1014. return;
  1015. }
  1016. }
  1017. catch (Exception e)
  1018. {
  1019. m_log.Error(e.ToString());
  1020. return;
  1021. }
  1022. }
  1023. /// <summary>
  1024. /// Reads a user profile from an active data reader
  1025. /// </summary>
  1026. /// <param name="reader">An active database reader</param>
  1027. /// <returns>A user profile</returns>
  1028. private static UserProfileData ReadUserRow(SqlDataReader reader)
  1029. {
  1030. UserProfileData retval = new UserProfileData();
  1031. if (reader.Read())
  1032. {
  1033. retval.ID = new UUID((Guid)reader["UUID"]);
  1034. retval.FirstName = (string)reader["username"];
  1035. retval.SurName = (string)reader["lastname"];
  1036. if (reader.IsDBNull(reader.GetOrdinal("email")))
  1037. retval.Email = "";
  1038. else
  1039. retval.Email = (string)reader["email"];
  1040. retval.PasswordHash = (string)reader["passwordHash"];
  1041. retval.PasswordSalt = (string)reader["passwordSalt"];
  1042. retval.HomeRegion = Convert.ToUInt64(reader["homeRegion"].ToString());
  1043. retval.HomeLocation = new Vector3(
  1044. Convert.ToSingle(reader["homeLocationX"].ToString()),
  1045. Convert.ToSingle(reader["homeLocationY"].ToString()),
  1046. Convert.ToSingle(reader["homeLocationZ"].ToString()));
  1047. retval.HomeLookAt = new Vector3(
  1048. Convert.ToSingle(reader["homeLookAtX"].ToString()),
  1049. Convert.ToSingle(reader["homeLookAtY"].ToString()),
  1050. Convert.ToSingle(reader["homeLookAtZ"].ToString()));
  1051. if (reader.IsDBNull(reader.GetOrdinal("homeRegionID")))
  1052. retval.HomeRegionID = UUID.Zero;
  1053. else
  1054. retval.HomeRegionID = new UUID((Guid)reader["homeRegionID"]);
  1055. retval.Created = Convert.ToInt32(reader["created"].ToString());
  1056. retval.LastLogin = Convert.ToInt32(reader["lastLogin"].ToString());
  1057. if (reader.IsDBNull(reader.GetOrdinal("userInventoryURI")))
  1058. retval.UserInventoryURI = "";
  1059. else
  1060. retval.UserInventoryURI = (string)reader["userInventoryURI"];
  1061. if (reader.IsDBNull(reader.GetOrdinal("userAssetURI")))
  1062. retval.UserAssetURI = "";
  1063. else
  1064. retval.UserAssetURI = (string)reader["userAssetURI"];
  1065. retval.CanDoMask = Convert.ToUInt32(reader["profileCanDoMask"].ToString());
  1066. retval.WantDoMask = Convert.ToUInt32(reader["profileWantDoMask"].ToString());
  1067. if (reader.IsDBNull(reader.GetOrdinal("profileAboutText")))
  1068. retval.AboutText = "";
  1069. else
  1070. retval.AboutText = (string)reader["profileAboutText"];
  1071. if (reader.IsDBNull(reader.GetOrdinal("profileFirstText")))
  1072. retval.FirstLifeAboutText = "";
  1073. else
  1074. retval.FirstLifeAboutText = (string)reader["profileFirstText"];
  1075. if (reader.IsDBNull(reader.GetOrdinal("profileImage")))
  1076. retval.Image = UUID.Zero;
  1077. else
  1078. retval.Image = new UUID((Guid)reader["profileImage"]);
  1079. if (reader.IsDBNull(reader.GetOrdinal("profileFirstImage")))
  1080. retval.Image = UUID.Zero;
  1081. else
  1082. retval.FirstLifeImage = new UUID((Guid)reader["profileFirstImage"]);
  1083. if (reader.IsDBNull(reader.GetOrdinal("webLoginKey")))
  1084. retval.WebLoginKey = UUID.Zero;
  1085. else
  1086. retval.WebLoginKey = new UUID((Guid)reader["webLoginKey"]);
  1087. retval.UserFlags = Convert.ToInt32(reader["userFlags"].ToString());
  1088. retval.GodLevel = Convert.ToInt32(reader["godLevel"].ToString());
  1089. if (reader.IsDBNull(reader.GetOrdinal("customType")))
  1090. retval.CustomType = "";
  1091. else
  1092. retval.CustomType = reader["customType"].ToString();
  1093. if (reader.IsDBNull(reader.GetOrdinal("partner")))
  1094. retval.Partner = UUID.Zero;
  1095. else
  1096. retval.Partner = new UUID((Guid)reader["partner"]);
  1097. }
  1098. else
  1099. {
  1100. return null;
  1101. }
  1102. return retval;
  1103. }
  1104. #endregion
  1105. }
  1106. }