XInventoryConnector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Services.Interfaces;
  37. using OpenSim.Server.Base;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class XInventoryServicesConnector : IInventoryService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. private object m_Lock = new object();
  48. public XInventoryServicesConnector()
  49. {
  50. }
  51. public XInventoryServicesConnector(string serverURI)
  52. {
  53. m_ServerURI = serverURI.TrimEnd('/');
  54. }
  55. public XInventoryServicesConnector(IConfigSource source)
  56. {
  57. Initialise(source);
  58. }
  59. public virtual void Initialise(IConfigSource source)
  60. {
  61. IConfig assetConfig = source.Configs["InventoryService"];
  62. if (assetConfig == null)
  63. {
  64. m_log.Error("[INVENTORY CONNECTOR]: InventoryService missing from OpenSim.ini");
  65. throw new Exception("Inventory connector init error");
  66. }
  67. string serviceURI = assetConfig.GetString("InventoryServerURI",
  68. String.Empty);
  69. if (serviceURI == String.Empty)
  70. {
  71. m_log.Error("[INVENTORY CONNECTOR]: No Server URI named in section InventoryService");
  72. throw new Exception("Inventory connector init error");
  73. }
  74. m_ServerURI = serviceURI;
  75. }
  76. public bool CreateUserInventory(UUID principalID)
  77. {
  78. Dictionary<string,object> ret = MakeRequest("CREATEUSERINVENTORY",
  79. new Dictionary<string,object> {
  80. { "PRINCIPAL", principalID.ToString() }
  81. });
  82. if (ret == null)
  83. return false;
  84. if (ret.Count == 0)
  85. return false;
  86. return bool.Parse(ret["RESULT"].ToString());
  87. }
  88. public List<InventoryFolderBase> GetInventorySkeleton(UUID principalID)
  89. {
  90. Dictionary<string,object> ret = MakeRequest("GETINVENTORYSKELETON",
  91. new Dictionary<string,object> {
  92. { "PRINCIPAL", principalID.ToString() }
  93. });
  94. if (ret == null)
  95. return null;
  96. if (ret.Count == 0)
  97. return null;
  98. List<InventoryFolderBase> folders = new List<InventoryFolderBase>();
  99. try
  100. {
  101. foreach (Object o in ret.Values)
  102. folders.Add(BuildFolder((Dictionary<string, object>)o));
  103. }
  104. catch (Exception e)
  105. {
  106. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception unwrapping folder list: {0}", e.Message);
  107. }
  108. return folders;
  109. }
  110. public InventoryFolderBase GetRootFolder(UUID principalID)
  111. {
  112. Dictionary<string,object> ret = MakeRequest("GETROOTFOLDER",
  113. new Dictionary<string,object> {
  114. { "PRINCIPAL", principalID.ToString() }
  115. });
  116. if (ret == null)
  117. return null;
  118. if (ret.Count == 0)
  119. return null;
  120. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  121. }
  122. public InventoryFolderBase GetFolderForType(UUID principalID, AssetType type)
  123. {
  124. Dictionary<string,object> ret = MakeRequest("GETFOLDERFORTYPE",
  125. new Dictionary<string,object> {
  126. { "PRINCIPAL", principalID.ToString() },
  127. { "TYPE", ((int)type).ToString() }
  128. });
  129. if (ret == null)
  130. return null;
  131. if (ret.Count == 0)
  132. return null;
  133. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  134. }
  135. public InventoryCollection GetFolderContent(UUID principalID, UUID folderID)
  136. {
  137. InventoryCollection inventory = new InventoryCollection();
  138. inventory.Folders = new List<InventoryFolderBase>();
  139. inventory.Items = new List<InventoryItemBase>();
  140. inventory.UserID = principalID;
  141. try
  142. {
  143. Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
  144. new Dictionary<string,object> {
  145. { "PRINCIPAL", principalID.ToString() },
  146. { "FOLDER", folderID.ToString() }
  147. });
  148. if (ret == null)
  149. return null;
  150. if (ret.Count == 0)
  151. return null;
  152. Dictionary<string,object> folders =
  153. (Dictionary<string,object>)ret["FOLDERS"];
  154. Dictionary<string,object> items =
  155. (Dictionary<string,object>)ret["ITEMS"];
  156. foreach (Object o in folders.Values) // getting the values directly, we don't care about the keys folder_i
  157. inventory.Folders.Add(BuildFolder((Dictionary<string, object>)o));
  158. foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
  159. inventory.Items.Add(BuildItem((Dictionary<string, object>)o));
  160. }
  161. catch (Exception e)
  162. {
  163. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception in GetFolderContent: {0}", e.Message);
  164. }
  165. return inventory;
  166. }
  167. public List<InventoryItemBase> GetFolderItems(UUID principalID, UUID folderID)
  168. {
  169. Dictionary<string,object> ret = MakeRequest("GETFOLDERITEMS",
  170. new Dictionary<string,object> {
  171. { "PRINCIPAL", principalID.ToString() },
  172. { "FOLDER", folderID.ToString() }
  173. });
  174. if (ret == null)
  175. return null;
  176. if (ret.Count == 0)
  177. return null;
  178. Dictionary<string, object> items = (Dictionary<string, object>)ret["ITEMS"];
  179. List<InventoryItemBase> fitems = new List<InventoryItemBase>();
  180. foreach (Object o in items.Values) // getting the values directly, we don't care about the keys item_i
  181. fitems.Add(BuildItem((Dictionary<string, object>)o));
  182. return fitems;
  183. }
  184. public bool AddFolder(InventoryFolderBase folder)
  185. {
  186. Dictionary<string,object> ret = MakeRequest("ADDFOLDER",
  187. new Dictionary<string,object> {
  188. { "ParentID", folder.ParentID.ToString() },
  189. { "Type", folder.Type.ToString() },
  190. { "Version", folder.Version.ToString() },
  191. { "Name", folder.Name.ToString() },
  192. { "Owner", folder.Owner.ToString() },
  193. { "ID", folder.ID.ToString() }
  194. });
  195. if (ret == null)
  196. return false;
  197. return bool.Parse(ret["RESULT"].ToString());
  198. }
  199. public bool UpdateFolder(InventoryFolderBase folder)
  200. {
  201. Dictionary<string,object> ret = MakeRequest("UPDATEFOLDER",
  202. new Dictionary<string,object> {
  203. { "ParentID", folder.ParentID.ToString() },
  204. { "Type", folder.Type.ToString() },
  205. { "Version", folder.Version.ToString() },
  206. { "Name", folder.Name.ToString() },
  207. { "Owner", folder.Owner.ToString() },
  208. { "ID", folder.ID.ToString() }
  209. });
  210. if (ret == null)
  211. return false;
  212. return bool.Parse(ret["RESULT"].ToString());
  213. }
  214. public bool MoveFolder(InventoryFolderBase folder)
  215. {
  216. Dictionary<string,object> ret = MakeRequest("MOVEFOLDER",
  217. new Dictionary<string,object> {
  218. { "ParentID", folder.ParentID.ToString() },
  219. { "ID", folder.ID.ToString() },
  220. { "PRINCIPAL", folder.Owner.ToString() }
  221. });
  222. if (ret == null)
  223. return false;
  224. return bool.Parse(ret["RESULT"].ToString());
  225. }
  226. public bool DeleteFolders(UUID principalID, List<UUID> folderIDs)
  227. {
  228. List<string> slist = new List<string>();
  229. foreach (UUID f in folderIDs)
  230. slist.Add(f.ToString());
  231. Dictionary<string,object> ret = MakeRequest("DELETEFOLDERS",
  232. new Dictionary<string,object> {
  233. { "PRINCIPAL", principalID.ToString() },
  234. { "FOLDERS", slist }
  235. });
  236. if (ret == null)
  237. return false;
  238. return bool.Parse(ret["RESULT"].ToString());
  239. }
  240. public bool PurgeFolder(InventoryFolderBase folder)
  241. {
  242. Dictionary<string,object> ret = MakeRequest("PURGEFOLDER",
  243. new Dictionary<string,object> {
  244. { "ID", folder.ID.ToString() }
  245. });
  246. if (ret == null)
  247. return false;
  248. return bool.Parse(ret["RESULT"].ToString());
  249. }
  250. public bool AddItem(InventoryItemBase item)
  251. {
  252. if (item.CreatorData == null)
  253. item.CreatorData = String.Empty;
  254. Dictionary<string,object> ret = MakeRequest("ADDITEM",
  255. new Dictionary<string,object> {
  256. { "AssetID", item.AssetID.ToString() },
  257. { "AssetType", item.AssetType.ToString() },
  258. { "Name", item.Name.ToString() },
  259. { "Owner", item.Owner.ToString() },
  260. { "ID", item.ID.ToString() },
  261. { "InvType", item.InvType.ToString() },
  262. { "Folder", item.Folder.ToString() },
  263. { "CreatorId", item.CreatorId.ToString() },
  264. { "CreatorData", item.CreatorData.ToString() },
  265. { "Description", item.Description.ToString() },
  266. { "NextPermissions", item.NextPermissions.ToString() },
  267. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  268. { "BasePermissions", item.BasePermissions.ToString() },
  269. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  270. { "GroupPermissions", item.GroupPermissions.ToString() },
  271. { "GroupID", item.GroupID.ToString() },
  272. { "GroupOwned", item.GroupOwned.ToString() },
  273. { "SalePrice", item.SalePrice.ToString() },
  274. { "SaleType", item.SaleType.ToString() },
  275. { "Flags", item.Flags.ToString() },
  276. { "CreationDate", item.CreationDate.ToString() }
  277. });
  278. if (ret == null)
  279. return false;
  280. return bool.Parse(ret["RESULT"].ToString());
  281. }
  282. public bool UpdateItem(InventoryItemBase item)
  283. {
  284. if (item.CreatorData == null)
  285. item.CreatorData = String.Empty;
  286. Dictionary<string,object> ret = MakeRequest("UPDATEITEM",
  287. new Dictionary<string,object> {
  288. { "AssetID", item.AssetID.ToString() },
  289. { "AssetType", item.AssetType.ToString() },
  290. { "Name", item.Name.ToString() },
  291. { "Owner", item.Owner.ToString() },
  292. { "ID", item.ID.ToString() },
  293. { "InvType", item.InvType.ToString() },
  294. { "Folder", item.Folder.ToString() },
  295. { "CreatorId", item.CreatorId.ToString() },
  296. { "CreatorData", item.CreatorData.ToString() },
  297. { "Description", item.Description.ToString() },
  298. { "NextPermissions", item.NextPermissions.ToString() },
  299. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  300. { "BasePermissions", item.BasePermissions.ToString() },
  301. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  302. { "GroupPermissions", item.GroupPermissions.ToString() },
  303. { "GroupID", item.GroupID.ToString() },
  304. { "GroupOwned", item.GroupOwned.ToString() },
  305. { "SalePrice", item.SalePrice.ToString() },
  306. { "SaleType", item.SaleType.ToString() },
  307. { "Flags", item.Flags.ToString() },
  308. { "CreationDate", item.CreationDate.ToString() }
  309. });
  310. if (ret == null)
  311. return false;
  312. return bool.Parse(ret["RESULT"].ToString());
  313. }
  314. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  315. {
  316. List<string> idlist = new List<string>();
  317. List<string> destlist = new List<string>();
  318. foreach (InventoryItemBase item in items)
  319. {
  320. idlist.Add(item.ID.ToString());
  321. destlist.Add(item.Folder.ToString());
  322. }
  323. Dictionary<string,object> ret = MakeRequest("MOVEITEMS",
  324. new Dictionary<string,object> {
  325. { "PRINCIPAL", principalID.ToString() },
  326. { "IDLIST", idlist },
  327. { "DESTLIST", destlist }
  328. });
  329. if (ret == null)
  330. return false;
  331. return bool.Parse(ret["RESULT"].ToString());
  332. }
  333. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  334. {
  335. List<string> slist = new List<string>();
  336. foreach (UUID f in itemIDs)
  337. slist.Add(f.ToString());
  338. Dictionary<string,object> ret = MakeRequest("DELETEITEMS",
  339. new Dictionary<string,object> {
  340. { "PRINCIPAL", principalID.ToString() },
  341. { "ITEMS", slist }
  342. });
  343. if (ret == null)
  344. return false;
  345. return bool.Parse(ret["RESULT"].ToString());
  346. }
  347. public InventoryItemBase GetItem(InventoryItemBase item)
  348. {
  349. try
  350. {
  351. Dictionary<string, object> ret = MakeRequest("GETITEM",
  352. new Dictionary<string, object> {
  353. { "ID", item.ID.ToString() }
  354. });
  355. if (ret == null)
  356. return null;
  357. if (ret.Count == 0)
  358. return null;
  359. return BuildItem((Dictionary<string, object>)ret["item"]);
  360. }
  361. catch (Exception e)
  362. {
  363. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception in GetItem: {0}", e.Message);
  364. }
  365. return null;
  366. }
  367. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  368. {
  369. try
  370. {
  371. Dictionary<string, object> ret = MakeRequest("GETFOLDER",
  372. new Dictionary<string, object> {
  373. { "ID", folder.ID.ToString() }
  374. });
  375. if (ret == null)
  376. return null;
  377. if (ret.Count == 0)
  378. return null;
  379. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  380. }
  381. catch (Exception e)
  382. {
  383. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception in GetFolder: {0}", e.Message);
  384. }
  385. return null;
  386. }
  387. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  388. {
  389. Dictionary<string,object> ret = MakeRequest("GETACTIVEGESTURES",
  390. new Dictionary<string,object> {
  391. { "PRINCIPAL", principalID.ToString() }
  392. });
  393. if (ret == null)
  394. return null;
  395. List<InventoryItemBase> items = new List<InventoryItemBase>();
  396. foreach (Object o in ret.Values) // getting the values directly, we don't care about the keys item_i
  397. items.Add(BuildItem((Dictionary<string, object>)o));
  398. return items;
  399. }
  400. public int GetAssetPermissions(UUID principalID, UUID assetID)
  401. {
  402. Dictionary<string,object> ret = MakeRequest("GETASSETPERMISSIONS",
  403. new Dictionary<string,object> {
  404. { "PRINCIPAL", principalID.ToString() },
  405. { "ASSET", assetID.ToString() }
  406. });
  407. if (ret == null)
  408. return 0;
  409. return int.Parse(ret["RESULT"].ToString());
  410. }
  411. // These are either obsolete or unused
  412. //
  413. public InventoryCollection GetUserInventory(UUID principalID)
  414. {
  415. return null;
  416. }
  417. public void GetUserInventory(UUID principalID, InventoryReceiptCallback callback)
  418. {
  419. }
  420. public bool HasInventoryForUser(UUID principalID)
  421. {
  422. return false;
  423. }
  424. // Helpers
  425. //
  426. private Dictionary<string,object> MakeRequest(string method,
  427. Dictionary<string,object> sendData)
  428. {
  429. sendData["METHOD"] = method;
  430. string reply = string.Empty;
  431. lock (m_Lock)
  432. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  433. m_ServerURI + "/xinventory",
  434. ServerUtils.BuildQueryString(sendData));
  435. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
  436. reply);
  437. return replyData;
  438. }
  439. private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
  440. {
  441. InventoryFolderBase folder = new InventoryFolderBase();
  442. try
  443. {
  444. folder.ParentID = new UUID(data["ParentID"].ToString());
  445. folder.Type = short.Parse(data["Type"].ToString());
  446. folder.Version = ushort.Parse(data["Version"].ToString());
  447. folder.Name = data["Name"].ToString();
  448. folder.Owner = new UUID(data["Owner"].ToString());
  449. folder.ID = new UUID(data["ID"].ToString());
  450. }
  451. catch (Exception e)
  452. {
  453. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception building folder: {0}", e.Message);
  454. }
  455. return folder;
  456. }
  457. private InventoryItemBase BuildItem(Dictionary<string,object> data)
  458. {
  459. InventoryItemBase item = new InventoryItemBase();
  460. try
  461. {
  462. item.AssetID = new UUID(data["AssetID"].ToString());
  463. item.AssetType = int.Parse(data["AssetType"].ToString());
  464. item.Name = data["Name"].ToString();
  465. item.Owner = new UUID(data["Owner"].ToString());
  466. item.ID = new UUID(data["ID"].ToString());
  467. item.InvType = int.Parse(data["InvType"].ToString());
  468. item.Folder = new UUID(data["Folder"].ToString());
  469. item.CreatorId = data["CreatorId"].ToString();
  470. if (data.ContainsKey("CreatorData"))
  471. item.CreatorData = data["CreatorData"].ToString();
  472. else
  473. item.CreatorData = String.Empty;
  474. item.Description = data["Description"].ToString();
  475. item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
  476. item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
  477. item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
  478. item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
  479. item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
  480. item.GroupID = new UUID(data["GroupID"].ToString());
  481. item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
  482. item.SalePrice = int.Parse(data["SalePrice"].ToString());
  483. item.SaleType = byte.Parse(data["SaleType"].ToString());
  484. item.Flags = uint.Parse(data["Flags"].ToString());
  485. item.CreationDate = int.Parse(data["CreationDate"].ToString());
  486. }
  487. catch (Exception e)
  488. {
  489. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception building item: {0}", e.Message);
  490. }
  491. return item;
  492. }
  493. }
  494. }