changed names, some may not be right.
[feisty_meow.git] / huffware / huffotronic_updater_freebie_v5.3 / non-script_giver_v2.8.lsl
1 
2 // huffware script: non-script giver, by fred huffhines.
3 //
4 // gives all objects, notecards, etc contained in an object when that object is touched.
5 // does not give out scripts, since these are generally not something that should be handed
6 // to the customer.
7 //
8 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
9 // do not use it in objects without fully realizing you are implicitly accepting that license.
10 //
11
12 integer ONLY_GIVE_TO_OWNER = TRUE;
13     // if this is true, then only the owner will receive a copy of the items.
14
15 integer GIVE_UNCOPYABLES = FALSE;
16     // this flag is dangerous when true, because it means that uncopyable objects will still
17     // be handed to the target.  if that target refuses the non-copyable items, then the items
18     // will be lost forever.  that is not so good if you've sold the person a non-copy item.
19
20 string EXCLUDED_NOTECARD = "product description";
21     // a special case; if there is a giftorse configuration card, we won't hand that out.
22
23 integer DEBUGGING = FALSE;  // set to true for noisier runs.
24
25 key first_toucher;  // tracks who clicked on the object to get contents.
26
27 // give out pictures, notecards, objects, etc. that are hiding in the object.
28 really_give_out_contents(key give_to)
29 {
30     list all_to_give = [];  // the set we will hand over in a batch.
31     list uncopyables = [];  // the list we have to do individually.
32     // find out how many items there are.
33     integer count = llGetInventoryNumber(INVENTORY_ALL);
34     // iterate across all the items and add them to the gift list if appropriate.
35     integer indy;
36     for (indy = 0; indy < count; indy++) {
37         string item_name = llGetInventoryName(INVENTORY_ALL, indy);
38         integer type = llGetInventoryType(item_name);
39         if ( (type != INVENTORY_SCRIPT) 
40             && ( (type != INVENTORY_NOTECARD) || (item_name != EXCLUDED_NOTECARD) ) ) {
41             // it's okay to add this item; it's not a script and we are not skipping the special notecard.
42             integer mask = MASK_OWNER;
43             if (!ONLY_GIVE_TO_OWNER) mask = MASK_EVERYONE;
44             integer perms = llGetInventoryPermMask(item_name, mask);
45             if (perms & PERM_COPY) {
46                 // a normal object that we can hand out.
47                 all_to_give += item_name;
48             } else {
49                 uncopyables += item_name;
50             }
51         }
52     }
53     // hand the customer the whole set as one big chunk, named after the object.
54     llGiveInventoryList(give_to, llGetObjectName(), all_to_give);
55
56     // handle any problematic items.  we cannot copy these objects into a category folder,
57     // so we can either not try to copy them (a lot safer) or we can try to deliver them
58     // normally as individual items.  the latter choice is more dangerous, because if the
59     // owner discards these items rather than keeping them, the items will be lost forever!
60     if (llGetListLength(uncopyables) > 0) {
61         string plural = " ";
62         string is_verb = "is ";
63         string third_noun_subj = "it ";
64         string third_noun_obj = "it ";
65         if (llGetListLength(uncopyables) > 1) {
66             plural = "s ";
67             is_verb = "are ";
68             third_noun_subj = "they ";
69             third_noun_obj = "them ";
70         }
71         
72         string uncopyable_message = "will be left inside the object.  To get " + third_noun_obj
73             + ", please copy " + third_noun_obj + "\nmanually from this object into your inventory.";
74         if (GIVE_UNCOPYABLES) {
75             uncopyable_message = "will be moved over to your inventory."
76             + "\nPlease look in your main folders for "
77             + third_noun_obj + "(e.g., in Objects or Textures).";
78         }
79         
80         string failure_message = "The item" + plural
81             + "[" + llDumpList2String(uncopyables, "; ") + "]\n"
82             + is_verb + "not copyable; " + third_noun_subj
83             + uncopyable_message;
84             
85         if (llGetOwner() == give_to) {
86             // the object can be moved to inventory, but not with the category method.
87             llOwnerSay(failure_message);
88         } else {
89             // this seems like a weird case; it will probably just fail anyhow?
90             // if the item's not copyable and you're not the owner of this object,
91             // how can we give it to you?
92             llInstantMessage(give_to, failure_message);
93         }
94         
95         // now that we've announced this weird situation, handle it appropriately.
96         if (GIVE_UNCOPYABLES) {
97             for (indy = 0; indy < llGetListLength(uncopyables); indy++) {
98                 string item_name = llList2String(uncopyables, indy);
99                 llGiveInventory(give_to, item_name);
100             }
101         }  // otherwise leave them be.
102     }
103 }
104
105
106 //////////////
107 // code borrowed from menutini to raise a menu asking if they actually meant to get all
108 // the contents.  an opensim inventory bug makes all the folders look foolish if we
109 // do any inventory giving accidentally.
110 //////////////
111
112 // global variables...
113
114 list _private_global_buttons;  // holds onto the active set of menu options.
115 string _private_global_av_key;  // the key for the avatar who clicks the menu.
116 string _private_global_title;  // holds onto current title text.
117
118 integer _menu_base_start = 0;  // position in the items of the current menu.
119
120 integer listening_id = 0;
121     // the current id of our listening for the menu.  it's an id returned by LSL
122     // that we need to track so we can cancel the listen.
123
124 integer menu_system_channel = -123;
125     // messages come back to us from this channel when user clicks the dialog.
126     // this is set later and the default is meaningless.
127
128 string global_menu_name = "";
129     // hangs onto the current menu's name.
130
131 //hmmm: note; to manage multiple concurrent menus on different channels,
132 //      we must make these into lists.  then the timeouts should apply
133 //      individually to these instead of overall (if we even do timeouts;
134 //      it's nicer if menus never stop being active).
135
136 string NEXT_MENU_TEXT = "Next >>";
137     // what the next item will say for showing next menu page.
138     
139 //integer TIMEOUT_FOR_MENU = 42;
140     // timeout for the menu in seconds.
141
142 // displays the menu requested.  it's "menu_name" is an internal name that is
143 // not displayed to the user.  the "title" is the content shown in the main area
144 // of the menu.  "commands_in" is the list of menu items to show as buttons.
145 // the "menu_channel" is where the user's clicked response will be sent.  the
146 // "listen_to" key is the avatar expected to click the menu, which is needed to
147 // listen to his response.
148 show_menu(string menu_name, string title, list buttons,
149     integer menu_channel, key listen_to)
150 {
151     // save our new parms.
152     global_menu_name = menu_name;
153     _private_global_title = title;
154     _private_global_buttons = buttons;
155     menu_system_channel = menu_channel;
156     _private_global_av_key = listen_to;
157     if (DEBUGGING) {
158         log_it("menu name: " + global_menu_name);
159         log_it("title: " + _private_global_title);
160         log_it("buttons: " + (string)buttons);
161         log_it("channel: " + (string)menu_system_channel);
162         log_it("listen key: " + (string)listen_to);
163     }
164
165     integer add_next = FALSE;  // true if we should add a next menu item.
166
167     // the math here incorporates current button position.
168     integer current = _menu_base_start;
169     integer max_buttons = llGetListLength(buttons) - current;
170
171     if (max_buttons > 12) {
172         // limitation of SL: menus have a max of 12 buttons.
173         max_buttons = 12;
174         add_next = TRUE;
175     } else if (llGetListLength(buttons) > 12) {
176         // we already have been adding next.  let's make sure this gets
177         // a wrap-around next button.
178         add_next = TRUE;
179     }
180     // chop out what we can use in a menu.
181     list trunc_buttons = llList2List(buttons, current, current + max_buttons - 1);
182     if (add_next) {
183         // we were asked to add a menu item for the next screen.
184         trunc_buttons = llList2List(trunc_buttons, 0, 10) + NEXT_MENU_TEXT;
185     }
186
187     listening_id = llListen(menu_channel, "", listen_to, "");
188     list commands;
189     integer i;
190     // take only the prefix of the string, to avoid getting a length complaint.
191     for (i = 0; i < llGetListLength(trunc_buttons); i++) {
192         string curr = llList2String(trunc_buttons, i);
193         integer last_pos = 23;  // default maximum, highest possible is 24.
194         if (llStringLength(curr) - 1 < last_pos) last_pos = llStringLength(curr) - 1;
195         curr = llGetSubString(curr, 0, last_pos);
196         commands += curr;
197     }
198     llDialog(listen_to, title, commands, menu_channel);
199 }
200
201 // shuts down any connection we might have had with any active menu.  we will not
202 // send any responses after this point (although we might already have responded when
203 // the user clicked the menu).
204 clear_menu()
205 {
206     llListenRemove(listening_id);
207 }
208
209 // process the response when the user chooses a menu item.
210 process_menu_response(integer channel, string name, key id, string message)
211 {
212   if (channel != menu_system_channel) return;  // not for us.
213   
214     if (message == NEXT_MENU_TEXT) {
215         // this is the special choice, so we need to go to the next page.
216         _menu_base_start += 11;
217         if (_menu_base_start > llGetListLength(_private_global_buttons)) {
218             // we have wrapped around the list.  go to the start again.
219             _menu_base_start = 0;
220         }
221         show_menu(global_menu_name, _private_global_title,
222             _private_global_buttons, menu_system_channel,
223             _private_global_av_key);
224         return;  // handled by opening a new menu.
225     }
226     
227     string calculated_name;
228     integer indy;
229     // first try for an exact match.
230     for (indy = 0; indy < llGetListLength(_private_global_buttons); indy++) {
231         string curr = llList2String(_private_global_buttons, indy);
232         if (curr == message) {
233             // correct the answer based on the full button string.
234             calculated_name = curr;
235         }//////////////[copy starting here...]//////////////
236
237     }
238     if (calculated_name == "") {
239         // try an imprecise match if the exact matching didn't work.
240         for (indy = 0; indy < llGetListLength(_private_global_buttons); indy++) {
241             string curr = llList2String(_private_global_buttons, indy);
242             if (is_prefix(curr, message)) {
243                 // correct the answer based on the full button string.
244                 calculated_name = curr;
245             }
246         }
247     }
248     if (calculated_name == "yes") {
249         // only send a response if that menu choice made sense to us.
250         really_give_out_contents(first_toucher);
251         clear_menu();
252     }
253 }
254
255 // end from menutini.
256 //////////////
257
258
259 //////////////
260 //
261 // borrowed from hufflets...
262
263 integer debug_num = 0;
264
265 // a debugging output method.  can be disabled entirely in one place.
266 log_it(string to_say)
267 {
268     debug_num++;
269     // tell this to the owner.    
270     llOwnerSay(llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
271 //llWhisper(0, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
272     // say this on an unusual channel for chat if it's not intended for general public.
273 //    llSay(108, llGetDate() + ": " + llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
274     // say this on open chat that anyone can hear.  we take off the bling for this one.
275 //    llSay(0, to_say);
276 }
277
278 // returns the index of the first occurrence of "pattern" inside
279 // the "full_string".  if it is not found, then a negative number is returned.
280 integer find_substring(string full_string, string pattern)
281 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
282
283 // returns TRUE if the "prefix" string is the first part of "compare_with".
284 integer is_prefix(string compare_with, string prefix)
285 { return (llSubStringIndex(compare_with, prefix) == 0); }
286
287 //////////////
288 // huffware script: auto-retire, by fred huffhines, version 2.8.
289 // distributed under BSD-like license.
290 //   !!  keep in mind that this code must be *copied* into another
291 //   !!  script that you wish to add auto-retirement capability to.
292 // when a script has auto_retire in it, it can be dropped into an
293 // object and the most recent version of the script will destroy
294 // all older versions.
295 //
296 // the version numbers are embedded into the script names themselves.
297 // the notation for versions uses a letter 'v', followed by two numbers
298 // in the form "major.minor".
299 // major and minor versions are implicitly considered as a floating point
300 // number that increases with each newer version of the script.  thus,
301 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
302 // and "hazmap v3.2" is a more recent version.
303 //
304 // example usage of the auto-retirement script:
305 //     default {
306 //         state_entry() {
307 //            auto_retire();  // make sure newest addition is only version of script.
308 //        }
309 //     }
310 // this script is partly based on the self-upgrading scripts from markov brodsky
311 // and jippen faddoul.
312 //////////////
313 auto_retire() {
314     string self = llGetScriptName();  // the name of this script.
315     list split = compute_basename_and_version(self);
316     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
317     string basename = llList2String(split, 0);  // script name with no version attached.
318     string version_string = llList2String(split, 1);  // the version found.
319     integer posn;
320     // find any scripts that match the basename.  they are variants of this script.
321     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
322         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
323         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
324             // found a basic match at least.
325             list inv_split = compute_basename_and_version(curr_script);
326             if (llGetListLength(inv_split) == 2) {
327                 // see if this script is more ancient.
328                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
329                 // must make sure that the retiring script is completely the identical basename;
330                 // just matching in the front doesn't make it a relative.
331                 if ( (llList2String(inv_split, 0) == basename)
332                     && ((float)inv_version_string < (float)version_string) ) {
333                     // remove script with same name from inventory that has inferior version.
334                     llRemoveInventory(curr_script);
335                 }
336             }
337         }
338     }
339 }
340 //
341 // separates the base script name and version number.  used by auto_retire.
342 list compute_basename_and_version(string to_chop_up)
343 {
344     // minimum script name is 2 characters plus a version.
345     integer space_v_posn;
346     // find the last useful space and 'v' combo.
347     for (space_v_posn = llStringLength(to_chop_up) - 3;
348         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
349         space_v_posn--) {
350         // look for space and v but do nothing else.
351     }
352     if (space_v_posn < 2) return [];  // no space found.
353     // now we zoom through the stuff after our beloved v character and find any evil
354     // space characters, which are most likely from SL having found a duplicate item
355     // name and not so helpfully renamed it for us.
356     integer indy;
357     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
358         if (llGetSubString(to_chop_up, indy, indy) == " ") {
359             // found one; zap it.  since we're going backwards we don't need to
360             // adjust the loop at all.
361             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
362         }
363     }
364     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
365     // ditch the space character for our numerical check.
366     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
367     // strip out a 'v' if there is one.
368     if (llGetSubString(chop_suffix, 0, 0) == "v")
369         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
370     // if valid floating point number and greater than zero, that works for our version.
371     string basename = to_chop_up;  // script name with no version attached.
372     if ((float)chop_suffix > 0.0) {
373         // this is a big success right here.
374         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
375         return [ basename, chop_suffix ];
376     }
377     // seems like we found nothing useful.
378     return [];
379 }
380 //
381 //////////////
382
383 default
384 {
385     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
386     on_rez(integer parm) { state rerun; }
387 }
388 state rerun { state_entry() { state default; } }
389
390 state real_default
391 {
392     state_entry() { 
393         auto_retire();
394     }
395     
396     on_rez(integer param) { llResetScript(); }
397
398     touch_start(integer num) {
399         first_toucher = llDetectedKey(0);
400         // are we only supposed to give stuff to the owner?
401         if (ONLY_GIVE_TO_OWNER && (first_toucher != llGetOwner()) ) {
402             first_toucher = NULL_KEY;
403             return;  // bail out.
404         }
405         show_menu("askreally", "Would you like a copy of this object's contents?",
406             ["yes", "no"], -18264, first_toucher);
407     }
408
409     listen(integer channel, string name, key id, string message)
410     { process_menu_response(channel, name, id, message); }
411
412 }