corrade-lsl-templates – Blame information for rev 21

Subversion Repositories:
Rev:
Rev Author Line No. Line
15 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that upgrades or downgrades Corrade via Stitch.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
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 ///////////////////////////////////////////////////////////////////////////
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 ///////////////////////////////////////////////////////////////////////////
165 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
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 = "";
182 string version = "";
183 string firstname = "";
184 string lastname = "";
185 string jump_state = "";
186  
187 default {
188 state_entry() {
189 llOwnerSay("[Stitch] Starting...");
190 llSetTimerEvent(10);
191 }
192 link_message(integer sender, integer num, string message, key id) {
193 if(id != "configuration") return;
194 llOwnerSay("[Stitch] Got configuration...");
195 configuration = message;
196 state listen_group;
197 }
198 timer() {
199 llOwnerSay("[Stitch] Requesting configuration...");
200 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
201 }
202 on_rez(integer num) {
203 llResetScript();
204 }
205 changed(integer change) {
206 if((change & CHANGED_INVENTORY) ||
207 (change & CHANGED_REGION_START) ||
208 (change & CHANGED_OWNER)) {
209 llResetScript();
210 }
211 }
212 state_exit() {
213 llSetTimerEvent(0);
214 }
215 }
216  
217 state listen_group {
218 state_entry() {
219 // DEBUG
220 llOwnerSay("[Stitch] Waiting for group messages...");
221 }
222 link_message(integer sender, integer num, string message, key id) {
223 // We only care about notifications now.
224 if(id != "notification")
225 return;
226  
227 // This script only processes group notifications.
228 if(wasKeyValueGet("type", message) != "group" &&
229 wasKeyValueGet("type", message) != "login")
230 return;
231  
232 // Process login notification.
233 if(wasKeyValueGet("type", message) == "login") {
234 string action = wasKeyValueGet("action", message);
235 string loginVersion = wasKeyValueGet("version", message);
236 if(action == "logout") {
237 version = loginVersion;
238 return;
239 }
240 if(action == "login" && loginVersion != version) {
241 data = "I have been stitched to version v" + loginVersion;
242 version = loginVersion;
243 state tell;
244 }
245 // Unknown login action.
246 return;
247 }
248  
249 // Get the sent message.
250 data = wasURLUnescape(
251 wasKeyValueGet(
252 "message",
253 message
254 )
255 );
256  
257 // Check if this is an eggdrop command.
258 if(llGetSubString(data, 0, 0) !=
259 wasKeyValueGet("command", configuration))
260 return;
261  
262 // Check if the command matches the current module.
263 list command = llParseString2List(data, [" "], []);
264 if(llList2String(command, 0) !=
265 wasKeyValueGet("command", configuration) + "stitch")
266 return;
267  
268 // Remove command.
269 command = llDeleteSubList(command, 0, 0);
270  
271 firstname = wasKeyValueGet("firstname", message);
272 lastname = wasKeyValueGet("lastname", message);
273  
274 if(firstname == "" || lastname == "") {
275 data = "And who would yarr be?";
276 state tell;
277 }
278  
279 // Dump the rest of the message.
280 data = llDumpList2String(command, " ");
281  
282 // Get an URL.
283 state url;
284 }
285 on_rez(integer num) {
286 llResetScript();
287 }
288 changed(integer change) {
289 if((change & CHANGED_INVENTORY) ||
290 (change & CHANGED_REGION_START) ||
291 (change & CHANGED_OWNER)) {
292 llResetScript();
293 }
294 }
295 }
296  
297 state url {
298 state_entry() {
299 // DEBUG
300 llOwnerSay("[Stitch] Requesting URL...");
301 llRequestURL();
302 }
303 http_request(key id, string method, string body) {
304 if(method != URL_REQUEST_GRANTED) return;
305 URL = body;
306 // DEBUG
307 llOwnerSay("[Stitch] Got URL...");
308 state get_caller_roles;
309 }
310 on_rez(integer num) {
311 llResetScript();
312 }
313 changed(integer change) {
314 if((change & CHANGED_INVENTORY) ||
315 (change & CHANGED_REGION_START) ||
316 (change & CHANGED_OWNER)) {
317 llResetScript();
318 }
319 }
320 }
321  
322 state get_caller_roles {
323 state_entry() {
324 // DEBUG
325 llOwnerSay("[Stitch] Searching for caller...");
326 llInstantMessage(
327 wasKeyValueGet(
328 "corrade",
329 configuration
330 ),
331 wasKeyValueEncode(
332 [
333 "command", "getmemberroles",
334 "group", wasURLEscape(
335 wasKeyValueGet(
336 "group",
337 configuration
338 )
339 ),
340 "password", wasURLEscape(
341 wasKeyValueGet(
342 "password",
343 configuration
344 )
345 ),
346 "firstname", firstname,
347 "lastname", lastname,
348 "callback", wasURLEscape(URL)
349 ]
350 )
351 );
352 llSetTimerEvent(60);
353 }
354 http_request(key id, string method, string body) {
355 llHTTPResponse(id, 200, "OK");
356 if(wasKeyValueGet("command", body) != "getmemberroles" ||
357 wasKeyValueGet("success", body) != "True") {
358 // DEBUG
359 llOwnerSay("[Stitch] Unable to get member roles: " +
360 wasURLUnescape(
361 wasKeyValueGet("error", body)
362 )
363 );
364 llReleaseURL(URL);
365 state listen_group;
366 }
367  
368 // Dump the roles to a list.
369 list roles = wasCSVToList(
370 wasURLUnescape(
371 wasKeyValueGet("data", body)
372 )
373 );
374  
375 if(llGetListLength(
376 wasSetIntersect(roles,
377 wasCSVToList(
378 wasKeyValueGet(
379 "admin roles", configuration
380 )
381 )
382 )
383 ) == 0) {
384 data = "You ain't got the cojones!";
385 llReleaseURL(URL);
386 state tell;
387 }
388  
389 list command = llParseString2List(data, [" "], []);
390  
391 version = llList2String(command, 0);
392  
393 // GC
394 command = [];
395  
396 if(version == "")
397 version = "\"latest\"";
398  
399 // Announce stitching process.
400 data = "Stitching to: " + version;
401 jump_state = "stitch";
402 state tell;
403 }
404 timer() {
405 llReleaseURL(URL);
406 state listen_group;
407 }
408 on_rez(integer num) {
409 llResetScript();
410 }
411 changed(integer change) {
412 if((change & CHANGED_INVENTORY) ||
413 (change & CHANGED_REGION_START) ||
414 (change & CHANGED_OWNER)) {
415 llResetScript();
416 }
417 }
418 state_exit() {
419 llSetTimerEvent(0);
420 }
421 }
422  
423 state stitch {
424 state_entry() {
425 // DEBUG
426 llOwnerSay("[Stitch] Stitching...");
427 llInstantMessage(
428 wasKeyValueGet(
429 "corrade",
430 configuration
431 ),
432 wasKeyValueEncode(
433 [
434 "command", "stitch",
435 "group", wasURLEscape(
436 wasKeyValueGet(
437 "group",
438 configuration
439 )
440 ),
441 "password", wasURLEscape(
442 wasKeyValueGet(
443 "password",
444 configuration
445 )
446 ),
447 "action", "stitch",
448 "version", version,
449 "callback", wasURLEscape(URL)
450 ]
451 )
452 );
453 llSetTimerEvent(60);
454 }
455 http_request(key id, string method, string body) {
456 llHTTPResponse(id, 200, "OK");
457 llReleaseURL(URL);
458 if(wasKeyValueGet("command", body) != "stitch" ||
459 wasKeyValueGet("success", body) != "True") {
460 // DEBUG
461 llOwnerSay("[Stitch] Unable to stitch: " +
462 wasURLUnescape(
463 wasKeyValueGet("error", body)
464 )
465 );
466 state listen_group;
467 }
468  
469 // Corrade proceeds with termination - cannot (should not) announce via "tell".
470 state listen_group;
471 }
472 timer() {
473 llReleaseURL(URL);
474 state listen_group;
475 }
476 on_rez(integer num) {
477 llResetScript();
478 }
479 changed(integer change) {
480 if((change & CHANGED_INVENTORY) ||
481 (change & CHANGED_REGION_START) ||
482 (change & CHANGED_OWNER)) {
483 llResetScript();
484 }
485 }
486 state_exit() {
487 llSetTimerEvent(0);
488 }
489 }
490  
491 state tell {
492 state_entry() {
493 // DEBUG
494 llOwnerSay("[Stitch] Sending to group.");
495 llInstantMessage(
496 wasKeyValueGet(
497 "corrade",
498 configuration
499 ),
500 wasKeyValueEncode(
501 [
502 "command", "tell",
503 "group", wasURLEscape(
504 wasKeyValueGet(
505 "group",
506 configuration
507 )
508 ),
509 "password", wasURLEscape(
510 wasKeyValueGet(
511 "password",
512 configuration
513 )
514 ),
515 "entity", "group",
516 "message", wasURLEscape(data)
517 ]
518 )
519 );
520  
521 // jump table
522 string nextState = jump_state;
523 jump_state = "";
524 if(nextState == "stitch")
525 state stitch;
526 state listen_group;
527 }
528 }