XInventoryConnector.cs 22 KB

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