corrade-lsl-templates – Blame information for rev 29

Subversion Repositories:
Rev:
Rev Author Line No. Line
27 office 1 ///////////////////////////////////////////////////////////////////////////
29 office 2 // Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0 //
27 office 3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that sends the current Corrade heartbeat to group chat.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
29 office 10 // Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0 //
27 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 //
27 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 //
27 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 //
27 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 //
27 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 //
27 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 //
27 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 //
27 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 = "";
182  
183 default {
184 state_entry() {
185 llOwnerSay("[Heartbeat] Starting...");
186 llSetTimerEvent(10);
187 }
188 link_message(integer sender, integer num, string message, key id) {
189 if(id != "configuration") return;
190 llOwnerSay("[Heartbeat] Got configuration...");
191 configuration = message;
192 state listen_group;
193 }
194 timer() {
195 llOwnerSay("[Heartbeat] Requesting configuration...");
196 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
197 }
198 on_rez(integer num) {
199 llResetScript();
200 }
201 changed(integer change) {
202 if((change & CHANGED_INVENTORY) ||
203 (change & CHANGED_REGION_START) ||
204 (change & CHANGED_OWNER)) {
205 llResetScript();
206 }
207 }
208 state_exit() {
209 llSetTimerEvent(0);
210 }
211 }
212  
213 state listen_group {
214 state_entry() {
215 // DEBUG
216 llOwnerSay("[Heartbeat] Waiting for group messages...");
217 }
218 link_message(integer sender, integer num, string message, key id) {
219 // We only care about notifications now.
220 if(id != "notification")
221 return;
222  
223 // This script only processes group notifications.
224 if(wasKeyValueGet("type", message) != "group")
225 return;
226  
227 // Get the sent message.
228 data = wasURLUnescape(
229 wasKeyValueGet(
230 "message",
231 message
232 )
233 );
234  
235 // Check if this is an eggdrop command.
236 if(llGetSubString(data, 0, 0) !=
237 wasKeyValueGet("command", configuration))
238 return;
239  
240 // Check if the command matches the current module.
241 list command = llParseString2List(data, [" "], []);
242 if(llList2String(command, 0) !=
243 wasKeyValueGet("command", configuration) + "heartbeat")
244 return;
245  
246 // Remove command.
247 command = llDeleteSubList(command, 0, 0);
248  
249 // Get an URL.
250 state url;
251 }
252 on_rez(integer num) {
253 llResetScript();
254 }
255 changed(integer change) {
256 if((change & CHANGED_INVENTORY) ||
257 (change & CHANGED_REGION_START) ||
258 (change & CHANGED_OWNER)) {
259 llResetScript();
260 }
261 }
262 }
263  
264 state url {
265 state_entry() {
266 // DEBUG
267 llOwnerSay("[Heartbeat] Requesting URL...");
268 llRequestURL();
269 }
270 http_request(key id, string method, string body) {
271 if(method != URL_REQUEST_GRANTED) return;
272 URL = body;
273 // DEBUG
274 llOwnerSay("[Heartbeat] Got URL...");
275 state version;
276 }
277 on_rez(integer num) {
278 llResetScript();
279 }
280 changed(integer change) {
281 if((change & CHANGED_INVENTORY) ||
282 (change & CHANGED_REGION_START) ||
283 (change & CHANGED_OWNER)) {
284 llResetScript();
285 }
286 }
287 }
288  
289 state version {
290 state_entry() {
291 // DEBUG
292 llOwnerSay("[Heartbeat] Getting heartbeat data...");
293 llInstantMessage(
294 wasKeyValueGet(
295 "corrade",
296 configuration
297 ),
298 wasKeyValueEncode(
299 [
300 "command", "getheartbeatdata",
301 "group", wasURLEscape(
302 wasKeyValueGet(
303 "group",
304 configuration
305 )
306 ),
307 "password", wasURLEscape(
308 wasKeyValueGet(
309 "password",
310 configuration
311 )
312 ),
313 "data", wasListToCSV(
314 [
315 "AverageCPUUsage",
316 "AverageRAMUsage"
317 ]
318 ),
319 "callback", wasURLEscape(URL)
320 ]
321 )
322 );
323 llSetTimerEvent(60);
324 }
325 http_request(key id, string method, string body) {
326 llHTTPResponse(id, 200, "OK");
327 llReleaseURL(URL);
328 if(wasKeyValueGet("command", body) != "getheartbeatdata" ||
329 wasKeyValueGet("success", body) != "True") {
330 // DEBUG
331 llOwnerSay("[Heartbeat] Unable to get heartbeat data: " +
332 wasURLUnescape(
333 wasKeyValueGet("error", body)
334 )
335 );
336 state listen_group;
337 }
338  
339 list stats = wasCSVToList(
340 wasURLUnescape(
341 wasKeyValueGet(
342 "data",
343 body
344 )
345 )
346 );
347  
348 string CPU = llList2String(
349 stats,
350 llListFindList(
351 stats,
352 ["AverageCPUUsage"]
353 ) + 1
354 );
355  
356 // AverageRAMUsage is returned in bytes,
357 // so convert the value to MiB and round.
358 string RAM = (string)llRound(
359 llList2Float(
360 stats,
361 llListFindList(
362 stats,
363 ["AverageRAMUsage"]
364 ) + 1
365 ) / 1024 / 1024
366 );
367  
368 data = "RAM: " + RAM + "MiB" + " CPU: " + CPU + "%";
369  
370 state tell;
371 }
372 timer() {
373 llReleaseURL(URL);
374 state listen_group;
375 }
376 on_rez(integer num) {
377 llResetScript();
378 }
379 changed(integer change) {
380 if((change & CHANGED_INVENTORY) ||
381 (change & CHANGED_REGION_START) ||
382 (change & CHANGED_OWNER)) {
383 llResetScript();
384 }
385 }
386 state_exit() {
387 llSetTimerEvent(0);
388 }
389 }
390  
391 state tell {
392 state_entry() {
393 // DEBUG
394 llOwnerSay("[Heartbeat] Sending to group.");
395 llInstantMessage(
396 wasKeyValueGet(
397 "corrade",
398 configuration
399 ),
400 wasKeyValueEncode(
401 [
402 "command", "tell",
403 "group", wasURLEscape(
404 wasKeyValueGet(
405 "group",
406 configuration
407 )
408 ),
409 "password", wasURLEscape(
410 wasKeyValueGet(
411 "password",
412 configuration
413 )
414 ),
415 "entity", "group",
416 "message", wasURLEscape(data)
417 ]
418 )
419 );
420 state listen_group;
421 }
422 }