normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / time_zone_picker_v0.6.txt
1 
2 // huffware script: time zone picker, by fred huffhines.
3 //
4 // allows the user to select a different time zone than the default.
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 // constants...
11
12 // secret codes to control the time zone.
13 integer SECRET_TIME_ZONE_ADJUSTER_ID = -2091823;
14 string TIME_ZONE_ADJUST_COMMAND = "tzadjust";
15
16 // requires menutini v3.9 or better...
17 //////////////
18 // do not redefine these constants.
19 integer MENUTINI_HUFFWARE_ID = 10009;
20     // the unique id within the huffware system for the jaunt script to
21     // accept commands on.  this is used in llMessageLinked as the num parameter.
22 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
23     // this pattern is an uncommon thing to see in text, so we use it to separate
24     // our commands in link messages.
25 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
26     // used to separate lists of items from each other when stored inside a parameter.
27     // this allows lists to be passed as single string parameters if needed.
28 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
29 // commands available via menutini:
30 string SHOW_MENU_COMMAND = "#menu#";
31     // the command that tells menutini to show a menu defined by parameters
32     // that are passed along.  these must be: the menu name, the menu's title
33     // (which is really the info to show as content in the main box of the menu),
34     // the wrapped list of commands to show as menu buttons, the menu system
35     // channel's for listening, and the key to listen to.
36 //
37 //////////////
38 // joins a list of sub-items using the item sentinel for the library.
39 string wrap_item_list(list to_wrap)
40 { return llDumpList2String(to_wrap, HUFFWARE_ITEM_SEPARATOR); }
41 //
42 //////////////
43
44 integer random_channel() { return -(integer)(llFrand(40000) + 20000); }
45
46 simply_display_menu(string menu_name, string title, list buttons)
47 {
48     integer menu_channel = random_channel();
49     key listen_to = llGetOwner();
50     llMessageLinked(LINK_THIS, MENUTINI_HUFFWARE_ID, SHOW_MENU_COMMAND,
51         menu_name + HUFFWARE_PARM_SEPARATOR
52         + title + HUFFWARE_PARM_SEPARATOR
53         + wrap_item_list(buttons) + HUFFWARE_PARM_SEPARATOR
54         + (string)menu_channel + HUFFWARE_PARM_SEPARATOR
55         + (string)listen_to);
56 }
57
58 // handles the response message when the user chooses a button.
59 react_to_menu(string menu_name, string av_key, string which_choice)
60 {
61     log_it("You selected a time zone offset of " + which_choice);
62     llMessageLinked(LINK_SET, SECRET_TIME_ZONE_ADJUSTER_ID, TIME_ZONE_ADJUST_COMMAND, which_choice);
63 }
64
65 // processes a message from a link.  some of this must be handled
66 // by the driver script that handles our configuration.
67 handle_link_message(integer sender, integer num, string msg, key id)
68 {
69     if ( (num != MENUTINI_HUFFWARE_ID + REPLY_DISTANCE)
70         || (msg != SHOW_MENU_COMMAND) ) return;  // not for us.
71
72 //log_it("menu reply: sndr=" + (string)sender + " num=" + (string)num + " msg=" + msg + " id=" + (string)id);
73     list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
74     string menu_name = llList2String(parms, 0);
75     string which_choice = llList2String(parms, 1);
76     key av_key = llList2String(parms, 2);
77     react_to_menu(menu_name, av_key, which_choice);
78 }
79
80 //////////////
81 // from hufflets...
82 //
83 integer debug_num = 0;
84
85 // a debugging output method.  can be disabled entirely in one place.
86 log_it(string to_say)
87 {
88     debug_num++;
89     // tell this to the owner.    
90     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
91     // say this on open chat, but use an unusual channel.
92 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
93 }
94
95 //
96 // end hufflets
97 //////////////
98
99 //////////////
100 // huffware script: auto-retire, by fred huffhines, version 2.5.
101 // distributed under BSD-like license.
102 //   !!  keep in mind that this code must be *copied* into another
103 //   !!  script that you wish to add auto-retirement capability to.
104 // when a script has auto_retire in it, it can be dropped into an
105 // object and the most recent version of the script will destroy
106 // all older versions.
107 //
108 // the version numbers are embedded into the script names themselves.
109 // the notation for versions uses a letter 'v', followed by two numbers
110 // in the form "major.minor".
111 // major and minor versions are implicitly considered as a floating point
112 // number that increases with each newer version of the script.  thus,
113 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
114 // and "hazmap v3.2" is a more recent version.
115 //
116 // example usage of the auto-retirement script:
117 //     default {
118 //         state_entry() {
119 //            auto_retire();  // make sure newest addition is only version of script.
120 //        }
121 //     }
122 // this script is partly based on the self-upgrading scripts from markov brodsky
123 // and jippen faddoul.
124 //////////////
125 auto_retire() {
126     string self = llGetScriptName();  // the name of this script.
127     list split = compute_basename_and_version(self);
128     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
129     string basename = llList2String(split, 0);  // script name with no version attached.
130     string version_string = llList2String(split, 1);  // the version found.
131     integer posn;
132     // find any scripts that match the basename.  they are variants of this script.
133     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
134 //log_it("invpo=" + (string)posn);
135         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
136         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
137             // found a basic match at least.
138             list inv_split = compute_basename_and_version(curr_script);
139             if (llGetListLength(inv_split) == 2) {
140                 // see if this script is more ancient.
141                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
142                 // must make sure that the retiring script is completely the identical basename;
143                 // just matching in the front doesn't make it a relative.
144                 if ( (llList2String(inv_split, 0) == basename)
145                     && ((float)inv_version_string < (float)version_string) ) {
146                     // remove script with same name from inventory that has inferior version.
147                     llRemoveInventory(curr_script);
148                 }
149             }
150         }
151     }
152 }
153 //
154 // separates the base script name and version number.  used by auto_retire.
155 list compute_basename_and_version(string to_chop_up)
156 {
157     // minimum script name is 2 characters plus a version.
158     integer space_v_posn;
159     // find the last useful space and 'v' combo.
160     for (space_v_posn = llStringLength(to_chop_up) - 3;
161         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
162         space_v_posn--) {
163         // look for space and v but do nothing else.
164 //log_it("pos=" + (string)space_v_posn);
165     }
166     if (space_v_posn < 2) return [];  // no space found.
167 //log_it("space v@" + (string)space_v_posn);
168     // now we zoom through the stuff after our beloved v character and find any evil
169     // space characters, which are most likely from SL having found a duplicate item
170     // name and not so helpfully renamed it for us.
171     integer indy;
172     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
173 //log_it("indy=" + (string)space_v_posn);
174         if (llGetSubString(to_chop_up, indy, indy) == " ") {
175             // found one; zap it.  since we're going backwards we don't need to
176             // adjust the loop at all.
177             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
178 //log_it("saw case of previously redundant item, aieee.  flattened: " + to_chop_up);
179         }
180     }
181     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
182     // ditch the space character for our numerical check.
183     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
184     // strip out a 'v' if there is one.
185     if (llGetSubString(chop_suffix, 0, 0) == "v")
186         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
187     // if valid floating point number and greater than zero, that works for our version.
188     string basename = to_chop_up;  // script name with no version attached.
189     if ((float)chop_suffix > 0.0) {
190         // this is a big success right here.
191         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
192         return [ basename, chop_suffix ];
193     }
194     // seems like we found nothing useful.
195     return [];
196 }
197 //
198 //////////////
199
200 default
201 {
202     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
203     on_rez(integer parm) { state rerun; }
204 }
205 state rerun { state_entry() { state default; } }
206
207 state real_default
208 {
209     state_entry()
210     {
211         auto_retire();  // make sure newest addition is only version of script.
212     }
213
214     touch_start(integer touchers) {
215         if (llDetectedKey(0) != llGetOwner()) return;  // only listen to owner for setting time zone.
216         simply_display_menu("tz", "Select your time zone offset from GMT:\nGMT is 0, EST is -5, PST is -8, etc.",
217             [ -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] );
218     }
219
220     link_message(integer sender, integer num, string msg, key id)
221     { handle_link_message(sender, num, msg, id); }
222 }