corrade-lsl-templates – Blame information for rev 42

Subversion Repositories:
Rev:
Rev Author Line No. Line
42 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A fortune module for Corrade Eggdrop.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return "";
15 list a = llParseStringKeepNulls(data, ["&", "="], []);
16 integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
17 if(i != -1) return llList2String(a, 2*i+1);
18 return "";
19 }
20  
21 ///////////////////////////////////////////////////////////////////////////
22 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
23 ///////////////////////////////////////////////////////////////////////////
24 string wasKeyValueEncode(list data) {
25 list k = llList2ListStrided(data, 0, -1, 2);
26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
27 data = [];
28 do {
29 data += llList2String(k, 0) + "=" + llList2String(v, 0);
30 k = llDeleteSubList(k, 0, 0);
31 v = llDeleteSubList(v, 0, 0);
32 } while(llGetListLength(k) != 0);
33 return llDumpList2String(data, "&");
34 }
35  
36 ///////////////////////////////////////////////////////////////////////////
37 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 //
38 ///////////////////////////////////////////////////////////////////////////
39 // http://was.fm/secondlife/wanderer
40 vector wasCirclePoint(float radius) {
41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
42 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
43 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
44 return <x, y, 0>;
45 return wasCirclePoint(radius);
46 }
47  
48 ///////////////////////////////////////////////////////////////////////////
49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
50 ///////////////////////////////////////////////////////////////////////////
51 // escapes a string in conformance with RFC1738
52 string wasURLEscape(string i) {
53 string o = "";
54 do {
55 string c = llGetSubString(i, 0, 0);
56 i = llDeleteSubString(i, 0, 0);
57 if(c == "") jump continue;
58 if(c == " ") {
59 o += "+";
60 jump continue;
61 }
62 if(c == "\n") {
63 o += "%0D" + llEscapeURL(c);
64 jump continue;
65 }
66 o += llEscapeURL(c);
67 @continue;
68 } while(i != "");
69 return o;
70 }
71  
72 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
74 ///////////////////////////////////////////////////////////////////////////
75 list wasCSVToList(string csv) {
76 list l = [];
77 list s = [];
78 string m = "";
79 do {
80 string a = llGetSubString(csv, 0, 0);
81 csv = llDeleteSubString(csv, 0, 0);
82 if(a == ",") {
83 if(llList2String(s, -1) != "\"") {
84 l += m;
85 m = "";
86 jump continue;
87 }
88 m += a;
89 jump continue;
90 }
91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
92 m += a;
93 csv = llDeleteSubString(csv, 0, 0);
94 jump continue;
95 }
96 if(a == "\"") {
97 if(llList2String(s, -1) != a) {
98 s += a;
99 jump continue;
100 }
101 s = llDeleteSubList(s, -1, -1);
102 jump continue;
103 }
104 m += a;
105 @continue;
106 } while(csv != "");
107 // postcondition: length(s) = 0
108 return l + m;
109 }
110  
111 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) {
115 list v = [];
116 do {
117 string a = llDumpList2String(
118 llParseStringKeepNulls(
119 llList2String(
120 l,
121  
122 ),
123 ["\""],
124 []
125 ),
126 "\"\""
127 );
128 if(llParseStringKeepNulls(
129 a,
130 [" ", ",", "\n", "\""], []
131 ) !=
132 (list) a
133 ) a = "\"" + a + "\"";
134 v += a;
135 l = llDeleteSubList(l, 0, 0);
136 } while(l != []);
137 return llDumpList2String(v, ",");
138 }
139  
140 ///////////////////////////////////////////////////////////////////////////
141 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
142 ///////////////////////////////////////////////////////////////////////////
143 // unescapes a string in conformance with RFC1738
144 string wasURLUnescape(string i) {
145 return llUnescapeURL(
146 llDumpList2String(
147 llParseString2List(
148 llDumpList2String(
149 llParseString2List(
150 i,
151 ["+"],
152 []
153 ),
154 " "
155 ),
156 ["%0D%0A"],
157 []
158 ),
159 "\n"
160 )
161 );
162 }
163  
164 // configuration data
165 string configuration = "";
166 // callback URL
167 string URL = "";
168 // store message over state.
169 string data = "";
170  
171 key queryID = NULL_KEY;
172 string region = "";
173  
174 string teleport_continue = "";
175  
176 default {
177 state_entry() {
178 llOwnerSay("[Survey] Starting module...");
179 llSetTimerEvent(10);
180 }
181 link_message(integer sender, integer num, string message, key id) {
182 if(id != "configuration") return;
183 llOwnerSay("[Survey] Got configuration...");
184 configuration = message;
185 state listen_group;
186 }
187 timer() {
188 llOwnerSay("[Survey] Requesting configuration...");
189 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
190 }
191 on_rez(integer num) {
192 llResetScript();
193 }
194 changed(integer change) {
195 if((change & CHANGED_INVENTORY) ||
196 (change & CHANGED_REGION_START) ||
197 (change & CHANGED_OWNER)) {
198 llResetScript();
199 }
200 }
201 state_exit() {
202 llSetTimerEvent(0);
203 }
204 }
205  
206 state listen_group {
207 state_entry() {
208 // DEBUG
209 llOwnerSay("[Survey] Waiting for group messages...");
210 }
211 link_message(integer sender, integer num, string message, key id) {
212 // We only care about notifications now.
213 if(id != "notification")
214 return;
215  
216 // This script only processes group notifications.
217 if(wasKeyValueGet("type", message) != "group" ||
218 (wasKeyValueGet("type", message) == "group" &&
219 wasURLUnescape(wasKeyValueGet("group", message)) !=
220 wasKeyValueGet("group", configuration)))
221 return;
222  
223 // Get the sent message.
224 data = wasURLUnescape(
225 wasKeyValueGet(
226 "message",
227 message
228 )
229 );
230  
231 // Check if this is an eggdrop command.
232 if(llGetSubString(data, 0, 0) !=
233 wasKeyValueGet("command", configuration))
234 return;
235  
236 // Check if the command matches the current module.
237 list command = llParseString2List(data, [" "], []);
238 if(llList2String(command, 0) !=
239 wasKeyValueGet("command", configuration) + "survey")
240 return;
241  
242 // Remove command.
243 command = llDeleteSubList(command, 0, 0);
244  
245 // Store the region name.
246 region = llStringTrim(
247 llDumpList2String(
248 command, " "
249 ),
250 STRING_TRIM
251 );
252  
253 if(llStringLength(region) == 0) {
254 data = "Must specify a region name";
255 state tell;
256 }
257  
258 llOwnerSay("[Survey] Checking region...");
259 queryID = llRequestSimulatorData(region, DATA_SIM_STATUS);
260 }
261 dataserver(key query_id, string data) {
262 if(query_id != queryID)
263 return;
264  
265 if(data == "unknown") {
266 data = "Region not found or in unknown state.";
267 state tell;
268 }
269  
270 if(data == "down") {
271 data = "Region is down.";
272 state tell;
273 }
274  
275 if(data == "starting") {
276 data = "Region is currently starting.";
277 state tell;
278 }
279  
280 if(data == "stopping") {
281 data = "Region is stopping.";
282 state tell;
283 }
284  
285 if(data == "crashed") {
286 data = "Region has crashed.";
287 state tell;
288 }
289  
290 // Teleport.
291 teleport_continue = "stats_trampoline";
292 state teleport_trampoline;
293 }
294 on_rez(integer num) {
295 llResetScript();
296 }
297 changed(integer change) {
298 if((change & CHANGED_INVENTORY) ||
299 (change & CHANGED_REGION_START) ||
300 (change & CHANGED_OWNER)) {
301 llResetScript();
302 }
303 }
304 }
305  
306 state teleport_trampoline {
307 state_entry() {
308 // DEBUG
309 llOwnerSay("Sleeping...");
310 llSetTimerEvent(3);
311 }
312 timer() {
313 state teleport;
314 }
315 on_rez(integer num) {
316 llResetScript();
317 }
318 changed(integer change) {
319 if((change & CHANGED_INVENTORY) ||
320 (change & CHANGED_REGION_START) ||
321 (change & CHANGED_OWNER)) {
322 llResetScript();
323 }
324 }
325 state_exit() {
326 llSetTimerEvent(0);
327 }
328 }
329  
330 state teleport {
331 state_entry() {
332 llOwnerSay("[Survey] Teleporting...");
333 llInstantMessage(
334 wasKeyValueGet(
335 "corrade",
336 configuration
337 ),
338 wasKeyValueEncode(
339 [
340 "command", "teleport",
341 "group", wasURLEscape(
342 wasKeyValueGet(
343 "group",
344 configuration
345 )
346 ),
347 "password", wasURLEscape(
348 wasKeyValueGet(
349 "password",
350 configuration
351 )
352 ),
353 "entity", "region",
354 "region", wasURLEscape(region),
355 "position", wasURLEscape("<128, 128, 4096>"),
356 "callback", wasURLEscape(
357 wasKeyValueGet(
358 "URL",
359 configuration
360 )
361 )
362 ]
363 )
364 );
365  
366 llSetTimerEvent(60);
367 }
368 link_message(integer sender, integer num, string body, key id) {
369 // Only process callbacks for the database command.
370 if(id != "callback" || wasKeyValueGet("command", body) != "teleport")
371 return;
372  
373 if(wasKeyValueGet("success", body) != "True") {
374 // DEBUG
375 llOwnerSay("[Survey] Teleport failed: " +
376 wasURLUnescape(
377 wasKeyValueGet("error", body)
378 )
379 );
380 state listen_group;
381 }
382  
383 if(teleport_continue == "stats_trampoline")
384 state stats_trampoline;
385  
386 if(teleport_continue == "tell")
387 state tell;
388  
389 llOwnerSay("[Survey] Jump tables corrupt, please contact vendor.");
390 llResetScript();
391 }
392 timer() {
393 data = "Teleport timed out...";
394 state tell;
395 }
396 on_rez(integer num) {
397 llResetScript();
398 }
399 changed(integer change) {
400 if((change & CHANGED_INVENTORY) ||
401 (change & CHANGED_REGION_START) ||
402 (change & CHANGED_OWNER)) {
403 llResetScript();
404 }
405 }
406 state_exit() {
407 llSetTimerEvent(0);
408 }
409 }
410  
411 state stats_trampoline {
412 state_entry() {
413 // DEBUG
414 llOwnerSay("Sleeping...");
415 llSetTimerEvent(3);
416 }
417 timer() {
418 state stats;
419 }
420 on_rez(integer num) {
421 llResetScript();
422 }
423 changed(integer change) {
424 if((change & CHANGED_INVENTORY) ||
425 (change & CHANGED_REGION_START) ||
426 (change & CHANGED_OWNER)) {
427 llResetScript();
428 }
429 }
430 state_exit() {
431 llSetTimerEvent(0);
432 }
433 }
434  
435 state stats {
436 state_entry() {
437 llSleep(10);
438 llOwnerSay("[Survey] Getting region statistics...");
439 llInstantMessage(
440 wasKeyValueGet(
441 "corrade",
442 configuration
443 ),
444 wasKeyValueEncode(
445 [
446 "command", "getregiondata",
447 "group", wasURLEscape(
448 wasKeyValueGet(
449 "group",
450 configuration
451 )
452 ),
453 "password", wasURLEscape(
454 wasKeyValueGet(
455 "password",
456 configuration
457 )
458 ),
459 "data", wasListToCSV(
460 [
461 "Stats.LastLag",
462 "Stats.Agents",
463 "Stats.Dilation",
464 "Stats.FPS",
465 "Stats.ActiveScripts",
466 "Stats.ScriptTime",
467 "Stats.Objects",
468 "Stats.PhysicsFPS",
469 "Stats.ScriptTime"
470 ]
471 ),
472 "callback", wasURLEscape(
473 wasKeyValueGet(
474 "URL",
475 configuration
476 )
477 )
478 ]
479 )
480 );
481  
482 llSetTimerEvent(300);
483 }
484 link_message(integer sender, integer num, string body, key id) {
485 // Only process callbacks for the database command.
486 if(id != "callback" || wasKeyValueGet("command", body) != "getregiondata")
487 return;
488  
489 if(wasKeyValueGet("success", body) != "True") {
490 // DEBUG
491 llOwnerSay("[Survey] Stats failed: " +
492 wasURLUnescape(
493 wasKeyValueGet("error", body)
494 )
495 );
496 state listen_group;
497 }
498  
499 list stat = wasCSVToList(
500 wasURLUnescape(
501 wasKeyValueGet(
502 "data",
503 body
504 )
505 )
506 );
507  
508 data = llDumpList2String(stat, " ");
509  
510 // Go home.
511 region = wasKeyValueGet(
512 "home region",
513 configuration
514 );
515 teleport_continue = "tell";
516 state teleport_trampoline;
517 }
518 timer() {
519 // Go home.
520 region = wasKeyValueGet(
521 "home region",
522 configuration
523 );
524 data = "Fetching data timed out...";
525 teleport_continue = "tell";
526 state teleport_trampoline;
527 }
528 on_rez(integer num) {
529 llResetScript();
530 }
531 changed(integer change) {
532 if((change & CHANGED_INVENTORY) ||
533 (change & CHANGED_REGION_START) ||
534 (change & CHANGED_OWNER)) {
535 llResetScript();
536 }
537 }
538 state_exit() {
539 llSetTimerEvent(0);
540 }
541 }
542  
543 state tell {
544 state_entry() {
545 // DEBUG
546 llOwnerSay("[Survey] Sending to group.");
547 llInstantMessage(
548 wasKeyValueGet(
549 "corrade",
550 configuration
551 ),
552 wasKeyValueEncode(
553 [
554 "command", "tell",
555 "group", wasURLEscape(
556 wasKeyValueGet(
557 "group",
558 configuration
559 )
560 ),
561 "password", wasURLEscape(
562 wasKeyValueGet(
563 "password",
564 configuration
565 )
566 ),
567 "entity", "group",
568 "message", wasURLEscape(data)
569 ]
570 )
571 );
572 state listen_group;
573 }
574 }