corrade-lsl-templates – Blame information for rev 36
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
15 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
29 | office | 2 | // Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0 // |
15 | office | 3 | /////////////////////////////////////////////////////////////////////////// |
4 | // |
||
5 | // A module using AI-SIML that allows group members to talk to Corrade. |
||
6 | // |
||
7 | /////////////////////////////////////////////////////////////////////////// |
||
8 | |||
9 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 10 | // Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 11 | /////////////////////////////////////////////////////////////////////////// |
12 | string wasKeyValueGet(string k, string data) { |
||
13 | if(llStringLength(data) == 0) return ""; |
||
14 | if(llStringLength(k) == 0) return ""; |
||
15 | list a = llParseString2List(data, ["&", "="], []); |
||
16 | integer i = llListFindList(a, [ k ]); |
||
17 | if(i != -1) return llList2String(a, i+1); |
||
18 | return ""; |
||
19 | } |
||
20 | |||
21 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 22 | // Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 37 | // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 49 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 73 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 112 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 141 | // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 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 | /////////////////////////////////////////////////////////////////////////// |
||
29 | office | 165 | // Copyright (C) 2017 Wizardry and Steamworks - License: CC BY 2.0 // |
15 | office | 166 | /////////////////////////////////////////////////////////////////////////// |
167 | list wasSetIntersect(list a, list b) { |
||
168 | if(llGetListLength(a) == 0) return []; |
||
169 | string i = llList2String(a, 0); |
||
170 | a = llDeleteSubList(a, 0, 0); |
||
171 | if(llListFindList(b, (list)i) == -1) |
||
172 | return wasSetIntersect(a, b); |
||
173 | return i + wasSetIntersect(a, b); |
||
174 | } |
||
175 | |||
176 | // configuration data |
||
177 | string configuration = ""; |
||
178 | // callback URL |
||
179 | string URL = ""; |
||
180 | // store message over state. |
||
181 | string data = ""; |
||
24 | office | 182 | string jump_table = ""; |
183 | string messageHash = ""; |
||
15 | office | 184 | |
185 | default { |
||
186 | state_entry() { |
||
187 | llOwnerSay("[AI] Starting..."); |
||
188 | llSetTimerEvent(10); |
||
189 | } |
||
190 | link_message(integer sender, integer num, string message, key id) { |
||
191 | if(id != "configuration") return; |
||
192 | llOwnerSay("[AI] Got configuration..."); |
||
193 | configuration = message; |
||
24 | office | 194 | |
195 | // Subscribe to MQTT messages. |
||
196 | jump_table = "subscribe"; |
||
197 | state url; |
||
15 | office | 198 | } |
199 | timer() { |
||
200 | llOwnerSay("[AI] Requesting configuration..."); |
||
201 | llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); |
||
202 | } |
||
203 | on_rez(integer num) { |
||
204 | llResetScript(); |
||
205 | } |
||
206 | changed(integer change) { |
||
207 | if((change & CHANGED_INVENTORY) || |
||
208 | (change & CHANGED_REGION_START) || |
||
209 | (change & CHANGED_OWNER)) { |
||
210 | llResetScript(); |
||
211 | } |
||
212 | } |
||
213 | state_exit() { |
||
214 | llSetTimerEvent(0); |
||
215 | } |
||
216 | } |
||
217 | |||
24 | office | 218 | state url { |
219 | state_entry() { |
||
220 | // DEBUG |
||
221 | llOwnerSay("[AI] Requesting URL..."); |
||
222 | llRequestURL(); |
||
223 | } |
||
224 | http_request(key id, string method, string body) { |
||
225 | if(method != URL_REQUEST_GRANTED) return; |
||
226 | URL = body; |
||
227 | |||
228 | // DEBUG |
||
229 | llOwnerSay("[AI] Got URL..."); |
||
230 | |||
231 | if(jump_table == "subscribe") |
||
232 | state subscribe; |
||
233 | if(jump_table == "publish") |
||
234 | state publish; |
||
235 | } |
||
236 | on_rez(integer num) { |
||
237 | llResetScript(); |
||
238 | } |
||
239 | changed(integer change) { |
||
240 | if((change & CHANGED_INVENTORY) || |
||
241 | (change & CHANGED_REGION_START) || |
||
242 | (change & CHANGED_OWNER)) { |
||
243 | llResetScript(); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | state subscribe { |
||
249 | state_entry() { |
||
250 | // DEBUG |
||
251 | llOwnerSay("[AI] Subscribing to Corrade AI..."); |
||
252 | llInstantMessage( |
||
253 | wasKeyValueGet( |
||
254 | "corrade", |
||
255 | configuration |
||
256 | ), |
||
257 | wasKeyValueEncode( |
||
258 | [ |
||
259 | "command", "MQTT", |
||
260 | "group", wasURLEscape( |
||
261 | wasKeyValueGet( |
||
262 | "group", |
||
263 | configuration |
||
264 | ) |
||
265 | ), |
||
266 | "password", wasURLEscape( |
||
267 | wasKeyValueGet( |
||
268 | "password", |
||
269 | configuration |
||
270 | ) |
||
271 | ), |
||
272 | // Subscribe to Corrade AI |
||
273 | "action", "subscribe", |
||
274 | "id", wasURLEscape( |
||
275 | wasKeyValueGet( |
||
276 | "ai subscription", |
||
277 | configuration |
||
278 | ) |
||
279 | ), |
||
280 | // Corrade AI listening host. |
||
281 | "host", wasURLEscape( |
||
282 | wasKeyValueGet( |
||
283 | "ai host", |
||
284 | configuration |
||
285 | ) |
||
286 | ), |
||
287 | // Corrade AI listening port. |
||
288 | "port", wasURLEscape( |
||
289 | wasKeyValueGet( |
||
290 | "ai port", |
||
291 | configuration |
||
292 | ) |
||
293 | ), |
||
294 | // Corrade AI credentials. |
||
295 | "username", wasURLEscape( |
||
296 | wasKeyValueGet( |
||
297 | "ai username", |
||
298 | configuration |
||
299 | ) |
||
300 | ), |
||
301 | "secret", wasURLEscape( |
||
302 | wasKeyValueGet( |
||
303 | "ai secret", |
||
304 | configuration |
||
305 | ) |
||
306 | ), |
||
307 | // Use the SIML module of Corrade AI. |
||
308 | "topic", "SIML", |
||
309 | // Send the result of the MQTT command to this URL. |
||
310 | "callback", wasURLEscape(URL) |
||
311 | ] |
||
312 | ) |
||
313 | ); |
||
314 | } |
||
315 | |||
316 | http_request(key id, string method, string body) { |
||
317 | llHTTPResponse(id, 200, "OK"); |
||
318 | llReleaseURL(URL); |
||
319 | if(wasKeyValueGet("command", body) != "MQTT" || |
||
320 | wasKeyValueGet("success", body) != "True") { |
||
321 | // DEBUG |
||
322 | llOwnerSay("[AI] Unable to subscribe to MQTT topic: " + |
||
323 | wasURLUnescape( |
||
324 | wasKeyValueGet("error", body) |
||
325 | ) |
||
326 | ); |
||
327 | llResetScript(); |
||
328 | } |
||
329 | |||
330 | state listen_group; |
||
331 | } |
||
332 | on_rez(integer num) { |
||
333 | llResetScript(); |
||
334 | } |
||
335 | changed(integer change) { |
||
336 | if((change & CHANGED_INVENTORY) || |
||
337 | (change & CHANGED_REGION_START) || |
||
338 | (change & CHANGED_OWNER)) { |
||
339 | llResetScript(); |
||
340 | } |
||
341 | } |
||
342 | state_exit() { |
||
343 | llSetTimerEvent(0); |
||
344 | } |
||
345 | } |
||
346 | |||
15 | office | 347 | state listen_group { |
348 | state_entry() { |
||
349 | // DEBUG |
||
350 | llOwnerSay("[AI] Waiting for group messages..."); |
||
351 | } |
||
352 | link_message(integer sender, integer num, string message, key id) { |
||
353 | // We only care about notifications now. |
||
354 | if(id != "notification") |
||
355 | return; |
||
356 | |||
24 | office | 357 | // Listen to group message notifications. |
15 | office | 358 | if(wasKeyValueGet("type", message) != "group") |
359 | return; |
||
360 | |||
361 | // Get the sent message. |
||
362 | data = wasURLUnescape( |
||
363 | wasKeyValueGet( |
||
364 | "message", |
||
365 | message |
||
366 | ) |
||
367 | ); |
||
368 | |||
369 | // Check if this is an eggdrop command. |
||
370 | if(llGetSubString(data, 0, 0) != |
||
371 | wasKeyValueGet("command", configuration)) |
||
372 | return; |
||
373 | |||
374 | // Check if the command matches the current module. |
||
375 | list command = llParseString2List(data, [" "], []); |
||
376 | if(llList2String(command, 0) != |
||
377 | wasKeyValueGet("command", configuration) + |
||
378 | wasKeyValueGet("nickname", configuration)) |
||
379 | return; |
||
380 | |||
381 | // Remove command. |
||
382 | command = llDeleteSubList(command, 0, 0); |
||
383 | |||
384 | // Dump the rest of the message. |
||
385 | data = llDumpList2String(command, " "); |
||
386 | |||
387 | // Get an URL. |
||
24 | office | 388 | jump_table = "publish"; |
15 | office | 389 | state url; |
390 | } |
||
391 | on_rez(integer num) { |
||
392 | llResetScript(); |
||
393 | } |
||
394 | changed(integer change) { |
||
395 | if((change & CHANGED_INVENTORY) || |
||
396 | (change & CHANGED_REGION_START) || |
||
397 | (change & CHANGED_OWNER)) { |
||
398 | llResetScript(); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
24 | office | 403 | state publish { |
15 | office | 404 | state_entry() { |
405 | // DEBUG |
||
406 | llOwnerSay("[AI] Sending to AI for processing..."); |
||
24 | office | 407 | |
408 | messageHash = llSHA1String(data); |
||
409 | |||
15 | office | 410 | llInstantMessage( |
411 | wasKeyValueGet( |
||
412 | "corrade", |
||
413 | configuration |
||
414 | ), |
||
415 | wasKeyValueEncode( |
||
416 | [ |
||
24 | office | 417 | "command", "MQTT", |
15 | office | 418 | "group", wasURLEscape( |
419 | wasKeyValueGet( |
||
420 | "group", |
||
421 | configuration |
||
422 | ) |
||
423 | ), |
||
424 | "password", wasURLEscape( |
||
425 | wasKeyValueGet( |
||
426 | "password", |
||
427 | configuration |
||
428 | ) |
||
429 | ), |
||
24 | office | 430 | "action", "publish", |
431 | // Corrade AI listening host. |
||
432 | "host", wasURLEscape( |
||
433 | wasKeyValueGet( |
||
434 | "ai host", |
||
435 | configuration |
||
436 | ) |
||
437 | ), |
||
438 | // Corrade AI listening port. |
||
439 | "port", wasURLEscape( |
||
440 | wasKeyValueGet( |
||
441 | "ai port", |
||
442 | configuration |
||
443 | ) |
||
444 | ), |
||
445 | // Corrade AI credentials. |
||
446 | "username", wasURLEscape( |
||
447 | wasKeyValueGet( |
||
448 | "ai username", |
||
449 | configuration |
||
450 | ) |
||
451 | ), |
||
452 | "secret", wasURLEscape( |
||
453 | wasKeyValueGet( |
||
454 | "ai secret", |
||
455 | configuration |
||
456 | ) |
||
457 | ), |
||
458 | // Use the SIML module of Corrade AI. |
||
459 | "topic", "SIML", |
||
460 | "payload", wasURLEscape( |
||
461 | wasKeyValueEncode( |
||
462 | [ |
||
463 | // The hash is an identifier that will allow responses from Corrade AI |
||
464 | // for various messages to be distinguished. It can be any identifier |
||
465 | // but a handy way of generating an identifier is to hash the message. |
||
466 | "Hash", messageHash, |
||
467 | // Note the double escaping! |
||
468 | "Message", wasURLEscape(data) |
||
469 | ] |
||
470 | ) |
||
471 | ), |
||
15 | office | 472 | "callback", wasURLEscape(URL) |
473 | ] |
||
474 | ) |
||
475 | ); |
||
476 | llSetTimerEvent(60); |
||
477 | } |
||
478 | http_request(key id, string method, string body) { |
||
479 | llHTTPResponse(id, 200, "OK"); |
||
480 | llReleaseURL(URL); |
||
24 | office | 481 | if(wasKeyValueGet("command", body) != "MQTT" || |
15 | office | 482 | wasKeyValueGet("success", body) != "True") { |
483 | // DEBUG |
||
24 | office | 484 | llOwnerSay("[AI] Unable to publish message: " + |
15 | office | 485 | wasURLUnescape( |
24 | office | 486 | wasKeyValueGet("data", body) |
15 | office | 487 | ) |
488 | ); |
||
489 | state listen_group; |
||
490 | } |
||
491 | |||
24 | office | 492 | // DEBUG |
493 | llOwnerSay("[AI] Message published successfully..."); |
||
494 | } |
||
495 | link_message(integer sender, integer num, string message, key id) { |
||
496 | // We only care about notifications now. |
||
497 | if(id != "notification") |
||
498 | return; |
||
15 | office | 499 | |
24 | office | 500 | // Listen to MQTT messages. |
501 | if(wasKeyValueGet("type", message) != "MQTT") |
||
502 | return; |
||
503 | |||
504 | // Get the sent message. |
||
505 | data = wasURLUnescape( |
||
506 | wasKeyValueGet( |
||
507 | "payload", |
||
508 | message |
||
509 | ) |
||
510 | ); |
||
511 | |||
512 | string hash = wasURLUnescape( |
||
513 | wasKeyValueGet( |
||
514 | "Hash", |
||
515 | data |
||
516 | ) |
||
517 | ); |
||
518 | |||
519 | string serverMessage = wasURLUnescape( |
||
520 | wasKeyValueGet( |
||
521 | "ServerMessage", |
||
522 | data |
||
523 | ) |
||
524 | ); |
||
525 | |||
526 | // Skip generated messages that are not for the published message. |
||
527 | if(hash != messageHash || |
||
528 | serverMessage != "True") |
||
529 | return; |
||
530 | |||
531 | data = wasURLUnescape( |
||
532 | wasKeyValueGet( |
||
533 | "Message", |
||
534 | data |
||
535 | ) |
||
536 | ); |
||
537 | |||
15 | office | 538 | state tell; |
539 | } |
||
540 | timer() { |
||
541 | llReleaseURL(URL); |
||
542 | state listen_group; |
||
543 | } |
||
544 | on_rez(integer num) { |
||
545 | llResetScript(); |
||
546 | } |
||
547 | changed(integer change) { |
||
548 | if((change & CHANGED_INVENTORY) || |
||
549 | (change & CHANGED_REGION_START) || |
||
550 | (change & CHANGED_OWNER)) { |
||
551 | llResetScript(); |
||
552 | } |
||
553 | } |
||
554 | state_exit() { |
||
555 | llSetTimerEvent(0); |
||
556 | } |
||
557 | } |
||
558 | |||
559 | state tell { |
||
560 | state_entry() { |
||
561 | // DEBUG |
||
562 | llOwnerSay("[AI] Sending to group."); |
||
563 | llInstantMessage( |
||
564 | wasKeyValueGet( |
||
565 | "corrade", |
||
566 | configuration |
||
567 | ), |
||
568 | wasKeyValueEncode( |
||
569 | [ |
||
570 | "command", "tell", |
||
571 | "group", wasURLEscape( |
||
572 | wasKeyValueGet( |
||
573 | "group", |
||
574 | configuration |
||
575 | ) |
||
576 | ), |
||
577 | "password", wasURLEscape( |
||
578 | wasKeyValueGet( |
||
579 | "password", |
||
580 | configuration |
||
581 | ) |
||
582 | ), |
||
583 | "entity", "group", |
||
24 | office | 584 | "message", wasURLEscape(data) |
15 | office | 585 | ] |
586 | ) |
||
587 | ); |
||
588 | state listen_group; |
||
589 | } |
||
590 | } |