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