normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / menu_list_manager_v12.7.txt
1 
2 // huffware script: menu list manager, by fred huffhines.
3 //
4 // deals with popping up menus when desired.  does not do any configuration
5 // reading; that all must be fed to the script via link messages.
6 //
7 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 // do not use it in objects without fully realizing you are implicitly accepting that license.
9 //
10
11 // constants...
12
13 string GIFTING_MENU_NAME = "#gifting#";
14     // the menu name that we support internally for giving gifts.
15
16 // global variables...
17
18 // these are the definition of the menuing system,
19 // and are valid once we have heard our config.
20
21 // unique identifiers of menu names.
22 list menu_names;
23 // a list of titles for the menus.
24 list menu_titles;
25 // the menu choices are strings containing encoded lists of button text labels.
26 list menu_button_lists;
27
28 // store current activated menu info...
29 string _private_global_av_name;
30 string _private_global_av_key;
31
32 // menu list manager link message API.
33 //////////////
34 // do not redefine these constants.
35 integer MENU_LIST_MANAGER_HUFFWARE_ID = 10033;
36     // the unique id within the huffware system for this sensor plugin script.
37 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
38     // this pattern is an uncommon thing to see in text, so we use it to separate
39     // our commands in link messages.
40 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
41     // used to separate lists of items from each other when stored inside a parameter.
42     // this allows lists to be passed as single string parameters if needed.
43 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
44 string MENU_LIST_MGR_RESET = "#ml-reset#";
45     // throws out any current menus and gets ready to load a new set.
46 string MENU_LIST_MGR_ADD_MENU = "#ml-addmenu#";
47     // puts a new menu into our list.  this requires 3 parameters: menu name,
48     // menu title, menu button list.
49 string MENU_LIST_MGR_SHOW_MENU = "#ml-shomenu#";
50     // a request that a particular menu be shown.  the first parameter is the menu index.
51     // the second parameter is the avatar name.  the third parameter is the avatar key.
52 string MENU_LIST_MGR_GIVE_ITEM = "#ml-give#";
53     // brings up a gift giving menu.  parms are avatar name and avatar key.
54 string MENU_LIST_MGR_MODIFY_MENU = "#ml-modmenu#";
55 string MENU_LIST_KEEP_WORD = "KEEP";
56     // replaces a menu already in the list.  this requires 4 parameters: menu index, menu name,
57     // menu title, menu button list.  if a parameter is the MENU_LIST_KEEP_WORD, then that field
58     // is not changed.
59 string MENU_LIST_MGR_CHOICE_MADE_ALERT = "#ml-event-picked";
60     // alerts the driver for this script that the owner has picked a choice.  the
61     // parameters include: the text of the choice, the name of the menu, the avatar name,
62     // the avatar key.
63 //
64 //////////////
65 // joins a list of parameters using the parameter sentinel for the library.
66 string wrap_parameters(list to_flatten)
67 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
68 //////////////
69
70 // requires HUD sensor plugin v21.4 or better.
71 //////////////
72 // do not redefine these constants.
73 integer SENSOR_PLUGIN_HUFFWARE_ID = 10014;
74     // the unique id within the huffware system for this sensor plugin script.
75 string GIVE_ALL_AVATARS = "#santa#";
76     // if this is received, the sensor plugin attempts to hand out an object passed as
77     // the first parameter to every avatar it knows about.
78 //////////////
79
80 // requires menutini v3.9 or better...
81 //////////////
82 // do not redefine these constants.
83 integer MENUTINI_HUFFWARE_ID = 10009;
84     // the unique id within the huffware system for the jaunt script to
85     // accept commands on.  this is used in llMessageLinked as the num parameter.
86 // commands available via menutini:
87 string SHOW_MENU_COMMAND = "#menu#";
88     // the command that tells menutini to show a menu defined by parameters
89     // that are passed along.  these must be: the menu name, the menu's title
90     // (which is really the info to show as content in the main box of the menu),
91     // the wrapped list of commands to show as menu buttons, the menu system
92     // channel's for listening, and the key to listen to.
93 //
94 //////////////
95 // joins a list of sub-items using the item sentinel for the library.
96 string wrap_item_list(list to_wrap)
97 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
98 //
99 //////////////
100
101 // replaces occurences of $owner and $avatar with the owner's name and the
102 // targeted avatars name.  _first and _last added to the name cause the first
103 // or last name to be used instead.
104 string senso_substitute_vars(string to_substitute, string avatars_name)
105 {
106     string owners_name = llKey2Name(llGetOwner());
107     return substitute_variable_list(to_substitute,
108         [ "avatar_first", "avatar_last", "avatar",
109             "owner_first", "owner_last", "owner" ],
110         [ first_name(avatars_name), last_name(avatars_name), avatars_name,
111            first_name(owners_name), last_name(owners_name), owners_name ]);
112 }
113
114 integer random_channel() { return -(integer)(llFrand(40000) + 20000); }
115
116 simply_display_menu(string menu_name, string title, list buttons,
117     string av_name, string av_key)
118 {
119     // we only manage one global now for this stuff...
120     _private_global_av_name = av_name;
121     _private_global_av_key = av_key;
122
123     integer menu_channel = random_channel();
124 ///    key listen_to = llGetOwner();
125 //log_it("passing in key to use: " + (string)listen_to);
126     llMessageLinked(LINK_THIS, MENUTINI_HUFFWARE_ID, SHOW_MENU_COMMAND,
127         menu_name + HUFFWARE_PARM_SEPARATOR
128         + title + HUFFWARE_PARM_SEPARATOR
129         + wrap_item_list(buttons) + HUFFWARE_PARM_SEPARATOR
130         + (string)menu_channel + HUFFWARE_PARM_SEPARATOR
131         + (string)av_key);
132 }
133
134 replace_vars_and_display_menu(integer menu_index, string av_name, string av_key)
135 {
136     // patch the avatar names and owner names before displaying.
137     string title = senso_substitute_vars(llList2String(menu_titles, menu_index), av_name);
138     string button_text = llList2String(menu_button_lists, menu_index);
139
140     simply_display_menu(llList2String(menu_names, menu_index), title,
141         llParseStringKeepNulls(button_text, [HUFFWARE_ITEM_SEPARATOR], []),
142         av_name, av_key);
143 }
144
145 // shows a menu by index.
146 show_menu(integer index, string av_name, string av_key)
147 {
148     replace_vars_and_display_menu(index, av_name, av_key);
149 //log_it("sent the menu request...  memory left=" + (string)llGetFreeMemory());
150 }
151
152 // handles the response message when the user chooses a button.
153 react_to_menu(string menu_name, string av_name, string av_key, string which_choice)
154 {
155 //log_it("you clicked " + which_choice + " item for av " + av_name);
156     handle_chosen_menu_item(menu_name, av_name, av_key, which_choice);
157 }
158
159 // the gift giving menu.  we show the menu after looking through the
160 // inventory.  if the owner picks an item, we'll deal with that response.
161 show_gift_menu(string av_name, string av_key)
162 {
163     string menu_name = "gift";
164     string menu_title = "Pick a gift for " + av_name + "...";
165     list menu_button_list = [];
166     integer indy;
167     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_OBJECT); indy++) {
168         menu_button_list += llGetInventoryName(INVENTORY_OBJECT, indy);
169     }
170     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_TEXTURE); indy++) {
171         string item_name = llGetInventoryName(INVENTORY_TEXTURE, indy);
172         if (!is_prefix(item_name, "ean hud keys"))
173             menu_button_list += item_name;
174     }
175     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_LANDMARK); indy++) {
176         menu_button_list += llGetInventoryName(INVENTORY_LANDMARK, indy);
177     }
178     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_NOTECARD); indy++) {
179         string item_name = llGetInventoryName(INVENTORY_NOTECARD, indy);
180         if ( (find_substring(item_name, "help") < 0)
181             && (find_substring(item_name, "Help") < 0)
182             && (find_substring(item_name, "config") < 0) )
183             menu_button_list += item_name;
184     }
185     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_CLOTHING); indy++) {
186         menu_button_list += llGetInventoryName(INVENTORY_CLOTHING, indy);
187     }
188     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_GESTURE); indy++) {
189         menu_button_list += llGetInventoryName(INVENTORY_GESTURE, indy);
190     }
191     
192     if (llGetListLength(menu_button_list) == 0) {
193         llOwnerSay("There are no objects loaded in the hud currently.  I can make no gifts.");
194     }
195     simply_display_menu(GIFTING_MENU_NAME, menu_title, menu_button_list,
196         av_name, av_key);
197 }
198
199 // handles a request for this library itself.
200 process_hud_menu_mgr_request(integer sender, integer num, string msg, key id)
201 {
202     list parms = llParseStringKeepNulls(id, [HUFFWARE_PARM_SEPARATOR], []);
203     if (msg == MENU_LIST_MGR_RESET) {
204         llResetScript();
205     } else if (msg == MENU_LIST_MGR_ADD_MENU) {
206         if (llGetListLength(parms) < 3) {
207             log_it("too few parms to add menu.");
208             return;
209         }
210 //hmmm: sloppy list processing!
211         menu_names += [ llList2String(parms, 0) ];
212         menu_titles += [ llList2String(parms, 1) ];
213         menu_button_lists += [ llList2String(parms, 2) ];
214     } else if (msg == MENU_LIST_MGR_GIVE_ITEM) {
215         // give over a gift.
216         string av_name = llList2String(parms, 0);
217         string av_key = llList2String(parms, 1);
218         show_gift_menu(av_name, av_key);
219     } else if (msg == MENU_LIST_MGR_SHOW_MENU) {
220         if (llGetListLength(parms) < 3) {
221             log_it("too few parms to show menu.");
222             return;
223         }
224 //log_it("key comes as " + llList2String(parms, 2));
225         show_menu((integer)llList2String(parms, 0), llList2String(parms, 1),
226             (key)llList2String(parms, 2));
227 //log_it("show menu...  memory left=" + (string)llGetFreeMemory());
228     } else if (msg == MENU_LIST_MGR_MODIFY_MENU) {
229         if (llGetListLength(parms) < 4) {
230             log_it("too few parms to replace menu.");
231             return;
232         }
233         integer indo = (integer)llList2String(parms, 0);
234 //log_it("modif menu request, indo is " + (string)indo);
235         if (indo >= llGetListLength(menu_names)) {
236 //log_it("index too large in replace menu");
237             return;
238         }
239 //log_it("replacing entry " + (string)indo + " with " + llList2String(parms, 1) + ", " + llList2String(parms, 2) + ", " + llList2String(parms, 3));
240         if (llList2String(parms, 1) != MENU_LIST_KEEP_WORD)
241             menu_names = replace_entry(menu_names, indo, [ llList2String(parms, 1) ]);
242         if (llList2String(parms, 2) != MENU_LIST_KEEP_WORD)
243             menu_titles = replace_entry(menu_titles, indo, [ llList2String(parms, 2) ]);
244         if (llList2String(parms, 3) != MENU_LIST_KEEP_WORD)
245             menu_button_lists = replace_entry(menu_button_lists, indo, [ llList2String(parms, 3) ]);
246 //log_it("new menu button:" + llList2String(parms, 3));
247     }
248 }
249
250 // processes a message from a link.  some of this must be handled
251 // by the driver script that handles our configuration.
252 handle_link_message(integer sender, integer num, string msg, key id)
253 {
254     if (num == MENU_LIST_MANAGER_HUFFWARE_ID) {
255         process_hud_menu_mgr_request(sender, num, msg, id);
256     }
257     
258     if ( (num != MENUTINI_HUFFWARE_ID + REPLY_DISTANCE)
259         || (msg != SHOW_MENU_COMMAND) ) return;  // not for us.
260
261 //log_it("menu reply: sndr=" + (string)sender + " num=" + (string)num + " msg=" + msg + " id=" + (string)id);
262     list parms = llParseStringKeepNulls(id, [HUFFWARE_PARM_SEPARATOR], []);
263     string menu_name = llList2String(parms, 0);
264     string which_choice = llList2String(parms, 1);
265 ///not used:    key av_key = llList2String(parms, 2);
266 //hmmm: should the menutini also offer av name and key options???
267     react_to_menu(menu_name, _private_global_av_name,
268         _private_global_av_key, which_choice);
269 }
270
271 //////////////
272
273 // gives a present to the avatar named here.
274 give_gift(string av_name, string av_key, string present)
275 {
276 //log_it("trying to give a gift of " + present + " to " + av_name);
277     if (av_name != "ALL") {
278         llGiveInventory(av_key, present);
279         llOwnerSay("gave " + present + " to " + av_name);
280     } else {
281         // send the request back to the sensor plugin for processing.
282         llMessageLinked(LINK_ROOT, SENSOR_PLUGIN_HUFFWARE_ID,
283             GIVE_ALL_AVATARS, wrap_parameters([present]));
284     }
285 }
286         
287 handle_chosen_menu_item(string menu_name, string av_name,
288     string av_key, string which_choice)
289 {
290     if (menu_name == GIFTING_MENU_NAME) {
291         give_gift(av_name, av_key, which_choice);
292     }
293
294     // send the alert to the driver.
295     llMessageLinked(LINK_ROOT, MENU_LIST_MANAGER_HUFFWARE_ID + REPLY_DISTANCE,
296         MENU_LIST_MGR_CHOICE_MADE_ALERT,
297         wrap_parameters([which_choice, menu_name, av_name, av_key]));
298 }
299
300 //////////////
301 // from hufflets...
302 //
303 integer debug_num = 0;
304
305 // a debugging output method.  can be disabled entirely in one place.
306 log_it(string to_say)
307 {
308     debug_num++;
309     // tell this to the owner.    
310     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
311     // say this on open chat, but use an unusual channel.
312 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
313 }
314
315 integer find_substring(string full_string, string pattern)
316 {
317     string full_lower = llToLower(full_string);
318     return llSubStringIndex(full_lower, pattern);
319 }
320
321 //////////////
322
323 // returns the position of the last space in "look_within" or a negative number.
324 integer find_last_space(string look_within)
325 {
326     integer indy = llStringLength(look_within) - 1;
327     while ( (indy >= 0) && (llGetSubString(look_within, indy, indy) != " ") ) indy--;
328     return indy;
329 }
330
331 // returns the first name for an avatar with the "full_name".
332 string first_name(string full_name)
333 {
334     integer finding = find_last_space(full_name);
335     if (finding >= 0) return llGetSubString(full_name, 0, finding - 1);
336     return full_name;  // failed to find space.
337 }
338
339 // returns the last name for an avatar with the "full_name".
340 string last_name(string full_name)
341 {
342     integer finding = find_last_space(full_name);
343     if (finding >= 0) return llGetSubString(full_name, finding + 1, -1);
344     return full_name;  // failed to find space.
345 }
346
347 // substitutes a variable's name for its value.  note that variables are assumed to start
348 // with a dollar sign character, which should not be provided in the "variable_name" parameter.
349 string substitute_variable(string substitute_within, string variable_name, string variable_value)
350 {
351     string to_return = substitute_within;
352     integer posn;
353     while ( (posn = find_substring(to_return, "$" + variable_name)) >= 0) {
354         // we found an occurrence of the variable.
355         to_return = llDeleteSubString(to_return, posn, -1)  // keep part before.
356             + variable_value  // add the value in place of the variable name.
357             + llDeleteSubString(to_return, 0, posn + llStringLength(variable_name));
358                 // keep part after.
359     }
360     return to_return;
361 }
362
363 // in "substitute_within", this finds any occurrences of items in the "variables_names"
364 // and replaces those with the corresponding item from "variable_values".
365 string substitute_variable_list(string substitute_within, list variable_names, list variable_values)
366 {
367     string to_return = substitute_within;
368     integer vars = llGetListLength(variable_names);
369     if (vars != llGetListLength(variable_values)) {
370 //        log_it("error in substitute_variable_list: inequal number of variable names vs. values.");
371         return to_return;
372     }
373     integer indy;
374     for (indy = 0; indy < vars; indy++) {
375         to_return = substitute_variable(to_return,
376             llList2String(variable_names, indy),
377             llList2String(variable_values, indy));
378     }
379     return to_return;
380 }
381
382 // locates the string "text" in the list to "search_in".
383 integer find_in_list(list search_in, string text)
384
385     integer len = llGetListLength(search_in);
386     integer i; 
387     for (i = 0; i < len; i++) { 
388         if (llList2String(search_in, i) == text) 
389             return i; 
390     } 
391     return -1;
392 }
393
394 // removes the entry at "index" and instead inserts the list "to_insert"
395 // at that position.
396 list replace_entry(list to_modify, integer index, list to_insert)
397 {
398     if (llGetListLength(to_modify) == 0) return to_insert;  // special case for empty.
399     return llListReplaceList(to_modify, to_insert, index, index);
400 }
401
402 // returns TRUE if the "prefix" string is the first part of "compare_with".
403 integer is_prefix(string compare_with, string prefix)
404 { return (llSubStringIndex(compare_with, prefix) == 0); }
405 //
406 // end hufflets
407 //////////////
408
409 //////////////
410 // huffware script: auto-retire, by fred huffhines, version 2.5.
411 // distributed under BSD-like license.
412 //   !!  keep in mind that this code must be *copied* into another
413 //   !!  script that you wish to add auto-retirement capability to.
414 // when a script has auto_retire in it, it can be dropped into an
415 // object and the most recent version of the script will destroy
416 // all older versions.
417 //
418 // the version numbers are embedded into the script names themselves.
419 // the notation for versions uses a letter 'v', followed by two numbers
420 // in the form "major.minor".
421 // major and minor versions are implicitly considered as a floating point
422 // number that increases with each newer version of the script.  thus,
423 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
424 // and "hazmap v3.2" is a more recent version.
425 //
426 // example usage of the auto-retirement script:
427 //     default {
428 //         state_entry() {
429 //            auto_retire();  // make sure newest addition is only version of script.
430 //        }
431 //     }
432 // this script is partly based on the self-upgrading scripts from markov brodsky
433 // and jippen faddoul.
434 //////////////
435 auto_retire() {
436     string self = llGetScriptName();  // the name of this script.
437     list split = compute_basename_and_version(self);
438     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
439     string basename = llList2String(split, 0);  // script name with no version attached.
440     string version_string = llList2String(split, 1);  // the version found.
441     integer posn;
442     // find any scripts that match the basename.  they are variants of this script.
443     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
444 //log_it("invpo=" + (string)posn);
445         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
446         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
447             // found a basic match at least.
448             list inv_split = compute_basename_and_version(curr_script);
449             if (llGetListLength(inv_split) == 2) {
450                 // see if this script is more ancient.
451                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
452                 // must make sure that the retiring script is completely the identical basename;
453                 // just matching in the front doesn't make it a relative.
454                 if ( (llList2String(inv_split, 0) == basename)
455                     && ((float)inv_version_string < (float)version_string) ) {
456                     // remove script with same name from inventory that has inferior version.
457                     llRemoveInventory(curr_script);
458                 }
459             }
460         }
461     }
462 }
463 //
464 // separates the base script name and version number.  used by auto_retire.
465 list compute_basename_and_version(string to_chop_up)
466 {
467     // minimum script name is 2 characters plus a version.
468     integer space_v_posn;
469     // find the last useful space and 'v' combo.
470     for (space_v_posn = llStringLength(to_chop_up) - 3;
471         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
472         space_v_posn--) {
473         // look for space and v but do nothing else.
474 //log_it("pos=" + (string)space_v_posn);
475     }
476     if (space_v_posn < 2) return [];  // no space found.
477 //log_it("space v@" + (string)space_v_posn);
478     // now we zoom through the stuff after our beloved v character and find any evil
479     // space characters, which are most likely from SL having found a duplicate item
480     // name and not so helpfully renamed it for us.
481     integer indy;
482     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
483 //log_it("indy=" + (string)space_v_posn);
484         if (llGetSubString(to_chop_up, indy, indy) == " ") {
485             // found one; zap it.  since we're going backwards we don't need to
486             // adjust the loop at all.
487             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
488 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
489         }
490     }
491     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
492     // ditch the space character for our numerical check.
493     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
494     // strip out a 'v' if there is one.
495     if (llGetSubString(chop_suffix, 0, 0) == "v")
496         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
497     // if valid floating point number and greater than zero, that works for our version.
498     string basename = to_chop_up;  // script name with no version attached.
499     if ((float)chop_suffix > 0.0) {
500         // this is a big success right here.
501         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
502         return [ basename, chop_suffix ];
503     }
504     // seems like we found nothing useful.
505     return [];
506 }
507 //
508 //////////////
509
510 default
511 {
512     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
513     on_rez(integer parm) { state rerun; }
514 }
515 state rerun { state_entry() { state default; } }
516
517 state real_default
518 {
519     state_entry()
520     {
521         auto_retire();  // make sure newest addition is only version of script.
522         // reset all our variables.
523         menu_names = [];
524         menu_titles = [];
525         menu_button_lists = [];
526         _private_global_av_name = "";
527         _private_global_av_key = NULL_KEY;
528     }
529
530     link_message(integer sender, integer num, string msg, key id)
531     { handle_link_message(sender, num, msg, id); }
532 }
533