corrade-lsl-templates – Diff between revs 28 and 29

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 28 Rev 29
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0 //
3 /////////////////////////////////////////////////////////////////////////// 3 ///////////////////////////////////////////////////////////////////////////
4 // 4 //
5 // A module that pulls information from an encyclopedia. 5 // A module that pulls information from an encyclopedia.
6 // 6 //
7 /////////////////////////////////////////////////////////////////////////// 7 ///////////////////////////////////////////////////////////////////////////
8   8  
9 /////////////////////////////////////////////////////////////////////////// 9 ///////////////////////////////////////////////////////////////////////////
10 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 10 // Copyright (C) 2014 Wizardry and Steamworks - License: CC BY 2.0 //
11 /////////////////////////////////////////////////////////////////////////// 11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) { 12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return ""; 13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return ""; 14 if(llStringLength(k) == 0) return "";
15 list a = llParseString2List(data, ["&", "="], []); 15 list a = llParseString2List(data, ["&", "="], []);
16 integer i = llListFindList(a, [ k ]); 16 integer i = llListFindList(a, [ k ]);
17 if(i != -1) return llList2String(a, i+1); 17 if(i != -1) return llList2String(a, i+1);
18 return ""; 18 return "";
19 } 19 }
20 20
21 /////////////////////////////////////////////////////////////////////////// 21 ///////////////////////////////////////////////////////////////////////////
22 // Copyright (C) 2013 Wizardry and Steamworks - License: GNU GPLv3 // 22 // Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0 //
23 /////////////////////////////////////////////////////////////////////////// 23 ///////////////////////////////////////////////////////////////////////////
24 string wasKeyValueEncode(list data) { 24 string wasKeyValueEncode(list data) {
25 list k = llList2ListStrided(data, 0, -1, 2); 25 list k = llList2ListStrided(data, 0, -1, 2);
26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2); 26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
27 data = []; 27 data = [];
28 do { 28 do {
29 data += llList2String(k, 0) + "=" + llList2String(v, 0); 29 data += llList2String(k, 0) + "=" + llList2String(v, 0);
30 k = llDeleteSubList(k, 0, 0); 30 k = llDeleteSubList(k, 0, 0);
31 v = llDeleteSubList(v, 0, 0); 31 v = llDeleteSubList(v, 0, 0);
32 } while(llGetListLength(k) != 0); 32 } while(llGetListLength(k) != 0);
33 return llDumpList2String(data, "&"); 33 return llDumpList2String(data, "&");
34 } 34 }
35   35  
36 /////////////////////////////////////////////////////////////////////////// 36 ///////////////////////////////////////////////////////////////////////////
37 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 // 37 // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 //
38 /////////////////////////////////////////////////////////////////////////// 38 ///////////////////////////////////////////////////////////////////////////
39 // http://was.fm/secondlife/wanderer 39 // http://was.fm/secondlife/wanderer
40 vector wasCirclePoint(float radius) { 40 vector wasCirclePoint(float radius) {
41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2); 41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
42 float y = 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)) 43 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
44 return <x, y, 0>; 44 return <x, y, 0>;
45 return wasCirclePoint(radius); 45 return wasCirclePoint(radius);
46 } 46 }
47   47  
48 /////////////////////////////////////////////////////////////////////////// 48 ///////////////////////////////////////////////////////////////////////////
49 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 49 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
50 /////////////////////////////////////////////////////////////////////////// 50 ///////////////////////////////////////////////////////////////////////////
51 // escapes a string in conformance with RFC1738 51 // escapes a string in conformance with RFC1738
52 string wasURLEscape(string i) { 52 string wasURLEscape(string i) {
53 string o = ""; 53 string o = "";
54 do { 54 do {
55 string c = llGetSubString(i, 0, 0); 55 string c = llGetSubString(i, 0, 0);
56 i = llDeleteSubString(i, 0, 0); 56 i = llDeleteSubString(i, 0, 0);
57 if(c == "") jump continue; 57 if(c == "") jump continue;
58 if(c == " ") { 58 if(c == " ") {
59 o += "+"; 59 o += "+";
60 jump continue; 60 jump continue;
61 } 61 }
62 if(c == "\n") { 62 if(c == "\n") {
63 o += "%0D" + llEscapeURL(c); 63 o += "%0D" + llEscapeURL(c);
64 jump continue; 64 jump continue;
65 } 65 }
66 o += llEscapeURL(c); 66 o += llEscapeURL(c);
67 @continue; 67 @continue;
68 } while(i != ""); 68 } while(i != "");
69 return o; 69 return o;
70 } 70 }
71   71  
72 /////////////////////////////////////////////////////////////////////////// 72 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 73 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
74 /////////////////////////////////////////////////////////////////////////// 74 ///////////////////////////////////////////////////////////////////////////
75 list wasCSVToList(string csv) { 75 list wasCSVToList(string csv) {
76 list l = []; 76 list l = [];
77 list s = []; 77 list s = [];
78 string m = ""; 78 string m = "";
79 do { 79 do {
80 string a = llGetSubString(csv, 0, 0); 80 string a = llGetSubString(csv, 0, 0);
81 csv = llDeleteSubString(csv, 0, 0); 81 csv = llDeleteSubString(csv, 0, 0);
82 if(a == ",") { 82 if(a == ",") {
83 if(llList2String(s, -1) != "\"") { 83 if(llList2String(s, -1) != "\"") {
84 l += m; 84 l += m;
85 m = ""; 85 m = "";
86 jump continue; 86 jump continue;
87 } 87 }
88 m += a; 88 m += a;
89 jump continue; 89 jump continue;
90 } 90 }
91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) { 91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
92 m += a; 92 m += a;
93 csv = llDeleteSubString(csv, 0, 0); 93 csv = llDeleteSubString(csv, 0, 0);
94 jump continue; 94 jump continue;
95 } 95 }
96 if(a == "\"") { 96 if(a == "\"") {
97 if(llList2String(s, -1) != a) { 97 if(llList2String(s, -1) != a) {
98 s += a; 98 s += a;
99 jump continue; 99 jump continue;
100 } 100 }
101 s = llDeleteSubList(s, -1, -1); 101 s = llDeleteSubList(s, -1, -1);
102 jump continue; 102 jump continue;
103 } 103 }
104 m += a; 104 m += a;
105 @continue; 105 @continue;
106 } while(csv != ""); 106 } while(csv != "");
107 // postcondition: length(s) = 0 107 // postcondition: length(s) = 0
108 return l + m; 108 return l + m;
109 } 109 }
110   110  
111 /////////////////////////////////////////////////////////////////////////// 111 ///////////////////////////////////////////////////////////////////////////
112 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 112 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
113 /////////////////////////////////////////////////////////////////////////// 113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) { 114 string wasListToCSV(list l) {
115 list v = []; 115 list v = [];
116 do { 116 do {
117 string a = llDumpList2String( 117 string a = llDumpList2String(
118 llParseStringKeepNulls( 118 llParseStringKeepNulls(
119 llList2String( 119 llList2String(
120 l, 120 l,
121 0 121 0
122 ), 122 ),
123 ["\""], 123 ["\""],
124 [] 124 []
125 ), 125 ),
126 "\"\"" 126 "\"\""
127 ); 127 );
128 if(llParseStringKeepNulls( 128 if(llParseStringKeepNulls(
129 a, 129 a,
130 [" ", ",", "\n", "\""], [] 130 [" ", ",", "\n", "\""], []
131 ) != 131 ) !=
132 (list) a 132 (list) a
133 ) a = "\"" + a + "\""; 133 ) a = "\"" + a + "\"";
134 v += a; 134 v += a;
135 l = llDeleteSubList(l, 0, 0); 135 l = llDeleteSubList(l, 0, 0);
136 } while(l != []); 136 } while(l != []);
137 return llDumpList2String(v, ","); 137 return llDumpList2String(v, ",");
138 } 138 }
139   139  
140 /////////////////////////////////////////////////////////////////////////// 140 ///////////////////////////////////////////////////////////////////////////
141 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 141 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
142 /////////////////////////////////////////////////////////////////////////// 142 ///////////////////////////////////////////////////////////////////////////
143 // unescapes a string in conformance with RFC1738 143 // unescapes a string in conformance with RFC1738
144 string wasURLUnescape(string i) { 144 string wasURLUnescape(string i) {
145 return llUnescapeURL( 145 return llUnescapeURL(
146 llDumpList2String( 146 llDumpList2String(
147 llParseString2List( 147 llParseString2List(
148 llDumpList2String( 148 llDumpList2String(
149 llParseString2List( 149 llParseString2List(
150 i, 150 i,
151 ["+"], 151 ["+"],
152 [] 152 []
153 ), 153 ),
154 " " 154 " "
155 ), 155 ),
156 ["%0D%0A"], 156 ["%0D%0A"],
157 [] 157 []
158 ), 158 ),
159 "\n" 159 "\n"
160 ) 160 )
161 ); 161 );
162 } 162 }
163   163  
164 // configuration data 164 // configuration data
165 string configuration = ""; 165 string configuration = "";
166 // callback URL 166 // callback URL
167 string URL = ""; 167 string URL = "";
168 // store message over state. 168 // store message over state.
169 string data = ""; 169 string data = "";
170 string query = ""; 170 string query = "";
171   171  
172 default { 172 default {
173 state_entry() { 173 state_entry() {
174 llOwnerSay("[Explain] Starting module..."); 174 llOwnerSay("[Explain] Starting module...");
175 llSetTimerEvent(10); 175 llSetTimerEvent(10);
176 } 176 }
177 link_message(integer sender, integer num, string message, key id) { 177 link_message(integer sender, integer num, string message, key id) {
178 if(id != "configuration") return; 178 if(id != "configuration") return;
179 llOwnerSay("[Explain] Got configuration..."); 179 llOwnerSay("[Explain] Got configuration...");
180 configuration = message; 180 configuration = message;
181 state listen_group; 181 state listen_group;
182 } 182 }
183 timer() { 183 timer() {
184 llOwnerSay("[Explain] Requesting configuration..."); 184 llOwnerSay("[Explain] Requesting configuration...");
185 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); 185 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
186 } 186 }
187 on_rez(integer num) { 187 on_rez(integer num) {
188 llResetScript(); 188 llResetScript();
189 } 189 }
190 changed(integer change) { 190 changed(integer change) {
191 if((change & CHANGED_INVENTORY) || 191 if((change & CHANGED_INVENTORY) ||
192 (change & CHANGED_REGION_START) || 192 (change & CHANGED_REGION_START) ||
193 (change & CHANGED_OWNER)) { 193 (change & CHANGED_OWNER)) {
194 llResetScript(); 194 llResetScript();
195 } 195 }
196 } 196 }
197 state_exit() { 197 state_exit() {
198 llSetTimerEvent(0); 198 llSetTimerEvent(0);
199 } 199 }
200 } 200 }
201   201  
202 state listen_group { 202 state listen_group {
203 state_entry() { 203 state_entry() {
204 // DEBUG 204 // DEBUG
205 llOwnerSay("[Explain] Waiting for group messages..."); 205 llOwnerSay("[Explain] Waiting for group messages...");
206 } 206 }
207 link_message(integer sender, integer num, string message, key id) { 207 link_message(integer sender, integer num, string message, key id) {
208 // We only care about notifications now. 208 // We only care about notifications now.
209 if(id != "notification") 209 if(id != "notification")
210 return; 210 return;
211 211
212 // This script only processes group notifications. 212 // This script only processes group notifications.
213 if(wasKeyValueGet("type", message) != "group") 213 if(wasKeyValueGet("type", message) != "group")
214 return; 214 return;
215 215
216 // Get the sent message. 216 // Get the sent message.
217 data = wasURLUnescape( 217 data = wasURLUnescape(
218 wasKeyValueGet( 218 wasKeyValueGet(
219 "message", 219 "message",
220 message 220 message
221 ) 221 )
222 ); 222 );
223 223
224 // Check if this is an eggdrop command. 224 // Check if this is an eggdrop command.
225 if(llGetSubString(data, 0, 0) != 225 if(llGetSubString(data, 0, 0) !=
226 wasKeyValueGet("command", configuration)) 226 wasKeyValueGet("command", configuration))
227 return; 227 return;
228 228
229 // Check if the command matches the current module. 229 // Check if the command matches the current module.
230 list command = llParseString2List(data, [" "], []); 230 list command = llParseString2List(data, [" "], []);
231 if(llList2String(command, 0) != 231 if(llList2String(command, 0) !=
232 wasKeyValueGet("command", configuration) + "explain") 232 wasKeyValueGet("command", configuration) + "explain")
233 return; 233 return;
234 234
235 // Remove command. 235 // Remove command.
236 command = llDeleteSubList(command, 0, 0); 236 command = llDeleteSubList(command, 0, 0);
237 237
238 query = llDumpList2String(command, " "); 238 query = llDumpList2String(command, " ");
239 239
240 state url; 240 state url;
241 } 241 }
242 on_rez(integer num) { 242 on_rez(integer num) {
243 llResetScript(); 243 llResetScript();
244 } 244 }
245 changed(integer change) { 245 changed(integer change) {
246 if((change & CHANGED_INVENTORY) || 246 if((change & CHANGED_INVENTORY) ||
247 (change & CHANGED_REGION_START) || 247 (change & CHANGED_REGION_START) ||
248 (change & CHANGED_OWNER)) { 248 (change & CHANGED_OWNER)) {
249 llResetScript(); 249 llResetScript();
250 } 250 }
251 } 251 }
252 } 252 }
253   253  
254 state url { 254 state url {
255 state_entry() { 255 state_entry() {
256 // DEBUG 256 // DEBUG
257 llOwnerSay("[Explain] Requesting URL..."); 257 llOwnerSay("[Explain] Requesting URL...");
258 llRequestURL(); 258 llRequestURL();
259 } 259 }
260 http_request(key id, string method, string body) { 260 http_request(key id, string method, string body) {
261 if(method != URL_REQUEST_GRANTED) return; 261 if(method != URL_REQUEST_GRANTED) return;
262 URL = body; 262 URL = body;
263 // DEBUG 263 // DEBUG
264 llOwnerSay("[Explain] Got URL..."); 264 llOwnerSay("[Explain] Got URL...");
265 state query_wikipedia; 265 state query_wikipedia;
266 } 266 }
267 on_rez(integer num) { 267 on_rez(integer num) {
268 llResetScript(); 268 llResetScript();
269 } 269 }
270 changed(integer change) { 270 changed(integer change) {
271 if((change & CHANGED_INVENTORY) || 271 if((change & CHANGED_INVENTORY) ||
272 (change & CHANGED_REGION_START) || 272 (change & CHANGED_REGION_START) ||
273 (change & CHANGED_OWNER)) { 273 (change & CHANGED_OWNER)) {
274 llResetScript(); 274 llResetScript();
275 } 275 }
276 } 276 }
277 } 277 }
278   278  
279 state query_wikipedia { 279 state query_wikipedia {
280 state_entry() { 280 state_entry() {
281 // DEBUG 281 // DEBUG
282 llOwnerSay("[Explain] Querying wikipedia..."); 282 llOwnerSay("[Explain] Querying wikipedia...");
283 llInstantMessage( 283 llInstantMessage(
284 wasKeyValueGet( 284 wasKeyValueGet(
285 "corrade", 285 "corrade",
286 configuration 286 configuration
287 ), 287 ),
288 wasKeyValueEncode( 288 wasKeyValueEncode(
289 [ 289 [
290 "command", "HTTP", 290 "command", "HTTP",
291 "group", wasURLEscape( 291 "group", wasURLEscape(
292 wasKeyValueGet( 292 wasKeyValueGet(
293 "group", 293 "group",
294 configuration 294 configuration
295 ) 295 )
296 ), 296 ),
297 "password", wasURLEscape( 297 "password", wasURLEscape(
298 wasKeyValueGet( 298 wasKeyValueGet(
299 "password", 299 "password",
300 configuration 300 configuration
301 ) 301 )
302 ), 302 ),
303 "action", "get", 303 "action", "get",
304 "type", "text", 304 "type", "text",
305 "URL", wasURLEscape("http://lookup.dbpedia.org/api/search/PrefixSearch?QueryClass=&MaxHits=1&QueryString=" + query), 305 "URL", wasURLEscape("http://lookup.dbpedia.org/api/search/PrefixSearch?QueryClass=&MaxHits=1&QueryString=" + query),
306 "sift", wasListToCSV( 306 "sift", wasListToCSV(
307 [ 307 [
308 "XPath", 308 "XPath",
309 wasURLEscape( 309 wasURLEscape(
310 wasKeyValueEncode( 310 wasKeyValueEncode(
311 [ 311 [
312 "default", "x", 312 "default", "x",
313 "path", wasURLEscape("/x:ArrayOfResult/x:Result/x:Description") 313 "path", wasURLEscape("/x:ArrayOfResult/x:Result/x:Description")
314 ] 314 ]
315 ) 315 )
316 ) 316 )
317 ] 317 ]
318 ), 318 ),
319 "callback", wasURLEscape(URL) 319 "callback", wasURLEscape(URL)
320 ] 320 ]
321 ) 321 )
322 ); 322 );
323 323
324 llSetTimerEvent(60); 324 llSetTimerEvent(60);
325 } 325 }
326 http_request(key id, string method, string body) { 326 http_request(key id, string method, string body) {
327 llHTTPResponse(id, 200, "OK"); 327 llHTTPResponse(id, 200, "OK");
328 if(wasKeyValueGet("command", body) != "HTTP" || 328 if(wasKeyValueGet("command", body) != "HTTP" ||
329 wasKeyValueGet("success", body) != "True") { 329 wasKeyValueGet("success", body) != "True") {
330 // DEBUG 330 // DEBUG
331 llOwnerSay("[Explain] Unable to query wikipedia: " + 331 llOwnerSay("[Explain] Unable to query wikipedia: " +
332 wasURLUnescape( 332 wasURLUnescape(
333 wasKeyValueGet("error", body) 333 wasKeyValueGet("error", body)
334 ) 334 )
335 ); 335 );
336 llReleaseURL(URL); 336 llReleaseURL(URL);
337 state listen_group; 337 state listen_group;
338 } 338 }
339 339
340 data = wasKeyValueGet("data", wasURLUnescape(body)); 340 data = wasKeyValueGet("data", wasURLUnescape(body));
341 341
342 state tell; 342 state tell;
343 } 343 }
344 timer() { 344 timer() {
345 llReleaseURL(URL); 345 llReleaseURL(URL);
346 state listen_group; 346 state listen_group;
347 } 347 }
348 on_rez(integer num) { 348 on_rez(integer num) {
349 llResetScript(); 349 llResetScript();
350 } 350 }
351 changed(integer change) { 351 changed(integer change) {
352 if((change & CHANGED_INVENTORY) || 352 if((change & CHANGED_INVENTORY) ||
353 (change & CHANGED_REGION_START) || 353 (change & CHANGED_REGION_START) ||
354 (change & CHANGED_OWNER)) { 354 (change & CHANGED_OWNER)) {
355 llResetScript(); 355 llResetScript();
356 } 356 }
357 } 357 }
358 state_exit() { 358 state_exit() {
359 llSetTimerEvent(0); 359 llSetTimerEvent(0);
360 } 360 }
361 } 361 }
362   362  
363 state tell { 363 state tell {
364 state_entry() { 364 state_entry() {
365 // DEBUG 365 // DEBUG
366 llOwnerSay("[Explain] Sending to group..."); 366 llOwnerSay("[Explain] Sending to group...");
367 llInstantMessage( 367 llInstantMessage(
368 wasKeyValueGet( 368 wasKeyValueGet(
369 "corrade", 369 "corrade",
370 configuration 370 configuration
371 ), 371 ),
372 wasKeyValueEncode( 372 wasKeyValueEncode(
373 [ 373 [
374 "command", "tell", 374 "command", "tell",
375 "group", wasURLEscape( 375 "group", wasURLEscape(
376 wasKeyValueGet( 376 wasKeyValueGet(
377 "group", 377 "group",
378 configuration 378 configuration
379 ) 379 )
380 ), 380 ),
381 "password", wasURLEscape( 381 "password", wasURLEscape(
382 wasKeyValueGet( 382 wasKeyValueGet(
383 "password", 383 "password",
384 configuration 384 configuration
385 ) 385 )
386 ), 386 ),
387 "entity", "group", 387 "entity", "group",
388 "message", wasURLEscape(data) 388 "message", wasURLEscape(data)
389 ] 389 ]
390 ) 390 )
391 ); 391 );
392 state listen_group; 392 state listen_group;
393 } 393 }
394 } 394 }
395   395