6dee724ddde1109b4a6fd3ad5b2b964b841475dd
[feisty_meow.git] / huffware / huffotronic_scripts / zap_updater_from_elevators_v1.3.txt
1 
2 // huffware script: zap updater from elevators, by fred huffhines
3 //
4 // this script is an evil little assassin that is supposed to remove the updater
5 // script from the objects it's added to.  it should not remove that script from
6 // any huffotronic devices though.
7 // this is an attempt to quell some SL stability problems due to some seeming
8 // combination of numbers of objects and numbers of scripts.
9 // we hope to be able to add the updater back in automatically later because the
10 // script pin should still be set.  let's see if osgrid has that behavior the same
11 // as second life did.
12 //
13 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
14 // do not use it in objects without fully realizing you are implicitly accepting that license.
15 //
16
17
18 // from hufflets...
19
20 //////////////
21 // huffware script: auto-retire, by fred huffhines, version 2.8.
22 // distributed under BSD-like license.
23 //   !!  keep in mind that this code must be *copied* into another
24 //   !!  script that you wish to add auto-retirement capability to.
25 // when a script has auto_retire in it, it can be dropped into an
26 // object and the most recent version of the script will destroy
27 // all older versions.
28 //
29 // the version numbers are embedded into the script names themselves.
30 // the notation for versions uses a letter 'v', followed by two numbers
31 // in the form "major.minor".
32 // major and minor versions are implicitly considered as a floating point
33 // number that increases with each newer version of the script.  thus,
34 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
35 // and "hazmap v3.2" is a more recent version.
36 //
37 // example usage of the auto-retirement script:
38 //     default {
39 //         state_entry() {
40 //            auto_retire();  // make sure newest addition is only version of script.
41 //        }
42 //     }
43 // this script is partly based on the self-upgrading scripts from markov brodsky
44 // and jippen faddoul.
45 //////////////
46 auto_retire() {
47     string self = llGetScriptName();  // the name of this script.
48     list split = compute_basename_and_version(self);
49     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
50     string basename = llList2String(split, 0);  // script name with no version attached.
51     string version_string = llList2String(split, 1);  // the version found.
52     integer posn;
53     // find any scripts that match the basename.  they are variants of this script.
54     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
55         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
56         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
57             // found a basic match at least.
58             list inv_split = compute_basename_and_version(curr_script);
59             if (llGetListLength(inv_split) == 2) {
60                 // see if this script is more ancient.
61                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
62                 // must make sure that the retiring script is completely the identical basename;
63                 // just matching in the front doesn't make it a relative.
64                 if ( (llList2String(inv_split, 0) == basename)
65                     && ((float)inv_version_string < (float)version_string) ) {
66                     // remove script with same name from inventory that has inferior version.
67                     llRemoveInventory(curr_script);
68                 }
69             }
70         }
71     }
72 }
73 //
74 // separates the base script name and version number.  used by auto_retire.
75 list compute_basename_and_version(string to_chop_up)
76 {
77     // minimum script name is 2 characters plus a version.
78     integer space_v_posn;
79     // find the last useful space and 'v' combo.
80     for (space_v_posn = llStringLength(to_chop_up) - 3;
81         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
82         space_v_posn--) {
83         // look for space and v but do nothing else.
84     }
85     if (space_v_posn < 2) return [];  // no space found.
86     // now we zoom through the stuff after our beloved v character and find any evil
87     // space characters, which are most likely from SL having found a duplicate item
88     // name and not so helpfully renamed it for us.
89     integer indy;
90     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
91         if (llGetSubString(to_chop_up, indy, indy) == " ") {
92             // found one; zap it.  since we're going backwards we don't need to
93             // adjust the loop at all.
94             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
95         }
96     }
97     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
98     // ditch the space character for our numerical check.
99     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
100     // strip out a 'v' if there is one.
101     if (llGetSubString(chop_suffix, 0, 0) == "v")
102         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
103     // if valid floating point number and greater than zero, that works for our version.
104     string basename = to_chop_up;  // script name with no version attached.
105     if ((float)chop_suffix > 0.0) {
106         // this is a big success right here.
107         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
108         return [ basename, chop_suffix ];
109     }
110     // seems like we found nothing useful.
111     return [];
112 }
113 //
114 //////////////
115
116 // returns the index of the first occurrence of "pattern" inside
117 // the "full_string".  if it is not found, then a negative number is returned.
118 integer find_substring(string full_string, string pattern)
119 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
120
121 // returns TRUE if the "prefix" string is the first part of "compare_with".
122 integer is_prefix(string compare_with, string prefix)
123 { return find_substring(compare_with, prefix) == 0; }
124
125 // returns true if this object is a huffotronic updater of some sort.
126 integer inside_of_updater()
127 {
128     return find_substring(llGetObjectName(), "huffotronic") >= 0;
129 }
130
131 // stops all the scripts besides this one.
132 knock_around_other_scripts(integer running_state)
133 {
134     integer indy;
135     integer insider = inside_of_updater();
136     string self_script = llGetScriptName();
137     // we set all other scripts to the running state requested.
138     for (indy = 0; indy < llGetInventoryNumber(INVENTORY_SCRIPT); indy++) {
139         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, indy);
140         if ( (curr_script != self_script)
141             && (!insider || matches_server_script(curr_script)) ) {
142             // this one seems ripe for being set to the state requested.
143             llSetScriptState(curr_script, running_state);
144         }
145     }
146 }
147
148 string SERVER_SCRIPT = "huffotronic update server";
149     // the prefix of our server script that hands out updates.
150
151 // returns true if a script is a version of our update server.
152 integer matches_server_script(string to_check)
153 {
154     return is_prefix(to_check, SERVER_SCRIPT);
155 }
156
157 // end hufflets.
158 //////////////
159
160 default
161 {
162     state_entry()
163     {
164         auto_retire();
165         if (inside_of_updater()) return;  // do nothing else.
166         
167         llWhisper(0, "hello, i'm jeeves.  pleased to meet you.");
168         llSleep(32);
169         
170         string gotta_zap = "huff-update client v";
171         integer i;
172         for (i = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; i >= 0; i--) {
173             string cur = llGetInventoryName(INVENTORY_SCRIPT, i);
174             if (is_prefix(cur, gotta_zap)) {
175                 llWhisper(0, "i, jeeves, will now clean out this script: " + cur);
176                 llRemoveInventory(cur);
177             }
178         }
179         llSleep(14);
180         knock_around_other_scripts(TRUE);
181         llSleep(4);
182         llWhisper(0, "i'm sorry to say sir that i now must bid you adieu, as i am removing myself from the world.");
183         llSleep(1);
184         llRemoveInventory(llGetScriptName());
185     }
186 }
187