XInventoryConnector.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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.Framework.Servers.HttpServer;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Server.Base;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.Connectors
  41. {
  42. public class XInventoryServicesConnector : IInventoryService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private string m_ServerURI = String.Empty;
  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. try
  139. {
  140. Dictionary<string,object> ret = MakeRequest("GETFOLDERCONTENT",
  141. new Dictionary<string,object> {
  142. { "PRINCIPAL", principalID.ToString() },
  143. { "FOLDER", folderID.ToString() }
  144. });
  145. if (ret == null)
  146. return null;
  147. if (ret.Count == 0)
  148. return null;
  149. inventory.Folders = new List<InventoryFolderBase>();
  150. inventory.Items = new List<InventoryItemBase>();
  151. inventory.UserID = principalID;
  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. Dictionary<string,object> ret = MakeRequest("ADDITEM",
  253. new Dictionary<string,object> {
  254. { "AssetID", item.AssetID.ToString() },
  255. { "AssetType", item.AssetType.ToString() },
  256. { "Name", item.Name.ToString() },
  257. { "Owner", item.Owner.ToString() },
  258. { "ID", item.ID.ToString() },
  259. { "InvType", item.InvType.ToString() },
  260. { "Folder", item.Folder.ToString() },
  261. { "CreatorId", item.CreatorId.ToString() },
  262. { "Description", item.Description.ToString() },
  263. { "NextPermissions", item.NextPermissions.ToString() },
  264. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  265. { "BasePermissions", item.BasePermissions.ToString() },
  266. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  267. { "GroupPermissions", item.GroupPermissions.ToString() },
  268. { "GroupID", item.GroupID.ToString() },
  269. { "GroupOwned", item.GroupOwned.ToString() },
  270. { "SalePrice", item.SalePrice.ToString() },
  271. { "SaleType", item.SaleType.ToString() },
  272. { "Flags", item.Flags.ToString() },
  273. { "CreationDate", item.CreationDate.ToString() }
  274. });
  275. if (ret == null)
  276. return false;
  277. return bool.Parse(ret["RESULT"].ToString());
  278. }
  279. public bool UpdateItem(InventoryItemBase item)
  280. {
  281. Dictionary<string,object> ret = MakeRequest("UPDATEITEM",
  282. new Dictionary<string,object> {
  283. { "AssetID", item.AssetID.ToString() },
  284. { "AssetType", item.AssetType.ToString() },
  285. { "Name", item.Name.ToString() },
  286. { "Owner", item.Owner.ToString() },
  287. { "ID", item.ID.ToString() },
  288. { "InvType", item.InvType.ToString() },
  289. { "Folder", item.Folder.ToString() },
  290. { "CreatorId", item.CreatorId.ToString() },
  291. { "Description", item.Description.ToString() },
  292. { "NextPermissions", item.NextPermissions.ToString() },
  293. { "CurrentPermissions", item.CurrentPermissions.ToString() },
  294. { "BasePermissions", item.BasePermissions.ToString() },
  295. { "EveryOnePermissions", item.EveryOnePermissions.ToString() },
  296. { "GroupPermissions", item.GroupPermissions.ToString() },
  297. { "GroupID", item.GroupID.ToString() },
  298. { "GroupOwned", item.GroupOwned.ToString() },
  299. { "SalePrice", item.SalePrice.ToString() },
  300. { "SaleType", item.SaleType.ToString() },
  301. { "Flags", item.Flags.ToString() },
  302. { "CreationDate", item.CreationDate.ToString() }
  303. });
  304. if (ret == null)
  305. return false;
  306. return bool.Parse(ret["RESULT"].ToString());
  307. }
  308. public bool MoveItems(UUID principalID, List<InventoryItemBase> items)
  309. {
  310. List<string> idlist = new List<string>();
  311. List<string> destlist = new List<string>();
  312. foreach (InventoryItemBase item in items)
  313. {
  314. idlist.Add(item.ID.ToString());
  315. destlist.Add(item.Folder.ToString());
  316. }
  317. Dictionary<string,object> ret = MakeRequest("MOVEITEMS",
  318. new Dictionary<string,object> {
  319. { "PRINCIPAL", principalID.ToString() },
  320. { "IDLIST", idlist },
  321. { "DESTLIST", destlist }
  322. });
  323. if (ret == null)
  324. return false;
  325. return bool.Parse(ret["RESULT"].ToString());
  326. }
  327. public bool DeleteItems(UUID principalID, List<UUID> itemIDs)
  328. {
  329. List<string> slist = new List<string>();
  330. foreach (UUID f in itemIDs)
  331. slist.Add(f.ToString());
  332. Dictionary<string,object> ret = MakeRequest("DELETEITEMS",
  333. new Dictionary<string,object> {
  334. { "PRINCIPAL", principalID.ToString() },
  335. { "ITEMS", slist }
  336. });
  337. if (ret == null)
  338. return false;
  339. return bool.Parse(ret["RESULT"].ToString());
  340. }
  341. public InventoryItemBase GetItem(InventoryItemBase item)
  342. {
  343. try
  344. {
  345. Dictionary<string, object> ret = MakeRequest("GETITEM",
  346. new Dictionary<string, object> {
  347. { "ID", item.ID.ToString() }
  348. });
  349. if (ret == null)
  350. return null;
  351. if (ret.Count == 0)
  352. return null;
  353. return BuildItem((Dictionary<string, object>)ret["item"]);
  354. }
  355. catch (Exception e)
  356. {
  357. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception in GetItem: {0}", e.Message);
  358. }
  359. return null;
  360. }
  361. public InventoryFolderBase GetFolder(InventoryFolderBase folder)
  362. {
  363. try
  364. {
  365. Dictionary<string, object> ret = MakeRequest("GETFOLDER",
  366. new Dictionary<string, object> {
  367. { "ID", folder.ID.ToString() }
  368. });
  369. if (ret == null)
  370. return null;
  371. if (ret.Count == 0)
  372. return null;
  373. return BuildFolder((Dictionary<string, object>)ret["folder"]);
  374. }
  375. catch (Exception e)
  376. {
  377. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception in GetFolder: {0}", e.Message);
  378. }
  379. return null;
  380. }
  381. public List<InventoryItemBase> GetActiveGestures(UUID principalID)
  382. {
  383. Dictionary<string,object> ret = MakeRequest("GETACTIVEGESTURES",
  384. new Dictionary<string,object> {
  385. { "PRINCIPAL", principalID.ToString() }
  386. });
  387. if (ret == null)
  388. return null;
  389. List<InventoryItemBase> items = new List<InventoryItemBase>();
  390. foreach (Object o in ret.Values) // getting the values directly, we don't care about the keys item_i
  391. items.Add(BuildItem((Dictionary<string, object>)o));
  392. return items;
  393. }
  394. public int GetAssetPermissions(UUID principalID, UUID assetID)
  395. {
  396. Dictionary<string,object> ret = MakeRequest("GETASSETPERMISSIONS",
  397. new Dictionary<string,object> {
  398. { "PRINCIPAL", principalID.ToString() },
  399. { "ASSET", assetID.ToString() }
  400. });
  401. if (ret == null)
  402. return 0;
  403. return int.Parse(ret["RESULT"].ToString());
  404. }
  405. // These are either obsolete or unused
  406. //
  407. public InventoryCollection GetUserInventory(UUID principalID)
  408. {
  409. return null;
  410. }
  411. public void GetUserInventory(UUID principalID, InventoryReceiptCallback callback)
  412. {
  413. }
  414. public bool HasInventoryForUser(UUID principalID)
  415. {
  416. return false;
  417. }
  418. // Helpers
  419. //
  420. private Dictionary<string,object> MakeRequest(string method,
  421. Dictionary<string,object> sendData)
  422. {
  423. sendData["METHOD"] = method;
  424. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  425. m_ServerURI + "/xinventory",
  426. ServerUtils.BuildQueryString(sendData));
  427. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(
  428. reply);
  429. return replyData;
  430. }
  431. private InventoryFolderBase BuildFolder(Dictionary<string,object> data)
  432. {
  433. InventoryFolderBase folder = new InventoryFolderBase();
  434. try
  435. {
  436. folder.ParentID = new UUID(data["ParentID"].ToString());
  437. folder.Type = short.Parse(data["Type"].ToString());
  438. folder.Version = ushort.Parse(data["Version"].ToString());
  439. folder.Name = data["Name"].ToString();
  440. folder.Owner = new UUID(data["Owner"].ToString());
  441. folder.ID = new UUID(data["ID"].ToString());
  442. }
  443. catch (Exception e)
  444. {
  445. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception building folder: {0}", e.Message);
  446. }
  447. return folder;
  448. }
  449. private InventoryItemBase BuildItem(Dictionary<string,object> data)
  450. {
  451. InventoryItemBase item = new InventoryItemBase();
  452. try
  453. {
  454. item.AssetID = new UUID(data["AssetID"].ToString());
  455. item.AssetType = int.Parse(data["AssetType"].ToString());
  456. item.Name = data["Name"].ToString();
  457. item.Owner = new UUID(data["Owner"].ToString());
  458. item.ID = new UUID(data["ID"].ToString());
  459. item.InvType = int.Parse(data["InvType"].ToString());
  460. item.Folder = new UUID(data["Folder"].ToString());
  461. item.CreatorId = data["CreatorId"].ToString();
  462. item.Description = data["Description"].ToString();
  463. item.NextPermissions = uint.Parse(data["NextPermissions"].ToString());
  464. item.CurrentPermissions = uint.Parse(data["CurrentPermissions"].ToString());
  465. item.BasePermissions = uint.Parse(data["BasePermissions"].ToString());
  466. item.EveryOnePermissions = uint.Parse(data["EveryOnePermissions"].ToString());
  467. item.GroupPermissions = uint.Parse(data["GroupPermissions"].ToString());
  468. item.GroupID = new UUID(data["GroupID"].ToString());
  469. item.GroupOwned = bool.Parse(data["GroupOwned"].ToString());
  470. item.SalePrice = int.Parse(data["SalePrice"].ToString());
  471. item.SaleType = byte.Parse(data["SaleType"].ToString());
  472. item.Flags = uint.Parse(data["Flags"].ToString());
  473. item.CreationDate = int.Parse(data["CreationDate"].ToString());
  474. }
  475. catch (Exception e)
  476. {
  477. m_log.DebugFormat("[XINVENTORY CONNECTOR STUB]: Exception building item: {0}", e.Message);
  478. }
  479. return item;
  480. }
  481. }
  482. }