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