more code committed for LSL huffware, basically dropping the freebie updater version
[feisty_meow.git] / huffware / huffotronic_eepaw_knowledge_v60.9 / searchbert_menus_v4.6.lsl
1 
2 // huffware script: searchbert menus, by fred huffhines.
3 //
4 // manages the menu system for searchbert so the main script is free to do its thing.
5 //
6 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
7 // do not use it in objects without fully realizing you are implicitly accepting that license.
8 //
9
10 //notes
11 // + need to have the searchbert initiate anything here.  this cannot be free range and
12 //   running even if the searchbert isn't in a menuable mode.
13 // + is the above true?
14
15 // global constants...
16
17 string MAIN_MENU_NAME = "main";  // name we use in menu list manager for the main menu.
18 // boilerplate text that's shown at top of main menu.  we'll add the channel info later.
19 string MAIN_MENU_PREFIX = "Quick help: Say '";
20 string MAIN_MENU_MIDDLE = "#find XYZ' in chat to locate nearby objects with 'XYZ' in their names.\nSay '";
21 string MAIN_MENU_SUFFIX = "#reset' to stop showing a previous search.\n\n[Matches] shows recent search results.\n[Configure] changes options.\n[Reset] clears recent results.\n[Help] dispenses an instruction notecard.";
22
23 // main menu items...
24 string HELP_CHOICE = "Help";
25 string CONFIG_CHOICE = "Configure";
26 string MATCHES_CHOICE = "Matches";
27 string RESET_CHOICE = "Reset";
28
29 // configuration menu items...
30 string MAX_MATCHES_CHOICE = "Max Match";
31 string CHAT_CHANNEL_CHOICE = "Channel";
32
33 // menu indices...
34 integer MAIN_MENU_INDEX = 0;  // we always do main menu as zero.
35 integer CONFIG_MENU_INDEX = 1;  // choices for configurable items.
36 integer MAX_MATCHES_MENU_INDEX = 2;  // changing the number of matches.
37
38 // searchbert menus API.
39 //////////////
40 // do not redefine these constants.
41 integer SEARCHBERT_MENUS_HUFFWARE_ID = 10034;
42     // the unique id within the huffware system for this script.
43 //////////////
44 string SM_CONFIGURE_INFO = "#sm-info#";
45     // sets important information this script will use, such as (1) the channel for listening.
46 string SM_POP_MAIN_MENU_UP = "#sm-main#";
47     // causes the main menu to be displayed.  this requires an avatar name and avatar key for the
48     // target of the menu.
49 //////////////
50 string SM_EVENT_MENU_CLICK = "#sm-clik#";
51     // the user has requested a particular menu item that this script cannot fulfill.  the
52     // event is generated back to the client of this script for handling.  it will include (1) the
53     // menu name in question, (2) the item clicked, (3) the avatar name, and (4) the avatar key.
54 //////////////
55
56 // imported interfaces...
57
58 // menu list manager link message API.
59 //////////////
60 // do not redefine these constants.
61 integer MENU_LIST_MANAGER_HUFFWARE_ID = 10033;
62     // the unique id within the huffware system for this sensor plugin script.
63 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
64     // this pattern is an uncommon thing to see in text, so we use it to separate
65     // our commands in link messages.
66 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
67     // used to separate lists of items from each other when stored inside a parameter.
68     // this allows lists to be passed as single string parameters if needed.
69 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
70 string MENU_LIST_MGR_RESET = "#ml-reset#";
71     // throws out any current menus and gets ready to load a new set.
72 string MENU_LIST_MGR_ADD_MENU = "#ml-addmenu#";
73     // puts a new menu into our list.  this requires 3 parameters: menu name,
74     // menu title, menu button list.
75 string MENU_LIST_MGR_SHOW_MENU = "#ml-shomenu#";
76     // a request that a particular menu be shown.  the first parameter is the menu index.
77     // the second parameter is the avatar name.  the third parameter is the avatar key.
78 string MENU_LIST_MGR_GIVE_ITEM = "#ml-give#";
79     // brings up a gift giving menu.  parms are avatar name and avatar key.
80 string MENU_LIST_MGR_MODIFY_MENU = "#ml-modmenu#";
81 string MENU_LIST_KEEP_WORD = "KEEP";
82     // replaces a menu already in the list.  this requires 4 parameters: menu index, menu name,
83     // menu title, menu button list.  if a parameter is the MENU_LIST_KEEP_WORD, then that field
84     // is not changed.
85 string MENU_LIST_MGR_CHOICE_MADE_ALERT = "#ml-event-picked";
86     // alerts the driver for this script that the owner has picked a choice.  the
87     // parameters include: the text of the choice, the name of the menu, the avatar name,
88     // the avatar key.
89 //
90 //////////////
91
92 // we snag just enough of this interface to masquerade as it...
93 // card configurator link message API:
94 //////////////
95 // do not redefine these constants.
96 integer CARD_CONFIGURATOR_HUFFWARE_ID = 10042;
97     // the unique id within the huffware system for the card configurator script to
98     // accept commands on.  this is used in llMessageLinked as the num parameter.
99 //////////////
100 string CARD_CONFIG_RECEIVED_ALERT = "#cfg-event-upd#";
101     // this message is sent when the configurator has found some data updates or has finished
102     // reading the configuration file.
103 //////////////
104
105 // global variables...
106
107 integer initialized_menus_yet = FALSE;
108     // records if the menus have been set up.
109
110 // additional information that's provided by the client script.
111 integer TALKY_CHANNEL = 0;
112
113 //////////////
114
115 // sends a new menu to be placed in the menu manager.
116 add_menu(string menu_name, string menu_title, list button_list)
117 {
118     if (llGetListLength(button_list) == 0) {
119         // patch this degenerate list into a bogus one.
120         button_list = [ "none" ];
121     }
122     llMessageLinked(LINK_ROOT, MENU_LIST_MANAGER_HUFFWARE_ID, MENU_LIST_MGR_ADD_MENU,
123         menu_name + HUFFWARE_PARM_SEPARATOR
124         + menu_title + HUFFWARE_PARM_SEPARATOR
125         + wrap_item_list(button_list));
126 }
127
128 // stuffs in a new version of an old menu.
129 replace_menu(integer index, string menu_name, string menu_title, list button_list)
130 {
131     llMessageLinked(LINK_ROOT, MENU_LIST_MANAGER_HUFFWARE_ID, MENU_LIST_MGR_MODIFY_MENU,
132         (string)index + HUFFWARE_PARM_SEPARATOR
133         + menu_name + HUFFWARE_PARM_SEPARATOR
134         + menu_title + HUFFWARE_PARM_SEPARATOR
135         + wrap_item_list(button_list));
136 }
137
138 // sets up the main menu options.  if "new_now" is false, then this replaces
139 // the main menu rather than expecting to add a new one.
140 establish_main_menu(integer new_now)
141 {
142     // menu zero: this is the main menu.
143     string menu_name = "main";
144     string menu_title = MAIN_MENU_PREFIX + channel_string() + MAIN_MENU_MIDDLE
145         + channel_string() + MAIN_MENU_SUFFIX;
146     list menu_button_list = [ MATCHES_CHOICE, CONFIG_CHOICE, RESET_CHOICE, HELP_CHOICE ];
147     if (new_now) {
148         add_menu(menu_name, menu_title, menu_button_list);
149     } else {
150         replace_menu(MAIN_MENU_INDEX, menu_name, menu_title, menu_button_list);
151     }
152 }
153
154 // sets up all of the static menus for searchbert.
155 prepare_all_menus()
156 {
157     establish_main_menu(TRUE);  // set up the main menu as a new menu.
158
159     // configuration menu provides tasty options for changing the way searchbert behaves.
160     string menu_name = CONFIG_CHOICE;  // reusing menu button from main menu for the menu name.
161     string menu_title = "Configurable Options...\n[" + MAX_MATCHES_CHOICE + "] selects number of items to find.\n[" + CHAT_CHANNEL_CHOICE + "] changes listening channel for commands.\n";
162     list menu_button_list = [ MAX_MATCHES_CHOICE, CHAT_CHANNEL_CHOICE ];
163     add_menu(menu_name, menu_title, menu_button_list);
164
165     menu_name = MAX_MATCHES_CHOICE;  // reusing menu button from config menu.
166     menu_title = "Choose a new maximum number of items to find:";
167     menu_button_list = produce_nums(17);  // ultimate number of arms available (magic constant!).
168     add_menu(menu_name, menu_title, menu_button_list);
169
170 }
171
172 // if we haven't initialized yet, we'll do it now.
173 maybe_really_setup_menus()
174 {
175     if (!initialized_menus_yet) {
176 //log_it("needed initialization still!");
177         prepare_all_menus();
178         initialized_menus_yet = TRUE;
179         llSetTimerEvent(0);  // stop timer.
180         return;
181     }
182 }
183
184 // deals with the timer elapsing.
185 handle_timer_hit()
186 {
187     // see if this timer is for initialization purposes.
188     maybe_really_setup_menus();
189 }
190
191 // processes a message requesting our services or updating our info.  or ignore it.
192 handle_link_message(integer sender, integer num, string msg, key id)
193 {
194     if ( (num != MENU_LIST_MANAGER_HUFFWARE_ID + REPLY_DISTANCE)
195             && (num != SEARCHBERT_MENUS_HUFFWARE_ID) )
196         return;
197
198     // make sure we're already initialized.
199     maybe_really_setup_menus();
200
201     list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
202
203     if (num == MENU_LIST_MANAGER_HUFFWARE_ID + REPLY_DISTANCE) {
204         if (msg == MENU_LIST_MGR_CHOICE_MADE_ALERT) {
205             // now deal with the implications of the menu they chose.
206             process_menu_choice(llList2String(parms, 1), llList2String(parms, 2),
207                 llList2String(parms, 3), llList2String(parms, 0));
208         }
209         return;
210     }
211     
212     // if we got to here, it must be for our main interface methods.
213 //log_it("got searchbert menu request: " + msg + ".  memory left=" + (string)llGetFreeMemory());
214     if (msg == SM_CONFIGURE_INFO) {
215         TALKY_CHANNEL = (integer)llList2String(parms, 0);
216         establish_main_menu(FALSE);
217     } else if (msg == SM_POP_MAIN_MENU_UP) {
218         // show the main menu for the specified avatar.
219         request_menu_popup(MAIN_MENU_INDEX, llList2String(parms, 0), llList2String(parms, 1));
220     }
221
222 }
223
224 request_menu_popup(integer menu_index, string av_name, string av_key)
225 {
226     llMessageLinked(LINK_ROOT, MENU_LIST_MANAGER_HUFFWARE_ID, MENU_LIST_MGR_SHOW_MENU,
227         wrap_parameters([menu_index, av_name, av_key]));
228 //log_it("sent menu popup...  memory left=" + (string)llGetFreeMemory());
229 }
230
231 // generates a list of numbers up to and including the "max".
232 list produce_nums(integer max)
233 {
234     list to_return;
235     integer indy;
236     for (indy = 1; indy <= max; indy++) {
237         to_return += (string)indy;
238     }
239     return to_return;
240 }
241
242 // returns the appropriate extra text if the channel is not zero for open chat.
243 string channel_string()
244 {
245     string add_in_channel = "";
246     if (TALKY_CHANNEL != 0)
247         add_in_channel = "/" + (string)TALKY_CHANNEL + " ";
248     return add_in_channel;
249 }
250
251 // handles when a menu has been clicked on.
252 process_menu_choice(string menu_name, string av_name, string av_key, string which_choice)
253 {
254 //log_it("into process menu " + menu_name + " for " + av_name + " who chose " + which_choice);
255     if (menu_name == MAIN_MENU_NAME) {
256         if (which_choice == HELP_CHOICE) {
257             // see if we can find a helper notecard.
258             integer indy = find_basename_in_inventory("docs", INVENTORY_NOTECARD);
259             if (indy < 0) {
260                 llSay(0, "We're very sorry, but there do not seem to be any documentation notecards available.  There may be a better version at eepaw shop (osgrid or SL).");
261             } else {
262                 string invname = llGetInventoryName(INVENTORY_NOTECARD, indy);
263                 llGiveInventory(av_key, invname);
264                 llWhisper(0, "Here's a copy of the help file: " + invname);
265             }
266             return;
267         } else if (which_choice == CONFIG_CHOICE) {
268             request_menu_popup(CONFIG_MENU_INDEX, av_name, av_key);
269             return;
270         }
271         // MATCHES_CHOICE falls through for forwarding.
272         // so does RESET_CHOICE.
273     } else if (menu_name == CONFIG_CHOICE) {
274         if (which_choice == MAX_MATCHES_CHOICE) {
275             // let them pick a new maximum number of matches to find.
276             request_menu_popup(MAX_MATCHES_MENU_INDEX, av_name, av_key);
277             return;
278         } else if (which_choice == CHAT_CHANNEL_CHOICE) {
279             llSay(0, "To change the chat channel, say '" + channel_string()
280                 + "#channel N' where N is your new channel number.");
281             return;
282         }
283 //more config options...        
284
285     } else if (menu_name == MAX_MATCHES_CHOICE) {
286         integer max = (integer)which_choice;
287         if (max >= 1) {
288             // a new number of search matches has been decided upon.
289             send_data_burst(["max_matches", which_choice]);
290             llSay(0, "Maximum number of matches is now set to " + (string)max + ".");
291             return;
292         }
293     }
294
295     // cases that aren't handled get forwarded to the client script.    
296     llMessageLinked(LINK_ROOT, SEARCHBERT_MENUS_HUFFWARE_ID + REPLY_DISTANCE, SM_EVENT_MENU_CLICK ,
297         wrap_parameters([menu_name, which_choice, av_name, av_key]));
298 }
299
300 // borrowed from card configurator.
301 // sends the currently held data out to whoever requested it.
302 send_data_burst(list to_send)
303 {
304 //log_it("sending " + (string)llGetListLength(to_send) + " items");
305     llMessageLinked(LINK_THIS, CARD_CONFIGURATOR_HUFFWARE_ID + REPLY_DISTANCE, CARD_CONFIG_RECEIVED_ALERT,
306         wrap_parameters(["yo-updated"] + to_send));
307     to_send = [];  // reset any items held.
308 }
309
310 // set up all the parts of the externally configured bits.
311 initialize()
312 {
313 //log_it("initializing.  memory left=" + (string)llGetFreeMemory());
314     llSleep(0.1);  // initial pause before chatting with menu manager.
315
316     // reset the menu manager to get it ready for our new menus.
317     llMessageLinked(LINK_THIS, MENU_LIST_MANAGER_HUFFWARE_ID, MENU_LIST_MGR_RESET, "");
318
319     // snooze a bit to allow our helper to wake up.
320     llSetTimerEvent(0.34);  // snooze until helper is ready.
321     initialized_menus_yet = FALSE;
322 }
323
324 //////////////
325 // from hufflets...
326 //
327 integer debug_num = 0;
328
329 // a debugging output method.  can be disabled entirely in one place.
330 log_it(string to_say)
331 {
332     debug_num++;
333     // tell this to the owner.    
334     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
335     // say this on open chat, but use an unusual channel.
336 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
337 }
338
339 //////////////
340
341 // joins a list of parameters using the parameter sentinel for the library.
342 string wrap_parameters(list to_flatten)
343 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
344
345 string wrap_item_list(list to_wrap)
346 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
347
348 //////////////
349
350 // locates the string "text" in the list to "search_in".
351 integer find_in_list(list search_in, string text)
352
353     integer len = llGetListLength(search_in);
354     integer i; 
355     for (i = 0; i < len; i++) { 
356         if (llList2String(search_in, i) == text) 
357             return i; 
358     } 
359     return -1;
360 }
361
362 // returns the index of the first occurrence of "pattern" inside
363 // the "full_string".  if it is not found, then a negative number is returned.
364 integer find_substring(string full_string, string pattern)
365 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
366
367 // returns TRUE if the "prefix" string is the first part of "compare_with".
368 integer is_prefix(string compare_with, string prefix)
369 { return find_substring(compare_with, prefix) == 0; }
370
371 // looks for an inventory item with the same prefix as the "basename_to_seek".
372 integer find_basename_in_inventory(string basename_to_seek, integer inv_type)
373 {
374     integer num_inv = llGetInventoryNumber(inv_type);
375     if (num_inv == 0) return -1;  // nothing there!
376     integer indy;
377     for (indy = 0; indy < num_inv; indy++) {
378         if (is_prefix(llGetInventoryName(inv_type, indy), basename_to_seek))
379             return indy;
380     }
381     return -2;  // failed to find it.
382 }
383
384 //////////////
385 // huffware script: auto-retire, by fred huffhines, version 2.8.
386 // distributed under BSD-like license.
387 //   !!  keep in mind that this code must be *copied* into another
388 //   !!  script that you wish to add auto-retirement capability to.
389 // when a script has auto_retire in it, it can be dropped into an
390 // object and the most recent version of the script will destroy
391 // all older versions.
392 //
393 // the version numbers are embedded into the script names themselves.
394 // the notation for versions uses a letter 'v', followed by two numbers
395 // in the form "major.minor".
396 // major and minor versions are implicitly considered as a floating point
397 // number that increases with each newer version of the script.  thus,
398 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
399 // and "hazmap v3.2" is a more recent version.
400 //
401 // example usage of the auto-retirement script:
402 //     default {
403 //         state_entry() {
404 //            auto_retire();  // make sure newest addition is only version of script.
405 //        }
406 //     }
407 // this script is partly based on the self-upgrading scripts from markov brodsky
408 // and jippen faddoul.
409 //////////////
410 auto_retire() {
411     string self = llGetScriptName();  // the name of this script.
412     list split = compute_basename_and_version(self);
413     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
414     string basename = llList2String(split, 0);  // script name with no version attached.
415     string version_string = llList2String(split, 1);  // the version found.
416     integer posn;
417     // find any scripts that match the basename.  they are variants of this script.
418     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
419         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
420         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
421             // found a basic match at least.
422             list inv_split = compute_basename_and_version(curr_script);
423             if (llGetListLength(inv_split) == 2) {
424                 // see if this script is more ancient.
425                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
426                 // must make sure that the retiring script is completely the identical basename;
427                 // just matching in the front doesn't make it a relative.
428                 if ( (llList2String(inv_split, 0) == basename)
429                     && ((float)inv_version_string < (float)version_string) ) {
430                     // remove script with same name from inventory that has inferior version.
431                     llRemoveInventory(curr_script);
432                 }
433             }
434         }
435     }
436 }
437 //
438 // separates the base script name and version number.  used by auto_retire.
439 list compute_basename_and_version(string to_chop_up)
440 {
441     // minimum script name is 2 characters plus a version.
442     integer space_v_posn;
443     // find the last useful space and 'v' combo.
444     for (space_v_posn = llStringLength(to_chop_up) - 3;
445         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
446         space_v_posn--) {
447         // look for space and v but do nothing else.
448     }
449     if (space_v_posn < 2) return [];  // no space found.
450     // now we zoom through the stuff after our beloved v character and find any evil
451     // space characters, which are most likely from SL having found a duplicate item
452     // name and not so helpfully renamed it for us.
453     integer indy;
454     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
455         if (llGetSubString(to_chop_up, indy, indy) == " ") {
456             // found one; zap it.  since we're going backwards we don't need to
457             // adjust the loop at all.
458             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
459         }
460     }
461     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
462     // ditch the space character for our numerical check.
463     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
464     // strip out a 'v' if there is one.
465     if (llGetSubString(chop_suffix, 0, 0) == "v")
466         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
467     // if valid floating point number and greater than zero, that works for our version.
468     string basename = to_chop_up;  // script name with no version attached.
469     if ((float)chop_suffix > 0.0) {
470         // this is a big success right here.
471         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
472         return [ basename, chop_suffix ];
473     }
474     // seems like we found nothing useful.
475     return [];
476 }
477 //
478 //////////////
479
480 // end hufflets.
481 //////////////
482
483 default {
484     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
485     on_rez(integer parm) { state rerun; }
486 }
487 state rerun { state_entry() { state default; } }
488
489 state real_default
490 {
491     state_entry() {
492         auto_retire();
493         initialize();
494     }
495     
496     on_rez(integer parm) { llResetScript(); }
497     
498     timer() { handle_timer_hit(); }
499
500     link_message(integer sender, integer num, string msg, key id) {
501         handle_link_message(sender, num, msg, id);
502     }
503
504 }
505