aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs489
1 files changed, 489 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
new file mode 100644
index 0000000..7aba860
--- /dev/null
+++ b/OpenSim/Region/OptionalModules/Scripting/JsonStore/JsonStoreScriptModule.cs
@@ -0,0 +1,489 @@
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 */
27using Mono.Addins;
28
29using System;
30using System.Reflection;
31using System.Threading;
32using System.Text;
33using System.Net;
34using System.Net.Sockets;
35using log4net;
36using Nini.Config;
37using OpenMetaverse;
38using OpenMetaverse.StructuredData;
39using OpenSim.Framework;
40using OpenSim.Region.Framework.Interfaces;
41using OpenSim.Region.Framework.Scenes;
42using System.Collections.Generic;
43using System.Text.RegularExpressions;
44
45namespace OpenSim.Region.OptionalModules.Scripting.JsonStore
46{
47 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "JsonStoreScriptModule")]
48
49 public class JsonStoreScriptModule : INonSharedRegionModule
50 {
51 private static readonly ILog m_log =
52 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53
54 private IConfig m_config = null;
55 private bool m_enabled = false;
56 private Scene m_scene = null;
57
58 private IScriptModuleComms m_comms;
59 private IJsonStoreModule m_store;
60
61#region IRegionModule Members
62
63 // -----------------------------------------------------------------
64 /// <summary>
65 /// Name of this shared module is it's class name
66 /// </summary>
67 // -----------------------------------------------------------------
68 public string Name
69 {
70 get { return this.GetType().Name; }
71 }
72
73 // -----------------------------------------------------------------
74 /// <summary>
75 /// Initialise this shared module
76 /// </summary>
77 /// <param name="scene">this region is getting initialised</param>
78 /// <param name="source">nini config, we are not using this</param>
79 // -----------------------------------------------------------------
80 public void Initialise(IConfigSource config)
81 {
82 try
83 {
84 if ((m_config = config.Configs["JsonStore"]) == null)
85 {
86 // There is no configuration, the module is disabled
87 m_log.InfoFormat("[JsonStoreScripts] no configuration info");
88 return;
89 }
90
91 m_enabled = m_config.GetBoolean("Enabled", m_enabled);
92 }
93 catch (Exception e)
94 {
95 m_log.ErrorFormat("[JsonStoreScripts] initialization error: {0}",e.Message);
96 return;
97 }
98
99 m_log.InfoFormat("[JsonStoreScripts] module {0} enabled",(m_enabled ? "is" : "is not"));
100 }
101
102 // -----------------------------------------------------------------
103 /// <summary>
104 /// everything is loaded, perform post load configuration
105 /// </summary>
106 // -----------------------------------------------------------------
107 public void PostInitialise()
108 {
109 }
110
111 // -----------------------------------------------------------------
112 /// <summary>
113 /// Nothing to do on close
114 /// </summary>
115 // -----------------------------------------------------------------
116 public void Close()
117 {
118 }
119
120 // -----------------------------------------------------------------
121 /// <summary>
122 /// </summary>
123 // -----------------------------------------------------------------
124 public void AddRegion(Scene scene)
125 {
126 }
127
128 // -----------------------------------------------------------------
129 /// <summary>
130 /// </summary>
131 // -----------------------------------------------------------------
132 public void RemoveRegion(Scene scene)
133 {
134 // need to remove all references to the scene in the subscription
135 // list to enable full garbage collection of the scene object
136 }
137
138 // -----------------------------------------------------------------
139 /// <summary>
140 /// Called when all modules have been added for a region. This is
141 /// where we hook up events
142 /// </summary>
143 // -----------------------------------------------------------------
144 public void RegionLoaded(Scene scene)
145 {
146 if (m_enabled)
147 {
148 m_scene = scene;
149 m_comms = m_scene.RequestModuleInterface<IScriptModuleComms>();
150 if (m_comms == null)
151 {
152 m_log.ErrorFormat("[JsonStoreScripts] ScriptModuleComms interface not defined");
153 m_enabled = false;
154 return;
155 }
156
157 m_store = m_scene.RequestModuleInterface<IJsonStoreModule>();
158 if (m_store == null)
159 {
160 m_log.ErrorFormat("[JsonStoreScripts] JsonModule interface not defined");
161 m_enabled = false;
162 return;
163 }
164
165 m_comms.RegisterScriptInvocation(this,"JsonCreateStore");
166 m_comms.RegisterScriptInvocation(this,"JsonDestroyStore");
167
168 m_comms.RegisterScriptInvocation(this,"JsonReadNotecard");
169 m_comms.RegisterScriptInvocation(this,"JsonWriteNotecard");
170
171 m_comms.RegisterScriptInvocation(this,"JsonTestPath");
172 m_comms.RegisterScriptInvocation(this,"JsonTestPathJson");
173
174 m_comms.RegisterScriptInvocation(this,"JsonGetValue");
175 m_comms.RegisterScriptInvocation(this,"JsonGetValueJson");
176
177 m_comms.RegisterScriptInvocation(this,"JsonTakeValue");
178 m_comms.RegisterScriptInvocation(this,"JsonTakeValueJson");
179
180 m_comms.RegisterScriptInvocation(this,"JsonReadValue");
181 m_comms.RegisterScriptInvocation(this,"JsonReadValueJson");
182
183 m_comms.RegisterScriptInvocation(this,"JsonSetValue");
184 m_comms.RegisterScriptInvocation(this,"JsonSetValueJson");
185
186 m_comms.RegisterScriptInvocation(this,"JsonRemoveValue");
187 }
188 }
189
190 /// -----------------------------------------------------------------
191 /// <summary>
192 /// </summary>
193 // -----------------------------------------------------------------
194 public Type ReplaceableInterface
195 {
196 get { return null; }
197 }
198
199#endregion
200
201#region ScriptInvocationInteface
202 // -----------------------------------------------------------------
203 /// <summary>
204 ///
205 /// </summary>
206 // -----------------------------------------------------------------
207 protected void GenerateRuntimeError(string msg)
208 {
209 throw new Exception("JsonStore Runtime Error: " + msg);
210 }
211
212 // -----------------------------------------------------------------
213 /// <summary>
214 ///
215 /// </summary>
216 // -----------------------------------------------------------------
217 protected UUID JsonCreateStore(UUID hostID, UUID scriptID, string value)
218 {
219 UUID uuid = UUID.Zero;
220 if (! m_store.CreateStore(value, out uuid))
221 GenerateRuntimeError("Failed to create Json store");
222
223 return uuid;
224 }
225
226 // -----------------------------------------------------------------
227 /// <summary>
228 ///
229 /// </summary>
230 // -----------------------------------------------------------------
231 protected int JsonDestroyStore(UUID hostID, UUID scriptID, UUID storeID)
232 {
233 return m_store.DestroyStore(storeID) ? 1 : 0;
234 }
235
236 // -----------------------------------------------------------------
237 /// <summary>
238 ///
239 /// </summary>
240 // -----------------------------------------------------------------
241 protected UUID JsonReadNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
242 {
243 UUID reqID = UUID.Random();
244 Util.FireAndForget(delegate(object o) { DoJsonReadNotecard(reqID,hostID,scriptID,storeID,path,assetID); });
245 return reqID;
246 }
247
248 // -----------------------------------------------------------------
249 /// <summary>
250 ///
251 /// </summary>
252 // -----------------------------------------------------------------
253 protected UUID JsonWriteNotecard(UUID hostID, UUID scriptID, UUID storeID, string path, string name)
254 {
255 UUID reqID = UUID.Random();
256 Util.FireAndForget(delegate(object o) { DoJsonWriteNotecard(reqID,hostID,scriptID,storeID,path,name); });
257 return reqID;
258 }
259
260 // -----------------------------------------------------------------
261 /// <summary>
262 ///
263 /// </summary>
264 // -----------------------------------------------------------------
265 protected int JsonTestPath(UUID hostID, UUID scriptID, UUID storeID, string path)
266 {
267 return m_store.TestPath(storeID,path,false) ? 1 : 0;
268 }
269
270 protected int JsonTestPathJson(UUID hostID, UUID scriptID, UUID storeID, string path)
271 {
272 return m_store.TestPath(storeID,path,true) ? 1 : 0;
273 }
274
275 // -----------------------------------------------------------------
276 /// <summary>
277 ///
278 /// </summary>
279 // -----------------------------------------------------------------
280 protected int JsonSetValue(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
281 {
282 return m_store.SetValue(storeID,path,value,false) ? 1 : 0;
283 }
284
285 protected int JsonSetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path, string value)
286 {
287 return m_store.SetValue(storeID,path,value,true) ? 1 : 0;
288 }
289
290 // -----------------------------------------------------------------
291 /// <summary>
292 ///
293 /// </summary>
294 // -----------------------------------------------------------------
295 protected int JsonRemoveValue(UUID hostID, UUID scriptID, UUID storeID, string path)
296 {
297 return m_store.RemoveValue(storeID,path) ? 1 : 0;
298 }
299
300 // -----------------------------------------------------------------
301 /// <summary>
302 ///
303 /// </summary>
304 // -----------------------------------------------------------------
305 protected string JsonGetValue(UUID hostID, UUID scriptID, UUID storeID, string path)
306 {
307 string value = String.Empty;
308 m_store.GetValue(storeID,path,false,out value);
309 return value;
310 }
311
312 protected string JsonGetValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
313 {
314 string value = String.Empty;
315 m_store.GetValue(storeID,path,true, out value);
316 return value;
317 }
318
319 // -----------------------------------------------------------------
320 /// <summary>
321 ///
322 /// </summary>
323 // -----------------------------------------------------------------
324 protected UUID JsonTakeValue(UUID hostID, UUID scriptID, UUID storeID, string path)
325 {
326 UUID reqID = UUID.Random();
327 Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,false); });
328 return reqID;
329 }
330
331 protected UUID JsonTakeValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
332 {
333 UUID reqID = UUID.Random();
334 Util.FireAndForget(delegate(object o) { DoJsonTakeValue(scriptID,reqID,storeID,path,true); });
335 return reqID;
336 }
337
338 private void DoJsonTakeValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
339 {
340 try
341 {
342 m_store.TakeValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
343 return;
344 }
345 catch (Exception e)
346 {
347 m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
348 }
349
350 DispatchValue(scriptID,reqID,String.Empty);
351 }
352
353
354 // -----------------------------------------------------------------
355 /// <summary>
356 ///
357 /// </summary>
358 // -----------------------------------------------------------------
359 protected UUID JsonReadValue(UUID hostID, UUID scriptID, UUID storeID, string path)
360 {
361 UUID reqID = UUID.Random();
362 Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,false); });
363 return reqID;
364 }
365
366 protected UUID JsonReadValueJson(UUID hostID, UUID scriptID, UUID storeID, string path)
367 {
368 UUID reqID = UUID.Random();
369 Util.FireAndForget(delegate(object o) { DoJsonReadValue(scriptID,reqID,storeID,path,true); });
370 return reqID;
371 }
372
373 private void DoJsonReadValue(UUID scriptID, UUID reqID, UUID storeID, string path, bool useJson)
374 {
375 try
376 {
377 m_store.ReadValue(storeID,path,useJson,delegate(string value) { DispatchValue(scriptID,reqID,value); });
378 return;
379 }
380 catch (Exception e)
381 {
382 m_log.InfoFormat("[JsonStoreScripts] unable to retrieve value; {0}",e.ToString());
383 }
384
385 DispatchValue(scriptID,reqID,String.Empty);
386 }
387
388#endregion
389
390 // -----------------------------------------------------------------
391 /// <summary>
392 ///
393 /// </summary>
394 // -----------------------------------------------------------------
395 protected void DispatchValue(UUID scriptID, UUID reqID, string value)
396 {
397 m_comms.DispatchReply(scriptID,1,value,reqID.ToString());
398 }
399
400 // -----------------------------------------------------------------
401 /// <summary>
402 ///
403 /// </summary>
404 // -----------------------------------------------------------------
405 private void DoJsonReadNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, UUID assetID)
406 {
407 AssetBase a = m_scene.AssetService.Get(assetID.ToString());
408 if (a == null)
409 GenerateRuntimeError(String.Format("Unable to find notecard asset {0}",assetID));
410
411 if (a.Type != (sbyte)AssetType.Notecard)
412 GenerateRuntimeError(String.Format("Invalid notecard asset {0}",assetID));
413
414 m_log.DebugFormat("[JsonStoreScripts] read notecard in context {0}",storeID);
415
416 try
417 {
418 System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
419 string jsondata = SLUtil.ParseNotecardToString(enc.GetString(a.Data));
420 int result = m_store.SetValue(storeID,path,jsondata,true) ? 1 : 0;
421 m_comms.DispatchReply(scriptID,result,"",reqID.ToString());
422 return;
423 }
424 catch (Exception e)
425 {
426 m_log.WarnFormat("[JsonStoreScripts] Json parsing failed; {0}",e.Message);
427 }
428
429 GenerateRuntimeError(String.Format("Json parsing failed for {0}",assetID.ToString()));
430 m_comms.DispatchReply(scriptID,0,"",reqID.ToString());
431 }
432
433 // -----------------------------------------------------------------
434 /// <summary>
435 ///
436 /// </summary>
437 // -----------------------------------------------------------------
438 private void DoJsonWriteNotecard(UUID reqID, UUID hostID, UUID scriptID, UUID storeID, string path, string name)
439 {
440 string data;
441 if (! m_store.GetValue(storeID,path,true, out data))
442 {
443 m_comms.DispatchReply(scriptID,0,UUID.Zero.ToString(),reqID.ToString());
444 return;
445 }
446
447 SceneObjectPart host = m_scene.GetSceneObjectPart(hostID);
448
449 // Create new asset
450 UUID assetID = UUID.Random();
451 AssetBase asset = new AssetBase(assetID, name, (sbyte)AssetType.Notecard, host.OwnerID.ToString());
452 asset.Description = "Json store";
453
454 int textLength = data.Length;
455 data = "Linden text version 2\n{\nLLEmbeddedItems version 1\n{\ncount 0\n}\nText length "
456 + textLength.ToString() + "\n" + data + "}\n";
457
458 asset.Data = Util.UTF8.GetBytes(data);
459 m_scene.AssetService.Store(asset);
460
461 // Create Task Entry
462 TaskInventoryItem taskItem = new TaskInventoryItem();
463
464 taskItem.ResetIDs(host.UUID);
465 taskItem.ParentID = host.UUID;
466 taskItem.CreationDate = (uint)Util.UnixTimeSinceEpoch();
467 taskItem.Name = asset.Name;
468 taskItem.Description = asset.Description;
469 taskItem.Type = (int)AssetType.Notecard;
470 taskItem.InvType = (int)InventoryType.Notecard;
471 taskItem.OwnerID = host.OwnerID;
472 taskItem.CreatorID = host.OwnerID;
473 taskItem.BasePermissions = (uint)PermissionMask.All;
474 taskItem.CurrentPermissions = (uint)PermissionMask.All;
475 taskItem.EveryonePermissions = 0;
476 taskItem.NextPermissions = (uint)PermissionMask.All;
477 taskItem.GroupID = host.GroupID;
478 taskItem.GroupPermissions = 0;
479 taskItem.Flags = 0;
480 taskItem.PermsGranter = UUID.Zero;
481 taskItem.PermsMask = 0;
482 taskItem.AssetID = asset.FullID;
483
484 host.Inventory.AddInventoryItem(taskItem, false);
485
486 m_comms.DispatchReply(scriptID,1,assetID.ToString(),reqID.ToString());
487 }
488 }
489}