normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / noteworthy_library_v12.5.txt
1 
2 // huffware script: noteworthy library, by fred huffhines.
3 //
4 // a handy approach to reading a notecard.  this version supports requiring
5 // a 'signature' in the notecard's first line, so that multiple notecards can
6 // exist in an object without being misinterpreted.  the script is accessed via
7 // its link message API, so it can be used in an object without all this code
8 // needing to be embedded in another script.  it also supports queuing up requests
9 // to read notecards, so multiple scripts can use it to read their specific
10 // notecards without any special handling (besides waiting a bit longer).
11 //
12 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
13 // do not use it in objects without fully realizing you are implicitly accepting that license.
14 //
15
16 // constants that can be adapted to your needs...
17
18 integer DEBUGGING = FALSE;
19     // if this is true, then a lot of extra noise is generated when notecards are read.
20
21 float TIME_OUT_ON_ONE_NOTECARD = 10.0;
22     // we allow one line of a notecard this long to be read before we decide it's hosed.
23     // some sims are very very slow, and a time-out of one minute has been found lacking;
24     // we saw at least one case where the first notecard line to be read took longer than
25     // 60 seconds.  that's why we kept cranking this time-out up...
26     // but now is the time for non-craziness, and that's absolutely crazy to wait that long.
27     // exploring the 10 second allottance time frame...
28
29 // constants that should not be changed...
30
31 // outcomes from handling a line in a notecard.
32 integer STILL_READING = -8;  // the notecard seems good, but isn't finished.
33 integer BAD_NOTECARD = -9;  // this notecard doesn't have our signature.
34 integer DONE_READING = -10;  // the notecard is finished being read.
35
36 integer LIST_ELEMENT_GUESS = 200;  // guess at how many bytes average list element uses.
37
38 integer MAXIMUM_LINES_USED = 4;
39     // we will read this many lines at a time from the appropriate notecard.
40
41 // this is the noteworthy library's API that is available via link messages.
42 //////////////
43 // do not redefine these constants.
44 integer NOTEWORTHY_HUFFWARE_ID = 10010;
45     // the unique id within the huffware system for the noteworthy script to
46     // accept commands on.  this is used in llMessageLinked as the num parameter.
47 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
48     // this pattern is an uncommon thing to see in text, so we use it to separate
49     // our commands in link messages.
50 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
51     // used to separate lists of items from each other when stored inside a parameter.
52     // this allows lists to be passed as single string parameters if needed.
53 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
54 string BAD_NOTECARD_INDICATOR = "bad_notecard";
55     // indicates that the notecard reading process has failed to find an appropriate one.
56     
57 //STRIKE THIS from everywhere.
58 //string BUSY_READING_INDICATOR = "busy_already";
59 //    // this return value indicates that the script is already in use by some other script.
60 //    // the calling script should try again later.
61
62 string NOTECARD_READ_CONTINUATION = "continue!";
63     // returned as first parameter if there is still more data to handle.
64 // commands available via the noteworthy library:
65 string READ_NOTECARD_COMMAND = "#read_note#";
66     // command used to tell the script to read notecards.  needs a signature to find
67     // in the card as the first parameter, and a randomly generated response code for
68     // the second parameter.  the response code is used to uniquely identify a set of
69     // pending notecard readings (hopefully).  the signature can be blank.
70     // the results will be fired back as the string value returned, which will have
71     // as first element the notecard's name (or BAD_NOTECARD_INDICATOR if none was
72     // found) and as subsequent elements an embedded list that was read from the
73     // notecard.  this necessarily limits the size of the notecards that we can read
74     // and return.
75 string READ_SPECIFIC_NOTECARD_COMMAND = "#read_thisun#";
76     // like the read notecard command, but specifies the notecard name to use.  only that
77     // specific notecard file will be consulted.  first and second parm are still signature
78     // and response code, third parm is the notecard name.
79 //
80 //////////////
81 // joins a list of parameters using the parameter sentinel for the library.
82 string wrap_parameters(list to_flatten)
83 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
84 //////////////
85
86 // global variables...
87
88 string requested_signature = "";
89     // if this is non-empty, then it must be found in the first line of the script.
90
91 integer only_read_one_notecard = FALSE;  // true if just one specific notecard should be used.
92
93 string global_notecard_name;  // the name of the card we're reading now.
94 key global_query_id = NULL_KEY;  // the identifier of our data query.
95 integer current_response_code = 0;  // the code that our client uses for its reading.
96 list global_query_contents;  // the lines we have read from the notecard.
97
98 integer line_number = 0;  // which line are we at in notecard?
99
100 integer found_signature_line = FALSE;  // did we find our first line in a notecard yet?
101
102 integer trying_notecard_at = -1;  // where we are currently looking for a good notecard.
103
104 list pending_signatures;  // signatures from queued requests for reading.
105 list pending_response_codes;  // response codes for the queued requests.
106 list pending_notecard_names;  // card names if it's a specific request.
107
108 //////////////
109
110 startup_initialize()
111 {
112     pending_signatures = [];
113     pending_response_codes = [];
114     pending_notecard_names = [];
115     current_response_code = 0;
116     llSetTimerEvent(0.0);
117 }
118
119 reset_for_reading(string signature, integer response_code)
120 {
121     requested_signature = signature;
122     current_response_code = response_code;
123     llSetTimerEvent(TIME_OUT_ON_ONE_NOTECARD);  // don't allow a read to happen forever.
124     global_query_contents = [];
125     global_notecard_name = "";
126     line_number = 0;
127     found_signature_line = FALSE;
128     trying_notecard_at = -1;
129     global_query_id = NULL_KEY;
130 }
131
132 // use the existing global notecard setting to start reading.
133 select_specific_notecard()
134 {
135     global_query_id = NULL_KEY;  // reset the query id so we don't get bogus answers.
136     line_number = 0;  // reset line number again.
137     global_query_id = llGetNotecardLine(global_notecard_name, 0);
138 }
139
140 // picks the notecard at the "index" (from 0 to num_notecards - 1) and
141 // starts reading it.
142 select_notecard(integer index)
143 {
144     global_query_id = NULL_KEY;  // reset the query id so we don't get bogus answers.
145     string card_specified = llGetInventoryName(INVENTORY_NOTECARD, index);
146     if (card_specified == "") return;   // not good.  bad index.
147     global_notecard_name = card_specified;
148     line_number = 0;  // reset line number again.
149     // we have a new file name, so load up the destinations, hopefully.
150     global_query_id = llGetNotecardLine(global_notecard_name, 0);
151 }
152
153 // increments our index in the count of notecards that the object has, and start
154 // reading the next notecard (at the index).
155 integer try_next_notecard()
156 {
157     if (only_read_one_notecard) {
158         return FALSE;  // we were only going to try one.
159     }
160     // reset some values that might have applied before.
161     global_notecard_name = "";
162     // skip to the next card.
163     trying_notecard_at++;
164     // make sure we're not over the count of cards.
165     if (trying_notecard_at >= llGetInventoryNumber(INVENTORY_NOTECARD)) {
166         // this is a problem.  we didn't find anything suitable.
167         return FALSE;
168     }
169     // so, now we'll try the next notecard to look for our signature.
170     select_notecard(trying_notecard_at);
171     // reset the timer in case we were close on the last card.
172     llSetTimerEvent(TIME_OUT_ON_ONE_NOTECARD);
173     return TRUE;
174 }
175
176 // process a line of text that we received from the current notecard.
177 integer handle_notecard_line(key query_id, string data)
178 {
179     // if we're not at the end of the notecard we're reading...
180     if (data != EOF) {
181         // there's more to read in the notecard still.
182         if (data != "") {
183             // make sure we even have a signature to look for.
184             if (!found_signature_line && (requested_signature == "")) {
185                 // no signature means that we always find it.
186                 found_signature_line = TRUE;
187             }
188             // did we already get our signature?  if not, see if this is it.
189             if (!found_signature_line && (data != requested_signature) ) {
190                 // this is a bad notecard.  skip it.
191                 if (!try_next_notecard()) {
192                     // we have no more to work with.
193                     return BAD_NOTECARD;
194                 }
195                 return STILL_READING;  // we'll keep going.
196             } else if (!found_signature_line && (data == requested_signature) ) {
197                 // this is a good signature line, so record that and then skip it.
198                 found_signature_line = TRUE;
199             } else {
200                 if (DEBUGGING
201                     && ( ( (requested_signature == "") && (line_number == 0) )
202                         || ( (requested_signature != "") && (line_number == 1) ) ) ) {
203                     log_it("started reading " + global_notecard_name + "...");
204                 }
205                 // don't record any lines that are comments.
206                 if ( (llGetSubString(data, 0, 0) != "#")
207                     && (llGetSubString(data, 0, 0) != ";") ) {
208                     // add the non-blank line to our list.
209                     global_query_contents += data;
210                     // make sure we still have enough space to keep going.
211                     if (llGetListLength(global_query_contents) >= MAXIMUM_LINES_USED) {
212                         // ooh, make sure we pause before blowing our heap&stack.
213                         send_reply(LINK_THIS, [ NOTECARD_READ_CONTINUATION,
214                             current_response_code ], READ_NOTECARD_COMMAND, TRUE);                
215                     }
216                 }
217             }
218         }
219         line_number++;  // increase the line count.
220         // reset the timer rather than timing out, if we actually got some data.
221         llSetTimerEvent(TIME_OUT_ON_ONE_NOTECARD);        
222         // request the next line from the notecard.
223         global_query_id = llGetNotecardLine(global_notecard_name, line_number);
224         if (global_query_id == NULL_KEY) {
225 //log_it("failed to restart notecard reading.");
226             return DONE_READING;
227 //is that the best outcome?
228         }
229         return STILL_READING;
230     } else {
231         // that's the end of the notecard.  we need some minimal verification that it
232         // wasn't full of garbage.
233         if (!found_signature_line) {
234             if (DEBUGGING) log_it("never found signature in " + global_notecard_name);
235             if (!try_next_notecard()) {
236                 return BAD_NOTECARD;  // we failed to find a good line?
237             } else {
238                 // the next notecard's coming through now.
239                 return STILL_READING;
240             }
241         } else {
242 //            if (DEBUGGING) log_it("found signature.");
243             // saw the signature line, so this is a good one.
244             return DONE_READING;
245         }
246     }
247 }
248
249 // only sends reply; does not reset notecard process.
250 send_reply(integer destination, list parms, string command,
251     integer include_query)
252 {
253 //integer items = llGetListLength(parms);
254 //if (include_query) items += llGetListLength(global_query_contents);
255 //log_it("pre-sending " + (string)items + " items, mem=" + (string)llGetFreeMemory());
256
257    if (!include_query) {
258         llMessageLinked(destination, NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE,
259             command, llDumpList2String(parms, HUFFWARE_PARM_SEPARATOR));
260     } else {
261         llMessageLinked(destination, NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE,
262             command,
263             llDumpList2String(parms + global_query_contents, HUFFWARE_PARM_SEPARATOR));
264     }
265     global_query_contents = [];
266 //log_it("post-sending, mem=" + (string)llGetFreeMemory());
267 }
268
269 // a simple version of a reply for a command that has been executed.  the parameters
270 // might contain an outcome or result of the operation that was requested.
271 send_reply_and_reset(integer destination, list parms, string command,
272     integer include_query)
273 {
274     llSetTimerEvent(0.0);  // stop the timer, since we're about to reply.
275     send_reply(destination, parms, command, include_query);
276     current_response_code = 0;  // reset back to default so we can start another read.
277     global_query_id = NULL_KEY;  // reset so we accept no more data.
278 }
279
280 // if there are other pending notecard reads, this goes to the next one listed.
281 dequeue_next_request()
282 {
283     if (llGetListLength(pending_signatures)) {
284         // get the info from the next pending item.
285         string sig = llList2String(pending_signatures, 0);
286         integer response_code = llList2Integer(pending_response_codes, 0);
287         string notecard = llList2String(pending_notecard_names, 0);
288         // whack the head of the queue since we grabbed the info.
289         pending_signatures = llDeleteSubList(pending_signatures, 0, 0);
290         pending_response_codes = llDeleteSubList(pending_response_codes, 0, 0);
291         pending_notecard_names = llDeleteSubList(pending_notecard_names, 0, 0);
292         if (llStringLength(notecard)) {
293             global_notecard_name = notecard;
294             select_specific_notecard();
295         } else {
296             reset_for_reading(sig, response_code);
297         }
298     }
299 }
300
301 // deals with one data server answer from the notecard.
302 process_notecard_line(key query_id, string data)
303 {
304     // try to consume a line from the notecard.
305     integer outcome = handle_notecard_line(query_id, data);
306     if (outcome == DONE_READING) {
307         // that was a valid notecard and we read all of it.
308         if (DEBUGGING) log_it("finished reading " + global_notecard_name + ".");
309         // send back the results.
310         send_reply_and_reset(LINK_THIS, [ global_notecard_name, current_response_code ],
311             READ_NOTECARD_COMMAND, TRUE);
312     } else if (outcome == BAD_NOTECARD) {
313         // bail; this problem must be addressed by other means.
314         if (DEBUGGING) log_it("failed to find an appropriate notecard");
315         send_reply_and_reset(LINK_THIS, [ BAD_NOTECARD_INDICATOR, current_response_code ],
316             READ_NOTECARD_COMMAND, FALSE);
317     } else if (outcome == STILL_READING) {
318         // we have a good card and are still processing it.
319         return;
320     } else {
321         if (DEBUGGING) log_it("unknown outcome from handle_notecard_line");
322         // again, bail out.  we have no idea what happened with this.
323         send_reply_and_reset(LINK_THIS, [ BAD_NOTECARD_INDICATOR, current_response_code ],
324             READ_NOTECARD_COMMAND, FALSE);
325     }
326     // if we have reached here, we should crank up the next queued notecard reading.
327     dequeue_next_request();
328 }
329
330 // processes requests from our users.
331 handle_link_message(integer which, integer num, string msg, key id)
332 {
333     if (num != NOTEWORTHY_HUFFWARE_ID) return;  // not for us.
334
335     if (msg == READ_NOTECARD_COMMAND) {
336         only_read_one_notecard = FALSE;  // general inquiry for any card.
337         list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
338 //log_it("read notecard--parms are: " + (string)parms);
339         string signature = llList2String(parms, 0);
340         integer response_code = llList2Integer(parms, 1);
341 //log_it("got signature " + signature + " and respcode " + (string)response_code);
342 //holding:        if (!current_response_code) {
343             // go ahead and process this request; we aren't busy.
344             reset_for_reading(signature, response_code);
345             if (!try_next_notecard()) {
346                 if (DEBUGGING) log_it("failed to find any appropriate notecards at all.");
347                 send_reply_and_reset(LINK_THIS, [ BAD_NOTECARD_INDICATOR, response_code ],
348                     READ_NOTECARD_COMMAND, FALSE);
349                 return;
350             }
351 //holding:        } else {
352 //holding:            // we're already busy.
353 //holding://            send_reply_and_reset(LINK_THIS, [ BUSY_READING_INDICATOR, response_code ],
354 //holding://                READ_NOTECARD_COMMAND, FALSE);
355 //holding:            // stack this request; another is in progress.
356 //holding:            pending_signatures += signature;
357 //holding:            pending_response_codes += response_code;
358 //holding:            pending_notecard_names += "";
359 //holding:        }
360     } else if (msg == READ_SPECIFIC_NOTECARD_COMMAND) {
361         only_read_one_notecard = TRUE;  // they want one particular card.
362         list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
363 //log_it("read specific--parms are: " + (string)parms);
364         string signature = llList2String(parms, 0);
365         integer response_code = llList2Integer(parms, 1);
366         string notecard_name = llList2String(parms, 2);
367 //log_it("got signature " + signature + " and respcode " + (string)response_code);
368 //holding:        if (!current_response_code) {
369             // go ahead and process this request; we aren't busy.
370             reset_for_reading(signature, response_code);
371             global_notecard_name = notecard_name;  // set our global.
372             select_specific_notecard();
373 //holding:        } else {
374 //holding:            // we're already busy.
375 //holding://            send_reply_and_reset(LINK_THIS, [ BUSY_READING_INDICATOR, response_code ],
376 //holding://                READ_NOTECARD_COMMAND, FALSE);
377 //holding:            // stack this request; another is in progress.
378 //holding:            pending_signatures += signature;
379 //holding:            pending_response_codes += response_code;
380 //holding:            pending_notecard_names += notecard_name;
381 //holding:        }
382     }
383 }
384
385
386 ///////////////
387
388 // from hufflets...
389
390 integer debug_num = 0;
391
392 // a debugging output method.  can be disabled entirely in one place.
393 log_it(string to_say)
394 {
395     debug_num++;
396     // tell this to the owner.    
397     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
398     // say this on open chat, but use an unusual channel.
399 //    llSay(108, (string)debug_num + "- " + to_say);
400 }
401
402 //////////////
403
404 //////////////
405 // huffware script: auto-retire, by fred huffhines, version 2.4.
406 // distributed under BSD-like license.
407 //   partly based on the self-upgrading scripts from markov brodsky and jippen faddoul.
408 // the function auto_retire() should be added *inside* a version numbered script that
409 // you wish to give the capability of self-upgrading.
410 //   this script supports a notation for versions embedded in script names where a 'v'
411 // is followed by a number in the form "major.minor", e.g. "grunkle script by ted v8.2".
412 // when the containing script is dropped into an object with a different version, the
413 // most recent version eats any existing ones.
414 //   keep in mind that this code must be *copied* into your script you wish to add
415 // auto-retirement capability to.
416 // example usage of the auto-retirement script:
417 //     default {
418 //         state_entry() {
419 //            auto_retire();  // make sure newest addition is only version of script.
420 //        }
421 //     }
422 auto_retire() {
423     string self = llGetScriptName();  // the name of this script.
424     list split = compute_basename_and_version(self);
425     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
426     string basename = llList2String(split, 0);  // script name with no version attached.
427     string version_string = llList2String(split, 1);  // the version found.
428     integer posn;
429     // find any scripts that match the basename.  they are variants of this script.
430     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
431 //log_it("invpo=" + (string)posn);
432         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
433         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
434             // found a basic match at least.
435             list inv_split = compute_basename_and_version(curr_script);
436             if (llGetListLength(inv_split) == 2) {
437                 // see if this script is more ancient.
438                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
439                 // must make sure that the retiring script is completely the identical basename;
440                 // just matching in the front doesn't make it a relative.
441                 if ( (llList2String(inv_split, 0) == basename)
442                     && ((float)inv_version_string < (float)version_string) ) {
443                     // remove script with same name from inventory that has inferior version.
444                     llRemoveInventory(curr_script);
445                 }
446             }
447         }
448     }
449 }
450 //
451 // separates the base script name and version number.  used by auto_retire.
452 list compute_basename_and_version(string to_chop_up)
453 {
454     // minimum script name is 2 characters plus a version.
455     integer space_v_posn;
456     // find the last useful space and 'v' combo.
457     for (space_v_posn = llStringLength(to_chop_up) - 3;
458         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
459         space_v_posn--) {
460         // look for space and v but do nothing else.
461 //log_it("pos=" + (string)space_v_posn);
462     }
463     if (space_v_posn < 2) return [];  // no space found.
464 //log_it("space v@" + (string)space_v_posn);
465     // now we zoom through the stuff after our beloved v character and find any evil
466     // space characters, which are most likely from SL having found a duplicate item
467     // name and not so helpfully renamed it for us.
468     integer indy;
469     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
470 //log_it("indy=" + (string)space_v_posn);
471         if (llGetSubString(to_chop_up, indy, indy) == " ") {
472             // found one; zap it.  since we're going backwards we don't need to
473             // adjust the loop at all.
474             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
475 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
476         }
477     }
478     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
479     // ditch the space character for our numerical check.
480     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
481     // strip out a 'v' if there is one.
482     if (llGetSubString(chop_suffix, 0, 0) == "v")
483         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
484     // if valid floating point number and greater than zero, that works for our version.
485     string basename = to_chop_up;  // script name with no version attached.
486     if ((float)chop_suffix > 0.0) {
487         // this is a big success right here.
488         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
489         return [ basename, chop_suffix ];
490     }
491     // seems like we found nothing useful.
492     return [];
493 }
494 //
495 //////////////
496
497 // end hufflets.
498 //////////////
499
500 default {
501     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
502     on_rez(integer parm) { state rerun; }
503 }
504 state rerun { state_entry() { state default; } }
505
506 state real_default
507 {
508     state_entry() {
509         auto_retire();
510         startup_initialize();
511     }
512     
513     state_exit() {
514         llSetTimerEvent(0);
515     }
516     
517     // we don't do anything until we're given a command to read a notecard.
518     link_message(integer which, integer num, string msg, key id) 
519     {
520         if (num != NOTEWORTHY_HUFFWARE_ID) return;  // not for us.
521         handle_link_message(which, num, msg, id);
522     }
523     
524     on_rez(integer parm) { state rerun; }
525     
526     timer() {
527         llSetTimerEvent(0.0);  // stop any timer now.
528         // let the caller know this has failed out.
529 //        if (DEBUGGING) log_it("time out processing '" + requested_signature + "'");
530         send_reply_and_reset(LINK_THIS, [ BAD_NOTECARD_INDICATOR, current_response_code ],
531             READ_NOTECARD_COMMAND, FALSE);
532         current_response_code = 0;  // we gave up on that one.
533         dequeue_next_request();  // get next reading started if we have anything to read.
534     }
535
536     dataserver(key query_id, string data) {
537         // make sure this data is for us.
538         if (global_query_id != query_id) return;
539         // yep, seems to be.
540         process_notecard_line(query_id, data);
541     }
542 }
543
544
545