corrade-lsl-templates – Blame information for rev 14

Subversion Repositories:
Rev:
Rev Author Line No. Line
8 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A wiki module that can memorize strings and recall them by path.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
11 ///////////////////////////////////////////////////////////////////////////
12 integer wasIsAlNum(string a) {
13 if(a == "") return FALSE;
14 integer x = llBase64ToInteger("AAAA" +
15 llStringToBase64(llGetSubString(a, 0, 0)));
16 return (x >= 65 && x <= 90) || (x >= 97 && x <= 122) ||
17 (x >= 48 && x <= 57);
18 }
19 ///////////////////////////////////////////////////////////////////////////
20 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
21 ///////////////////////////////////////////////////////////////////////////
22 string wasKeyValueGet(string k, string data) {
23 if(llStringLength(data) == 0) return "";
24 if(llStringLength(k) == 0) return "";
25 list a = llParseString2List(data, ["&", "="], []);
26 integer i = llListFindList(a, [ k ]);
27 if(i != -1) return llList2String(a, i+1);
28 return "";
29 }
30  
31 ///////////////////////////////////////////////////////////////////////////
32 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 //
33 ///////////////////////////////////////////////////////////////////////////
34 string wasKeyValueEncode(list data) {
35 list k = llList2ListStrided(data, 0, -1, 2);
36 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
37 data = [];
38 do {
39 data += llList2String(k, 0) + "=" + llList2String(v, 0);
40 k = llDeleteSubList(k, 0, 0);
41 v = llDeleteSubList(v, 0, 0);
42 } while(llGetListLength(k) != 0);
43 return llDumpList2String(data, "&");
44 }
45  
46 ///////////////////////////////////////////////////////////////////////////
47 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
48 ///////////////////////////////////////////////////////////////////////////
49 // escapes a string in conformance with RFC1738
50 string wasURLEscape(string i) {
51 string o = "";
52 do {
53 string c = llGetSubString(i, 0, 0);
54 i = llDeleteSubString(i, 0, 0);
55 if(c == "") jump continue;
56 if(c == " ") {
57 o += "+";
58 jump continue;
59 }
60 if(c == "\n") {
61 o += "%0D" + llEscapeURL(c);
62 jump continue;
63 }
64 o += llEscapeURL(c);
65 @continue;
66 } while(i != "");
67 return o;
68 }
69  
70 ///////////////////////////////////////////////////////////////////////////
71 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
72 ///////////////////////////////////////////////////////////////////////////
73 list wasCSVToList(string csv) {
74 list l = [];
75 list s = [];
76 string m = "";
77 do {
78 string a = llGetSubString(csv, 0, 0);
79 csv = llDeleteSubString(csv, 0, 0);
80 if(a == ",") {
81 if(llList2String(s, -1) != "\"") {
82 l += m;
83 m = "";
84 jump continue;
85 }
86 m += a;
87 jump continue;
88 }
89 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
90 m += a;
91 csv = llDeleteSubString(csv, 0, 0);
92 jump continue;
93 }
94 if(a == "\"") {
95 if(llList2String(s, -1) != a) {
96 s += a;
97 jump continue;
98 }
99 s = llDeleteSubList(s, -1, -1);
100 jump continue;
101 }
102 m += a;
103 @continue;
104 } while(csv != "");
105 // postcondition: length(s) = 0
106 return l + m;
107 }
108  
109 ///////////////////////////////////////////////////////////////////////////
110 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
111 ///////////////////////////////////////////////////////////////////////////
112 string wasListToCSV(list l) {
113 list v = [];
114 do {
115 string a = llDumpList2String(
116 llParseStringKeepNulls(
117 llList2String(
118 l,
119  
120 ),
121 ["\""],
122 []
123 ),
124 "\"\""
125 );
126 if(llParseStringKeepNulls(
127 a,
128 [" ", ",", "\n", "\""], []
129 ) !=
130 (list) a
131 ) a = "\"" + a + "\"";
132 v += a;
133 l = llDeleteSubList(l, 0, 0);
134 } while(l != []);
135 return llDumpList2String(v, ",");
136 }
137  
138 ///////////////////////////////////////////////////////////////////////////
139 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
140 ///////////////////////////////////////////////////////////////////////////
141 // unescapes a string in conformance with RFC1738
142 string wasURLUnescape(string i) {
143 return llUnescapeURL(
144 llDumpList2String(
145 llParseString2List(
146 llDumpList2String(
147 llParseString2List(
148 i,
149 ["+"],
150 []
151 ),
152 " "
153 ),
154 ["%0D%0A"],
155 []
156 ),
157 "\n"
158 )
159 );
160 }
161  
162 // configuration data
163 string configuration = "";
164 // callback URL
165 string URL = "";
166 // store message over state.
12 office 167 string path = "";
8 office 168 string data = "";
12 office 169 string action = "";
170 string statement = "";
171 string parameters = "";
8 office 172  
173 default {
174 state_entry() {
175 llOwnerSay("[Wiki] Starting...");
176 llSetTimerEvent(10);
177 }
178 link_message(integer sender, integer num, string message, key id) {
179 if(id != "configuration") return;
180 llOwnerSay("[Wiki] Got configuration...");
181 configuration = message;
12 office 182 action = "create";
8 office 183 state url;
184 }
185 timer() {
186 llOwnerSay("[Wiki] Requesting configuration...");
187 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
188 }
189 on_rez(integer num) {
190 llResetScript();
191 }
192 changed(integer change) {
193 if((change & CHANGED_INVENTORY) ||
194 (change & CHANGED_REGION_START) ||
195 (change & CHANGED_OWNER)) {
196 llResetScript();
197 }
198 }
199 state_exit() {
200 llSetTimerEvent(0);
201 }
202 }
203  
204 state url {
205 state_entry() {
206 // DEBUG
207 llOwnerSay("[Wiki] Requesting URL...");
208 llRequestURL();
209 }
210 http_request(key id, string method, string body) {
211 if(method != URL_REQUEST_GRANTED) return;
212 URL = body;
213 // DEBUG
12 office 214 llOwnerSay("[Wiki] Got URL.");
8 office 215  
12 office 216 if(action == "create") {
217 statement = wasURLEscape("CREATE TABLE IF NOT EXISTS \"" +
218 wasKeyValueGet("wiki table", configuration) +
219 "\" (path text unique collate nocase, data text)");
220 state query;
8 office 221 }
12 office 222  
223 if(action == "get") {
224 statement = wasURLEscape("SELECT data FROM \"" +
225 wasKeyValueGet("wiki table", configuration) +
226 "\" WHERE path=:path");
227 parameters = wasURLEscape(
228 wasListToCSV(
229 [
230 "path",
231 wasURLEscape(path)
232 ]
8 office 233 )
234 );
12 office 235 state query;
8 office 236 }
12 office 237  
238 if(action == "set") {
239 if(data == "") {
240 statement = wasURLEscape("DELETE FROM \"" +
241 wasKeyValueGet("wiki table", configuration) +
242 "\" WHERE path=:path");
243 parameters = wasURLEscape(
244 wasListToCSV(
245 [
246 "path",
247 wasURLEscape(path)
248 ]
249 )
250 );
251 state query;
252 }
253 statement = wasURLEscape("REPLACE INTO \"" +
254 wasKeyValueGet("wiki table", configuration) +
255 "\" (path, data) VALUES (:path, :data)");
256 parameters = wasURLEscape(
257 wasListToCSV(
258 [
259 "path",
260 wasURLEscape(path),
261 "data",
262 wasURLEscape(data)
263 ]
264 )
265 );
266 state query;
267 }
268  
269 if(action == "dir") {
270 if(path == "/") {
271 path = "";
14 office 272 statement = wasURLEscape(
273 "SELECT DISTINCT SUBSTR(path, 1, LENGTH(path) - LENGTH(LTRIM(SUBSTR(path,2), 'abcdefghijklmnopqrstuvwxyz'))) AS path FROM \"" +
274 wasKeyValueGet("wiki table", configuration) +
275 "\" WHERE path LIKE '/%' LIMIT " +
276 wasKeyValueGet("wiki results limit", configuration)
277 );
12 office 278 state query;
279 }
14 office 280 statement = wasURLEscape(
281 "SELECT DISTINCT SUBSTR(REPLACE(path, :base, ''),1, LENGTH(REPLACE(path, :base, '')) - LENGTH(LTRIM(REPLACE(path, :base, ''), 'abcdefghijklmnopqrstuvwxyz'))) AS path FROM \"" +
282 wasKeyValueGet("wiki table", configuration) +
283 "\" WHERE path LIKE :path LIMIT " +
284 wasKeyValueGet("wiki results limit", configuration)
285 );
12 office 286 parameters = wasURLEscape(
287 wasListToCSV(
288 [
289 "path",
290 wasURLEscape(path + "/" + "%"),
291 "base",
14 office 292 wasURLEscape("/" +
293 llDumpList2String(
294 llParseString2List(
295 path,
296 ["/"],
297 []
298 ),
299 "/"
300 ) +
301 "/"
302 )
12 office 303 ]
304 )
305 );
306 state query;
307 }
308  
14 office 309 if(action == "find") {
310 if(data == "") {
311 data = "Command requires two parameters: a path followed by a search term.";
312 state tell;
313 }
314 if(path == "/")
315 path = "";
316 statement = wasURLEscape(
317 "SELECT DISTINCT path FROM \"" +
318 wasKeyValueGet("wiki table", configuration) +
319 "\" WHERE path LIKE :path AND ( data LIKE :data OR path LIKE :data ) COLLATE NOCASE LIMIT " +
320 wasKeyValueGet("wiki search limit", configuration)
321 );
322 parameters = wasURLEscape(
323 wasListToCSV(
324 [
325 "path",
326 wasURLEscape(path + "/" + "%"),
327 "data",
328 wasURLEscape("%" + data + "%")
329 ]
330 )
331 );
332 state query;
333 }
334  
12 office 335 // DEBUG
336 llOwnerSay("[Wiki] Jump table corrupted, please contact creator...");
337 llResetScript();
8 office 338 }
339 on_rez(integer num) {
340 llResetScript();
341 }
342 changed(integer change) {
343 if((change & CHANGED_INVENTORY) ||
344 (change & CHANGED_REGION_START) ||
345 (change & CHANGED_OWNER)) {
346 llResetScript();
347 }
348 }
349 }
350  
351 state listen_group {
352 state_entry() {
353 // DEBUG
354 llOwnerSay("[Wiki] Waiting for group messages...");
355 }
356 link_message(integer sender, integer num, string message, key id) {
357 // We only care about notifications now.
358 if(id != "notification")
359 return;
360  
361 // This script only processes group notifications.
362 if(wasKeyValueGet("type", message) != "group")
363 return;
364  
365 // Get the sent message.
366 data = wasURLUnescape(
367 wasKeyValueGet(
368 "message",
369 message
370 )
371 );
372  
11 office 373 // Check if this is an eggdrop command.
8 office 374 if(llGetSubString(data, 0, 0) !=
375 wasKeyValueGet("command", configuration))
376 return;
11 office 377  
378 // Check if the command matches the current module.
12 office 379 list command = llParseString2List(data, [" "], []);
380 if(llList2String(command, 0) !=
381 wasKeyValueGet("command", configuration) + "wiki")
8 office 382 return;
383  
384 // Remove command.
385 command = llDeleteSubList(command, 0, 0);
386  
387 // Check for supported sub-commands.
388 if(llList2String(command, 0) != "set" &&
389 llList2String(command, 0) != "get" &&
14 office 390 llList2String(command, 0) != "dir" &&
391 llList2String(command, 0) != "find") {
392 data = "Subcommands are: get, set, dir or find";
8 office 393 state tell;
394 }
395  
396 // Get the sub-command and store it as a jump state.
12 office 397 action = llList2String(command, 0);
8 office 398  
399 // Remove sub-command.
400 command = llDeleteSubList(command, 0, 0);
401  
402 // Get the path parts.
403 list path_parts = llParseString2List(
404 llList2String(command, 0), ["/"], []
405 );
406  
407 // Dump the path and store it over states.
408 path = llStringTrim(
409 llDumpList2String(
410 path_parts,
411 "/"
412 ),
413 STRING_TRIM
414 );
415  
416 if(path != "") {
417 integer i = llStringLength(path) - 1;
418 do {
419 string c = llGetSubString(path, i, i);
420 if(c != "/" && !wasIsAlNum(c)) {
421 data = "Only alpha-numerics accepted in the path string.";
422 state tell;
423 }
424 } while(--i > -1);
425 }
426  
427 path = "/" + path;
428  
429 // Remove path.
430 command = llDeleteSubList(command, 0, 0);
431  
432 // Dump the rest of the message.
433 data = llDumpList2String(command, " ");
434  
435 // Get an URL.
436 state url;
437 }
438 on_rez(integer num) {
439 llResetScript();
440 }
441 changed(integer change) {
442 if((change & CHANGED_INVENTORY) ||
443 (change & CHANGED_REGION_START) ||
444 (change & CHANGED_OWNER)) {
445 llResetScript();
446 }
447 }
448 }
449  
12 office 450 state query {
8 office 451 state_entry() {
14 office 452 // Check messge length.
453 string message = wasKeyValueEncode(
454 [
455 "command", "database",
456 "group", wasURLEscape(
457 wasKeyValueGet(
458 "group",
459 configuration
460 )
461 ),
462 "password", wasURLEscape(
463 wasKeyValueGet(
464 "password",
465 configuration
466 )
467 ),
468 "SQL", statement,
469 "data", parameters,
470 "callback", wasURLEscape(URL)
471 ]
472 );
473 // GC - none of these are needed anymore.
474 statement = "";
475 parameters = "";
476 if(llStringLength(message) > 1023) {
477 data = "Message length exceeded 1023 characters.";
478 state tell;
479 }
8 office 480 // DEBUG
12 office 481 llOwnerSay("[Wiki] Executing action: " + action);
8 office 482 llInstantMessage(
483 wasKeyValueGet(
484 "corrade",
485 configuration
486 ),
14 office 487 message
8 office 488 );
14 office 489 // GC
490 message = "";
8 office 491 llSetTimerEvent(60);
492 }
493 http_request(key id, string method, string body) {
494 llHTTPResponse(id, 200, "OK");
495 llReleaseURL(URL);
496 if(wasKeyValueGet("command", body) != "database" ||
497 wasKeyValueGet("success", body) != "True") {
498 // DEBUG
12 office 499 llOwnerSay("[Wiki] Unable query database: " +
8 office 500 wasURLUnescape(
501 wasKeyValueGet("error", body)
502 )
503 );
504 state listen_group;
505 }
12 office 506  
507 // Process actions.
508  
509 if(action == "set") {
510 if(data == "") {
511 data = "Deleted from " + path;
512 state tell;
513 }
514 data = "Stored into " + path;
8 office 515 state tell;
516 }
14 office 517  
518 if(action == "find") {
519 data = llDumpList2String(
520 llList2ListStrided(
521 llDeleteSubList(
522 wasCSVToList(
523 wasURLUnescape(
524 wasKeyValueGet("data", body)
525 )
526 ),
527 0,
528  
529 ),
530 0,
531 -1,
532 2
533 ),
534 ","
535 );
536 if(data == "") {
537 data = "Sorry, the term was not found.";
538 state tell;
539 }
540 state tell;
541 }
542  
12 office 543 if(action == "get") {
544 data = llDumpList2String(
545 llDeleteSubList(
546 wasCSVToList(
547 wasURLUnescape(
548 wasKeyValueGet("data", body)
8 office 549 )
550 ),
12 office 551 0,
552  
553 ),
554 ""
8 office 555 );
556  
12 office 557 if(data == "") {
558 data = "Sorry, that path contains no data.";
559 state tell;
560 }
8 office 561  
12 office 562 data = path + ": " + data;
8 office 563 state tell;
564 }
565  
12 office 566 if(action == "dir") {
567 list paths = llList2ListStrided(
568 llDeleteSubList(
569 wasCSVToList(
570 wasURLUnescape(
571 wasKeyValueGet("data", body)
8 office 572 )
573 ),
12 office 574 0,
575  
8 office 576 ),
577 0,
12 office 578 -1,
579 2
580 );
8 office 581  
12 office 582 if(llGetListLength(paths) == 0) {
583 data = "Sorry, that path contains no sub-paths.";
584 state tell;
585 }
8 office 586  
12 office 587 // Eliminate path component.
588 if(path == "/")
589 path = "";
8 office 590  
12 office 591 list sibling = [];
592 do {
593 // Get the path part.
594 string part = llList2String(paths, 0);
8 office 595  
12 office 596 // Remove the path component.
597 string child = llStringTrim(
598 llDumpList2String(
599 llParseString2List(
600 part,
601 [path, "/"],
602 []
603 ),
604 "/"
8 office 605 ),
12 office 606 STRING_TRIM
8 office 607  
12 office 608 );
8 office 609  
12 office 610 integer i = llSubStringIndex(child, "/");
611 if(i == -1) {
612 sibling += path + "/" + child;
613 jump continue_dir;
614 }
615 child = path + "/" + llDeleteSubString(child, i, -1) + "/";
616 if(llListFindList(sibling, (list)child) == -1)
617 sibling += child;
618 @continue_dir;
619 paths = llDeleteSubList(paths, 0, 0);
620 } while(llGetListLength(paths) != 0);
8 office 621  
12 office 622 data = llList2CSV(sibling);
623 // GC
624 sibling = [];
8 office 625  
12 office 626 state tell;
627 }
628  
629 // Don't announce creating table.
630 if(action == "create")
631 state listen_group;
632  
633 // DEBUG
634 llOwnerSay("[Wiki] Jump table corrupted, please contact creator...");
635 state listen_group;
8 office 636 }
637 timer() {
638 llReleaseURL(URL);
639 state listen_group;
640 }
641 on_rez(integer num) {
642 llResetScript();
643 }
644 changed(integer change) {
645 if((change & CHANGED_INVENTORY) ||
646 (change & CHANGED_REGION_START) ||
647 (change & CHANGED_OWNER)) {
648 llResetScript();
649 }
650 }
651 state_exit() {
652 llSetTimerEvent(0);
653 }
654 }
655  
656 state tell {
657 state_entry() {
658 // DEBUG
659 llOwnerSay("[Wiki] Sending to group.");
660 llInstantMessage(
661 wasKeyValueGet(
662 "corrade",
663 configuration
664 ),
665 wasKeyValueEncode(
666 [
667 "command", "tell",
668 "group", wasURLEscape(
669 wasKeyValueGet(
670 "group",
671 configuration
672 )
673 ),
674 "password", wasURLEscape(
675 wasKeyValueGet(
676 "password",
677 configuration
678 )
679 ),
680 "entity", "group",
681 "message", wasURLEscape(data)
682 ]
683 )
684 );
14 office 685 // GC
686 path = "";
687 data = "";
8 office 688 state listen_group;
689 }
690 }