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

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 16 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 joke module for Corrade Eggdrop. 5 // A joke module for Corrade Eggdrop.
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   170  
171 // Notecard reading. 171 // Notecard reading.
172 key nQuery = NULL_KEY; 172 key nQuery = NULL_KEY;
173 string joke = ""; 173 string joke = "";
174 list q = [ ]; 174 list q = [ ];
175   175  
176 /////////////////////////////////////////////////////////////////////////// 176 ///////////////////////////////////////////////////////////////////////////
177 // Copyright (C) 2011 Wizardry and Steamworks - License: GNU GPLv3 // 177 // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 //
178 /////////////////////////////////////////////////////////////////////////// 178 ///////////////////////////////////////////////////////////////////////////
179 // Requires: a limit. 179 // Requires: a limit.
180 // Provides: true random number between [0, max) or (-max, 0]. 180 // Provides: true random number between [0, max) or (-max, 0].
181 ////////////////////////////////////////////////////////// 181 //////////////////////////////////////////////////////////
182 integer wasFPSrand(integer max) { 182 integer wasFPSrand(integer max) {
183 integer r = (integer)(llGetRegionFPS() * 10000000.0) % max; 183 integer r = (integer)(llGetRegionFPS() * 10000000.0) % max;
184 if(max > 0) return r; else return -r; 184 if(max > 0) return r; else return -r;
185 } 185 }
186   186  
187 default { 187 default {
188 state_entry() { 188 state_entry() {
189 llOwnerSay("[Joke] Starting module..."); 189 llOwnerSay("[Joke] Starting module...");
190 llSetTimerEvent(10); 190 llSetTimerEvent(10);
191 } 191 }
192 link_message(integer sender, integer num, string message, key id) { 192 link_message(integer sender, integer num, string message, key id) {
193 if(id != "configuration") return; 193 if(id != "configuration") return;
194 llOwnerSay("[Joke] Got configuration..."); 194 llOwnerSay("[Joke] Got configuration...");
195 configuration = message; 195 configuration = message;
196 state count_jokes; 196 state count_jokes;
197 } 197 }
198 timer() { 198 timer() {
199 llOwnerSay("[Joke] Requesting configuration..."); 199 llOwnerSay("[Joke] Requesting configuration...");
200 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY); 200 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
201 } 201 }
202 on_rez(integer num) { 202 on_rez(integer num) {
203 llResetScript(); 203 llResetScript();
204 } 204 }
205 changed(integer change) { 205 changed(integer change) {
206 if((change & CHANGED_INVENTORY) || 206 if((change & CHANGED_INVENTORY) ||
207 (change & CHANGED_REGION_START) || 207 (change & CHANGED_REGION_START) ||
208 (change & CHANGED_OWNER)) { 208 (change & CHANGED_OWNER)) {
209 llResetScript(); 209 llResetScript();
210 } 210 }
211 } 211 }
212 state_exit() { 212 state_exit() {
213 llSetTimerEvent(0); 213 llSetTimerEvent(0);
214 } 214 }
215 } 215 }
216   216  
217 state count_jokes { 217 state count_jokes {
218 state_entry() { 218 state_entry() {
219 // Build a list of dataserver queries and retrieve the number of lines. 219 // Build a list of dataserver queries and retrieve the number of lines.
220 joke = (string)llGetInventoryNumber(INVENTORY_NOTECARD); 220 joke = (string)llGetInventoryNumber(INVENTORY_NOTECARD);
221 integer i = (integer)joke - 1; 221 integer i = (integer)joke - 1;
222 do { 222 do {
223 string card = llGetInventoryName(INVENTORY_NOTECARD, i); 223 string card = llGetInventoryName(INVENTORY_NOTECARD, i);
224 // Filter notecards containing the "jokes" configuration key value. 224 // Filter notecards containing the "jokes" configuration key value.
225 if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1) { 225 if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1) {
226 joke = (string)((integer)joke - 1); 226 joke = (string)((integer)joke - 1);
227 jump continue; 227 jump continue;
228 } 228 }
229 q = llListInsertList(q, [ llGetNumberOfNotecardLines(card) ], 0); 229 q = llListInsertList(q, [ llGetNumberOfNotecardLines(card) ], 0);
230 @continue; 230 @continue;
231 } while(--i >= -1); 231 } while(--i >= -1);
232 q += NULL_KEY; 232 q += NULL_KEY;
233 } 233 }
234 dataserver(key id, string data) { 234 dataserver(key id, string data) {
235 // Find the query in the query list. 235 // Find the query in the query list.
236 integer i = llListFindList(q, [ id ]); 236 integer i = llListFindList(q, [ id ]);
237 if(i == -1) return; 237 if(i == -1) return;
238 238
239 // Replace the query by the number of notecard lines (lists are covariant). 239 // Replace the query by the number of notecard lines (lists are covariant).
240 q = llListReplaceList(q, [ (integer) data ], i, i); 240 q = llListReplaceList(q, [ (integer) data ], i, i);
241 joke = (string)((integer)joke - 1); 241 joke = (string)((integer)joke - 1);
242 if((integer)joke == 0) 242 if((integer)joke == 0)
243 state listen_group; 243 state listen_group;
244 } 244 }
245 on_rez(integer num) { 245 on_rez(integer num) {
246 llResetScript(); 246 llResetScript();
247 } 247 }
248 changed(integer change) { 248 changed(integer change) {
249 if((change & CHANGED_INVENTORY) || 249 if((change & CHANGED_INVENTORY) ||
250 (change & CHANGED_REGION_START) || 250 (change & CHANGED_REGION_START) ||
251 (change & CHANGED_OWNER)) { 251 (change & CHANGED_OWNER)) {
252 llResetScript(); 252 llResetScript();
253 } 253 }
254 } 254 }
255 } 255 }
256   256  
257 state listen_group { 257 state listen_group {
258 state_entry() { 258 state_entry() {
259 // DEBUG 259 // DEBUG
260 llOwnerSay("[Joke] Waiting for group messages..."); 260 llOwnerSay("[Joke] Waiting for group messages...");
261 } 261 }
262 link_message(integer sender, integer num, string message, key id) { 262 link_message(integer sender, integer num, string message, key id) {
263 // We only care about notifications now. 263 // We only care about notifications now.
264 if(id != "notification") 264 if(id != "notification")
265 return; 265 return;
266 266
267 // This script only processes group notifications. 267 // This script only processes group notifications.
268 if(wasKeyValueGet("type", message) != "group") 268 if(wasKeyValueGet("type", message) != "group")
269 return; 269 return;
270 270
271 // Get the sent message. 271 // Get the sent message.
272 data = wasURLUnescape( 272 data = wasURLUnescape(
273 wasKeyValueGet( 273 wasKeyValueGet(
274 "message", 274 "message",
275 message 275 message
276 ) 276 )
277 ); 277 );
278 278
279 // Check if this is an eggdrop command. 279 // Check if this is an eggdrop command.
280 if(llGetSubString(data, 0, 0) != 280 if(llGetSubString(data, 0, 0) !=
281 wasKeyValueGet("command", configuration)) 281 wasKeyValueGet("command", configuration))
282 return; 282 return;
283 283
284 // Check if the command matches the current module. 284 // Check if the command matches the current module.
285 list command = llParseString2List(data, [" "], []); 285 list command = llParseString2List(data, [" "], []);
286 if(llList2String(command, 0) != 286 if(llList2String(command, 0) !=
287 wasKeyValueGet("command", configuration) + "joke") 287 wasKeyValueGet("command", configuration) + "joke")
288 return; 288 return;
289 289
290 // Remove command. 290 // Remove command.
291 command = llDeleteSubList(command, 0, 0); 291 command = llDeleteSubList(command, 0, 0);
292 292
293 state read_joke; 293 state read_joke;
294 } 294 }
295 on_rez(integer num) { 295 on_rez(integer num) {
296 llResetScript(); 296 llResetScript();
297 } 297 }
298 changed(integer change) { 298 changed(integer change) {
299 if((change & CHANGED_INVENTORY) || 299 if((change & CHANGED_INVENTORY) ||
300 (change & CHANGED_REGION_START) || 300 (change & CHANGED_REGION_START) ||
301 (change & CHANGED_OWNER)) { 301 (change & CHANGED_OWNER)) {
302 llResetScript(); 302 llResetScript();
303 } 303 }
304 } 304 }
305 } 305 }
306   306  
307 state read_joke { 307 state read_joke {
308 state_entry() { 308 state_entry() {
309 // DEBUG 309 // DEBUG
310 llOwnerSay("[Joke] Reading a joke..."); 310 llOwnerSay("[Joke] Reading a joke...");
311 311
312 integer i = llGetInventoryNumber(INVENTORY_NOTECARD) - 1; 312 integer i = llGetInventoryNumber(INVENTORY_NOTECARD) - 1;
313 list c = []; 313 list c = [];
314 do { 314 do {
315 string card = llGetInventoryName(INVENTORY_NOTECARD, i); 315 string card = llGetInventoryName(INVENTORY_NOTECARD, i);
316 if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1) 316 if(llSubStringIndex(card, wasKeyValueGet("jokes", configuration)) == -1)
317 jump continue; 317 jump continue;
318 c = llListInsertList(c, [ i ], 0); 318 c = llListInsertList(c, [ i ], 0);
319 @continue; 319 @continue;
320 } while(--i >= -1); 320 } while(--i >= -1);
321 321
322 i = (integer)llFrand(llGetListLength(c)); 322 i = (integer)llFrand(llGetListLength(c));
323 nQuery = llGetNotecardLine( 323 nQuery = llGetNotecardLine(
324 llGetInventoryName( 324 llGetInventoryName(
325 INVENTORY_NOTECARD, 325 INVENTORY_NOTECARD,
326 llList2Integer(c, i) 326 llList2Integer(c, i)
327 ), 327 ),
328 (integer) 328 (integer)
329 ( 329 (
330 llFrand( 330 llFrand(
331 llList2Integer( 331 llList2Integer(
332 q, 332 q,
333 i 333 i
334 ) 334 )
335 ) 335 )
336 ) 336 )
337 ); 337 );
338 } 338 }
339 dataserver(key id, string data) { 339 dataserver(key id, string data) {
340 if(id != nQuery) return; 340 if(id != nQuery) return;
341 joke = data; 341 joke = data;
342 state tell; 342 state tell;
343 } 343 }
344 on_rez(integer num) { 344 on_rez(integer num) {
345 llResetScript(); 345 llResetScript();
346 } 346 }
347 changed(integer change) { 347 changed(integer change) {
348 if((change & CHANGED_INVENTORY) || 348 if((change & CHANGED_INVENTORY) ||
349 (change & CHANGED_REGION_START) || 349 (change & CHANGED_REGION_START) ||
350 (change & CHANGED_OWNER)) { 350 (change & CHANGED_OWNER)) {
351 llResetScript(); 351 llResetScript();
352 } 352 }
353 } 353 }
354 } 354 }
355   355  
356 state tell { 356 state tell {
357 state_entry() { 357 state_entry() {
358 // DEBUG 358 // DEBUG
359 llOwnerSay("[Joke] Sending to group."); 359 llOwnerSay("[Joke] Sending to group.");
360 llInstantMessage( 360 llInstantMessage(
361 wasKeyValueGet( 361 wasKeyValueGet(
362 "corrade", 362 "corrade",
363 configuration 363 configuration
364 ), 364 ),
365 wasKeyValueEncode( 365 wasKeyValueEncode(
366 [ 366 [
367 "command", "tell", 367 "command", "tell",
368 "group", wasURLEscape( 368 "group", wasURLEscape(
369 wasKeyValueGet( 369 wasKeyValueGet(
370 "group", 370 "group",
371 configuration 371 configuration
372 ) 372 )
373 ), 373 ),
374 "password", wasURLEscape( 374 "password", wasURLEscape(
375 wasKeyValueGet( 375 wasKeyValueGet(
376 "password", 376 "password",
377 configuration 377 configuration
378 ) 378 )
379 ), 379 ),
380 "entity", "group", 380 "entity", "group",
381 "message", wasURLEscape(joke) 381 "message", wasURLEscape(joke)
382 ] 382 ]
383 ) 383 )
384 ); 384 );
385 385
386 // GC 386 // GC
387 joke = ""; 387 joke = "";
388 388
389 state listen_group; 389 state listen_group;
390 } 390 }
391 } 391 }
392   392