corrade-lsl-templates – Blame information for rev 8

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 module that spanks group members using fuzzy name matching. . .
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 // configuration data
165 string configuration = "";
166 // callback URL
167 string URL = "";
168 // store message over state.
169 string data = "";
170  
171 default {
172 state_entry() {
173 llOwnerSay("[Spank] Starting...");
174 llSetTimerEvent(10);
175 }
176 link_message(integer sender, integer num, string message, key id) {
177 if(id != "configuration") return;
178 llOwnerSay("[Spank] Got configuration...");
179 configuration = message;
180 state listen_group;
181 }
182 timer() {
183 llOwnerSay("[Spank] Requesting configuration...");
184 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
185 }
186 on_rez(integer num) {
187 llResetScript();
188 }
189 changed(integer change) {
190 if((change & CHANGED_INVENTORY) ||
191 (change & CHANGED_REGION_START) ||
192 (change & CHANGED_OWNER)) {
193 llResetScript();
194 }
195 }
196 state_exit() {
197 llSetTimerEvent(0);
198 }
199 }
200  
201 state listen_group {
202 state_entry() {
203 // DEBUG
204 llOwnerSay("[Spank] Waiting for group messages...");
205 }
206 link_message(integer sender, integer num, string message, key id) {
207 // We only care about notifications now.
208 if(id != "notification")
209 return;
210  
211 // This script only processes group notifications.
212 if(wasKeyValueGet("type", message) != "group")
213 return;
214  
215 // Get the sent message.
216 data = wasURLUnescape(
217 wasKeyValueGet(
218 "message",
219 message
220 )
221 );
222  
223 if(llGetSubString(data, 0, 0) !=
224 wasKeyValueGet("command", configuration))
225 return;
226  
227 list command = llParseString2List(data, ["@", " "], []);
228 if(llList2String(command, 0) != "spank")
229 return;
230  
231 // Remove command.
232 command = llDeleteSubList(command, 0, 0);
233  
234 // Dump the rest of the message.
235 data = llDumpList2String(command, " ");
236  
237 // Get an URL.
238 state url;
239 }
240 on_rez(integer num) {
241 llResetScript();
242 }
243 changed(integer change) {
244 if((change & CHANGED_INVENTORY) ||
245 (change & CHANGED_REGION_START) ||
246 (change & CHANGED_OWNER)) {
247 llResetScript();
248 }
249 }
250 }
251  
252 state url {
253 state_entry() {
254 // DEBUG
255 llOwnerSay("[Spank] Requesting URL...");
256 llRequestURL();
257 }
258 http_request(key id, string method, string body) {
259 if(method != URL_REQUEST_GRANTED) return;
260 URL = body;
261 // DEBUG
262 llOwnerSay("[Spank] Got URL...");
263 state search;
264 }
265 on_rez(integer num) {
266 llResetScript();
267 }
268 changed(integer change) {
269 if((change & CHANGED_INVENTORY) ||
270 (change & CHANGED_REGION_START) ||
271 (change & CHANGED_OWNER)) {
272 llResetScript();
273 }
274 }
275 }
276  
277 state search {
278 state_entry() {
279 // DEBUG
280 llOwnerSay("[Spank] Searching for agent.");
281 llInstantMessage(
282 wasKeyValueGet(
283 "corrade",
284 configuration
285 ),
286 wasKeyValueEncode(
287 [
288 "command", "getmembers",
289 "group", wasURLEscape(
290 wasKeyValueGet(
291 "group",
292 configuration
293 )
294 ),
295 "password", wasURLEscape(
296 wasKeyValueGet(
297 "password",
298 configuration
299 )
300 ),
301 "sift", wasURLEscape(
302 wasListToCSV(
303 [
304 "match",
305 wasURLEscape("(?i),?([^,$]*" + data +"[^,$]*),?")
306 ]
307 )
308 ),
309 "callback", wasURLEscape(URL)
310 ]
311 )
312 );
313 llSetTimerEvent(60);
314 }
315 http_request(key id, string method, string body) {
316 llHTTPResponse(id, 200, "OK");
317 llReleaseURL(URL);
318 if(wasKeyValueGet("command", body) != "getmembers" ||
319 wasKeyValueGet("success", body) != "True") {
320 // DEBUG
321 llOwnerSay("[Spank] Unable to get members: " +
322 wasURLUnescape(
323 wasKeyValueGet("error", body)
324 )
325 );
326 state listen_group;
327 }
328  
329 // Dump the members to a list.
330 data = llList2CSV(
331 wasCSVToList(
332 wasURLUnescape(
333 wasKeyValueGet("data", body)
334 )
335 )
336 );
337  
338 if(data == "") {
339 data = "I could not find anyone to spank (story of my life).";
340 state tell;
341 }
342  
343 llOwnerSay("Spanking: " + data);
344  
345 data = "/me spanks " + data;
346  
347 state tell;
348 }
349 timer() {
350 llReleaseURL(URL);
351 state listen_group;
352 }
353 on_rez(integer num) {
354 llResetScript();
355 }
356 changed(integer change) {
357 if((change & CHANGED_INVENTORY) ||
358 (change & CHANGED_REGION_START) ||
359 (change & CHANGED_OWNER)) {
360 llResetScript();
361 }
362 }
363 state_exit() {
364 llSetTimerEvent(0);
365 }
366 }
367  
368 state tell {
369 state_entry() {
370 // DEBUG
371 llOwnerSay("[Spank] Sending to group.");
372 llInstantMessage(
373 wasKeyValueGet(
374 "corrade",
375 configuration
376 ),
377 wasKeyValueEncode(
378 [
379 "command", "tell",
380 "group", wasURLEscape(
381 wasKeyValueGet(
382 "group",
383 configuration
384 )
385 ),
386 "password", wasURLEscape(
387 wasKeyValueGet(
388 "password",
389 configuration
390 )
391 ),
392 "entity", "group",
393 "message", wasURLEscape(data)
394 ]
395 )
396 );
397 state listen_group;
398 }
399 }