normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / no_sitting_on_vendor_v1.0.txt
1 
2 // huffware script: no sitting on vendor, by fred huffhines.
3 //
4 //   this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
5 //   do not use it in objects without fully realizing you are implicitly accepting that license.
6 //
7
8 // this message will be displayed when someone sits on the vendor.
9 string AVATAR_MESSAGE = "You cannot sit here; I am a vendor, not a chair.  Please touch me for a notecard, or pay me to purchase the product.  Thanks!";
10
11 //////////////
12 // huffware script: auto-retire, by fred huffhines, version 2.5.
13 // distributed under BSD-like license.
14 //   !!  keep in mind that this code must be *copied* into another
15 //   !!  script that you wish to add auto-retirement capability to.
16 // when a script has auto_retire in it, it can be dropped into an
17 // object and the most recent version of the script will destroy
18 // all older versions.
19 //
20 // the version numbers are embedded into the script names themselves.
21 // the notation for versions uses a letter 'v', followed by two numbers
22 // in the form "major.minor".
23 // major and minor versions are implicitly considered as a floating point
24 // number that increases with each newer version of the script.  thus,
25 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
26 // and "hazmap v3.2" is a more recent version.
27 //
28 // example usage of the auto-retirement script:
29 //     default {
30 //         state_entry() {
31 //            auto_retire();  // make sure newest addition is only version of script.
32 //        }
33 //     }
34 // this script is partly based on the self-upgrading scripts from markov brodsky
35 // and jippen faddoul.
36 //////////////
37 auto_retire() {
38     string self = llGetScriptName();  // the name of this script.
39     list split = compute_basename_and_version(self);
40     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
41     string basename = llList2String(split, 0);  // script name with no version attached.
42     string version_string = llList2String(split, 1);  // the version found.
43     integer posn;
44     // find any scripts that match the basename.  they are variants of this script.
45     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
46 //log_it("invpo=" + (string)posn);
47         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
48         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
49             // found a basic match at least.
50             list inv_split = compute_basename_and_version(curr_script);
51             if (llGetListLength(inv_split) == 2) {
52                 // see if this script is more ancient.
53                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
54                 // must make sure that the retiring script is completely the identical basename;
55                 // just matching in the front doesn't make it a relative.
56                 if ( (llList2String(inv_split, 0) == basename)
57                     && ((float)inv_version_string < (float)version_string) ) {
58                     // remove script with same name from inventory that has inferior version.
59                     llRemoveInventory(curr_script);
60                 }
61             }
62         }
63     }
64 }
65 //
66 // separates the base script name and version number.  used by auto_retire.
67 list compute_basename_and_version(string to_chop_up)
68 {
69     // minimum script name is 2 characters plus a version.
70     integer space_v_posn;
71     // find the last useful space and 'v' combo.
72     for (space_v_posn = llStringLength(to_chop_up) - 3;
73         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
74         space_v_posn--) {
75         // look for space and v but do nothing else.
76 //log_it("pos=" + (string)space_v_posn);
77     }
78     if (space_v_posn < 2) return [];  // no space found.
79 //log_it("space v@" + (string)space_v_posn);
80     // now we zoom through the stuff after our beloved v character and find any evil
81     // space characters, which are most likely from SL having found a duplicate item
82     // name and not so helpfully renamed it for us.
83     integer indy;
84     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
85 //log_it("indy=" + (string)space_v_posn);
86         if (llGetSubString(to_chop_up, indy, indy) == " ") {
87             // found one; zap it.  since we're going backwards we don't need to
88             // adjust the loop at all.
89             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
90 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
91         }
92     }
93     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
94     // ditch the space character for our numerical check.
95     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
96     // strip out a 'v' if there is one.
97     if (llGetSubString(chop_suffix, 0, 0) == "v")
98         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
99     // if valid floating point number and greater than zero, that works for our version.
100     string basename = to_chop_up;  // script name with no version attached.
101     if ((float)chop_suffix > 0.0) {
102         // this is a big success right here.
103         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
104         return [ basename, chop_suffix ];
105     }
106     // seems like we found nothing useful.
107     return [];
108 }
109 //
110 //////////////
111
112 default {
113     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
114     on_rez(integer parm) { state rerun; }
115 }
116 state rerun { state_entry() { state default; } }
117
118 state real_default
119 {
120     state_entry()
121     {
122         auto_retire();  // make sure newest addition is only version of script.
123         llSitTarget(ZERO_VECTOR, ZERO_ROTATION);
124     }
125     
126     changed(integer change)
127     {
128         key id = llAvatarOnSitTarget();
129         if (change & CHANGED_LINK) {
130             if (id != NULL_KEY) {
131                 llUnSit(id);
132                 llDialog(id, AVATAR_MESSAGE, [], -981238);
133                     // we don't listen for the response.
134             }
135         }
136     }
137 }