16d198a2c6c2a2b6dcb04f9f13e5fba8063b5df2
[feisty_meow.git] / huffware / huffotronic_tools_n_testers_v6.1 / email_notecards_v6.0.txt
1 
2 // huffware script: email notecards, by fred huffhines.
3 //
4 // this script allows us to export notecards out of the grids.  it reads every notecard
5 // that's dropped into the object, and then emails it to the specified recipient.
6 //
7 // this script is licensed by the GPL v3 which is documented at: http://www.gnu.org/licenses/gpl.html
8 // do not use it in objects without fully realizing you are implicitly accepting that license.
9
10
11 //hmmm: need a command to clean up the inventory...
12 //      among things that are not notecards, we need a way to clean up all inventory
13 //      except the huffotronic updater and this script itself.
14
15
16 // configure these for your own purposes:
17
18 // the list of emails that should receive the notecard content.
19 list EMAIL_LIST_FOR_NOTECARDS = [
20     "fred@spork.com"
21 ];
22
23 // global constants...
24
25 integer DEBUGGING = FALSE;  // if true, will make the run a lot noisier.
26
27 float TIMER_PERIOD = 2.0;  // period for the timer to check on any pending notecards.
28
29 // we'll keep our emails at or under this length.
30 integer MAXIMUM_STRING_BUFFER = 3200;
31   // older versions of opensim have email limit of 1024 for sum of subject and body length.
32   // the documented behavior for lsl email is a max of 4096 for sum of subject and body length.
33
34 vector TEXT_COLOR = <0.9, 0.7, 0.5>;  // the color the object's text will be painted with.
35
36 // global variables....
37
38 key current_query_id = NULL_KEY;  // the query ID for the current notecard.
39
40 integer line_number;  // which line are we at in notecard?
41
42 string current_notecard_name;  // we are reading a notecard named this, possibly.
43
44 list pending_notecards;  // notecards that are going to be read eventually.
45
46 integer processing_a_notecard = FALSE;  // are we currently reading and emailing a notecard?
47
48 integer need_to_scan_notecards = FALSE;  // should the list of notecards be re-scanned?
49
50 /////
51 //hmmm: add to hufflets...
52
53 // basic email sending, with some extra info passed along.
54 send_email(string recipient, string subject, string content)
55 {
56     if (DEBUGGING) log_it("before sending an email: " + subject);
57     llEmail(recipient, subject + " [via " + llKey2Name(llGetOwner()) + "]", content);
58     if (DEBUGGING) log_it("after sending an email: " + subject);
59 }
60
61 list __email_buffer;  // contents to send in email should pile up here.
62
63 // sends out all the email that's pending in the __email_buffer variable.
64 send_buffered_email(string subject)
65 {
66     string temp_buffer;  // used to build up the email.
67     integer chunk_number = 1;  // tracks which part of the message this is.
68     integer i;
69     for (i = 0; i < llGetListLength(EMAIL_LIST_FOR_NOTECARDS); i++) {
70         string recip = llList2String(EMAIL_LIST_FOR_NOTECARDS, i);
71         temp_buffer = "";
72         integer j;
73         for (j = 0; j < llGetListLength(__email_buffer); j++) {
74             string line = llList2String(__email_buffer, j);        
75             if (llStringLength(temp_buffer) + llStringLength(line) >= MAXIMUM_STRING_BUFFER) {
76                 // we're over our limit per email.  send out what we have and
77                 // then freshen our buffer.
78                 if (DEBUGGING)
79                     log_it("part " + chunk_number + " is " + llStringLength(temp_buffer) + " bytes.");
80                 send_email(recip, subject + " (part " + chunk_number + ")", temp_buffer);
81                 temp_buffer = "";
82                 chunk_number++;
83             }
84             temp_buffer += llList2String(__email_buffer, j);
85         }
86         if (llStringLength(temp_buffer) > 0) {
87             // send the last piece of the email out.
88             if (DEBUGGING)
89                 log_it("part " + chunk_number + " is " + llStringLength(temp_buffer) + " bytes.");
90             send_email(recip, subject + " (part " + chunk_number + ")", temp_buffer);
91         }
92     }
93     __email_buffer = [];  // clear the buffer now that the email is flying out.
94 }
95 ////// add to hufflets end.
96 ////
97
98
99 // reset any important variables and set up our assets and timers anat.
100 initialize()
101 {
102     current_query_id = NULL_KEY;
103     __email_buffer = [];
104     line_number = 0;
105         
106     llAllowInventoryDrop(TRUE);  // allow people to drop things into us.
107
108     // slap a title on the object.
109     string recip_list;
110     integer i;
111     for (i = 0; i < llGetListLength(EMAIL_LIST_FOR_NOTECARDS); i++) {
112         recip_list += "\n" + llList2String(EMAIL_LIST_FOR_NOTECARDS, i);
113     }
114     llSetText("Drop notecards into me\nto email them to:" + recip_list, TEXT_COLOR, 1.0);
115         
116     // set the timer to processing notecards that are pending.
117     reset_timer(TIMER_PERIOD);
118 }
119
120 //hmmm: add to hufflets?
121 // safely reschedules the timer for a duration specified.
122 // this gets around a gnarly SL bug where the timer stops working if not stopped first.
123 reset_timer(float period)
124 {
125     llSetTimerEvent(0.0);
126     llSetTimerEvent(period);
127 }
128
129 // starts reading the current notecard name, which should have been set elsewhere.
130 consume_notecard()
131 {
132     if (current_notecard_name == "") {
133         log_it("somehow we do not have a notecard to read in consume_notecard.");
134         return;
135     }
136     line_number = 0;
137     __email_buffer = [];
138     current_query_id = llGetNotecardLine(current_notecard_name, 0);    
139 }
140
141 // when the timer goes off, this checks our ongoing processes and kicks them
142 // down the road a bit if they need it.
143 handle_timer_pong()
144 {
145     if (processing_a_notecard) return;  // already busy.
146     if (need_to_scan_notecards) {
147         queue_up_any_new_notecards();
148         need_to_scan_notecards = FALSE;
149     }
150
151     if (llGetListLength(pending_notecards) < 1) return;  // nothing to do.
152     processing_a_notecard = TRUE;
153     current_notecard_name = llList2String(pending_notecards, 0);
154     if (DEBUGGING) log_it("scheduling notecard: " + current_notecard_name);            
155     consume_notecard();
156 }
157
158 // processes the data chunks coming in from our notecard reading.
159 handle_data_arriving(key query_id, string data)
160 {
161     if (query_id != current_query_id) {
162         if (DEBUGGING) log_it("not our query id somehow?");
163         return;
164     }
165     // if we're not at the end of the notecard we're reading...
166     if (data != EOF) {
167         if (!line_number) {
168             if (DEBUGGING) log_it("starting to read notecard " + current_notecard_name + "...");    
169         }
170         // add the line to our destination list.
171         if (is_prefix(data, "From")) data = "#" + data;  // voodoo.
172         __email_buffer += [ data + "\n" ];
173         if (DEBUGGING) log_it("line " + (string)line_number + ": data=" + data);
174         line_number++;  // increase the line count.
175         // request the next line from the notecard.
176         current_query_id = llGetNotecardLine(current_notecard_name, line_number);
177     } else {
178         // no more data, so we're done with this card.
179         current_query_id = NULL_KEY;
180         // blast out the notecard's content in an email.
181         llSay(0, "Sending \"" + current_notecard_name + "\" with " + line_number + " lines.\n");
182         send_buffered_email(current_notecard_name);
183         // chop that name out of our current pending cards too.
184         integer where = llListFindList(pending_notecards, [current_notecard_name]);
185 //log_it("found the notecard to remove at index " + where);        
186         if (where >= 0) {
187             pending_notecards = llDeleteSubList(pending_notecards, where, where);
188         }
189         // done with it, so eat the current note card.
190         llRemoveInventory(current_notecard_name);
191         llSay(0, "Done with notecard \"" + current_notecard_name + "\"; now removing it.");
192         // reset our flag to signal that we're ready to eat another notecard.
193         processing_a_notecard = FALSE;        
194         current_notecard_name = "";
195         // make sure we have all current notecards queued.
196         need_to_scan_notecards = TRUE;
197         // push timer out.
198         reset_timer(TIMER_PERIOD);
199     }
200 }
201
202 // look through our inventory and if there are any notecards we don't know about,
203 // add them to the list for processing.
204 queue_up_any_new_notecards()
205 {
206     integer i;
207     for (i = 0; i < llGetInventoryNumber(INVENTORY_NOTECARD); i++) {
208         string note_name = llGetInventoryName(INVENTORY_NOTECARD, i);
209         // don't add the notecard if the name is already listed.
210         integer where = llListFindList(pending_notecards, [ note_name ]);
211         if (where >= 0) {
212 //            if (DEBUGGING) log_it("notecard already present; skipping: " + note_name);
213         } else {
214             // schedule notecard reading by adding to queue.
215             pending_notecards += [ note_name ];
216 //            if (DEBUGGING) log_it("notecard added to pending: " + note_name);
217         }
218     }
219 }
220
221 //////////////
222
223 // borrowed from hufflets...
224
225 integer debug_num = 0;
226
227 // a debugging output method.  can be disabled entirely in one place.
228 log_it(string to_say)
229 {
230     debug_num++;
231     // tell this to the owner.    
232     llOwnerSay(llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
233     // say this on an unusual channel for chat if it's not intended for general public.
234 //    llSay(108, llGetScriptName() + "[" + (string)debug_num + "] " + to_say);
235     // say this on open chat that anyone can hear.  we take off the bling for this one.
236 //    llSay(0, to_say);
237 }
238
239 //////////////
240
241 // the string processing methods are not case sensitive.
242   
243 // returns TRUE if the "pattern" is found in the "full_string".
244 integer matches_substring(string full_string, string pattern)
245 { return (find_substring(full_string, pattern) >= 0); }
246
247 // returns the index of the first occurrence of "pattern" inside
248 // the "full_string".  if it is not found, then a negative number is returned.
249 integer find_substring(string full_string, string pattern)
250 { return llSubStringIndex(llToLower(full_string), llToLower(pattern)); }
251
252 // returns TRUE if the "prefix" string is the first part of "compare_with".
253 integer is_prefix(string compare_with, string prefix)
254 { return find_substring(compare_with, prefix) == 0; }
255
256 //////////////
257
258 //////////////
259 // huffware script: auto-retire, by fred huffhines, version 2.8.
260 // distributed under BSD-like license.
261 //   !!  keep in mind that this code must be *copied* into another
262 //   !!  script that you wish to add auto-retirement capability to.
263 // when a script has auto_retire in it, it can be dropped into an
264 // object and the most recent version of the script will destroy
265 // all older versions.
266 //
267 // the version numbers are embedded into the script names themselves.
268 // the notation for versions uses a letter 'v', followed by two numbers
269 // in the form "major.minor".
270 // major and minor versions are implicitly considered as a floating point
271 // number that increases with each newer version of the script.  thus,
272 // "hazmap v0.1" might be the first script in the "hazmap" script continuum,
273 // and "hazmap v3.2" is a more recent version.
274 //
275 // example usage of the auto-retirement script:
276 //     default {
277 //         state_entry() {
278 //            auto_retire();  // make sure newest addition is only version of script.
279 //        }
280 //     }
281 // this script is partly based on the self-upgrading scripts from markov brodsky
282 // and jippen faddoul.
283 //////////////
284 auto_retire() {
285     string self = llGetScriptName();  // the name of this script.
286     list split = compute_basename_and_version(self);
287     if (llGetListLength(split) != 2) return;  // nothing to do for this script.
288     string basename = llList2String(split, 0);  // script name with no version attached.
289     string version_string = llList2String(split, 1);  // the version found.
290     integer posn;
291     // find any scripts that match the basename.  they are variants of this script.
292     for (posn = llGetInventoryNumber(INVENTORY_SCRIPT) - 1; posn >= 0; posn--) {
293         string curr_script = llGetInventoryName(INVENTORY_SCRIPT, posn);
294         if ( (curr_script != self) && (llSubStringIndex(curr_script, basename) == 0) ) {
295             // found a basic match at least.
296             list inv_split = compute_basename_and_version(curr_script);
297             if (llGetListLength(inv_split) == 2) {
298                 // see if this script is more ancient.
299                 string inv_version_string = llList2String(inv_split, 1);  // the version found.
300                 // must make sure that the retiring script is completely the identical basename;
301                 // just matching in the front doesn't make it a relative.
302                 if ( (llList2String(inv_split, 0) == basename)
303                     && ((float)inv_version_string < (float)version_string) ) {
304                     // remove script with same name from inventory that has inferior version.
305                     llRemoveInventory(curr_script);
306                 }
307             }
308         }
309     }
310 }
311 //
312 // separates the base script name and version number.  used by auto_retire.
313 list compute_basename_and_version(string to_chop_up)
314 {
315     // minimum script name is 2 characters plus a version.
316     integer space_v_posn;
317     // find the last useful space and 'v' combo.
318     for (space_v_posn = llStringLength(to_chop_up) - 3;
319         (space_v_posn >= 2) && (llGetSubString(to_chop_up, space_v_posn, space_v_posn + 1) != " v");
320         space_v_posn--) {
321         // look for space and v but do nothing else.
322     }
323     if (space_v_posn < 2) return [];  // no space found.
324     // now we zoom through the stuff after our beloved v character and find any evil
325     // space characters, which are most likely from SL having found a duplicate item
326     // name and not so helpfully renamed it for us.
327     integer indy;
328     for (indy = llStringLength(to_chop_up) - 1; indy > space_v_posn; indy--) {
329         if (llGetSubString(to_chop_up, indy, indy) == " ") {
330             // found one; zap it.  since we're going backwards we don't need to
331             // adjust the loop at all.
332             to_chop_up = llDeleteSubString(to_chop_up, indy, indy);
333         }
334     }
335     string full_suffix = llGetSubString(to_chop_up, space_v_posn, -1);
336     // ditch the space character for our numerical check.
337     string chop_suffix = llGetSubString(full_suffix, 1, llStringLength(full_suffix) - 1);
338     // strip out a 'v' if there is one.
339     if (llGetSubString(chop_suffix, 0, 0) == "v")
340         chop_suffix = llGetSubString(chop_suffix, 1, llStringLength(chop_suffix) - 1);
341     // if valid floating point number and greater than zero, that works for our version.
342     string basename = to_chop_up;  // script name with no version attached.
343     if ((float)chop_suffix > 0.0) {
344         // this is a big success right here.
345         basename = llGetSubString(to_chop_up, 0, -llStringLength(full_suffix) - 1);
346         return [ basename, chop_suffix ];
347     }
348     // seems like we found nothing useful.
349     return [];
350 }
351 //
352 //////////////
353
354 // end from hufflets.
355 //////////////
356
357 default {
358     state_entry() { if (llSubStringIndex(llGetObjectName(), "huffotronic") < 0) state real_default; }
359     on_rez(integer parm) { state rerun; }
360 }
361 state rerun { state_entry() { state default; } }
362
363 state real_default
364 {
365     state_entry() {
366         auto_retire();
367         initialize();
368     }
369     
370     changed(integer mask) {
371         if (mask & (CHANGED_ALLOWED_DROP | CHANGED_INVENTORY)) {
372             need_to_scan_notecards = TRUE;
373             pending_notecards = [];  // reset the list so we re-add all and drop any dead ones.
374             reset_timer(TIMER_PERIOD);
375         }
376     }
377     
378     timer() { handle_timer_pong(); }
379     
380     dataserver(key query_id, string data) { handle_data_arriving(query_id, data); }        
381 }