JsonStoreScriptModule.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Copyright (c) Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Mono.Addins;
  28. using System;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Text;
  32. using System.Net;
  33. using System.Net.Sockets;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using System.Collections.Generic;
  42. using System.Text.RegularExpressions;
  43. namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreScriptModule")]
  46. public class JsonStoreScriptModule : INonSharedRegionModule
  47. {
  48. private static readonly ILog m_log =
  49. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IConfig m_config = null;
  51. private bool m_enabled = false;
  52. private Scene m_scene = null;
  53. private IScriptModuleComms m_comms;
  54. private IJsonStoreModule m_store;
  55. #region Region Module interface
  56. // -----------------------------------------------------------------
  57. /// <summary>
  58. /// Name of this shared module is it's class name
  59. /// </summary>
  60. // -----------------------------------------------------------------
  61. public string Name
  62. {
  63. get { return this.GetType().Name; }
  64. }
  65. // -----------------------------------------------------------------
  66. /// <summary>
  67. /// Initialise this shared module
  68. /// </summary>
  69. /// <param name="scene">this region is getting initialised</param>
  70. /// <param name="source">nini config, we are not using this</param>
  71. // -----------------------------------------------------------------
  72. public void Initialise(IConfigSource config)
  73. {
  74. try
  75. {
  76. if ((m_config = config.Configs["JsonStore"]) == null)
  77. {
  78. // There is no configuration, the module is disabled
  79. // m_log.InfoFormat("[JsonStoreScripts] no configuration info");
  80. return;
  81. }
  82. m_enabled = m_config.GetBoolean("Enabled", m_enabled);
  83. }
  84. catch (Exception e)
  85. {
  86. m_log.ErrorFormat("[JsonStoreScripts] initialization error: {0}",e.Message);
  87. return;
  88. }
  89. if (m_enabled)
  90. m_log.DebugFormat("[JsonStoreScripts] module is enabled");
  91. }
  92. // -----------------------------------------------------------------
  93. /// <summary>
  94. /// everything is loaded, perform post load configuration
  95. /// </summary>
  96. // -----------------------------------------------------------------
  97. public void PostInitialise()
  98. {
  99. }
  100. // -----------------------------------------------------------------
  101. /// <summary>
  102. /// Nothing to do on close
  103. /// </summary>
  104. // -----------------------------------------------------------------
  105. public void Close()
  106. {
  107. }
  108. // -----------------------------------------------------------------
  109. /// <summary>
  110. /// </summary>
  111. // -----------------------------------------------------------------
  112. public void AddRegion(Scene scene)
  113. {
  114. }
  115. // -----------------------------------------------------------------
  116. /// <summary>
  117. /// </summary>
  118. // -----------------------------------------------------------------
  119. public void RemoveRegion(Scene scene)
  120. {
  121. // need to remove all references to the scene in the subscription
  122. // list to enable full garbage collection of the scene object
  123. }
  124. // -----------------------------------------------------------------
  125. /// <summary>
  126. /// Called when all modules have been added for a region. This is
  127. /// where we hook up events
  128. /// </summary>
  129. // -----------------------------------------------------------------
  130. public void RegionLoaded(Scene scene)
  131. {
  132. if (m_enabled)
  133. {
  134. m_scene = scene;
  135. m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
  136. if (m_comms == null)
  137. {
  138. m_log.ErrorFormat("[JsonStoreScripts] ScriptModuleComms interface not defined");
  139. m_enabled = false;
  140. return;
  141. }
  142. m_store = m_scene.RequestModuleInterface<IJsonStoreModule>();
  143. if (m_store == null)
  144. {
  145. m_log.ErrorFormat("[JsonStoreScripts] JsonModule interface not defined");
  146. m_enabled = false;
  147. return;
  148. }
  149. try
  150. {
  151. m_comms.RegisterScriptInvocation(this,"JsonCreateStore");
  152. m_comms.RegisterScriptInvocation(this,"JsonDestroyStore");
  153. m_comms.RegisterScriptInvocation(this,"JsonReadNotecard");
  154. m_comms.RegisterScriptInvocation(this,"JsonWriteNotecard");
  155. m_comms.RegisterScriptInvocation(this,"JsonTestPath");
  156. m_comms.RegisterScriptInvocation(this,"JsonTestPathJson");
  157. m_comms.RegisterScriptInvocation(this,"JsonGetValue");
  158. m_comms.RegisterScriptInvocation(this,"JsonGetValueJson");
  159. m_comms.RegisterScriptInvocation(this,"JsonTakeValue");
  160. m_comms.RegisterScriptInvocation(this,"JsonTakeValueJson");
  161. m_comms.RegisterScriptInvocation(this,"JsonReadValue");
  162. m_comms.RegisterScriptInvocation(this,"JsonReadValueJson");
  163. m_comms.RegisterScriptInvocation(this,"JsonSetValue");
  164. m_comms.RegisterScriptInvocation(this,"JsonSetValueJson");
  165. m_comms.RegisterScriptInvocation(this,"JsonRemoveValue");
  166. }
  167. catch (Exception e)
  168. {
  169. // See http://opensimulator.org/mantis/view.php?id=5971 for more information
  170. m_log.WarnFormat("[JsonStroreScripts] script method registration failed; {0}",e.Message);
  171. m_enabled = false;
  172. }
  173. }
  174. }
  175. /// -----------------------------------------------------------------
  176. /// <summary>
  177. /// </summary>
  178. // -----------------------------------------------------------------
  179. public Type ReplaceableInterface
  180. {
  181. get { return null; }
  182. }
  183. #endregion
  184. #region ScriptInvocationInteface
  185. // -----------------------------------------------------------------
  186. /// <summary>
  187. ///
  188. /// </summary>
  189. // -----------------------------------------------------------------
  190. protected void GenerateRuntimeError(string msg)
  191. {
  192. throw new Exception("JsonStore Runtime Error: " + msg);
  193. }
  194. // -----------------------------------------------------------------
  195. /// <summary>
  196. ///
  197. /// </summary>
  198. // -----------------------------------------------------------------
  199. protected UUID JsonCreateStore(UUID hostID, UUID scriptID, string value)
  200. {
  201. UUID uuid = UUID.Zero;
  202. if (! m_store.CreateStore(value, ref uuid))
  203. GenerateRuntimeError("Failed to create Json store");
  204. return uuid;
  205. }
  206. // -----------------------------------------------------------------
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. // -----------------------------------------------------------------
  211. protected int JsonDestroyStore(UUID hostID, UUID scriptID, UUID storeID)
  212. {
  213. return m_store.DestroyStore(storeID) ? 1 : 0;
  214. }
  215. // -----------------------------------------------------------------
  216. /// <summary>
  217. ///
  218. /// </summary>
  219. // -----------------------------------------------------------------
  220. protected UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
  221. {
  222. UUID reqID = UUID.Random();
  223. Util.FireAndForget(delegate(object o) { DoJsonReadNotecard(reqID,hostID,scriptID,storeID,path,assetID); });
  224. return reqID;
  225. }
  226. // -----------------------------------------------------------------
  227. /// <summary>
  228. ///
  229. /// </summary>
  230. // -----------------------------------------------------------------
  231. protected UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  232. {
  233. UUID reqID = UUID.Random();
  234. Util.FireAndForget(delegate(object o) { DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name); });
  235. return reqID;
  236. }
  237. // -----------------------------------------------------------------
  238. /// <summary>
  239. ///
  240. /// </summary>
  241. // -----------------------------------------------------------------
  242. protected int JsonTestPath(UUID hostID, UUID scriptID, UUID storeID, string path)
  243. {
  244. return m_store.TestPath(storeID,path,false) ? 1 : 0;
  245. }
  246. protected int JsonTestPathJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  247. {
  248. return m_store.TestPath(storeID,path,true) ? 1 : 0;
  249. }
  250. // -----------------------------------------------------------------
  251. /// <summary>
  252. ///
  253. /// </summary>
  254. // -----------------------------------------------------------------
  255. protected int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  256. {
  257. return m_store.SetValue(storeID,path,value,false) ? 1 : 0;
  258. }
  259. protected int JsonSetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
  260. {
  261. return m_store.SetValue(storeID,path,value,true) ? 1 : 0;
  262. }
  263. // -----------------------------------------------------------------
  264. /// <summary>
  265. ///
  266. /// </summary>
  267. // -----------------------------------------------------------------
  268. protected int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  269. {
  270. return m_store.RemoveValue(storeID,path) ? 1 : 0;
  271. }
  272. // -----------------------------------------------------------------
  273. /// <summary>
  274. ///
  275. /// </summary>
  276. // -----------------------------------------------------------------
  277. protected string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  278. {
  279. string value = String.Empty;
  280. m_store.GetValue(storeID,path,false,out value);
  281. return value;
  282. }
  283. protected string JsonGetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  284. {
  285. string value = String.Empty;
  286. m_store.GetValue(storeID,path,true, out value);
  287. return value;
  288. }
  289. // -----------------------------------------------------------------
  290. /// <summary>
  291. ///
  292. /// </summary>
  293. // -----------------------------------------------------------------
  294. protected UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  295. {
  296. UUID reqID = UUID.Random();
  297. Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,false); });
  298. return reqID;
  299. }
  300. protected UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  301. {
  302. UUID reqID = UUID.Random();
  303. Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,true); });
  304. return reqID;
  305. }
  306. private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  307. {
  308. try
  309. {
  310. m_store.TakeValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  311. return;
  312. }
  313. catch (Exception e)
  314. {
  315. m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
  316. }
  317. DispatchValue(scriptID,reqID,String.Empty);
  318. }
  319. // -----------------------------------------------------------------
  320. /// <summary>
  321. ///
  322. /// </summary>
  323. // -----------------------------------------------------------------
  324. protected UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
  325. {
  326. UUID reqID = UUID.Random();
  327. Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,false); });
  328. return reqID;
  329. }
  330. protected UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
  331. {
  332. UUID reqID = UUID.Random();
  333. Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,true); });
  334. return reqID;
  335. }
  336. private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
  337. {
  338. try
  339. {
  340. m_store.ReadValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
  341. return;
  342. }
  343. catch (Exception e)
  344. {
  345. m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
  346. }
  347. DispatchValue(scriptID,reqID,String.Empty);
  348. }
  349. #endregion
  350. // -----------------------------------------------------------------
  351. /// <summary>
  352. ///
  353. /// </summary>
  354. // -----------------------------------------------------------------
  355. protected void DispatchValue(UUID scriptID, UUID reqID, string value)
  356. {
  357. m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
  358. }
  359. // -----------------------------------------------------------------
  360. /// <summary>
  361. ///
  362. /// </summary>
  363. // -----------------------------------------------------------------
  364. private void DoJsonReadNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
  365. {
  366. AssetBase a = m_scene.AssetService.Get(assetID.ToString());
  367. if (a == null)
  368. GenerateRuntimeError(String.Format("Unable to find notecard asset {0}",assetID));
  369. if (a.Type != (sbyte)AssetType.Notecard)
  370. GenerateRuntimeError(String.Format("Invalid notecard asset {0}",assetID));
  371. m_log.DebugFormat("[JsonStoreScripts] read notecard in context {0}",storeID);
  372. try
  373. {
  374. string jsondata = SLUtil.ParseNotecardToString(Encoding.UTF8.GetString(a.Data));
  375. int result = m_store.SetValue(storeID, path, jsondata,true) ? 1 : 0;
  376. m_comms.DispatchReply(scriptID,result, "", reqID.ToString());
  377. return;
  378. }
  379. catch (Exception e)
  380. {
  381. m_log.WarnFormat("[JsonStoreScripts] Json parsing failed; {0}",e.Message);
  382. }
  383. GenerateRuntimeError(String.Format("Json parsing failed for {0}",assetID.ToString()));
  384. m_comms.DispatchReply(scriptID,0,"",reqID.ToString());
  385. }
  386. // -----------------------------------------------------------------
  387. /// <summary>
  388. ///
  389. /// </summary>
  390. // -----------------------------------------------------------------
  391. private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string name)
  392. {
  393. string data;
  394. if (! m_store.GetValue(storeID,path,true, out data))
  395. {
  396. m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
  397. return;
  398. }
  399. SceneObjectPart host = m_scene.GetSceneObjectPart(hostID);
  400. // Create new asset
  401. UUID assetID = UUID.Random();
  402. AssetBase asset = new AssetBase(assetID, name, (sbyte)AssetType.Notecard, host.OwnerID.ToString());
  403. asset.Description = "Json store";
  404. int textLength = data.Length;
  405. data = "Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
  406. + textLength.ToString() + "\n" + data + "}\n";
  407. asset.Data = Util.UTF8.GetBytes(data);
  408. m_scene.AssetService.Store(asset);
  409. // Create Task Entry
  410. TaskInventoryItem taskItem = new TaskInventoryItem();
  411. taskItem.ResetIDs(host.UUID);
  412. taskItem.ParentID = host.UUID;
  413. taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
  414. taskItem.Name = asset.Name;
  415. taskItem.Description = asset.Description;
  416. taskItem.Type = (int)AssetType.Notecard;
  417. taskItem.InvType = (int)InventoryType.Notecard;
  418. taskItem.OwnerID = host.OwnerID;
  419. taskItem.CreatorID = host.OwnerID;
  420. taskItem.BasePermissions = (uint)PermissionMask.All;
  421. taskItem.CurrentPermissions = (uint)PermissionMask.All;
  422. taskItem.EveryonePermissions = 0;
  423. taskItem.NextPermissions = (uint)PermissionMask.All;
  424. taskItem.GroupID = host.GroupID;
  425. taskItem.GroupPermissions = 0;
  426. taskItem.Flags = 0;
  427. taskItem.PermsGranter = UUID.Zero;
  428. taskItem.PermsMask = 0;
  429. taskItem.AssetID = asset.FullID;
  430. host.Inventory.AddInventoryItem(taskItem, false);
  431. m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
  432. }
  433. }
  434. }