normalized perms on all files, to avoid relying on any stored executable bits in...
[feisty_meow.git] / huffware / huffotronic_scripts / noteworthy_example_usage_v3.6.txt
1 
2 // huffware script: noteworthy example usage, by fred huffhines
3 //
4 // shows how to use the noteworthy script as a configuration helper.
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 // global constants...
11
12 string EXAMPLE_NOTEWORTHY_SIGNATURE = "#example_noteworthy";
13     // the notecard must begin with this as its first line for it to be
14     // recognized as our configuration card.
15
16 // global variables...
17
18 string global_notecard_name;  // name of our notecard in the object's inventory.
19 integer response_code;  // set to uniquely identify the notecard read in progress.
20 list global_config_list;  // a collection of configuration parameters from our notecard.
21 integer global_config_index;  // allows wrap-around feature, which we don't use here.
22
23 // requires noteworthy library v8.3 or better.
24 //////////////
25 // do not redefine these constants.
26 integer NOTEWORTHY_HUFFWARE_ID = 10010;
27     // the unique id within the huffware system for the noteworthy script to
28     // accept commands on.  this is used in llMessageLinked as the num parameter.
29 string HUFFWARE_PARM_SEPARATOR = "{~~~}";
30     // this pattern is an uncommon thing to see in text, so we use it to separate
31     // our commands in link messages.
32 string HUFFWARE_ITEM_SEPARATOR = "{|||}";
33     // used to separate lists of items from each other when stored inside a parameter.
34     // this allows lists to be passed as single string parameters if needed.
35 integer REPLY_DISTANCE = 100008;  // offset added to service's huffware id in reply IDs.
36 //////////////
37 string BAD_NOTECARD_INDICATOR = "bad_notecard";
38     // indicates that the notecard reading process has failed to find an appropriate one.
39 string BUSY_READING_INDICATOR = "busy_already";
40     // this return value indicates that the script is already in use by some other script.
41     // the calling script should try again later.
42 string NOTECARD_READ_CONTINUATION = "continue!";
43     // returned as first parameter if there is still more data to handle.
44 // commands available via the noteworthy library:
45 string READ_NOTECARD_COMMAND = "#read_note#";
46     // command used to tell the script to read notecards.  needs a signature to find
47     // in the card as the first parameter, and a randomly generated response code for
48     // the second parameter.  the response code is used to uniquely identify a set of
49     // pending notecard readings (hopefully).  the signature can be empty or missing.
50     // the results will be fired back as the string value returned, which will have
51     // as first element the notecard's name (or "bad_notecard" if none was found) and
52     // as subsequent elements an embedded list that was read from the notecard.  this
53     // necessarily limits the size of the notecards that we can read and return.
54 //
55 //////////////
56 // joins a list of parameters using the parameter sentinel for the library.
57 string wrap_parameters(list to_flatten)
58 { return llDumpList2String(to_flatten, HUFFWARE_PARM_SEPARATOR); }
59 //////////////
60
61 // this function fires off a request to the noteworthy library via a link message.
62 // noteworthy will look for a notecard with our particular signature in it and
63 // if it finds one, it will read the configuration therein.  an empty string is
64 // returned if noteworthy couldn't find anything.
65 request_configuration()
66 {
67     global_notecard_name = "";  // reset any previous card.
68     // try to find a notecard with our configuration.
69     response_code = -1 * (integer)randomize_within_range(23, 80000, FALSE);
70     string parms_sent = wrap_parameters([EXAMPLE_NOTEWORTHY_SIGNATURE, response_code]);
71     llMessageLinked(LINK_THIS, NOTEWORTHY_HUFFWARE_ID, READ_NOTECARD_COMMAND,
72          parms_sent);
73 }
74
75 // processes link messages received from the noteworthy library.
76 handle_link_message(integer which, integer num, string msg, key id)
77 {
78     if ( (num != NOTEWORTHY_HUFFWARE_ID + REPLY_DISTANCE)
79             || (msg != READ_NOTECARD_COMMAND) )
80         return;  // not for us.
81     // process the result of reading the notecard.
82     list parms = llParseString2List(id, [HUFFWARE_PARM_SEPARATOR], []);
83     string notecard_name = llList2String(parms, 0);
84     integer response_for = llList2Integer(parms, 1);
85     if (response_for != response_code) return;  // oops, this isn't for us.
86     if (notecard_name == BAD_NOTECARD_INDICATOR) {
87         // we hated the notecards we found, or there were none.
88         log_it("We apologize; there seem to be no notecards with a first line of '"
89             + EXAMPLE_NOTEWORTHY_SIGNATURE
90             + "'.  We can't read any configuration until that situation improves.");
91     } else {
92         // snag all but the first two elements for our config now.
93         global_config_list += llList2List(parms, 2, -1);
94         // make sure we shouldn't keep going.
95         if (notecard_name != NOTECARD_READ_CONTINUATION) {
96             // a valid notecard has been found.
97             global_notecard_name = notecard_name;  // record its name for later use.
98             global_config_index = 0;  // we are starting over in the config list.
99             // now echo the card we received...
100             log_it("read notecard \"" + global_notecard_name + "\":");
101             integer lines_to_say = llGetListLength(global_config_list);
102             if (lines_to_say > 8)
103                 lines_to_say = 8;  // limit how much text is spoken.
104             integer indy;
105             for (indy = 0; indy < lines_to_say; indy++)
106                 log_it("line #" + (string)(indy + 1) + ": " + llList2String(global_config_list, indy));
107             if (lines_to_say != llGetListLength(global_config_list))
108                 log_it("...and so forth....");
109             // and process the file as a set of definitions.
110             process_ini_config();
111         }
112     }
113 }
114
115 ///////////////
116
117 // consumes the notecard in a very application specific way to retrieve our configuration items.
118 // the example script only looks for two variables: name and description.  if those are found in
119 // the sample card, then they are proudly shown.
120 parse_variable_definition(string to_parse)
121 {
122 //    string content;  // filled after finding a variable name.
123 //    if ( (content = get_variable_value(to_parse, "name")) != "")
124 //        log_it("** got a name of '" + content + "'");
125 //    else if ( (content = get_variable_value(to_parse, "description")) != "")
126 //        log_it("** got a description of '" + content + "'");
127 }
128
129 initialize()
130 {
131     // reset our relevant variables.
132     global_notecard_name = "";
133     global_config_list = [];
134     global_config_index = 0;
135
136     // announce that we're open for business.
137     log_it("example noteworthy usage is started, free mem=" + (string)llGetFreeMemory());
138
139     // request that the noteworthy library start looking for our notecard.
140     request_configuration();
141 }
142
143 //////////////
144 // from hufflets...
145 //
146
147 integer debug_num = 0;
148
149 // a debugging output method.  can be disabled entirely in one place.
150 log_it(string to_say)
151 {
152     debug_num++;
153     // tell this to the owner.    
154     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
155     // say this on an unusual channel for chat if it's not intended for general public.
156 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
157     // say this on open chat that anyone can hear.  we take off the bling for this one.
158 //    llSay(0, to_say);
159 }
160
161 // locates the string "text" in the list to "search_in".
162 integer find_in_list(list search_in, string text)
163
164     integer len = llGetListLength(search_in);
165     integer i; 
166     for (i = 0; i < len; i++) { 
167         if (llList2String(search_in, i) == text) 
168             return i; 
169     } 
170     return -1;
171 }
172
173 // returns TRUE if the "prefix" string is the first part of "compare_with".
174 integer is_prefix(string compare_with, string prefix)
175 { return (llSubStringIndex(compare_with, prefix) == 0); }
176
177 // returns a number at most "maximum" and at least "minimum".
178 // if "allow_negative" is TRUE, then the return may be positive or negative.
179 float randomize_within_range(float minimum, float maximum, integer allow_negative)
180 {
181     if (minimum > maximum) {
182         // flip the two if they are reversed.
183         float temp = minimum; minimum = maximum; maximum = temp;
184     }
185     float to_return = minimum + llFrand(maximum - minimum);
186     if (allow_negative) {
187         if (llFrand(1.0) < 0.5) to_return *= -1.0;
188     }
189     return to_return;
190 }
191
192 // strips the spaces off of the beginning and end of a string.
193 string strip_spaces(string to_strip)
194 {
195     // clean out initial spaces.
196     while (llGetSubString(to_strip, 0, 0) == " ")
197         to_strip = llDeleteSubString(to_strip, 0, 0);
198     // clean out ending spaces.
199     while (llGetSubString(to_strip, -1, -1) == " ")
200         to_strip = llDeleteSubString(to_strip, -1, -1);
201     return to_strip;
202 }
203
204 // parses a variable definition to find the name of the variable and its value.
205 // this returns two strings [X, Y], if "to_split" is in the form X=Y.
206 list separate_variable_definition(string to_split)
207 {
208     integer equals_indy = llSubStringIndex(to_split, "=");
209     // we don't support missing an equals sign, and we don't support it as the first character.
210     if (equals_indy <= 0) return [];  // no match.
211     string x = llGetSubString(to_split, 0, equals_indy - 1);
212     string y = llGetSubString(to_split, equals_indy + 1, -1);
213     to_split = "";  // save space.
214     return [ strip_spaces(x), strip_spaces(y) ];
215 }
216
217 // returns a non-empty string if "to_check" defines a value for "variable_name".
218 // this must be in the form "X=Y", where X is the variable_name and Y is the value.
219 string get_variable_value(string to_check, string variable_name)
220 {
221     list x_y = separate_variable_definition(to_check);
222     if (llGetListLength(x_y) != 2) return "";  // failure to parse a variable def at all.
223     if (!is_prefix(llList2String(x_y, 0), variable_name)) return "";  // no match.
224     return llList2String(x_y, 1);  // a match!
225 }
226
227 // examines all entries that we got from the notecard to see if any contain definitions.
228 // this is basically an INI file reader, but it uses a list instead of a file.
229 // ini files provide a format with multiple sections of config information, like so:
230 //    [section_1]
231 //    name1=value1
232 //    name2=value2 ...etc...
233 //    [section_2]
234 //    name1=value1 ...etc...
235 process_ini_config()
236 {
237     log_it("scanning notecard for variable definitions...");
238     integer indy;
239     integer count = llGetListLength(global_config_list);
240     string section_name;  // set later if we see one.
241
242     // iterate across the items in our configuration to look for ones that are not done yet.            
243     for (indy = global_config_index; indy < count; indy++) {
244         string line = llList2String(global_config_list, indy);
245         // search for a section beginning.
246         if (llGetSubString(line, 0, 0) == "[") {
247             // we found the start of a section name.  now read the contents.
248             indy++;  // skip section line.
249             section_name = llGetSubString(line, 1, -2);
250             log_it("reading section: " + section_name);
251         }
252         integer sec_indy;
253         for (sec_indy = indy; sec_indy < count; sec_indy++) {
254             // read the lines in the section.
255             line = llList2String(global_config_list, sec_indy);
256             if (llGetSubString(line, 0, 0) != "[") {
257                 // try to interpret this line as a variable setting.  this is just
258                 // one example of a way to handle the config file; one might instead
259                 // want to do something below once a whole section is read.
260                 parse_variable_definition(line);
261                 indy = sec_indy;  // track that we've passed this line.
262             } else {
263                 // we're at the beginning of a new section now, so start processing its
264                 // configuration in the outer loop.
265                 indy = sec_indy - 1;  // set indy to proper beginning of section.
266                 global_config_index = indy;  // remember where we had read to.
267                 sec_indy = count + 3;  // skip remainder of inner loop.
268             }
269         }
270     }
271
272     global_config_index = 0;  // reset outer position if want to re-read.
273 }
274
275 // locates the item with "name_to_find" in the inventory items with the "type".
276 // a value from 0 to N-1 is returned if it's found, where N is the number of
277 // items in the inventory.
278 integer find_in_inventory(string name_to_find, integer inv_type)
279 {
280     integer num_inv = llGetInventoryNumber(inv_type);
281     if (num_inv == 0) return -1;  // nothing there!
282     integer inv;
283     for (inv = 0; inv < num_inv; inv++) {
284         if (llGetInventoryName(inv_type, inv) == name_to_find)
285             return inv;
286     }
287     return -2;  // failed to find it.
288 }
289 // end hufflets
290 //////////////
291
292 default
293 {
294     state_entry() { if (llSubStringIndex(llGetObjectName(),  "huffotronic") < 0) state real_default; }
295     on_rez(integer parm) { state rerun; }
296 }
297 state rerun { state_entry() { state default; } }
298
299 state real_default
300 {
301     state_entry()
302     {
303         initialize();
304     }
305     
306     on_rez(integer parm) { llResetScript(); }
307     
308     touch_start(integer count) {
309         log_it("re-initializing script now to read card again...");
310         initialize();
311     }
312
313     // reset when we see changes to our notecard configuration.
314     changed(integer change) {
315         if (change & CHANGED_INVENTORY) {
316             llSleep(3.14159265358);  // delay to avoid interfering with upgrade.
317             llResetScript(); 
318         }
319     }
320     
321     // process the response from the noteworthy library.
322     link_message(integer which, integer num, string msg, key id)
323     { handle_link_message(which, num, msg, id); }
324 }